diff --git a/components/nvs_flash/test/test_nvs.c b/components/nvs_flash/test/test_nvs.c index db97879bc2..07d01db468 100644 --- a/components/nvs_flash/test/test_nvs.c +++ b/components/nvs_flash/test/test_nvs.c @@ -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 +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); diff --git a/examples/bluetooth/blufi/main/blufi_main.c b/examples/bluetooth/blufi/main/blufi_main.c index b46ffb4648..d35ab71214 100644 --- a/examples/bluetooth/blufi/main/blufi_main.c +++ b/examples/bluetooth/blufi/main/blufi_main.c @@ -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(); diff --git a/examples/protocols/coap_client/main/coap_client.c b/examples/protocols/coap_client/main/coap_client.c index 7c0df1afec..a45f748d4a 100644 --- a/examples/protocols/coap_client/main/coap_client.c +++ b/examples/protocols/coap_client/main/coap_client.c @@ -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); } diff --git a/examples/protocols/coap_server/main/coap_server.c b/examples/protocols/coap_server/main/coap_server.c index 75e3296f79..f232853889 100644 --- a/examples/protocols/coap_server/main/coap_server.c +++ b/examples/protocols/coap_server/main/coap_server.c @@ -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); diff --git a/examples/protocols/http_request/main/http_request_main.c b/examples/protocols/http_request/main/http_request_main.c index 3831ae65b9..130729f06f 100644 --- a/examples/protocols/http_request/main/http_request_main.c +++ b/examples/protocols/http_request/main/http_request_main.c @@ -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); } diff --git a/examples/protocols/https_request/main/https_request_main.c b/examples/protocols/https_request/main/https_request_main.c index b953252aac..305b562205 100644 --- a/examples/protocols/https_request/main/https_request_main.c +++ b/examples/protocols/https_request/main/https_request_main.c @@ -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); } diff --git a/examples/protocols/mdns/main/mdns_example_main.c b/examples/protocols/mdns/main/mdns_example_main.c index d19fd1ae68..50b74e08ca 100644 --- a/examples/protocols/mdns/main/mdns_example_main.c +++ b/examples/protocols/mdns/main/mdns_example_main.c @@ -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); } diff --git a/examples/protocols/openssl_client/main/openssl_client.c b/examples/protocols/openssl_client/main/openssl_client.c index 69e16141be..16c9a0efa6 100644 --- a/examples/protocols/openssl_client/main/openssl_client.c +++ b/examples/protocols/openssl_client/main/openssl_client.c @@ -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(); } diff --git a/examples/protocols/openssl_server/main/openssl_server.c b/examples/protocols/openssl_server/main/openssl_server.c index c74bb0e41f..e1d0619d29 100755 --- a/examples/protocols/openssl_server/main/openssl_server.c +++ b/examples/protocols/openssl_server/main/openssl_server.c @@ -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(); } diff --git a/examples/protocols/sntp/main/sntp_main.c b/examples/protocols/sntp/main/sntp_main.c index 438505d7b6..5a29fcac87 100644 --- a/examples/protocols/sntp/main/sntp_main.c +++ b/examples/protocols/sntp/main/sntp_main.c @@ -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); diff --git a/examples/storage/nvs_rw_blob/main/nvs_rw_blob.c b/examples/storage/nvs_rw_blob/main/nvs_rw_blob.c index c0a865eb6a..6232984f44 100644 --- a/examples/storage/nvs_rw_blob/main/nvs_rw_blob.c +++ b/examples/storage/nvs_rw_blob/main/nvs_rw_blob.c @@ -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); diff --git a/examples/storage/nvs_rw_value/main/nvs_rw_value.c b/examples/storage/nvs_rw_value/main/nvs_rw_value.c index 1b3e06b859..db01a6afc4 100644 --- a/examples/storage/nvs_rw_value/main/nvs_rw_value.c +++ b/examples/storage/nvs_rw_value/main/nvs_rw_value.c @@ -13,23 +13,32 @@ #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"); diff --git a/examples/system/ota/main/ota_example.c b/examples/system/ota/main/ota_example.c index 06ed81e638..bc910f70c5 100644 --- a/examples/system/ota/main/ota_example.c +++ b/examples/system/ota/main/ota_example.c @@ -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); } diff --git a/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c b/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c index 7d325c76a0..0932aec931 100644 --- a/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c +++ b/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c @@ -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); }