esp_flash_read()/esp_flash_write() ignoring length parameter
Posted: 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:
My Serial Prints
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);
}
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
...