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