avr-libc  2.0.0
Standard C library for AVR-GCC

AVR Libc Home Page

AVRs

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: sleep_8h_source.html,v 1.1.1.6 2016/02/09 07:13:53 joerg_wunsch Exp $ */
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 
144 #elif defined(SMCR)
145 
146  #define _SLEEP_CONTROL_REG SMCR
147  #define _SLEEP_ENABLE_MASK _BV(SE)
148 
149 #elif defined(__AVR_AT94K__)
150 
151  #define _SLEEP_CONTROL_REG MCUR
152  #define _SLEEP_ENABLE_MASK _BV(SE)
153 
154 #elif !defined(__DOXYGEN__)
155 
156  #define _SLEEP_CONTROL_REG MCUCR
157  #define _SLEEP_ENABLE_MASK _BV(SE)
158 
159 #endif
160 
161 
162 /* Special casing these three devices - they are the
163  only ones that need to write to more than one register. */
164 #if defined(__AVR_ATmega161__)
165 
166  #define set_sleep_mode(mode) \
167  do { \
168  MCUCR = ((MCUCR & ~_BV(SM1)) | ((mode) == SLEEP_MODE_PWR_DOWN || (mode) == SLEEP_MODE_PWR_SAVE ? _BV(SM1) : 0)); \
169  EMCUCR = ((EMCUCR & ~_BV(SM0)) | ((mode) == SLEEP_MODE_PWR_SAVE ? _BV(SM0) : 0)); \
170  } while(0)
171 
172 
173 #elif defined(__AVR_ATmega162__) \
174 || defined(__AVR_ATmega8515__)
175 
176  #define set_sleep_mode(mode) \
177  do { \
178  MCUCR = ((MCUCR & ~_BV(SM1)) | ((mode) == SLEEP_MODE_IDLE ? 0 : _BV(SM1))); \
179  MCUCSR = ((MCUCSR & ~_BV(SM2)) | ((mode) == SLEEP_MODE_STANDBY || (mode) == SLEEP_MODE_EXT_STANDBY ? _BV(SM2) : 0)); \
180  EMCUCR = ((EMCUCR & ~_BV(SM0)) | ((mode) == SLEEP_MODE_PWR_SAVE || (mode) == SLEEP_MODE_EXT_STANDBY ? _BV(SM0) : 0)); \
181  } while(0)
182 
183 /* For xmegas, check presence of SLEEP_SMODE<n>_bm and define set_sleep_mode accordingly. */
184 #elif defined(__AVR_XMEGA__)
185 #if defined(SLEEP_SMODE2_bm)
186 
187  #define set_sleep_mode(mode) \
188  do { \
189  _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(SLEEP_SMODE2_bm | SLEEP_SMODE1_bm | SLEEP_SMODE0_bm)) | (mode)); \
190  } while(0)
191 
192 #elif defined(SLEEP_SMODE1_bm)
193 
194  #define set_sleep_mode(mode) \
195  do { \
196  _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(SLEEP_SMODE1_bm | SLEEP_SMODE0_bm)) | (mode)); \
197  } while(0)
198 
199 #else
200 
201  #define set_sleep_mode(mode) \
202  do { \
203  _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~( SLEEP_SMODE0_bm)) | (mode)); \
204  } while(0)
205 
206 
207 #endif /* #if defined(SLEEP_SMODE2_bm) */
208 
209 /* For everything else, check for presence of SM<n> and define set_sleep_mode accordingly. */
210 #else
211 #if defined(SM2)
212 
213  #define set_sleep_mode(mode) \
214  do { \
215  _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
216  } while(0)
217 
218 #elif defined(SM1)
219 
220  #define set_sleep_mode(mode) \
221  do { \
222  _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1))) | (mode)); \
223  } while(0)
224 
225 #elif defined(SM)
226 
227  #define set_sleep_mode(mode) \
228  do { \
229  _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~_BV(SM)) | (mode)); \
230  } while(0)
231 
232 #else
233 
234  #error "No SLEEP mode defined for this device."
235 
236 #endif /* if defined(SM2) */
237 #endif /* #if defined(__AVR_ATmega161__) */
238 
239 
240 
241 /** \ingroup avr_sleep
242 
243  Put the device in sleep mode. How the device is brought out of sleep mode
244  depends on the specific mode selected with the set_sleep_mode() function.
245  See the data sheet for your device for more details. */
246 
247 
248 #if defined(__DOXYGEN__)
249 
250 /** \ingroup avr_sleep
251 
252  Set the SE (sleep enable) bit.
253 */
254 extern void sleep_enable (void);
255 
256 #else
257 
258 #define sleep_enable() \
259 do { \
260  _SLEEP_CONTROL_REG |= (uint8_t)_SLEEP_ENABLE_MASK; \
261 } while(0)
262 
263 #endif
264 
265 
266 #if defined(__DOXYGEN__)
267 
268 /** \ingroup avr_sleep
269 
270  Clear the SE (sleep enable) bit.
271 */
272 extern void sleep_disable (void);
273 
274 #else
275 
276 #define sleep_disable() \
277 do { \
278  _SLEEP_CONTROL_REG &= (uint8_t)(~_SLEEP_ENABLE_MASK); \
279 } while(0)
280 
281 #endif
282 
283 
284 /** \ingroup avr_sleep
285 
286  Put the device into sleep mode. The SE bit must be set
287  beforehand, and it is recommended to clear it afterwards.
288 */
289 #if defined(__DOXYGEN__)
290 
291 extern void sleep_cpu (void);
292 
293 #else
294 
295 #define sleep_cpu() \
296 do { \
297  __asm__ __volatile__ ( "sleep" "\n\t" :: ); \
298 } while(0)
299 
300 #endif
301 
302 
303 #if defined(__DOXYGEN__)
304 
305 /** \ingroup avr_sleep
306 
307  Put the device into sleep mode, taking care of setting
308  the SE bit before, and clearing it afterwards. */
309 extern void sleep_mode (void);
310 
311 #else
312 
313 #define sleep_mode() \
314 do { \
315  sleep_enable(); \
316  sleep_cpu(); \
317  sleep_disable(); \
318 } while (0)
319 
320 #endif
321 
322 
323 #if defined(__DOXYGEN__)
324 
325 /** \ingroup avr_sleep
326 
327  Disable BOD before going to sleep.
328  Not available on all devices.
329 */
330 extern void sleep_bod_disable (void);
331 
332 #else
333 
334 #if defined(BODS) && defined(BODSE)
335 
336 #ifdef BODCR
337 
338 #define BOD_CONTROL_REG BODCR
339 
340 #else
341 
342 #define BOD_CONTROL_REG MCUCR
343 
344 #endif
345 
346 #define sleep_bod_disable() \
347 do { \
348  uint8_t tempreg; \
349  __asm__ __volatile__("in %[tempreg], %[mcucr]" "\n\t" \
350  "ori %[tempreg], %[bods_bodse]" "\n\t" \
351  "out %[mcucr], %[tempreg]" "\n\t" \
352  "andi %[tempreg], %[not_bodse]" "\n\t" \
353  "out %[mcucr], %[tempreg]" \
354  : [tempreg] "=&d" (tempreg) \
355  : [mcucr] "I" _SFR_IO_ADDR(BOD_CONTROL_REG), \
356  [bods_bodse] "i" (_BV(BODS) | _BV(BODSE)), \
357  [not_bodse] "i" (~_BV(BODSE))); \
358 } while (0)
359 
360 #endif
361 
362 #endif
363 
364 
365 /*@}*/
366 
367 #endif /* _AVR_SLEEP_H_ */
void sleep_mode(void)
void sleep_cpu(void)
void sleep_enable(void)
void sleep_disable(void)
void sleep_bod_disable(void)