36#define _AVR_SLEEP_H_ 1
139#if defined(SLEEP_CTRL)
142 #define _SLEEP_CONTROL_REG SLEEP_CTRL
143 #define _SLEEP_ENABLE_MASK SLEEP_SEN_bm
144 #define _SLEEP_SMODE_GROUP_MASK SLEEP_SMODE_gm
146#elif defined(SLPCTRL)
149 #define _SLEEP_CONTROL_REG SLPCTRL_CTRLA
150 #define _SLEEP_ENABLE_MASK SLPCTRL_SEN_bm
151 #define _SLEEP_SMODE_GROUP_MASK SLPCTRL_SMODE_gm
155 #define _SLEEP_CONTROL_REG SMCR
156 #define _SLEEP_ENABLE_MASK _BV(SE)
158#elif defined(__AVR_AT94K__)
160 #define _SLEEP_CONTROL_REG MCUR
161 #define _SLEEP_ENABLE_MASK _BV(SE)
163#elif !defined(__DOXYGEN__)
165 #define _SLEEP_CONTROL_REG MCUCR
166 #define _SLEEP_ENABLE_MASK _BV(SE)
173#if defined(__AVR_ATmega161__)
175 #define set_sleep_mode(mode) \
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)); \
182#elif defined(__AVR_ATmega162__) \
183|| defined(__AVR_ATmega8515__)
185 #define set_sleep_mode(mode) \
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)); \
193#elif defined(__AVR_XMEGA__)
195#define set_sleep_mode(mode) \
197 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_SLEEP_SMODE_GROUP_MASK)) | (mode)); \
204 #define set_sleep_mode(mode) \
206 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
211 #define set_sleep_mode(mode) \
213 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1))) | (mode)); \
218 #define set_sleep_mode(mode) \
220 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~_BV(SM)) | (mode)); \
225 #error "No SLEEP mode defined for this device."
239#if defined(__DOXYGEN__)
249#define sleep_enable() \
251 _SLEEP_CONTROL_REG |= (uint8_t)_SLEEP_ENABLE_MASK; \
257#if defined(__DOXYGEN__)
267#define sleep_disable() \
269 _SLEEP_CONTROL_REG &= (uint8_t)(~_SLEEP_ENABLE_MASK); \
280#if defined(__DOXYGEN__)
288 __asm__ __volatile__ ( "sleep" "\n\t" :: ); \
294#if defined(__DOXYGEN__)
304#define sleep_mode() \
314#if defined(__DOXYGEN__)
325#if defined(BODS) && defined(BODSE)
329#define BOD_CONTROL_REG BODCR
333#define BOD_CONTROL_REG MCUCR
337#define sleep_bod_disable() \
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))); \
void sleep_bod_disable(void)