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.4 2004/06/06 18:25:44 bernie
19 * Rename event macros to look like regular functions.
21 * Revision 1.3 2004/06/06 17:18:42 bernie
22 * Fix \!CONFIG_KERN_SIGNALS code paths.
24 * Revision 1.2 2004/06/03 11:27:09 bernie
25 * Add dual-license information.
27 * Revision 1.1 2004/05/23 18:23:30 bernie
28 * Import drv/timer module.
36 #ifdef CONFIG_KERN_SIGNALS
37 #include <kern/proc.h>
40 #if (ARCH & ARCH_EMUL)
44 #elif defined(__AVR__)
45 # include "timer_avr.h"
46 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
47 # include "timer_i196.h"
48 #elif defined (__m56800__)
49 # include "timer_dsp56k.h"
51 # error Unknown system
55 /*! Number of available timers */
59 //! Master system clock (1ms accuracy)
60 volatile time_t _clock;
62 static Timer soft_timers[MAX_TIMERS]; /*!< Pool of Timer structures */
63 static List timers_pool; /*!< Pool of free timers */
64 REGISTER static List timers_queue; /*!< Active timers */
68 * Return a new timer picking and removing it from the available
69 * timers pool. Return NULL if no more timers are available.
71 Timer *timer_new(void)
76 DISABLE_IRQSAVE(flags);
78 /* Should never happen */
79 if (ISLISTEMPTY(&timers_pool))
81 ENABLE_IRQRESTORE(flags);
82 DB(kprintf("Tmrspool empty\n");)
86 /* Get a timer from the free pool */
87 timer = (Timer *)timers_pool.head;
88 REMOVE((Node *)timer);
90 ENABLE_IRQRESTORE(flags);
97 * Delete a timer, putting it in the available timers queue.
99 void timer_delete(Timer *timer)
102 DISABLE_IRQSAVE(flags);
103 ADDHEAD(&timers_pool, &timer->link);
104 ENABLE_IRQRESTORE(flags);
109 * Add the specified timer to the software timer service queue.
110 * When the delay indicated by the timer expires, the timer
111 * device will execute the event associated with it.
113 * \note Interrupt safe
115 void timer_add(Timer *timer)
120 DISABLE_IRQSAVE(flags);
122 /* Calculate expiration time for this timer */
123 timer->tick = _clock + timer->delay;
125 /* Search for the first node whose expiration time is
126 * greater than the timer we want to add.
128 node = (Timer *)timers_queue.head;
129 while (node->link.succ)
131 /* Stop just after the insert point */
132 if (node->tick > timer->tick)
135 /* Go to next node */
136 node = (Timer *)node->link.succ;
139 /* Enqueue timer request into the list */
140 INSERTBEFORE((Node *)timer, (Node *)node);
142 ENABLE_IRQRESTORE(flags);
147 * Remove a timer from the timer queue before it has expired
149 Timer *timer_abort(Timer *timer)
152 DISABLE_IRQSAVE(flags);
153 REMOVE((Node *)timer);
154 ENABLE_IRQRESTORE(flags);
161 * Wait for the specified amount of time (expressed in ms)
163 void timer_delay(time_t time)
165 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
168 ASSERT(!sig_check(SIG_SINGLE));
169 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
170 timer_set_delay(&t, time);
172 sig_wait(SIG_SINGLE);
174 #else /* !CONFIG_KERN_SIGNALS */
176 time_t start = timer_gettick();
179 while (timer_gettick() - start < time) { /* nop */ }
181 #endif /* !CONFIG_KERN_SIGNALS */
186 * Wait for the specified amount of time (expressed in microseconds)
188 void timer_udelay(utime_t usec_delay)
190 if (usec_delay > 1000)
192 timer_delay(usec_delay / 1000);
196 // FIXME: This multiplication is too slow at run-time. We should try and move it
197 // to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header
199 hptime_t start = timer_hw_hpread();
200 hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul;
202 while (timer_hw_hpread() - start < delay)
208 * Timer interrupt handler. Find soft timers expired and
209 * trigger corresponding events.
213 /* With the Metrowerks compiler, the only way to force the compiler generate
214 an interrupt service routine is to put a pragma directive within the function
217 #pragma interrupt saveall
224 /* Update the master ms counter */
228 * Check the first timer request in the list and process
229 * it when it has expired. Repeat this check until the
230 * first node has not yet expired. Since the list is sorted
231 * by expiry time, all the following requests are guaranteed
234 while ((timer = (Timer *)timers_queue.head)->link.succ)
236 /* This request in list has not yet expired? */
237 if (_clock < timer->tick)
240 /* Retreat the expired timer */
241 REMOVE((Node *)timer);
243 /* Execute the associated event */
244 event_doIntr(&timer->expire);
253 void timer_init(void)
257 INITLIST(&timers_queue);
258 INITLIST(&timers_pool);
260 /* Init all software timers in the free pool */
261 for (i = 0; i < MAX_TIMERS; i++)
262 ADDTAIL(&timers_pool, (Node *)&soft_timers[i]);