nvs_flash_init retry?

jorgeespinoza
Posts: 3
Joined: Wed Feb 13, 2019 9:10 pm

nvs_flash_init retry?

Postby jorgeespinoza » Wed Feb 13, 2019 9:30 pm

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

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

Re: nvs_flash_init retry?

Postby ESP_igrr » Wed Feb 13, 2019 9:42 pm

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.

jorgeespinoza
Posts: 3
Joined: Wed Feb 13, 2019 9:10 pm

Re: nvs_flash_init retry?

Postby jorgeespinoza » Wed Feb 13, 2019 10:49 pm

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!!

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

Re: nvs_flash_init retry?

Postby ESP_igrr » Thu Feb 14, 2019 1:21 am

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.

jorgeespinoza
Posts: 3
Joined: Wed Feb 13, 2019 9:10 pm

Re: nvs_flash_init retry?

Postby jorgeespinoza » Fri Feb 15, 2019 8:47 am

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!!

Who is online

Users browsing this forum: Bing [Bot] and 122 guests