]> granicus.if.org Git - esp-idf/commitdiff
examples: check return value of nvs_flash_init
authorIvan Grokhotkov <ivan@espressif.com>
Tue, 14 Mar 2017 13:39:44 +0000 (21:39 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Wed, 15 Mar 2017 02:44:54 +0000 (10:44 +0800)
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.

14 files changed:
components/nvs_flash/test/test_nvs.c
examples/bluetooth/blufi/main/blufi_main.c
examples/protocols/coap_client/main/coap_client.c
examples/protocols/coap_server/main/coap_server.c
examples/protocols/http_request/main/http_request_main.c
examples/protocols/https_request/main/https_request_main.c
examples/protocols/mdns/main/mdns_example_main.c
examples/protocols/openssl_client/main/openssl_client.c
examples/protocols/openssl_server/main/openssl_server.c
examples/protocols/sntp/main/sntp_main.c
examples/storage/nvs_rw_blob/main/nvs_rw_blob.c
examples/storage/nvs_rw_value/main/nvs_rw_value.c
examples/system/ota/main/ota_example.c
examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c

index db97879bc29d142f1fdb965794350753635b924b..07d01db468f1d66f3391e470d6d35d2f516fc9ac 100644 (file)
@@ -6,14 +6,26 @@
 #include "unity.h"
 #include "nvs.h"
 #include "nvs_flash.h"
-#include "esp_spi_flash.h"
+#include "esp_partition.h"
+#include "esp_log.h"
 #include <string.h>
 
+static const char* TAG = "test_nvs";
 
 TEST_CASE("various nvs tests", "[nvs]")
 {
     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_set_i32(handle_1, "foo", 0x12345678), ESP_ERR_NVS_INVALID_HANDLE);
index b46ffb46484dba0170e60b747deaf52eadf2284b..d35ab71214cf0d05de77a65c8f7945941db56e28 100644 (file)
@@ -312,7 +312,7 @@ void app_main()
 {
     esp_err_t ret;
 
-    nvs_flash_init();
+    ESP_ERROR_CHECK( nvs_flash_init() );
     initialise_wifi();
 
     esp_bt_controller_init();
index 7c0df1afec8f66aeaaa31ad7c4ef588a6397c1f1..a45f748d4a079d44d1166c9d974932e951ef0829 100644 (file)
@@ -199,7 +199,7 @@ static void wifi_conn_init(void)
 
 void app_main(void)
 {
-    nvs_flash_init();
+    ESP_ERROR_CHECK( nvs_flash_init() );
     wifi_conn_init();
     xTaskCreate(coap_demo_thread, "coap", 2048, NULL, 5, NULL);
 }
index 75e3296f79a23b293ea7cfd6a38c996400d1e377..f232853889318575d31982db43ebdd11d147c763 100644 (file)
@@ -185,7 +185,7 @@ static void wifi_conn_init(void)
 
 void app_main(void)
 {
-    nvs_flash_init();
+    ESP_ERROR_CHECK( nvs_flash_init() );
     wifi_conn_init();
 
     xTaskCreate(coap_demo_thread, "coap", 2048, NULL, 5, NULL);
index 3831ae65b9d61f9a1e8420999f09580dc1562ce4..130729f06fa4bd2707480a1cb4000cdbaa730776 100644 (file)
@@ -174,7 +174,7 @@ static void http_get_task(void *pvParameters)
 
 void app_main()
 {
-    nvs_flash_init();
+    ESP_ERROR_CHECK( nvs_flash_init() );
     initialise_wifi();
     xTaskCreate(&http_get_task, "http_get_task", 2048, NULL, 5, NULL);
 }
index b953252aac70b15d964f0893b454fb19f55f90af..305b562205b0d1d697fc890d0dd337f183088af3 100644 (file)
@@ -325,7 +325,7 @@ static void https_get_task(void *pvParameters)
 
 void app_main()
 {
-    nvs_flash_init();
+    ESP_ERROR_CHECK( nvs_flash_init() );
     initialise_wifi();
     xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL);
 }
index d19fd1ae68aa9cf13b07e4cc29a2042b67815add..50b74e08ca20a4628237919d3fd8d27b89d42950 100644 (file)
@@ -178,7 +178,7 @@ static void mdns_task(void *pvParameters)
 
 void app_main()
 {
-    nvs_flash_init();
+    ESP_ERROR_CHECK( nvs_flash_init() );
     initialise_wifi();
     xTaskCreate(&mdns_task, "mdns_task", 2048, NULL, 5, NULL);
 }
index 69e16141be0b82a7ca6988fa47cfec7eb930dc8c..16c9a0efa6d7a8f38df9ccce78bf8a2bb89db752 100644 (file)
@@ -220,6 +220,6 @@ static void wifi_conn_init(void)
 
 void app_main(void)
 {
-    nvs_flash_init();
+    ESP_ERROR_CHECK( nvs_flash_init() );
     wifi_conn_init();
 }
index c74bb0e41f67fb072902a011b55577e7814434aa..e1d0619d29b992562baed107bca2dd2f2b6a3e4d 100755 (executable)
@@ -255,6 +255,6 @@ static void wifi_conn_init(void)
 
 void app_main(void)
 {
-    nvs_flash_init();
+    ESP_ERROR_CHECK( nvs_flash_init() );
     wifi_conn_init();
 }
index 438505d7b6be70675559bab24cc59768db3d4cfb..5a29fcac87a82c46195dd49f2396e6a1cb4c1b32 100644 (file)
@@ -93,7 +93,7 @@ void app_main()
 
 static void obtain_time(void)
 {
-    nvs_flash_init();
+    ESP_ERROR_CHECK( nvs_flash_init() );
     initialise_wifi();
     xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
                         false, true, portMAX_DELAY);
index c0a865eb6ad5f6a924e96a60bd63bb1ad635772e..6232984f44fcefeb276128b8bd013268d80efdde 100644 (file)
@@ -13,6 +13,7 @@
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 #include "esp_system.h"
+#include "esp_partition.h"
 #include "nvs_flash.h"
 #include "nvs.h"
 #include "driver/gpio.h"
@@ -146,9 +147,17 @@ esp_err_t print_what_saved(void)
 
 void app_main()
 {
-    nvs_flash_init();
-
-    esp_err_t err;
+    esp_err_t err = nvs_flash_init();
+    if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
+        // 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();
     if (err != ESP_OK) printf("Error (%d) reading data from NVS!\n", err);
index 1b3e06b859890d45c4a9c86141399b3439fb8dc7..db01a6afc42b0d59e5e6d4ad9e2ad7067bafe427 100644 (file)
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 #include "esp_system.h"
+#include "esp_partition.h"
 #include "nvs_flash.h"
 #include "nvs.h"
 
 void app_main()
 {
-    nvs_flash_init();
-
-    nvs_handle my_handle;
-    esp_err_t err;
-
-    printf("\n");
+    // Initialize NVS
+    esp_err_t err = nvs_flash_init();
+    if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
+        // 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 );
 
     // 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);
     if (err != ESP_OK) {
-        printf("Error (%d) opening NVS!\n", err);
+        printf("Error (%d) opening NVS handle!\n", err);
     } else {
         printf("Done\n");
 
index 06ed81e63865387d7ec08f1f4804e68a526ce92a..bc910f70c5d5bde8bedb9f08efdebc898f5ee939 100644 (file)
@@ -21,6 +21,7 @@
 #include "esp_ota_ops.h"
 #include "esp_partition.h"
 
+#include "nvs.h"
 #include "nvs_flash.h"
 
 #define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
@@ -279,7 +280,20 @@ void main_task(void *pvParameter)
 
 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();
     xTaskCreate(&main_task, "main_task", 8192, NULL, 5, NULL);
 }
index 7d325c76a08c14077aec8fd344b946df43c13c2f..0932aec931256162b65cd5d71dd9507cd2e3d5f6 100644 (file)
@@ -148,7 +148,7 @@ static void wpa2_enterprise_task(void *pvParameters)
 
 void app_main()
 {
-    nvs_flash_init();
+    ESP_ERROR_CHECK( nvs_flash_init() );
     initialise_wifi();
     xTaskCreate(&wpa2_enterprise_task, "wpa2_enterprise_task", 4096, NULL, 5, NULL);
 }