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.
pull/264/merge
Ivan Grokhotkov 2017-03-14 21:39:44 +08:00
rodzic f59f13efd5
commit 4813ab2d28
14 zmienionych plików z 68 dodań i 24 usunięć

Wyświetl plik

@ -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);

Wyświetl plik

@ -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();

Wyświetl plik

@ -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);
}

Wyświetl plik

@ -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);

Wyświetl plik

@ -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);
}

Wyświetl plik

@ -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);
}

Wyświetl plik

@ -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);
}

Wyświetl plik

@ -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();
}

Wyświetl plik

@ -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();
}

Wyświetl plik

@ -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);

Wyświetl plik

@ -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);

Wyświetl plik

@ -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");

Wyświetl plik

@ -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);
}

Wyświetl plik

@ -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);
}