Replace IPTR with iptr_t.
[bertos.git] / drv / timer.h
1 /*!
2  * \file
3  * <!--
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.
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.21  2004/12/09 08:35:21  bernie
19  *#* Replace IPTR with iptr_t.
20  *#*
21  *#* Revision 1.20  2004/12/08 08:56:41  bernie
22  *#* Rename sigset_t to sigmask_t; Reformat.
23  *#*
24  *#* Revision 1.19  2004/12/08 08:30:37  bernie
25  *#* Convert to mtime_t; timer_minutes(): Remove.
26  *#*
27  *#* Revision 1.18  2004/11/16 23:09:52  bernie
28  *#* Disable timer_minutes() for targets with 16bit time_t.
29  *#*
30  *#* Revision 1.17  2004/11/16 22:37:14  bernie
31  *#* Replace IPTR with iptr_t.
32  *#*
33  *#* Revision 1.16  2004/08/25 14:12:08  rasky
34  *#* Aggiornato il comment block dei log RCS
35  *#*
36  *#* Revision 1.15  2004/08/10 06:59:09  bernie
37  *#* timer_gettick(): Rename to timer_ticks() and add backwards compatibility inline.
38  *#*
39  *#* Revision 1.12  2004/07/30 14:34:10  rasky
40  *#* Vari fix per documentazione e commenti
41  *#* Aggiunte PP_CATn e STATIC_ASSERT
42  *#*
43  *#* Revision 1.11  2004/07/29 22:40:12  bernie
44  *#* Spelling fix.
45  *#*
46  *#* Revision 1.10  2004/07/21 00:13:57  bernie
47  *#* Put timer driver on diet.
48  *#*
49  *#* Revision 1.9  2004/07/20 23:45:01  bernie
50  *#* Finally remove redundant protos.
51  *#*
52  *#* Revision 1.8  2004/07/18 21:57:32  bernie
53  *#* timer_gettick(): Rename to timer_tick() and document better.
54  *#*
55  *#* Revision 1.7  2004/06/27 15:26:17  aleph
56  *#* Declaration fix for build with GCC 3.4
57  *#*
58  *#* Revision 1.6  2004/06/07 18:10:06  aleph
59  *#* Remove free pool of timers; use user-provided Timer structure instead
60  *#*
61  *#* Revision 1.5  2004/06/07 15:57:12  aleph
62  *#* Add function prototypes
63  *#*
64  *#* Revision 1.4  2004/06/06 18:25:44  bernie
65  *#* Rename event macros to look like regular functions.
66  *#*
67  *#* Revision 1.3  2004/06/06 16:57:18  bernie
68  *#* Mark some functions INLINE instead of 'extern inline'.
69  *#*
70  *#* Revision 1.2  2004/06/03 11:27:09  bernie
71  *#* Add dual-license information.
72  *#*
73  *#* Revision 1.1  2004/05/23 18:23:30  bernie
74  *#* Import drv/timer module.
75  *#*
76  *#*/
77 #ifndef DRV_TIMER_H
78 #define DRV_TIMER_H
79
80 #include <mware/list.h>
81 #include <cpu.h>
82 #include <compiler.h>
83 #include <config.h>
84
85 /*! Number of timer ticks per second. */
86 #define TICKS_PER_SEC  ((mtime_t)1000)
87
88 /* Function protos */
89 extern void timer_init(void);
90 extern void timer_delay(mtime_t time);
91
92 #ifndef CONFIG_TIMER_DISABLE_UDELAY
93 extern void timer_udelay(utime_t utime);
94 #endif
95
96
97 #ifndef CONFIG_TIMER_DISABLE_EVENTS
98
99 #if CONFIG_KERNEL
100         #include <kern/event.h>
101 #else
102         #include <mware/event.h>
103 #endif
104
105 /*!
106  * The timer driver supports multiple synchronous timers
107  * that can trigger an event when they expire.
108  *
109  * \sa timer_add()
110  * \sa timer_abort()
111  */
112 typedef struct Timer
113 {
114         Node    link;     /*!< Link into timers queue */
115         mtime_t delay;    /*!< Timer delay in ms */
116         mtime_t tick;     /*!< Timer will expire at this tick */
117         Event   expire;   /*!< Event to execute when the timer expires */
118 } Timer;
119
120 extern void timer_add(Timer *timer);
121 extern Timer *timer_abort(Timer *timer);
122
123 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
124
125 /*! Set the timer so that it sends a signal when it expires */
126 INLINE void timer_set_event_signal(Timer *timer, struct Process *proc, sigmask_t sigs)
127 {
128         event_initSignal(&timer->expire, proc, sigs);
129 }
130
131 #endif /* CONFIG_KERN_SIGNALS */
132
133 /*! Set the timer so that it calls an user hook when it expires */
134 INLINE void timer_set_event_softint(Timer *timer, Hook func, iptr_t user_data)
135 {
136         event_initSoftInt(&timer->expire, func, user_data);
137 }
138
139 /*! Set the timer delay (the time before the event will be triggered) */
140 INLINE void timer_set_delay(Timer *timer, mtime_t delay)
141 {
142         timer->delay = delay;
143 }
144
145 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
146
147 extern volatile mtime_t _clock;
148
149 /*!
150  * \brief Return the system tick counter (expressed in ms)
151  *
152  * The result is guaranteed to increment monotonically,
153  * but client code must be tolerant with respect to overflows.
154  *
155  * The following code is safe:
156  *
157  * \code
158  *   mtime_t tea_start_time = get_tick();
159  *
160  *   boil_water();
161  *
162  *   if (get_tick() - tea_start_time > TEAPOT_DELAY)
163  *       printf("Your tea, Sir.\n");
164  * \endcode
165  *
166  * When the tick counter increments every millisecond and mtime_t
167  * is 32bit wide, the tick count will overflow every 49.7 days.
168  *
169  * \note This function must disable interrupts on 8/16bit CPUs because the
170  * clock variable is larger than the processor word size and can't
171  * be copied atomically.
172  */
173 INLINE mtime_t timer_ticks(void)
174 {
175         mtime_t result;
176         cpuflags_t flags;
177
178         DISABLE_IRQSAVE(flags);
179         result = _clock;
180         ENABLE_IRQRESTORE(flags);
181
182         return result;
183 }
184
185
186 /*!
187  * Faster version of timer_ticks(), to be called only when the timer
188  * interrupt is disabled (DISABLE_INTS) or overridden by a
189  * higher-priority or non-nesting interrupt.
190  *
191  * \sa timer_ticks
192  */
193 INLINE mtime_t timer_ticks_unlocked(void)
194 {
195         return _clock;
196 }
197
198 #endif /* DRV_TIMER_H */