Split cpu/cpu.h in 3 files: irq, types and attr.
[bertos.git] / 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 <cpu/attr.h>
46 #include <cpu/irq.h>
47
48 /*
49  * Include platform-specific binding header if we're hosted.
50  * Try the CPU specific one for bare-metal environments.
51  */
52 #if OS_HOSTED
53         #include OS_HEADER(timer)
54 #else
55         #include CPU_HEADER(timer)
56 #endif
57
58 #include <mware/list.h>
59 #include <cfg/debug.h>
60 #include <cfg/compiler.h>
61 #include <appconfig.h>
62
63
64 extern volatile ticks_t _clock;
65
66 /**
67  * \brief Return the system tick counter (expressed in ticks)
68  *
69  * The result is guaranteed to increment monotonically,
70  * but client code must be tolerant with respect to overflows.
71  *
72  * The following code is safe:
73  *
74  * \code
75  *   ticks_t tea_start_time = timer_clock();
76  *
77  *   boil_water();
78  *
79  *   if (timer_clock() - tea_start_time > TEAPOT_DELAY)
80  *       printf("Your tea, Sir.\n");
81  * \endcode
82  *
83  * \note This function must disable interrupts on 8/16bit CPUs because the
84  * clock variable is larger than the processor word size and can't
85  * be copied atomically.
86  */
87 INLINE ticks_t timer_clock(void)
88 {
89         ticks_t result;
90
91         ATOMIC(result = _clock);
92
93         return result;
94 }
95
96 /**
97  * Faster version of timer_clock(), to be called only when the timer
98  * interrupt is disabled (DISABLE_INTS) or overridden by a
99  * higher-priority or non-nesting interrupt.
100  *
101  * \sa timer_clock
102  */
103 INLINE ticks_t timer_clock_unlocked(void)
104 {
105         return _clock;
106 }
107
108 /** Convert \a ms [ms] to ticks. */
109 INLINE ticks_t ms_to_ticks(mtime_t ms)
110 {
111 #if TIMER_TICKS_PER_SEC < 1000
112         /* Slow timer: avoid rounding down too much. */
113         return (ms * TIMER_TICKS_PER_SEC) / 1000;
114 #else
115         /* Fast timer: don't overflow ticks_t. */
116         return ms * ((TIMER_TICKS_PER_SEC + 500) / 1000);
117 #endif
118 }
119
120 /** Convert \a us [us] to ticks. */
121 INLINE ticks_t us_to_ticks(utime_t us)
122 {
123 #if TIMER_TICKS_PER_SEC < 1000
124         /* Slow timer: avoid rounding down too much. */
125         return ((us / 1000) * TIMER_TICKS_PER_SEC) / 1000;
126 #else
127         /* Fast timer: don't overflow ticks_t. */
128         return (us * ((TIMER_TICKS_PER_SEC + 500) / 1000)) / 1000;
129 #endif
130 }
131
132 /** Convert \a ticks [ticks] to ms. */
133 INLINE mtime_t ticks_to_ms(ticks_t ticks)
134 {
135 #if TIMER_TICKS_PER_SEC < 1000
136         /* Slow timer: avoid rounding down too much. */
137         return (ticks * 1000) / TIMER_TICKS_PER_SEC;
138 #else
139         /* Fast timer: avoid overflowing ticks_t. */
140         return ticks / (TIMER_TICKS_PER_SEC / 1000);
141 #endif
142 }
143
144 /** Convert \a ticks [ticks] to us. */
145 INLINE utime_t ticks_to_us(ticks_t ticks)
146 {
147 #if TIMER_TICKS_PER_SEC < 1000
148         /* Slow timer: avoid rounding down too much. */
149         return ((ticks * 1000) / TIMER_TICKS_PER_SEC) * 1000;
150 #else
151         /* Fast timer: avoid overflowing ticks_t. */
152         return (ticks / (TIMER_TICKS_PER_SEC / 1000)) * 1000;
153 #endif
154 }
155
156 /** Convert \a us [us] to hpticks */
157 INLINE hptime_t us_to_hptime(utime_t us)
158 {
159 #if TIMER_HW_HPTICKS_PER_SEC > 10000000UL
160         return us * ((TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL);
161 #else
162         return (us * ((TIMER_HW_HPTICKS_PER_SEC + 500) / 1000UL) + 500) / 1000UL;
163 #endif
164 }
165
166 /** Convert \a hpticks [hptime] to usec */
167 INLINE utime_t hptime_to_us(hptime_t hpticks)
168 {
169 #if TIMER_HW_HPTICKS_PER_SEC < 100000UL
170         return hpticks * ((1000000UL + TIMER_HW_HPTICKS_PER_SEC / 2) / TIMER_HW_HPTICKS_PER_SEC);
171 #else
172         return (hpticks * 1000UL) / ((TIMER_HW_HPTICKS_PER_SEC + 500) / 1000UL);
173 #endif /* TIMER_HW_HPTICKS_PER_SEC < 100000UL */
174 }
175
176
177 void timer_init(void);
178 void timer_delayTicks(ticks_t delay);
179 INLINE void timer_delay(mtime_t delay)
180 {
181         timer_delayTicks(ms_to_ticks(delay));
182 }
183
184 #if !defined(CONFIG_TIMER_DISABLE_UDELAY)
185 void timer_busyWait(hptime_t delay);
186 void timer_delayHp(hptime_t delay);
187 INLINE void timer_udelay(utime_t delay)
188 {
189         timer_delayHp(us_to_hptime(delay));
190 }
191 #endif
192
193 #ifndef CONFIG_TIMER_DISABLE_EVENTS
194
195 #include <mware/event.h>
196
197 /**
198  * The timer driver supports multiple synchronous timers
199  * that can trigger an event when they expire.
200  *
201  * \sa timer_add()
202  * \sa timer_abort()
203  */
204 typedef struct Timer
205 {
206         Node    link;     /**< Link into timers queue */
207         ticks_t _delay;   /**< Timer delay in ms */
208         ticks_t tick;     /**< Timer will expire at this tick */
209         Event   expire;   /**< Event to execute when the timer expires */
210         DB(uint16_t magic;)
211 } Timer;
212
213 /** Timer is active when Timer.magic contains this value (for debugging purposes). */
214 #define TIMER_MAGIC_ACTIVE    0xABBA
215 #define TIMER_MAGIC_INACTIVE  0xBAAB
216
217 extern void timer_add(Timer *timer);
218 extern Timer *timer_abort(Timer *timer);
219
220 /** Set the timer so that it calls an user hook when it expires */
221 INLINE void timer_set_event_softint(Timer *timer, Hook func, iptr_t user_data)
222 {
223         event_initSoftInt(&timer->expire, func, user_data);
224 }
225
226 /** Set the timer delay (the time before the event will be triggered) */
227 INLINE void timer_setDelay(Timer *timer, ticks_t delay)
228 {
229         timer->_delay = delay;
230 }
231
232 #endif /* CONFIG_TIMER_DISABLE_EVENTS */
233
234 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
235
236 /** Set the timer so that it sends a signal when it expires */
237 INLINE void timer_set_event_signal(Timer *timer, struct Process *proc, sigmask_t sigs)
238 {
239         event_initSignal(&timer->expire, proc, sigs);
240 }
241
242 #endif /* CONFIG_KERN_SIGNALS */
243
244
245 #endif /* DRV_TIMER_H */