Add new config vars.
[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 README.devlib for information.
7  * -->
8  *
9  * \brief Hardware independent timer driver (implementation)
10  *
11  * \version $Id$
12  * \author Bernardo Innocenti <bernie@develer.com>
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.27  2005/11/27 03:04:08  bernie
18  *#* Move test code to timer_test.c; Add OS_HOSTED support.
19  *#*
20  *#* Revision 1.26  2005/11/04 16:20:02  bernie
21  *#* Fix reference to README.devlib in header.
22  *#*
23  *#* Revision 1.25  2005/07/19 07:26:37  bernie
24  *#* Refactor to decouple timer ticks from milliseconds.
25  *#*
26  *#* Revision 1.24  2005/04/11 19:10:28  bernie
27  *#* Include top-level headers from cfg/ subdir.
28  *#*
29  *#* Revision 1.23  2004/12/13 12:07:06  bernie
30  *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
31  *#*
32  *#* Revision 1.22  2004/12/08 09:12:09  bernie
33  *#* Rename time_t to mtime_t.
34  *#*
35  *#* Revision 1.21  2004/11/28 23:20:25  bernie
36  *#* Remove obsolete INITLIST macro.
37  *#*
38  *#* Revision 1.20  2004/11/16 20:59:06  bernie
39  *#* Add watchdog timer support.
40  *#*/
41
42 #include "timer.h"
43 #include <cfg/cpu.h>
44 #include <cfg/os.h>
45 #include <cfg/debug.h>
46 #include <appconfig.h>
47
48 /*
49  * Include platform-specific binding code if we're hosted.
50  * Try the CPU specific one for bare-metal environments.
51  */
52 #if OS_HOSTED
53         #include OS_CSOURCE(timer)
54 #else
55         #include CPU_CSOURCE(timer)
56 #endif
57
58 /*
59  * Sanity check for config parameters required by this module.
60  */
61 #if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
62         #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
63 #endif
64 #if !defined(CONFIG_WATCHDOG) || ((CONFIG_WATCHDOG != 0) && CONFIG_WATCHDOG != 1)
65         #error CONFIG_WATCHDOG must be set to either 0 or 1 in config.h
66 #endif
67
68 #if CONFIG_WATCHDOG
69         #include <drv/wdt.h>
70 #endif
71
72 #if CONFIG_KERNEL && CONFIG_KERN_SIGNALS
73         #include <kern/proc.h>
74 #endif
75
76
77 /*!
78  * \def CONFIG_TIMER_STROBE
79  *
80  * This is a debug facility that can be used to
81  * monitor timer interrupt activity on an external pin.
82  *
83  * To use strobes, redefine the macros TIMER_STROBE_ON,
84  * TIMER_STROBE_OFF and TIMER_STROBE_INIT and set
85  * CONFIG_TIMER_STROBE to 1.
86  */
87 #if !defined(CONFIG_TIMER_STROBE) || !CONFIG_TIMER_STROBE
88         #define TIMER_STROBE_ON    do {/*nop*/} while(0)
89         #define TIMER_STROBE_OFF   do {/*nop*/} while(0)
90         #define TIMER_STROBE_INIT  do {/*nop*/} while(0)
91 #endif
92
93
94 //! Master system clock (1 tick accuracy)
95 volatile ticks_t _clock;
96
97
98 #ifndef CONFIG_TIMER_DISABLE_EVENTS
99
100 /*!
101  * List of active asynchronous timers.
102  */
103 REGISTER static List timers_queue;
104
105
106 /*!
107  * Add the specified timer to the software timer service queue.
108  * When the delay indicated by the timer expires, the timer
109  * device will execute the event associated with it.
110  *
111  * \note Interrupt safe
112  */
113 void timer_add(Timer *timer)
114 {
115         Timer *node;
116         cpuflags_t flags;
117
118
119         /* Inserting timers twice causes mayhem. */
120         ASSERT(timer->magic != TIMER_MAGIC_ACTIVE);
121         DB(timer->magic = TIMER_MAGIC_ACTIVE;)
122
123         IRQ_SAVE_DISABLE(flags);
124
125         /* Calculate expiration time for this timer */
126         timer->tick = _clock + timer->_delay;
127
128         /*
129          * Search for the first node whose expiration time is
130          * greater than the timer we want to add.
131          */
132         node = (Timer *)LIST_HEAD(&timers_queue);
133         while (node->link.succ)
134         {
135                 /*
136                  * Stop just after the insertion point.
137                  * (this fancy compare takes care of wrap-arounds).
138                  */
139                 if (node->tick - timer->tick > 0)
140                         break;
141
142                 /* Go to next node */
143                 node = (Timer *)node->link.succ;
144         }
145
146         /* Enqueue timer request into the list */
147         INSERTBEFORE(&timer->link, &node->link);
148
149         IRQ_RESTORE(flags);
150 }
151
152
153 /*!
154  * Remove a timer from the timer queue before it has expired.
155  */
156 Timer *timer_abort(Timer *timer)
157 {
158         ATOMIC(REMOVE(&timer->link));
159         DB(timer->magic = TIMER_MAGIC_INACTIVE;)
160
161         return timer;
162 }
163
164 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
165
166
167 /*!
168  * Wait for the specified amount of time (expressed in ms).
169  */
170 void timer_delayTicks(ticks_t delay)
171 {
172 #if defined(IRQ_GETSTATE)
173         /* We shouldn't sleep with interrupts disabled */
174         ASSERT(IRQ_GETSTATE());
175 #endif
176
177 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
178         Timer t;
179
180         ASSERT(!sig_check(SIG_SINGLE));
181         timer_set_event_signal(&t, proc_current(), SIG_SINGLE);
182         timer_set_delay(&t, delay);
183         timer_add(&t);
184         sig_wait(SIG_SINGLE);
185
186 #else /* !CONFIG_KERN_SIGNALS */
187
188         ticks_t start = timer_clock();
189
190         /* Busy wait */
191         while (timer_clock() - start < delay)
192         {
193 #if CONFIG_WATCHDOG
194                 wdt_reset();
195 #endif
196         }
197
198 #endif /* !CONFIG_KERN_SIGNALS */
199 }
200
201
202 #ifndef CONFIG_TIMER_DISABLE_UDELAY
203
204 /*!
205  * Busy wait until the specified amount of high-precision ticks have elapsed.
206  *
207  * \note This function is interrupt safe, the only
208  *       requirement is a running hardware timer.
209  */
210 void timer_busyWait(hptime_t delay)
211 {
212         hptime_t now, prev = timer_hw_hpread();
213         hptime_t delta;
214
215         for(;;)
216         {
217                 now = timer_hw_hpread();
218                 /*
219                  * We rely on hptime_t being unsigned here to
220                  * reduce the modulo to an AND in the common
221                  * case of TIMER_HW_CNT.
222                  */
223                 delta = (now - prev) % TIMER_HW_CNT;
224                 if (delta >= delay)
225                         break;
226                 delay -= delta;
227                 prev = now;
228         }
229 }
230
231 /*!
232  * Wait for the specified amount of time (expressed in microseconds).
233  *
234  * \bug In AVR arch the maximum amount of time that can be used as
235  *      delay could be very limited, depending on the hardware timer
236  *      used. Check timer_avr.h, and what register is used as hptime_t.
237  */
238 void timer_delayHp(hptime_t delay)
239 {
240         if (UNLIKELY(delay > us_to_hptime(1000)))
241         {
242                 timer_delayTicks(delay / (TIMER_HW_HPTICKS_PER_SEC / TIMER_TICKS_PER_SEC));
243                 delay %= (TIMER_HW_HPTICKS_PER_SEC / TIMER_TICKS_PER_SEC);
244         }
245
246         timer_busyWait(delay);
247 }
248 #endif /* CONFIG_TIMER_DISABLE_UDELAY */
249
250
251 /*!
252  * Timer interrupt handler. Find soft timers expired and
253  * trigger corresponding events.
254  */
255 DEFINE_TIMER_ISR
256 {
257         /*
258          * With the Metrowerks compiler, the only way to force the compiler generate
259          * an interrupt service routine is to put a pragma directive within the function
260          * body.
261          */
262         #ifdef __MWERKS__
263         #pragma interrupt saveall
264         #endif
265
266 #ifndef CONFIG_TIMER_DISABLE_EVENTS
267         Timer *timer;
268 #endif
269
270         TIMER_STROBE_ON;
271
272         timer_hw_irq();
273
274         /* Update the master ms counter */
275         ++_clock;
276
277 #ifndef CONFIG_TIMER_DISABLE_EVENTS
278         /*
279          * Check the first timer request in the list and process
280          * it when it has expired. Repeat this check until the
281          * first node has not yet expired. Since the list is sorted
282          * by expiry time, all the following requests are guaranteed
283          * to expire later.
284          */
285         while ((timer = (Timer *)LIST_HEAD(&timers_queue))->link.succ)
286         {
287                 /* This request in list has not yet expired? */
288                 if (_clock - timer->tick < 0)
289                         break;
290
291                 /* Retreat the expired timer */
292                 REMOVE(&timer->link);
293                 DB(timer->magic = TIMER_MAGIC_INACTIVE;)
294
295                 /* Execute the associated event */
296                 event_do(&timer->expire);
297         }
298 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
299
300         TIMER_STROBE_OFF;
301 }
302
303
304 /*!
305  * Initialize timer
306  */
307 void timer_init(void)
308 {
309         TIMER_STROBE_INIT;
310
311 #ifndef CONFIG_TIMER_DISABLE_EVENTS
312         LIST_INIT(&timers_queue);
313 #endif
314
315         _clock = 0;
316
317         timer_hw_init();
318 }