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