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