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