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.6 2004/06/07 18:10:06 aleph
19 * Remove free pool of timers; use user-provided Timer structure instead
21 * Revision 1.5 2004/06/07 15:56:55 aleph
22 * Some tabs cleanup and add timer strobe macros
24 * Revision 1.4 2004/06/06 18:25:44 bernie
25 * Rename event macros to look like regular functions.
27 * Revision 1.3 2004/06/06 17:18:42 bernie
28 * Fix \!CONFIG_KERN_SIGNALS code paths.
30 * Revision 1.2 2004/06/03 11:27:09 bernie
31 * Add dual-license information.
33 * Revision 1.1 2004/05/23 18:23:30 bernie
34 * Import drv/timer module.
42 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
43 #include <kern/proc.h>
46 #if (ARCH & ARCH_EMUL)
48 #elif defined(__AVR__)
49 #include "timer_avr.h"
50 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
51 #include "timer_i196.h"
52 #elif defined (__m56800__)
53 #include "timer_dsp56k.h"
59 //! Master system clock (1ms accuracy)
60 volatile time_t _clock;
62 REGISTER static List timers_queue; /*!< Active timers */
66 * Add the specified timer to the software timer service queue.
67 * When the delay indicated by the timer expires, the timer
68 * device will execute the event associated with it.
70 * \note Interrupt safe
72 void timer_add(Timer *timer)
77 DISABLE_IRQSAVE(flags);
79 /* Calculate expiration time for this timer */
80 timer->tick = _clock + timer->delay;
82 /* Search for the first node whose expiration time is
83 * greater than the timer we want to add.
85 node = (Timer *)timers_queue.head;
86 while (node->link.succ)
88 /* Stop just after the insert point */
89 if (node->tick > timer->tick)
93 node = (Timer *)node->link.succ;
96 /* Enqueue timer request into the list */
97 INSERTBEFORE((Node *)timer, (Node *)node);
99 ENABLE_IRQRESTORE(flags);
104 * Remove a timer from the timer queue before it has expired
106 Timer *timer_abort(Timer *timer)
109 DISABLE_IRQSAVE(flags);
110 REMOVE((Node *)timer);
111 ENABLE_IRQRESTORE(flags);
118 * Wait for the specified amount of time (expressed in ms)
120 void timer_delay(time_t time)
122 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
125 ASSERT(!sig_check(SIG_SINGLE));
126 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
127 timer_set_delay(&t, time);
129 sig_wait(SIG_SINGLE);
131 #else /* !CONFIG_KERN_SIGNALS */
133 time_t start = timer_gettick();
136 while (timer_gettick() - start < time) { /* nop */ }
138 #endif /* !CONFIG_KERN_SIGNALS */
143 * Wait for the specified amount of time (expressed in microseconds)
145 * \bug In AVR arch the maximum amount of time that can be used as
146 * delay could be very limited, depending on the hardware timer
147 * used. Check timer_avr.h, and what register is used as hptime_t.
149 void timer_udelay(utime_t usec_delay)
151 if (usec_delay > 1000)
153 timer_delay(usec_delay / 1000);
157 // FIXME: This multiplication is too slow at run-time. We should try and move it
158 // to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header
160 hptime_t start = timer_hw_hpread();
161 hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul;
163 while (timer_hw_hpread() - start < delay)
169 * Timer interrupt handler. Find soft timers expired and
170 * trigger corresponding events.
174 /* With the Metrowerks compiler, the only way to force the compiler generate
175 an interrupt service routine is to put a pragma directive within the function
178 #pragma interrupt saveall
187 /* Update the master ms counter */
191 * Check the first timer request in the list and process
192 * it when it has expired. Repeat this check until the
193 * first node has not yet expired. Since the list is sorted
194 * by expiry time, all the following requests are guaranteed
197 while ((timer = (Timer *)timers_queue.head)->link.succ)
199 /* This request in list has not yet expired? */
200 if (_clock < timer->tick)
203 /* Retreat the expired timer */
204 REMOVE((Node *)timer);
206 /* Execute the associated event */
207 event_doIntr(&timer->expire);
217 void timer_init(void)
221 INITLIST(&timers_queue);