Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By mph070770
#58507 Thanks again Martin. I've given this a go, using this as a aid: https://github.com/esp8266/Arduino/blob ... fs_hal.cpp but I must be doing something wrong...

I'm using NodeMCU 1.0, with 4MB flash (1M SPIFFS).

The code I'm using is:

Code: Select allSerial.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?
User avatar
By mph070770
#58862 Ok, an update. This worked:

Code: Select allstart_raw_location=(ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));


... to get a valid starting point for free flash. I found it here:

https://github.com/esp8266/Arduino/issues/2665

I still don't understand why the suggested solution from Martin didn't work (flash addr start 0x40300000) and I'd like to understand the flash memory map properly so that I can safely fill the spare flash with my own code.

Can anyone else suggest a good definitive explanation?