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.11 2004/08/02 20:20:29 aleph
19 * Merge from project_ks
21 * Revision 1.10 2004/07/30 14:15:53 rasky
22 * Nuovo supporto unificato per detect della CPU
24 * Revision 1.9 2004/07/21 00:15:13 bernie
25 * Put timer driver on diet.
27 * Revision 1.8 2004/07/18 21:57:07 bernie
28 * Fix preprocessor warning with potentially undefined symbol.
30 * Revision 1.6 2004/06/07 18:10:06 aleph
31 * Remove free pool of timers; use user-provided Timer structure instead
33 * Revision 1.5 2004/06/07 15:56:55 aleph
34 * Some tabs cleanup and add timer strobe macros
36 * Revision 1.4 2004/06/06 18:25:44 bernie
37 * Rename event macros to look like regular functions.
39 * Revision 1.3 2004/06/06 17:18:42 bernie
40 * Fix \!CONFIG_KERN_SIGNALS code paths.
42 * Revision 1.2 2004/06/03 11:27:09 bernie
43 * Add dual-license information.
45 * Revision 1.1 2004/05/23 18:23:30 bernie
46 * Import drv/timer module.
53 #include CPU_HEADER(timer)
55 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
56 #include <kern/proc.h>
59 //! Master system clock (1ms accuracy)
60 volatile time_t _clock;
63 #ifndef CONFIG_TIMER_DISABLE_EVENTS
66 * List of active asynchronous timers.
68 REGISTER static List timers_queue;
72 * Add the specified timer to the software timer service queue.
73 * When the delay indicated by the timer expires, the timer
74 * device will execute the event associated with it.
76 * \note Interrupt safe
78 void timer_add(Timer *timer)
83 DISABLE_IRQSAVE(flags);
85 /* Calculate expiration time for this timer */
86 timer->tick = _clock + timer->delay;
88 /* Search for the first node whose expiration time is
89 * greater than the timer we want to add.
91 node = (Timer *)timers_queue.head;
92 while (node->link.succ)
94 /* Stop just after the insert point */
95 if (node->tick > timer->tick)
99 node = (Timer *)node->link.succ;
102 /* Enqueue timer request into the list */
103 INSERTBEFORE((Node *)timer, (Node *)node);
105 ENABLE_IRQRESTORE(flags);
110 * Remove a timer from the timer queue before it has expired
112 Timer *timer_abort(Timer *timer)
115 DISABLE_IRQSAVE(flags);
116 REMOVE((Node *)timer);
117 ENABLE_IRQRESTORE(flags);
122 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
126 * Wait for the specified amount of time (expressed in ms)
128 void timer_delay(time_t time)
130 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
133 ASSERT(!sig_check(SIG_SINGLE));
134 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
135 timer_set_delay(&t, time);
137 sig_wait(SIG_SINGLE);
139 #else /* !CONFIG_KERN_SIGNALS */
141 time_t start = timer_gettick();
144 while (timer_gettick() - start < time) { /* nop */ }
146 #endif /* !CONFIG_KERN_SIGNALS */
150 #ifndef CONFIG_TIMER_DISABLE_UDELAY
152 * Wait for the specified amount of time (expressed in microseconds)
154 * \bug In AVR arch the maximum amount of time that can be used as
155 * delay could be very limited, depending on the hardware timer
156 * used. Check timer_avr.h, and what register is used as hptime_t.
158 void timer_udelay(utime_t usec_delay)
160 if (usec_delay > 1000)
162 timer_delay(usec_delay / 1000);
166 // FIXME: This multiplication is too slow at run-time. We should try and move it
167 // to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header
169 hptime_t start = timer_hw_hpread();
170 hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul;
172 while (timer_hw_hpread() - start < delay)
175 #endif /* CONFIG_TIMER_DISABLE_UDELAY */
179 * Timer interrupt handler. Find soft timers expired and
180 * trigger corresponding events.
185 * With the Metrowerks compiler, the only way to force the compiler generate
186 * an interrupt service routine is to put a pragma directive within the function
190 #pragma interrupt saveall
193 #ifndef CONFIG_TIMER_DISABLE_EVENTS
201 /* Update the master ms counter */
204 #ifndef CONFIG_TIMER_DISABLE_EVENTS
206 * Check the first timer request in the list and process
207 * it when it has expired. Repeat this check until the
208 * first node has not yet expired. Since the list is sorted
209 * by expiry time, all the following requests are guaranteed
212 while ((timer = (Timer *)timers_queue.head)->link.succ)
214 /* This request in list has not yet expired? */
215 if (_clock < timer->tick)
218 /* Retreat the expired timer */
219 REMOVE((Node *)timer);
221 /* Execute the associated event */
222 event_do(&timer->expire);
224 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
233 void timer_init(void)
237 #ifndef CONFIG_TIMER_DISABLE_EVENTS
238 INITLIST(&timers_queue);