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.3 2004/06/06 17:18:42 bernie
19 * Fix \!CONFIG_KERN_SIGNALS code paths.
21 * Revision 1.2 2004/06/03 11:27:09 bernie
22 * Add dual-license information.
24 * Revision 1.1 2004/05/23 18:23:30 bernie
25 * Import drv/timer module.
33 #ifdef CONFIG_KERN_SIGNALS
34 #include <kern/proc.h>
37 #if (ARCH & ARCH_EMUL)
41 #elif defined(__AVR__)
42 # include "timer_avr.h"
43 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
44 # include "timer_i196.h"
45 #elif defined (__m56800__)
46 # include "timer_dsp56k.h"
48 # error Unknown system
52 /*! Number of available timers */
56 //! Master system clock (1ms accuracy)
57 volatile time_t _clock;
59 static Timer soft_timers[MAX_TIMERS]; /*!< Pool of Timer structures */
60 static List timers_pool; /*!< Pool of free timers */
61 REGISTER static List timers_queue; /*!< Active timers */
65 * Return a new timer picking and removing it from the available
66 * timers pool. Return NULL if no more timers are available.
68 Timer *timer_new(void)
73 DISABLE_IRQSAVE(flags);
75 /* Should never happen */
76 if (ISLISTEMPTY(&timers_pool))
78 ENABLE_IRQRESTORE(flags);
79 DB(kprintf("Tmrspool empty\n");)
83 /* Get a timer from the free pool */
84 timer = (Timer *)timers_pool.head;
85 REMOVE((Node *)timer);
87 ENABLE_IRQRESTORE(flags);
94 * Delete a timer, putting it in the available timers queue.
96 void timer_delete(Timer *timer)
99 DISABLE_IRQSAVE(flags);
100 ADDHEAD(&timers_pool, &timer->link);
101 ENABLE_IRQRESTORE(flags);
106 * Add the specified timer to the software timer service queue.
107 * When the delay indicated by the timer expires, the timer
108 * device will execute the event associated with it.
110 * \note Interrupt safe
112 void timer_add(Timer *timer)
117 DISABLE_IRQSAVE(flags);
119 /* Calculate expiration time for this timer */
120 timer->tick = _clock + timer->delay;
122 /* Search for the first node whose expiration time is
123 * greater than the timer we want to add.
125 node = (Timer *)timers_queue.head;
126 while (node->link.succ)
128 /* Stop just after the insert point */
129 if (node->tick > timer->tick)
132 /* Go to next node */
133 node = (Timer *)node->link.succ;
136 /* Enqueue timer request into the list */
137 INSERTBEFORE((Node *)timer, (Node *)node);
139 ENABLE_IRQRESTORE(flags);
144 * Remove a timer from the timer queue before it has expired
146 Timer *timer_abort(Timer *timer)
149 DISABLE_IRQSAVE(flags);
150 REMOVE((Node *)timer);
151 ENABLE_IRQRESTORE(flags);
158 * Wait for the specified amount of time (expressed in ms)
160 void timer_delay(time_t time)
162 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
165 ASSERT(!sig_check(SIG_SINGLE));
166 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
167 timer_set_delay(&t, time);
169 sig_wait(SIG_SINGLE);
171 #else /* !CONFIG_KERN_SIGNALS */
173 time_t start = timer_gettick();
176 while (timer_gettick() - start < time) { /* nop */ }
178 #endif /* !CONFIG_KERN_SIGNALS */
183 * Wait for the specified amount of time (expressed in microseconds)
185 void timer_udelay(utime_t usec_delay)
187 if (usec_delay > 1000)
189 timer_delay(usec_delay / 1000);
193 // FIXME: This multiplication is too slow at run-time. We should try and move it
194 // to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header
196 hptime_t start = timer_hw_hpread();
197 hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul;
199 while (timer_hw_hpread() - start < delay)
205 * Timer interrupt handler. Find soft timers expired and
206 * trigger corresponding events.
210 /* With the Metrowerks compiler, the only way to force the compiler generate
211 an interrupt service routine is to put a pragma directive within the function
214 #pragma interrupt saveall
221 /* Update the master ms counter */
225 * Check the first timer request in the list and process
226 * it when it has expired. Repeat this check until the
227 * first node has not yet expired. Since the list is sorted
228 * by expiry time, all the following requests are guaranteed
231 while ((timer = (Timer *)timers_queue.head)->link.succ)
233 /* This request in list has not yet expired? */
234 if (_clock < timer->tick)
237 /* Retreat the expired timer */
238 REMOVE((Node *)timer);
240 /* Execute the associated event */
241 DOEVENT_INTR(&timer->expire);
250 void timer_init(void)
254 INITLIST(&timers_queue);
255 INITLIST(&timers_pool);
257 /* Init all software timers in the free pool */
258 for (i = 0; i < MAX_TIMERS; i++)
259 ADDTAIL(&timers_pool, (Node *)&soft_timers[i]);