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