AVR-LibC  2.3.0git
Standard C library for AVR-GCC
 

AVR-LibC Documen­tation

AVR-LibC Development Pages

Main Page

User Manual

Library Refe­rence

FAQ

Example Projects

File List

Index

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