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 #ifndef __AVR_CPP_SLEEPING_H__
00028 #define __AVR_CPP_SLEEPING_H__
00029
00030
00031 #if defined(__AVR_ATmega48__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
00032
00033 #define __SLEEPING_NUMBER_OF_MODES__ 5
00034 #define __SLEEPING_REGISTER__ _SMCR
00035
00036 #elif defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega644__) \
00037 || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega164P__)
00038
00039 #define __SLEEPING_NUMBER_OF_MODES__ 6
00040 #define __SLEEPING_REGISTER__ _SMCR
00041
00042 #elif defined(__AVR_ATmega8__)
00043
00044 #define __SLEEPING_NUMBER_OF_MODES__ 5
00045 #define __SLEEPING_REGISTER__ _MCUCR
00046
00047 #elif defined(__AVR_ATmega128__) || defined(__AVR_ATmega64__)
00048
00049 #define __SLEEPING_NUMBER_OF_MODES__ 6
00050 #define __SLEEPING_REGISTER__ _MCUCR
00051
00052 #elif defined(__AVR_ATmega8515__)
00053
00054 #define __SLEEPING_NUMBER_OF_MODES__ 3
00055
00056
00057 #else
00058
00059 #error "Device is not selected or selected device is not supported."
00060
00061 #endif
00062
00063
00064 #if defined(__SLEEPING_NUMBER_OF_MODES__)
00065
00066 #include "IO.h"
00067 #include "Assembler.h"
00068
00069 namespace AVRCpp
00070 {
00071 namespace Sleeping
00072 {
00073 enum EnabledFlag
00074 {
00075 Disabled = 0x00,
00076 Enabled = _SE
00077
00078 };
00079
00080
00081 #if __SLEEPING_NUMBER_OF_MODES__ == 6
00082
00083 enum SleepMode
00084 {
00085 Idle = 0x00,
00086 ADCNoiseReduction = _SM0,
00087 PowerDown = _SM1,
00088 PowerSave = _SM1 | _SM0,
00089 Standby = _SM2 | _SM1,
00090 ExtendedStandby = _SM2 | _SM1 | _SM0
00091
00092 };
00093
00094 #elif __SLEEPING_NUMBER_OF_MODES__ == 5
00095
00096 enum SleepMode
00097 {
00098 Idle = 0x00,
00099 ADCNoiseReduction = _SM0,
00100 PowerDown = _SM1,
00101 PowerSave = _SM1 | _SM0,
00102 Standby = _SM2 | _SM1
00103
00104 };
00105
00106 #elif __SLEEPING_NUMBER_OF_MODES__ == 3
00107
00108 enum SleepMode
00109 {
00110 Idle = 0x00,
00111 PowerDown = _SM1,
00112 Standby = _SM2 | _SM1
00113
00114 };
00115
00116 #endif // if __SLEEPING_NUMBER_OF_MODES__ == ?
00117
00118
00119 #ifdef __SLEEPING_REGISTER__
00120
00121 inline void SetUp(SleepMode sleepMode, EnabledFlag enabled)
00122 {
00123 ChangeBits<__SLEEPING_REGISTER__>(_SM0 | _SM1 | _SM2 | _SE, sleepMode | enabled);
00124
00125 }
00126
00127 inline void FallAsleep() { Assembler::SLEEP(); }
00128
00129 inline void Enable() { SetBits<__SLEEPING_REGISTER__>(_SE); }
00130 inline void Disable() { ClearBits<__SLEEPING_REGISTER__>(_SE); }
00131 inline uint8_t IsEnabled() { return IsBitsSet<__SLEEPING_REGISTER__>(_SE); }
00132
00133 inline uint8_t GetSleepMode() { return SelectBits<__SLEEPING_REGISTER__>(_SM0 | _SM1 | _SM2); }
00134 inline void SetSleepMode(SleepMode sleepMode) { ChangeBits<__SLEEPING_REGISTER__>(_SM0 | _SM1 | _SM2, sleepMode); }
00135
00136 #undef __SLEEPING_REGISTER__
00137
00138 #else // __SLEEPING_REGISTER__ not defined
00139
00140 inline void SetUp(SleepMode sleepMode, EnabledFlag enabled)
00141 {
00142 ChangeBits<_MCUCR>(_SM1 | _SE, sleepMode | enabled);
00143 ChangeBits<_MCUCSR>(_SM2, sleepMode);
00144 ChangeBits<_EMCUCR>(_SM0, sleepMode);
00145
00146 }
00147
00148 inline void FallAsleep() { Assembler::SLEEP(); }
00149
00150 inline void Enable() { SetBits<_MCUCR>(_SE); }
00151 inline void Disable() { ClearBits<_MCUCR>(_SE); }
00152 inline uint8_t IsEnabled() { return IsBitsSet<_MCUCR>(_SE); }
00153
00154 inline uint8_t GetSleepMode()
00155 {
00156 return ( SelectBits<_MCUCR>(_SM1)
00157 | SelectBits<_MCUCSR>(_SM2)
00158 | SelectBits<_EMCUCR>(_SM0) );
00159
00160 }
00161
00162 inline void SetSleepMode(SleepMode sleepMode)
00163 {
00164 ChangeBits<_MCUCR>(_SM1, sleepMode);
00165 ChangeBits<_MCUCSR>(_SM2, sleepMode);
00166 ChangeBits<_EMCUCR>(_SM0, sleepMode);
00167
00168 }
00169
00170 #endif // ifdef __SLEEPING_REGISTER__
00171
00172
00173 }
00174
00175 }
00176
00177
00178 #endif // if defined(__SLEEPING_NUMBER_OF_MODES__)
00179
00180 #endif // ifndef __AVR_CPP_SLEEPING_H__