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