ESP32 - nvs pointer issue

Jimis1
Posts: 35
Joined: Wed May 03, 2023 6:20 am

ESP32 - nvs pointer issue

Postby Jimis1 » Sun Feb 11, 2024 5:20 pm

I'm getting the below pointer error. The Esp idf function nvs_set_i32() requires a int32_t (not uint32!?) I made a macro to make my life easier but I get the error Can someone help ?

"passing argument 3 of 'nvs_set_i32' makes integer from pointer without a cast [-Wint-conversion]"
Attachments
Screenshot_1.jpg
Screenshot_1.jpg (109.06 KiB) Viewed 2003 times
Screenshot_2.jpg
Screenshot_2.jpg (38.74 KiB) Viewed 2003 times

ESP_adokitkat
Posts: 52
Joined: Thu Jun 22, 2023 12:50 pm

Re: ESP32 - nvs pointer issue

Postby ESP_adokitkat » Sun Feb 11, 2024 6:08 pm

Hello. This is a function signature for nvs_set_i32.
  1. esp_err_t nvs_set_i32 (nvs_handle_t handle, const char* key, int32_t value);
You're passing int32_t pointer (int32_t *) into int32_t value field. You need to dereference it first with * or use just int32_t without pointer in your function parameter. Also your are using # macro stringification on "val", but I think it will stringify "val" itself and not the value, so you overwrite your previous values.

Jimis1
Posts: 35
Joined: Wed May 03, 2023 6:20 am

Re: ESP32 - nvs pointer issue

Postby Jimis1 » Sun Mar 03, 2024 8:53 am

Thanks ESP_adokitkat I finally did it properly.

I still have a question though if you can answer.

I'm trying to check whether the ESP has reset from an upgrade (USB or OTA) to reset my "reset_counter" which is stored in flash.

I tried to detect that from the response from the function nvs_get(). If I get a "value not initialized" then it means I had a code upgrade and I should reset the "reset_counter" variable. But it seems that after I upgrade my code this variable is still initialized !
Attachments
Screenshot_2.jpg
Screenshot_2.jpg (35.54 KiB) Viewed 1769 times

ESP_adokitkat
Posts: 52
Joined: Thu Jun 22, 2023 12:50 pm

Re: ESP32 - nvs pointer issue

Postby ESP_adokitkat » Thu Mar 07, 2024 2:10 am

NVS is short for non-volatile storage. NVS partition is not erased during OTA upgrade nor even if you re-flash your firmware via USB, your values should still be there. You need to specifically erase the partition or do `idf.py erase-flash` to erase everything, or re-flash NVS partition with `idf.py <partition>-flash` i.e. `idf.py nvs-flash` if you generate it on PC.

If you are doing OTA upgrade for example you can just add another key to you NVS which you will set to e.g. 1 before performing the upgrade - if the upgrade fails, reset it back to 0, if it succeeds and reboots, it will stay 1 so you can check it and know you rebooted because of the upgrade and then reset it back to 0. Or you can write your own OTA upgrade function which increases some value in NVS like "ota_upgrade_counter" before rebooting...

For USB upgrade it is more tricky but you could do something like store firmware version in a define/variable and store it in NVS as well. At boot you can check if the define is the same as the value in the NVS and if not, you can increase e.g. "upgrade_counter" in NVS and set the new version value for future comparisons...

Just some quick ideas, maybe there are better ways.

Jimis1
Posts: 35
Joined: Wed May 03, 2023 6:20 am

Re: ESP32 - nvs pointer issue

Postby Jimis1 » Sun Mar 10, 2024 12:13 pm

Thanks ESP_adokitkat :)

Who is online

Users browsing this forum: Google [Bot] and 94 guests