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