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