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