Page 1 of 1

esp_flash_read()/esp_flash_write() ignoring length parameter

Posted: Sat Aug 10, 2024 6:41 pm
by gailu96
Hi Experts,

I am trying to esp_flash_read() and esp_flash_write() apis of ESP IDF. Using ESP32 Dev board with 2MB RAM. I have reserved 1MB flash starting for my custom data usage. I am able to write and read in to flash using apis, but only 1 Byte. Rest bytes of my buffer are either not written to flash or not read properly from flash even though I am passing length parameter correctly. I am sure platofrm api cannot have such problem so I must be doing something wrong in my code but not able to find mistake in my code either. Please note that I am using Arduino with ESP32 version 3.0.4. Can someone please help me what is wrong here

My Code:

Code: Select all

/*
   SPIFlash
*/

extern "C"{
    // Platform Includes
    #include <esp_flash.h>
    #include <esp_err.h>
}

#include <Arduino.h>


// the setup function runs once when you press reset or power the board
void setup() {
  esp_err_t ret;

  Serial.begin(115200);

  ret = esp_flash_init(esp_flash_default_chip);

  Serial.printf("esp_flash_init() returned: %s\r\n", esp_err_to_name(ret));
  
}

// the loop function runs over and over again forever
void loop() {
  esp_err_t ret;
  const char buffer[] = "Hello World";
  char value[16];

  delay(1000);
  ret = esp_flash_write(esp_flash_default_chip, buffer, 0x100000, strlen(buffer)+1);
  Serial.printf("esp_flash_write() returned: %s length written: %d\r\n", esp_err_to_name(ret), strlen(buffer)+1);

  delay(1000);
  ret = esp_flash_read(esp_flash_default_chip, value, 0x100000, 16);
  Serial.printf("esp_flash_read() returned: %s Original Value: %s Retrieved Value: %s\r\n", esp_err_to_name(ret), buffer, value);  

  delay(5000);

}

My Serial Prints

Code: Select all

esp_flash_write() returned: ESP_OK length written: 12
esp_flash_read() returned: ESP_OK Original Value: Hello World Retrieved Value: H
esp_flash_write() returned: ESP_OK length written: 12
esp_flash_read() returned: ESP_OK Original Value: Hello World Retrieved Value: H
esp_flash_write() returned: ESP_OK length written: 12
...

Re: esp_flash_read()/esp_flash_write() ignoring length parameter

Posted: Sun Aug 11, 2024 8:22 am
by boarchuz
With NOR flash, writes can only set bits to 0. Erase commands must be used to reset all bits to 1.

Ordinarily you'd erase the sector (or block) before writing to it, otherwise trying to write any bits 0->1 will have no effect.

It seems your byte at 0x100001 is already 0x00.

Re: esp_flash_read()/esp_flash_write() ignoring length parameter

Posted: Sun Aug 11, 2024 9:46 am
by gailu96
Thanks. After erasing Its working as expected. Thanks for your support

Re: esp_flash_read()/esp_flash_write() ignoring length parameter

Posted: Sun Aug 11, 2024 3:21 pm
by aliarifat794
Before writing data to flash memory, you should erase the sector where you intend to write the data so that all bits in that sector are set to 1, allowing the subsequent write operation to correctly set bits to 0 where needed.