From 8fc78a958a0d7535831027bba8102c166b4c2155 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 20 Jul 2017 14:25:49 +0800 Subject: [PATCH 1/2] Merge branch 'bugfix/init_nvs_in_bluetooth' into 'master' bt: call nvs_flash_init in examples, show error if NVS is not initialized See merge request !1003 --- components/bt/bluedroid/osi/config.c | 11 +++++++++- components/esp32/phy_init.c | 20 ++++++++----------- components/nvs_flash/include/nvs_flash.h | 13 ++++++++++++ components/nvs_flash/src/nvs_api.cpp | 11 ++++++++++ examples/bluetooth/a2dp_sink/main/main.c | 9 ++++++++- examples/bluetooth/ble_adv/main/app_bt.c | 8 ++++++++ .../bluetooth/blufi/main/blufi_example_main.c | 9 ++++++++- .../main/controller_hci_uart_demo.c | 11 +++++++++- .../bluetooth/gatt_client/main/gattc_demo.c | 10 ++++++++++ .../main/example_ble_sec_gatts_demo.c | 9 +++++++++ .../bluetooth/gatt_server/main/gatts_demo.c | 8 ++++++++ .../main/gatts_table_creat_demo.c | 8 ++++++++ .../nvs_rw_blob/main/nvs_blob_example_main.c | 6 +----- .../main/nvs_value_example_main.c | 6 +----- examples/system/ota/main/ota_example_main.c | 6 +----- examples/wifi/power_save/main/power_save.c | 9 +++++++++ examples/wifi/wps/main/wps.c | 9 +++++++++ 17 files changed, 132 insertions(+), 31 deletions(-) diff --git a/components/bt/bluedroid/osi/config.c b/components/bt/bluedroid/osi/config.c index 7c876b843..dc617f6e5 100644 --- a/components/bt/bluedroid/osi/config.c +++ b/components/bt/bluedroid/osi/config.c @@ -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; } @@ -296,6 +301,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; } diff --git a/components/esp32/phy_init.c b/components/esp32/phy_init.c index 43e9c97ce..5a007ce5d 100644 --- a/components/esp32/phy_init.c +++ b/components/esp32/phy_init.c @@ -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) diff --git a/components/nvs_flash/include/nvs_flash.h b/components/nvs_flash/include/nvs_flash.h index 8307fe352..9220f0d3f 100644 --- a/components/nvs_flash/include/nvs_flash.h +++ b/components/nvs_flash/include/nvs_flash.h @@ -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 diff --git a/components/nvs_flash/src/nvs_api.cpp b/components/nvs_flash/src/nvs_api.cpp index 7c9ec89a6..300ea2894 100644 --- a/components/nvs_flash/src/nvs_api.cpp +++ b/components/nvs_flash/src/nvs_api.cpp @@ -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) diff --git a/examples/bluetooth/a2dp_sink/main/main.c b/examples/bluetooth/a2dp_sink/main/main.c index 09bb92354..3e97b4f3e 100644 --- a/examples/bluetooth/a2dp_sink/main/main.c +++ b/examples/bluetooth/a2dp_sink/main/main.c @@ -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) { diff --git a/examples/bluetooth/ble_adv/main/app_bt.c b/examples/bluetooth/ble_adv/main/app_bt.c index 31e8f2fa0..6d0ef2459 100644 --- a/examples/bluetooth/ble_adv/main/app_bt.c +++ b/examples/bluetooth/ble_adv/main/app_bt.c @@ -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) { diff --git a/examples/bluetooth/blufi/main/blufi_example_main.c b/examples/bluetooth/blufi/main/blufi_example_main.c index 3a91ac790..d349663a6 100644 --- a/examples/bluetooth/blufi/main/blufi_example_main.c +++ b/examples/bluetooth/blufi/main/blufi_example_main.c @@ -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(); diff --git a/examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c b/examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c index 6270596d9..0aa130de8 100644 --- a/examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c +++ b/examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c @@ -14,7 +14,7 @@ #include #include - +#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(); diff --git a/examples/bluetooth/gatt_client/main/gattc_demo.c b/examples/bluetooth/gatt_client/main/gattc_demo.c index 080eb29d3..8d7dcd37c 100644 --- a/examples/bluetooth/gatt_client/main/gattc_demo.c +++ b/examples/bluetooth/gatt_client/main/gattc_demo.c @@ -24,6 +24,8 @@ #include #include #include +#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); diff --git a/examples/bluetooth/gatt_security_server/main/example_ble_sec_gatts_demo.c b/examples/bluetooth/gatt_security_server/main/example_ble_sec_gatts_demo.c index 3bdbb5142..4b708f3ab 100644 --- a/examples/bluetooth/gatt_security_server/main/example_ble_sec_gatts_demo.c +++ b/examples/bluetooth/gatt_security_server/main/example_ble_sec_gatts_demo.c @@ -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) { diff --git a/examples/bluetooth/gatt_server/main/gatts_demo.c b/examples/bluetooth/gatt_server/main/gatts_demo.c index aa6bad6e5..a49e3f81c 100644 --- a/examples/bluetooth/gatt_server/main/gatts_demo.c +++ b/examples/bluetooth/gatt_server/main/gatts_demo.c @@ -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) { diff --git a/examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c b/examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c index 5cfe563b0..facad9fd8 100644 --- a/examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c +++ b/examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c @@ -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) { diff --git a/examples/storage/nvs_rw_blob/main/nvs_blob_example_main.c b/examples/storage/nvs_rw_blob/main/nvs_blob_example_main.c index 6232984f4..ad55e6b0a 100644 --- a/examples/storage/nvs_rw_blob/main/nvs_blob_example_main.c +++ b/examples/storage/nvs_rw_blob/main/nvs_blob_example_main.c @@ -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 ); diff --git a/examples/storage/nvs_rw_value/main/nvs_value_example_main.c b/examples/storage/nvs_rw_value/main/nvs_value_example_main.c index db01a6afc..03205ad4d 100644 --- a/examples/storage/nvs_rw_value/main/nvs_value_example_main.c +++ b/examples/storage/nvs_rw_value/main/nvs_value_example_main.c @@ -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 ); diff --git a/examples/system/ota/main/ota_example_main.c b/examples/system/ota/main/ota_example_main.c index bb931ae0a..d33cccced 100644 --- a/examples/system/ota/main/ota_example_main.c +++ b/examples/system/ota/main/ota_example_main.c @@ -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 ); diff --git a/examples/wifi/power_save/main/power_save.c b/examples/wifi/power_save/main/power_save.c index f9112c444..d03881ff8 100644 --- a/examples/wifi/power_save/main/power_save.c +++ b/examples/wifi/power_save/main/power_save.c @@ -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(); } diff --git a/examples/wifi/wps/main/wps.c b/examples/wifi/wps/main/wps.c index 9f60a00a8..dd4a66c19 100644 --- a/examples/wifi/wps/main/wps.c +++ b/examples/wifi/wps/main/wps.c @@ -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(); } From 1891382db6abc47b98de96e20ae16660e4733093 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 20 Jul 2017 16:40:03 +0800 Subject: [PATCH 2/2] examples: add missing nvs_flash.h include --- .../controller_hci_uart/main/controller_hci_uart_demo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c b/examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c index 0aa130de8..0cce686de 100644 --- a/examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c +++ b/examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c @@ -14,7 +14,7 @@ #include #include -#include "nvs.h" +#include "nvs_flash.h" #include "bt.h" #include "driver/uart.h" #include "esp_log.h"