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