3877fcf38212483ee8e3ed7a37726089008323ff
[bertos.git] / drv / timer.c
1 /**
2  * \file
3  * <!--
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.
7  * -->
8  *
9  * \brief Hardware independent timer driver (implementation)
10  *
11  * \version $Id$
12  * \author Bernardo Innocenti <bernie@develer.com>
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.32  2007/10/08 12:14:32  batt
18  *#* Fix some review issues.
19  *#*
20  *#* Revision 1.31  2006/07/19 12:56:26  bernie
21  *#* Convert to new Doxygen style.
22  *#*
23  *#* Revision 1.30  2006/02/24 00:26:49  bernie
24  *#* Fixes for CONFIG_KERNEL.
25  *#*
26  *#* Revision 1.29  2006/02/17 22:24:07  bernie
27  *#* Add MOD_CHECK() checks.
28  *#*
29  *#* Revision 1.28  2006/02/10 12:32:52  bernie
30  *#* Update Copyright year.
31  *#*
32  *#* Revision 1.27  2005/11/27 03:04:08  bernie
33  *#* Move test code to timer_test.c; Add OS_HOSTED support.
34  *#*
35  *#* Revision 1.26  2005/11/04 16:20:02  bernie
36  *#* Fix reference to README.devlib in header.
37  *#*
38  *#* Revision 1.25  2005/07/19 07:26:37  bernie
39  *#* Refactor to decouple timer ticks from milliseconds.
40  *#*
41  *#* Revision 1.24  2005/04/11 19:10:28  bernie
42  *#* Include top-level headers from cfg/ subdir.
43  *#*
44  *#* Revision 1.23  2004/12/13 12:07:06  bernie
45  *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
46  *#*
47  *#* Revision 1.22  2004/12/08 09:12:09  bernie
48  *#* Rename time_t to mtime_t.
49  *#*
50  *#* Revision 1.21  2004/11/28 23:20:25  bernie
51  *#* Remove obsolete INITLIST macro.
52  *#*
53  *#* Revision 1.20  2004/11/16 20:59:06  bernie
54  *#* Add watchdog timer support.
55  *#*/
56
57 #include "timer.h"
58 #include <cfg/cpu.h>
59 #include <cfg/os.h>
60 #include <cfg/debug.h>
61 #include <cfg/module.h>
62 #include <appconfig.h>
63
64 /*
65  * Include platform-specific binding code if we're hosted.
66  * Try the CPU specific one for bare-metal environments.
67  */
68 #if OS_HOSTED
69         #include OS_CSOURCE(timer)
70 #else
71         #include CPU_CSOURCE(timer)
72 #endif
73
74 /*
75  * Sanity check for config parameters required by this module.
76  */
77 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
78         #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
79 #endif
80 #if !defined(CONFIG_WATCHDOG) || ((CONFIG_WATCHDOG != 0) && CONFIG_WATCHDOG != 1)
81         #error CONFIG_WATCHDOG must be set to either 0 or 1 in config.h
82 #endif
83
84 #if CONFIG_WATCHDOG
85         #include <drv/wdt.h>
86 #endif
87
88 #if CONFIG_KERNEL
89         #include <config_kern.h>
90         #if CONFIG_KERN_SIGNALS
91                 #include <kern/signal.h> /* sig_wait(), sig_check() */
92                 #include <kern/proc.h>   /* proc_current() */
93                 #include <cfg/macros.h>  /* BV() */
94         #endif
95 #endif
96
97
98 /**
99  * \def CONFIG_TIMER_STROBE
100  *
101  * This is a debug facility that can be used to
102  * monitor timer interrupt activity on an external pin.
103  *
104  * To use strobes, redefine the macros TIMER_STROBE_ON,
105  * TIMER_STROBE_OFF and TIMER_STROBE_INIT and set
106  * CONFIG_TIMER_STROBE to 1.
107  */
108 #if !defined(CONFIG_TIMER_STROBE) || !CONFIG_TIMER_STROBE
109         #define TIMER_STROBE_ON    do {/*nop*/} while(0)
110         #define TIMER_STROBE_OFF   do {/*nop*/} while(0)
111         #define TIMER_STROBE_INIT  do {/*nop*/} while(0)
112 #endif
113
114
115 /// Master system clock (1 tick accuracy)
116 volatile ticks_t _clock;
117
118
119 #ifndef CONFIG_TIMER_DISABLE_EVENTS
120
121 /**
122  * List of active asynchronous timers.
123  */
124 REGISTER static List timers_queue;
125
126
127 /**
128  * Add the specified timer to the software timer service queue.
129  * When the delay indicated by the timer expires, the timer
130  * device will execute the event associated with it.
131  *
132  * \note Interrupt safe
133  */
134 void timer_add(Timer *timer)
135 {
136         Timer *node;
137         cpuflags_t flags;
138
139
140         /* Inserting timers twice causes mayhem. */
141         ASSERT(timer->magic != TIMER_MAGIC_ACTIVE);
142         DB(timer->magic = TIMER_MAGIC_ACTIVE;)
143
144         IRQ_SAVE_DISABLE(flags);
145
146         /* Calculate expiration time for this timer */
147         timer->tick = _clock + timer->_delay;
148
149         /*
150          * Search for the first node whose expiration time is
151          * greater than the timer we want to add.
152          */
153         node = (Timer *)LIST_HEAD(&timers_queue);
154         while (node->link.succ)
155         {
156                 /*
157                  * Stop just after the insertion point.
158                  * (this fancy compare takes care of wrap-arounds).
159                  */
160                 if (node->tick - timer->tick > 0)
161                         break;
162
163                 /* Go to next node */
164                 node = (Timer *)node->link.succ;
165         }
166
167         /* Enqueue timer request into the list */
168         INSERT_BEFORE(&timer->link, &node->link);
169
170         IRQ_RESTORE(flags);
171 }
172
173
174 /**
175  * Remove a timer from the timer queue before it has expired.
176  */
177 Timer *timer_abort(Timer *timer)
178 {
179         ATOMIC(REMOVE(&timer->link));
180         DB(timer->magic = TIMER_MAGIC_INACTIVE;)
181
182         return timer;
183 }
184
185 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
186
187
188 /**
189  * Wait for the specified amount of timer ticks.
190  */
191 void timer_delayTicks(ticks_t delay)
192 {
193 #if defined(IRQ_GETSTATE)
194         /* We shouldn't sleep with interrupts disabled */
195         ASSERT(IRQ_GETSTATE());
196 #endif
197
198 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
199         Timer t;
200
201         ASSERT(!sig_check(SIG_SINGLE));
202         timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
203         timer_setDelay(&t, delay);
204         timer_add(&t);
205         sig_wait(SIG_SINGLE);
206
207 #else /* !CONFIG_KERN_SIGNALS */
208
209         ticks_t start = timer_clock();
210
211         /* Busy wait */
212         while (timer_clock() - start < delay)
213         {
214 #if CONFIG_WATCHDOG
215                 wdt_reset();
216 #endif
217         }
218
219 #endif /* !CONFIG_KERN_SIGNALS */
220 }
221
222
223 #ifndef CONFIG_TIMER_DISABLE_UDELAY
224
225 /**
226  * Busy wait until the specified amount of high-precision ticks have elapsed.
227  *
228  * \note This function is interrupt safe, the only
229  *       requirement is a running hardware timer.
230  */
231 void timer_busyWait(hptime_t delay)
232 {
233         hptime_t now, prev = timer_hw_hpread();
234         hptime_t delta;
235
236         for(;;)
237         {
238                 now = timer_hw_hpread();
239                 /*
240                  * We rely on hptime_t being unsigned here to
241                  * reduce the modulo to an AND in the common
242                  * case of TIMER_HW_CNT.
243                  */
244                 delta = (now - prev) % TIMER_HW_CNT;
245                 if (delta >= delay)
246                         break;
247                 delay -= delta;
248                 prev = now;
249         }
250 }
251
252 /**
253  * Wait for the specified amount of time (expressed in microseconds).
254  *
255  * \bug In AVR arch the maximum amount of time that can be used as
256  *      delay could be very limited, depending on the hardware timer
257  *      used. Check timer_avr.h, and what register is used as hptime_t.
258  */
259 void timer_delayHp(hptime_t delay)
260 {
261         if (UNLIKELY(delay > us_to_hptime(1000)))
262         {
263                 timer_delayTicks(delay / (TIMER_HW_HPTICKS_PER_SEC / TIMER_TICKS_PER_SEC));
264                 delay %= (TIMER_HW_HPTICKS_PER_SEC / TIMER_TICKS_PER_SEC);
265         }
266
267         timer_busyWait(delay);
268 }
269 #endif /* CONFIG_TIMER_DISABLE_UDELAY */
270
271
272 /**
273  * Timer interrupt handler. Find soft timers expired and
274  * trigger corresponding events.
275  */
276 DEFINE_TIMER_ISR
277 {
278         /*
279          * With the Metrowerks compiler, the only way to force the compiler generate
280          * an interrupt service routine is to put a pragma directive within the function
281          * body.
282          */
283         #ifdef __MWERKS__
284         #pragma interrupt saveall
285         #endif
286
287 #ifndef CONFIG_TIMER_DISABLE_EVENTS
288         Timer *timer;
289 #endif
290         /*
291          * On systems sharing IRQ line and vector, this check is needed
292          * to ensure that IRQ is generated by timer source.
293          */
294         if (!timer_hw_triggered())
295                 return;
296
297         TIMER_STROBE_ON;
298
299         /* Perform hw IRQ handling */
300         timer_hw_irq();
301
302         /* Update the master ms counter */
303         ++_clock;
304
305 #ifndef CONFIG_TIMER_DISABLE_EVENTS
306         /*
307          * Check the first timer request in the list and process
308          * it when it has expired. Repeat this check until the
309          * first node has not yet expired. Since the list is sorted
310          * by expiry time, all the following requests are guaranteed
311          * to expire later.
312          */
313         while ((timer = (Timer *)LIST_HEAD(&timers_queue))->link.succ)
314         {
315                 /* This request in list has not yet expired? */
316                 if (_clock - timer->tick < 0)
317                         break;
318
319                 /* Retreat the expired timer */
320                 REMOVE(&timer->link);
321                 DB(timer->magic = TIMER_MAGIC_INACTIVE;)
322
323                 /* Execute the associated event */
324                 event_do(&timer->expire);
325         }
326 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
327
328         TIMER_STROBE_OFF;
329 }
330
331 MOD_DEFINE(timer)
332
333 /**
334  * Initialize timer
335  */
336 void timer_init(void)
337 {
338         TIMER_STROBE_INIT;
339
340 #ifndef CONFIG_TIMER_DISABLE_EVENTS
341         LIST_INIT(&timers_queue);
342 #endif
343
344         _clock = 0;
345
346         timer_hw_init();
347
348         MOD_INIT(timer);
349 }