1 | /* Copyright (c) 2002, Marek Michalkiewicz
|
2 | Copyright (c) 2007 Joerg Wunsch
|
3 | All rights reserved.
|
4 |
|
5 | Redistribution and use in source and binary forms, with or without
|
6 | modification, are permitted provided that the following conditions are met:
|
7 |
|
8 | * Redistributions of source code must retain the above copyright
|
9 | notice, this list of conditions and the following disclaimer.
|
10 |
|
11 | * Redistributions in binary form must reproduce the above copyright
|
12 | notice, this list of conditions and the following disclaimer in
|
13 | the documentation and/or other materials provided with the
|
14 | distribution.
|
15 |
|
16 | * Neither the name of the copyright holders nor the names of
|
17 | contributors may be used to endorse or promote products derived
|
18 | from this software without specific prior written permission.
|
19 |
|
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
24 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
27 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
28 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
29 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30 | POSSIBILITY OF SUCH DAMAGE. */
|
31 |
|
32 | /* $Id: delay_basic.h,v 1.1 2007/05/13 21:23:20 joerg_wunsch Exp $ */
|
33 |
|
34 | #ifndef _UTIL_DELAY_BASIC_H_
|
35 | #define _UTIL_DELAY_BASIC_H_ 1
|
36 |
|
37 | #include <inttypes.h>
|
38 |
|
39 | /** \file */
|
40 | /** \defgroup util_delay_basic <util/delay_basic.h>: Basic busy-wait delay loops
|
41 | \code
|
42 | #include <util/delay_basic.h>
|
43 | \endcode
|
44 |
|
45 | The functions in this header file implement simple delay loops
|
46 | that perform a busy-waiting. They are typically used to
|
47 | facilitate short delays in the program execution. They are
|
48 | implemented as count-down loops with a well-known CPU cycle
|
49 | count per loop iteration. As such, no other processing can
|
50 | occur simultaneously. It should be kept in mind that the
|
51 | functions described here do not disable interrupts.
|
52 |
|
53 | In general, for long delays, the use of hardware timers is
|
54 | much preferrable, as they free the CPU, and allow for
|
55 | concurrent processing of other events while the timer is
|
56 | running. However, in particular for very short delays, the
|
57 | overhead of setting up a hardware timer is too much compared
|
58 | to the overall delay time.
|
59 |
|
60 | Two inline functions are provided for the actual delay algorithms.
|
61 |
|
62 | */
|
63 |
|
64 | #if !defined(__DOXYGEN__)
|
65 | static inline void _delay_loop_1(uint8_t __count) __attribute__((always_inline));
|
66 | static inline void _delay_loop_2(uint16_t __count) __attribute__((always_inline));
|
67 | #endif
|
68 |
|
69 | /** \ingroup util_delay_basic
|
70 |
|
71 | Delay loop using an 8-bit counter \c __count, so up to 256
|
72 | iterations are possible. (The value 256 would have to be passed
|
73 | as 0.) The loop executes three CPU cycles per iteration, not
|
74 | including the overhead the compiler needs to setup the counter
|
75 | register.
|
76 |
|
77 | Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds
|
78 | can be achieved.
|
79 | */
|
80 | void
|
81 | _delay_loop_1(uint8_t __count)
|
82 | {
|
83 | __asm__ volatile (
|
84 | "1: dec %0" "\n\t"
|
85 | "brne 1b"
|
86 | : "=r" (__count)
|
87 | : "0" (__count)
|
88 | );
|
89 | }
|
90 |
|
91 | /** \ingroup util_delay_basic
|
92 |
|
93 | Delay loop using a 16-bit counter \c __count, so up to 65536
|
94 | iterations are possible. (The value 65536 would have to be
|
95 | passed as 0.) The loop executes four CPU cycles per iteration,
|
96 | not including the overhead the compiler requires to setup the
|
97 | counter register pair.
|
98 |
|
99 | Thus, at a CPU speed of 1 MHz, delays of up to about 262.1
|
100 | milliseconds can be achieved.
|
101 | */
|
102 | void
|
103 | _delay_loop_2(uint16_t __count)
|
104 | {
|
105 | __asm__ volatile (
|
106 | "1: sbiw %0,1" "\n\t"
|
107 | "brne 1b"
|
108 | : "=w" (__count)
|
109 | : "0" (__count)
|
110 | );
|
111 | }
|
112 |
|
113 | #endif /* _UTIL_DELAY_BASIC_H_ */
|