4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2000, 2008 Bernie Innocenti <bernie@codewiz.org>
33 * \brief Hardware independent timer driver.
36 * \author Bernie Innocenti <bernie@codewiz.org>
41 * "configuration" : "bertos/cfg/cfg_timer.h"
49 #include <cfg/macros.h>
56 * Include platform-specific binding header if we're hosted.
57 * Try the CPU specific one for bare-metal environments.
60 //#include OS_HEADER(timer)
61 #include <emul/timer_posix.h>
63 #include CPU_HEADER(timer)
66 #include "cfg/cfg_timer.h"
67 #include <cfg/debug.h>
68 #include <cfg/compiler.h>
70 #include <struct/list.h>
73 * Sanity check for config parameters required by this module.
75 #if !defined(CONFIG_TIMER_EVENTS) || ((CONFIG_TIMER_EVENTS != 0) && CONFIG_TIMER_EVENTS != 1)
76 #error CONFIG_TIMER_EVENTS must be set to either 0 or 1 in cfg_timer.h
78 #if !defined(CONFIG_TIMER_UDELAY) || ((CONFIG_TIMER_UDELAY != 0) && CONFIG_TIMER_EVENTS != 1)
79 #error CONFIG_TIMER_UDELAY must be set to either 0 or 1 in cfg_timer.h
81 #if defined(CONFIG_TIMER_DISABLE_UDELAY)
82 #error Obosolete config option CONFIG_TIMER_DISABLE_UDELAY. Use CONFIG_TIMER_UDELAY
84 #if defined(CONFIG_TIMER_DISABLE_EVENTS)
85 #error Obosolete config option CONFIG_TIMER_DISABLE_EVENTS. Use CONFIG_TIMER_EVENTS
88 extern volatile ticks_t _clock;
91 * \brief Return the system tick counter (expressed in ticks)
93 * The result is guaranteed to increment monotonically,
94 * but client code must be tolerant with respect to overflows.
96 * The following code is safe:
100 * ticks_t tea_start_time = timer_clock();
104 * if (timer_clock() - tea_start_time > TEAPOT_DELAY)
106 * printf("Your tea, Sir.\n");
113 * \note This function must disable interrupts on 8/16bit CPUs because the
114 * clock variable is larger than the processor word size and can't
115 * be copied atomically.
117 INLINE ticks_t timer_clock(void)
121 ATOMIC(result = _clock);
127 * Faster version of timer_clock(), to be called only when the timer
128 * interrupt is disabled (DISABLE_INTS) or overridden by a
129 * higher-priority or non-nesting interrupt.
133 INLINE ticks_t timer_clock_unlocked(void)
138 /** Convert \a ms [ms] to ticks. */
139 INLINE ticks_t ms_to_ticks(mtime_t ms)
141 #if TIMER_TICKS_PER_SEC < 1000
142 /* Slow timer: avoid rounding down too much. */
143 return (ms * TIMER_TICKS_PER_SEC) / 1000;
145 /* Fast timer: don't overflow ticks_t. */
146 return ms * DIV_ROUND(TIMER_TICKS_PER_SEC, 1000);
150 /** Convert \a us [us] to ticks. */
151 INLINE ticks_t us_to_ticks(utime_t us)
153 #if TIMER_TICKS_PER_SEC < 1000
154 /* Slow timer: avoid rounding down too much. */
155 return ((us / 1000) * TIMER_TICKS_PER_SEC) / 1000;
157 /* Fast timer: don't overflow ticks_t. */
158 return (us * DIV_ROUND(TIMER_TICKS_PER_SEC, 1000)) / 1000;
162 /** Convert \a ticks [ticks] to ms. */
163 INLINE mtime_t ticks_to_ms(ticks_t ticks)
165 #if TIMER_TICKS_PER_SEC < 1000
166 /* Slow timer: avoid rounding down too much. */
167 return (ticks * 1000) / TIMER_TICKS_PER_SEC;
169 /* Fast timer: avoid overflowing ticks_t. */
170 return ticks / (TIMER_TICKS_PER_SEC / 1000);
174 /** Convert \a ticks [ticks] to us. */
175 INLINE utime_t ticks_to_us(ticks_t ticks)
177 #if TIMER_TICKS_PER_SEC < 1000
178 /* Slow timer: avoid rounding down too much. */
179 return ((ticks * 1000) / TIMER_TICKS_PER_SEC) * 1000;
181 /* Fast timer: avoid overflowing ticks_t. */
182 return (ticks / (TIMER_TICKS_PER_SEC / 1000)) * 1000;
186 /** Convert \a us [us] to hpticks */
187 INLINE hptime_t us_to_hptime(utime_t us)
189 #if TIMER_HW_HPTICKS_PER_SEC > 10000000UL
190 return us * DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, 1000000UL);
192 return (us * ((TIMER_HW_HPTICKS_PER_SEC + 500) / 1000UL) + 500) / 1000UL;
196 /** Convert \a hpticks [hptime] to usec */
197 INLINE utime_t hptime_to_us(hptime_t hpticks)
199 #if TIMER_HW_HPTICKS_PER_SEC < 100000UL
200 return hpticks * DIV_ROUND(1000000UL, TIMER_HW_HPTICKS_PER_SEC);
202 return (hpticks * 1000UL) / DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, 1000UL);
203 #endif /* TIMER_HW_HPTICKS_PER_SEC < 100000UL */
206 void timer_delayTicks(ticks_t delay);
207 INLINE void timer_delay(mtime_t delay)
209 timer_delayTicks(ms_to_ticks(delay));
212 void timer_init(void);
213 void timer_cleanup(void);
215 int timer_testSetup(void);
216 int timer_testRun(void);
217 int timer_testTearDown(void);
219 #if CONFIG_TIMER_UDELAY
220 void timer_busyWait(hptime_t delay);
221 void timer_delayHp(hptime_t delay);
222 INLINE void timer_udelay(utime_t delay)
224 timer_delayHp(us_to_hptime(delay));
228 #if CONFIG_TIMER_EVENTS
230 #include <mware/event.h>
233 * The timer driver supports multiple synchronous timers
234 * that can trigger an event when they expire.
241 Node link; /**< Link into timers queue */
242 ticks_t _delay; /**< [ticks] Timer delay */
243 ticks_t tick; /**< [ticks] When this timer will expire */
244 Event expire; /**< Event to execute when the timer expires */
248 /** Timer is active when Timer.magic contains this value (for debugging purposes). */
249 #define TIMER_MAGIC_ACTIVE 0xABBA
250 #define TIMER_MAGIC_INACTIVE 0xBAAB
252 void timer_add(Timer *timer);
253 Timer *timer_abort(Timer *timer);
255 /** Set the timer so that it calls an user hook when it expires */
256 INLINE void timer_setSoftint(Timer *timer, Hook func, iptr_t user_data)
258 event_initSoftint(&timer->expire, func, user_data);
261 /** Set the timer delay (the time before the event will be triggered) */
262 INLINE void timer_setDelay(Timer *timer, ticks_t delay)
264 timer->_delay = delay;
267 #endif /* CONFIG_TIMER_EVENTS */
269 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
271 /** Set the timer so that it sends a signal when it expires */
272 INLINE void timer_setSignal(Timer *timer, struct Process *proc, sigmask_t sigs)
274 event_initSignal(&timer->expire, proc, sigs);
277 #define timer_set_event_signal timer_setSignal
279 #endif /* CONFIG_KERN_SIGNALS */
281 #endif /* DRV_TIMER_H */