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