Esp32S3 : Question concerning NVS

ThomasESP32
Posts: 214
Joined: Thu Jul 14, 2022 5:15 am

Esp32S3 : Question concerning NVS

Postby ThomasESP32 » Fri Jun 16, 2023 11:24 am

Good afternoon,


working with an Esp32s3, I need to define a section in flash in order to put parameters/datas.
Using the "partition.csv" file, I can define a "Parameter" section.

Moreover, I need to define another section in flash in order to put a copy of this first section.
Using the "partition.csv" file, I can define a "ParameterCopy" section.

Looking at the libraries, I see that the NVS library can be used in order to write things into a flash section.
At the beginning, I thought I can use this library in order to write to these two sections.

However, looking at the NVS library documentation, I can see that only one section can be in the Active state.
As I understand, when a section of flash is full, the NVS library can write to a new section (Which is in Active state).
Am I right ??
And I understand, it means that I can't use the NVS library in order to write things into my "Parameter" section (This section will not be full) and to write a copy of this section into my "ParameterCopy" section (This section will not be full too). This because, I can't have two sections in Active state. Am I right ?

So, could you please tell me how to read/write/erase things/datas into two different sections of FLASH ?
Which library/driver I can use please ?

Best regards,

Thomas TRUILHE

ThomasESP32
Posts: 214
Joined: Thu Jul 14, 2022 5:15 am

Re: Esp32S3 : Question concerning NVS

Postby ThomasESP32 » Fri Jun 16, 2023 12:10 pm

Good afternoon,


reading the Esp32S3 documentation, maybe the Partitions API can do what I want to do ?
Could you please confirm me ?

Do you have any idea about the Erase_Size of the partions that are in FLASH_Memory
(The smaller amount of bytes that I can erase in these partitions ? I am using a Esp32-S3-DevKit-C-1 board having 8Mo Flash) ?
The same question, do you have any idea about the Write_Size of the partitions ? (The smaller amout of bytes that I can write in these
partitions).

Thank you for your help,

Best regards,

Thomas TRUILHE

ThomasESP32
Posts: 214
Joined: Thu Jul 14, 2022 5:15 am

Re: Esp32S3 : Question concerning NVS

Postby ThomasESP32 » Fri Jun 16, 2023 1:02 pm

Good afternoon,

Here is a description of what I would like to do :

.section (Name = "Parameter", Size = 5KBytes) : For example, this section will contain the following values :
- uint8_t Name = 1;
- uint8_t Type = 2;
- uint16_t Number = 5;
- char Bluetooth[20] = "BLEName";
There are still 5096 bytes available.

.section (Name = "ParameterCopy", Size = 5KBytes) : This section will be a copy of the "Parameter" section. So, it will contain
the following values :
- uint8_t Name = 1;
- uint8_t Type = 2;
- uint16_t Number = 5;
- char Bluetooth[20] = "BLEName";
There are still 5096 bytes available.

So, as we can see these two sections are not full.
I would like to have the possibility to read/write/erase bytes in these sections of Flash.

Do you think I can do this using NVS library ? (This question because these two sections are not full).
Or do you think I can do this using the Partition library/driver ? In this case, do you know the erase_size and write_size
in embedded memory please ?

Thank you for your help,

Best regards,

Thomas TRUILHE

MicroController
Posts: 1597
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Esp32S3 : Question concerning NVS

Postby MicroController » Fri Jun 16, 2023 2:44 pm

This because, I can't have two sections in Active state
Have you tried? I find nothing in the docs saying you can't have multiple nvs partitions opened at the same time; if you actually need to have them open at the same time.
Do you have any idea about the Erase_Size of the partions that are in FLASH_Memory
SPI_FLASH_SEC_SIZE should be 4KB.

ThomasESP32
Posts: 214
Joined: Thu Jul 14, 2022 5:15 am

Re: Esp32S3 : Question concerning NVS

Postby ThomasESP32 » Tue Jun 20, 2023 10:11 am

According to the Espressif Library documentation :
NVS paragraph :
Each page can be in one of the following states:

Empty/uninitialized
Flash storage for the page is empty (all bytes are 0xff). Page is not used to store any data at this point and does not have a sequence number.

Active
Flash storage is initialized, page header has been written to flash, page has a valid sequence number. Page has some empty entries and data can be written there. No more than one page can be in this state at any given moment.

Full
Flash storage is in a consistent state and is filled with key-value pairs. Writing new key-value pairs into this page is not possible. It is still possible to mark some key-value pairs as erased.

Erasing
Non-erased key-value pairs are being moved into another page so that the current page can be erased. This is a transient state, i.e., page should never stay in this state at the time when any API call returns. In case of a sudden power off, the move-and-erase process will be completed upon the next power-on.

Corrupted
Page header contains invalid data, and further parsing of page data was canceled. Any items previously written into this page will not be accessible. The corresponding flash sector will not be erased immediately and will be kept along with sectors in uninitialized state for later use. This may be useful for debugging.

What do you think ?

ESP_Sprite
Posts: 9607
Joined: Thu Nov 26, 2015 4:08 am

Re: Esp32S3 : Question concerning NVS

Postby ESP_Sprite » Wed Jun 21, 2023 2:14 am

Honestly, this sounds like an X-Y problem. Can you expand a little bit in why exactly you want to have a copy of the parameters? Is it for redundancy, possibility to factory reset, resilience to bug-induced flaws, ...? Asking because the solution may be simpler than you think.

ThomasESP32
Posts: 214
Joined: Thu Jul 14, 2022 5:15 am

Re: Esp32S3 : Question concerning NVS

Postby ThomasESP32 » Wed Jun 21, 2023 5:26 am

Finally, I have tried to initialize two NVS partitions at the same time and to write/read entries in it at the
same time and it seems to be OK. I got no error from the library while doing it.
So, I think I will use the NVS in order to do what I want to do.

However, do you know if there is the possibility to define memory sections on the Esp32S3 using
a linker script or something and to place constants in it direcly in the program ?
I was doing it on Renesas Microcontrollers, using a linker scripts I could define memory sections
and I could put constant in the program using the pragma attribute section keywords.

Moreover, using driver methods like flash_write, I could read my constant to extract some parameters
and write others using the flash_write methods. This enabled me to Read/Write from FlashMem directly
without using NVS library. Do you think it is possible ???

Best regards,

ESP_Sprite
Posts: 9607
Joined: Thu Nov 26, 2015 4:08 am

Re: Esp32S3 : Question concerning NVS

Postby ESP_Sprite » Wed Jun 21, 2023 5:52 am

No, we don't support using constants in flash like the Renesas solution does as it's pretty bad practice from a failsafe and flash wear-out point.

Again, if you tell us what you actually want to achieve with this, we may be able to direct you to some better solutions.

ThomasESP32
Posts: 214
Joined: Thu Jul 14, 2022 5:15 am

Re: Esp32S3 : Question concerning NVS

Postby ThomasESP32 » Wed Jun 21, 2023 6:17 am

Ok thank you for your answer, that is ok.
So according to your recommandation, I will use the NVS in order to do this functionnality.

However, I have one more question please :
I have seen (Looking at the NVS library doc) that there is a CRC in the NVS Header
and a CRC for every parameter located in the NVS.

When opening, the NVS (Initializing I mean) or when reding the parameters fro the NVS,
how can I check that the CRC are ok or not ?
I need to have the possibility to check the CRCs of my partition in order to detect memory failure.
Do you think it is possible, is there a particular error code or something ?

Best regards,

Thomas TRUILHE

ESP_Sprite
Posts: 9607
Joined: Thu Nov 26, 2015 4:08 am

Re: Esp32S3 : Question concerning NVS

Postby ESP_Sprite » Thu Jun 22, 2023 2:07 am

ThomasESP32 wrote:
Wed Jun 21, 2023 6:17 am
However, I have one more question please :
I have seen (Looking at the NVS library doc) that there is a CRC in the NVS Header
and a CRC for every parameter located in the NVS.
It's abstracted away by the NVS driver; if the CRC of a parameter does not match, you will not get that parameter. If the CRC for the header does not match, from memory the NVS fails initialization entirely and you need to re-initialize it. However, note that NVS is designed to be resistant against power fails in the middle of a write and will handle that gracefully without any user interference because of how it's designed.

Who is online

Users browsing this forum: Baidu [Spider] and 81 guests