AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page User Manual Library Reference FAQ Alphabetical Index Example Projects

sleep.h

Go to the documentation of this file.
00001 /* Copyright (c) 2002, 2004 Theodore A. Roth
00002    Copyright (c) 2004, 2007, 2008 Eric B. Weddington
00003    Copyright (c) 2005, 2006, 2007 Joerg Wunsch
00004    All rights reserved.
00005 
00006    Redistribution and use in source and binary forms, with or without
00007    modification, are permitted provided that the following conditions are met:
00008 
00009    * Redistributions of source code must retain the above copyright
00010      notice, this list of conditions and the following disclaimer.
00011 
00012    * Redistributions in binary form must reproduce the above copyright
00013      notice, this list of conditions and the following disclaimer in
00014      the documentation and/or other materials provided with the
00015      distribution.
00016 
00017    * Neither the name of the copyright holders nor the names of
00018      contributors may be used to endorse or promote products derived
00019      from this software without specific prior written permission.
00020 
00021    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00022    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00025    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00026    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00027    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00029    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00030    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031    POSSIBILITY OF SUCH DAMAGE. */
00032 
00033 /* $Id: sleep.h 2211 2011-02-14 14:04:25Z aboyapati $ */
00034 
00035 #ifndef _AVR_SLEEP_H_
00036 #define _AVR_SLEEP_H_ 1
00037 
00038 #include <avr/io.h>
00039 #include <stdint.h>
00040 
00041 
00042 /** \file */
00043 
00044 /** \defgroup avr_sleep <avr/sleep.h>: Power Management and Sleep Modes
00045 
00046     \code #include <avr/sleep.h>\endcode
00047 
00048     Use of the \c SLEEP instruction can allow an application to reduce its
00049     power comsumption considerably. AVR devices can be put into different
00050     sleep modes. Refer to the datasheet for the details relating to the device
00051     you are using.
00052 
00053     There are several macros provided in this header file to actually
00054     put the device into sleep mode.  The simplest way is to optionally
00055     set the desired sleep mode using \c set_sleep_mode() (it usually
00056     defaults to idle mode where the CPU is put on sleep but all
00057     peripheral clocks are still running), and then call
00058     \c sleep_mode(). This macro automatically sets the sleep enable bit, goes 
00059     to sleep, and clears the sleep enable bit.
00060     
00061     Example:
00062     \code
00063     #include <avr/sleep.h>
00064 
00065     ...
00066       set_sleep_mode(<mode>);
00067       sleep_mode();
00068     \endcode
00069     
00070     Note that unless your purpose is to completely lock the CPU (until a 
00071     hardware reset), interrupts need to be enabled before going to sleep.
00072 
00073     As the \c sleep_mode() macro might cause race conditions in some
00074     situations, the individual steps of manipulating the sleep enable
00075     (SE) bit, and actually issuing the \c SLEEP instruction, are provided
00076     in the macros \c sleep_enable(), \c sleep_disable(), and
00077     \c sleep_cpu().  This also allows for test-and-sleep scenarios that
00078     take care of not missing the interrupt that will awake the device
00079     from sleep.
00080 
00081     Example:
00082     \code
00083     #include <avr/interrupt.h>
00084     #include <avr/sleep.h>
00085 
00086     ...
00087       set_sleep_mode(<mode>);
00088       cli();
00089       if (some_condition)
00090       {
00091         sleep_enable();
00092         sei();
00093         sleep_cpu();
00094         sleep_disable();
00095       }
00096       sei();
00097     \endcode
00098 
00099     This sequence ensures an atomic test of \c some_condition with
00100     interrupts being disabled.  If the condition is met, sleep mode
00101     will be prepared, and the \c SLEEP instruction will be scheduled
00102     immediately after an \c SEI instruction.  As the intruction right
00103     after the \c SEI is guaranteed to be executed before an interrupt
00104     could trigger, it is sure the device will really be put to sleep.
00105 
00106     Some devices have the ability to disable the Brown Out Detector (BOD) before 
00107     going to sleep. This will also reduce power while sleeping. If the
00108     specific AVR device has this ability then an additional macro is defined:
00109     \c sleep_bod_disable(). This macro generates inlined assembly code
00110     that will correctly implement the timed sequence for disabling the BOD
00111     before sleeping. However, there is a limited number of cycles after the
00112     BOD has been disabled that the device can be put into sleep mode, otherwise
00113     the BOD will not truly be disabled. Recommended practice is to disable
00114     the BOD (\c sleep_bod_disable()), set the interrupts (\c sei()), and then
00115     put the device to sleep (\c sleep_cpu()), like so:
00116 
00117     \code
00118     #include <avr/interrupt.h>
00119     #include <avr/sleep.h>
00120 
00121     ...
00122       set_sleep_mode(<mode>);
00123       cli();
00124       if (some_condition)
00125       {
00126         sleep_enable();
00127         sleep_bod_disable();
00128         sei();
00129         sleep_cpu();
00130         sleep_disable();
00131       }
00132       sei();
00133     \endcode
00134 */
00135 
00136 
00137 /* Define an internal sleep control register and an internal sleep enable bit mask. */
00138 #if defined(SLEEP_CTRL)
00139 
00140     /* XMEGA devices */
00141     #define _SLEEP_CONTROL_REG  SLEEP_CTRL
00142     #define _SLEEP_ENABLE_MASK  SLEEP_SEN_bm
00143 
00144 #elif defined(SMCR)
00145 
00146     #define _SLEEP_CONTROL_REG  SMCR
00147     #define _SLEEP_ENABLE_MASK  _BV(SE)
00148 
00149 #elif defined(__AVR_AT94K__)
00150 
00151     #define _SLEEP_CONTROL_REG  MCUR
00152     #define _SLEEP_ENABLE_MASK  _BV(SE)
00153 
00154 #else
00155 
00156     #define _SLEEP_CONTROL_REG  MCUCR
00157     #define _SLEEP_ENABLE_MASK  _BV(SE)
00158 
00159 #endif
00160 
00161 
00162 /* Define set_sleep_mode() and sleep mode values per device. */
00163 #if defined(__AVR_ATmega161__)
00164 
00165     #define SLEEP_MODE_IDLE         0
00166     #define SLEEP_MODE_PWR_DOWN     1
00167     #define SLEEP_MODE_PWR_SAVE     2
00168 
00169     #define set_sleep_mode(mode) \
00170     do { \
00171         MCUCR = ((MCUCR & ~_BV(SM1)) | ((mode) == SLEEP_MODE_PWR_DOWN || (mode) == SLEEP_MODE_PWR_SAVE ? _BV(SM1) : 0)); \
00172         EMCUCR = ((EMCUCR & ~_BV(SM0)) | ((mode) == SLEEP_MODE_PWR_SAVE ? _BV(SM0) : 0)); \
00173     } while(0)
00174 
00175 
00176 #elif defined(__AVR_ATmega162__) \
00177 || defined(__AVR_ATmega8515__)
00178 
00179     #define SLEEP_MODE_IDLE         0
00180     #define SLEEP_MODE_PWR_DOWN     1
00181     #define SLEEP_MODE_PWR_SAVE     2
00182     #define SLEEP_MODE_ADC          3
00183     #define SLEEP_MODE_STANDBY      4
00184     #define SLEEP_MODE_EXT_STANDBY  5
00185 
00186     #define set_sleep_mode(mode) \
00187     do { \
00188         MCUCR = ((MCUCR & ~_BV(SM1)) | ((mode) == SLEEP_MODE_IDLE ? 0 : _BV(SM1))); \
00189         MCUCSR = ((MCUCSR & ~_BV(SM2)) | ((mode) == SLEEP_MODE_STANDBY  || (mode) == SLEEP_MODE_EXT_STANDBY ? _BV(SM2) : 0)); \
00190         EMCUCR = ((EMCUCR & ~_BV(SM0)) | ((mode) == SLEEP_MODE_PWR_SAVE || (mode) == SLEEP_MODE_EXT_STANDBY ? _BV(SM0) : 0)); \
00191     } while(0)
00192 
00193 #elif defined(__AVR_AT90S2313__) \
00194 || defined(__AVR_AT90S2323__) \
00195 || defined(__AVR_AT90S2333__) \
00196 || defined(__AVR_AT90S2343__) \
00197 || defined(__AVR_AT43USB320__) \
00198 || defined(__AVR_AT43USB355__) \
00199 || defined(__AVR_AT90S4414__) \
00200 || defined(__AVR_AT90S4433__) \
00201 || defined(__AVR_AT90S8515__) \
00202 || defined(__AVR_ATtiny22__)
00203 
00204     #define SLEEP_MODE_IDLE         0
00205     #define SLEEP_MODE_PWR_DOWN     _BV(SM)
00206 
00207     #define set_sleep_mode(mode) \
00208     do { \
00209         _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~__BV(SM)) | (mode)); \
00210     } while(0)
00211 
00212 #elif defined(__AVR_ATtiny167__) \
00213 || defined(__AVR_ATtiny87__)
00214 
00215     #define SLEEP_MODE_IDLE         0
00216     #define SLEEP_MODE_ADC          _BV(SM0)
00217     #define SLEEP_MODE_PWR_DOWN     _BV(SM1)
00218 
00219     #define set_sleep_mode(mode) \
00220     do { \
00221         _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1))) | (mode)); \
00222     } while(0)
00223 
00224 #elif defined(__AVR_AT90S4434__) \
00225 || defined(__AVR_AT76C711__) \
00226 || defined(__AVR_AT90S8535__) \
00227 || defined(__AVR_ATmega103__) \
00228 || defined(__AVR_ATmega161__) \
00229 || defined(__AVR_ATmega163__) \
00230 || defined(__AVR_ATmega16HVB__) \
00231 || defined(__AVR_ATmega16HVBREVB__) \
00232 || defined(__AVR_ATmega32HVB__) \
00233 || defined(__AVR_ATmega32HVBREVB__) \
00234 || defined(__AVR_ATtiny13__) \
00235 || defined(__AVR_ATtiny13A__) \
00236 || defined(__AVR_ATtiny15__) \
00237 || defined(__AVR_ATtiny24__) \
00238 || defined(__AVR_ATtiny24A__) \
00239 || defined(__AVR_ATtiny44__) \
00240 || defined(__AVR_ATtiny44A__) \
00241 || defined(__AVR_ATtiny84__) \
00242 || defined(__AVR_ATtiny84A__) \
00243 || defined(__AVR_ATtiny25__) \
00244 || defined(__AVR_ATtiny45__) \
00245 || defined(__AVR_ATtiny48__) \
00246 || defined(__AVR_ATtiny85__) \
00247 || defined(__AVR_ATtiny261__) \
00248 || defined(__AVR_ATtiny261A__) \
00249 || defined(__AVR_ATtiny461__) \
00250 || defined(__AVR_ATtiny461A__) \
00251 || defined(__AVR_ATtiny861__) \
00252 || defined(__AVR_ATtiny861A__) \
00253 || defined(__AVR_ATtiny88__) \
00254 || defined(__AVR_ATtiny1634__)
00255 
00256     #define SLEEP_MODE_IDLE         0
00257     #define SLEEP_MODE_ADC          _BV(SM0)
00258     #define SLEEP_MODE_PWR_DOWN     _BV(SM1)
00259     #define SLEEP_MODE_PWR_SAVE     (_BV(SM0) | _BV(SM1))
00260 
00261     #define set_sleep_mode(mode) \
00262     do { \
00263         _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1))) | (mode)); \
00264     } while(0)
00265 
00266 #elif defined(__AVR_ATtiny2313__) \
00267 || defined(__AVR_ATtiny2313A__) \
00268 || defined(__AVR_ATtiny4313__)
00269 
00270     #define SLEEP_MODE_IDLE         0
00271     #define SLEEP_MODE_PWR_DOWN     (_BV(SM0) | _BV(SM1))
00272     #define SLEEP_MODE_STANDBY      _BV(SM1)
00273 
00274     #define set_sleep_mode(mode) \
00275     do { \
00276         _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1))) | (mode)); \
00277     } while(0)
00278 
00279 #elif defined(__AVR_AT94K__)
00280 
00281     #define SLEEP_MODE_IDLE         0
00282     #define SLEEP_MODE_PWR_DOWN     _BV(SM1)
00283     #define SLEEP_MODE_PWR_SAVE     (_BV(SM0) | _BV(SM1))
00284 
00285     #define set_sleep_mode(mode) \
00286     do { \
00287         _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1))) | (mode)); \
00288     } while(0)
00289 
00290 #elif defined(__AVR_ATtiny26__) \
00291 || defined(__AVR_ATtiny43U__)
00292 
00293     #define SLEEP_MODE_IDLE         0
00294     #define SLEEP_MODE_ADC          _BV(SM0)
00295     #define SLEEP_MODE_PWR_DOWN     _BV(SM1)
00296     #define SLEEP_MODE_STANDBY      (_BV(SM0) | _BV(SM1))
00297 
00298     #define set_sleep_mode(mode) \
00299     do { \
00300         _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1))) | (mode)); \
00301     } while(0)
00302 
00303 #elif defined(__AVR_AT90PWM216__) \
00304 || defined(__AVR_AT90PWM316__) \
00305 || defined(__AVR_AT90PWM161__) \
00306 || defined(__AVR_AT90PWM81__)
00307 
00308     #define SLEEP_MODE_IDLE         0
00309     #define SLEEP_MODE_ADC          _BV(SM0)
00310     #define SLEEP_MODE_PWR_DOWN     _BV(SM1)
00311     #define SLEEP_MODE_STANDBY      (_BV(SM1) | _BV(SM2))
00312 
00313     #define set_sleep_mode(mode) \
00314     do { \
00315         _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
00316     } while(0)
00317 
00318 #elif defined(__AVR_AT90CAN128__) \
00319 || defined(__AVR_AT90CAN32__) \
00320 || defined(__AVR_AT90CAN64__) \
00321 || defined(__AVR_AT90PWM1__) \
00322 || defined(__AVR_AT90PWM2__) \
00323 || defined(__AVR_AT90PWM2B__) \
00324 || defined(__AVR_AT90PWM3__) \
00325 || defined(__AVR_AT90PWM3B__) \
00326 || defined(__AVR_AT90USB162__) \
00327 || defined(__AVR_AT90USB82__) \
00328 || defined(__AVR_AT90USB1286__) \
00329 || defined(__AVR_AT90USB1287__) \
00330 || defined(__AVR_AT90USB646__) \
00331 || defined(__AVR_AT90USB647__) \
00332 || defined(__AVR_ATmega128__) \
00333 || defined(__AVR_ATmega1280__) \
00334 || defined(__AVR_ATmega1281__) \
00335 || defined(__AVR_ATmega1284P__) \
00336 || defined(__AVR_ATmega128RFA1__) \
00337 || defined(__AVR_ATmega16__) \
00338 || defined(__AVR_ATmega16A__) \
00339 || defined(__AVR_ATmega162__) \
00340 || defined(__AVR_ATmega164A__) \
00341 || defined(__AVR_ATmega164P__) \
00342 || defined(__AVR_ATmega165__) \
00343 || defined(__AVR_ATmega165A__) \
00344 || defined(__AVR_ATmega165P__) \
00345 || defined(__AVR_ATmega168__) \
00346 || defined(__AVR_ATmega168A__) \
00347 || defined(__AVR_ATmega168P__) \
00348 || defined(__AVR_ATmega169__) \
00349 || defined(__AVR_ATmega169A__) \
00350 || defined(__AVR_ATmega169P__) \
00351 || defined(__AVR_ATmega169PA__) \
00352 || defined(__AVR_ATmega16HVA__) \
00353 || defined(__AVR_ATmega16HVA2__) \
00354 || defined(__AVR_ATmega16M1__) \
00355 || defined(__AVR_ATmega16U2__) \
00356 || defined(__AVR_ATmega16U4__) \
00357 || defined(__AVR_ATmega2560__) \
00358 || defined(__AVR_ATmega2561__) \
00359 || defined(__AVR_ATmega32__) \
00360 || defined(__AVR_ATmega323__) \
00361 || defined(__AVR_ATmega324A__) \
00362 || defined(__AVR_ATmega324P__) \
00363 || defined(__AVR_ATmega324PA__) \
00364 || defined(__AVR_ATmega325__) \
00365 || defined(__AVR_ATmega325A__) \
00366 || defined(__AVR_ATmega325PA__) \
00367 || defined(__AVR_ATmega3250__) \
00368 || defined(__AVR_ATmega3250A__) \
00369 || defined(__AVR_ATmega3250PA__) \
00370 || defined(__AVR_ATmega328__) \
00371 || defined(__AVR_ATmega328P__) \
00372 || defined(__AVR_ATmega329__) \
00373 || defined(__AVR_ATmega329A__) \
00374 || defined(__AVR_ATmega329P__) \
00375 || defined(__AVR_ATmega329PA__) \
00376 || defined(__AVR_ATmega3290__) \
00377 || defined(__AVR_ATmega3290A__) \
00378 || defined(__AVR_ATmega3290P__) \
00379 || defined(__AVR_ATmega3290PA__) \
00380 || defined(__AVR_ATmega32C1__) \
00381 || defined(__AVR_ATmega32M1__) \
00382 || defined(__AVR_ATmega32U2__) \
00383 || defined(__AVR_ATmega32U4__) \
00384 || defined(__AVR_ATmega32U6__) \
00385 || defined(__AVR_ATmega406__) \
00386 || defined(__AVR_ATmega48__) \
00387 || defined(__AVR_ATmega48A__) \
00388 || defined(__AVR_ATmega48PA__) \
00389 || defined(__AVR_ATmega48P__) \
00390 || defined(__AVR_ATmega64__) \
00391 || defined(__AVR_ATmega640__) \
00392 || defined(__AVR_ATmega644__) \
00393 || defined(__AVR_ATmega644A__) \
00394 || defined(__AVR_ATmega644P__) \
00395 || defined(__AVR_ATmega644PA__) \
00396 || defined(__AVR_ATmega645__) \
00397 || defined(__AVR_ATmega645A__) \
00398 || defined(__AVR_ATmega645P__) \
00399 || defined(__AVR_ATmega6450__) \
00400 || defined(__AVR_ATmega6450A__) \
00401 || defined(__AVR_ATmega6450P__) \
00402 || defined(__AVR_ATmega649__) \
00403 || defined(__AVR_ATmega649A__) \
00404 || defined(__AVR_ATmega6490__) \
00405 || defined(__AVR_ATmega6490A__) \
00406 || defined(__AVR_ATmega6490P__) \
00407 || defined(__AVR_ATmega649P__) \
00408 || defined(__AVR_ATmega64C1__) \
00409 || defined(__AVR_ATmega64HVE__) \
00410 || defined(__AVR_ATmega64M1__) \
00411 || defined(__AVR_ATmega8__) \
00412 || defined(__AVR_ATmega8515__) \
00413 || defined(__AVR_ATmega8535__) \
00414 || defined(__AVR_ATmega88__) \
00415 || defined(__AVR_ATmega88A__) \
00416 || defined(__AVR_ATmega88P__) \
00417 || defined(__AVR_ATmega88PA__) \
00418 || defined(__AVR_ATmega8HVA__) \
00419 || defined(__AVR_ATmega8U2__)
00420 
00421 
00422     #define SLEEP_MODE_IDLE         (0)
00423     #define SLEEP_MODE_ADC          _BV(SM0)
00424     #define SLEEP_MODE_PWR_DOWN     _BV(SM1)
00425     #define SLEEP_MODE_PWR_SAVE     (_BV(SM0) | _BV(SM1))
00426     #define SLEEP_MODE_STANDBY      (_BV(SM1) | _BV(SM2))
00427     #define SLEEP_MODE_EXT_STANDBY  (_BV(SM0) | _BV(SM1) | _BV(SM2))
00428 
00429 
00430     #define set_sleep_mode(mode) \
00431     do { \
00432         _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
00433     } while(0)
00434 
00435 #elif defined(__AVR_ATxmega16A4__) \
00436 || defined(__AVR_ATxmega16D4__) \
00437 || defined(__AVR_ATxmega32A4__) \
00438 || defined(__AVR_ATxmega32D4__) \
00439 || defined(__AVR_ATxmega32X1__) \
00440 || defined(__AVR_ATxmega64A1__) \
00441 || defined(__AVR_ATxmega64A1U__) \
00442 || defined(__AVR_ATxmega64A3__) \
00443 || defined(__AVR_ATxmega64D3__) \
00444 || defined(__AVR_ATxmega128A1__) \
00445 || defined(__AVR_ATxmega128A1U__) \
00446 || defined(__AVR_ATxmega128A3__) \
00447 || defined(__AVR_ATxmega128B1__) \
00448 || defined(__AVR_ATxmega128D3__) \
00449 || defined(__AVR_ATxmega192A3__) \
00450 || defined(__AVR_ATxmega192D3__) \
00451 || defined(__AVR_ATxmega256A3__) \
00452 || defined(__AVR_ATxmega256D3__) \
00453 || defined(__AVR_ATxmega256A3B__) \
00454 || defined(__AVR_ATxmega256A3BU__)
00455 
00456     #define SLEEP_MODE_IDLE         (0)
00457     #define SLEEP_MODE_PWR_DOWN     (SLEEP_SMODE1_bm)
00458     #define SLEEP_MODE_PWR_SAVE     (SLEEP_SMODE1_bm | SLEEP_SMODE0_bm)
00459     #define SLEEP_MODE_STANDBY      (SLEEP_SMODE2_bm | SLEEP_SMODE1_bm)
00460     #define SLEEP_MODE_EXT_STANDBY  (SLEEP_SMODE2_bm | SLEEP_SMODE1_bm | SLEEP_SMODE0_bm)
00461 
00462     #define set_sleep_mode(mode) \
00463     do { \
00464         _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(SLEEP_SMODE2_bm | SLEEP_SMODE1_bm | SLEEP_SMODE0_bm)) | (mode)); \
00465     } while(0)
00466 
00467 #elif defined(__AVR_AT90SCR100__)
00468 
00469     #define SLEEP_MODE_IDLE         (0)
00470     #define SLEEP_MODE_PWR_DOWN     _BV(SM1)
00471     #define SLEEP_MODE_PWR_SAVE     (_BV(SM0) | _BV(SM1))
00472     #define SLEEP_MODE_STANDBY      (_BV(SM1) | _BV(SM2))
00473     #define SLEEP_MODE_EXT_STANDBY  (_BV(SM0) | _BV(SM1) | _BV(SM2))
00474 
00475     #define set_sleep_mode(mode) \
00476     do { \
00477         _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
00478     } while(0)
00479 
00480 #elif defined(__AVR_ATA6289__)
00481 
00482     #define SLEEP_MODE_IDLE                     (0)
00483     #define SLEEP_MODE_SENSOR_NOISE_REDUCTION   (_BV(SM0))
00484     #define SLEEP_MODE_PWR_DOWN                 (_BV(SM1))
00485 
00486     #define set_sleep_mode(mode) \
00487     do { \
00488         _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
00489     } while(0)
00490 
00491 #elif defined(__AVR_ATtiny4__) \
00492 || defined(__AVR_ATtiny5__) \
00493 || defined(__AVR_ATtiny9__) \
00494 || defined(__AVR_ATtiny10__) \
00495 || defined(__AVR_ATtiny20__) \
00496 || defined(__AVR_ATtiny40__)
00497 
00498     #define SLEEP_MODE_IDLE         0
00499     #define SLEEP_MODE_ADC          _BV(SM0)
00500     #define SLEEP_MODE_PWR_DOWN     _BV(SM1)
00501     #define SLEEP_MODE_STANDBY      _BV(SM2)
00502 
00503     #define set_sleep_mode(mode) \
00504     do { \
00505         _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
00506     } while(0)
00507 
00508 #else
00509 
00510     #error "No SLEEP mode defined for this device."
00511 
00512 #endif
00513 
00514 
00515 
00516 /** \ingroup avr_sleep
00517 
00518     Put the device in sleep mode. How the device is brought out of sleep mode
00519     depends on the specific mode selected with the set_sleep_mode() function.
00520     See the data sheet for your device for more details. */
00521 
00522 
00523 #if defined(__DOXYGEN__)
00524 
00525 /** \ingroup avr_sleep
00526 
00527     Set the SE (sleep enable) bit.
00528 */
00529 extern void sleep_enable (void);
00530 
00531 #else
00532 
00533 #define sleep_enable()             \
00534 do {                               \
00535   _SLEEP_CONTROL_REG |= (uint8_t)_SLEEP_ENABLE_MASK;   \
00536 } while(0)
00537 
00538 #endif
00539 
00540 
00541 #if defined(__DOXYGEN__)
00542 
00543 /** \ingroup avr_sleep
00544 
00545     Clear the SE (sleep enable) bit.
00546 */
00547 extern void sleep_disable (void);
00548 
00549 #else
00550 
00551 #define sleep_disable()            \
00552 do {                               \
00553   _SLEEP_CONTROL_REG &= (uint8_t)(~_SLEEP_ENABLE_MASK);  \
00554 } while(0)
00555 
00556 #endif
00557 
00558 
00559 /** \ingroup avr_sleep
00560 
00561     Put the device into sleep mode.  The SE bit must be set
00562     beforehand, and it is recommended to clear it afterwards.
00563 */
00564 #if defined(__DOXYGEN__)
00565 
00566 extern void sleep_cpu (void);
00567 
00568 #else
00569 
00570 #define sleep_cpu()                              \
00571 do {                                             \
00572   __asm__ __volatile__ ( "sleep" "\n\t" :: );    \
00573 } while(0)
00574 
00575 #endif
00576 
00577 
00578 #if defined(__DOXYGEN__)
00579 
00580 extern void sleep_mode (void);
00581 
00582 #else
00583 
00584 #define sleep_mode() \
00585 do {                 \
00586     sleep_enable();  \
00587     sleep_cpu();     \
00588     sleep_disable(); \
00589 } while (0)
00590 
00591 #endif
00592 
00593 
00594 #if defined(__DOXYGEN__)
00595 
00596 extern void sleep_bod_disable (void);
00597 
00598 #else
00599 
00600 #if defined(BODS) && defined(BODSE)
00601 
00602 #define sleep_bod_disable() \
00603 do { \
00604   uint8_t tempreg; \
00605   __asm__ __volatile__("in %[tempreg], %[mcucr]" "\n\t" \
00606                        "ori %[tempreg], %[bods_bodse]" "\n\t" \
00607                        "out %[mcucr], %[tempreg]" "\n\t" \
00608                        "andi %[tempreg], %[not_bodse]" "\n\t" \
00609                        "out %[mcucr], %[tempreg]" \
00610                        : [tempreg] "=&d" (tempreg) \
00611                        : [mcucr] "I" _SFR_IO_ADDR(MCUCR), \
00612                          [bods_bodse] "i" (_BV(BODS) | _BV(BODSE)), \
00613                          [not_bodse] "i" (~_BV(BODSE))); \
00614 } while (0)
00615 
00616 #endif
00617 
00618 #endif
00619 
00620 
00621 /*@}*/
00622 
00623 #endif /* _AVR_SLEEP_H_ */

Automatically generated by Doxygen 1.7.3 on Thu May 19 2011.