esp_flash_read()/esp_flash_write() ignoring length parameter

gailu96
Posts: 9
Joined: Sat Aug 10, 2024 6:06 pm

esp_flash_read()/esp_flash_write() ignoring length parameter

Postby gailu96 » Sat Aug 10, 2024 6:41 pm

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
...

boarchuz
Posts: 598
Joined: Tue Aug 21, 2018 5:28 am

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

Postby boarchuz » Sun Aug 11, 2024 8:22 am

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.

gailu96
Posts: 9
Joined: Sat Aug 10, 2024 6:06 pm

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

Postby gailu96 » Sun Aug 11, 2024 9:46 am

Thanks. After erasing Its working as expected. Thanks for your support

aliarifat794
Posts: 128
Joined: Sun Jun 23, 2024 6:18 pm

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

Postby aliarifat794 » Sun Aug 11, 2024 3:21 pm

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.

Who is online

Users browsing this forum: Bing [Bot] and 167 guests