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