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