]> granicus.if.org Git - esp-idf/commitdiff
bt: call nvs_flash_init in examples, show error if NVS is not initialized
authorIvan Grokhotkov <ivan@espressif.com>
Thu, 13 Jul 2017 15:46:19 +0000 (23:46 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Mon, 17 Jul 2017 13:29:50 +0000 (21:29 +0800)
NVS is used to store PHY calibration data, WiFi configuration, and BT
configuration. Previously BT examples did not call nvs_flash_init,
relying on the fact that it is called during PHY init. However PHY init
did not handle possible NVS initialization errors.

This change moves PHY init procedure into the application, and adds
diagnostic messages to BT config management routines if NVS is not
initialized.

17 files changed:
components/bt/bluedroid/osi/config.c
components/esp32/phy_init.c
components/nvs_flash/include/nvs_flash.h
components/nvs_flash/src/nvs_api.cpp
examples/bluetooth/a2dp_sink/main/main.c
examples/bluetooth/ble_adv/main/app_bt.c
examples/bluetooth/blufi/main/blufi_example_main.c
examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c
examples/bluetooth/gatt_client/main/gattc_demo.c
examples/bluetooth/gatt_security_server/main/example_ble_sec_gatts_demo.c
examples/bluetooth/gatt_server/main/gatts_demo.c
examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c
examples/storage/nvs_rw_blob/main/nvs_blob_example_main.c
examples/storage/nvs_rw_value/main/nvs_value_example_main.c
examples/system/ota/main/ota_example_main.c
examples/wifi/power_save/main/power_save.c
examples/wifi/wps/main/wps.c

index 38281ed9dab3d285a7c78f0c5250bf82295e74d6..99b30219ee80cc1d262824f3171ce01fa9e4a670 100644 (file)
@@ -91,7 +91,12 @@ config_t *config_new(const char *filename)
     nvs_handle fp;
     err = nvs_open(filename, NVS_READWRITE, &fp);
     if (err != ESP_OK) {
-        LOG_ERROR("%s unable to open file '%s'\n", __func__, filename);
+        if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
+            LOG_ERROR("%s: NVS not initialized. "
+                      "Call nvs_flash_init before initializing bluetooth.", __func__);
+        } else {
+            LOG_ERROR("%s unable to open NVS namespace '%s'\n", __func__, filename);
+        }
         config_free(config);
         return NULL;
     }
@@ -316,6 +321,10 @@ bool config_save(const config_t *config, const char *filename)
 
     err = nvs_open(filename, NVS_READWRITE, &fp);
     if (err != ESP_OK) {
+        if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
+            LOG_ERROR("%s: NVS not initialized. "
+                      "Call nvs_flash_init before initializing bluetooth.", __func__);
+        }
         err_code |= 0x02;
         goto error;
     }
index 43e9c97ce5ac15b9e7d7292633490bbe042354a1..5a007ce5ddd68a148808674101ec06b12475a640 100644 (file)
@@ -159,22 +159,18 @@ static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
 
 esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_data)
 {
-    esp_err_t err = nvs_flash_init();
-    if (err != ESP_OK) {
-        ESP_LOGW(TAG, "%s: failed to initialize NVS (0x%x)", __func__, err);
-        return err;
-    }
     nvs_handle handle;
-    err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
-    if (err != ESP_OK) {
+    esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
+    if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
+        ESP_LOGE(TAG, "%s: NVS has not been initialized. "
+                "Call nvs_flash_init before starting WiFi/BT.", __func__);
+    } else if (err != ESP_OK) {
         ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
         return err;
     }
-    else {
-        err = load_cal_data_from_nvs_handle(handle, out_cal_data);
-        nvs_close(handle);
-        return err;
-    }
+    err = load_cal_data_from_nvs_handle(handle, out_cal_data);
+    nvs_close(handle);
+    return err;
 }
 
 esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data)
index 8307fe3521ce59143885c3e0c475a7d83e2b80a8..9220f0d3ff5d39a4f0f0afc8e60e416d1fddc693 100644 (file)
@@ -18,6 +18,8 @@
 extern "C" {
 #endif
 
+#include "nvs.h"
+
 /**
  * @brief Initialize NVS flash storage with layout given in the partition table.
  *
@@ -30,6 +32,17 @@ extern "C" {
 esp_err_t nvs_flash_init(void);
 
 
+/**
+ * @brief Erase NVS partition
+ *
+ * This function erases all contents of NVS partition
+ *
+ * @return
+ *      - ESP_OK on success
+ *      - ESP_ERR_NOT_FOUND if there is no NVS partition in the partition table
+ */
+esp_err_t nvs_flash_erase(void);
+
 #ifdef __cplusplus
 }
 #endif
index 7c9ec89a6f43ec2ac8dfebe74448f4ae068e21af..300ea289421fd242111c6b8836b993237855c69c 100644 (file)
@@ -86,6 +86,17 @@ extern "C" esp_err_t nvs_flash_init(void)
     return nvs_flash_init_custom(partition->address / SPI_FLASH_SEC_SIZE,
             partition->size / SPI_FLASH_SEC_SIZE);
 }
+
+extern "C" esp_err_t nvs_flash_erase()
+{
+    const esp_partition_t* partition = esp_partition_find_first(
+            ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
+    if (partition == NULL) {
+        return ESP_ERR_NOT_FOUND;
+    }
+
+    return esp_partition_erase_range(partition, 0, partition->size);
+}
 #endif
 
 static esp_err_t nvs_find_ns_handle(nvs_handle handle, HandleEntry& entry)
index 09bb9235425a555a166429a19cf656ff4d03f38f..3e97b4f3ec642e3ba9563ea971d0e993a504db14 100644 (file)
@@ -43,7 +43,14 @@ static void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
 
 void app_main()
 {
-    nvs_flash_init();
+    /* Initialize NVS — it is used to store PHY calibration data */
+    esp_err_t ret = nvs_flash_init();
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
+        ESP_ERROR_CHECK(nvs_flash_erase());
+        ret = nvs_flash_init();
+    }
+    ESP_ERROR_CHECK( ret );
+
 
     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
     if (esp_bt_controller_init(&bt_cfg) != ESP_OK) {
index 31e8f2fa00aa676f7fa37a6387f44f5d427be4d8..6d0ef24595230c58d6775cc87eeb698ec109284e 100644 (file)
@@ -18,6 +18,7 @@
 #include "freertos/task.h"
 #include "bt.h"
 #include "esp_log.h"
+#include "nvs_flash.h"
 
 static const char *tag = "BLE_ADV";
 
@@ -217,6 +218,13 @@ void bleAdvtTask(void *pvParameters)
 
 void app_main()
 {
+    /* Initialize NVS — it is used to store PHY calibration data */
+    esp_err_t ret = nvs_flash_init();
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
+        ESP_ERROR_CHECK(nvs_flash_erase());
+        ret = nvs_flash_init();
+    }
+    ESP_ERROR_CHECK( ret );
     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
     
     if (esp_bt_controller_init(&bt_cfg) != ESP_OK) {
index 3a91ac7905b35572bdc528f7f8dfb29c326abf8c..d349663a621074057b060aef4482b96404bcda49 100644 (file)
@@ -321,7 +321,14 @@ void app_main()
 {
     esp_err_t ret;
 
-    ESP_ERROR_CHECK( nvs_flash_init() );
+    // Initialize NVS
+    ret = nvs_flash_init();
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
+        ESP_ERROR_CHECK(nvs_flash_erase());
+        ret = nvs_flash_init();
+    }
+    ESP_ERROR_CHECK( ret );
+
     initialise_wifi();
 
     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
index 6270596d92b71c85acb401283846f89e2f57eaa2..0aa130de8fd666a282a55a3170fe6a8385694623 100644 (file)
@@ -14,7 +14,7 @@
 
 #include <stdio.h>
 #include <string.h>
-
+#include "nvs.h"
 #include "bt.h"
 #include "driver/uart.h"
 #include "esp_log.h"
@@ -34,6 +34,15 @@ void app_main()
 { 
     esp_err_t ret;
 
+    /* Initialize NVS — it is used to store PHY calibration data */
+    ret = nvs_flash_init();
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
+        ESP_ERROR_CHECK(nvs_flash_erase());
+        ret = nvs_flash_init();
+    }
+    ESP_ERROR_CHECK( ret );
+
+
     /* As the UART1/2 pin conflict with flash pin, so do matrix of the signal and pin */
     uart_gpio_reset();
 
index 080eb29d3a8f7211a621b2136b2bf0cc40cff061..8d7dcd37c285aacec98b208715f1b1235be381c9 100644 (file)
@@ -24,6 +24,8 @@
 #include <string.h>
 #include <stdbool.h>
 #include <stdio.h>
+#include "nvs.h"
+#include "nvs_flash.h"
 #include "controller.h"
 
 #include "bt.h"
@@ -409,6 +411,14 @@ void gattc_client_test(void)
 
 void app_main()
 {
+    // Initialize NVS.
+    esp_err_t ret = nvs_flash_init();
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
+        ESP_ERROR_CHECK(nvs_flash_erase());
+        ret = nvs_flash_init();
+    }
+    ESP_ERROR_CHECK( ret );
+
     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
     esp_bt_controller_init(&bt_cfg);
     esp_bt_controller_enable(ESP_BT_MODE_BTDM);
index 3bdbb514288ceb13aa377a73948fb7590a8fdf0e..4b708f3ab21e3068a4a5efc32083869afc1b9711 100644 (file)
@@ -366,6 +366,15 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
 void app_main()
 {
     esp_err_t ret;
+
+    // Initialize NVS.
+    ret = nvs_flash_init();
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
+        ESP_ERROR_CHECK(nvs_flash_erase());
+        ret = nvs_flash_init();
+    }
+    ESP_ERROR_CHECK( ret );
+
     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
     ret = esp_bt_controller_init(&bt_cfg);
     if (ret) {
index aa6bad6e5ccf50fcd41b691589010cd5d59c0256..a49e3f81c6d9d5d6e21b2d69fda9f669c730d6ea 100644 (file)
@@ -503,6 +503,14 @@ void app_main()
 {
     esp_err_t ret;
 
+    // Initialize NVS.
+    ret = nvs_flash_init();
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
+        ESP_ERROR_CHECK(nvs_flash_erase());
+        ret = nvs_flash_init();
+    }
+    ESP_ERROR_CHECK( ret );
+
     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
     ret = esp_bt_controller_init(&bt_cfg);
     if (ret) {
index 5cfe563b016d5861bd8ea548ffe3cb5604ba10b9..facad9fd82355b3c076d106bde483f9ff3d19c0b 100644 (file)
@@ -318,6 +318,14 @@ void app_main()
 {
     esp_err_t ret;
 
+    // Initialize NVS.
+    ret = nvs_flash_init();
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
+        ESP_ERROR_CHECK(nvs_flash_erase());
+        ret = nvs_flash_init();
+    }
+    ESP_ERROR_CHECK( ret );
+
     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
     ret = esp_bt_controller_init(&bt_cfg);
     if (ret) {
index 6232984f44fcefeb276128b8bd013268d80efdde..ad55e6b0a6845427e3d2f83658da233ac3dd10eb 100644 (file)
@@ -13,7 +13,6 @@
 #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"
@@ -150,11 +149,8 @@ void app_main()
     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
+        ESP_ERROR_CHECK(nvs_flash_erase());
         err = nvs_flash_init();
     }
     ESP_ERROR_CHECK( err );
index db01a6afc42b0d59e5e6d4ad9e2ad7067bafe427..03205ad4daa889bb177670fd26d8f0cab0a4702e 100644 (file)
@@ -13,7 +13,6 @@
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 #include "esp_system.h"
-#include "esp_partition.h"
 #include "nvs_flash.h"
 #include "nvs.h"
 
@@ -23,11 +22,8 @@ void app_main()
     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
+        ESP_ERROR_CHECK(nvs_flash_erase());
         err = nvs_flash_init();
     }
     ESP_ERROR_CHECK( err );
index bb931ae0afec480f51b025480d83e47cee8f2ba1..d33cccced77048cf63f3bd94b32ba281722c5411 100644 (file)
@@ -19,7 +19,6 @@
 #include "esp_event_loop.h"
 #include "esp_log.h"
 #include "esp_ota_ops.h"
-#include "esp_partition.h"
 
 #include "nvs.h"
 #include "nvs_flash.h"
@@ -286,10 +285,7 @@ void app_main()
         // 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) );
+        ESP_ERROR_CHECK(nvs_flash_erase());
         err = nvs_flash_init();
     }
     ESP_ERROR_CHECK( err );
index f9112c44469588bd7e8b850c0fed4f7d43393dfb..d03881ff80805966e42fa54f14d156637eb800a2 100644 (file)
@@ -17,6 +17,7 @@
 #include "esp_wifi.h"
 #include "esp_log.h"
 #include "esp_event_loop.h"
+#include "nvs_flash.h"
 
 /*set the ssid and password via "make menuconfig"*/
 #define DEFAULT_SSID CONFIG_WIFI_SSID
@@ -79,5 +80,13 @@ static void wifi_power_save(void)
 
 void app_main()
 {
+    // Initialize NVS
+    esp_err_t ret = nvs_flash_init();
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
+        ESP_ERROR_CHECK(nvs_flash_erase());
+        ret = nvs_flash_init();
+    }
+    ESP_ERROR_CHECK( ret );
+
     wifi_power_save();
 }
index 9f60a00a861646332862a4d4ffc84ca86144abdd..dd4a66c195ae4019269d79c6c704ecb00ea10860 100644 (file)
@@ -20,6 +20,7 @@
 #include "esp_log.h"
 #include "esp_wps.h"
 #include "esp_event_loop.h"
+#include "nvs_flash.h"
 
 
 /*set wps mode via "make menuconfig"*/
@@ -106,5 +107,13 @@ static void start_wps(void)
 
 void app_main()
 {
+    /* Initialize NVS — it is used to store PHY calibration data */
+    esp_err_t ret = nvs_flash_init();
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
+        ESP_ERROR_CHECK(nvs_flash_erase());
+        ret = nvs_flash_init();
+    }
+    ESP_ERROR_CHECK( ret );
+
     start_wps();
 }