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