Added natural delay/frequency settings for timers.

This commit is contained in:
lephe 2016-12-22 19:47:05 +01:00
parent e6e0989436
commit 2564096a13
8 changed files with 65 additions and 24 deletions

1
TODO
View File

@ -10,7 +10,6 @@ Simple improvements:
- display: Rectangle-based drawing functions
- tales: Test all font encodings
- time: Compute CLOCKS_PER_SEC
- timer: Add duration and frequency settings
- core: Add VBR handlers debugging information (if possible)
- core: Implement all callbacks and a complete user API
- project: Enhance Makefile install_lib rules

View File

@ -177,7 +177,7 @@ void debug(void)
dtext("ML...", 2, 2);
dupdate();
timer_start(TIMER_USER, 0x0fffffff, TIMER_Po_4, NULL, 0);
timer_start2(TIMER_USER, 0x0fffffff, TIMER_Po_4, NULL, 0);
for(i = 0; i < 1000; i++) ML_bmp_or_cl(screen, 1, 1, 128, 64);
time1 = timer->TCNT;
timer_stop(TIMER_USER);
@ -187,7 +187,7 @@ void debug(void)
dtext("gint...", 2, 2);
dupdate();
timer_start(TIMER_USER, 0x0fffffff, TIMER_Po_4, NULL, 0);
timer_start2(TIMER_USER, 0x0fffffff, TIMER_Po_4, NULL, 0);
for(i = 0; i < 1000; i++) dimage(&res_screen_start, 1, 1);
time2 = timer->TCNT;
timer_stop(TIMER_USER);

View File

@ -35,8 +35,7 @@ static void timing_timer(void)
static void timing_start(void)
{
timer_start(TIMER_USER, clock_setting(16, Clock_Hz), TIMER_Po_4,
timing_timer, 0);
timer_start(TIMER_USER, 16, Clock_Hz, timing_timer, 0);
rtc_cb_edit(cb_id, RTCFreq_16Hz, timing_rtc);
elapsed_timer = 0;

View File

@ -46,17 +46,32 @@
Configures and starts a timer. The timer argument expects a timer name.
You can use TIMER_USER anytime. You may also use TIMER_GRAY if you're
not running the gray engine.
The delay is in clock counts unit. The possible values for the
prescaler are dividers of the peripheral clock frequency Po:
Unit names are defined in the clock.h header and must be one of the
following:
- Clock_us (microseconds)
- Clock_ms (milliseconds)
- Clock_s (seconds)
- Clock_Hz (hertz)
- Clock_kHz (kilohertz)
- Clock_MHz (megahertz)
The number of repeats may to set to 0. In this case, the timer will not
stop until timer_stop() is explicitly called.
*/
void timer_start(int timer, int delay_or_frequency, enum ClockUnit unit,
void (*callback)(void), int repeats);
/*
timer_start2()
Basically the same as timer_start(), but uses a clock-count delay and a
prescaler. The possible values for the prescaler are dividers of the
peripheral clock frequency Po:
- TIMER_Po_4
- TIMER_Po_16
- TIMER_Po_64
- TIMER_Po_256
- TIMER_TCLK
The number of repeats may to set to 0. In this case, the timer will not
stop until timer_stop() is explicitly called.
*/
void timer_start(int timer, int delay, int prescaler, void (*callback)(void),
void timer_start2(int timer, int delay, int prescaler, void (*callback)(void),
int repeats);
/*
@ -68,10 +83,17 @@ void timer_stop(int timer);
/*
timer_reload()
Reloads the given timer with the given constant. Starts the timer if
it was stopped. The new delay uses the same unit as in timer_start().
Reloads the given timer with the supplied constant. Starts the timer if
it was stopped.
*/
void timer_reload(int timer, int new_delay);
void timer_reload(int timer, int new_delay_or_frequency, enum ClockUnit unit);
/*
timer_reload2()
Same as timer_reload(), but again uses the native clock count. The
prescaler may not be changed.
*/
void timer_reload2(int timer, int new_delay);

View File

@ -106,8 +106,7 @@ static void sleep_us_callback(void)
void sleep_us(int us_delay)
{
sleep_us_done = 0;
timer_start(TIMER_USER, clock_setting(us_delay, Clock_us), TIMER_Po_4,
sleep_us_callback, 1);
timer_start(TIMER_USER, us_delay, Clock_us, sleep_us_callback, 1);
do sleep();
while(!sleep_us_done);
}
@ -122,8 +121,8 @@ void sleep_us(int us_delay)
static volatile int clock_measure_done = 0;
// Once again SH7705 and SH7305 need different methods...
static int cb_id_7705 = -1;
static void clock_measure_7705();
static void clock_compute_7305();
static void clock_measure_7705(void);
static void clock_compute_7305(void);
/*
clock_measure()

View File

@ -36,7 +36,7 @@ void gray_start(void)
{
if(runs) return;
timer_start(TIMER_GRAY, delays[0], GRAY_PRESCALER, gray_interrupt, 0);
timer_start2(TIMER_GRAY, delays[0], GRAY_PRESCALER, gray_interrupt, 0);
current &= 1;
runs = 1;
}
@ -136,7 +136,7 @@ inline void gupdate(void)
*/
void gray_interrupt(void)
{
timer_reload(TIMER_GRAY, delays[(~current) & 1]);
timer_reload2(TIMER_GRAY, delays[(~current) & 1]);
screen_display(vrams[current]);
current ^= 1;
}

View File

@ -3,10 +3,20 @@
/*
timer_reload()
Reloads the given timer with the given constant. Starts the timer if
Reloads the given timer with the supplied constant. Starts the timer if
it was stopped.
*/
void timer_reload(int timer, int new_delay)
void timer_reload(int timer, int new_delay_or_frequency, enum ClockUnit unit)
{
timer_reload2(timer, clock_setting(new_delay_or_frequency, unit));
}
/*
timer_reload2()
Same as timer_reload(), but again uses the native clock count. The
prescaler may not be changed.
*/
void timer_reload2(int timer, int new_delay)
{
volatile struct mod_tmu *tmu;
volatile unsigned char *tstr;

View File

@ -2,10 +2,10 @@
#include <timer.h>
/*
timer_start()
Configures and starts a timer.
timer_start2()
Configures and starts a time using a clock count and a prescaler.
*/
void timer_start(int timer, int delay, int prescaler, void (*callback)(void),
void timer_start2(int timer, int delay, int prescaler, void (*callback)(void),
int repeats)
{
volatile struct mod_tmu *tmu;
@ -33,3 +33,15 @@ void timer_start(int timer, int delay, int prescaler, void (*callback)(void),
// Starting the timer.
*tstr |= byte;
}
/*
timer_start()
Configures and starts a timer using a delay, or a frequency, and the
associated unit.
*/
void timer_start(int timer, int delay, enum ClockUnit unit,
void (*callback)(void), int repeats)
{
timer_start2(timer, clock_setting(delay, unit), TIMER_Po_4, callback,
repeats);
}