I'm using NodeMCU 1.0, with 4MB flash (1M SPIFFS).
The code I'm using is:
Serial.println("Erasing");
const uint32_t addr = 0x40300000;
const uint32_t sector = addr / SPI_FLASH_SEC_SIZE;
Serial.println(SPI_FLASH_SEC_SIZE);
if (!ESP.flashEraseSector(sector))
{
Serial.println("Flash erase failed");
}
else
{
uint32_t readData;
Serial.println();
Serial.println("Reading");
for (int i=0; i<100; i=i+4)
{
ESP.flashRead(addr+i, &readData, 4);
Serial.print(readData&0xff);
Serial.print(" ");
Serial.print((readData&0xff00)>>8);
Serial.print(" ");
Serial.print((readData&0xff0000)>>16);
Serial.print(" ");
Serial.print(readData>>24);
Serial.print(" ");
}
}
Serial.println();
const uint32_t dataIn = {0x44332211};
if (!ESP.flashWrite(addr, (uint32_t*)dataIn, 4))
{
Serial.println("Flash write failed");
}
else
{
uint32_t readData;
Serial.println();
Serial.println("Reading after write");
for (int i=0; i<100; i=i+4)
{
ESP.flashRead(addr+i, &readData, 4);
Serial.print(readData&0xff);
Serial.print(" ");
Serial.print((readData&0xff00)>>8);
Serial.print(" ");
Serial.print((readData&0xff0000)>>16);
Serial.print(" ");
Serial.print(readData>>24);
Serial.print(" ");
}
}
I'm assuming flashEraseSector does work on sector numbers, which is the address divided by 4096 (sector size). flashRead and flashWrite operate on addresses, and I've used your suggested 0x40300000. I believe I'm correctly byte aligned and I'm reading 4 (bytes?) with flashRead - or should this be set to 1 (32bit read)?
The erase completes without error but when I read the corresponding address, I read the following:
254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254 254 239 239 254
The attempted write after this read fails.
Any idea what I'm doing wrong?