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