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