Refactor BeRTOS to be in his own directory.
[bertos.git] / bertos / drv / timer.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2000 Bernardo Innocenti <bernie@develer.com>
31  *
32  * -->
33  *
34  * \version $Id$
35  *
36  * \author Bernardo Innocenti <bernie@develer.com>
37  *
38  * \brief Hardware independent timer driver (interface)
39  */
40
41 #ifndef DRV_TIMER_H
42 #define DRV_TIMER_H
43
44 #include <cfg/os.h>
45 #include <cfg/macros.h>
46 #include <cpu/attr.h>
47 #include <cpu/irq.h>
48
49
50 /*
51  * Include platform-specific binding header if we're hosted.
52  * Try the CPU specific one for bare-metal environments.
53  */
54 #if OS_HOSTED
55         #include OS_HEADER(timer)
56 #else
57         #include CPU_HEADER(timer)
58 #endif
59
60 #include <mware/list.h>
61 #include <cfg/debug.h>
62 #include <cfg/compiler.h>
63 #include <appconfig.h>
64
65
66 extern volatile ticks_t _clock;
67
68 /**
69  * \brief Return the system tick counter (expressed in ticks)
70  *
71  * The result is guaranteed to increment monotonically,
72  * but client code must be tolerant with respect to overflows.
73  *
74  * The following code is safe:
75  *
76  * \code
77  *   ticks_t tea_start_time = timer_clock();
78  *
79  *   boil_water();
80  *
81  *   if (timer_clock() - tea_start_time > TEAPOT_DELAY)
82  *       printf("Your tea, Sir.\n");
83  * \endcode
84  *
85  * \note This function must disable interrupts on 8/16bit CPUs because the
86  * clock variable is larger than the processor word size and can't
87  * be copied atomically.
88  */
89 INLINE ticks_t timer_clock(void)
90 {
91         ticks_t result;
92
93         ATOMIC(result = _clock);
94
95         return result;
96 }
97
98 /**
99  * Faster version of timer_clock(), to be called only when the timer
100  * interrupt is disabled (DISABLE_INTS) or overridden by a
101  * higher-priority or non-nesting interrupt.
102  *
103  * \sa timer_clock
104  */
105 INLINE ticks_t timer_clock_unlocked(void)
106 {
107         return _clock;
108 }
109
110 /** Convert \a ms [ms] to ticks. */
111 INLINE ticks_t ms_to_ticks(mtime_t ms)
112 {
113 #if TIMER_TICKS_PER_SEC < 1000
114         /* Slow timer: avoid rounding down too much. */
115         return (ms * TIMER_TICKS_PER_SEC) / 1000;
116 #else
117         /* Fast timer: don't overflow ticks_t. */
118         return ms * DIV_ROUND(TIMER_TICKS_PER_SEC, 1000);
119 #endif
120 }
121
122 /** Convert \a us [us] to ticks. */
123 INLINE ticks_t us_to_ticks(utime_t us)
124 {
125 #if TIMER_TICKS_PER_SEC < 1000
126         /* Slow timer: avoid rounding down too much. */
127         return ((us / 1000) * TIMER_TICKS_PER_SEC) / 1000;
128 #else
129         /* Fast timer: don't overflow ticks_t. */
130         return (us * DIV_ROUND(TIMER_TICKS_PER_SEC, 1000)) / 1000;
131 #endif
132 }
133
134 /** Convert \a ticks [ticks] to ms. */
135 INLINE mtime_t ticks_to_ms(ticks_t ticks)
136 {
137 #if TIMER_TICKS_PER_SEC < 1000
138         /* Slow timer: avoid rounding down too much. */
139         return (ticks * 1000) / TIMER_TICKS_PER_SEC;
140 #else
141         /* Fast timer: avoid overflowing ticks_t. */
142         return ticks / (TIMER_TICKS_PER_SEC / 1000);
143 #endif
144 }
145
146 /** Convert \a ticks [ticks] to us. */
147 INLINE utime_t ticks_to_us(ticks_t ticks)
148 {
149 #if TIMER_TICKS_PER_SEC < 1000
150         /* Slow timer: avoid rounding down too much. */
151         return ((ticks * 1000) / TIMER_TICKS_PER_SEC) * 1000;
152 #else
153         /* Fast timer: avoid overflowing ticks_t. */
154         return (ticks / (TIMER_TICKS_PER_SEC / 1000)) * 1000;
155 #endif
156 }
157
158 /** Convert \a us [us] to hpticks */
159 INLINE hptime_t us_to_hptime(utime_t us)
160 {
161 #if TIMER_HW_HPTICKS_PER_SEC > 10000000UL
162         return us * DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, 1000000UL);
163 #else
164         return (us * ((TIMER_HW_HPTICKS_PER_SEC + 500) / 1000UL) + 500) / 1000UL;
165 #endif
166 }
167
168 /** Convert \a hpticks [hptime] to usec */
169 INLINE utime_t hptime_to_us(hptime_t hpticks)
170 {
171 #if TIMER_HW_HPTICKS_PER_SEC < 100000UL
172         return hpticks * DIV_ROUND(1000000UL, TIMER_HW_HPTICKS_PER_SEC);
173 #else
174         return (hpticks * 1000UL) / DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, 1000UL);
175 #endif /* TIMER_HW_HPTICKS_PER_SEC < 100000UL */
176 }
177
178
179 void timer_init(void);
180 void timer_delayTicks(ticks_t delay);
181 INLINE void timer_delay(mtime_t delay)
182 {
183         timer_delayTicks(ms_to_ticks(delay));
184 }
185
186 #if !defined(CONFIG_TIMER_DISABLE_UDELAY)
187 void timer_busyWait(hptime_t delay);
188 void timer_delayHp(hptime_t delay);
189 INLINE void timer_udelay(utime_t delay)
190 {
191         timer_delayHp(us_to_hptime(delay));
192 }
193 #endif
194
195 #ifndef CONFIG_TIMER_DISABLE_EVENTS
196
197 #include <mware/event.h>
198
199 /**
200  * The timer driver supports multiple synchronous timers
201  * that can trigger an event when they expire.
202  *
203  * \sa timer_add()
204  * \sa timer_abort()
205  */
206 typedef struct Timer
207 {
208         Node    link;     /**< Link into timers queue */
209         ticks_t _delay;   /**< Timer delay in ms */
210         ticks_t tick;     /**< Timer will expire at this tick */
211         Event   expire;   /**< Event to execute when the timer expires */
212         DB(uint16_t magic;)
213 } Timer;
214
215 /** Timer is active when Timer.magic contains this value (for debugging purposes). */
216 #define TIMER_MAGIC_ACTIVE    0xABBA
217 #define TIMER_MAGIC_INACTIVE  0xBAAB
218
219 extern void timer_add(Timer *timer);
220 extern Timer *timer_abort(Timer *timer);
221
222 /** Set the timer so that it calls an user hook when it expires */
223 INLINE void timer_set_event_softint(Timer *timer, Hook func, iptr_t user_data)
224 {
225         event_initSoftInt(&timer->expire, func, user_data);
226 }
227
228 /** Set the timer delay (the time before the event will be triggered) */
229 INLINE void timer_setDelay(Timer *timer, ticks_t delay)
230 {
231         timer->_delay = delay;
232 }
233
234 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
235
236 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
237
238 /** Set the timer so that it sends a signal when it expires */
239 INLINE void timer_set_event_signal(Timer *timer, struct Process *proc, sigmask_t sigs)
240 {
241         event_initSignal(&timer->expire, proc, sigs);
242 }
243
244 #endif /* CONFIG_KERN_SIGNALS */
245
246
247 #endif /* DRV_TIMER_H */