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