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