nvs_open return ESP_ERR_NVS_NOT_FOUND after erase flash

steeveone
Posts: 10
Joined: Fri Jan 10, 2020 10:17 pm

nvs_open return ESP_ERR_NVS_NOT_FOUND after erase flash

Postby steeveone » Fri Jan 13, 2023 1:47 am

hello,
i have a working project that uses nvs.
if i do a
idf.py erase-flash
and reflash the project, NVS don't work anymore because when i do

Code: Select all

esp_err_t err = nvs_open("storage", NVS_READONLY, &my_handle);
it return ESP_ERR_NVS_NOT_FOUND.
if i flash an example from $IDF_PATH/examples/storage/nvs... the example works, and than if i flash my project it starts working again.
What i am missing?

follows partition configuration:

Code: Select all

nvs,      data, nvs,     0x9000,  0x6000,
phy_init, data, phy,     0xf000,  0x1000,
factory,  app,  factory, 0x10000, 1M,
storage,  data, spiffs,  ,        0xF0000,
and my initNvs function, if it can help

Code: Select all

void initNvs(void)
{
    ESP_LOGI(TAG, "init nvs");
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ESP_LOGI(TAG, "format nvs");
      ret = nvs_flash_init();
    }
    ESP_LOGI(TAG,"init nvs return %s", esp_err_to_name(ret));
    ESP_ERROR_CHECK(ret);
}

User avatar
mbratch
Posts: 302
Joined: Fri Jun 11, 2021 1:51 pm

Re: nvs_open return ESP_ERR_NVS_NOT_FOUND after erase flash

Postby mbratch » Fri Jan 13, 2023 2:33 am

I assume when your program starts, you always call `nvsInit()`, correct?
Is your call to `nvs_init()` successful?

User avatar
gtjoseph
Posts: 92
Joined: Fri Oct 15, 2021 10:55 pm

Re: nvs_open return ESP_ERR_NVS_NOT_FOUND after erase flash

Postby gtjoseph » Fri Jan 13, 2023 3:02 am

steeveone wrote:
Fri Jan 13, 2023 1:47 am
hello,
i have a working project that uses nvs.
if i do a
idf.py erase-flash
and reflash the project, NVS don't work anymore because when i do

Code: Select all

esp_err_t err = nvs_open("storage", NVS_READONLY, &my_handle);
it return ESP_ERR_NVS_NOT_FOUND.
if i flash an example from $IDF_PATH/examples/storage/nvs... the example works, and than if i flash my project it starts working again.
What i am missing?
ESP_ERR_NVS_NOT_FOUND is telling you that the namespace "storage" doesn't exist which is correct if the flash was just erased. The example code works because it calls nvs_open with NVS_READWRITE which automatically creates the namespace if it doesn't exist. You used NVS_READONLY though so it can't do the automatic creation and just returns the NOT FOUND error. To handle that error, call nvs_open again with NVS_READWRITE, close the handle, then call nvs_open again with NVS_READONLY.

steeveone
Posts: 10
Joined: Fri Jan 10, 2020 10:17 pm

Re: nvs_open return ESP_ERR_NVS_NOT_FOUND after erase flash

Postby steeveone » Fri Jan 13, 2023 3:37 pm

added

Code: Select all

    nvs_handle my_handle;
    ret = nvs_open("storage", NVS_READWRITE, &my_handle);
    nvs_close(my_handle);
to initNvs, now it works!
thanks

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 124 guests