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.2 2004/06/03 11:27:09 bernie
19 * Add dual-license information.
21 * Revision 1.1 2004/05/23 18:23:30 bernie
22 * Import drv/timer module.
30 #ifdef CONFIG_KERN_SIGNALS
31 #include <kern/proc.h>
34 #if (ARCH & ARCH_EMUL)
38 #elif defined(__AVR__)
39 # include "timer_avr.h"
40 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
41 # include "timer_i196.h"
42 #elif defined (__m56800__)
43 # include "timer_dsp56k.h"
45 # error Unknown system
49 /*! Number of available timers */
53 //! Master system clock (1ms accuracy)
54 volatile time_t _clock;
56 static Timer soft_timers[MAX_TIMERS]; /*!< Pool of Timer structures */
57 static List timers_pool; /*!< Pool of free timers */
58 REGISTER static List timers_queue; /*!< Active timers */
62 * Return a new timer picking and removing it from the available
63 * timers pool. Return NULL if no more timers are available.
65 Timer *timer_new(void)
70 DISABLE_IRQSAVE(flags);
72 /* Should never happen */
73 if (ISLISTEMPTY(&timers_pool))
75 ENABLE_IRQRESTORE(flags);
76 DB(kprintf("Tmrspool empty\n");)
80 /* Get a timer from the free pool */
81 timer = (Timer *)timers_pool.head;
82 REMOVE((Node *)timer);
84 ENABLE_IRQRESTORE(flags);
91 * Delete a timer, putting it in the available timers queue.
93 void timer_delete(Timer *timer)
96 DISABLE_IRQSAVE(flags);
97 ADDHEAD(&timers_pool, &timer->link);
98 ENABLE_IRQRESTORE(flags);
103 * Add the specified timer to the software timer service queue.
104 * When the delay indicated by the timer expires, the timer
105 * device will execute the event associated with it.
107 * \note Interrupt safe
109 void timer_add(Timer *timer)
114 DISABLE_IRQSAVE(flags);
116 /* Calculate expiration time for this timer */
117 timer->tick = _clock + timer->delay;
119 /* Search for the first node whose expiration time is
120 * greater than the timer we want to add.
122 node = (Timer *)timers_queue.head;
123 while (node->link.succ)
125 /* Stop just after the insert point */
126 if (node->tick > timer->tick)
129 /* Go to next node */
130 node = (Timer *)node->link.succ;
133 /* Enqueue timer request into the list */
134 INSERTBEFORE((Node *)timer, (Node *)node);
136 ENABLE_IRQRESTORE(flags);
141 * Remove a timer from the timer queue before it has expired
143 Timer *timer_abort(Timer *timer)
146 DISABLE_IRQSAVE(flags);
147 REMOVE((Node *)timer);
148 ENABLE_IRQRESTORE(flags);
155 * Wait for the specified amount of time (expressed in ms)
157 void timer_delay(time_t time)
159 #ifdef CONFIG_KERN_SIGNALS
162 ASSERT(!sig_check(SIG_SINGLE));
163 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
164 timer_set_delay(&t, time);
166 sig_wait(SIG_SINGLE);
168 time_t start = timer_gettick();
170 while (timer_gettick() - start < time) { /* nop */ }
175 * Wait for the specified amount of time (expressed in microseconds)
177 void timer_udelay(utime_t usec_delay)
179 if (usec_delay > 1000)
181 timer_delay(usec_delay / 1000);
185 // FIXME: This multiplication is too slow at run-time. We should try and move it
186 // to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header
188 hptime_t start = timer_hw_hpread();
189 hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul;
191 while (timer_hw_hpread() - start < delay)
197 * Timer interrupt handler. Find soft timers expired and
198 * trigger corresponding events.
202 /* With the Metrowerks compiler, the only way to force the compiler generate
203 an interrupt service routine is to put a pragma directive within the function
206 #pragma interrupt saveall
213 /* Update the master ms counter */
217 * Check the first timer request in the list and process
218 * it when it has expired. Repeat this check until the
219 * first node has not yet expired. Since the list is sorted
220 * by expiry time, all the following requests are guaranteed
223 while ((timer = (Timer *)timers_queue.head)->link.succ)
225 /* This request in list has not yet expired? */
226 if (_clock < timer->tick)
229 /* Retreat the expired timer */
230 REMOVE((Node *)timer);
232 /* Execute the associated event */
233 DOEVENT_INTR(&timer->expire);
242 void timer_init(void)
246 INITLIST(&timers_queue);
247 INITLIST(&timers_pool);
249 /* Init all software timers in the free pool */
250 for (i = 0; i < MAX_TIMERS; i++)
251 ADDTAIL(&timers_pool, (Node *)&soft_timers[i]);