examples: check return value of nvs_flash_init

nvs_flash_init may return an error code in some cases, and applications
should check this error code (or at least assert on it being ESP_OK, to
make potential issues more immediately obvious).

This change modifies all the examples which use NVS to check the error
code. Most examples get a simple ESP_ERROR_CHECK assert, while NVS
examples, OTA example, and NVS unit tests get a more verbose check which
may be used in real applications.
This commit is contained in:
Ivan Grokhotkov 2017-03-14 21:39:44 +08:00
parent f59f13efd5
commit 4813ab2d28
14 changed files with 68 additions and 24 deletions

View File

@ -6,14 +6,26 @@
#include "unity.h" #include "unity.h"
#include "nvs.h" #include "nvs.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "esp_spi_flash.h" #include "esp_partition.h"
#include "esp_log.h"
#include <string.h> #include <string.h>
static const char* TAG = "test_nvs";
TEST_CASE("various nvs tests", "[nvs]") TEST_CASE("various nvs tests", "[nvs]")
{ {
nvs_handle handle_1; nvs_handle handle_1;
TEST_ESP_OK(nvs_flash_init()); esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
ESP_LOGW(TAG, "nvs_flash_init failed (0x%x), erasing partition and retrying", err);
const esp_partition_t* nvs_partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
assert(nvs_partition && "partition table must have an NVS partition");
ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
TEST_ESP_ERR(nvs_open("test_namespace1", NVS_READONLY, &handle_1), ESP_ERR_NVS_NOT_FOUND); TEST_ESP_ERR(nvs_open("test_namespace1", NVS_READONLY, &handle_1), ESP_ERR_NVS_NOT_FOUND);
TEST_ESP_ERR(nvs_set_i32(handle_1, "foo", 0x12345678), ESP_ERR_NVS_INVALID_HANDLE); TEST_ESP_ERR(nvs_set_i32(handle_1, "foo", 0x12345678), ESP_ERR_NVS_INVALID_HANDLE);

View File

@ -312,7 +312,7 @@ void app_main()
{ {
esp_err_t ret; esp_err_t ret;
nvs_flash_init(); ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi(); initialise_wifi();
esp_bt_controller_init(); esp_bt_controller_init();

View File

@ -199,7 +199,7 @@ static void wifi_conn_init(void)
void app_main(void) void app_main(void)
{ {
nvs_flash_init(); ESP_ERROR_CHECK( nvs_flash_init() );
wifi_conn_init(); wifi_conn_init();
xTaskCreate(coap_demo_thread, "coap", 2048, NULL, 5, NULL); xTaskCreate(coap_demo_thread, "coap", 2048, NULL, 5, NULL);
} }

View File

@ -185,7 +185,7 @@ static void wifi_conn_init(void)
void app_main(void) void app_main(void)
{ {
nvs_flash_init(); ESP_ERROR_CHECK( nvs_flash_init() );
wifi_conn_init(); wifi_conn_init();
xTaskCreate(coap_demo_thread, "coap", 2048, NULL, 5, NULL); xTaskCreate(coap_demo_thread, "coap", 2048, NULL, 5, NULL);

View File

@ -174,7 +174,7 @@ static void http_get_task(void *pvParameters)
void app_main() void app_main()
{ {
nvs_flash_init(); ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi(); initialise_wifi();
xTaskCreate(&http_get_task, "http_get_task", 2048, NULL, 5, NULL); xTaskCreate(&http_get_task, "http_get_task", 2048, NULL, 5, NULL);
} }

View File

@ -325,7 +325,7 @@ static void https_get_task(void *pvParameters)
void app_main() void app_main()
{ {
nvs_flash_init(); ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi(); initialise_wifi();
xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL); xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL);
} }

View File

@ -178,7 +178,7 @@ static void mdns_task(void *pvParameters)
void app_main() void app_main()
{ {
nvs_flash_init(); ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi(); initialise_wifi();
xTaskCreate(&mdns_task, "mdns_task", 2048, NULL, 5, NULL); xTaskCreate(&mdns_task, "mdns_task", 2048, NULL, 5, NULL);
} }

View File

@ -220,6 +220,6 @@ static void wifi_conn_init(void)
void app_main(void) void app_main(void)
{ {
nvs_flash_init(); ESP_ERROR_CHECK( nvs_flash_init() );
wifi_conn_init(); wifi_conn_init();
} }

View File

@ -255,6 +255,6 @@ static void wifi_conn_init(void)
void app_main(void) void app_main(void)
{ {
nvs_flash_init(); ESP_ERROR_CHECK( nvs_flash_init() );
wifi_conn_init(); wifi_conn_init();
} }

View File

@ -93,7 +93,7 @@ void app_main()
static void obtain_time(void) static void obtain_time(void)
{ {
nvs_flash_init(); ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi(); initialise_wifi();
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
false, true, portMAX_DELAY); false, true, portMAX_DELAY);

View File

@ -13,6 +13,7 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_partition.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "nvs.h" #include "nvs.h"
#include "driver/gpio.h" #include "driver/gpio.h"
@ -146,9 +147,17 @@ esp_err_t print_what_saved(void)
void app_main() void app_main()
{ {
nvs_flash_init(); esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
esp_err_t err; // NVS partition was truncated and needs to be erased
const esp_partition_t* nvs_partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
assert(nvs_partition && "partition table must have an NVS partition");
ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
// Retry nvs_flash_init
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
err = print_what_saved(); err = print_what_saved();
if (err != ESP_OK) printf("Error (%d) reading data from NVS!\n", err); if (err != ESP_OK) printf("Error (%d) reading data from NVS!\n", err);

View File

@ -13,23 +13,32 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_partition.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "nvs.h" #include "nvs.h"
void app_main() void app_main()
{ {
nvs_flash_init(); // Initialize NVS
esp_err_t err = nvs_flash_init();
nvs_handle my_handle; if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
esp_err_t err; // NVS partition was truncated and needs to be erased
const esp_partition_t* nvs_partition = esp_partition_find_first(
printf("\n"); ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
assert(nvs_partition && "partition table must have an NVS partition");
ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
// Retry nvs_flash_init
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
// Open // Open
printf("Opening Non-Volatile Storage (NVS) ... "); printf("\n");
printf("Opening Non-Volatile Storage (NVS) handle... ");
nvs_handle my_handle;
err = nvs_open("storage", NVS_READWRITE, &my_handle); err = nvs_open("storage", NVS_READWRITE, &my_handle);
if (err != ESP_OK) { if (err != ESP_OK) {
printf("Error (%d) opening NVS!\n", err); printf("Error (%d) opening NVS handle!\n", err);
} else { } else {
printf("Done\n"); printf("Done\n");

View File

@ -21,6 +21,7 @@
#include "esp_ota_ops.h" #include "esp_ota_ops.h"
#include "esp_partition.h" #include "esp_partition.h"
#include "nvs.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID #define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
@ -279,7 +280,20 @@ void main_task(void *pvParameter)
void app_main() void app_main()
{ {
nvs_flash_init(); // Initialize NVS.
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
// OTA app partition table has a smaller NVS partition size than the non-OTA
// partition table. This size mismatch may cause NVS initialization to fail.
// If this happens, we erase NVS partition and initialize NVS again.
const esp_partition_t* nvs_partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
assert(nvs_partition && "partition table must have an NVS partition");
ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
initialise_wifi(); initialise_wifi();
xTaskCreate(&main_task, "main_task", 8192, NULL, 5, NULL); xTaskCreate(&main_task, "main_task", 8192, NULL, 5, NULL);
} }

View File

@ -148,7 +148,7 @@ static void wpa2_enterprise_task(void *pvParameters)
void app_main() void app_main()
{ {
nvs_flash_init(); ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi(); initialise_wifi();
xTaskCreate(&wpa2_enterprise_task, "wpa2_enterprise_task", 4096, NULL, 5, NULL); xTaskCreate(&wpa2_enterprise_task, "wpa2_enterprise_task", 4096, NULL, 5, NULL);
} }