Question : Can i define the address where will be stored NVS key-value pairs in flash?

ogensyts
Posts: 9
Joined: Fri Jan 05, 2018 2:41 pm
Location: Quebec, Canada

Question : Can i define the address where will be stored NVS key-value pairs in flash?

Postby ogensyts » Sun Apr 22, 2018 11:51 pm

Hi everyone,

For my project i need to use the flash to store 2 types of information :

1. Vital configuration of my ESP32 (i.e server authentification keys, WIFI credentials and some other values...)
2. Sensor data that will be stored in flash and that will be recovered after a week of usage.

Currently, to store items listed in ( 1 ) i use NVS library to store key-value pairs in flash.
To store items listed in ( 2 ), i use spi_flash_write() to write data when needed. I begin writing at sector 0x1A2 (418).

Now, why i am writing this post : i do not know where the NVS library used in ( 1 ) stores it's data, meaning at what address in the flash. I am scared that by writing data using spi_flash_write() like in ( 2 ), that it will someday overwrite the data stored from the NVS library where will be stored vital data for my system to function ( 1 ).

Here is an exemple of the flash from begin to end at a certain moment :

flash_begin ---- > data written in ( 2 ) -----> empty (not used yet) -----> used by NVS library ( 1 ) (where my key-value pairs are stored, maybe) ----> empty -----> flash_end

And what i am scared that will happen after a certain time ( that using spi_flash_write will overwrite my key-value pairs) :

flash_begin ---- > data written in ( 2 ) ----->data written in ( 2 ) -----> data written in ( 2 ) (OVERWRITED key-value data) --> empty -----> flash_end

So my question is : how can i be sure that i will never overwrite data stored by the NVS library? Can i define the start address where will be stored NVS key-value pairs in flash to be sure to never overwrite them by using spi_flash_write() ? Cause i do not know where NVS library stores its data in flash.

Note : i am using ESP-WROOM-32 chip.


Regards,
ogensyts

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Question : Can i define the address where will be stored NVS key-value pairs in flash?

Postby fly135 » Mon Apr 23, 2018 12:53 am

The address of the NVS partition is defined in the partition table.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Question : Can i define the address where will be stored NVS key-value pairs in flash?

Postby ESP_Angus » Mon Apr 23, 2018 12:55 am

Hi ogensyts,

ESP-IDF has the notion of "partitions" for this kind of use case. You can read about partition tables here:
http://esp-idf.readthedocs.io/en/latest ... ables.html

As you'll see in the docs, in the default partition table NVS starts at 0x9000 and is 0x6000 bytes long.

It sounds like you've picked an area in flash around 1.6MB into the chip, which in the default (non-OTA) partition table is not used.

The recommended approach is to define your own custom partition type and add a partition of this type to the partition table you flash to the chip. Then you can use the partition APIs to find this partition and there are partition-specific functions to read & write data in that partition only. This removes the possibility of accidentally clobbering vital regions of flash:
http://esp-idf.readthedocs.io/en/latest ... table-apis

Using a custom data partition also means that you change the (absolute) layout of your flash in the future, without needing to change your code.

Who is online

Users browsing this forum: No registered users and 116 guests