Remove obsolete INITLIST macro.
[bertos.git] / drv / timer.c
1 /*!
2  * \file
3  * <!--
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.
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.21  2004/11/28 23:20:25  bernie
18  *#* Remove obsolete INITLIST macro.
19  *#*
20  *#* Revision 1.20  2004/11/16 20:59:06  bernie
21  *#* Add watchdog timer support.
22  *#*
23  *#* Revision 1.19  2004/10/19 08:56:49  bernie
24  *#* TIMER_STROBE_ON, TIMER_STROBE_OFF, TIMER_STROBE_INIT: Move from timer_avr.h to timer.h, where they really belong.
25  *#*
26  *#* Revision 1.18  2004/10/14 23:14:05  bernie
27  *#* Fix longstanding problem with wrap-arounds.
28  *#*
29  *#* Revision 1.17  2004/10/03 18:52:08  bernie
30  *#* Move \brief on top in header to please Doxygen.
31  *#*
32  *#* Revision 1.16  2004/10/03 18:48:01  bernie
33  *#* timer_delay(): Add a sanity check to avoid sleeping forever.
34  *#*
35  *#* Revision 1.15  2004/09/14 21:07:18  bernie
36  *#* Use debug.h instead of kdebug.h.
37  *#*
38  *#* Revision 1.14  2004/08/25 14:12:08  rasky
39  *#* Aggiornato il comment block dei log RCS
40  *#*
41  *#* Revision 1.13  2004/08/10 06:59:09  bernie
42  *#* timer_gettick(): Rename to timer_ticks() and add backwards compatibility inline.
43  *#*
44  *#* Revision 1.12  2004/08/08 05:59:37  bernie
45  *#* Remove a few useless casts.
46  *#*
47  *#* Revision 1.11  2004/08/02 20:20:29  aleph
48  *#* Merge from project_ks
49  *#*
50  *#* Revision 1.10  2004/07/30 14:15:53  rasky
51  *#* Nuovo supporto unificato per detect della CPU
52  *#*
53  *#* Revision 1.9  2004/07/21 00:15:13  bernie
54  *#* Put timer driver on diet.
55  *#*
56  *#* Revision 1.8  2004/07/18 21:57:07  bernie
57  *#* Fix preprocessor warning with potentially undefined symbol.
58  *#*
59  *#* Revision 1.6  2004/06/07 18:10:06  aleph
60  *#* Remove free pool of timers; use user-provided Timer structure instead
61  *#*
62  *#* Revision 1.5  2004/06/07 15:56:55  aleph
63  *#* Some tabs cleanup and add timer strobe macros
64  *#*
65  *#* Revision 1.4  2004/06/06 18:25:44  bernie
66  *#* Rename event macros to look like regular functions.
67  *#*
68  *#* Revision 1.3  2004/06/06 17:18:42  bernie
69  *#* Fix \!CONFIG_KERN_SIGNALS code paths.
70  *#*
71  *#* Revision 1.2  2004/06/03 11:27:09  bernie
72  *#* Add dual-license information.
73  *#*
74  *#* Revision 1.1  2004/05/23 18:23:30  bernie
75  *#* Import drv/timer module.
76  *#*
77  *#*/
78
79 #include "timer.h"
80 #include <cpu.h>
81 #include <hw.h>
82 #include CPU_HEADER(timer)
83 #include <debug.h>
84 #include <config.h>
85
86 /*
87  * Sanity check for config parameters required by this module.
88  */
89 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
90         #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
91 #endif
92 #if !defined(CONFIG_WATCHDOG) || ((CONFIG_WATCHDOG != 0) && CONFIG_WATCHDOG != 1)
93         #error CONFIG_WATCHDOG must be set to either 0 or 1 in config.h
94 #endif
95
96 #if CONFIG_WATCHDOG
97         #include <drv/wdt.h>
98 #endif
99
100 #if CONFIG_KERNEL && CONFIG_KERN_SIGNALS
101         #include <kern/proc.h>
102 #endif
103
104
105 /*!
106  * \def CONFIG_TIMER_STROBE
107  *
108  * This is a debug facility that can be used to
109  * monitor timer interrupt activity on an external pin.
110  *
111  * To use strobes, redefine the macros TIMER_STROBE_ON,
112  * TIMER_STROBE_OFF and TIMER_STROBE_INIT and set
113  * CONFIG_TIMER_STROBE to 1.
114  */
115 #if !defined(CONFIG_TIMER_STROBE) || !CONFIG_TIMER_STROBE
116         #define TIMER_STROBE_ON    do {/*nop*/} while(0)
117         #define TIMER_STROBE_OFF   do {/*nop*/} while(0)
118         #define TIMER_STROBE_INIT  do {/*nop*/} while(0)
119 #endif
120
121
122 //! Master system clock (1ms accuracy)
123 volatile time_t _clock;
124
125
126 #ifndef CONFIG_TIMER_DISABLE_EVENTS
127
128 /*!
129  * List of active asynchronous timers.
130  */
131 REGISTER static List timers_queue;
132
133
134 /*!
135  * Add the specified timer to the software timer service queue.
136  * When the delay indicated by the timer expires, the timer
137  * device will execute the event associated with it.
138  *
139  * \note Interrupt safe
140  */
141 void timer_add(Timer *timer)
142 {
143         Timer *node;
144         cpuflags_t flags;
145
146         DISABLE_IRQSAVE(flags);
147
148         /* Calculate expiration time for this timer */
149         timer->tick = _clock + timer->delay;
150
151         /*
152          * Search for the first node whose expiration time is
153          * greater than the timer we want to add.
154          */
155         node = (Timer *)timers_queue.head;
156         while (node->link.succ)
157         {
158                 /*
159                  * Stop just after the insertion point.
160                  * (this fancy compare takes care of wrap-arounds).
161                  */
162                 if (node->tick - timer->tick > 0)
163                         break;
164
165                 /* Go to next node */
166                 node = (Timer *)node->link.succ;
167         }
168
169         /* Enqueue timer request into the list */
170         INSERTBEFORE(&timer->link, &node->link);
171
172         ENABLE_IRQRESTORE(flags);
173 }
174
175
176 /*!
177  * Remove a timer from the timer queue before it has expired
178  */
179 Timer *timer_abort(Timer *timer)
180 {
181         cpuflags_t flags;
182         DISABLE_IRQSAVE(flags);
183         REMOVE(&timer->link);
184         ENABLE_IRQRESTORE(flags);
185
186         return timer;
187 }
188
189 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
190
191
192 /*!
193  * Wait for the specified amount of time (expressed in ms)
194  */
195 void timer_delay(time_t time)
196 {
197 #if defined(IRQ_GETSTATE)
198         /* We shouldn't sleep with interrupts disabled */
199         ASSERT(IRQ_GETSTATE());
200 #endif
201
202 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
203         Timer t;
204
205         ASSERT(!sig_check(SIG_SINGLE));
206         timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
207         timer_set_delay(&t, time);
208         timer_add(&t);
209         sig_wait(SIG_SINGLE);
210
211 #else /* !CONFIG_KERN_SIGNALS */
212
213         time_t start = timer_ticks();
214
215         /* Busy wait */
216         while (timer_ticks() - start < time)
217         {
218 #if CONFIG_WATCHDOG
219                 wdt_reset();
220 #endif
221         }
222
223 #endif /* !CONFIG_KERN_SIGNALS */
224 }
225
226
227 #ifndef CONFIG_TIMER_DISABLE_UDELAY
228 /*!
229  * Wait for the specified amount of time (expressed in microseconds)
230  *
231  * \bug In AVR arch the maximum amount of time that can be used as
232  *      delay could be very limited, depending on the hardware timer
233  *      used. Check timer_avr.h, and what register is used as hptime_t.
234  */
235 void timer_udelay(utime_t usec_delay)
236 {
237         if (UNLIKELY(usec_delay > 1000))
238         {
239                 timer_delay(usec_delay / 1000);
240                 usec_delay %= 1000;
241         }
242
243         // FIXME: This multiplication is too slow at run-time. We should try and move it
244         //  to compile-time by exposing the TIMER_HW_HPTICKS_PER_SEC in the header
245         //  file.
246         hptime_t start = timer_hw_hpread();
247         hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul;
248
249         while (timer_hw_hpread() - start < delay)
250         {}
251 }
252 #endif /* CONFIG_TIMER_DISABLE_UDELAY */
253
254
255 /*!
256  * Timer interrupt handler. Find soft timers expired and
257  * trigger corresponding events.
258  */
259 DEFINE_TIMER_ISR
260 {
261         /*
262          * With the Metrowerks compiler, the only way to force the compiler generate
263          * an interrupt service routine is to put a pragma directive within the function
264          * body.
265          */
266         #ifdef __MWERKS__
267         #pragma interrupt saveall
268         #endif
269
270 #ifndef CONFIG_TIMER_DISABLE_EVENTS
271         Timer *timer;
272 #endif
273
274         TIMER_STROBE_ON;
275
276         timer_hw_irq();
277
278         /* Update the master ms counter */
279         ++_clock;
280
281 #ifndef CONFIG_TIMER_DISABLE_EVENTS
282         /*
283          * Check the first timer request in the list and process
284          * it when it has expired. Repeat this check until the
285          * first node has not yet expired. Since the list is sorted
286          * by expiry time, all the following requests are guaranteed
287          * to expire later.
288          */
289         while ((timer = (Timer *)timers_queue.head)->link.succ)
290         {
291                 /* This request in list has not yet expired? */
292                 if (_clock < timer->tick)
293                         break;
294
295                 /* Retreat the expired timer */
296                 REMOVE(&timer->link);
297
298                 /* Execute the associated event */
299                 event_do(&timer->expire);
300         }
301 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
302
303         TIMER_STROBE_OFF;
304 }
305
306
307 /*!
308  * Initialize timer
309  */
310 void timer_init(void)
311 {
312         TIMER_STROBE_INIT;
313
314 #ifndef CONFIG_TIMER_DISABLE_EVENTS
315         LIST_INIT(&timers_queue);
316 #endif
317
318         _clock = 0;
319
320         timer_hw_init();
321 }