Skip to content

Commit dd58d72

Browse files
committed
EEPROM: fix incorrect start address, support multiple instances
related to #279, #240
1 parent 921a8c9 commit dd58d72

File tree

3 files changed

+100
-105
lines changed

3 files changed

+100
-105
lines changed

hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.cpp

+63-67
Original file line numberDiff line numberDiff line change
@@ -28,95 +28,91 @@
2828
#include "os_type.h"
2929
#include "osapi.h"
3030
#include "spi_flash.h"
31-
extern uint32_t _SPIFFS_end;
3231
}
3332

34-
#define CONFIG_START_SECTOR (((uint32_t)_SPIFFS_end - 0x40200000) / 4096)
35-
#define CONFIG_SECTOR (CONFIG_START_SECTOR + 0)
36-
#define CONFIG_ADDR (SPI_FLASH_SEC_SIZE * CONFIG_SECTOR)
37-
38-
EEPROMClass::EEPROMClass()
39-
: _data(0), _size(0), _dirty(false)
33+
EEPROMClass::EEPROMClass(uint32_t sector)
34+
: _sector(sector)
35+
, _data(0)
36+
, _size(0)
37+
, _dirty(false)
4038
{
4139
}
4240

43-
void EEPROMClass::begin(size_t size)
44-
{
45-
if (size <= 0)
46-
return;
47-
if (size > SPI_FLASH_SEC_SIZE)
48-
size = SPI_FLASH_SEC_SIZE;
41+
void EEPROMClass::begin(size_t size) {
42+
if (size <= 0)
43+
return;
44+
if (size > SPI_FLASH_SEC_SIZE)
45+
size = SPI_FLASH_SEC_SIZE;
46+
47+
if (_data) {
48+
delete[] _data;
49+
}
4950

50-
_data = new uint8_t[size];
51-
_size = size;
51+
_data = new uint8_t[size];
52+
_size = size;
5253

53-
noInterrupts();
54-
spi_flash_read(CONFIG_ADDR, reinterpret_cast<uint32_t*>(_data), _size);
55-
interrupts();
54+
noInterrupts();
55+
spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
56+
interrupts();
5657
}
5758

58-
void EEPROMClass::end()
59-
{
60-
if (!_size)
61-
return;
59+
void EEPROMClass::end() {
60+
if (!_size)
61+
return;
6262

63-
commit();
64-
if(_data) {
65-
delete[] _data;
66-
}
67-
_data = 0;
68-
_size = 0;
63+
commit();
64+
if(_data) {
65+
delete[] _data;
66+
}
67+
_data = 0;
68+
_size = 0;
6969
}
7070

7171

72-
uint8_t EEPROMClass::read(int address)
73-
{
74-
if (address < 0 || (size_t)address >= _size)
75-
return 0;
76-
if(!_data)
77-
return 0;
72+
uint8_t EEPROMClass::read(int address) {
73+
if (address < 0 || (size_t)address >= _size)
74+
return 0;
75+
if(!_data)
76+
return 0;
7877

79-
return _data[address];
78+
return _data[address];
8079
}
8180

82-
void EEPROMClass::write(int address, uint8_t value)
83-
{
84-
if (address < 0 || (size_t)address >= _size)
85-
return;
86-
if(!_data)
87-
return;
81+
void EEPROMClass::write(int address, uint8_t value) {
82+
if (address < 0 || (size_t)address >= _size)
83+
return;
84+
if(!_data)
85+
return;
8886

89-
_data[address] = value;
90-
_dirty = true;
87+
_data[address] = value;
88+
_dirty = true;
9189
}
9290

93-
bool EEPROMClass::commit()
94-
{
95-
bool ret = false;
96-
if (!_size)
97-
return false;
98-
if(!_dirty)
99-
return true;
100-
if(!_data)
101-
return false;
102-
103-
noInterrupts();
104-
if(spi_flash_erase_sector(CONFIG_SECTOR) == SPI_FLASH_RESULT_OK) {
105-
if(spi_flash_write(CONFIG_ADDR, reinterpret_cast<uint32_t*>(_data), _size) == SPI_FLASH_RESULT_OK) {
106-
_dirty = false;
107-
ret = true;
108-
}
91+
bool EEPROMClass::commit() {
92+
bool ret = false;
93+
if (!_size)
94+
return false;
95+
if(!_dirty)
96+
return true;
97+
if(!_data)
98+
return false;
99+
100+
noInterrupts();
101+
if(spi_flash_erase_sector(_sector) == SPI_FLASH_RESULT_OK) {
102+
if(spi_flash_write(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size) == SPI_FLASH_RESULT_OK) {
103+
_dirty = false;
104+
ret = true;
109105
}
110-
interrupts();
106+
}
107+
interrupts();
111108

112-
return ret;
109+
return ret;
113110
}
114111

115-
uint8_t * EEPROMClass::getDataPtr()
116-
{
117-
_dirty = true;
118-
return &_data[0];
112+
uint8_t * EEPROMClass::getDataPtr() {
113+
_dirty = true;
114+
return &_data[0];
119115
}
120116

121-
122-
EEPROMClass EEPROM;
117+
extern "C" uint32_t _SPIFFS_end;
118+
EEPROMClass EEPROM((((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE));

hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h

+36-37
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,42 @@
2626
#include <stdint.h>
2727
#include <string.h>
2828

29-
class EEPROMClass
30-
{
31-
public:
32-
EEPROMClass();
33-
void begin(size_t size);
34-
uint8_t read(int address);
35-
void write(int address, uint8_t val);
36-
bool commit();
37-
void end();
38-
39-
uint8_t * getDataPtr();
40-
41-
template<typename T> T &get(int address, T &t)
42-
{
43-
if (address < 0 || address + sizeof(T) > _size)
44-
return t;
45-
46-
uint8_t *ptr = (uint8_t*) &t;
47-
memcpy(ptr, _data + address, sizeof(T));
48-
return t;
49-
}
50-
51-
template<typename T> const T &put(int address, const T &t)
52-
{
53-
if (address < 0 || address + sizeof(T) > _size)
54-
return t;
55-
56-
const uint8_t *ptr = (const uint8_t*) &t;
57-
memcpy(_data + address, ptr, sizeof(T));
58-
_dirty = true;
59-
return t;
60-
}
61-
62-
protected:
63-
uint8_t* _data;
64-
size_t _size;
65-
bool _dirty;
29+
class EEPROMClass {
30+
public:
31+
EEPROMClass(uint32_t sector);
32+
33+
void begin(size_t size);
34+
uint8_t read(int address);
35+
void write(int address, uint8_t val);
36+
bool commit();
37+
void end();
38+
39+
uint8_t * getDataPtr();
40+
41+
template<typename T>
42+
T &get(int address, T &t) {
43+
if (address < 0 || address + sizeof(T) > _size)
44+
return t;
45+
46+
memcpy((uint8_t*) &t, _data + address, sizeof(T));
47+
return t;
48+
}
49+
50+
template<typename T>
51+
const T &put(int address, const T &t) {
52+
if (address < 0 || address + sizeof(T) > _size)
53+
return t;
54+
55+
memcpy(_data + address, (const uint8_t*) &t, sizeof(T));
56+
_dirty = true;
57+
return t;
58+
}
59+
60+
protected:
61+
uint32_t _sector;
62+
uint8_t* _data;
63+
size_t _size;
64+
bool _dirty;
6665
};
6766

6867
extern EEPROMClass EEPROM;

hardware/esp8266com/esp8266/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void loop()
2222
// need to divide by 4 because analog inputs range from
2323
// 0 to 1023 and each byte of the EEPROM can only hold a
2424
// value from 0 to 255.
25-
int val = analogRead(0) / 4;
25+
int val = analogRead(A0) / 4;
2626

2727
// write the value to the appropriate byte of the EEPROM.
2828
// these values will remain there when the board is

0 commit comments

Comments
 (0)