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