4 * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2000 Bernardo Innocenti <bernie@develer.com>
6 * This file is part of DevLib - See README.devlib for information.
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief Hardware independent timer driver (interface)
18 *#* Revision 1.29 2006/02/21 21:28:02 bernie
19 *#* New time handling based on TIMER_TICKS_PER_SEC to support slow timers with ticks longer than 1ms.
21 *#* Revision 1.28 2006/02/17 22:24:21 bernie
22 *#* Update POSIX timer emulator.
24 *#* Revision 1.27 2005/11/27 03:04:19 bernie
25 *#* Move test code to timer_test.c; Add OS_HOSTED support.
27 *#* Revision 1.26 2005/11/04 16:20:02 bernie
28 *#* Fix reference to README.devlib in header.
30 *#* Revision 1.25 2005/07/19 07:26:37 bernie
31 *#* Refactor to decouple timer ticks from milliseconds.
33 *#* Revision 1.24 2005/04/11 19:10:28 bernie
34 *#* Include top-level headers from cfg/ subdir.
36 *#* Revision 1.23 2005/03/01 23:25:46 bernie
37 *#* Move event.h to mware/.
39 *#* Revision 1.22 2004/12/13 12:07:06 bernie
40 *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
42 *#* Revision 1.21 2004/12/09 08:35:21 bernie
43 *#* Replace IPTR with iptr_t.
45 *#* Revision 1.20 2004/12/08 08:56:41 bernie
46 *#* Rename sigset_t to sigmask_t; Reformat.
48 *#* Revision 1.19 2004/12/08 08:30:37 bernie
49 *#* Convert to mtime_t; timer_minutes(): Remove.
51 *#* Revision 1.18 2004/11/16 23:09:52 bernie
52 *#* Disable timer_minutes() for targets with 16bit time_t.
54 *#* Revision 1.17 2004/11/16 22:37:14 bernie
55 *#* Replace IPTR with iptr_t.
57 *#* Revision 1.16 2004/08/25 14:12:08 rasky
58 *#* Aggiornato il comment block dei log RCS
60 *#* Revision 1.15 2004/08/10 06:59:09 bernie
61 *#* timer_gettick(): Rename to timer_ticks() and add backwards compatibility inline.
63 *#* Revision 1.12 2004/07/30 14:34:10 rasky
64 *#* Vari fix per documentazione e commenti
65 *#* Aggiunte PP_CATn e STATIC_ASSERT
67 *#* Revision 1.11 2004/07/29 22:40:12 bernie
70 *#* Revision 1.10 2004/07/21 00:13:57 bernie
71 *#* Put timer driver on diet.
73 *#* Revision 1.9 2004/07/20 23:45:01 bernie
74 *#* Finally remove redundant protos.
76 *#* Revision 1.8 2004/07/18 21:57:32 bernie
77 *#* timer_gettick(): Rename to timer_tick() and document better.
79 *#* Revision 1.7 2004/06/27 15:26:17 aleph
80 *#* Declaration fix for build with GCC 3.4
82 *#* Revision 1.6 2004/06/07 18:10:06 aleph
83 *#* Remove free pool of timers; use user-provided Timer structure instead
85 *#* Revision 1.5 2004/06/07 15:57:12 aleph
86 *#* Add function prototypes
88 *#* Revision 1.4 2004/06/06 18:25:44 bernie
89 *#* Rename event macros to look like regular functions.
91 *#* Revision 1.3 2004/06/06 16:57:18 bernie
92 *#* Mark some functions INLINE instead of 'extern inline'.
94 *#* Revision 1.2 2004/06/03 11:27:09 bernie
95 *#* Add dual-license information.
97 *#* Revision 1.1 2004/05/23 18:23:30 bernie
98 *#* Import drv/timer module.
108 * Include platform-specific binding header if we're hosted.
109 * Try the CPU specific one for bare-metal environments.
112 #include OS_HEADER(timer)
114 #include CPU_HEADER(timer)
117 #include <mware/list.h>
118 #include <cfg/debug.h>
119 #include <cfg/compiler.h>
120 #include <appconfig.h>
123 extern volatile ticks_t _clock;
126 * \brief Return the system tick counter (expressed in ticks)
128 * The result is guaranteed to increment monotonically,
129 * but client code must be tolerant with respect to overflows.
131 * The following code is safe:
134 * ticks_t tea_start_time = timer_clock();
138 * if (timer_clock() - tea_start_time > TEAPOT_DELAY)
139 * printf("Your tea, Sir.\n");
142 * \note This function must disable interrupts on 8/16bit CPUs because the
143 * clock variable is larger than the processor word size and can't
144 * be copied atomically.
146 INLINE ticks_t timer_clock(void)
150 ATOMIC(result = _clock);
156 * Faster version of timer_clock(), to be called only when the timer
157 * interrupt is disabled (DISABLE_INTS) or overridden by a
158 * higher-priority or non-nesting interrupt.
162 INLINE ticks_t timer_clock_unlocked(void)
169 //TODO: take care of slow timers so add convertions for seconds to ticks and viceversa.
171 /*! Convert \a ms [ms] to ticks. */
172 INLINE ticks_t ms_to_ticks(mtime_t ms)
174 #if TIMER_TICKS_PER_SEC < 1000
175 /* Slow timer: avoid rounding down too much. */
176 return (ms * TIMER_TICKS_PER_SEC) / 1000;
178 /* Fast timer: don't overflow ticks_t. */
179 return ms * ((TIMER_TICKS_PER_SEC + 500) / 1000);
183 /*! Convert \a us [us] to ticks. */
184 INLINE ticks_t us_to_ticks(utime_t us)
186 #if TIMER_TICKS_PER_SEC < 1000
187 /* Slow timer: avoid rounding down too much. */
188 return ((us / 1000) * TIMER_TICKS_PER_SEC) / 1000;
190 /* Fast timer: don't overflow ticks_t. */
191 return (us * ((TIMER_TICKS_PER_SEC + 500) / 1000)) / 1000;
195 /*! Convert \a ticks [ticks] to ms. */
196 INLINE mtime_t ticks_to_ms(ticks_t ticks)
198 #if TIMER_TICKS_PER_SEC < 1000
199 /* Slow timer: avoid rounding down too much. */
200 return (ticks * 1000) / TIMER_TICKS_PER_SEC;
202 /* Fast timer: avoid overflowing ticks_t. */
203 return ticks / (TIMER_TICKS_PER_SEC / 1000);
207 /*! Convert \a ticks [ticks] to us. */
208 INLINE utime_t ticks_to_us(ticks_t ticks)
210 #if TIMER_TICKS_PER_SEC < 1000
211 /* Slow timer: avoid rounding down too much. */
212 return ((ticks * 1000) / TIMER_TICKS_PER_SEC) * 1000;
214 /* Fast timer: avoid overflowing ticks_t. */
215 return (ticks / (TIMER_TICKS_PER_SEC / 1000)) * 1000;
219 /*! Convert \a us [us] to hpticks */
220 INLINE hptime_t us_to_hptime(utime_t us)
222 #if TIMER_HW_HPTICKS_PER_SEC > 10000000UL
223 return(us * ((TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL));
225 return((us * TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL);
229 /*! Convert \a hpticks [hptime] to usec */
230 INLINE utime_t hptime_to_us(hptime_t hpticks)
232 #if TIMER_HW_HPTICKS_PER_SEC < 100000UL
233 return(hpticks * (1000000UL / TIMER_HW_HPTICKS_PER_SEC));
235 return((hpticks * 1000000UL) / TIMER_HW_HPTICKS_PER_SEC);
236 #endif /* TIMER_HW_HPTICKS_PER_SEC < 100000UL */
240 void timer_init(void);
241 void timer_delayTicks(ticks_t delay);
242 INLINE void timer_delay(mtime_t delay)
244 timer_delayTicks(ms_to_ticks(delay));
247 #if !defined(CONFIG_TIMER_DISABLE_UDELAY)
248 void timer_busyWait(hptime_t delay);
249 void timer_delayHp(hptime_t delay);
250 INLINE void timer_udelay(utime_t delay)
252 timer_delayHp(us_to_hptime(delay));
256 #ifndef CONFIG_TIMER_DISABLE_EVENTS
258 #include <mware/event.h>
261 * The timer driver supports multiple synchronous timers
262 * that can trigger an event when they expire.
269 Node link; /*!< Link into timers queue */
270 ticks_t _delay; /*!< Timer delay in ms */
271 ticks_t tick; /*!< Timer will expire at this tick */
272 Event expire; /*!< Event to execute when the timer expires */
276 /*! Timer is active when Timer.magic contains this value (for debugging purposes). */
277 #define TIMER_MAGIC_ACTIVE 0xABBA
278 #define TIMER_MAGIC_INACTIVE 0xBAAB
280 extern void timer_add(Timer *timer);
281 extern Timer *timer_abort(Timer *timer);
283 /*! Set the timer so that it calls an user hook when it expires */
284 INLINE void timer_set_event_softint(Timer *timer, Hook func, iptr_t user_data)
286 event_initSoftInt(&timer->expire, func, user_data);
289 /*! Set the timer delay (the time before the event will be triggered) */
290 INLINE void timer_setDelay(Timer *timer, ticks_t delay)
292 timer->_delay = delay;
295 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
297 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
299 /*! Set the timer so that it sends a signal when it expires */
300 INLINE void timer_set_event_signal(Timer *timer, struct Process *proc, sigmask_t sigs)
302 event_initSignal(&timer->expire, proc, sigs);
305 #endif /* CONFIG_KERN_SIGNALS */
308 #endif /* DRV_TIMER_H */