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