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