Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 #if defined(SLEEP_CTRL)
00139
00140
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
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
00517
00518
00519
00520
00521
00522
00523 #if defined(__DOXYGEN__)
00524
00525
00526
00527
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
00544
00545
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
00560
00561
00562
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