From 2564096a13cb8097397bb4f227d597f0dbf321cc Mon Sep 17 00:00:00 2001 From: lephe Date: Thu, 22 Dec 2016 19:47:05 +0100 Subject: [PATCH] Added natural delay/frequency settings for timers. --- TODO | 1 - demo/gintdemo.c | 4 ++-- demo/test_timer.c | 3 +-- include/timer.h | 38 ++++++++++++++++++++++++++++++-------- src/clock/clock.c | 7 +++---- src/gray/gray_engine.c | 4 ++-- src/timer/timer_reload.c | 14 ++++++++++++-- src/timer/timer_start.c | 18 +++++++++++++++--- 8 files changed, 65 insertions(+), 24 deletions(-) diff --git a/TODO b/TODO index a176eda..25baea0 100644 --- a/TODO +++ b/TODO @@ -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 diff --git a/demo/gintdemo.c b/demo/gintdemo.c index 7284c35..114aa13 100644 --- a/demo/gintdemo.c +++ b/demo/gintdemo.c @@ -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); diff --git a/demo/test_timer.c b/demo/test_timer.c index 36ca64d..7178be4 100644 --- a/demo/test_timer.c +++ b/demo/test_timer.c @@ -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; diff --git a/include/timer.h b/include/timer.h index f896c4a..34fe987 100644 --- a/include/timer.h +++ b/include/timer.h @@ -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); diff --git a/src/clock/clock.c b/src/clock/clock.c index bc16418..28230be 100644 --- a/src/clock/clock.c +++ b/src/clock/clock.c @@ -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() diff --git a/src/gray/gray_engine.c b/src/gray/gray_engine.c index 639c750..893897f 100644 --- a/src/gray/gray_engine.c +++ b/src/gray/gray_engine.c @@ -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; } diff --git a/src/timer/timer_reload.c b/src/timer/timer_reload.c index 8cc047e..7d6bbe8 100644 --- a/src/timer/timer_reload.c +++ b/src/timer/timer_reload.c @@ -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; diff --git a/src/timer/timer_start.c b/src/timer/timer_start.c index 6b7aa1e..7e7c9d6 100644 --- a/src/timer/timer_start.c +++ b/src/timer/timer_start.c @@ -2,10 +2,10 @@ #include /* - 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); +}