4 * Copyright 2003, 2004 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.20 2004/12/08 08:56:41 bernie
19 *#* Rename sigset_t to sigmask_t; Reformat.
21 *#* Revision 1.19 2004/12/08 08:30:37 bernie
22 *#* Convert to mtime_t; timer_minutes(): Remove.
24 *#* Revision 1.18 2004/11/16 23:09:52 bernie
25 *#* Disable timer_minutes() for targets with 16bit time_t.
27 *#* Revision 1.17 2004/11/16 22:37:14 bernie
28 *#* Replace IPTR with iptr_t.
30 *#* Revision 1.16 2004/08/25 14:12:08 rasky
31 *#* Aggiornato il comment block dei log RCS
33 *#* Revision 1.15 2004/08/10 06:59:09 bernie
34 *#* timer_gettick(): Rename to timer_ticks() and add backwards compatibility inline.
36 *#* Revision 1.12 2004/07/30 14:34:10 rasky
37 *#* Vari fix per documentazione e commenti
38 *#* Aggiunte PP_CATn e STATIC_ASSERT
40 *#* Revision 1.11 2004/07/29 22:40:12 bernie
43 *#* Revision 1.10 2004/07/21 00:13:57 bernie
44 *#* Put timer driver on diet.
46 *#* Revision 1.9 2004/07/20 23:45:01 bernie
47 *#* Finally remove redundant protos.
49 *#* Revision 1.8 2004/07/18 21:57:32 bernie
50 *#* timer_gettick(): Rename to timer_tick() and document better.
52 *#* Revision 1.7 2004/06/27 15:26:17 aleph
53 *#* Declaration fix for build with GCC 3.4
55 *#* Revision 1.6 2004/06/07 18:10:06 aleph
56 *#* Remove free pool of timers; use user-provided Timer structure instead
58 *#* Revision 1.5 2004/06/07 15:57:12 aleph
59 *#* Add function prototypes
61 *#* Revision 1.4 2004/06/06 18:25:44 bernie
62 *#* Rename event macros to look like regular functions.
64 *#* Revision 1.3 2004/06/06 16:57:18 bernie
65 *#* Mark some functions INLINE instead of 'extern inline'.
67 *#* Revision 1.2 2004/06/03 11:27:09 bernie
68 *#* Add dual-license information.
70 *#* Revision 1.1 2004/05/23 18:23:30 bernie
71 *#* Import drv/timer module.
77 #include <mware/list.h>
82 /*! Number of timer ticks per second. */
83 #define TICKS_PER_SEC ((mtime_t)1000)
86 extern void timer_init(void);
87 extern void timer_delay(mtime_t time);
89 #ifndef CONFIG_TIMER_DISABLE_UDELAY
90 extern void timer_udelay(utime_t utime);
94 #ifndef CONFIG_TIMER_DISABLE_EVENTS
97 #include <kern/event.h>
99 #include <mware/event.h>
103 * The timer driver supports multiple synchronous timers
104 * that can trigger an event when they expire.
111 Node link; /*!< Link into timers queue */
112 mtime_t delay; /*!< Timer delay in ms */
113 mtime_t tick; /*!< Timer will expire at this tick */
114 Event expire; /*!< Event to execute when the timer expires */
117 extern void timer_add(Timer *timer);
118 extern Timer *timer_abort(Timer *timer);
120 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
122 /*! Set the timer so that it sends a signal when it expires */
123 INLINE void timer_set_event_signal(Timer *timer, struct Process *proc, sigmask_t sigs)
125 event_initSignal(&timer->expire, proc, sigs);
128 #endif /* CONFIG_KERN_SIGNALS */
130 /*! Set the timer so that it calls an user hook when it expires */
131 INLINE void timer_set_event_softint(Timer *timer, Hook func, IPTR user_data)
133 event_initSoftInt(&timer->expire, func, user_data);
136 /*! Set the timer delay (the time before the event will be triggered) */
137 INLINE void timer_set_delay(Timer *timer, mtime_t delay)
139 timer->delay = delay;
142 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
144 extern volatile mtime_t _clock;
147 * \brief Return the system tick counter (expressed in ms)
149 * The result is guaranteed to increment monotonically,
150 * but client code must be tolerant with respect to overflows.
152 * The following code is safe:
155 * mtime_t tea_start_time = get_tick();
159 * if (get_tick() - tea_start_time > TEAPOT_DELAY)
160 * printf("Your tea, Sir.\n");
163 * When the tick counter increments every millisecond and mtime_t
164 * is 32bit wide, the tick count will overflow every 49.7 days.
166 * \note This function must disable interrupts on 8/16bit CPUs because the
167 * clock variable is larger than the processor word size and can't
168 * be copied atomically.
170 INLINE mtime_t timer_ticks(void)
175 DISABLE_IRQSAVE(flags);
177 ENABLE_IRQRESTORE(flags);
184 * Faster version of timer_ticks(), to be called only when the timer
185 * interrupt is disabled (DISABLE_INTS) or overridden by a
186 * higher-priority or non-nesting interrupt.
190 INLINE mtime_t timer_ticks_unlocked(void)
195 #endif /* DRV_TIMER_H */