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