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