4 * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2000 Bernardo Innocenti <bernie@develer.com>
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief Hardware independent timer driver (implementation)
18 * Revision 1.1 2004/05/23 18:23:30 bernie
19 * Import drv/timer module.
27 #ifdef CONFIG_KERN_SIGNALS
28 #include <kern/proc.h>
31 #if (ARCH & ARCH_EMUL)
35 #elif defined(__AVR__)
36 # include "timer_avr.h"
37 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
38 # include "timer_i196.h"
39 #elif defined (__m56800__)
40 # include "timer_dsp56k.h"
42 # error Unknown system
46 /*! Number of available timers */
50 //! Master system clock (1ms accuracy)
51 volatile time_t _clock;
53 static Timer soft_timers[MAX_TIMERS]; /*!< Pool of Timer structures */
54 static List timers_pool; /*!< Pool of free timers */
55 REGISTER static List timers_queue; /*!< Active timers */
59 * Return a new timer picking and removing it from the available
60 * timers pool. Return NULL if no more timers are available.
62 Timer *timer_new(void)
67 DISABLE_IRQSAVE(flags);
69 /* Should never happen */
70 if (ISLISTEMPTY(&timers_pool))
72 ENABLE_IRQRESTORE(flags);
73 DB(kprintf("Tmrspool empty\n");)
77 /* Get a timer from the free pool */
78 timer = (Timer *)timers_pool.head;
79 REMOVE((Node *)timer);
81 ENABLE_IRQRESTORE(flags);
88 * Delete a timer, putting it in the available timers queue.
90 void timer_delete(Timer *timer)
93 DISABLE_IRQSAVE(flags);
94 ADDHEAD(&timers_pool, &timer->link);
95 ENABLE_IRQRESTORE(flags);
100 * Add the specified timer to the software timer service queue.
101 * When the delay indicated by the timer expires, the timer
102 * device will execute the event associated with it.
104 * \note Interrupt safe
106 void timer_add(Timer *timer)
111 DISABLE_IRQSAVE(flags);
113 /* Calculate expiration time for this timer */
114 timer->tick = _clock + timer->delay;
116 /* Search for the first node whose expiration time is
117 * greater than the timer we want to add.
119 node = (Timer *)timers_queue.head;
120 while (node->link.succ)
122 /* Stop just after the insert point */
123 if (node->tick > timer->tick)
126 /* Go to next node */
127 node = (Timer *)node->link.succ;
130 /* Enqueue timer request into the list */
131 INSERTBEFORE((Node *)timer, (Node *)node);
133 ENABLE_IRQRESTORE(flags);
138 * Remove a timer from the timer queue before it has expired
140 Timer *timer_abort(Timer *timer)
143 DISABLE_IRQSAVE(flags);
144 REMOVE((Node *)timer);
145 ENABLE_IRQRESTORE(flags);
152 * Wait for the specified amount of time (expressed in ms)
154 void timer_delay(time_t time)
156 #ifdef CONFIG_KERN_SIGNALS
159 ASSERT(!sig_check(SIG_SINGLE));
160 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
161 timer_set_delay(&t, time);
163 sig_wait(SIG_SINGLE);
165 time_t start = timer_gettick();
167 while (timer_gettick() - start < time) { /* nop */ }
172 * Wait for the specified amount of time (expressed in microseconds)
174 void timer_udelay(utime_t usec_delay)
176 if (usec_delay > 1000)
178 timer_delay(usec_delay / 1000);
182 // FIXME: This multiplication is too slow at run-time. We should try and move it
183 // to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header
185 hptime_t start = timer_hw_hpread();
186 hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul;
188 while (timer_hw_hpread() - start < delay)
194 * Timer interrupt handler. Find soft timers expired and
195 * trigger corresponding events.
199 /* With the Metrowerks compiler, the only way to force the compiler generate
200 an interrupt service routine is to put a pragma directive within the function
203 #pragma interrupt saveall
210 /* Update the master ms counter */
214 * Check the first timer request in the list and process
215 * it when it has expired. Repeat this check until the
216 * first node has not yet expired. Since the list is sorted
217 * by expiry time, all the following requests are guaranteed
220 while ((timer = (Timer *)timers_queue.head)->link.succ)
222 /* This request in list has not yet expired? */
223 if (_clock < timer->tick)
226 /* Retreat the expired timer */
227 REMOVE((Node *)timer);
229 /* Execute the associated event */
230 DOEVENT_INTR(&timer->expire);
239 void timer_init(void)
243 INITLIST(&timers_queue);
244 INITLIST(&timers_pool);
246 /* Init all software timers in the free pool */
247 for (i = 0; i < MAX_TIMERS; i++)
248 ADDTAIL(&timers_pool, (Node *)&soft_timers[i]);