4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004, 2005, 2006 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2000, 2008 Bernie Innocenti <bernie@codewiz.org>
33 * \brief Hardware independent timer driver (implementation)
36 * \author Bernie Innocenti <bernie@codewiz.org>
41 #include "cfg/cfg_timer.h"
42 #include "cfg/cfg_wdt.h"
43 #include "cfg/cfg_proc.h"
44 #include "cfg/cfg_signal.h"
46 #include <cfg/debug.h>
47 #include <cfg/module.h>
50 #include <cpu/types.h>
52 #include <cpu/power.h> // cpu_relax()
55 * Include platform-specific binding code if we're hosted.
56 * Try the CPU specific one for bare-metal environments.
59 //#include OS_CSOURCE(timer)
60 #include <emul/timer_posix.c>
63 #warning Deprecated: now you should include timer_<cpu> directly in the makefile. Remove this line and the following once done.
64 #include CPU_CSOURCE(timer)
69 * Sanity check for config parameters required by this module.
71 #if !defined(CONFIG_KERN) || ((CONFIG_KERN != 0) && CONFIG_KERN != 1)
72 #error CONFIG_KERN must be set to either 0 or 1 in config.h
74 #if !defined(CONFIG_WATCHDOG) || ((CONFIG_WATCHDOG != 0) && CONFIG_WATCHDOG != 1)
75 #error CONFIG_WATCHDOG must be set to either 0 or 1 in config.h
82 #if defined (CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
83 #include <kern/signal.h> /* sig_wait(), sig_check() */
84 #include <kern/proc.h> /* proc_current() */
85 #include <cfg/macros.h> /* BV() */
90 * \def CONFIG_TIMER_STROBE
92 * This is a debug facility that can be used to
93 * monitor timer interrupt activity on an external pin.
95 * To use strobes, redefine the macros TIMER_STROBE_ON,
96 * TIMER_STROBE_OFF and TIMER_STROBE_INIT and set
97 * CONFIG_TIMER_STROBE to 1.
99 #if !defined(CONFIG_TIMER_STROBE) || !CONFIG_TIMER_STROBE
100 #define TIMER_STROBE_ON do {/*nop*/} while(0)
101 #define TIMER_STROBE_OFF do {/*nop*/} while(0)
102 #define TIMER_STROBE_INIT do {/*nop*/} while(0)
106 /// Master system clock (1 tick accuracy)
107 volatile ticks_t _clock;
110 #if CONFIG_TIMER_EVENTS
113 * List of active asynchronous timers.
115 REGISTER static List timers_queue;
119 * Add the specified timer to the software timer service queue.
120 * When the delay indicated by the timer expires, the timer
121 * device will execute the event associated with it.
123 * \note Interrupt safe
125 void timer_add(Timer *timer)
131 /* Inserting timers twice causes mayhem. */
132 ASSERT(timer->magic != TIMER_MAGIC_ACTIVE);
133 DB(timer->magic = TIMER_MAGIC_ACTIVE;)
135 IRQ_SAVE_DISABLE(flags);
137 /* Calculate expiration time for this timer */
138 timer->tick = _clock + timer->_delay;
141 * Search for the first node whose expiration time is
142 * greater than the timer we want to add.
144 node = (Timer *)LIST_HEAD(&timers_queue);
145 while (node->link.succ)
148 * Stop just after the insertion point.
149 * (this fancy compare takes care of wrap-arounds).
151 if (node->tick - timer->tick > 0)
154 /* Go to next node */
155 node = (Timer *)node->link.succ;
158 /* Enqueue timer request into the list */
159 INSERT_BEFORE(&timer->link, &node->link);
166 * Remove a timer from the timers queue before it has expired.
168 * \note Attempting to remove a timer already expired cause
169 * undefined behaviour.
171 Timer *timer_abort(Timer *timer)
173 ATOMIC(REMOVE(&timer->link));
174 DB(timer->magic = TIMER_MAGIC_INACTIVE;)
179 #endif /* CONFIG_TIMER_EVENTS */
183 * Wait for the specified amount of timer ticks.
185 void timer_delayTicks(ticks_t delay)
187 /* We shouldn't sleep with interrupts disabled */
188 IRQ_ASSERT_ENABLED();
190 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
193 ASSERT(!sig_check(SIG_SINGLE));
194 timer_setSignal(&t, proc_current(), SIG_SINGLE);
195 timer_setDelay(&t, delay);
197 sig_wait(SIG_SINGLE);
199 #else /* !CONFIG_KERN_SIGNALS */
201 ticks_t start = timer_clock();
204 while (timer_clock() - start < delay)
207 #endif /* !CONFIG_KERN_SIGNALS */
211 #if CONFIG_TIMER_UDELAY
214 * Busy wait until the specified amount of high-precision ticks have elapsed.
216 * \note This function is interrupt safe, the only
217 * requirement is a running hardware timer.
219 void timer_busyWait(hptime_t delay)
221 hptime_t now, prev = timer_hw_hpread();
226 now = timer_hw_hpread();
228 * We rely on hptime_t being unsigned here to
229 * reduce the modulo to an AND in the common
230 * case of TIMER_HW_CNT.
232 delta = (now - prev) % TIMER_HW_CNT;
241 * Wait for the specified amount of time (expressed in microseconds).
243 * \bug In AVR arch the maximum amount of time that can be used as
244 * delay could be very limited, depending on the hardware timer
245 * used. Check timer_avr.h, and what register is used as hptime_t.
247 void timer_delayHp(hptime_t delay)
249 if (UNLIKELY(delay > us_to_hptime(1000)))
251 timer_delayTicks(delay / (TIMER_HW_HPTICKS_PER_SEC / TIMER_TICKS_PER_SEC));
252 delay %= (TIMER_HW_HPTICKS_PER_SEC / TIMER_TICKS_PER_SEC);
255 timer_busyWait(delay);
257 #endif /* CONFIG_TIMER_UDELAY */
261 * Timer interrupt handler. Find soft timers expired and
262 * trigger corresponding events.
267 * With the Metrowerks compiler, the only way to force the compiler generate
268 * an interrupt service routine is to put a pragma directive within the function
272 #pragma interrupt saveall
275 #if CONFIG_TIMER_EVENTS
280 * On systems sharing IRQ line and vector, this check is needed
281 * to ensure that IRQ is generated by timer source.
283 if (!timer_hw_triggered())
288 /* Perform hw IRQ handling */
291 /* Update the master ms counter */
294 #if CONFIG_TIMER_EVENTS
296 * Check the first timer request in the list and process
297 * it when it has expired. Repeat this check until the
298 * first node has not yet expired. Since the list is sorted
299 * by expiry time, all the following requests are guaranteed
302 while ((timer = (Timer *)LIST_HEAD(&timers_queue))->link.succ)
304 /* This request in list has not yet expired? */
305 if (_clock - timer->tick < 0)
308 /* Retreat the expired timer */
309 REMOVE(&timer->link);
310 DB(timer->magic = TIMER_MAGIC_INACTIVE;)
312 /* Execute the associated event */
313 event_do(&timer->expire);
315 #endif /* CONFIG_TIMER_EVENTS */
325 void timer_init(void)
331 #if CONFIG_TIMER_EVENTS
332 LIST_INIT(&timers_queue);
345 #if (ARCH & ARCH_EMUL)
347 * Stop timer (only used by emulator)
349 void timer_cleanup(void)
355 // Hmmm... apparently, the demo app does not cleanup properly
356 //ASSERT(LIST_EMPTY(&timers_queue));
358 #endif /* ARCH_EMUL */