nvs_set_str - max length of string

pijmleko
Posts: 2
Joined: Mon Mar 27, 2017 5:59 pm

nvs_set_str - max length of string

Postby pijmleko » Mon Mar 27, 2017 6:04 pm

hi, I'm trying to store a key-value using nvs_set_str and it fails with ESP_ERR_NVS_NOT_ENOUGH_SPACE.
String has length = 3436 bytes and it is the only key-value pair my program uses.

I don't quite understand the NVS description from the dev book... is there a limit for a single value, or rather all stored values combined? is there any way to determine free storage left?

I'm using DevkitC board.

cheers

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: nvs_set_str - max length of string

Postby ESP_igrr » Mon Mar 27, 2017 6:21 pm

The maximum length of a single string value is 4000 bytes (including the zero terminator byte). The error you are getting indicates that there is not enough space left in the partition to fit such a key value pair. You may try increasing NVS partition size in the partition table by one sector (for default partition tables this would mean moving NVS somewhere after the factory app).

NVS doesn't have an API to get available space. It has been requested, but hasn't been implemented yet.

pijmleko
Posts: 2
Joined: Mon Mar 27, 2017 5:59 pm

Re: nvs_set_str - max length of string

Postby pijmleko » Mon Mar 27, 2017 6:43 pm

thanks, got it.

Although what bothers me is the fact that I get this error only occasionally when updating my key-value pair.
Since this is the only kv in my program, and nvs partition size is 0x6000 ("Single factory app, no OTA") there should be way enough space to store it (even if OS uses some nvs too). So now I wonder if NVS checks remaining space properly when value is replaced for the existing key

Code: Select all

it should be
assert sizeof(key.new_value) < nvs_partition.remaining_space() - sizeof(key.old_value)
rather than
assert sizeof(key.new_value) < nvs_partition.remaining_space()

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: nvs_set_str - max length of string

Postby ESP_igrr » Tue Mar 28, 2017 3:20 am

If you make a dump of nvs partition using 'epstool.py read_flash' command, i can help check what you have in your nvs partition.

The proposal about size calculation makes sense, but this is not how nvs works. One of the design principles of nvs is that there should be no possibility of data loss due to a power off event. Therefore if new data has to be written, it is written first, and then the old data is erased.

Looking at the code again, I see that NVS might be a bit pessimistic in some cases when huge key-value pairs are written... The algorithm was optimized mostly for smaller sizes.
It this point I recommend to see if you can split the data into multiple key-value pairs instead of storing it in one string. I'll put the issue with large key-value pairs onto a list of improvements for the next version of NVS.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 147 guests