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.10 2004/07/30 14:15:53 rasky
19 * Nuovo supporto unificato per detect della CPU
21 * Revision 1.9 2004/07/21 00:15:13 bernie
22 * Put timer driver on diet.
24 * Revision 1.8 2004/07/18 21:57:07 bernie
25 * Fix preprocessor warning with potentially undefined symbol.
27 * Revision 1.6 2004/06/07 18:10:06 aleph
28 * Remove free pool of timers; use user-provided Timer structure instead
30 * Revision 1.5 2004/06/07 15:56:55 aleph
31 * Some tabs cleanup and add timer strobe macros
33 * Revision 1.4 2004/06/06 18:25:44 bernie
34 * Rename event macros to look like regular functions.
36 * Revision 1.3 2004/06/06 17:18:42 bernie
37 * Fix \!CONFIG_KERN_SIGNALS code paths.
39 * Revision 1.2 2004/06/03 11:27:09 bernie
40 * Add dual-license information.
42 * Revision 1.1 2004/05/23 18:23:30 bernie
43 * Import drv/timer module.
50 #include CPU_HEADER(timer)
52 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
53 #include <kern/proc.h>
56 //! Master system clock (1ms accuracy)
57 volatile time_t _clock;
60 #ifndef CONFIG_TIMER_DISABLE_EVENTS
63 * List of active asynchronous timers.
65 REGISTER static List timers_queue;
69 * Add the specified timer to the software timer service queue.
70 * When the delay indicated by the timer expires, the timer
71 * device will execute the event associated with it.
73 * \note Interrupt safe
75 void timer_add(Timer *timer)
80 DISABLE_IRQSAVE(flags);
82 /* Calculate expiration time for this timer */
83 timer->tick = _clock + timer->delay;
85 /* Search for the first node whose expiration time is
86 * greater than the timer we want to add.
88 node = (Timer *)timers_queue.head;
89 while (node->link.succ)
91 /* Stop just after the insert point */
92 if (node->tick > timer->tick)
96 node = (Timer *)node->link.succ;
99 /* Enqueue timer request into the list */
100 INSERTBEFORE((Node *)timer, (Node *)node);
102 ENABLE_IRQRESTORE(flags);
107 * Remove a timer from the timer queue before it has expired
109 Timer *timer_abort(Timer *timer)
112 DISABLE_IRQSAVE(flags);
113 REMOVE((Node *)timer);
114 ENABLE_IRQRESTORE(flags);
119 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
123 * Wait for the specified amount of time (expressed in ms)
125 void timer_delay(time_t time)
127 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
130 ASSERT(!sig_check(SIG_SINGLE));
131 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
132 timer_set_delay(&t, time);
134 sig_wait(SIG_SINGLE);
136 #else /* !CONFIG_KERN_SIGNALS */
138 time_t start = timer_gettick();
141 while (timer_gettick() - start < time) { /* nop */ }
143 #endif /* !CONFIG_KERN_SIGNALS */
147 #ifndef CONFIG_TIMER_DISABLE_UDELAY
149 * Wait for the specified amount of time (expressed in microseconds)
151 * \bug In AVR arch the maximum amount of time that can be used as
152 * delay could be very limited, depending on the hardware timer
153 * used. Check timer_avr.h, and what register is used as hptime_t.
155 void timer_udelay(utime_t usec_delay)
157 if (usec_delay > 1000)
159 timer_delay(usec_delay / 1000);
163 // FIXME: This multiplication is too slow at run-time. We should try and move it
164 // to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header
166 hptime_t start = timer_hw_hpread();
167 hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul;
169 while (timer_hw_hpread() - start < delay)
172 #endif /* CONFIG_TIMER_DISABLE_UDELAY */
176 * Timer interrupt handler. Find soft timers expired and
177 * trigger corresponding events.
182 * With the Metrowerks compiler, the only way to force the compiler generate
183 * an interrupt service routine is to put a pragma directive within the function
187 #pragma interrupt saveall
190 #ifndef CONFIG_TIMER_DISABLE_EVENTS
198 /* Update the master ms counter */
201 #ifndef CONFIG_TIMER_DISABLE_EVENTS
203 * Check the first timer request in the list and process
204 * it when it has expired. Repeat this check until the
205 * first node has not yet expired. Since the list is sorted
206 * by expiry time, all the following requests are guaranteed
209 while ((timer = (Timer *)timers_queue.head)->link.succ)
211 /* This request in list has not yet expired? */
212 if (_clock < timer->tick)
215 /* Retreat the expired timer */
216 REMOVE((Node *)timer);
218 /* Execute the associated event */
219 event_doIntr(&timer->expire);
221 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
230 void timer_init(void)
234 #ifndef CONFIG_TIMER_DISABLE_EVENTS
235 INITLIST(&timers_queue);