AVR-LibC  2.3.0
Standard C library for AVR-GCC
 

AVR-LibC Manual

AVR-LibC Sources

Main Page

User Manual

Lib­rary Refe­rence

FAQ

Exam­ple Pro­jects

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