/* Copyright (c) 2002, Marek Michalkiewicz Copyright (c) 2004,2005 Joerg Wunsch All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* $Id: delay.h,v 1.1.2.1 2005/12/12 23:19:49 joerg_wunsch Exp $ */ #ifndef _UTIL_DELAY_H_ #define _UTIL_DELAY_H_ 1 #include /** \defgroup util_delay : Busy-wait delay loops \code #define F_CPU 1000000UL // 1 MHz //#define F_CPU 14.7456E6 #include \endcode \note As an alternative method, it is possible to pass the F_CPU macro down to the compiler from the Makefile. Obviously, in that case, no \c \#define statement should be used. The functions in this header file implement simple delay loops that perform a busy-waiting. They are typically used to facilitate short delays in the program execution. They are implemented as count-down loops with a well-known CPU cycle count per loop iteration. As such, no other processing can occur simultaneously. It should be kept in mind that the functions described here do not disable interrupts. In general, for long delays, the use of hardware timers is much preferrable, as they free the CPU, and allow for concurrent processing of other events while the timer is running. However, in particular for very short delays, the overhead of setting up a hardware timer is too much compared to the overall delay time. Two inline functions are provided for the actual delay algorithms. Two wrapper functions allow the specification of microsecond, and millisecond delays directly, using the application-supplied macro F_CPU as the CPU clock frequency (in Hertz). These functions operate on double typed arguments, however when optimization is turned on, the entire floating-point calculation will be done at compile-time. \note When using _delay_us() and _delay_ms(), the expressions passed as arguments to these functions shall be compile-time constants, otherwise the floating-point calculations to setup the loops will be done at run-time, thereby drastically increasing both the resulting code size, as well as the time required to setup the loops. */ #if !defined(__DOXYGEN__) static inline void _delay_loop_1(uint8_t __count) __attribute__((always_inline)); static inline void _delay_loop_2(uint16_t __count) __attribute__((always_inline)); static inline void _delay_loop_2b(uint16_t __count) __attribute__((always_inline)); static inline void _delay_us(double __us) __attribute__((always_inline)); static inline void _delay_us_2(double __us) __attribute__((always_inline)); static inline void _delay_ms(double __ms) __attribute__((always_inline)); #endif /** \ingroup util_delay Delay loop using an 8-bit counter \c __count, so up to 256 iterations are possible. (The value 256 would have to be passed as 0.) The loop executes three CPU cycles per iteration, not including the overhead the compiler needs to setup the counter register. Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds can be achieved. */ void _delay_loop_1(uint8_t __count) { __asm__ volatile ( "1: dec %0" "\n\t" "brne 1b" : "=r" (__count) : "0" (__count) ); } /** \ingroup util_delay Delay loop using a 16-bit counter \c __count, so up to 65536 iterations are possible. (The value 65536 would have to be passed as 0.) The loop executes four CPU cycles per iteration, not including the overhead the compiler requires to setup the counter register pair. Thus, at a CPU speed of 1 MHz, delays of up to about 262.1 milliseconds can be achieved. */ void _delay_loop_2(uint16_t __count) { __asm__ volatile ( "1: sbiw %0,1" "\n\t" "brne 1b" : "=w" (__count) : "0" (__count) ); } /** \ingroup util_delay Just like __delay_loop_2 but "plan b" explicitly uses r24-5 and clobber directive to (hopefully) repel any mov shenanigans that may eat clocks. Delay loop using a 16-bit counter \c __count, so up to 65536 iterations are possible. (The value 65536 would have to be passed as 0.) The loop executes four CPU cycles per iteration. not including the overhead the compiler requires to setup the counter register pair. Thus, at a CPU speed of 1 MHz, delays of up to about 262.1 milliseconds can be achieved. */ void _delay_loop_2b(uint16_t __count) { __asm__ volatile ( "ldi r24,lo8(%0) \n\t" "ldi r25,hi8(%0) \n\t" "1: sbiw r24,1 \n\t" "brne 1b \n\t" : : "n" (__count) : "r24", "r25" ); } #ifndef F_CPU /* prevent compiler error by supplying a default */ # warning "F_CPU not defined for " # define F_CPU 1000000UL #endif /** \ingroup util_delay Perform a delay of \c __us microseconds, using _delay_loop_1(). The macro F_CPU is supposed to be defined to a constant defining the CPU clock frequency (in Hertz). The maximal possible delay is 768 us / F_CPU in MHz. */ void _delay_us(double __us) { uint8_t __ticks; double __tmp = ((F_CPU) / 3e6) * __us; if (__tmp < 1.0) __ticks = 1; else if (__tmp > 255) __ticks = 0; /* i.e. 256 */ else __ticks = (uint8_t)__tmp; _delay_loop_1(__ticks); } /** \ingroup util_delay Perform a delay of \c __us microseconds, using _delay_loop_2b(). This routine will use nop shims to be as precise as possible, down to just 1 nop. Or, in the case of 0.9 clocks-worth of delay, this will do NOTHING (IOW, it rounds down). It must be called with a value known at compile-time. It needs gcc optimization (-O) in order to be inlined properly. Optimization will clash with using -g to hand-count a listing, sorry. The macro F_CPU is supposed to be defined to a constant defining the CPU clock frequency (in Hertz). The maximal possible delay is ~262.14 ms (not us!) / F_CPU in MHz. */ void _delay_us_2(const double __us) { const double __clocks_per_us=((F_CPU)/1e6); const double __clocks_delay = __clocks_per_us * __us; const char __loop_2b_fixed=7; // the 4 ldi clocks + the 3clk last loop const uint32_t __loop_2b_runs = ((__clocks_delay - __loop_2b_fixed) / 4)+1; // +1, don't forget the 3clk loop! const double __remainder_clocks= __clocks_delay - ((__loop_2b_runs-1) * 4 + __loop_2b_fixed); //the minus 1 un-considers the 3clk last loop, which is counted in __loop_3_fixed if(__loop_2b_runs < 65536) { if (__clocks_delay < 1) {/*DONOTHING*/} else if (__clocks_delay < 2) {asm volatile ("nop");} else if (__clocks_delay < 3) {asm volatile ("rjmp +0");} /*rjmp 0(implicit PC+1) = 2 nops*/ else if (__clocks_delay < 4) {asm volatile ("rjmp +0\n\t nop");} else if (__clocks_delay < 5) {asm volatile ("rjmp +0\n\t rjmp +0");} else if (__clocks_delay < 6) {asm volatile ("rjmp +0\n\t rjmp +0 \n\t nop");} else if (__clocks_delay < 7) {asm volatile ("rjmp +0\n\t rjmp +0 \n\t rjmp +0");} else if (__remainder_clocks < 1) { _delay_loop_2b((uint16_t)__loop_2b_runs); } else if (__remainder_clocks < 2) { _delay_loop_2b((uint16_t)__loop_2b_runs); asm volatile ("nop"); } else if (__remainder_clocks < 3) { _delay_loop_2b((uint16_t)__loop_2b_runs); asm volatile ("rjmp +0"); } else if (__remainder_clocks < 4) { _delay_loop_2b((uint16_t)__loop_2b_runs); asm volatile ("rjmp +0\n\t nop"); } } else {/*NOTREACHED*/} } /** \ingroup util_delay Perform a delay of \c __ms milliseconds, using _delay_loop_2(). The macro F_CPU is supposed to be defined to a constant defining the CPU clock frequency (in Hertz). The maximal possible delay is 262.14 ms / F_CPU in MHz. */ void _delay_ms(double __ms) { uint16_t __ticks; double __tmp = ((F_CPU) / 4e3) * __ms; if (__tmp < 1.0) __ticks = 1; else if (__tmp > 65535) __ticks = 0; /* i.e. 65536 */ else __ticks = (uint16_t)__tmp; _delay_loop_2(__ticks); } #endif /* _UTIL_DELAY_H_ */