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