AVR-LibC  2.2.0
Standard C library for AVR-GCC
 

AVR-LibC Documentation

Logo

AVR-LibC Development Pages

Main Page

User Manual

Library Reference

FAQ

Example Projects

File List

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