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