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