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.5 2004/06/07 15:56:55 aleph
19 * Some tabs cleanup and add timer strobe macros
21 * Revision 1.4 2004/06/06 18:25:44 bernie
22 * Rename event macros to look like regular functions.
24 * Revision 1.3 2004/06/06 17:18:42 bernie
25 * Fix \!CONFIG_KERN_SIGNALS code paths.
27 * Revision 1.2 2004/06/03 11:27:09 bernie
28 * Add dual-license information.
30 * Revision 1.1 2004/05/23 18:23:30 bernie
31 * Import drv/timer module.
39 #ifdef CONFIG_KERN_SIGNALS
40 #include <kern/proc.h>
43 #if (ARCH & ARCH_EMUL)
45 #elif defined(__AVR__)
46 #include "timer_avr.h"
47 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
48 #include "timer_i196.h"
49 #elif defined (__m56800__)
50 #include "timer_dsp56k.h"
56 /*! Number of available timers */
60 //! Master system clock (1ms accuracy)
61 volatile time_t _clock;
63 static Timer soft_timers[MAX_TIMERS]; /*!< Pool of Timer structures */
64 static List timers_pool; /*!< Pool of free timers */
65 REGISTER static List timers_queue; /*!< Active timers */
69 * Return a new timer picking and removing it from the available
70 * timers pool. Return NULL if no more timers are available.
72 Timer *timer_new(void)
77 DISABLE_IRQSAVE(flags);
79 /* Should never happen */
80 if (ISLISTEMPTY(&timers_pool))
82 ENABLE_IRQRESTORE(flags);
83 DB(kprintf("Tmrspool empty\n");)
87 /* Get a timer from the free pool */
88 timer = (Timer *)timers_pool.head;
89 REMOVE((Node *)timer);
91 ENABLE_IRQRESTORE(flags);
98 * Delete a timer, putting it in the available timers queue.
100 void timer_delete(Timer *timer)
103 DISABLE_IRQSAVE(flags);
104 ADDHEAD(&timers_pool, &timer->link);
105 ENABLE_IRQRESTORE(flags);
110 * Add the specified timer to the software timer service queue.
111 * When the delay indicated by the timer expires, the timer
112 * device will execute the event associated with it.
114 * \note Interrupt safe
116 void timer_add(Timer *timer)
121 DISABLE_IRQSAVE(flags);
123 /* Calculate expiration time for this timer */
124 timer->tick = _clock + timer->delay;
126 /* Search for the first node whose expiration time is
127 * greater than the timer we want to add.
129 node = (Timer *)timers_queue.head;
130 while (node->link.succ)
132 /* Stop just after the insert point */
133 if (node->tick > timer->tick)
136 /* Go to next node */
137 node = (Timer *)node->link.succ;
140 /* Enqueue timer request into the list */
141 INSERTBEFORE((Node *)timer, (Node *)node);
143 ENABLE_IRQRESTORE(flags);
148 * Remove a timer from the timer queue before it has expired
150 Timer *timer_abort(Timer *timer)
153 DISABLE_IRQSAVE(flags);
154 REMOVE((Node *)timer);
155 ENABLE_IRQRESTORE(flags);
162 * Wait for the specified amount of time (expressed in ms)
164 void timer_delay(time_t time)
166 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
169 ASSERT(!sig_check(SIG_SINGLE));
170 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
171 timer_set_delay(&t, time);
173 sig_wait(SIG_SINGLE);
175 #else /* !CONFIG_KERN_SIGNALS */
177 time_t start = timer_gettick();
180 while (timer_gettick() - start < time) { /* nop */ }
182 #endif /* !CONFIG_KERN_SIGNALS */
187 * Wait for the specified amount of time (expressed in microseconds)
189 * \bug In AVR arch the maximum amount of time that can be used as
190 * delay could be very limited, depending on the hardware timer
191 * used. Check timer_avr.h, and what register is used as hptime_t.
193 void timer_udelay(utime_t usec_delay)
195 if (usec_delay > 1000)
197 timer_delay(usec_delay / 1000);
201 // FIXME: This multiplication is too slow at run-time. We should try and move it
202 // to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header
204 hptime_t start = timer_hw_hpread();
205 hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul;
207 while (timer_hw_hpread() - start < delay)
213 * Timer interrupt handler. Find soft timers expired and
214 * trigger corresponding events.
218 /* With the Metrowerks compiler, the only way to force the compiler generate
219 an interrupt service routine is to put a pragma directive within the function
222 #pragma interrupt saveall
231 /* Update the master ms counter */
235 * Check the first timer request in the list and process
236 * it when it has expired. Repeat this check until the
237 * first node has not yet expired. Since the list is sorted
238 * by expiry time, all the following requests are guaranteed
241 while ((timer = (Timer *)timers_queue.head)->link.succ)
243 /* This request in list has not yet expired? */
244 if (_clock < timer->tick)
247 /* Retreat the expired timer */
248 REMOVE((Node *)timer);
250 /* Execute the associated event */
251 event_doIntr(&timer->expire);
261 void timer_init(void)
267 INITLIST(&timers_queue);
268 INITLIST(&timers_pool);
270 /* Init all software timers in the free pool */
271 for (i = 0; i < MAX_TIMERS; i++)
272 ADDTAIL(&timers_pool, (Node *)&soft_timers[i]);