1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

EEPROM: fix incorrect start address, support multiple instances

related to #279, #240
This commit is contained in:
Ivan Grokhotkov 2015-05-21 10:22:45 +03:00
parent 6e14666d2f
commit b1f2fdb9cc
3 changed files with 104 additions and 109 deletions

View File

@ -28,95 +28,91 @@
#include "os_type.h" #include "os_type.h"
#include "osapi.h" #include "osapi.h"
#include "spi_flash.h" #include "spi_flash.h"
extern uint32_t _SPIFFS_end;
} }
#define CONFIG_START_SECTOR (((uint32_t)_SPIFFS_end - 0x40200000) / 4096) EEPROMClass::EEPROMClass(uint32_t sector)
#define CONFIG_SECTOR (CONFIG_START_SECTOR + 0) : _sector(sector)
#define CONFIG_ADDR (SPI_FLASH_SEC_SIZE * CONFIG_SECTOR) , _data(0)
, _size(0)
EEPROMClass::EEPROMClass() , _dirty(false)
: _data(0), _size(0), _dirty(false)
{ {
} }
void EEPROMClass::begin(size_t size) void EEPROMClass::begin(size_t size) {
{ if (size <= 0)
if (size <= 0) return;
return; if (size > SPI_FLASH_SEC_SIZE)
if (size > SPI_FLASH_SEC_SIZE) size = SPI_FLASH_SEC_SIZE;
size = SPI_FLASH_SEC_SIZE;
_data = new uint8_t[size]; if (_data) {
_size = size; delete[] _data;
}
noInterrupts(); _data = new uint8_t[size];
spi_flash_read(CONFIG_ADDR, reinterpret_cast<uint32_t*>(_data), _size); _size = size;
interrupts();
noInterrupts();
spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
interrupts();
} }
void EEPROMClass::end() void EEPROMClass::end() {
{ if (!_size)
if (!_size) return;
return;
commit(); commit();
if(_data) { if(_data) {
delete[] _data; delete[] _data;
}
_data = 0;
_size = 0;
}
uint8_t EEPROMClass::read(int address) {
if (address < 0 || (size_t)address >= _size)
return 0;
if(!_data)
return 0;
return _data[address];
}
void EEPROMClass::write(int address, uint8_t value) {
if (address < 0 || (size_t)address >= _size)
return;
if(!_data)
return;
_data[address] = value;
_dirty = true;
}
bool EEPROMClass::commit() {
bool ret = false;
if (!_size)
return false;
if(!_dirty)
return true;
if(!_data)
return false;
noInterrupts();
if(spi_flash_erase_sector(_sector) == SPI_FLASH_RESULT_OK) {
if(spi_flash_write(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size) == SPI_FLASH_RESULT_OK) {
_dirty = false;
ret = true;
} }
_data = 0; }
_size = 0; interrupts();
return ret;
} }
uint8_t * EEPROMClass::getDataPtr() {
uint8_t EEPROMClass::read(int address) _dirty = true;
{ return &_data[0];
if (address < 0 || (size_t)address >= _size)
return 0;
if(!_data)
return 0;
return _data[address];
} }
void EEPROMClass::write(int address, uint8_t value) extern "C" uint32_t _SPIFFS_end;
{ EEPROMClass EEPROM((((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE));
if (address < 0 || (size_t)address >= _size)
return;
if(!_data)
return;
_data[address] = value;
_dirty = true;
}
bool EEPROMClass::commit()
{
bool ret = false;
if (!_size)
return false;
if(!_dirty)
return true;
if(!_data)
return false;
noInterrupts();
if(spi_flash_erase_sector(CONFIG_SECTOR) == SPI_FLASH_RESULT_OK) {
if(spi_flash_write(CONFIG_ADDR, reinterpret_cast<uint32_t*>(_data), _size) == SPI_FLASH_RESULT_OK) {
_dirty = false;
ret = true;
}
}
interrupts();
return ret;
}
uint8_t * EEPROMClass::getDataPtr()
{
_dirty = true;
return &_data[0];
}
EEPROMClass EEPROM;

View File

@ -26,43 +26,42 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
class EEPROMClass class EEPROMClass {
{ public:
public: EEPROMClass(uint32_t sector);
EEPROMClass();
void begin(size_t size);
uint8_t read(int address);
void write(int address, uint8_t val);
bool commit();
void end();
uint8_t * getDataPtr(); void begin(size_t size);
uint8_t read(int address);
void write(int address, uint8_t val);
bool commit();
void end();
template<typename T> T &get(int address, T &t) uint8_t * getDataPtr();
{
if (address < 0 || address + sizeof(T) > _size)
return t;
uint8_t *ptr = (uint8_t*) &t; template<typename T>
memcpy(ptr, _data + address, sizeof(T)); T &get(int address, T &t) {
return t; if (address < 0 || address + sizeof(T) > _size)
} return t;
template<typename T> const T &put(int address, const T &t) memcpy((uint8_t*) &t, _data + address, sizeof(T));
{ return t;
if (address < 0 || address + sizeof(T) > _size) }
return t;
const uint8_t *ptr = (const uint8_t*) &t; template<typename T>
memcpy(_data + address, ptr, sizeof(T)); const T &put(int address, const T &t) {
_dirty = true; if (address < 0 || address + sizeof(T) > _size)
return t; return t;
}
protected: memcpy(_data + address, (const uint8_t*) &t, sizeof(T));
uint8_t* _data; _dirty = true;
size_t _size; return t;
bool _dirty; }
protected:
uint32_t _sector;
uint8_t* _data;
size_t _size;
bool _dirty;
}; };
extern EEPROMClass EEPROM; extern EEPROMClass EEPROM;

View File

@ -22,7 +22,7 @@ void loop()
// need to divide by 4 because analog inputs range from // need to divide by 4 because analog inputs range from
// 0 to 1023 and each byte of the EEPROM can only hold a // 0 to 1023 and each byte of the EEPROM can only hold a
// value from 0 to 255. // value from 0 to 255.
int val = analogRead(0) / 4; int val = analogRead(A0) / 4;
// write the value to the appropriate byte of the EEPROM. // write the value to the appropriate byte of the EEPROM.
// these values will remain there when the board is // these values will remain there when the board is