mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-29 05:21:37 +03:00
EEPROM: fix incorrect start address, support multiple instances
related to #279, #240
This commit is contained in:
@ -28,95 +28,91 @@
|
||||
#include "os_type.h"
|
||||
#include "osapi.h"
|
||||
#include "spi_flash.h"
|
||||
extern uint32_t _SPIFFS_end;
|
||||
}
|
||||
|
||||
#define CONFIG_START_SECTOR (((uint32_t)_SPIFFS_end - 0x40200000) / 4096)
|
||||
#define CONFIG_SECTOR (CONFIG_START_SECTOR + 0)
|
||||
#define CONFIG_ADDR (SPI_FLASH_SEC_SIZE * CONFIG_SECTOR)
|
||||
|
||||
EEPROMClass::EEPROMClass()
|
||||
: _data(0), _size(0), _dirty(false)
|
||||
EEPROMClass::EEPROMClass(uint32_t sector)
|
||||
: _sector(sector)
|
||||
, _data(0)
|
||||
, _size(0)
|
||||
, _dirty(false)
|
||||
{
|
||||
}
|
||||
|
||||
void EEPROMClass::begin(size_t size)
|
||||
{
|
||||
if (size <= 0)
|
||||
return;
|
||||
if (size > SPI_FLASH_SEC_SIZE)
|
||||
size = SPI_FLASH_SEC_SIZE;
|
||||
void EEPROMClass::begin(size_t size) {
|
||||
if (size <= 0)
|
||||
return;
|
||||
if (size > SPI_FLASH_SEC_SIZE)
|
||||
size = SPI_FLASH_SEC_SIZE;
|
||||
|
||||
_data = new uint8_t[size];
|
||||
_size = size;
|
||||
if (_data) {
|
||||
delete[] _data;
|
||||
}
|
||||
|
||||
noInterrupts();
|
||||
spi_flash_read(CONFIG_ADDR, reinterpret_cast<uint32_t*>(_data), _size);
|
||||
interrupts();
|
||||
_data = new uint8_t[size];
|
||||
_size = size;
|
||||
|
||||
noInterrupts();
|
||||
spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
|
||||
interrupts();
|
||||
}
|
||||
|
||||
void EEPROMClass::end()
|
||||
{
|
||||
if (!_size)
|
||||
return;
|
||||
void EEPROMClass::end() {
|
||||
if (!_size)
|
||||
return;
|
||||
|
||||
commit();
|
||||
if(_data) {
|
||||
delete[] _data;
|
||||
commit();
|
||||
if(_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::read(int address)
|
||||
{
|
||||
if (address < 0 || (size_t)address >= _size)
|
||||
return 0;
|
||||
if(!_data)
|
||||
return 0;
|
||||
|
||||
return _data[address];
|
||||
uint8_t * EEPROMClass::getDataPtr() {
|
||||
_dirty = true;
|
||||
return &_data[0];
|
||||
}
|
||||
|
||||
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(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;
|
||||
extern "C" uint32_t _SPIFFS_end;
|
||||
EEPROMClass EEPROM((((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE));
|
||||
|
Reference in New Issue
Block a user