Page 1 of 1

ESP32: Migrating from version 2.x to 3.0 (esp_flash_read, spi_flash_read)

Posted: Mon Dec 16, 2024 12:28 pm
by esp_man
Hello.
I have upgraded ESP32 library in my Arduino, from version 2.x to 3.0 (formally, 3.0 is based on ESP-IDF 5.1).
And I have issues with spi_flash_xxxx functions.
I find by myself that functions was upgraded and names was changed to "esp". For example, now there are esp_flash_read instead spi_flash_read.
Also, esp_flash_t *chip parameter was added.

Code: Select all

esp_err_t esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
More information here:
https://docs.espressif.com/projects/esp ... index.html

My old code working good for 2 years. Part to which the question relates are very simple. I have RAM array:

Code: Select all

uint8_t FLASH_Buffer[4096];
And at startup, I reads one Flash sector into it:

Code: Select all

esp_flash_read(0x003FF000, (uint32_t*)FLASH_Buffer, 4096);
Please ignore "magic numbers". This code at least compile, and works.
I reading from main SPI FLASH (the same that contain ESP32 executable code), so I don't call esp_flash_init function. Because ESP32 initializes this memory (and SPI) by itself at startup.

But in 3.0 version, I have to include esp_flash_t *chip parameter.
I try NULL or esp_flash_default_chip, but without success.

Code: Select all

esp_flash_read(esp_flash_default_chip, 0x003FF000, (uint32_t*)FLASH_Buffer, 4096);
I getting errors like "function was not declared in this scope", or:
Compilation error: invalid conversion from 'int' to 'void*' [-fpermissive]
The same errors for other flash functions (write, erase sector, etc).

I need to obtain at least compilable code.
What I'm doing wrong, and how to fix it?

Re: ESP32: Migrating from version 2.x to 3.0 (esp_flash_read, spi_flash_read)

Posted: Mon Dec 16, 2024 5:37 pm
by lbernstone
The data parameter is a void*, so rather than trying to cast the variable in the read function, set it to the type you want as a variable. The compiler will figure out how to pack it in the structure.

Code: Select all

  uint32_t dataSize = 50;
  uint32_t readData[dataSize];
  esp_flash_read(NULL, readData, 0x8000, dataSize*4);
  for (int x=0; x< dataSize; x++) Serial.printf("%X\n", readData[x]);

Re: ESP32: Migrating from version 2.x to 3.0 (esp_flash_read, spi_flash_read)

Posted: Mon Dec 16, 2024 7:30 pm
by esp_man
This was not an issue, but Your answer directed me to something else.
Order of argument is different.
In ESP-IDF 5.1, address must be third parameter, not the second one as in ESP-IDF 4.4.
This code compiles without errors:

Code: Select all

esp_flash_read(esp_flash_default_chip, (uint32_t*)FLASH_Buffer, 0x003FF000, 4096);
I will check later if it works in real.
So Thank You!

Re: ESP32: Migrating from version 2.x to 3.0 (esp_flash_read, spi_flash_read)

Posted: Tue Dec 17, 2024 4:57 am
by lbernstone
Glad to help you get unstuck