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.17 2004/10/03 18:52:08 bernie
18 *#* Move \brief on top in header to please Doxygen.
20 *#* Revision 1.16 2004/10/03 18:48:01 bernie
21 *#* timer_delay(): Add a sanity check to avoid sleeping forever.
23 *#* Revision 1.15 2004/09/14 21:07:18 bernie
24 *#* Use debug.h instead of kdebug.h.
26 *#* Revision 1.14 2004/08/25 14:12:08 rasky
27 *#* Aggiornato il comment block dei log RCS
29 *#* Revision 1.13 2004/08/10 06:59:09 bernie
30 *#* timer_gettick(): Rename to timer_ticks() and add backwards compatibility inline.
32 *#* Revision 1.12 2004/08/08 05:59:37 bernie
33 *#* Remove a few useless casts.
35 *#* Revision 1.11 2004/08/02 20:20:29 aleph
36 *#* Merge from project_ks
38 *#* Revision 1.10 2004/07/30 14:15:53 rasky
39 *#* Nuovo supporto unificato per detect della CPU
41 *#* Revision 1.9 2004/07/21 00:15:13 bernie
42 *#* Put timer driver on diet.
44 *#* Revision 1.8 2004/07/18 21:57:07 bernie
45 *#* Fix preprocessor warning with potentially undefined symbol.
47 *#* Revision 1.6 2004/06/07 18:10:06 aleph
48 *#* Remove free pool of timers; use user-provided Timer structure instead
50 *#* Revision 1.5 2004/06/07 15:56:55 aleph
51 *#* Some tabs cleanup and add timer strobe macros
53 *#* Revision 1.4 2004/06/06 18:25:44 bernie
54 *#* Rename event macros to look like regular functions.
56 *#* Revision 1.3 2004/06/06 17:18:42 bernie
57 *#* Fix \!CONFIG_KERN_SIGNALS code paths.
59 *#* Revision 1.2 2004/06/03 11:27:09 bernie
60 *#* Add dual-license information.
62 *#* Revision 1.1 2004/05/23 18:23:30 bernie
63 *#* Import drv/timer module.
69 #include CPU_HEADER(timer)
72 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
73 #include <kern/proc.h>
76 //! Master system clock (1ms accuracy)
77 volatile time_t _clock;
80 #ifndef CONFIG_TIMER_DISABLE_EVENTS
83 * List of active asynchronous timers.
85 REGISTER static List timers_queue;
89 * Add the specified timer to the software timer service queue.
90 * When the delay indicated by the timer expires, the timer
91 * device will execute the event associated with it.
93 * \note Interrupt safe
95 void timer_add(Timer *timer)
100 DISABLE_IRQSAVE(flags);
102 /* Calculate expiration time for this timer */
103 timer->tick = _clock + timer->delay;
105 /* Search for the first node whose expiration time is
106 * greater than the timer we want to add.
108 node = (Timer *)timers_queue.head;
109 while (node->link.succ)
111 /* Stop just after the insertion point */
112 if (node->tick > timer->tick)
115 /* Go to next node */
116 node = (Timer *)node->link.succ;
119 /* Enqueue timer request into the list */
120 INSERTBEFORE(&timer->link, &node->link);
122 ENABLE_IRQRESTORE(flags);
127 * Remove a timer from the timer queue before it has expired
129 Timer *timer_abort(Timer *timer)
132 DISABLE_IRQSAVE(flags);
133 REMOVE(&timer->link);
134 ENABLE_IRQRESTORE(flags);
139 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
143 * Wait for the specified amount of time (expressed in ms)
145 void timer_delay(time_t time)
147 #if defined(IRQ_GETSTATE)
148 /* We shouldn't sleep with interrupts disabled */
149 ASSERT(IRQ_GETSTATE());
152 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
155 ASSERT(!sig_check(SIG_SINGLE));
156 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
157 timer_set_delay(&t, time);
159 sig_wait(SIG_SINGLE);
161 #else /* !CONFIG_KERN_SIGNALS */
163 time_t start = timer_ticks();
166 while (timer_ticks() - start < time) { /* nop */ }
168 #endif /* !CONFIG_KERN_SIGNALS */
172 #ifndef CONFIG_TIMER_DISABLE_UDELAY
174 * Wait for the specified amount of time (expressed in microseconds)
176 * \bug In AVR arch the maximum amount of time that can be used as
177 * delay could be very limited, depending on the hardware timer
178 * used. Check timer_avr.h, and what register is used as hptime_t.
180 void timer_udelay(utime_t usec_delay)
182 if (UNLIKELY(usec_delay > 1000))
184 timer_delay(usec_delay / 1000);
188 // FIXME: This multiplication is too slow at run-time. We should try and move it
189 // to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header
191 hptime_t start = timer_hw_hpread();
192 hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul;
194 while (timer_hw_hpread() - start < delay)
197 #endif /* CONFIG_TIMER_DISABLE_UDELAY */
201 * Timer interrupt handler. Find soft timers expired and
202 * trigger corresponding events.
207 * With the Metrowerks compiler, the only way to force the compiler generate
208 * an interrupt service routine is to put a pragma directive within the function
212 #pragma interrupt saveall
215 #ifndef CONFIG_TIMER_DISABLE_EVENTS
223 /* Update the master ms counter */
226 #ifndef CONFIG_TIMER_DISABLE_EVENTS
228 * Check the first timer request in the list and process
229 * it when it has expired. Repeat this check until the
230 * first node has not yet expired. Since the list is sorted
231 * by expiry time, all the following requests are guaranteed
234 while ((timer = (Timer *)timers_queue.head)->link.succ)
236 /* This request in list has not yet expired? */
237 if (_clock < timer->tick)
240 /* Retreat the expired timer */
241 REMOVE(&timer->link);
243 /* Execute the associated event */
244 event_do(&timer->expire);
246 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
255 void timer_init(void)
259 #ifndef CONFIG_TIMER_DISABLE_EVENTS
260 INITLIST(&timers_queue);