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 devlib/README for information.
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief Hardware independent timer driver (interface)
18 *#* Revision 1.25 2005/07/19 07:26:37 bernie
19 *#* Refactor to decouple timer ticks from milliseconds.
21 *#* Revision 1.24 2005/04/11 19:10:28 bernie
22 *#* Include top-level headers from cfg/ subdir.
24 *#* Revision 1.23 2005/03/01 23:25:46 bernie
25 *#* Move event.h to mware/.
27 *#* Revision 1.22 2004/12/13 12:07:06 bernie
28 *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
30 *#* Revision 1.21 2004/12/09 08:35:21 bernie
31 *#* Replace IPTR with iptr_t.
33 *#* Revision 1.20 2004/12/08 08:56:41 bernie
34 *#* Rename sigset_t to sigmask_t; Reformat.
36 *#* Revision 1.19 2004/12/08 08:30:37 bernie
37 *#* Convert to mtime_t; timer_minutes(): Remove.
39 *#* Revision 1.18 2004/11/16 23:09:52 bernie
40 *#* Disable timer_minutes() for targets with 16bit time_t.
42 *#* Revision 1.17 2004/11/16 22:37:14 bernie
43 *#* Replace IPTR with iptr_t.
45 *#* Revision 1.16 2004/08/25 14:12:08 rasky
46 *#* Aggiornato il comment block dei log RCS
48 *#* Revision 1.15 2004/08/10 06:59:09 bernie
49 *#* timer_gettick(): Rename to timer_ticks() and add backwards compatibility inline.
51 *#* Revision 1.12 2004/07/30 14:34:10 rasky
52 *#* Vari fix per documentazione e commenti
53 *#* Aggiunte PP_CATn e STATIC_ASSERT
55 *#* Revision 1.11 2004/07/29 22:40:12 bernie
58 *#* Revision 1.10 2004/07/21 00:13:57 bernie
59 *#* Put timer driver on diet.
61 *#* Revision 1.9 2004/07/20 23:45:01 bernie
62 *#* Finally remove redundant protos.
64 *#* Revision 1.8 2004/07/18 21:57:32 bernie
65 *#* timer_gettick(): Rename to timer_tick() and document better.
67 *#* Revision 1.7 2004/06/27 15:26:17 aleph
68 *#* Declaration fix for build with GCC 3.4
70 *#* Revision 1.6 2004/06/07 18:10:06 aleph
71 *#* Remove free pool of timers; use user-provided Timer structure instead
73 *#* Revision 1.5 2004/06/07 15:57:12 aleph
74 *#* Add function prototypes
76 *#* Revision 1.4 2004/06/06 18:25:44 bernie
77 *#* Rename event macros to look like regular functions.
79 *#* Revision 1.3 2004/06/06 16:57:18 bernie
80 *#* Mark some functions INLINE instead of 'extern inline'.
82 *#* Revision 1.2 2004/06/03 11:27:09 bernie
83 *#* Add dual-license information.
85 *#* Revision 1.1 2004/05/23 18:23:30 bernie
86 *#* Import drv/timer module.
92 #include <cfg/debug.h>
94 #include CPU_HEADER(timer)
95 #include <mware/list.h>
97 #include <cfg/compiler.h>
98 #include <appconfig.h>
100 /*! Number of timer ticks per second. */
101 #define TIMER_TICKS_PER_SEC (TIMER_TICKS_PER_MSEC * 1000)
103 /*! Number of ticks per microsecond */
104 #define TIMER_TICKS_PER_USEC ((TIMER_TICKS_PER_MSEC + 500) / 1000)
107 extern volatile ticks_t _clock;
110 * \brief Return the system tick counter (expressed in ticks)
112 * The result is guaranteed to increment monotonically,
113 * but client code must be tolerant with respect to overflows.
115 * The following code is safe:
118 * ticks_t tea_start_time = timer_clock();
122 * if (timer_clock() - tea_start_time > TEAPOT_DELAY)
123 * printf("Your tea, Sir.\n");
126 * \note This function must disable interrupts on 8/16bit CPUs because the
127 * clock variable is larger than the processor word size and can't
128 * be copied atomically.
130 INLINE ticks_t timer_clock(void)
134 ATOMIC(result = _clock);
140 * Faster version of timer_clock(), to be called only when the timer
141 * interrupt is disabled (DISABLE_INTS) or overridden by a
142 * higher-priority or non-nesting interrupt.
146 INLINE ticks_t timer_clock_unlocked(void)
153 //TODO: take care of slow timers so add convertions for seconds to ticks and viceversa.
155 /*! Convert \a ms [ms] to ticks */
156 INLINE ticks_t ms_to_ticks(mtime_t ms)
158 return ms * TIMER_TICKS_PER_MSEC;
161 /*! Convert \a us [us] to ticks */
162 INLINE ticks_t us_to_ticks(utime_t us)
164 #if TIMER_TICKS_PER_MSEC < 10000
165 return (us * TIMER_TICKS_PER_MSEC + 500) / 1000;
167 return (us * TIMER_TICKS_PER_USEC);
171 /*! Convert \a ticks [ticks] to ms */
172 INLINE mtime_t ticks_to_ms(ticks_t ticks)
174 return (ticks + TIMER_TICKS_PER_MSEC / 2) / TIMER_TICKS_PER_MSEC;
177 /*! Convert \a ticks [ticks] to us */
178 INLINE utime_t ticks_to_us(ticks_t ticks)
180 #if TIMER_TICKS_PER_USEC > 10
181 return (ticks / TIMER_TICKS_PER_USEC);
183 return (ticks * 1000 + TIMER_TICKS_PER_MSEC / 2) / TIMER_TICKS_PER_MSEC;
187 /*! Convert \a us [us] to hpticks */
188 INLINE hptime_t us_to_hptime(utime_t us)
190 #if TIMER_HW_HPTICKS_PER_SEC > 10000000UL
191 return(us * ((TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL));
193 return((us * TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL));
194 #endif /* TIMER_HW_HPTICKS_PER_SEC > 10000000UL */
197 /*! Convert \a hpticks [hptime] to usec */
198 INLINE utime_t hptime_to_us(hptime_t hpticks)
200 #if TIMER_HW_HPTICKS_PER_SEC < 100000UL
201 return(hpticks * (1000000UL / TIMER_HW_HPTICKS_PER_SEC));
203 return((hpticks * 1000000UL) / TIMER_HW_HPTICKS_PER_SEC);
204 #endif /* TIMER_HW_HPTICKS_PER_SEC < 100000UL */
208 void timer_init(void);
209 void timer_delayTicks(ticks_t delay);
210 INLINE void timer_delay(mtime_t delay)
212 timer_delayTicks(ms_to_ticks(delay));
215 #if !defined(CONFIG_TIMER_DISABLE_UDELAY)
216 void timer_busyWait(hptime_t delay);
217 void timer_delayHp(hptime_t delay);
218 INLINE void timer_udelay(utime_t delay)
220 timer_delayHp(us_to_hptime(delay));
225 void timer_test(void);
226 #endif /* CONFIG_TEST */
228 #ifndef CONFIG_TIMER_DISABLE_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; /*!< Timer delay in ms */
243 ticks_t tick; /*!< Timer will expire at this tick */
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 extern void timer_add(Timer *timer);
253 extern Timer *timer_abort(Timer *timer);
255 /*! Set the timer so that it calls an user hook when it expires */
256 INLINE void timer_set_event_softint(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_DISABLE_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_set_event_signal(Timer *timer, struct Process *proc, sigmask_t sigs)
274 event_initSignal(&timer->expire, proc, sigs);
277 #endif /* CONFIG_KERN_SIGNALS */
280 #endif /* DRV_TIMER_H */