I can’t reprodue your problem. I’ve written this EEPROM helper to help dump, read, write and erase the flash.
#include <Arduino.h>
#include <ESP.h>
#include <EEPROM.h>
String userInput;
/* imitate logic from EEPROM.cpp */
extern "C" uint32_t _SPIFFS_end;
uint32_t eeprom_sector = (((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE);
const int _size = 4;
uint8_t _data[_size];
void hexDump (const char *desc, const void *addr, const int len);
void setup() {
Serial.begin(115200);
printf("\nEEPROM Helper start. EEPROM sector at 0x%08x, end of SPIFFS at 0x%08x\n", (unsigned)eeprom_sector, (unsigned)&_SPIFFS_end);
//use only first 4 bytes/ 1 flash word
EEPROM.begin(_size);
}
void dumpFlash() {
memset(_data, 0, _size);
ESP.flashRead(eeprom_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
hexDump("Flash data", _data, _size);
}
void loop() {
if(Serial.available()) {
userInput = Serial.readString();
userInput.trim();
if(userInput == "dump") {
dumpFlash();
} else if(userInput == "write") {
EEPROM.write(0, 0x01);
EEPROM.write(1, 0x02);
EEPROM.write(2, 0x03);
EEPROM.write(3, 0x04);
EEPROM.commit();
Serial.println("Wrote 1..4 into addresses 0..3");
} else if(userInput == "read") {
for(int i=0; i < 4; i++) {
printf("EEPROM[%d] = 0x%02x\n", i, (int)EEPROM.read(i));
}
} else if(userInput == "erase") {
EEPROM.end();
printf("Erasing sector 0x%08x\n", eeprom_sector);
ESP.flashEraseSector(eeprom_sector);
//re-begin EEPROM otherwise old contents are still in RAM
EEPROM.begin(_size);
} else {
Serial.print ("Unknown command: ");
Serial.println(userInput);
}
}
}
void hexDump (const char *desc, const void *addr, const int len) {
int i;
unsigned char buff[17];
const unsigned char *pc = (const unsigned char*)addr;
// Output description if given.
if (desc != NULL)
printf ("%s:\n", desc);
if (len == 0) {
printf(" ZERO LENGTH\n");
return;
}
if (len < 0) {
printf(" NEGATIVE LENGTH: %i\n",len);
return;
}
// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0)
printf (" %s\n", buff);
// Output the offset.
printf (" %04x ", i);
}
// Now the hex code for the specific character.
printf (" %02x", pc[i]);
// And store a printable ASCII character for later.
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
buff[i % 16] = '.';
else
buff[i % 16] = pc[i];
buff[(i % 16) + 1] = '\0';
}
// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf (" ");
i++;
}
// And print the final ASCII bit.
printf (" %s\n", buff);
}
with platformio.ini
[env:esp8266]
monitor_speed = 115200
platform = espressif8266
board = nodemcuv2
framework = arduino
I test it as follows:
- erase the chip
- use the dump command to make sure the flash is erased (must read as
0xff..
- write in EEPROM
- read back EEPROM
- reflash the firmware
- read back EEPROM
The test result is that EEPROM data is preserved upon reflashes. Output
C:\Users\Maxi>"C:\Program Files (x86)\Python35\Scripts\miniterm.py" --echo --exit-char=50 COM12 115200
--- Miniterm on COM12 115200,8,N,1 ---
--- Quit: '2' | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
EEPROM Helper start. EEPROM sector at 0x000003fb, end of SPIFFS at 0x405fb000
dump
Flash data:
0000 ff ff ff ff ....
read
EEPROM[0] = 0xff
EEPROM[1] = 0xff
EEPROM[2] = 0xff
EEPROM[3] = 0xff
write
Wrote 1..4 into addresses 0..3
dump
Flash data:
0000 01 02 03 04 ....
read
EEPROM[0] = 0x01
EEPROM[1] = 0x02
EEPROM[2] = 0x03
EEPROM[3] = 0x04
--- exit ---
<reflash using "PlatformIO upload" task>
C:\Users\Maxi>"C:\Program Files (x86)\Python35\Scripts\miniterm.py" --echo --exit-char=50 COM12 115200
--- Miniterm on COM12 115200,8,N,1 ---
--- Quit: '2' | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
EEPROM Helper start. EEPROM sector at 0x000003fb, end of SPIFFS at 0x405fb000
dump
Flash data:
0000 01 02 03 04 ....
read
EEPROM[0] = 0x01
EEPROM[1] = 0x02
EEPROM[2] = 0x03
EEPROM[3] = 0x04
--- exit ---
So, did I understand the problem correctly that this is exactly what shouldn’t happen, i.e. after a reflash data is destroyed? Also with what exact platformio.ini
are you working? Do you have a minimal example for problem reproduction?