Fix support status for ATmega103.
[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, 2008 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \brief Hardware independent timer driver.
34  *
35  * \version $Id$
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  *
38  * $WIZ$ module_name = "timer"
39  * $WIZ$ module_configuration = "bertos/cfg/cfg_timer.h"
40  * $WIZ$ module_supports = "not atmega103"
41  */
42
43 #ifndef DRV_TIMER_H
44 #define DRV_TIMER_H
45
46 #include <cfg/os.h>
47 #include <cfg/macros.h>
48
49 #include <cpu/attr.h>
50 #include <cpu/irq.h>
51
52
53 /*
54  * Include platform-specific binding header if we're hosted.
55  * Try the CPU specific one for bare-metal environments.
56  */
57 #if OS_HOSTED
58         //#include OS_HEADER(timer)
59         #include <emul/timer_posix.h>
60 #else
61         #include CPU_HEADER(timer)
62 #endif
63
64 #include "cfg/cfg_timer.h"
65 #include <cfg/debug.h>
66 #include <cfg/compiler.h>
67
68 #include <struct/list.h>
69
70 /*
71  * Sanity check for config parameters required by this module.
72  */
73 #if !defined(CONFIG_TIMER_EVENTS) || ((CONFIG_TIMER_EVENTS != 0) && CONFIG_TIMER_EVENTS != 1)
74         #error CONFIG_TIMER_EVENTS must be set to either 0 or 1 in cfg_timer.h
75 #endif
76 #if !defined(CONFIG_TIMER_UDELAY) || ((CONFIG_TIMER_UDELAY != 0) && CONFIG_TIMER_EVENTS != 1)
77         #error CONFIG_TIMER_UDELAY must be set to either 0 or 1 in cfg_timer.h
78 #endif
79 #if defined(CONFIG_TIMER_DISABLE_UDELAY)
80         #error Obosolete config option CONFIG_TIMER_DISABLE_UDELAY.  Use CONFIG_TIMER_UDELAY
81 #endif
82 #if defined(CONFIG_TIMER_DISABLE_EVENTS)
83         #error Obosolete config option CONFIG_TIMER_DISABLE_EVENTS.  Use CONFIG_TIMER_EVENTS
84 #endif
85
86 extern volatile ticks_t _clock;
87
88 /**
89  * \brief Return the system tick counter (expressed in ticks)
90  *
91  * The result is guaranteed to increment monotonically,
92  * but client code must be tolerant with respect to overflows.
93  *
94  * The following code is safe:
95  *
96  * \code
97  *   drop_teabag();
98  *   ticks_t tea_start_time = timer_clock();
99  *
100  *   for (;;)
101  *   {
102  *       if (timer_clock() - tea_start_time > TEAPOT_DELAY)
103  *       {
104  *           printf("Your tea, Sir.\n");
105  *           break;
106  *       }
107  *       patience();
108  *   }
109  * \endcode
110  *
111  * \note This function must disable interrupts on 8/16bit CPUs because the
112  * clock variable is larger than the processor word size and can't
113  * be copied atomically.
114  */
115 INLINE ticks_t timer_clock(void)
116 {
117         ticks_t result;
118
119         ATOMIC(result = _clock);
120
121         return result;
122 }
123
124 /**
125  * Faster version of timer_clock(), to be called only when the timer
126  * interrupt is disabled (DISABLE_INTS) or overridden by a
127  * higher-priority or non-nesting interrupt.
128  *
129  * \sa timer_clock
130  */
131 INLINE ticks_t timer_clock_unlocked(void)
132 {
133         return _clock;
134 }
135
136 /** Convert \a ms [ms] to ticks. */
137 INLINE ticks_t ms_to_ticks(mtime_t ms)
138 {
139 #if TIMER_TICKS_PER_SEC < 1000
140         /* Slow timer: avoid rounding down too much. */
141         return (ms * TIMER_TICKS_PER_SEC) / 1000;
142 #else
143         /* Fast timer: don't overflow ticks_t. */
144         return ms * DIV_ROUND(TIMER_TICKS_PER_SEC, 1000);
145 #endif
146 }
147
148 /** Convert \a us [us] to ticks. */
149 INLINE ticks_t us_to_ticks(utime_t us)
150 {
151 #if TIMER_TICKS_PER_SEC < 1000
152         /* Slow timer: avoid rounding down too much. */
153         return ((us / 1000) * TIMER_TICKS_PER_SEC) / 1000;
154 #else
155         /* Fast timer: don't overflow ticks_t. */
156         return (us * DIV_ROUND(TIMER_TICKS_PER_SEC, 1000)) / 1000;
157 #endif
158 }
159
160 /** Convert \a ticks [ticks] to ms. */
161 INLINE mtime_t ticks_to_ms(ticks_t ticks)
162 {
163 #if TIMER_TICKS_PER_SEC < 1000
164         /* Slow timer: avoid rounding down too much. */
165         return (ticks * 1000) / TIMER_TICKS_PER_SEC;
166 #else
167         /* Fast timer: avoid overflowing ticks_t. */
168         return ticks / (TIMER_TICKS_PER_SEC / 1000);
169 #endif
170 }
171
172 /** Convert \a ticks [ticks] to us. */
173 INLINE utime_t ticks_to_us(ticks_t ticks)
174 {
175 #if TIMER_TICKS_PER_SEC < 1000
176         /* Slow timer: avoid rounding down too much. */
177         return ((ticks * 1000) / TIMER_TICKS_PER_SEC) * 1000;
178 #else
179         /* Fast timer: avoid overflowing ticks_t. */
180         return (ticks / (TIMER_TICKS_PER_SEC / 1000)) * 1000;
181 #endif
182 }
183
184 /** Convert \a us [us] to hpticks */
185 INLINE hptime_t us_to_hptime(utime_t us)
186 {
187 #if TIMER_HW_HPTICKS_PER_SEC > 10000000UL
188         return us * DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, 1000000UL);
189 #else
190         return (us * ((TIMER_HW_HPTICKS_PER_SEC + 500) / 1000UL) + 500) / 1000UL;
191 #endif
192 }
193
194 /** Convert \a hpticks [hptime] to usec */
195 INLINE utime_t hptime_to_us(hptime_t hpticks)
196 {
197 #if TIMER_HW_HPTICKS_PER_SEC < 100000UL
198         return hpticks * DIV_ROUND(1000000UL, TIMER_HW_HPTICKS_PER_SEC);
199 #else
200         return (hpticks * 1000UL) / DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, 1000UL);
201 #endif /* TIMER_HW_HPTICKS_PER_SEC < 100000UL */
202 }
203
204 void timer_delayTicks(ticks_t delay);
205 INLINE void timer_delay(mtime_t delay)
206 {
207         timer_delayTicks(ms_to_ticks(delay));
208 }
209
210 void timer_init(void);
211 void timer_cleanup(void);
212
213 int timer_testSetup(void);
214 int timer_testRun(void);
215 int timer_testTearDown(void);
216
217 #if CONFIG_TIMER_UDELAY
218 void timer_busyWait(hptime_t delay);
219 void timer_delayHp(hptime_t delay);
220 INLINE void timer_udelay(utime_t delay)
221 {
222         timer_delayHp(us_to_hptime(delay));
223 }
224 #endif
225
226 #if CONFIG_TIMER_EVENTS
227
228 #include <mware/event.h>
229
230 /**
231  * The timer driver supports multiple synchronous timers
232  * that can trigger an event when they expire.
233  *
234  * \sa timer_add()
235  * \sa timer_abort()
236  */
237 typedef struct Timer
238 {
239         Node    link;     /**< Link into timers queue */
240         ticks_t _delay;   /**< [ticks] Timer delay */
241         ticks_t tick;     /**< [ticks] When this timer will expire */
242         Event   expire;   /**< Event to execute when the timer expires */
243         DB(uint16_t magic;)
244 } Timer;
245
246 /** Timer is active when Timer.magic contains this value (for debugging purposes). */
247 #define TIMER_MAGIC_ACTIVE    0xABBA
248 #define TIMER_MAGIC_INACTIVE  0xBAAB
249
250 void timer_add(Timer *timer);
251 Timer *timer_abort(Timer *timer);
252
253 /** Set the timer so that it calls an user hook when it expires */
254 INLINE void timer_setSoftint(Timer *timer, Hook func, iptr_t user_data)
255 {
256         event_initSoftint(&timer->expire, func, user_data);
257 }
258
259 /** Set the timer delay (the time before the event will be triggered) */
260 INLINE void timer_setDelay(Timer *timer, ticks_t delay)
261 {
262         timer->_delay = delay;
263 }
264
265 #endif /* CONFIG_TIMER_EVENTS */
266
267 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
268
269 /** Set the timer so that it sends a signal when it expires */
270 INLINE void timer_setSignal(Timer *timer, struct Process *proc, sigmask_t sigs)
271 {
272         event_initSignal(&timer->expire, proc, sigs);
273 }
274
275 #define timer_set_event_signal timer_setSignal
276
277 #endif /* CONFIG_KERN_SIGNALS */
278
279 #endif /* DRV_TIMER_H */