Page 1 of 1

nvs_open return ESP_ERR_NVS_NOT_FOUND after erase flash

Posted: Fri Jan 13, 2023 1:47 am
by steeveone
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);
}

Re: nvs_open return ESP_ERR_NVS_NOT_FOUND after erase flash

Posted: Fri Jan 13, 2023 2:33 am
by mbratch
I assume when your program starts, you always call `nvsInit()`, correct?
Is your call to `nvs_init()` successful?

Re: nvs_open return ESP_ERR_NVS_NOT_FOUND after erase flash

Posted: Fri Jan 13, 2023 3:02 am
by gtjoseph
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.

Re: nvs_open return ESP_ERR_NVS_NOT_FOUND after erase flash

Posted: Fri Jan 13, 2023 3:37 pm
by steeveone
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