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