avr/cpp/EEPROM.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 
00028 
00029 #ifndef __AVR_CPP_EEPROM_H__
00030 #define __AVR_CPP_EEPROM_H__
00031 
00032 #define __EEPROM_SIMPLE__               0x00
00033 #define __EEPROM_ERASEBLE__             0x01
00034 #define __EEPROM_NEW_NAMES__    0x02
00035 #define __EEPROM_OLD_FLASH__    0x04
00036 #define __EEPROM_LONG_FLASH__   0x08
00037 
00038 #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega8515__)
00039 
00040 #define __EEPROM_PROPERTIES__   (__EEPROM_SIMPLE__ | __EEPROM_OLD_FLASH__)
00041 
00042 #elif defined(__AVR_ATmega128__) || defined(__AVR_ATmega64__) 
00043 
00044 #define __EEPROM_PROPERTIES__   (__EEPROM_SIMPLE__)
00045 
00046 #elif defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega644__) \
00047                 || defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega644P__)
00048 
00049 #define __EEPROM_PROPERTIES__   (__EEPROM_SIMPLE__ | __EEPROM_NEW_NAMES__ | __EEPROM_ERASEBLE__)
00050 
00051 #elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
00052 
00053 #define __EEPROM_PROPERTIES__   (__EEPROM_SIMPLE__ | __EEPROM_NEW_NAMES__ | __EEPROM_ERASEBLE__ | __EEPROM_LONG_FLASH__)
00054 
00055 #else
00056 
00057 #error "Device is not selected or selected device is not supported."
00058 
00059 #endif
00060 
00061 #ifdef __EEPROM_PROPERTIES__
00062 
00063 #include "IO.h"
00064 
00065 #define EEPROM_SIZE E2END
00066 
00067 #define EE_RDY_ns               EEPROM
00068 #define EE_RDY_struct   EE_RDY_ns::ReadyInterrupt
00069 
00070 /**********************************************************************************************************************\
00071         
00072         Universal bit and register name definitions according to device EEPROM properties.
00073 
00074 \**********************************************************************************************************************/
00075 #if __EEPROM_PROPERTIES__ & __EEPROM_NEW_NAMES__
00076 
00077 #define __EEMPE__ _EEMPE
00078 #define __EEPE__ _EEPE
00079 
00080 #else
00081 
00082 #define __EEMPE__ _EEMWE
00083 #define __EEPE__ _EEWE
00084 
00085 #endif // if __EEPROM_PROPERTIES__ & __EEPROM_NEW_NAMES__
00086 
00087 
00088 #if __EEPROM_PROPERTIES__ & __EEPROM_LONG_FLASH__
00089 #define __SPMEN__ _SELFPRGEN
00090 #else
00091 #define __SPMEN__ _SPMEN
00092 #endif
00093 
00094 #if __EEPROM_PROPERTIES__ & __EEPROM_OLD_FLASH__
00095 #define __SPMCSR__      _SPMCR
00096 #else
00097 #define __SPMCSR__      _SPMCSR
00098 #endif
00099 
00100 /**********************************************************************************************************************/
00101 
00102 
00103 /**********************************************************************************************************************\
00104 
00105         EEPROM read/write functionality
00106 
00107 \**********************************************************************************************************************/
00108 
00109 namespace AVRCpp
00110 {
00111         namespace EEPROM
00112         {
00117                 struct ReadyInterrupt : BasicInterrupt<Bits<_EECR, _EERIE> > { __INTERRUPT_HANDLER_SUPPORT__ };
00118                 
00120                 inline bool IsWriting() {       return IsBitsSet<_EECR>(__EEPE__); }
00122                 inline void WaitWhileWriting() { while (IsWriting() ); }
00124                 inline void SetAddressUnsafe(uint16_t address) { EEAR = address; }
00126                 inline void SetAddress(uint16_t address) { WaitWhileWriting(); SetAddressUnsafe(address); }
00128                 inline uint16_t GetAddress() {  return EEAR; }
00130                 void Write(uint8_t data);
00132                 void Write(uint16_t address, uint8_t data);
00134                 bool MoveNext();
00135                 
00137                 inline uint8_t Read()
00138                 {
00139                         WaitWhileWriting();
00140                         
00141                         SetBits<_EECR>(_EERE);
00142                         
00143                         return EEDR;
00144                         
00145                 } // Read 1
00146                 
00148                 inline uint8_t Read(uint16_t address)
00149                 {
00150                         SetAddress(address);
00151                         
00152                         SetBits<_EECR>(_EERE);
00153                         
00154                         return EEDR;
00155                         
00156                 } // Read 2
00157 
00158 #if __EEPROM_PROPERTIES__ & __EEPROM_ERASEBLE__
00159 
00160                 enum Mode
00161                 {
00163                         EraseOnly               = _EEPM0,
00165                         WriteOnly               = _EEPM1,
00167                         EraseAndWrite   = _EEPM0 |_EEPM1
00168 
00169                 }; // enum Mode
00170                 
00172                 inline void SetMode(Mode mode) { ChangeBits<_EECR>(_EEPM0 | _EEPM1, mode); }
00174                 inline uint8_t GetMode() { return SelectBits<_EECR>(_EEPM0 | _EEPM1); }
00176                 bool Erase(uint16_t numberOfBytes);
00177                 
00178 #endif // if __EEPROM_PROPERTIES & __EEPROM_ERASEBLE__
00179 
00180                 
00181                 // Functions from this namespace should not be called by user
00182                 namespace Internal
00183                 {
00184                         
00185                         bool LoadingProcess(uint16_t lastByteAddr, uint8_t *uint8Data);
00186                         bool SavingProcess(uint16_t lastByteAddr, uint8_t *uint8Data);
00187                         
00188                         template<typename T> static inline bool LoadSaveOperation(bool isSaving, T *data, uint16_t elementsCount)
00189                         {
00190                                 // Is there anything to read or write ?
00191                                 if (sizeof(T) == 0 || elementsCount == 0)
00192                                         return true;
00193                                 
00194                                 {
00195                                         uint16_t lastByteAddr = EEAR + sizeof(T) * elementsCount - 1;
00196                                         uint8_t *uint8Data = (uint8_t *)(data);
00197                                         
00198                                         if (isSaving)
00199                                                 return SavingProcess(lastByteAddr, uint8Data);
00200                                         else
00201                                                 return LoadingProcess(lastByteAddr, uint8Data);
00202                                 }
00203                                 
00204                         } // LoadSaveOperation
00205                         
00206                 } // namespace Internal
00207                 
00212                 template <typename T> bool Save(T data) { return Internal::LoadSaveOperation<T>(true, &data, 1); }
00213 
00218                 template <typename T> bool Load(T &data) { return Internal::LoadSaveOperation<T>(false, &data, 1); }
00219                         
00224                 template <typename T> bool SaveArray(T *data, uint16_t elementsCount) { return Internal::LoadSaveOperation<T>(true, data, elementsCount); }
00225 
00230                 template <typename T> bool LoadArray(T *data, uint16_t elementsCount) { return Internal::LoadSaveOperation<T>(false, data, elementsCount); }
00231                 
00232         } // namespace EEPROM
00233         
00234 } // namespace AVRCpp
00235 
00236 /**********************************************************************************************************************/
00237 
00238 #endif // ifdef __EEPROM_PROPERTIES__
00239 
00240 #endif // ifndef __AVR_CPP_EEPROM_H__

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