1 | /* Copyright (c) 2002, Marek Michalkiewicz
|
2 | Copyright (c) 2004,2005,2007 Joerg Wunsch
|
3 | Copyright (c) 2007 Florin-Viorel Petrov
|
4 | All rights reserved.
|
5 |
|
6 | Redistribution and use in source and binary forms, with or without
|
7 | modification, are permitted provided that the following conditions are met:
|
8 |
|
9 | * Redistributions of source code must retain the above copyright
|
10 | notice, this list of conditions and the following disclaimer.
|
11 |
|
12 | * Redistributions in binary form must reproduce the above copyright
|
13 | notice, this list of conditions and the following disclaimer in
|
14 | the documentation and/or other materials provided with the
|
15 | distribution.
|
16 |
|
17 | * Neither the name of the copyright holders nor the names of
|
18 | contributors may be used to endorse or promote products derived
|
19 | from this software without specific prior written permission.
|
20 |
|
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
31 | POSSIBILITY OF SUCH DAMAGE. */
|
32 |
|
33 | /* $Id: delay.h.in 2189 2010-10-13 09:39:34Z aboyapati $ */
|
34 |
|
35 | #ifndef _UTIL_DELAY_H_
|
36 | #define _UTIL_DELAY_H_ 1
|
37 |
|
38 | #ifndef __HAS_DELAY_CYCLES
|
39 | #define __HAS_DELAY_CYCLES 1
|
40 | #endif
|
41 |
|
42 | #include <inttypes.h>
|
43 | #include <util/delay_basic.h>
|
44 |
|
45 | /** \file */
|
46 | /** \defgroup util_delay <util/delay.h>: Convenience functions for busy-wait delay loops
|
47 | \code
|
48 | #define F_CPU 1000000UL // 1 MHz
|
49 | //#define F_CPU 14.7456E6
|
50 | #include <util/delay.h>
|
51 | \endcode
|
52 |
|
53 | \note As an alternative method, it is possible to pass the
|
54 | F_CPU macro down to the compiler from the Makefile.
|
55 | Obviously, in that case, no \c \#define statement should be
|
56 | used.
|
57 |
|
58 | The functions in this header file are wrappers around the basic
|
59 | busy-wait functions from <util/delay_basic.h>. They are meant as
|
60 | convenience functions where actual time values can be specified
|
61 | rather than a number of cycles to wait for. The idea behind is
|
62 | that compile-time constant expressions will be eliminated by
|
63 | compiler optimization so floating-point expressions can be used
|
64 | to calculate the number of delay cycles needed based on the CPU
|
65 | frequency passed by the macro F_CPU.
|
66 |
|
67 | \note In order for these functions to work as intended, compiler
|
68 | optimizations <em>must</em> be enabled, and the delay time
|
69 | <em>must</em> be an expression that is a known constant at
|
70 | compile-time. If these requirements are not met, the resulting
|
71 | delay will be much longer (and basically unpredictable), and
|
72 | applications that otherwise do not use floating-point calculations
|
73 | will experience severe code bloat by the floating-point library
|
74 | routines linked into the application.
|
75 |
|
76 | The functions available allow the specification of microsecond, and
|
77 | millisecond delays directly, using the application-supplied macro
|
78 | F_CPU as the CPU clock frequency (in Hertz).
|
79 |
|
80 | */
|
81 |
|
82 | #if !defined(__DOXYGEN__)
|
83 | static inline void _delay_us(double __us) __attribute__((always_inline));
|
84 | static inline void _delay_ms(double __ms) __attribute__((always_inline));
|
85 | #endif
|
86 |
|
87 | #ifndef F_CPU
|
88 | /* prevent compiler error by supplying a default */
|
89 | # warning "F_CPU not defined for <util/delay.h>"
|
90 | # define F_CPU 1000000UL
|
91 | #endif
|
92 |
|
93 | #ifndef __OPTIMIZE__
|
94 | # warning "Compiler optimizations disabled; functions from <util/delay.h> won't work as designed"
|
95 | #endif
|
96 |
|
97 | /**
|
98 | \ingroup util_delay
|
99 |
|
100 | Perform a delay of \c __ms milliseconds, using _delay_loop_2().
|
101 |
|
102 | The macro F_CPU is supposed to be defined to a
|
103 | constant defining the CPU clock frequency (in Hertz).
|
104 |
|
105 | The maximal possible delay is 262.14 ms / F_CPU in MHz.
|
106 |
|
107 | When the user request delay which exceed the maximum possible one,
|
108 | _delay_ms() provides a decreased resolution functionality. In this
|
109 | mode _delay_ms() will work with a resolution of 1/10 ms, providing
|
110 | delays up to 6.5535 seconds (independent from CPU frequency). The
|
111 | user will not be informed about decreased resolution.
|
112 |
|
113 | If the avr-gcc toolchain has __builtin_avr_delay_cycles(unsigned long)
|
114 | support, maximal possible delay is 4294967.295 ms/ F_CPU in MHz. For
|
115 | values greater than the maximal possible delay, overflows results in
|
116 | no delay i.e., 0ms.
|
117 |
|
118 | Conversion of __us into clock cycles may not always result in integer.
|
119 | By default, the clock cycles rounded up to next integer. This ensures that
|
120 | the user gets atleast __us microseconds of delay.
|
121 |
|
122 | Alternatively, user can define __DELAY_ROUND_DOWN__ and __DELAY_ROUND_CLOSEST__
|
123 | to round down and round to closest integer.
|
124 |
|
125 | Note: The new implementation of _delay_ms(double __ms) with
|
126 | __builtin_avr_delay_cycles(unsigned long) support is not backward compatible.
|
127 | User can define __DELAY_BACKWARD_COMPATIBLE__ to get a backward compatible delay
|
128 | although this will be deprecated in future.
|
129 |
|
130 | */
|
131 | void
|
132 | _delay_ms(double __ms)
|
133 | {
|
134 | uint16_t __ticks;
|
135 | double __tmp ;
|
136 | #if __HAS_DELAY_CYCLES && defined(__OPTIMIZE__) && !defined(__DELAY_BACKWARD_COMPATIBLE__)
|
137 | uint32_t __ticks_dc;
|
138 | extern void __builtin_avr_delay_cycles(unsigned long);
|
139 | __tmp = ((F_CPU) / 1e3) * __ms;
|
140 |
|
141 | #if defined(__DELAY_ROUND_DOWN__)
|
142 | __ticks_dc = (uint32_t)fabs(__tmp);
|
143 |
|
144 | #elif defined(__DELAY_ROUND_CLOSEST__)
|
145 | __ticks_dc = (uint32_t)(fabs(__tmp)+0.5);
|
146 |
|
147 | #else
|
148 | //round up by default
|
149 | __ticks_dc = (uint32_t)(ceil(fabs(__tmp)));
|
150 | #endif
|
151 |
|
152 | __builtin_avr_delay_cycles(__ticks_dc);
|
153 |
|
154 | #elif !__HAS_DELAY_CYCLES || (__HAS_DELAY_CYCLES && !defined(__OPTIMIZE__)) || defined (__DELAY_BACKWARD_COMPATIBLE__)
|
155 | __tmp = ((F_CPU) / 4e3) * __ms;
|
156 | if (__tmp < 1.0)
|
157 | __ticks = 1;
|
158 | else if (__tmp > 65535)
|
159 | {
|
160 | // __ticks = requested delay in 1/10 ms
|
161 | __ticks = (uint16_t) (__ms * 10.0);
|
162 | while(__ticks)
|
163 | {
|
164 | // wait 1/10 ms
|
165 | _delay_loop_2(((F_CPU) / 4e3) / 10);
|
166 | __ticks --;
|
167 | }
|
168 | return;
|
169 | }
|
170 | else
|
171 | __ticks = (uint16_t)__tmp;
|
172 | _delay_loop_2(__ticks);
|
173 | #endif
|
174 | }
|
175 |
|
176 | /**
|
177 | \ingroup util_delay
|
178 |
|
179 | Perform a delay of \c __us microseconds, using _delay_loop_1().
|
180 |
|
181 | The macro F_CPU is supposed to be defined to a
|
182 | constant defining the CPU clock frequency (in Hertz).
|
183 |
|
184 | The maximal possible delay is 768 us / F_CPU in MHz.
|
185 |
|
186 | If the user requests a delay greater than the maximal possible one,
|
187 | _delay_us() will automatically call _delay_ms() instead. The user
|
188 | will not be informed about this case.
|
189 |
|
190 | If the avr-gcc toolchain has __builtin_avr_delay_cycles(unsigned long)
|
191 | support, maximal possible delay is 4294967.295 us/ F_CPU in MHz. For
|
192 | values greater than the maximal possible delay, overflow results in
|
193 | no delay i.e., 0us.
|
194 |
|
195 | Conversion of __us into clock cycles may not always result in integer.
|
196 | By default, the clock cycles rounded up to next integer. This ensures that
|
197 | the user gets atleast __us microseconds of delay.
|
198 |
|
199 | Alternatively, user can define __DELAY_ROUND_DOWN__ and __DELAY_ROUND_CLOSEST__
|
200 | to round down and round to closest integer.
|
201 |
|
202 | Note: The new implementation of _delay_us(double __us) with
|
203 | __builtin_avr_delay_cycles(unsigned long) support is not backward compatible.
|
204 | User can define __DELAY_BACKWARD_COMPATIBLE__ to get a backward compatible delay
|
205 | although this will be deprecated in future.
|
206 |
|
207 | */
|
208 | void
|
209 | _delay_us(double __us)
|
210 | {
|
211 | uint8_t __ticks;
|
212 | double __tmp ;
|
213 | #if __HAS_DELAY_CYCLES && defined(__OPTIMIZE__) && !defined(__DELAY_BACKWARD_COMPATIBLE__)
|
214 | uint32_t __ticks_dc;
|
215 | extern void __builtin_avr_delay_cycles(unsigned long);
|
216 | __tmp = ((F_CPU) / 1e6) * __us;
|
217 |
|
218 | #if defined(__DELAY_ROUND_DOWN__)
|
219 | __ticks_dc = (uint32_t)fabs(__tmp);
|
220 |
|
221 | #elif defined(__DELAY_ROUND_CLOSEST__)
|
222 | __ticks_dc = (uint32_t)(fabs(__tmp)+0.5);
|
223 |
|
224 | #else
|
225 | //round up by default
|
226 | __ticks_dc = (uint32_t)(ceil(fabs(__tmp)));
|
227 | #endif
|
228 |
|
229 | __builtin_avr_delay_cycles(__ticks_dc);
|
230 |
|
231 | #elif !__HAS_DELAY_CYCLES || (__HAS_DELAY_CYCLES && !defined(__OPTIMIZE__)) || defined (__DELAY_BACKWARD_COMPATIBLE__)
|
232 | __tmp = ((F_CPU) / 3e6) * __us;
|
233 | if (__tmp < 1.0)
|
234 | __ticks = 1;
|
235 | else if (__tmp > 255)
|
236 | {
|
237 | _delay_ms(__us / 1000.0);
|
238 | return;
|
239 | }
|
240 | else
|
241 | __ticks = (uint8_t)__tmp;
|
242 | _delay_loop_1(__ticks);
|
243 | #endif
|
244 | }
|
245 |
|
246 |
|
247 | #endif /* _UTIL_DELAY_H_ */
|