From: bernie Date: Tue, 19 Jul 2005 07:28:36 +0000 (+0000) Subject: Refactor to decouple timer ticks from milliseconds. X-Git-Tag: 1.0.0~812 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=528790e0e4433fa1e1394cdfa23eb33dfd934185;hp=baf900d5e62848ee595c2cedc769743b6632a367;p=bertos.git Refactor to decouple timer ticks from milliseconds. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@429 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/drv/timer.c b/drv/timer.c index c1f16145..0986e0a4 100755 --- a/drv/timer.c +++ b/drv/timer.c @@ -14,6 +14,9 @@ /*#* *#* $Log$ + *#* Revision 1.25 2005/07/19 07:26:37 bernie + *#* Refactor to decouple timer ticks from milliseconds. + *#* *#* Revision 1.24 2005/04/11 19:10:28 bernie *#* Include top-level headers from cfg/ subdir. *#* @@ -28,69 +31,15 @@ *#* *#* Revision 1.20 2004/11/16 20:59:06 bernie *#* Add watchdog timer support. - *#* - *#* Revision 1.19 2004/10/19 08:56:49 bernie - *#* TIMER_STROBE_ON, TIMER_STROBE_OFF, TIMER_STROBE_INIT: Move from timer_avr.h to timer.h, where they really belong. - *#* - *#* Revision 1.18 2004/10/14 23:14:05 bernie - *#* Fix longstanding problem with wrap-arounds. - *#* - *#* Revision 1.17 2004/10/03 18:52:08 bernie - *#* Move \brief on top in header to please Doxygen. - *#* - *#* Revision 1.16 2004/10/03 18:48:01 bernie - *#* timer_delay(): Add a sanity check to avoid sleeping forever. - *#* - *#* Revision 1.15 2004/09/14 21:07:18 bernie - *#* Use debug.h instead of kdebug.h. - *#* - *#* Revision 1.14 2004/08/25 14:12:08 rasky - *#* Aggiornato il comment block dei log RCS - *#* - *#* Revision 1.13 2004/08/10 06:59:09 bernie - *#* timer_gettick(): Rename to timer_ticks() and add backwards compatibility inline. - *#* - *#* Revision 1.12 2004/08/08 05:59:37 bernie - *#* Remove a few useless casts. - *#* - *#* Revision 1.11 2004/08/02 20:20:29 aleph - *#* Merge from project_ks - *#* - *#* Revision 1.10 2004/07/30 14:15:53 rasky - *#* Nuovo supporto unificato per detect della CPU - *#* - *#* Revision 1.9 2004/07/21 00:15:13 bernie - *#* Put timer driver on diet. - *#* - *#* Revision 1.8 2004/07/18 21:57:07 bernie - *#* Fix preprocessor warning with potentially undefined symbol. - *#* - *#* Revision 1.6 2004/06/07 18:10:06 aleph - *#* Remove free pool of timers; use user-provided Timer structure instead - *#* - *#* Revision 1.5 2004/06/07 15:56:55 aleph - *#* Some tabs cleanup and add timer strobe macros - *#* - *#* Revision 1.4 2004/06/06 18:25:44 bernie - *#* Rename event macros to look like regular functions. - *#* - *#* Revision 1.3 2004/06/06 17:18:42 bernie - *#* Fix \!CONFIG_KERN_SIGNALS code paths. - *#* - *#* Revision 1.2 2004/06/03 11:27:09 bernie - *#* Add dual-license information. - *#* - *#* Revision 1.1 2004/05/23 18:23:30 bernie - *#* Import drv/timer module. - *#* *#*/ #include "timer.h" #include #include -#include CPU_HEADER(timer) #include -#include +#include + +#include CPU_CSOURCE(timer) /* * Sanity check for config parameters required by this module. @@ -128,8 +77,8 @@ #endif -//! Master system clock (1ms accuracy) -volatile mtime_t _clock; +//! Master system clock (1 tick accuracy) +volatile ticks_t _clock; #ifndef CONFIG_TIMER_DISABLE_EVENTS @@ -152,16 +101,21 @@ void timer_add(Timer *timer) Timer *node; cpuflags_t flags; + + /* Inserting timers twice causes mayhem. */ + ASSERT(timer->magic != TIMER_MAGIC_ACTIVE); + DB(timer->magic = TIMER_MAGIC_ACTIVE;) + IRQ_SAVE_DISABLE(flags); /* Calculate expiration time for this timer */ - timer->tick = _clock + timer->delay; + timer->tick = _clock + timer->_delay; /* * Search for the first node whose expiration time is * greater than the timer we want to add. */ - node = (Timer *)timers_queue.head; + node = (Timer *)LIST_HEAD(&timers_queue); while (node->link.succ) { /* @@ -188,6 +142,7 @@ void timer_add(Timer *timer) Timer *timer_abort(Timer *timer) { ATOMIC(REMOVE(&timer->link)); + DB(timer->magic = TIMER_MAGIC_INACTIVE;) return timer; } @@ -198,7 +153,7 @@ Timer *timer_abort(Timer *timer) /*! * Wait for the specified amount of time (expressed in ms). */ -void timer_delay(mtime_t time) +void timer_delayTicks(ticks_t delay) { #if defined(IRQ_GETSTATE) /* We shouldn't sleep with interrupts disabled */ @@ -210,16 +165,16 @@ void timer_delay(mtime_t time) ASSERT(!sig_check(SIG_SINGLE)); timer_set_event_signal(&t, proc_current(), SIG_SINGLE); - timer_set_delay(&t, time); + timer_set_delay(&t, delay); timer_add(&t); sig_wait(SIG_SINGLE); #else /* !CONFIG_KERN_SIGNALS */ - mtime_t start = timer_ticks(); + ticks_t start = timer_clock(); /* Busy wait */ - while (timer_ticks() - start < time) + while (timer_clock() - start < delay) { #if CONFIG_WATCHDOG wdt_reset(); @@ -231,6 +186,34 @@ void timer_delay(mtime_t time) #ifndef CONFIG_TIMER_DISABLE_UDELAY + +/*! + * Busy wait until the specified amount of high-precision ticks have elapsed. + * + * \note This function is interrupt safe, the only + * requirement is a running hardware timer. + */ +void timer_busyWait(hptime_t delay) +{ + hptime_t now, prev = timer_hw_hpread(); + hptime_t delta; + + for(;;) + { + now = timer_hw_hpread(); + /* + * We rely on hptime_t being unsigned here to + * reduce the modulo to an AND in the common + * case of TIMER_HW_CNT. + */ + delta = (now - prev) % TIMER_HW_CNT; + if (delta >= delay) + break; + delay -= delta; + prev = now; + } +} + /*! * Wait for the specified amount of time (expressed in microseconds). * @@ -238,22 +221,15 @@ void timer_delay(mtime_t time) * delay could be very limited, depending on the hardware timer * used. Check timer_avr.h, and what register is used as hptime_t. */ -void timer_udelay(utime_t usec_delay) +void timer_delayHp(hptime_t delay) { - if (UNLIKELY(usec_delay > 1000)) + if (UNLIKELY(delay > us_to_hptime(1000))) { - timer_delay(usec_delay / 1000); - usec_delay %= 1000; + timer_delayTicks(delay / (TIMER_HW_HPTICKS_PER_SEC / TIMER_TICKS_PER_SEC)); + delay %= (TIMER_HW_HPTICKS_PER_SEC / TIMER_TICKS_PER_SEC); } - // FIXME: This multiplication is too slow at run-time. We should try and move it - // to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header - // file. - hptime_t start = timer_hw_hpread(); - hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul; - - while (timer_hw_hpread() - start < delay) - {} + timer_busyWait(delay); } #endif /* CONFIG_TIMER_DISABLE_UDELAY */ @@ -292,14 +268,15 @@ DEFINE_TIMER_ISR * by expiry time, all the following requests are guaranteed * to expire later. */ - while ((timer = (Timer *)timers_queue.head)->link.succ) + while ((timer = (Timer *)LIST_HEAD(&timers_queue))->link.succ) { /* This request in list has not yet expired? */ - if (_clock < timer->tick) + if (_clock - timer->tick < 0) break; /* Retreat the expired timer */ REMOVE(&timer->link); + DB(timer->magic = TIMER_MAGIC_INACTIVE;) /* Execute the associated event */ event_do(&timer->expire); @@ -325,3 +302,95 @@ void timer_init(void) timer_hw_init(); } + + +#if CONFIG_TEST + +static void timer_test_constants(void) +{ + kprintf("TIMER_PRESCALER=%d\n", TIMER_PRESCALER); + kprintf("TIMER_HW_HPTICKS_PER_SEC=%lu\n", TIMER_HW_HPTICKS_PER_SEC); + #ifdef TIMER1_OVF_COUNT + kprintf("TIMER1_OVF_COUNT=%d\n", (int)TIMER1_OVF_COUNT); + #endif + kprintf("TIMER_TICKS_PER_MSEC=%d\n", (int)TIMER_TICKS_PER_MSEC); + kprintf("\n"); + kprintf("ms_to_ticks(100)=%lu\n", ms_to_ticks(100)); + kprintf("ms_to_ticks(10000)=%lu\n", ms_to_ticks(10000)); + kprintf("us_to_ticks(100)=%lu\n", us_to_ticks(100)); + kprintf("us_to_ticks(10000)=%lu\n", us_to_ticks(10000)); + kprintf("\n"); + kprintf("ticks_to_ms(100)=%lu\n", ticks_to_ms(100)); + kprintf("ticks_to_ms(10000)=%lu\n", ticks_to_ms(10000)); + kprintf("ticks_to_us(100)=%lu\n", ticks_to_us(100)); + kprintf("ticks_to_us(10000)=%lu\n", ticks_to_us(10000)); + kprintf("\n"); + kprintf("hptime_to_us(100)=%lu\n", hptime_to_us(100)); + kprintf("hptime_to_us(10000)=%lu\n", hptime_to_us(10000)); + kprintf("us_to_hptime(100)=%lu\n", us_to_hptime(100)); + kprintf("us_to_hptime(10000)=%lu\n", us_to_hptime(10000)); +} + +static void timer_test_delay(void) +{ + int i; + + kputs("Delay test\n"); + for (i = 0; i < 1000; i += 100) + { + kprintf("delay %d...", i); + timer_delay(i); + kputs("done\n"); + } +} + +static void timer_test_hook(iptr_t _timer) +{ + Timer *timer = (Timer *)(void *)_timer; + + kprintf("Timer %ld expired\n", ticks_to_ms(timer->_delay)); + timer_add(timer); +} + +static void timer_test_async(void) +{ + static Timer test_timers[5]; + static const mtime_t test_delays[5] = { 170, 50, 310, 1500, 310 }; + size_t i; + + for (i = 0; i < countof(test_timers); ++i) + { + Timer *timer = &test_timers[i]; + timer_setDelay(timer, ms_to_ticks(test_delays[i])); + timer_set_event_softint(timer, timer_test_hook, (iptr_t)timer); + timer_add(timer); + } +} + +static void timer_test_poll(void) +{ + int secs = 0; + mtime_t start_time = ticks_to_ms(timer_clock()); + mtime_t now; + + while (secs <= 10) + { + now = ticks_to_ms(timer_clock()); + if (now - start_time >= 1000) + { + ++secs; + start_time += 1000; + kprintf("seconds = %d, ticks=%ld\n", secs, now); + } + } +} + +void timer_test(void) +{ + timer_test_constants(); + timer_test_delay(); + timer_test_async(); + timer_test_poll(); +} + +#endif /* CONFIG_TEST */ diff --git a/drv/timer.h b/drv/timer.h index bd7b1f7e..7bde5f5b 100755 --- a/drv/timer.h +++ b/drv/timer.h @@ -1,7 +1,7 @@ /*! * \file * @@ -15,6 +15,9 @@ /*#* *#* $Log$ + *#* Revision 1.25 2005/07/19 07:26:37 bernie + *#* Refactor to decouple timer ticks from milliseconds. + *#* *#* Revision 1.24 2005/04/11 19:10:28 bernie *#* Include top-level headers from cfg/ subdir. *#* @@ -86,22 +89,141 @@ #ifndef DRV_TIMER_H #define DRV_TIMER_H +#include + +#include CPU_HEADER(timer) #include #include #include -#include +#include /*! Number of timer ticks per second. */ -#define TICKS_PER_SEC ((mtime_t)1000) +#define TIMER_TICKS_PER_SEC (TIMER_TICKS_PER_MSEC * 1000) + +/*! Number of ticks per microsecond */ +#define TIMER_TICKS_PER_USEC ((TIMER_TICKS_PER_MSEC + 500) / 1000) + + +extern volatile ticks_t _clock; + +/*! + * \brief Return the system tick counter (expressed in ticks) + * + * The result is guaranteed to increment monotonically, + * but client code must be tolerant with respect to overflows. + * + * The following code is safe: + * + * \code + * ticks_t tea_start_time = timer_clock(); + * + * boil_water(); + * + * if (timer_clock() - tea_start_time > TEAPOT_DELAY) + * printf("Your tea, Sir.\n"); + * \endcode + * + * \note This function must disable interrupts on 8/16bit CPUs because the + * clock variable is larger than the processor word size and can't + * be copied atomically. + */ +INLINE ticks_t timer_clock(void) +{ + ticks_t result; + + ATOMIC(result = _clock); + + return result; +} + +/*! + * Faster version of timer_clock(), to be called only when the timer + * interrupt is disabled (DISABLE_INTS) or overridden by a + * higher-priority or non-nesting interrupt. + * + * \sa timer_ticks + */ +INLINE ticks_t timer_clock_unlocked(void) +{ + return _clock; +} + + + +//TODO: take care of slow timers so add convertions for seconds to ticks and viceversa. + +/*! Convert \a ms [ms] to ticks */ +INLINE ticks_t ms_to_ticks(mtime_t ms) +{ + return ms * TIMER_TICKS_PER_MSEC; +} + +/*! Convert \a us [us] to ticks */ +INLINE ticks_t us_to_ticks(utime_t us) +{ +#if TIMER_TICKS_PER_MSEC < 10000 + return (us * TIMER_TICKS_PER_MSEC + 500) / 1000; +#else + return (us * TIMER_TICKS_PER_USEC); +#endif +} + +/*! Convert \a ticks [ticks] to ms */ +INLINE mtime_t ticks_to_ms(ticks_t ticks) +{ + return (ticks + TIMER_TICKS_PER_MSEC / 2) / TIMER_TICKS_PER_MSEC; +} + +/*! Convert \a ticks [ticks] to us */ +INLINE utime_t ticks_to_us(ticks_t ticks) +{ +#if TIMER_TICKS_PER_USEC > 10 + return (ticks / TIMER_TICKS_PER_USEC); +#else + return (ticks * 1000 + TIMER_TICKS_PER_MSEC / 2) / TIMER_TICKS_PER_MSEC; +#endif +} + +/*! Convert \a us [us] to hpticks */ +INLINE hptime_t us_to_hptime(utime_t us) +{ + #if TIMER_HW_HPTICKS_PER_SEC > 10000000UL + return(us * ((TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL)); + #else + return((us * TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL)); + #endif /* TIMER_HW_HPTICKS_PER_SEC > 10000000UL */ +} + +/*! Convert \a hpticks [hptime] to usec */ +INLINE utime_t hptime_to_us(hptime_t hpticks) +{ + #if TIMER_HW_HPTICKS_PER_SEC < 100000UL + return(hpticks * (1000000UL / TIMER_HW_HPTICKS_PER_SEC)); + #else + return((hpticks * 1000000UL) / TIMER_HW_HPTICKS_PER_SEC); + #endif /* TIMER_HW_HPTICKS_PER_SEC < 100000UL */ +} + -/* Function protos */ -extern void timer_init(void); -extern void timer_delay(mtime_t time); +void timer_init(void); +void timer_delayTicks(ticks_t delay); +INLINE void timer_delay(mtime_t delay) +{ + timer_delayTicks(ms_to_ticks(delay)); +} -#ifndef CONFIG_TIMER_DISABLE_UDELAY -extern void timer_udelay(utime_t utime); +#if !defined(CONFIG_TIMER_DISABLE_UDELAY) +void timer_busyWait(hptime_t delay); +void timer_delayHp(hptime_t delay); +INLINE void timer_udelay(utime_t delay) +{ + timer_delayHp(us_to_hptime(delay)); +} #endif +#if CONFIG_TEST +void timer_test(void); +#endif /* CONFIG_TEST */ #ifndef CONFIG_TIMER_DISABLE_EVENTS @@ -117,24 +239,19 @@ extern void timer_udelay(utime_t utime); typedef struct Timer { Node link; /*!< Link into timers queue */ - mtime_t delay; /*!< Timer delay in ms */ - mtime_t tick; /*!< Timer will expire at this tick */ + ticks_t _delay; /*!< Timer delay in ms */ + ticks_t tick; /*!< Timer will expire at this tick */ Event expire; /*!< Event to execute when the timer expires */ + DB(uint16_t magic;) } Timer; +/*! Timer is active when Timer.magic contains this value (for debugging purposes). */ +#define TIMER_MAGIC_ACTIVE 0xABBA +#define TIMER_MAGIC_INACTIVE 0xBAAB + extern void timer_add(Timer *timer); extern Timer *timer_abort(Timer *timer); -#if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS - -/*! Set the timer so that it sends a signal when it expires */ -INLINE void timer_set_event_signal(Timer *timer, struct Process *proc, sigmask_t sigs) -{ - event_initSignal(&timer->expire, proc, sigs); -} - -#endif /* CONFIG_KERN_SIGNALS */ - /*! Set the timer so that it calls an user hook when it expires */ INLINE void timer_set_event_softint(Timer *timer, Hook func, iptr_t user_data) { @@ -142,59 +259,22 @@ INLINE void timer_set_event_softint(Timer *timer, Hook func, iptr_t user_data) } /*! Set the timer delay (the time before the event will be triggered) */ -INLINE void timer_set_delay(Timer *timer, mtime_t delay) +INLINE void timer_setDelay(Timer *timer, ticks_t delay) { - timer->delay = delay; + timer->_delay = delay; } #endif /* CONFIG_TIMER_DISABLE_EVENTS */ -extern volatile mtime_t _clock; +#if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS -/*! - * \brief Return the system tick counter (expressed in ms) - * - * The result is guaranteed to increment monotonically, - * but client code must be tolerant with respect to overflows. - * - * The following code is safe: - * - * \code - * mtime_t tea_start_time = get_tick(); - * - * boil_water(); - * - * if (get_tick() - tea_start_time > TEAPOT_DELAY) - * printf("Your tea, Sir.\n"); - * \endcode - * - * When the tick counter increments every millisecond and mtime_t - * is 32bit wide, the tick count will overflow every 49.7 days. - * - * \note This function must disable interrupts on 8/16bit CPUs because the - * clock variable is larger than the processor word size and can't - * be copied atomically. - */ -INLINE mtime_t timer_ticks(void) +/*! Set the timer so that it sends a signal when it expires */ +INLINE void timer_set_event_signal(Timer *timer, struct Process *proc, sigmask_t sigs) { - mtime_t result; - - ATOMIC(result = _clock); - - return result; + event_initSignal(&timer->expire, proc, sigs); } +#endif /* CONFIG_KERN_SIGNALS */ -/*! - * Faster version of timer_ticks(), to be called only when the timer - * interrupt is disabled (DISABLE_INTS) or overridden by a - * higher-priority or non-nesting interrupt. - * - * \sa timer_ticks - */ -INLINE mtime_t timer_ticks_unlocked(void) -{ - return _clock; -} #endif /* DRV_TIMER_H */ diff --git a/drv/timer_avr.c b/drv/timer_avr.c new file mode 100755 index 00000000..555d6a80 --- /dev/null +++ b/drv/timer_avr.c @@ -0,0 +1,184 @@ +/*! + * \file + * + * + * \version $Id$ + * + * \author Bernardo Innocenti + * \author Francesco Sacchi + * + * \brief Low-level timer module for AVR (implementation). + */ + +/*#* + *#* $Log$ + *#* Revision 1.1 2005/07/19 07:28:36 bernie + *#* Refactor to decouple timer ticks from milliseconds. + *#* + *#* Revision 1.1 2005/05/24 09:17:58 batt + *#* Move drivers to top-level. + *#* + *#*/ +#include +#include // ARCH_BOARD_KC +#include // BV() +#include + +#include +#include + +/*! HW dependent timer initialization */ +#if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0) + + static void timer_hw_init(void) + { + cpuflags_t flags; + IRQ_SAVE_DISABLE(flags); + + /* Reset Timer flags */ + TIFR = BV(OCF0) | BV(TOV0); + + /* Setup Timer/Counter interrupt */ + ASSR = 0x00; /* Internal system clock */ + TCCR0 = BV(WGM01) /* Clear on Compare match */ + #if TIMER_PRESCALER == 64 + | BV(CS02) + #else + #error Unsupported value of TIMER_PRESCALER + #endif + ; + TCNT0 = 0x00; /* Initialization of Timer/Counter */ + OCR0 = OCR_DIVISOR; /* Timer/Counter Output Compare Register */ + + /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */ + TIMSK &= ~BV(TOIE0); + TIMSK |= BV(OCIE0); + + IRQ_RESTORE(flags); + } + + INLINE hptime_t timer_hw_hpread(void) + { + return TCNT0; + } + +#elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1) + + static void timer_hw_init(void) + { + cpuflags_t flags; + IRQ_SAVE_DISABLE(flags); + + /* Reset Timer overflow flag */ + TIFR |= BV(TOV1); + + /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */ + #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9) + TCCR1A |= BV(WGM11); + TCCR1A &= ~BV(WGM10); + TCCR1B |= BV(WGM12) | BV(CS10); + TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12)); + /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */ + #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8) + TCCR1A |= BV(WGM10); + TCCR1A &= ~BV(WGM11); + TCCR1B |= BV(WGM12) | BV(CS10); + TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12)); + #else + #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS + #endif + + TCNT1 = 0x00; /* initialization of Timer/Counter */ + + /* Enable timer interrupt: Timer/Counter1 Overflow */ + TIMSK |= BV(TOIE1); + + IRQ_RESTORE(flags); + } + + INLINE hptime_t timer_hw_hpread(void) + { + return TCNT1; + } + +#elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2) + + static void timer_hw_init(void) + { + cpuflags_t flags; + IRQ_SAVE_DISABLE(flags); + + /* Reset Timer flags */ + TIFR = BV(OCF2) | BV(TOV2); + + /* Setup Timer/Counter interrupt */ + TCCR2 = BV(WGM21) + #if TIMER_PRESCALER == 64 + | BV(CS21) | BV(CS20) + #else + #error Unsupported value of TIMER_PRESCALER + #endif + ; + /* Clear on Compare match & prescaler = 64, internal sys clock. + When changing prescaler change TIMER_HW_HPTICKS_PER_SEC too */ + TCNT2 = 0x00; /* initialization of Timer/Counter */ + OCR2 = OCR_DIVISOR; /* Timer/Counter Output Compare Register */ + + /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */ + TIMSK &= ~BV(TOIE2); + TIMSK |= BV(OCIE2); + + IRQ_RESTORE(flags); + } + + INLINE hptime_t timer_hw_hpread(void) + { + return TCNT2; + } +#elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3) + + static void timer_hw_init(void) + { + cpuflags_t flags; + IRQ_SAVE_DISABLE(flags); + + /* Reset Timer overflow flag */ + TIFR |= BV(TOV3); + + /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */ + #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9) + TCCR3A |= BV(WGM31); + TCCR3A &= ~BV(WGM30); + TCCR3B |= BV(WGM32) | BV(CS30); + TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32)); + /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */ + #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8) + TCCR3A |= BV(WGM30); + TCCR3A &= ~BV(WGM31); + TCCR3B |= BV(WGM32) | BV(CS30); + TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32)); + #else + #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS + #endif + + TCNT3 = 0x00; /* initialization of Timer/Counter */ + + /* Enable timer interrupt: Timer/Counter3 Overflow */ + /* ATTENTION! TOIE3 is only on ETIMSK, not TIMSK */ + ETIMSK |= BV(TOIE3); + + IRQ_RESTORE(flags); + } + + INLINE hptime_t timer_hw_hpread(void) + { + return TCNT3; + } + +#else + #error Unimplemented value for CONFIG_TIMER +#endif /* CONFIG_TIMER */ + diff --git a/drv/timer_avr.h b/drv/timer_avr.h index d308608a..0ba8d52b 100755 --- a/drv/timer_avr.h +++ b/drv/timer_avr.h @@ -9,12 +9,16 @@ * \version $Id$ * * \author Bernardo Innocenti + * \author Francesco Sacchi * - * \brief Low-level timer module for AVR + * \brief Low-level timer module for AVR (interface). */ /*#* *#* $Log$ + *#* Revision 1.25 2005/07/19 07:26:37 bernie + *#* Refactor to decouple timer ticks from milliseconds. + *#* *#* Revision 1.24 2005/04/11 19:10:28 bernie *#* Include top-level headers from cfg/ subdir. *#* @@ -32,49 +36,13 @@ *#* *#* Revision 1.18 2004/09/20 03:31:03 bernie *#* Fix racy racy code. - *#* - *#* Revision 1.17 2004/09/14 21:07:09 bernie - *#* Include hw.h explicitly. - *#* - *#* Revision 1.16 2004/09/06 21:49:26 bernie - *#* CONFIG_TIMER_STROBE: be tolerant with missing optional macro. - *#* - *#* Revision 1.15 2004/08/25 14:12:08 rasky - *#* Aggiornato il comment block dei log RCS - *#* - *#* Revision 1.14 2004/08/24 16:27:01 bernie - *#* Add missing headers. - *#* - *#* Revision 1.13 2004/08/24 14:30:11 bernie - *#* Use new-style config macros for drv/timer.c - *#* - *#* Revision 1.12 2004/08/10 06:59:45 bernie - *#* CONFIG_TIMER_STROBE: Define no-op default macros. - *#* - *#* Revision 1.11 2004/08/03 15:53:17 aleph - *#* Fix spacing - *#* - *#* Revision 1.10 2004/08/02 20:20:29 aleph - *#* Merge from project_ks - *#* - *#* Revision 1.9 2004/07/22 02:01:14 bernie - *#* Use TIMER_PRESCALER consistently. *#*/ #ifndef DRV_TIMER_AVR_H #define DRV_TIMER_AVR_H -#include // ARCH_BOARD_KC -#include // BV() +#include /* CONFIG_TIMER */ #include -#include -#include - -#if defined(ARCH_BOARD_KC) && (ARCH & ARCH_BOARD_KC) - #include -#endif - - /*! * Values for CONFIG_TIMER. * @@ -84,229 +52,73 @@ #define TIMER_ON_OUTPUT_COMPARE0 1 #define TIMER_ON_OVERFLOW1 2 #define TIMER_ON_OUTPUT_COMPARE2 3 - - -/* Not needed, IRQ timer flag cleared automatically */ -#define timer_hw_irq() do {} while (0) - -#define TIMER_PRESCALER 64 - -/*! - * System timer: additional division after the prescaler - * 12288000 / 64 / 192 (0..191) = 1 ms - */ -#define OCR_DIVISOR (CLOCK_FREQ / TIMER_PRESCALER / TICKS_PER_SEC - 1) /* 191 */ +#define TIMER_ON_OVERFLOW3 4 /*! HW dependent timer initialization */ #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0) + #define TIMER_PRESCALER 64 + #define TIMER_HW_BITS 8 + #define DEFINE_TIMER_ISR SIGNAL(SIG_OUTPUT_COMPARE0) + #define TIMER_TICKS_PER_MSEC 1 + #define TIMER_HW_CNT OCR_DIVISOR + //! Type of time expressed in ticks of the hardware high-precision timer typedef uint8_t hptime_t; - static void timer_hw_init(void) - { - cpuflags_t flags; - IRQ_SAVE_DISABLE(flags); - - /* Reset Timer flags */ - TIFR = BV(OCF0) | BV(TOV0); - - /* Setup Timer/Counter interrupt */ - ASSR = 0x00; /* Internal system clock */ - TCCR0 = BV(WGM01) /* Clear on Compare match */ - #if TIMER_PRESCALER == 64 - | BV(CS02) - #else - #error Unsupported value of TIMER_PRESCALER - #endif - ; - TCNT0 = 0x00; /* Initialization of Timer/Counter */ - OCR0 = OCR_DIVISOR; /* Timer/Counter Output Compare Register */ - - /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */ - TIMSK &= ~BV(TOIE0); - TIMSK |= BV(OCIE0); - - IRQ_RESTORE(flags); - } - - //! Frequency of the hardware high precision timer - #define TIMER_HW_HPTICKS_PER_SEC (CLOCK_FREQ / TIMER_PRESCALER) - - INLINE hptime_t timer_hw_hpread(void) - { - return TCNT0; - } - #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1) + #define TIMER_PRESCALER 1 + #define TIMER_HW_BITS 8 + /*! This value is the maximum in overflow based timers. */ + #define TIMER_HW_CNT (1 << TIMER_HW_BITS) + #define DEFINE_TIMER_ISR SIGNAL(SIG_OVERFLOW1) + #define TIMER_TICKS_PER_MSEC (((TIMER_HW_HPTICKS_PER_SEC + TIMER_HW_CNT / 2) / TIMER_HW_CNT + 500) / 1000) + //! Type of time expressed in ticks of the hardware high precision timer typedef uint16_t hptime_t; - static void timer_hw_init(void) - { - cpuflags_t flags; - IRQ_SAVE_DISABLE(flags); - - /* Reset Timer overflow flag */ - TIFR |= BV(TOV1); - - /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. When changing freq or - resolution (top of TCNT), change TIMER_HW_HPTICKS_PER_SEC too */ - TCCR1A |= BV(WGM11); - TCCR1A &= ~BV(WGM10); - TCCR1B |= BV(WGM12) | BV(CS10); - TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12)); - - TCNT1 = 0x00; /* initialization of Timer/Counter */ - - /* Enable timer interrupt: Timer/Counter1 Overflow */ - TIMSK |= BV(TOIE1); - - IRQ_RESTORE(flags); - } - - //! Frequency of the hardware high precision timer - #define TIMER_HW_HPTICKS_PER_SEC (24000ul * 512) +#elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2) - INLINE hptime_t timer_hw_hpread(void) - { - return TCNT1; - } + #define TIMER_PRESCALER 64 + #define TIMER_HW_BITS 8 + #define DEFINE_TIMER_ISR SIGNAL(SIG_OUTPUT_COMPARE2) + #define TIMER_TICKS_PER_MSEC 1 + /*! Value for OCR register in output-compare based timers. */ + #define TIMER_HW_CNT OCR_DIVISOR -#elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2) //! Type of time expressed in ticks of the hardware high precision timer typedef uint8_t hptime_t; - static void timer_hw_init(void) - { - cpuflags_t flags; - IRQ_SAVE_DISABLE(flags); - - /* Reset Timer flags */ - TIFR = BV(OCF2) | BV(TOV2); - - /* Setup Timer/Counter interrupt */ - TCCR2 = BV(WGM21) - #if TIMER_PRESCALER == 64 - | BV(CS21) | BV(CS20) - #else - #error Unsupported value of TIMER_PRESCALER - #endif - ; - /* Clear on Compare match & prescaler = 64, internal sys clock. - When changing prescaler change TIMER_HW_HPTICKS_PER_SEC too */ - TCNT2 = 0x00; /* initialization of Timer/Counter */ - OCR2 = OCR_DIVISOR; /* Timer/Counter Output Compare Register */ - - /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */ - TIMSK &= ~BV(TOIE2); - TIMSK |= BV(OCIE2); - - IRQ_RESTORE(flags); - } +#elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3) - //! Frequency of the hardware high precision timer - #define TIMER_HW_HPTICKS_PER_SEC (CLOCK_FREQ / TIMER_PRESCALER) - - INLINE hptime_t timer_hw_hpread(void) - { - return TCNT2; - } + #define TIMER_PRESCALER 1 + #define TIMER_HW_BITS 8 + /*! This value is the maximum in overflow based timers. */ + #define TIMER_HW_CNT (1 << TIMER_HW_BITS) + #define DEFINE_TIMER_ISR SIGNAL(SIG_OVERFLOW3) + #define TIMER_TICKS_PER_MSEC (((TIMER_HW_HPTICKS_PER_SEC + TIMER_HW_CNT / 2) / TIMER_HW_CNT + 500) / 1000) + //! Type of time expressed in ticks of the hardware high precision timer + typedef uint16_t hptime_t; #else + #error Unimplemented value for CONFIG_TIMER #endif /* CONFIG_TIMER */ -#if (CONFIG_TIMER == TIMER_ON_OVERFLOW1) - - #define DEFINE_TIMER_ISR \ - static void timer_handler(void) - - DEFINE_TIMER_ISR; - - /* - * Timer 1 overflow irq handler. It's called at the frequency of the timer 1 - * PWM (should be 24 kHz). It's too much for timer purposes, so the interrupt - * handler is really a counter that call the true handler in timer.c - * every 1 ms. - */ - SIGNAL(SIG_OVERFLOW1) - { - #if (ARCH & ARCH_BOARD_KC) - /* - * Super-optimization-hack: switch CPU ADC mux here, ASAP after the start - * of conversion (auto-triggered with timer 1 overflow). - * The switch can be done 2 ADC cycles after start of conversion. - * The handler prologue takes a little more than 32 CPU cycles: with - * the prescaler at 1/16 the timing should be correct even at the start - * of the handler. - * - * The switch is synchronized with the ADC handler using _adc_trigger_lock. - * - * Mel (A Real Programmer) - */ - extern uint8_t _adc_idx_next; - extern bool _adc_trigger_lock; - - if (!_adc_trigger_lock) - { - // Backwards compatibility fix for avr-libc 1.0.4 - #ifndef ADATE - #define ADATE ADFR - #endif +/*! Frequency of the hardware high precision timer */ +#define TIMER_HW_HPTICKS_PER_SEC ((CLOCK_FREQ + TIMER_PRESCALER / 2) / TIMER_PRESCALER) - /* - * Disable free-running mode to avoid starting a - * new conversion before the ADC handler has read - * the ongoing one. This condition could occur - * under very high interrupt load and would have the - * unwanted effect of reading from the wrong ADC - * channel. - * - * NOTE: writing 0 to ADSC and ADIF has no effect. - */ - ADCSRA = ADCSRA & ~(BV(ADATE) | BV(ADIF) | BV(ADSC)); - - ADC_SETCHN(_adc_idx_next); - _adc_trigger_lock = true; - } - #endif // ARCH_BOARD_KC - - /*! - * How many timer overflows we must count before calling the real - * timer handler. - * When the timer is programmed to overflow at 24 kHz, a value of - * 24 will result in 1ms between each call. - */ - #define TIMER1_OVF_COUNT 24 - //#warning TIMER1_OVF_COUNT for timer at 12 kHz - //#define TIMER1_OVF_COUNT 12 - - static uint8_t count = TIMER1_OVF_COUNT; - - count--; - if (!count) - { - timer_handler(); - count = TIMER1_OVF_COUNT; - } - } - -#elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0) - - #define DEFINE_TIMER_ISR \ - SIGNAL(SIG_OUTPUT_COMPARE0) - -#elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2) +/*! + * System timer: additional division after the prescaler + * 12288000 / 64 / 192 (0..191) = 1 ms + */ +#define OCR_DIVISOR (((CLOCK_FREQ + TIMER_PRESCALER / 2) / TIMER_PRESCALER + TICKS_PER_SEC / 2) / TICKS_PER_SEC - 1) /* 191 */ - #define DEFINE_TIMER_ISR \ - SIGNAL(SIG_OUTPUT_COMPARE2) +/*! Not needed, IRQ timer flag cleared automatically */ +#define timer_hw_irq() do {} while (0) -#else - #error Unimplemented value for CONFIG_TIMER -#endif /* CONFIG_TIMER */ #endif /* DRV_TIMER_AVR_H */