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