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