move TIMER_AFTER() and TIMER_BEFORE() macros in drv/timer.h
[bertos.git] / bertos / drv / timer.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2000, 2008 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \defgroup drv_timers Timer module
34  * \ingroup core
35  * \{
36  *
37  * \brief Hardware independent timer driver.
38  *
39  * All timer related functions are implemented in this module. You have several options to use timers:
40  * \li simple delay: just use timer_delay() if you want to wait for a few milliseconds;
41  * \li delay with callback: create a timer structure and use timer_setDelay() and timer_setSoftint() to set the callback;
42  * \li delay with signal: same as above but use timer_setSignal() to set specify which signal to send.
43  * \li simple synchronous timer based scheduler: use synctimer_add() to schedule an event in a user provided queue.
44  *
45  * Whenever a timer expires you need to explicitly arm it again with timer_add(). If you want to abort a timer, use timer_abort().
46  * You can use conversion macros when using msecs to specify the delay.
47  *
48  * \author Bernie Innocenti <bernie@codewiz.org>
49  *
50  * $WIZ$ module_name = "timer"
51  * $WIZ$ module_configuration = "bertos/cfg/cfg_timer.h"
52  * $WIZ$ module_depends = "event", "sysirq"
53  * $WIZ$ module_supports = "not atmega103 and not atmega8"
54  */
55
56 #ifndef DRV_TIMER_H
57 #define DRV_TIMER_H
58
59 #include <cfg/os.h>
60 #include <cfg/macros.h>
61
62 #include <cpu/attr.h>
63 #include <cpu/irq.h>
64
65
66 /*
67  * Include platform-specific binding header if we're hosted.
68  * Try the CPU specific one for bare-metal environments.
69  */
70 #if OS_HOSTED
71         //#include OS_HEADER(timer)
72         #include <emul/timer_posix.h>
73 #else
74         #include CPU_HEADER(timer)
75 #endif
76
77 STATIC_ASSERT(sizeof(hptime_t) == SIZEOF_HPTIME_T);
78
79 #include "cfg/cfg_timer.h"
80 #include <cfg/debug.h>
81 #include <cfg/compiler.h>
82
83 #include <struct/list.h>
84
85 /*
86  * Sanity check for config parameters required by this module.
87  */
88 #if !defined(CONFIG_TIMER_EVENTS) || ((CONFIG_TIMER_EVENTS != 0) && CONFIG_TIMER_EVENTS != 1)
89         #error CONFIG_TIMER_EVENTS must be set to either 0 or 1 in cfg_timer.h
90 #endif
91 #if !defined(CONFIG_TIMER_UDELAY) || ((CONFIG_TIMER_UDELAY != 0) && CONFIG_TIMER_EVENTS != 1)
92         #error CONFIG_TIMER_UDELAY must be set to either 0 or 1 in cfg_timer.h
93 #endif
94 #if defined(CONFIG_TIMER_DISABLE_UDELAY)
95         #error Obosolete config option CONFIG_TIMER_DISABLE_UDELAY.  Use CONFIG_TIMER_UDELAY
96 #endif
97 #if defined(CONFIG_TIMER_DISABLE_EVENTS)
98         #error Obosolete config option CONFIG_TIMER_DISABLE_EVENTS.  Use CONFIG_TIMER_EVENTS
99 #endif
100
101 extern volatile ticks_t _clock;
102
103 #define TIMER_AFTER(x, y) ((long)(y) - (long)(x) < 0)
104 #define TIMER_BEFORE(x, y) TIMER_AFTER(y, x)
105
106 /**
107  * \brief Return the system tick counter (expressed in ticks)
108  *
109  * The result is guaranteed to increment monotonically,
110  * but client code must be tolerant with respect to overflows.
111  *
112  * The following code is safe:
113  *
114  * \code
115  *   drop_teabag();
116  *   ticks_t tea_start_time = timer_clock();
117  *
118  *   for (;;)
119  *   {
120  *       if (timer_clock() - tea_start_time > TEAPOT_DELAY)
121  *       {
122  *           printf("Your tea, Sir.\n");
123  *           break;
124  *       }
125  *       patience();
126  *   }
127  * \endcode
128  *
129  * \note This function must disable interrupts on 8/16bit CPUs because the
130  * clock variable is larger than the processor word size and can't
131  * be copied atomically.
132  * \sa timer_delay()
133  */
134 INLINE ticks_t timer_clock(void)
135 {
136         ticks_t result;
137
138         ATOMIC(result = _clock);
139
140         return result;
141 }
142
143 /**
144  * Faster version of timer_clock(), to be called only when the timer
145  * interrupt is disabled (DISABLE_INTS) or overridden by a
146  * higher-priority or non-nesting interrupt.
147  *
148  * \sa timer_clock
149  */
150 INLINE ticks_t timer_clock_unlocked(void)
151 {
152         return _clock;
153 }
154
155
156 /** Convert \a ms [ms] to ticks. */
157 INLINE ticks_t ms_to_ticks(mtime_t ms)
158 {
159 #if TIMER_TICKS_PER_SEC < 1000
160         /* Slow timer: avoid rounding down too much. */
161         return (ms * TIMER_TICKS_PER_SEC) / 1000;
162 #else
163         /* Fast timer: don't overflow ticks_t. */
164         return ms * DIV_ROUND(TIMER_TICKS_PER_SEC, 1000);
165 #endif
166 }
167
168 /** Convert \a us [us] to ticks. */
169 INLINE ticks_t us_to_ticks(utime_t us)
170 {
171 #if TIMER_TICKS_PER_SEC < 1000
172         /* Slow timer: avoid rounding down too much. */
173         return ((us / 1000) * TIMER_TICKS_PER_SEC) / 1000;
174 #else
175         /* Fast timer: don't overflow ticks_t. */
176         return (us * DIV_ROUND(TIMER_TICKS_PER_SEC, 1000)) / 1000;
177 #endif
178 }
179
180 /** Convert \a ticks [ticks] to ms. */
181 INLINE mtime_t ticks_to_ms(ticks_t ticks)
182 {
183 #if TIMER_TICKS_PER_SEC < 1000
184         /* Slow timer: avoid rounding down too much. */
185         return (ticks * 1000) / TIMER_TICKS_PER_SEC;
186 #else
187         /* Fast timer: avoid overflowing ticks_t. */
188         return ticks / (TIMER_TICKS_PER_SEC / 1000);
189 #endif
190 }
191
192 /** Convert \a ticks [ticks] to us. */
193 INLINE utime_t ticks_to_us(ticks_t ticks)
194 {
195 #if TIMER_TICKS_PER_SEC < 1000
196         /* Slow timer: avoid rounding down too much. */
197         return ((ticks * 1000) / TIMER_TICKS_PER_SEC) * 1000;
198 #else
199         /* Fast timer: avoid overflowing ticks_t. */
200         return (ticks / (TIMER_TICKS_PER_SEC / 1000)) * 1000;
201 #endif
202 }
203
204 /** Convert \a us [us] to hpticks */
205 INLINE hptime_t us_to_hptime(utime_t us)
206 {
207 #if TIMER_HW_HPTICKS_PER_SEC > 10000000UL
208         return us * DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, 1000000UL);
209 #else
210         return (us * ((TIMER_HW_HPTICKS_PER_SEC + 500) / 1000UL) + 500) / 1000UL;
211 #endif
212 }
213
214 /** Convert \a hpticks [hptime] to usec */
215 INLINE utime_t hptime_to_us(hptime_t hpticks)
216 {
217 #if TIMER_HW_HPTICKS_PER_SEC < 100000UL
218         return hpticks * DIV_ROUND(1000000UL, TIMER_HW_HPTICKS_PER_SEC);
219 #else
220         return (hpticks * 1000UL) / DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, 1000UL);
221 #endif /* TIMER_HW_HPTICKS_PER_SEC < 100000UL */
222 }
223
224 void timer_delayTicks(ticks_t delay);
225 /**
226  * Wait some time [ms].
227  *
228  * \note CPU is released while waiting so you don't have to call cpu_relax() explicitly.
229  * \param delay Time to wait [ms].
230  */
231 INLINE void timer_delay(mtime_t delay)
232 {
233         timer_delayTicks(ms_to_ticks(delay));
234 }
235
236 void timer_init(void);
237 void timer_cleanup(void);
238
239 int timer_testSetup(void);
240 int timer_testRun(void);
241 int timer_testTearDown(void);
242
243 #if CONFIG_TIMER_UDELAY
244 void timer_busyWait(hptime_t delay);
245 void timer_delayHp(hptime_t delay);
246 INLINE void timer_udelay(utime_t delay)
247 {
248         timer_delayHp(us_to_hptime(delay));
249 }
250 #endif
251
252 #if CONFIG_TIMER_EVENTS
253
254 #include <mware/event.h>
255
256 /**
257  * The timer driver supports multiple synchronous timers
258  * that can trigger an event when they expire.
259  *
260  * \sa timer_add()
261  * \sa timer_abort()
262  */
263 typedef struct Timer
264 {
265         Node    link;     /**< Link into timers queue */
266         ticks_t _delay;   /**< [ticks] Timer delay */
267         ticks_t tick;     /**< [ticks] When this timer will expire */
268         Event   expire;   /**< Event to execute when the timer expires */
269         DB(uint16_t magic;)
270 } Timer;
271
272 /* Timer is active when Timer.magic contains this value (for debugging purposes). */
273 #define TIMER_MAGIC_ACTIVE    0xABBA
274 #define TIMER_MAGIC_INACTIVE  0xBAAB
275
276 void timer_add(Timer *timer);
277 Timer *timer_abort(Timer *timer);
278
279 /**
280  * Set the timer so that it calls an user hook when it expires
281  *
282  * Sometimes you may want to use the same callback for different events, so you must have
283  * different data to operate on. The user_data parameter is such data.
284  *
285  * \param timer Timer struct to set the callback to
286  * \param func  Function that will be called when the timer expires
287  * \param user_data Additional data you may want to pass to the callback
288  */
289 INLINE void timer_setSoftint(Timer *timer, Hook func, iptr_t user_data)
290 {
291         event_initSoftint(&timer->expire, func, user_data);
292 }
293
294 /** Set the timer delay (the time before the event will be triggered) */
295 INLINE void timer_setDelay(Timer *timer, ticks_t delay)
296 {
297         timer->_delay = delay;
298 }
299
300
301 void synctimer_add(Timer *timer, List* q);
302
303 /** \sa timer_abort */
304 #define synctimer_abort(t) timer_abort(t)
305
306 void synctimer_poll(List* q);
307
308
309 #endif /* CONFIG_TIMER_EVENTS */
310
311 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
312
313 /** Set the timer so that it sends a signal when it expires */
314 INLINE void timer_setSignal(Timer *timer, struct Process *proc, sigmask_t sigs)
315 {
316         event_initSignal(&timer->expire, proc, sigs);
317 }
318
319 #define timer_set_event_signal timer_setSignal
320
321 #endif /* CONFIG_KERN_SIGNALS */
322
323 /** \} */ //defgroup drv_timers
324
325 #endif /* DRV_TIMER_H */