writing to NVS

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

writing to NVS

Postby mzimmers » Fri Jun 01, 2018 6:48 pm

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.

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Re: writing to NVS

Postby loboris » Fri Jun 01, 2018 7:08 pm

The links to two examples on the Non-volatile storage library documentation works fine:
storage/nvs_rw_value
storage/nvs_rw_blob

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

Re: writing to NVS

Postby fly135 » Fri Jun 01, 2018 8:47 pm

Kolban's library has some easy to use routines that work for everthing you mentioned.

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: writing to NVS

Postby mzimmers » Fri Jun 01, 2018 9:55 pm

Neil has a library? I couldn't find mention of it in The Bible.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: writing to NVS

Postby kolban » Fri Jun 01, 2018 10:38 pm

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".
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: writing to NVS

Postby mzimmers » Fri Jun 01, 2018 10:56 pm

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...

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

Re: writing to NVS

Postby fly135 » Fri Jun 01, 2018 11:12 pm

You have to specify a partition for NVS. This is from one of the examples...

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, 
I'm guessing all the partition files have it by default.

John A

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: writing to NVS

Postby mzimmers » Mon Jun 04, 2018 3:19 pm

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:
  • MAC Address
    serial number
    device name
    number of writes to NVS
As I mentioned, in the past on other devices, I've gotten into the habit of just creating a C struct containing these items and mapping it onto the NVS. Am I really better off using the filesystem? I'm also a little concerned about how much program space doing so will require.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: writing to NVS

Postby kolban » Mon Jun 04, 2018 3:52 pm

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?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: writing to NVS

Postby mzimmers » Mon Jun 04, 2018 4:28 pm

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 224 guests