4 * Copyright 2003, 2004, 2005, 2006 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2000 Bernardo Innocenti <bernie@develer.com>
6 * This file is part of DevLib - See README.devlib for information.
9 * \brief Hardware independent timer driver (implementation)
12 * \author Bernardo Innocenti <bernie@develer.com>
17 *#* Revision 1.28 2006/02/10 12:32:52 bernie
18 *#* Update Copyright year.
20 *#* Revision 1.27 2005/11/27 03:04:08 bernie
21 *#* Move test code to timer_test.c; Add OS_HOSTED support.
23 *#* Revision 1.26 2005/11/04 16:20:02 bernie
24 *#* Fix reference to README.devlib in header.
26 *#* Revision 1.25 2005/07/19 07:26:37 bernie
27 *#* Refactor to decouple timer ticks from milliseconds.
29 *#* Revision 1.24 2005/04/11 19:10:28 bernie
30 *#* Include top-level headers from cfg/ subdir.
32 *#* Revision 1.23 2004/12/13 12:07:06 bernie
33 *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
35 *#* Revision 1.22 2004/12/08 09:12:09 bernie
36 *#* Rename time_t to mtime_t.
38 *#* Revision 1.21 2004/11/28 23:20:25 bernie
39 *#* Remove obsolete INITLIST macro.
41 *#* Revision 1.20 2004/11/16 20:59:06 bernie
42 *#* Add watchdog timer support.
48 #include <cfg/debug.h>
49 #include <appconfig.h>
52 * Include platform-specific binding code if we're hosted.
53 * Try the CPU specific one for bare-metal environments.
56 #include OS_CSOURCE(timer)
58 #include CPU_CSOURCE(timer)
62 * Sanity check for config parameters required by this module.
64 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
65 #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
67 #if !defined(CONFIG_WATCHDOG) || ((CONFIG_WATCHDOG != 0) && CONFIG_WATCHDOG != 1)
68 #error CONFIG_WATCHDOG must be set to either 0 or 1 in config.h
75 #if CONFIG_KERNEL && CONFIG_KERN_SIGNALS
76 #include <kern/proc.h>
81 * \def CONFIG_TIMER_STROBE
83 * This is a debug facility that can be used to
84 * monitor timer interrupt activity on an external pin.
86 * To use strobes, redefine the macros TIMER_STROBE_ON,
87 * TIMER_STROBE_OFF and TIMER_STROBE_INIT and set
88 * CONFIG_TIMER_STROBE to 1.
90 #if !defined(CONFIG_TIMER_STROBE) || !CONFIG_TIMER_STROBE
91 #define TIMER_STROBE_ON do {/*nop*/} while(0)
92 #define TIMER_STROBE_OFF do {/*nop*/} while(0)
93 #define TIMER_STROBE_INIT do {/*nop*/} while(0)
97 //! Master system clock (1 tick accuracy)
98 volatile ticks_t _clock;
101 #ifndef CONFIG_TIMER_DISABLE_EVENTS
104 * List of active asynchronous timers.
106 REGISTER static List timers_queue;
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)
122 /* Inserting timers twice causes mayhem. */
123 ASSERT(timer->magic != TIMER_MAGIC_ACTIVE);
124 DB(timer->magic = TIMER_MAGIC_ACTIVE;)
126 IRQ_SAVE_DISABLE(flags);
128 /* Calculate expiration time for this timer */
129 timer->tick = _clock + timer->_delay;
132 * Search for the first node whose expiration time is
133 * greater than the timer we want to add.
135 node = (Timer *)LIST_HEAD(&timers_queue);
136 while (node->link.succ)
139 * Stop just after the insertion point.
140 * (this fancy compare takes care of wrap-arounds).
142 if (node->tick - timer->tick > 0)
145 /* Go to next node */
146 node = (Timer *)node->link.succ;
149 /* Enqueue timer request into the list */
150 INSERTBEFORE(&timer->link, &node->link);
157 * Remove a timer from the timer queue before it has expired.
159 Timer *timer_abort(Timer *timer)
161 ATOMIC(REMOVE(&timer->link));
162 DB(timer->magic = TIMER_MAGIC_INACTIVE;)
167 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
171 * Wait for the specified amount of time (expressed in ms).
173 void timer_delayTicks(ticks_t delay)
175 #if defined(IRQ_GETSTATE)
176 /* We shouldn't sleep with interrupts disabled */
177 ASSERT(IRQ_GETSTATE());
180 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
183 ASSERT(!sig_check(SIG_SINGLE));
184 timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
185 timer_set_delay(&t, delay);
187 sig_wait(SIG_SINGLE);
189 #else /* !CONFIG_KERN_SIGNALS */
191 ticks_t start = timer_clock();
194 while (timer_clock() - start < delay)
201 #endif /* !CONFIG_KERN_SIGNALS */
205 #ifndef CONFIG_TIMER_DISABLE_UDELAY
208 * Busy wait until the specified amount of high-precision ticks have elapsed.
210 * \note This function is interrupt safe, the only
211 * requirement is a running hardware timer.
213 void timer_busyWait(hptime_t delay)
215 hptime_t now, prev = timer_hw_hpread();
220 now = timer_hw_hpread();
222 * We rely on hptime_t being unsigned here to
223 * reduce the modulo to an AND in the common
224 * case of TIMER_HW_CNT.
226 delta = (now - prev) % TIMER_HW_CNT;
235 * Wait for the specified amount of time (expressed in microseconds).
237 * \bug In AVR arch the maximum amount of time that can be used as
238 * delay could be very limited, depending on the hardware timer
239 * used. Check timer_avr.h, and what register is used as hptime_t.
241 void timer_delayHp(hptime_t delay)
243 if (UNLIKELY(delay > us_to_hptime(1000)))
245 timer_delayTicks(delay / (TIMER_HW_HPTICKS_PER_SEC / TIMER_TICKS_PER_SEC));
246 delay %= (TIMER_HW_HPTICKS_PER_SEC / TIMER_TICKS_PER_SEC);
249 timer_busyWait(delay);
251 #endif /* CONFIG_TIMER_DISABLE_UDELAY */
255 * Timer interrupt handler. Find soft timers expired and
256 * trigger corresponding events.
261 * With the Metrowerks compiler, the only way to force the compiler generate
262 * an interrupt service routine is to put a pragma directive within the function
266 #pragma interrupt saveall
269 #ifndef CONFIG_TIMER_DISABLE_EVENTS
277 /* Update the master ms counter */
280 #ifndef CONFIG_TIMER_DISABLE_EVENTS
282 * Check the first timer request in the list and process
283 * it when it has expired. Repeat this check until the
284 * first node has not yet expired. Since the list is sorted
285 * by expiry time, all the following requests are guaranteed
288 while ((timer = (Timer *)LIST_HEAD(&timers_queue))->link.succ)
290 /* This request in list has not yet expired? */
291 if (_clock - timer->tick < 0)
294 /* Retreat the expired timer */
295 REMOVE(&timer->link);
296 DB(timer->magic = TIMER_MAGIC_INACTIVE;)
298 /* Execute the associated event */
299 event_do(&timer->expire);
301 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
310 void timer_init(void)
314 #ifndef CONFIG_TIMER_DISABLE_EVENTS
315 LIST_INIT(&timers_queue);