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.
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief Hardware independent timer driver (implementation)
18 * Revision 1.12 2004/08/08 05:59:37 bernie
19 * Remove a few useless casts.
21 * Revision 1.11 2004/08/02 20:20:29 aleph
22 * Merge from project_ks
24 * Revision 1.10 2004/07/30 14:15:53 rasky
25 * Nuovo supporto unificato per detect della CPU
27 * Revision 1.9 2004/07/21 00:15:13 bernie
28 * Put timer driver on diet.
30 * Revision 1.8 2004/07/18 21:57:07 bernie
31 * Fix preprocessor warning with potentially undefined symbol.
33 * Revision 1.6 2004/06/07 18:10:06 aleph
34 * Remove free pool of timers; use user-provided Timer structure instead
36 * Revision 1.5 2004/06/07 15:56:55 aleph
37 * Some tabs cleanup and add timer strobe macros
39 * Revision 1.4 2004/06/06 18:25:44 bernie
40 * Rename event macros to look like regular functions.
42 * Revision 1.3 2004/06/06 17:18:42 bernie
43 * Fix \!CONFIG_KERN_SIGNALS code paths.
45 * Revision 1.2 2004/06/03 11:27:09 bernie
46 * Add dual-license information.
48 * Revision 1.1 2004/05/23 18:23:30 bernie
49 * Import drv/timer module.
56 #include CPU_HEADER(timer)
58 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
59 #include <kern/proc.h>
62 //! Master system clock (1ms accuracy)
63 volatile time_t _clock;
66 #ifndef CONFIG_TIMER_DISABLE_EVENTS
69 * List of active asynchronous timers.
71 REGISTER static List timers_queue;
75 * Add the specified timer to the software timer service queue.
76 * When the delay indicated by the timer expires, the timer
77 * device will execute the event associated with it.
79 * \note Interrupt safe
81 void timer_add(Timer *timer)
86 DISABLE_IRQSAVE(flags);
88 /* Calculate expiration time for this timer */
89 timer->tick = _clock + timer->delay;
91 /* Search for the first node whose expiration time is
92 * greater than the timer we want to add.
94 node = (Timer *)timers_queue.head;
95 while (node->link.succ)
97 /* Stop just after the insertion point */
98 if (node->tick > timer->tick)
101 /* Go to next node */
102 node = (Timer *)node->link.succ;
105 /* Enqueue timer request into the list */
106 INSERTBEFORE(&timer->link, &node->link);
108 ENABLE_IRQRESTORE(flags);
113 * Remove a timer from the timer queue before it has expired
115 Timer *timer_abort(Timer *timer)
118 DISABLE_IRQSAVE(flags);
119 REMOVE(&timer->link);
120 ENABLE_IRQRESTORE(flags);
125 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
129 * Wait for the specified amount of time (expressed in ms)
131 void timer_delay(time_t time)
133 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
136 ASSERT(!sig_check(SIG_SINGLE));
137 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
138 timer_set_delay(&t, time);
140 sig_wait(SIG_SINGLE);
142 #else /* !CONFIG_KERN_SIGNALS */
144 time_t start = timer_gettick();
147 while (timer_gettick() - start < time) { /* nop */ }
149 #endif /* !CONFIG_KERN_SIGNALS */
153 #ifndef CONFIG_TIMER_DISABLE_UDELAY
155 * Wait for the specified amount of time (expressed in microseconds)
157 * \bug In AVR arch the maximum amount of time that can be used as
158 * delay could be very limited, depending on the hardware timer
159 * used. Check timer_avr.h, and what register is used as hptime_t.
161 void timer_udelay(utime_t usec_delay)
163 if (UNLIKELY(usec_delay > 1000))
165 timer_delay(usec_delay / 1000);
169 // FIXME: This multiplication is too slow at run-time. We should try and move it
170 // to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header
172 hptime_t start = timer_hw_hpread();
173 hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul;
175 while (timer_hw_hpread() - start < delay)
178 #endif /* CONFIG_TIMER_DISABLE_UDELAY */
182 * Timer interrupt handler. Find soft timers expired and
183 * trigger corresponding events.
188 * With the Metrowerks compiler, the only way to force the compiler generate
189 * an interrupt service routine is to put a pragma directive within the function
193 #pragma interrupt saveall
196 #ifndef CONFIG_TIMER_DISABLE_EVENTS
204 /* Update the master ms counter */
207 #ifndef CONFIG_TIMER_DISABLE_EVENTS
209 * Check the first timer request in the list and process
210 * it when it has expired. Repeat this check until the
211 * first node has not yet expired. Since the list is sorted
212 * by expiry time, all the following requests are guaranteed
215 while ((timer = (Timer *)timers_queue.head)->link.succ)
217 /* This request in list has not yet expired? */
218 if (_clock < timer->tick)
221 /* Retreat the expired timer */
222 REMOVE(&timer->link);
224 /* Execute the associated event */
225 event_do(&timer->expire);
227 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
236 void timer_init(void)
240 #ifndef CONFIG_TIMER_DISABLE_EVENTS
241 INITLIST(&timers_queue);