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