4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2000 Bernardo Innocenti <bernie@develer.com>
6 * This file is part of DevLib - See devlib/README for information.
9 * \brief Hardware independent timer driver (implementation)
12 * \author Bernardo Innocenti <bernie@develer.com>
17 *#* Revision 1.25 2005/07/19 07:26:37 bernie
18 *#* Refactor to decouple timer ticks from milliseconds.
20 *#* Revision 1.24 2005/04/11 19:10:28 bernie
21 *#* Include top-level headers from cfg/ subdir.
23 *#* Revision 1.23 2004/12/13 12:07:06 bernie
24 *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
26 *#* Revision 1.22 2004/12/08 09:12:09 bernie
27 *#* Rename time_t to mtime_t.
29 *#* Revision 1.21 2004/11/28 23:20:25 bernie
30 *#* Remove obsolete INITLIST macro.
32 *#* Revision 1.20 2004/11/16 20:59:06 bernie
33 *#* Add watchdog timer support.
39 #include <cfg/debug.h>
40 #include <appconfig.h>
42 #include CPU_CSOURCE(timer)
45 * Sanity check for config parameters required by this module.
47 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
48 #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
50 #if !defined(CONFIG_WATCHDOG) || ((CONFIG_WATCHDOG != 0) && CONFIG_WATCHDOG != 1)
51 #error CONFIG_WATCHDOG must be set to either 0 or 1 in config.h
58 #if CONFIG_KERNEL && CONFIG_KERN_SIGNALS
59 #include <kern/proc.h>
64 * \def CONFIG_TIMER_STROBE
66 * This is a debug facility that can be used to
67 * monitor timer interrupt activity on an external pin.
69 * To use strobes, redefine the macros TIMER_STROBE_ON,
70 * TIMER_STROBE_OFF and TIMER_STROBE_INIT and set
71 * CONFIG_TIMER_STROBE to 1.
73 #if !defined(CONFIG_TIMER_STROBE) || !CONFIG_TIMER_STROBE
74 #define TIMER_STROBE_ON do {/*nop*/} while(0)
75 #define TIMER_STROBE_OFF do {/*nop*/} while(0)
76 #define TIMER_STROBE_INIT do {/*nop*/} while(0)
80 //! Master system clock (1 tick accuracy)
81 volatile ticks_t _clock;
84 #ifndef CONFIG_TIMER_DISABLE_EVENTS
87 * List of active asynchronous timers.
89 REGISTER static List timers_queue;
93 * Add the specified timer to the software timer service queue.
94 * When the delay indicated by the timer expires, the timer
95 * device will execute the event associated with it.
97 * \note Interrupt safe
99 void timer_add(Timer *timer)
105 /* Inserting timers twice causes mayhem. */
106 ASSERT(timer->magic != TIMER_MAGIC_ACTIVE);
107 DB(timer->magic = TIMER_MAGIC_ACTIVE;)
109 IRQ_SAVE_DISABLE(flags);
111 /* Calculate expiration time for this timer */
112 timer->tick = _clock + timer->_delay;
115 * Search for the first node whose expiration time is
116 * greater than the timer we want to add.
118 node = (Timer *)LIST_HEAD(&timers_queue);
119 while (node->link.succ)
122 * Stop just after the insertion point.
123 * (this fancy compare takes care of wrap-arounds).
125 if (node->tick - timer->tick > 0)
128 /* Go to next node */
129 node = (Timer *)node->link.succ;
132 /* Enqueue timer request into the list */
133 INSERTBEFORE(&timer->link, &node->link);
140 * Remove a timer from the timer queue before it has expired.
142 Timer *timer_abort(Timer *timer)
144 ATOMIC(REMOVE(&timer->link));
145 DB(timer->magic = TIMER_MAGIC_INACTIVE;)
150 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
154 * Wait for the specified amount of time (expressed in ms).
156 void timer_delayTicks(ticks_t delay)
158 #if defined(IRQ_GETSTATE)
159 /* We shouldn't sleep with interrupts disabled */
160 ASSERT(IRQ_GETSTATE());
163 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
166 ASSERT(!sig_check(SIG_SINGLE));
167 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
168 timer_set_delay(&t, delay);
170 sig_wait(SIG_SINGLE);
172 #else /* !CONFIG_KERN_SIGNALS */
174 ticks_t start = timer_clock();
177 while (timer_clock() - start < delay)
184 #endif /* !CONFIG_KERN_SIGNALS */
188 #ifndef CONFIG_TIMER_DISABLE_UDELAY
191 * Busy wait until the specified amount of high-precision ticks have elapsed.
193 * \note This function is interrupt safe, the only
194 * requirement is a running hardware timer.
196 void timer_busyWait(hptime_t delay)
198 hptime_t now, prev = timer_hw_hpread();
203 now = timer_hw_hpread();
205 * We rely on hptime_t being unsigned here to
206 * reduce the modulo to an AND in the common
207 * case of TIMER_HW_CNT.
209 delta = (now - prev) % TIMER_HW_CNT;
218 * Wait for the specified amount of time (expressed in microseconds).
220 * \bug In AVR arch the maximum amount of time that can be used as
221 * delay could be very limited, depending on the hardware timer
222 * used. Check timer_avr.h, and what register is used as hptime_t.
224 void timer_delayHp(hptime_t delay)
226 if (UNLIKELY(delay > us_to_hptime(1000)))
228 timer_delayTicks(delay / (TIMER_HW_HPTICKS_PER_SEC / TIMER_TICKS_PER_SEC));
229 delay %= (TIMER_HW_HPTICKS_PER_SEC / TIMER_TICKS_PER_SEC);
232 timer_busyWait(delay);
234 #endif /* CONFIG_TIMER_DISABLE_UDELAY */
238 * Timer interrupt handler. Find soft timers expired and
239 * trigger corresponding events.
244 * With the Metrowerks compiler, the only way to force the compiler generate
245 * an interrupt service routine is to put a pragma directive within the function
249 #pragma interrupt saveall
252 #ifndef CONFIG_TIMER_DISABLE_EVENTS
260 /* Update the master ms counter */
263 #ifndef CONFIG_TIMER_DISABLE_EVENTS
265 * Check the first timer request in the list and process
266 * it when it has expired. Repeat this check until the
267 * first node has not yet expired. Since the list is sorted
268 * by expiry time, all the following requests are guaranteed
271 while ((timer = (Timer *)LIST_HEAD(&timers_queue))->link.succ)
273 /* This request in list has not yet expired? */
274 if (_clock - timer->tick < 0)
277 /* Retreat the expired timer */
278 REMOVE(&timer->link);
279 DB(timer->magic = TIMER_MAGIC_INACTIVE;)
281 /* Execute the associated event */
282 event_do(&timer->expire);
284 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
293 void timer_init(void)
297 #ifndef CONFIG_TIMER_DISABLE_EVENTS
298 LIST_INIT(&timers_queue);
309 static void timer_test_constants(void)
311 kprintf("TIMER_PRESCALER=%d\n", TIMER_PRESCALER);
312 kprintf("TIMER_HW_HPTICKS_PER_SEC=%lu\n", TIMER_HW_HPTICKS_PER_SEC);
313 #ifdef TIMER1_OVF_COUNT
314 kprintf("TIMER1_OVF_COUNT=%d\n", (int)TIMER1_OVF_COUNT);
316 kprintf("TIMER_TICKS_PER_MSEC=%d\n", (int)TIMER_TICKS_PER_MSEC);
318 kprintf("ms_to_ticks(100)=%lu\n", ms_to_ticks(100));
319 kprintf("ms_to_ticks(10000)=%lu\n", ms_to_ticks(10000));
320 kprintf("us_to_ticks(100)=%lu\n", us_to_ticks(100));
321 kprintf("us_to_ticks(10000)=%lu\n", us_to_ticks(10000));
323 kprintf("ticks_to_ms(100)=%lu\n", ticks_to_ms(100));
324 kprintf("ticks_to_ms(10000)=%lu\n", ticks_to_ms(10000));
325 kprintf("ticks_to_us(100)=%lu\n", ticks_to_us(100));
326 kprintf("ticks_to_us(10000)=%lu\n", ticks_to_us(10000));
328 kprintf("hptime_to_us(100)=%lu\n", hptime_to_us(100));
329 kprintf("hptime_to_us(10000)=%lu\n", hptime_to_us(10000));
330 kprintf("us_to_hptime(100)=%lu\n", us_to_hptime(100));
331 kprintf("us_to_hptime(10000)=%lu\n", us_to_hptime(10000));
334 static void timer_test_delay(void)
338 kputs("Delay test\n");
339 for (i = 0; i < 1000; i += 100)
341 kprintf("delay %d...", i);
347 static void timer_test_hook(iptr_t _timer)
349 Timer *timer = (Timer *)(void *)_timer;
351 kprintf("Timer %ld expired\n", ticks_to_ms(timer->_delay));
355 static void timer_test_async(void)
357 static Timer test_timers[5];
358 static const mtime_t test_delays[5] = { 170, 50, 310, 1500, 310 };
361 for (i = 0; i < countof(test_timers); ++i)
363 Timer *timer = &test_timers[i];
364 timer_setDelay(timer, ms_to_ticks(test_delays[i]));
365 timer_set_event_softint(timer, timer_test_hook, (iptr_t)timer);
370 static void timer_test_poll(void)
373 mtime_t start_time = ticks_to_ms(timer_clock());
378 now = ticks_to_ms(timer_clock());
379 if (now - start_time >= 1000)
383 kprintf("seconds = %d, ticks=%ld\n", secs, now);
388 void timer_test(void)
390 timer_test_constants();
396 #endif /* CONFIG_TEST */