Update POSIX timer emulator.
[bertos.git] / drv / timer.h
1 /*!
2  * \file
3  * <!--
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.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  *
13  * \brief Hardware independent timer driver (interface)
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.28  2006/02/17 22:24:21  bernie
19  *#* Update POSIX timer emulator.
20  *#*
21  *#* Revision 1.27  2005/11/27 03:04:19  bernie
22  *#* Move test code to timer_test.c; Add OS_HOSTED support.
23  *#*
24  *#* Revision 1.26  2005/11/04 16:20:02  bernie
25  *#* Fix reference to README.devlib in header.
26  *#*
27  *#* Revision 1.25  2005/07/19 07:26:37  bernie
28  *#* Refactor to decouple timer ticks from milliseconds.
29  *#*
30  *#* Revision 1.24  2005/04/11 19:10:28  bernie
31  *#* Include top-level headers from cfg/ subdir.
32  *#*
33  *#* Revision 1.23  2005/03/01 23:25:46  bernie
34  *#* Move event.h to mware/.
35  *#*
36  *#* Revision 1.22  2004/12/13 12:07:06  bernie
37  *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
38  *#*
39  *#* Revision 1.21  2004/12/09 08:35:21  bernie
40  *#* Replace IPTR with iptr_t.
41  *#*
42  *#* Revision 1.20  2004/12/08 08:56:41  bernie
43  *#* Rename sigset_t to sigmask_t; Reformat.
44  *#*
45  *#* Revision 1.19  2004/12/08 08:30:37  bernie
46  *#* Convert to mtime_t; timer_minutes(): Remove.
47  *#*
48  *#* Revision 1.18  2004/11/16 23:09:52  bernie
49  *#* Disable timer_minutes() for targets with 16bit time_t.
50  *#*
51  *#* Revision 1.17  2004/11/16 22:37:14  bernie
52  *#* Replace IPTR with iptr_t.
53  *#*
54  *#* Revision 1.16  2004/08/25 14:12:08  rasky
55  *#* Aggiornato il comment block dei log RCS
56  *#*
57  *#* Revision 1.15  2004/08/10 06:59:09  bernie
58  *#* timer_gettick(): Rename to timer_ticks() and add backwards compatibility inline.
59  *#*
60  *#* Revision 1.12  2004/07/30 14:34:10  rasky
61  *#* Vari fix per documentazione e commenti
62  *#* Aggiunte PP_CATn e STATIC_ASSERT
63  *#*
64  *#* Revision 1.11  2004/07/29 22:40:12  bernie
65  *#* Spelling fix.
66  *#*
67  *#* Revision 1.10  2004/07/21 00:13:57  bernie
68  *#* Put timer driver on diet.
69  *#*
70  *#* Revision 1.9  2004/07/20 23:45:01  bernie
71  *#* Finally remove redundant protos.
72  *#*
73  *#* Revision 1.8  2004/07/18 21:57:32  bernie
74  *#* timer_gettick(): Rename to timer_tick() and document better.
75  *#*
76  *#* Revision 1.7  2004/06/27 15:26:17  aleph
77  *#* Declaration fix for build with GCC 3.4
78  *#*
79  *#* Revision 1.6  2004/06/07 18:10:06  aleph
80  *#* Remove free pool of timers; use user-provided Timer structure instead
81  *#*
82  *#* Revision 1.5  2004/06/07 15:57:12  aleph
83  *#* Add function prototypes
84  *#*
85  *#* Revision 1.4  2004/06/06 18:25:44  bernie
86  *#* Rename event macros to look like regular functions.
87  *#*
88  *#* Revision 1.3  2004/06/06 16:57:18  bernie
89  *#* Mark some functions INLINE instead of 'extern inline'.
90  *#*
91  *#* Revision 1.2  2004/06/03 11:27:09  bernie
92  *#* Add dual-license information.
93  *#*
94  *#* Revision 1.1  2004/05/23 18:23:30  bernie
95  *#* Import drv/timer module.
96  *#*
97  *#*/
98 #ifndef DRV_TIMER_H
99 #define DRV_TIMER_H
100
101 #include <cfg/os.h>
102 #include <cfg/cpu.h>
103
104 /*
105  * Include platform-specific binding header if we're hosted.
106  * Try the CPU specific one for bare-metal environments.
107  */
108 #if OS_HOSTED
109         #include OS_HEADER(timer)
110 #else
111         #include CPU_HEADER(timer)
112 #endif
113
114 #include <mware/list.h>
115 #include <cfg/debug.h>
116 #include <cfg/compiler.h>
117 #include <appconfig.h>
118
119 /*! Number of timer ticks per second. */
120 #define TIMER_TICKS_PER_SEC  (TIMER_TICKS_PER_MSEC * 1000)
121
122 /*! Number of ticks per microsecond */
123 #define TIMER_TICKS_PER_USEC ((TIMER_TICKS_PER_MSEC + 500) / 1000)
124
125
126 extern volatile ticks_t _clock;
127
128 /*!
129  * \brief Return the system tick counter (expressed in ticks)
130  *
131  * The result is guaranteed to increment monotonically,
132  * but client code must be tolerant with respect to overflows.
133  *
134  * The following code is safe:
135  *
136  * \code
137  *   ticks_t tea_start_time = timer_clock();
138  *
139  *   boil_water();
140  *
141  *   if (timer_clock() - tea_start_time > TEAPOT_DELAY)
142  *       printf("Your tea, Sir.\n");
143  * \endcode
144  *
145  * \note This function must disable interrupts on 8/16bit CPUs because the
146  * clock variable is larger than the processor word size and can't
147  * be copied atomically.
148  */
149 INLINE ticks_t timer_clock(void)
150 {
151         ticks_t result;
152
153         ATOMIC(result = _clock);
154
155         return result;
156 }
157
158 /*!
159  * Faster version of timer_clock(), to be called only when the timer
160  * interrupt is disabled (DISABLE_INTS) or overridden by a
161  * higher-priority or non-nesting interrupt.
162  *
163  * \sa timer_ticks
164  */
165 INLINE ticks_t timer_clock_unlocked(void)
166 {
167         return _clock;
168 }
169
170
171
172 //TODO: take care of slow timers so add convertions for seconds to ticks and viceversa.
173
174 /*! Convert \a ms [ms] to ticks */
175 INLINE ticks_t ms_to_ticks(mtime_t ms)
176 {
177         return ms * TIMER_TICKS_PER_MSEC;
178 }
179
180 /*! Convert \a us [us] to ticks */
181 INLINE ticks_t us_to_ticks(utime_t us)
182 {
183 #if TIMER_TICKS_PER_MSEC < 10000
184         return (us * TIMER_TICKS_PER_MSEC + 500) / 1000;
185 #else
186         return (us * TIMER_TICKS_PER_USEC);
187 #endif
188 }
189
190 /*! Convert \a ticks [ticks] to ms */
191 INLINE mtime_t ticks_to_ms(ticks_t ticks)
192 {
193         return (ticks + TIMER_TICKS_PER_MSEC / 2) / TIMER_TICKS_PER_MSEC;
194 }
195
196 /*! Convert \a ticks [ticks] to us */
197 INLINE utime_t ticks_to_us(ticks_t ticks)
198 {
199 #if TIMER_TICKS_PER_USEC > 10
200         return (ticks / TIMER_TICKS_PER_USEC);
201 #else
202         return (ticks * 1000 + TIMER_TICKS_PER_MSEC / 2) / TIMER_TICKS_PER_MSEC;
203 #endif
204 }
205
206 /*! Convert \a us [us] to hpticks */
207 INLINE hptime_t us_to_hptime(utime_t us)
208 {
209         #if TIMER_HW_HPTICKS_PER_SEC > 10000000UL
210                 return(us * ((TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL));
211         #else
212                 return((us * TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL);
213         #endif /* TIMER_HW_HPTICKS_PER_SEC > 10000000UL */
214 }
215
216 /*! Convert \a hpticks [hptime] to usec */
217 INLINE utime_t hptime_to_us(hptime_t hpticks)
218 {
219         #if TIMER_HW_HPTICKS_PER_SEC < 100000UL
220                 return(hpticks * (1000000UL / TIMER_HW_HPTICKS_PER_SEC));
221         #else
222                 return((hpticks * 1000000UL) / TIMER_HW_HPTICKS_PER_SEC);
223         #endif /* TIMER_HW_HPTICKS_PER_SEC < 100000UL */
224 }
225
226
227 void timer_init(void);
228 void timer_delayTicks(ticks_t delay);
229 INLINE void timer_delay(mtime_t delay)
230 {
231         timer_delayTicks(ms_to_ticks(delay));
232 }
233
234 #if !defined(CONFIG_TIMER_DISABLE_UDELAY)
235 void timer_busyWait(hptime_t delay);
236 void timer_delayHp(hptime_t delay);
237 INLINE void timer_udelay(utime_t delay)
238 {
239         timer_delayHp(us_to_hptime(delay));
240 }
241 #endif
242
243 #ifndef CONFIG_TIMER_DISABLE_EVENTS
244
245 #include <mware/event.h>
246
247 /*!
248  * The timer driver supports multiple synchronous timers
249  * that can trigger an event when they expire.
250  *
251  * \sa timer_add()
252  * \sa timer_abort()
253  */
254 typedef struct Timer
255 {
256         Node    link;     /*!< Link into timers queue */
257         ticks_t _delay;   /*!< Timer delay in ms */
258         ticks_t tick;     /*!< Timer will expire at this tick */
259         Event   expire;   /*!< Event to execute when the timer expires */
260         DB(uint16_t magic;)
261 } Timer;
262
263 /*! Timer is active when Timer.magic contains this value (for debugging purposes). */
264 #define TIMER_MAGIC_ACTIVE    0xABBA
265 #define TIMER_MAGIC_INACTIVE  0xBAAB
266
267 extern void timer_add(Timer *timer);
268 extern Timer *timer_abort(Timer *timer);
269
270 /*! Set the timer so that it calls an user hook when it expires */
271 INLINE void timer_set_event_softint(Timer *timer, Hook func, iptr_t user_data)
272 {
273         event_initSoftInt(&timer->expire, func, user_data);
274 }
275
276 /*! Set the timer delay (the time before the event will be triggered) */
277 INLINE void timer_setDelay(Timer *timer, ticks_t delay)
278 {
279         timer->_delay = delay;
280 }
281
282 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
283
284 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
285
286 /*! Set the timer so that it sends a signal when it expires */
287 INLINE void timer_set_event_signal(Timer *timer, struct Process *proc, sigmask_t sigs)
288 {
289         event_initSignal(&timer->expire, proc, sigs);
290 }
291
292 #endif /* CONFIG_KERN_SIGNALS */
293
294
295 #endif /* DRV_TIMER_H */