avr-libc: sleep.h Source File

avr-libc 2.0.0 Standard C library for AVR-GCC |

## AVR Libc Home Page |  | ## AVR Libc Development Pages
## Main Page | ## User Manual | ## Library Reference | ## FAQ | ## Example Projects

sleep.h
Go to the documentation of this file.
1 /* Copyright (c) 2002, 2004 Theodore A. Roth
2 Copyright (c) 2004, 2007, 2008 Eric B. Weddington
3 Copyright (c) 2005, 2006, 2007 Joerg Wunsch
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$ */
34
35 #ifndef _AVR_SLEEP_H_
36 #define _AVR_SLEEP_H_ 1
37
38 #include <avr/io.h>
39 #include <stdint.h>
40
41
42 /** \file */
43
44 /** \defgroup avr_sleep <avr/sleep.h>: Power Management and Sleep Modes
45
46 \code #include <avr/sleep.h>\endcode
47
48 Use of the \c SLEEP instruction can allow an application to reduce its
49 power comsumption considerably. AVR devices can be put into different
50 sleep modes. Refer to the datasheet for the details relating to the device
51 you are using.
52
53 There are several macros provided in this header file to actually
54 put the device into sleep mode. The simplest way is to optionally
55 set the desired sleep mode using \c set_sleep_mode() (it usually
56 defaults to idle mode where the CPU is put on sleep but all
57 peripheral clocks are still running), and then call
58 \c sleep_mode(). This macro automatically sets the sleep enable bit, goes
59 to sleep, and clears the sleep enable bit.
60
61 Example:
62 \code
63 #include <avr/sleep.h>
64
65 ...
66 set_sleep_mode(<mode>);
67 sleep_mode();
68 \endcode
69
70 Note that unless your purpose is to completely lock the CPU (until a
71 hardware reset), interrupts need to be enabled before going to sleep.
72
73 As the \c sleep_mode() macro might cause race conditions in some
74 situations, the individual steps of manipulating the sleep enable
75 (SE) bit, and actually issuing the \c SLEEP instruction, are provided
76 in the macros \c sleep_enable(), \c sleep_disable(), and
77 \c sleep_cpu(). This also allows for test-and-sleep scenarios that
78 take care of not missing the interrupt that will awake the device
79 from sleep.
80
81 Example:
82 \code
83 #include <avr/interrupt.h>
84 #include <avr/sleep.h>
85
86 ...
87 set_sleep_mode(<mode>);
88 cli();
89 if (some_condition)
90 {
91 sleep_enable();
92 sei();
93 sleep_cpu();
94 sleep_disable();
95 }
96 sei();
97 \endcode
98
99 This sequence ensures an atomic test of \c some_condition with
100 interrupts being disabled. If the condition is met, sleep mode
101 will be prepared, and the \c SLEEP instruction will be scheduled
102 immediately after an \c SEI instruction. As the intruction right
103 after the \c SEI is guaranteed to be executed before an interrupt
104 could trigger, it is sure the device will really be put to sleep.
105
106 Some devices have the ability to disable the Brown Out Detector (BOD) before
107 going to sleep. This will also reduce power while sleeping. If the
108 specific AVR device has this ability then an additional macro is defined:
109 \c sleep_bod_disable(). This macro generates inlined assembly code
110 that will correctly implement the timed sequence for disabling the BOD
111 before sleeping. However, there is a limited number of cycles after the
112 BOD has been disabled that the device can be put into sleep mode, otherwise
113 the BOD will not truly be disabled. Recommended practice is to disable
114 the BOD (\c sleep_bod_disable()), set the interrupts (\c sei()), and then
115 put the device to sleep (\c sleep_cpu()), like so:
116
117 \code
118 #include <avr/interrupt.h>
119 #include <avr/sleep.h>
120
121 ...
122 set_sleep_mode(<mode>);
123 cli();
124 if (some_condition)
125 {
126 sleep_enable();
127 sleep_bod_disable();
128 sei();
129 sleep_cpu();
130 sleep_disable();
131 }
132 sei();
133 \endcode
134 */
135
136
137 /* Define an internal sleep control register and an internal sleep enable bit mask. */
138 #if defined(SLEEP_CTRL)
139
140 /* XMEGA devices */
141 #define _SLEEP_CONTROL_REG SLEEP_CTRL
142 #define _SLEEP_ENABLE_MASK SLEEP_SEN_bm
143 #define _SLEEP_SMODE_GROUP_MASK SLEEP_SMODE_gm
144
145 #elif defined(SLPCTRL)
146
147 /* New xmega devices */
148 #define _SLEEP_CONTROL_REG SLPCTRL_CTRLA
149 #define _SLEEP_ENABLE_MASK SLPCTRL_SEN_bm
150 #define _SLEEP_SMODE_GROUP_MASK SLPCTRL_SMODE_gm
151
152 #elif defined(SMCR)
153
154 #define _SLEEP_CONTROL_REG SMCR
155 #define _SLEEP_ENABLE_MASK _BV(SE)
156
157 #elif defined(__AVR_AT94K__)
158
159 #define _SLEEP_CONTROL_REG MCUR
160 #define _SLEEP_ENABLE_MASK _BV(SE)
161
162 #elif !defined(__DOXYGEN__)
163
164 #define _SLEEP_CONTROL_REG MCUCR
165 #define _SLEEP_ENABLE_MASK _BV(SE)
166
167 #endif
168
169
170 /* Special casing these three devices - they are the
171 only ones that need to write to more than one register. */
172 #if defined(__AVR_ATmega161__)
173
174 #define set_sleep_mode(mode) \
175 do { \
176 MCUCR = ((MCUCR & ~_BV(SM1)) | ((mode) == SLEEP_MODE_PWR_DOWN || (mode) == SLEEP_MODE_PWR_SAVE ? _BV(SM1) : 0)); \
177 EMCUCR = ((EMCUCR & ~_BV(SM0)) | ((mode) == SLEEP_MODE_PWR_SAVE ? _BV(SM0) : 0)); \
178 } while(0)
179
180
181 #elif defined(__AVR_ATmega162__) \
182 || defined(__AVR_ATmega8515__)
183
184 #define set_sleep_mode(mode) \
185 do { \
186 MCUCR = ((MCUCR & ~_BV(SM1)) | ((mode) == SLEEP_MODE_IDLE ? 0 : _BV(SM1))); \
187 MCUCSR = ((MCUCSR & ~_BV(SM2)) | ((mode) == SLEEP_MODE_STANDBY || (mode) == SLEEP_MODE_EXT_STANDBY ? _BV(SM2) : 0)); \
188 EMCUCR = ((EMCUCR & ~_BV(SM0)) | ((mode) == SLEEP_MODE_PWR_SAVE || (mode) == SLEEP_MODE_EXT_STANDBY ? _BV(SM0) : 0)); \
189 } while(0)
190
191 /* For xmegas, check presence of SLEEP_SMODE<n>_bm and define set_sleep_mode accordingly. */
192 #elif defined(__AVR_XMEGA__)
193
194 #define set_sleep_mode(mode) \
195 do { \
196 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_SLEEP_SMODE_GROUP_MASK)) | (mode)); \
197 } while(0)
198
199 /* For everything else, check for presence of SM<n> and define set_sleep_mode accordingly. */
200 #else
201 #if defined(SM2)
202
203 #define set_sleep_mode(mode) \
204 do { \
205 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
206 } while(0)
207
208 #elif defined(SM1)
209
210 #define set_sleep_mode(mode) \
211 do { \
212 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1))) | (mode)); \
213 } while(0)
214
215 #elif defined(SM)
216
217 #define set_sleep_mode(mode) \
218 do { \
219 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~_BV(SM)) | (mode)); \
220 } while(0)
221
222 #else
223
224 #error "No SLEEP mode defined for this device."
225
226 #endif /* if defined(SM2) */
227 #endif /* #if defined(__AVR_ATmega161__) */
228
229
230
231 /** \ingroup avr_sleep
232
233 Put the device in sleep mode. How the device is brought out of sleep mode
234 depends on the specific mode selected with the set_sleep_mode() function.
235 See the data sheet for your device for more details. */
236
237
238 #if defined(__DOXYGEN__)
239
240 /** \ingroup avr_sleep
241
242 Set the SE (sleep enable) bit.
243 */
244 extern void sleep_enable (void);
245
246 #else
247
248 #define sleep_enable() \
249 do { \
250 _SLEEP_CONTROL_REG |= (uint8_t)_SLEEP_ENABLE_MASK; \
251 } while(0)
252
253 #endif
254
255
256 #if defined(__DOXYGEN__)
257
258 /** \ingroup avr_sleep
259
260 Clear the SE (sleep enable) bit.
261 */
262 extern void sleep_disable (void);
263
264 #else
265
266 #define sleep_disable() \
267 do { \
268 _SLEEP_CONTROL_REG &= (uint8_t)(~_SLEEP_ENABLE_MASK); \
269 } while(0)
270
271 #endif
272
273
274 /** \ingroup avr_sleep
275
276 Put the device into sleep mode. The SE bit must be set
277 beforehand, and it is recommended to clear it afterwards.
278 */
279 #if defined(__DOXYGEN__)
280
281 extern void sleep_cpu (void);
282
283 #else
284
285 #define sleep_cpu() \
286 do { \
287 __asm__ __volatile__ ( "sleep" "\n\t" :: ); \
288 } while(0)
289
290 #endif
291
292
293 #if defined(__DOXYGEN__)
294
295 /** \ingroup avr_sleep
296
297 Put the device into sleep mode, taking care of setting
298 the SE bit before, and clearing it afterwards. */
299 extern void sleep_mode (void);
300
301 #else
302
303 #define sleep_mode() \
304 do { \
305 sleep_enable(); \
306 sleep_cpu(); \
307 sleep_disable(); \
308 } while (0)
309
310 #endif
311
312
313 #if defined(__DOXYGEN__)
314
315 /** \ingroup avr_sleep
316
317 Disable BOD before going to sleep.
318 Not available on all devices.
319 */
320 extern void sleep_bod_disable (void);
321
322 #else
323
324 #if defined(BODS) && defined(BODSE)
325
326 #ifdef BODCR
327
328 #define BOD_CONTROL_REG BODCR
329
330 #else
331
332 #define BOD_CONTROL_REG MCUCR
333
334 #endif
335
336 #define sleep_bod_disable() \
337 do { \
338 uint8_t tempreg; \
339 __asm__ __volatile__("in %[tempreg], %[mcucr]" "\n\t" \
340 "ori %[tempreg], %[bods_bodse]" "\n\t" \
341 "out %[mcucr], %[tempreg]" "\n\t" \
342 "andi %[tempreg], %[not_bodse]" "\n\t" \
343 "out %[mcucr], %[tempreg]" \
344 : [tempreg] "=&d" (tempreg) \
345 : [mcucr] "I" _SFR_IO_ADDR(BOD_CONTROL_REG), \
346 [bods_bodse] "i" (_BV(BODS) | _BV(BODSE)), \
347 [not_bodse] "i" (~_BV(BODSE))); \
348 } while (0)
349
350 #endif
351
352 #endif
353
354
355 /*@}*/
356
357 #endif /* _AVR_SLEEP_H_ */
sleep_enable
void sleep_enable(void)
sleep_mode
void sleep_mode(void)
sleep_bod_disable
void sleep_bod_disable(void)
sleep_disable
void sleep_disable(void)
sleep_cpu
void sleep_cpu(void)
stdint.h
io.h