writing to NVS
writing to NVS
Hi all -
I'm about ready to tackle writing to non-volatile storage. I've read the page on Non-volatile storage library, as well as The Bible. Unfortunately, the links to the examples are broken. Does anyone have a good example? In the past, on other systems, I've just dedicated a portion of flash (with a known address) to this purpose, and wrote my own I/O routines. With the ESP32, I'm happy to use the API, if it will serve my needs. I'd like to define a struct with all the elements I need to store. I see, though, that the interfaces uses a key/value system, and the values are limited to a handful of data primitives. Am I truly restricted in this way, or is there another system that I haven't yet discovered.
The stuff I want to store will be like MAC address, serial number, host address and various start-up parameters. It's not going to be a lot of data; 100 bytes is probably more than enough.
Thanks for any suggestions.
I'm about ready to tackle writing to non-volatile storage. I've read the page on Non-volatile storage library, as well as The Bible. Unfortunately, the links to the examples are broken. Does anyone have a good example? In the past, on other systems, I've just dedicated a portion of flash (with a known address) to this purpose, and wrote my own I/O routines. With the ESP32, I'm happy to use the API, if it will serve my needs. I'd like to define a struct with all the elements I need to store. I see, though, that the interfaces uses a key/value system, and the values are limited to a handful of data primitives. Am I truly restricted in this way, or is there another system that I haven't yet discovered.
The stuff I want to store will be like MAC address, serial number, host address and various start-up parameters. It's not going to be a lot of data; 100 bytes is probably more than enough.
Thanks for any suggestions.
Re: writing to NVS
The links to two examples on the Non-volatile storage library documentation works fine:
storage/nvs_rw_value
storage/nvs_rw_blob
storage/nvs_rw_value
storage/nvs_rw_blob
Re: writing to NVS
Kolban's library has some easy to use routines that work for everthing you mentioned.
Re: writing to NVS
Neil has a library? I couldn't find mention of it in The Bible.
Re: writing to NVS
I have a sample C++ class for working with NVS found here:
https://github.com/nkolban/esp32-snippe ... s/CPPNVS.h
I sure hope that no-ones religion is based on writings created in the manner in which I jot down my notes on ESP32. Conversely, we might have ended up with:
"And Moses descended the mountain with 0xa operating instructions flashed to read-only media".
https://github.com/nkolban/esp32-snippe ... s/CPPNVS.h
I sure hope that no-ones religion is based on writings created in the manner in which I jot down my notes on ESP32. Conversely, we might have ended up with:
"And Moses descended the mountain with 0xa operating instructions flashed to read-only media".
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Re: writing to NVS
These examples are much more helpful than the documentation IMO.
So, if I understand this, the usage sequence would be:
nvs_open()
nvs_set_blob() // do it first time to verify space
nvs_set_blob() to update
nvs_get_blob() to retrieve (and I map my struct onto the returned value of nvs_get_blob)
nvs_commit // wherever necessary
That about right?
This is good for a start. Now, I noticed in nvs.h there's mention of partition awareness. I'm probably going to use the "Factory app, two OTA definitions" configuration; do I need to be concerned about partitions when I use NVS?
Thanks...
So, if I understand this, the usage sequence would be:
nvs_open()
nvs_set_blob() // do it first time to verify space
nvs_set_blob() to update
nvs_get_blob() to retrieve (and I map my struct onto the returned value of nvs_get_blob)
nvs_commit // wherever necessary
That about right?
This is good for a start. Now, I noticed in nvs.h there's mention of partition awareness. I'm probably going to use the "Factory app, two OTA definitions" configuration; do I need to be concerned about partitions when I use NVS?
Thanks...
Re: writing to NVS
You have to specify a partition for NVS. This is from one of the examples...
I'm guessing all the partition files have it by default.
John A
Code: Select all
# Name, Type, SubType, Offset, Size, Flags
# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M,
storage, data, fat, , 1M,
John A
Re: writing to NVS
Oh, yes. I actually went through this exercise about a month ago, but I've been involved with so many other tasks that I'd forgotten. Thanks for the reminder.
According to the docs:
NVS works best for storing many small values, rather than a few large values of type ‘string’ and ‘blob’. If storing large blobs or strings is required, consider using the facilities provided by the FAT filesystem on top of the wear levelling library.
I'm not sure what the author meant by "many," but I'm going to need to store maybe 10 elements, none of which are big. They'll be things like:
According to the docs:
NVS works best for storing many small values, rather than a few large values of type ‘string’ and ‘blob’. If storing large blobs or strings is required, consider using the facilities provided by the FAT filesystem on top of the wear levelling library.
I'm not sure what the author meant by "many," but I'm going to need to store maybe 10 elements, none of which are big. They'll be things like:
- MAC Address
serial number
device name
number of writes to NVS
Re: writing to NVS
To me, the key information on whether to use NVS vs File System is based on:
1. What is the total amount of data you may anticipate having stored at any one time?
2. What is the "write/update" rate? How often are you creating new or updated data? Once, once per hour, once per minute, 20 times a second?
1. What is the total amount of data you may anticipate having stored at any one time?
2. What is the "write/update" rate? How often are you creating new or updated data? Once, once per hour, once per minute, 20 times a second?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Re: writing to NVS
Our needs in this area are quite modest. We'll need less (probably much less) than 1K of storage, and it will be updated less frequently than daily.
Who is online
Users browsing this forum: No registered users and 96 guests