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