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