doc: Add timer module documentation.
[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  * \brief Hardware independent timer driver.
34  *
35  * All timer related functions are implemented in this module. You have several options to use timers:
36  * \li simple delay: just use timer_delay() if you want to wait for a few milliseconds;
37  * \li delay with callback: create a timer structure and use timer_setDelay() and timer_setSoftint() to set the callback;
38  * \li delay with signal: same as above but use timer_setSignal() to set specify which signal to send.
39  *
40  * Whenever a timer expires you need to explicitly arm it again with timer_add(). If you want to abort a timer, use timer_abort().
41  * You can use conversion macros when using msecs to specify the delay.
42  *
43  * \version $Id$
44  * \author Bernie Innocenti <bernie@codewiz.org>
45  *
46  * $WIZ$ module_name = "timer"
47  * $WIZ$ module_configuration = "bertos/cfg/cfg_timer.h"
48  * $WIZ$ module_depends = "event", "sysirq"
49  * $WIZ$ module_supports = "not atmega103 and not atmega8"
50  */
51
52 #ifndef DRV_TIMER_H
53 #define DRV_TIMER_H
54
55 #include <cfg/os.h>
56 #include <cfg/macros.h>
57
58 #include <cpu/attr.h>
59 #include <cpu/irq.h>
60
61
62 /*
63  * Include platform-specific binding header if we're hosted.
64  * Try the CPU specific one for bare-metal environments.
65  */
66 #if OS_HOSTED
67         //#include OS_HEADER(timer)
68         #include <emul/timer_posix.h>
69 #else
70         #include CPU_HEADER(timer)
71 #endif
72
73 #include "cfg/cfg_timer.h"
74 #include <cfg/debug.h>
75 #include <cfg/compiler.h>
76
77 #include <struct/list.h>
78
79 /*
80  * Sanity check for config parameters required by this module.
81  */
82 #if !defined(CONFIG_TIMER_EVENTS) || ((CONFIG_TIMER_EVENTS != 0) && CONFIG_TIMER_EVENTS != 1)
83         #error CONFIG_TIMER_EVENTS must be set to either 0 or 1 in cfg_timer.h
84 #endif
85 #if !defined(CONFIG_TIMER_UDELAY) || ((CONFIG_TIMER_UDELAY != 0) && CONFIG_TIMER_EVENTS != 1)
86         #error CONFIG_TIMER_UDELAY must be set to either 0 or 1 in cfg_timer.h
87 #endif
88 #if defined(CONFIG_TIMER_DISABLE_UDELAY)
89         #error Obosolete config option CONFIG_TIMER_DISABLE_UDELAY.  Use CONFIG_TIMER_UDELAY
90 #endif
91 #if defined(CONFIG_TIMER_DISABLE_EVENTS)
92         #error Obosolete config option CONFIG_TIMER_DISABLE_EVENTS.  Use CONFIG_TIMER_EVENTS
93 #endif
94
95 extern volatile ticks_t _clock;
96
97 /**
98  * \brief Return the system tick counter (expressed in ticks)
99  *
100  * The result is guaranteed to increment monotonically,
101  * but client code must be tolerant with respect to overflows.
102  *
103  * The following code is safe:
104  *
105  * \code
106  *   drop_teabag();
107  *   ticks_t tea_start_time = timer_clock();
108  *
109  *   for (;;)
110  *   {
111  *       if (timer_clock() - tea_start_time > TEAPOT_DELAY)
112  *       {
113  *           printf("Your tea, Sir.\n");
114  *           break;
115  *       }
116  *       patience();
117  *   }
118  * \endcode
119  *
120  * \note This function must disable interrupts on 8/16bit CPUs because the
121  * clock variable is larger than the processor word size and can't
122  * be copied atomically.
123  * \sa timer_delay()
124  */
125 INLINE ticks_t timer_clock(void)
126 {
127         ticks_t result;
128
129         ATOMIC(result = _clock);
130
131         return result;
132 }
133
134 /**
135  * Faster version of timer_clock(), to be called only when the timer
136  * interrupt is disabled (DISABLE_INTS) or overridden by a
137  * higher-priority or non-nesting interrupt.
138  *
139  * \sa timer_clock
140  */
141 INLINE ticks_t timer_clock_unlocked(void)
142 {
143         return _clock;
144 }
145
146 /** Convert \a ms [ms] to ticks. */
147 INLINE ticks_t ms_to_ticks(mtime_t ms)
148 {
149 #if TIMER_TICKS_PER_SEC < 1000
150         /* Slow timer: avoid rounding down too much. */
151         return (ms * TIMER_TICKS_PER_SEC) / 1000;
152 #else
153         /* Fast timer: don't overflow ticks_t. */
154         return ms * DIV_ROUND(TIMER_TICKS_PER_SEC, 1000);
155 #endif
156 }
157
158 /** Convert \a us [us] to ticks. */
159 INLINE ticks_t us_to_ticks(utime_t us)
160 {
161 #if TIMER_TICKS_PER_SEC < 1000
162         /* Slow timer: avoid rounding down too much. */
163         return ((us / 1000) * TIMER_TICKS_PER_SEC) / 1000;
164 #else
165         /* Fast timer: don't overflow ticks_t. */
166         return (us * DIV_ROUND(TIMER_TICKS_PER_SEC, 1000)) / 1000;
167 #endif
168 }
169
170 /** Convert \a ticks [ticks] to ms. */
171 INLINE mtime_t ticks_to_ms(ticks_t ticks)
172 {
173 #if TIMER_TICKS_PER_SEC < 1000
174         /* Slow timer: avoid rounding down too much. */
175         return (ticks * 1000) / TIMER_TICKS_PER_SEC;
176 #else
177         /* Fast timer: avoid overflowing ticks_t. */
178         return ticks / (TIMER_TICKS_PER_SEC / 1000);
179 #endif
180 }
181
182 /** Convert \a ticks [ticks] to us. */
183 INLINE utime_t ticks_to_us(ticks_t ticks)
184 {
185 #if TIMER_TICKS_PER_SEC < 1000
186         /* Slow timer: avoid rounding down too much. */
187         return ((ticks * 1000) / TIMER_TICKS_PER_SEC) * 1000;
188 #else
189         /* Fast timer: avoid overflowing ticks_t. */
190         return (ticks / (TIMER_TICKS_PER_SEC / 1000)) * 1000;
191 #endif
192 }
193
194 /** Convert \a us [us] to hpticks */
195 INLINE hptime_t us_to_hptime(utime_t us)
196 {
197 #if TIMER_HW_HPTICKS_PER_SEC > 10000000UL
198         return us * DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, 1000000UL);
199 #else
200         return (us * ((TIMER_HW_HPTICKS_PER_SEC + 500) / 1000UL) + 500) / 1000UL;
201 #endif
202 }
203
204 /** Convert \a hpticks [hptime] to usec */
205 INLINE utime_t hptime_to_us(hptime_t hpticks)
206 {
207 #if TIMER_HW_HPTICKS_PER_SEC < 100000UL
208         return hpticks * DIV_ROUND(1000000UL, TIMER_HW_HPTICKS_PER_SEC);
209 #else
210         return (hpticks * 1000UL) / DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, 1000UL);
211 #endif /* TIMER_HW_HPTICKS_PER_SEC < 100000UL */
212 }
213
214 void timer_delayTicks(ticks_t delay);
215 /**
216  * Wait some time [ms].
217  *
218  * \note CPU is released while waiting so you don't have to call cpu_relax() explicitly.
219  * \param delay Time to wait [ms].
220  */
221 INLINE void timer_delay(mtime_t delay)
222 {
223         timer_delayTicks(ms_to_ticks(delay));
224 }
225
226 void timer_init(void);
227 void timer_cleanup(void);
228
229 int timer_testSetup(void);
230 int timer_testRun(void);
231 int timer_testTearDown(void);
232
233 #if CONFIG_TIMER_UDELAY
234 void timer_busyWait(hptime_t delay);
235 void timer_delayHp(hptime_t delay);
236 INLINE void timer_udelay(utime_t delay)
237 {
238         timer_delayHp(us_to_hptime(delay));
239 }
240 #endif
241
242 #if CONFIG_TIMER_EVENTS
243
244 #include <mware/event.h>
245
246 /**
247  * The timer driver supports multiple synchronous timers
248  * that can trigger an event when they expire.
249  *
250  * \sa timer_add()
251  * \sa timer_abort()
252  */
253 typedef struct Timer
254 {
255         Node    link;     /**< Link into timers queue */
256         ticks_t _delay;   /**< [ticks] Timer delay */
257         ticks_t tick;     /**< [ticks] When this timer will expire */
258         Event   expire;   /**< Event to execute when the timer expires */
259         DB(uint16_t magic;)
260 } Timer;
261
262 /* Timer is active when Timer.magic contains this value (for debugging purposes). */
263 #define TIMER_MAGIC_ACTIVE    0xABBA
264 #define TIMER_MAGIC_INACTIVE  0xBAAB
265
266 void timer_add(Timer *timer);
267 Timer *timer_abort(Timer *timer);
268
269 /** Set the timer so that it calls an user hook when it expires */
270 INLINE void timer_setSoftint(Timer *timer, Hook func, iptr_t user_data)
271 {
272         event_initSoftint(&timer->expire, func, user_data);
273 }
274
275 /** Set the timer delay (the time before the event will be triggered) */
276 INLINE void timer_setDelay(Timer *timer, ticks_t delay)
277 {
278         timer->_delay = delay;
279 }
280
281 #endif /* CONFIG_TIMER_EVENTS */
282
283 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
284
285 /** Set the timer so that it sends a signal when it expires */
286 INLINE void timer_setSignal(Timer *timer, struct Process *proc, sigmask_t sigs)
287 {
288         event_initSignal(&timer->expire, proc, sigs);
289 }
290
291 #define timer_set_event_signal timer_setSignal
292
293 #endif /* CONFIG_KERN_SIGNALS */
294
295 #endif /* DRV_TIMER_H */