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