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.8 2004/07/18 21:57:07 bernie
19 * Fix preprocessor warning with potentially undefined symbol.
21 * Revision 1.6 2004/06/07 18:10:06 aleph
22 * Remove free pool of timers; use user-provided Timer structure instead
24 * Revision 1.5 2004/06/07 15:56:55 aleph
25 * Some tabs cleanup and add timer strobe macros
27 * Revision 1.4 2004/06/06 18:25:44 bernie
28 * Rename event macros to look like regular functions.
30 * Revision 1.3 2004/06/06 17:18:42 bernie
31 * Fix \!CONFIG_KERN_SIGNALS code paths.
33 * Revision 1.2 2004/06/03 11:27:09 bernie
34 * Add dual-license information.
36 * Revision 1.1 2004/05/23 18:23:30 bernie
37 * Import drv/timer module.
45 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
46 #include <kern/proc.h>
49 #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
51 #elif defined(__AVR__)
52 #include "timer_avr.h"
53 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
54 #include "timer_i196.h"
55 #elif defined (__m56800__)
56 #include "timer_dsp56k.h"
62 //! Master system clock (1ms accuracy)
63 volatile time_t _clock;
65 REGISTER static List timers_queue; /*!< Active timers */
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);
121 * Wait for the specified amount of time (expressed in ms)
123 void timer_delay(time_t time)
125 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
128 ASSERT(!sig_check(SIG_SINGLE));
129 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
130 timer_set_delay(&t, time);
132 sig_wait(SIG_SINGLE);
134 #else /* !CONFIG_KERN_SIGNALS */
136 time_t start = timer_gettick();
139 while (timer_gettick() - start < time) { /* nop */ }
141 #endif /* !CONFIG_KERN_SIGNALS */
146 * Wait for the specified amount of time (expressed in microseconds)
148 * \bug In AVR arch the maximum amount of time that can be used as
149 * delay could be very limited, depending on the hardware timer
150 * used. Check timer_avr.h, and what register is used as hptime_t.
152 void timer_udelay(utime_t usec_delay)
154 if (usec_delay > 1000)
156 timer_delay(usec_delay / 1000);
160 // FIXME: This multiplication is too slow at run-time. We should try and move it
161 // to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header
163 hptime_t start = timer_hw_hpread();
164 hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul;
166 while (timer_hw_hpread() - start < delay)
172 * Timer interrupt handler. Find soft timers expired and
173 * trigger corresponding events.
177 /* With the Metrowerks compiler, the only way to force the compiler generate
178 an interrupt service routine is to put a pragma directive within the function
181 #pragma interrupt saveall
190 /* Update the master ms counter */
194 * Check the first timer request in the list and process
195 * it when it has expired. Repeat this check until the
196 * first node has not yet expired. Since the list is sorted
197 * by expiry time, all the following requests are guaranteed
200 while ((timer = (Timer *)timers_queue.head)->link.succ)
202 /* This request in list has not yet expired? */
203 if (_clock < timer->tick)
206 /* Retreat the expired timer */
207 REMOVE((Node *)timer);
209 /* Execute the associated event */
210 event_doIntr(&timer->expire);
220 void timer_init(void)
224 INITLIST(&timers_queue);