avr/cpp/Sleeping.h

Go to the documentation of this file.
00001 /**********************************************************************************************************************\
00002 
00003         C++ library for Atmel AVR microcontrollers
00004         Copyright (C) 2007 Lauri Kirikal, Mikk Leini, MT� TT� Robotiklubi
00005 
00006         This program is free software; you can redistribute it and/or
00007         modify it under the terms of the GNU General Public License
00008         as published by the Free Software Foundation; either version 2
00009         of the License, or (at your option) any later version.
00010 
00011         This program is distributed in the hope that it will be useful,
00012         but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License
00017         along with this program; if not, write to the Free Software
00018         Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 
00020         See http://creativecommons.org/licenses/GPL/2.0/
00021 
00022         MT� TT� Robotiklubi  http://www.robotiklubi.ee robotiklubi@gmail.com
00023         Lauri Kirikal        laurikirikal@gmail.com
00024         Mikk Leini           mikk.leini@gmail.com
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 // __SLEEPING_REGISTER__ is not defined here because bits are scattered into various registers.
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                 }; // EnabledFlag
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                 }; // enum SleepMode
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                 }; // enum SleepMode
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                 }; // enum SleepMode
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                 } // SetUp
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                 } // SetUp
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                 } // GetSleepMode
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                 } // SetSleepMode
00169 
00170 #endif // ifdef __SLEEPING_REGISTER__
00171 
00172                 
00173         } // namespace Sleeping
00174 
00175 } // namespace AVRCpp
00176 
00177 
00178 #endif // if defined(__SLEEPING_NUMBER_OF_MODES__)
00179 
00180 #endif // ifndef __AVR_CPP_SLEEPING_H__

Generated on Sat Sep 15 23:41:21 2007 for AVR C++ Lib for ATmega644P by  doxygen 1.5.2
SourceForge.net Logo MTÜ TTÜ Robotiklubi