nvs_flash_init retry?
Posted: Wed Feb 13, 2019 9:30 pm
by jorgeespinoza
Hi,
In the example
esp-idf/examples/wifi/wps/main/wps.c, I see there is a retry if `nvs_flash_init` returns specific failure codes.
Code: Select all
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());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
In other examples, like
esp-idf/examples/protocols/https_server/main/main.c, there is no retry.
Code: Select all
ESP_ERROR_CHECK(nvs_flash_init());
Why the difference in these examples? Is one better in certain situations or unsuited to others?
Thanks!!
Jorge
Re: nvs_flash_init retry?
Posted: Wed Feb 13, 2019 9:42 pm
by ESP_igrr
The first option is better because it handles errors and goes on instead of aborting. The second is a simple version which is only okay for example projects, and should not be used in the production code.
Re: nvs_flash_init retry?
Posted: Wed Feb 13, 2019 10:49 pm
by jorgeespinoza
I saw in the
commit history of esp-idf/examples/wifi/wps/main/wps.c that a second return code check was added. This makes me wonder how to make the check more future proof in case more return codes need to be checked in the future.
Would it be more robust and future proof if the retry was written as the following or would this introduce other problems?
Code: Select all
esp_err_t ret = nvs_flash_init();
if (ret != ESP_OK) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
Thanks!!
Re: nvs_flash_init retry?
Posted: Thu Feb 14, 2019 1:21 am
by ESP_igrr
Not all of the possible errors can be solved by erasing flash and retrying, and erasing flash might obfuscate the cause of the original issue. So the recommended way is to only check for the specific list of errors, as shown in the NVS examples. If you are updating your code to the new version of IDF, it is worth checking the release notes and documentation to see if some new return codes have been introduced.
Re: nvs_flash_init retry?
Posted: Fri Feb 15, 2019 8:47 am
by jorgeespinoza
I created a ticket about adding the retry in the ESP-IDF template repository on GitHub. Is there any interest in me creating a pull request?
https://github.com/espressif/esp-idf-template/issues/11
Thanks!!