このファイルのドキュメントを参照する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
00255 #define SLEEP_MODE_IDLE 0
00256 #define SLEEP_MODE_ADC _BV(SM0)
00257 #define SLEEP_MODE_PWR_DOWN _BV(SM1)
00258 #define SLEEP_MODE_PWR_SAVE (_BV(SM0) | _BV(SM1))
00259
00260 #define set_sleep_mode(mode) \
00261 do { \
00262 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1))) | (mode)); \
00263 } while(0)
00264
00265 #elif defined(__AVR_ATtiny2313__) \
00266 || defined(__AVR_ATtiny2313A__) \
00267 || defined(__AVR_ATtiny4313__)
00268
00269 #define SLEEP_MODE_IDLE 0
00270 #define SLEEP_MODE_PWR_DOWN (_BV(SM0) | _BV(SM1))
00271 #define SLEEP_MODE_STANDBY _BV(SM1)
00272
00273 #define set_sleep_mode(mode) \
00274 do { \
00275 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1))) | (mode)); \
00276 } while(0)
00277
00278 #elif defined(__AVR_AT94K__)
00279
00280 #define SLEEP_MODE_IDLE 0
00281 #define SLEEP_MODE_PWR_DOWN _BV(SM1)
00282 #define SLEEP_MODE_PWR_SAVE (_BV(SM0) | _BV(SM1))
00283
00284 #define set_sleep_mode(mode) \
00285 do { \
00286 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1))) | (mode)); \
00287 } while(0)
00288
00289 #elif defined(__AVR_ATtiny26__) \
00290 || defined(__AVR_ATtiny43U__)
00291
00292 #define SLEEP_MODE_IDLE 0
00293 #define SLEEP_MODE_ADC _BV(SM0)
00294 #define SLEEP_MODE_PWR_DOWN _BV(SM1)
00295 #define SLEEP_MODE_STANDBY (_BV(SM0) | _BV(SM1))
00296
00297 #define set_sleep_mode(mode) \
00298 do { \
00299 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1))) | (mode)); \
00300 } while(0)
00301
00302 #elif defined(__AVR_AT90PWM216__) \
00303 || defined(__AVR_AT90PWM316__) \
00304 || defined(__AVR_AT90PWM81__)
00305
00306 #define SLEEP_MODE_IDLE 0
00307 #define SLEEP_MODE_ADC _BV(SM0)
00308 #define SLEEP_MODE_PWR_DOWN _BV(SM1)
00309 #define SLEEP_MODE_STANDBY (_BV(SM1) | _BV(SM2))
00310
00311 #define set_sleep_mode(mode) \
00312 do { \
00313 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
00314 } while(0)
00315
00316 #elif defined(__AVR_AT90CAN128__) \
00317 || defined(__AVR_AT90CAN32__) \
00318 || defined(__AVR_AT90CAN64__) \
00319 || defined(__AVR_AT90PWM1__) \
00320 || defined(__AVR_AT90PWM2__) \
00321 || defined(__AVR_AT90PWM2B__) \
00322 || defined(__AVR_AT90PWM3__) \
00323 || defined(__AVR_AT90PWM3B__) \
00324 || defined(__AVR_AT90USB162__) \
00325 || defined(__AVR_AT90USB82__) \
00326 || defined(__AVR_AT90USB1286__) \
00327 || defined(__AVR_AT90USB1287__) \
00328 || defined(__AVR_AT90USB646__) \
00329 || defined(__AVR_AT90USB647__) \
00330 || defined(__AVR_ATmega128__) \
00331 || defined(__AVR_ATmega1280__) \
00332 || defined(__AVR_ATmega1281__) \
00333 || defined(__AVR_ATmega1284P__) \
00334 || defined(__AVR_ATmega128RFA1__) \
00335 || defined(__AVR_ATmega16__) \
00336 || defined(__AVR_ATmega16A__) \
00337 || defined(__AVR_ATmega162__) \
00338 || defined(__AVR_ATmega164A__) \
00339 || defined(__AVR_ATmega164P__) \
00340 || defined(__AVR_ATmega165__) \
00341 || defined(__AVR_ATmega165A__) \
00342 || defined(__AVR_ATmega165P__) \
00343 || defined(__AVR_ATmega168__) \
00344 || defined(__AVR_ATmega168A__) \
00345 || defined(__AVR_ATmega168P__) \
00346 || defined(__AVR_ATmega169__) \
00347 || defined(__AVR_ATmega169A__) \
00348 || defined(__AVR_ATmega169P__) \
00349 || defined(__AVR_ATmega169PA__) \
00350 || defined(__AVR_ATmega16HVA__) \
00351 || defined(__AVR_ATmega16HVA2__) \
00352 || defined(__AVR_ATmega16M1__) \
00353 || defined(__AVR_ATmega16U2__) \
00354 || defined(__AVR_ATmega16U4__) \
00355 || defined(__AVR_ATmega2560__) \
00356 || defined(__AVR_ATmega2561__) \
00357 || defined(__AVR_ATmega32__) \
00358 || defined(__AVR_ATmega323__) \
00359 || defined(__AVR_ATmega324A__) \
00360 || defined(__AVR_ATmega324P__) \
00361 || defined(__AVR_ATmega324PA__) \
00362 || defined(__AVR_ATmega325__) \
00363 || defined(__AVR_ATmega325A__) \
00364 || defined(__AVR_ATmega3250__) \
00365 || defined(__AVR_ATmega3250A__) \
00366 || defined(__AVR_ATmega328__) \
00367 || defined(__AVR_ATmega328P__) \
00368 || defined(__AVR_ATmega329__) \
00369 || defined(__AVR_ATmega329A__) \
00370 || defined(__AVR_ATmega329P__) \
00371 || defined(__AVR_ATmega329PA__) \
00372 || defined(__AVR_ATmega3290__) \
00373 || defined(__AVR_ATmega3290A__) \
00374 || defined(__AVR_ATmega3290P__) \
00375 || defined(__AVR_ATmega32C1__) \
00376 || defined(__AVR_ATmega32M1__) \
00377 || defined(__AVR_ATmega32U2__) \
00378 || defined(__AVR_ATmega32U4__) \
00379 || defined(__AVR_ATmega32U6__) \
00380 || defined(__AVR_ATmega406__) \
00381 || defined(__AVR_ATmega48__) \
00382 || defined(__AVR_ATmega48A__) \
00383 || defined(__AVR_ATmega48P__) \
00384 || defined(__AVR_ATmega64__) \
00385 || defined(__AVR_ATmega640__) \
00386 || defined(__AVR_ATmega644__) \
00387 || defined(__AVR_ATmega644A__) \
00388 || defined(__AVR_ATmega644P__) \
00389 || defined(__AVR_ATmega644PA__) \
00390 || defined(__AVR_ATmega645__) \
00391 || defined(__AVR_ATmega645A__) \
00392 || defined(__AVR_ATmega645P__) \
00393 || defined(__AVR_ATmega6450__) \
00394 || defined(__AVR_ATmega6450A__) \
00395 || defined(__AVR_ATmega6450P__) \
00396 || defined(__AVR_ATmega649__) \
00397 || defined(__AVR_ATmega649A__) \
00398 || defined(__AVR_ATmega6490__) \
00399 || defined(__AVR_ATmega6490A__) \
00400 || defined(__AVR_ATmega6490P__) \
00401 || defined(__AVR_ATmega649P__) \
00402 || defined(__AVR_ATmega64C1__) \
00403 || defined(__AVR_ATmega64HVE__) \
00404 || defined(__AVR_ATmega64M1__) \
00405 || defined(__AVR_ATmega8__) \
00406 || defined(__AVR_ATmega8515__) \
00407 || defined(__AVR_ATmega8535__) \
00408 || defined(__AVR_ATmega88__) \
00409 || defined(__AVR_ATmega88A__) \
00410 || defined(__AVR_ATmega88P__) \
00411 || defined(__AVR_ATmega88PA__) \
00412 || defined(__AVR_ATmega8HVA__) \
00413 || defined(__AVR_ATmega8U2__)
00414
00415
00416 #define SLEEP_MODE_IDLE (0)
00417 #define SLEEP_MODE_ADC _BV(SM0)
00418 #define SLEEP_MODE_PWR_DOWN _BV(SM1)
00419 #define SLEEP_MODE_PWR_SAVE (_BV(SM0) | _BV(SM1))
00420 #define SLEEP_MODE_STANDBY (_BV(SM1) | _BV(SM2))
00421 #define SLEEP_MODE_EXT_STANDBY (_BV(SM0) | _BV(SM1) | _BV(SM2))
00422
00423
00424 #define set_sleep_mode(mode) \
00425 do { \
00426 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
00427 } while(0)
00428
00429 #elif defined(__AVR_ATxmega16A4__) \
00430 || defined(__AVR_ATxmega16D4__) \
00431 || defined(__AVR_ATxmega32A4__) \
00432 || defined(__AVR_ATxmega32D4__) \
00433 || defined(__AVR_ATxmega64A1__) \
00434 || defined(__AVR_ATxmega64A1U__) \
00435 || defined(__AVR_ATxmega64A3__) \
00436 || defined(__AVR_ATxmega64D3__) \
00437 || defined(__AVR_ATxmega128A1__) \
00438 || defined(__AVR_ATxmega128A1U__) \
00439 || defined(__AVR_ATxmega128A3__) \
00440 || defined(__AVR_ATxmega128D3__) \
00441 || defined(__AVR_ATxmega192A3__) \
00442 || defined(__AVR_ATxmega192D3__) \
00443 || defined(__AVR_ATxmega256A3__) \
00444 || defined(__AVR_ATxmega256D3__) \
00445 || defined(__AVR_ATxmega256A3B__)
00446
00447 #define SLEEP_MODE_IDLE (0)
00448 #define SLEEP_MODE_PWR_DOWN (SLEEP_SMODE1_bm)
00449 #define SLEEP_MODE_PWR_SAVE (SLEEP_SMODE1_bm | SLEEP_SMODE0_bm)
00450 #define SLEEP_MODE_STANDBY (SLEEP_SMODE2_bm | SLEEP_SMODE1_bm)
00451 #define SLEEP_MODE_EXT_STANDBY (SLEEP_SMODE2_bm | SLEEP_SMODE1_bm | SLEEP_SMODE0_bm)
00452
00453 #define set_sleep_mode(mode) \
00454 do { \
00455 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(SLEEP_SMODE2_bm | SLEEP_SMODE1_bm | SLEEP_SMODE0_bm)) | (mode)); \
00456 } while(0)
00457
00458 #elif defined(__AVR_AT90SCR100__)
00459
00460 #define SLEEP_MODE_IDLE (0)
00461 #define SLEEP_MODE_PWR_DOWN _BV(SM1)
00462 #define SLEEP_MODE_PWR_SAVE (_BV(SM0) | _BV(SM1))
00463 #define SLEEP_MODE_STANDBY (_BV(SM1) | _BV(SM2))
00464 #define SLEEP_MODE_EXT_STANDBY (_BV(SM0) | _BV(SM1) | _BV(SM2))
00465
00466 #define set_sleep_mode(mode) \
00467 do { \
00468 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
00469 } while(0)
00470
00471 #elif defined(__AVR_ATA6289__)
00472
00473 #define SLEEP_MODE_IDLE (0)
00474 #define SLEEP_MODE_SENSOR_NOISE_REDUCTION (_BV(SM0))
00475 #define SLEEP_MODE_PWR_DOWN (_BV(SM1))
00476
00477 #define set_sleep_mode(mode) \
00478 do { \
00479 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
00480 } while(0)
00481
00482 #elif defined(__AVR_ATtiny4__) \
00483 || defined(__AVR_ATtiny5__) \
00484 || defined(__AVR_ATtiny9__) \
00485 || defined(__AVR_ATtiny10__) \
00486 || defined(__AVR_ATtiny20__) \
00487 || defined(__AVR_ATtiny40__)
00488
00489 #define SLEEP_MODE_IDLE 0
00490 #define SLEEP_MODE_ADC _BV(SM0)
00491 #define SLEEP_MODE_PWR_DOWN _BV(SM1)
00492 #define SLEEP_MODE_STANDBY _BV(SM2)
00493
00494 #define set_sleep_mode(mode) \
00495 do { \
00496 _SLEEP_CONTROL_REG = ((_SLEEP_CONTROL_REG & ~(_BV(SM0) | _BV(SM1) | _BV(SM2))) | (mode)); \
00497 } while(0)
00498
00499 #else
00500
00501 #error "No SLEEP mode defined for this device."
00502
00503 #endif
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514 #if defined(__DOXYGEN__)
00515
00516
00517
00518
00519
00520 extern void sleep_enable (void);
00521
00522 #else
00523
00524 #define sleep_enable() \
00525 do { \
00526 _SLEEP_CONTROL_REG |= (uint8_t)_SLEEP_ENABLE_MASK; \
00527 } while(0)
00528
00529 #endif
00530
00531
00532 #if defined(__DOXYGEN__)
00533
00534
00535
00536
00537
00538 extern void sleep_disable (void);
00539
00540 #else
00541
00542 #define sleep_disable() \
00543 do { \
00544 _SLEEP_CONTROL_REG &= (uint8_t)(~_SLEEP_ENABLE_MASK); \
00545 } while(0)
00546
00547 #endif
00548
00549
00550
00551
00552
00553
00554
00555 #if defined(__DOXYGEN__)
00556
00557 extern void sleep_cpu (void);
00558
00559 #else
00560
00561 #define sleep_cpu() \
00562 do { \
00563 __asm__ __volatile__ ( "sleep" "\n\t" :: ); \
00564 } while(0)
00565
00566 #endif
00567
00568
00569 #if defined(__DOXYGEN__)
00570
00571 extern void sleep_mode (void);
00572
00573 #else
00574
00575 #define sleep_mode() \
00576 do { \
00577 sleep_enable(); \
00578 sleep_cpu(); \
00579 sleep_disable(); \
00580 } while (0)
00581
00582 #endif
00583
00584
00585 #if defined(__DOXYGEN__)
00586
00587 extern void sleep_bod_disable (void);
00588
00589 #else
00590
00591 #if defined(BODS) && defined(BODSE)
00592
00593 #define sleep_bod_disable() \
00594 do { \
00595 uint8_t tempreg; \
00596 __asm__ __volatile__("in %[tempreg], %[mcucr]" "\n\t" \
00597 "ori %[tempreg], %[bods_bodse]" "\n\t" \
00598 "out %[mcucr], %[tempreg]" "\n\t" \
00599 "andi %[tempreg], %[not_bodse]" "\n\t" \
00600 "out %[mcucr], %[tempreg]" \
00601 : [tempreg] "=&d" (tempreg) \
00602 : [mcucr] "I" _SFR_IO_ADDR(MCUCR), \
00603 [bods_bodse] "i" (_BV(BODS) | _BV(BODSE)), \
00604 [not_bodse] "i" (~_BV(BODSE))); \
00605 } while (0)
00606
00607 #endif
00608
00609 #endif
00610
00611
00612
00613
00614 #endif