Skip to content

Commit efdbc63

Browse files
committed
Merge branch 'cherry-pick-292c2abd' into 'release/v2.1'
Merge branch 'bugfix/init_nvs_in_bluetooth' into 'master' See merge request !1031
2 parents 16cd517 + 1891382 commit efdbc63

File tree

17 files changed

+132
-31
lines changed

17 files changed

+132
-31
lines changed

components/bt/bluedroid/osi/config.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ config_t *config_new(const char *filename)
9191
nvs_handle fp;
9292
err = nvs_open(filename, NVS_READWRITE, &fp);
9393
if (err != ESP_OK) {
94-
LOG_ERROR("%s unable to open file '%s'\n", __func__, filename);
94+
if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
95+
LOG_ERROR("%s: NVS not initialized. "
96+
"Call nvs_flash_init before initializing bluetooth.", __func__);
97+
} else {
98+
LOG_ERROR("%s unable to open NVS namespace '%s'\n", __func__, filename);
99+
}
95100
config_free(config);
96101
return NULL;
97102
}
@@ -296,6 +301,10 @@ bool config_save(const config_t *config, const char *filename)
296301

297302
err = nvs_open(filename, NVS_READWRITE, &fp);
298303
if (err != ESP_OK) {
304+
if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
305+
LOG_ERROR("%s: NVS not initialized. "
306+
"Call nvs_flash_init before initializing bluetooth.", __func__);
307+
}
299308
err_code |= 0x02;
300309
goto error;
301310
}

components/esp32/phy_init.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,22 +159,18 @@ static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
159159

160160
esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_data)
161161
{
162-
esp_err_t err = nvs_flash_init();
163-
if (err != ESP_OK) {
164-
ESP_LOGW(TAG, "%s: failed to initialize NVS (0x%x)", __func__, err);
165-
return err;
166-
}
167162
nvs_handle handle;
168-
err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
169-
if (err != ESP_OK) {
163+
esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
164+
if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
165+
ESP_LOGE(TAG, "%s: NVS has not been initialized. "
166+
"Call nvs_flash_init before starting WiFi/BT.", __func__);
167+
} else if (err != ESP_OK) {
170168
ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
171169
return err;
172170
}
173-
else {
174-
err = load_cal_data_from_nvs_handle(handle, out_cal_data);
175-
nvs_close(handle);
176-
return err;
177-
}
171+
err = load_cal_data_from_nvs_handle(handle, out_cal_data);
172+
nvs_close(handle);
173+
return err;
178174
}
179175

180176
esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data)

components/nvs_flash/include/nvs_flash.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
extern "C" {
1919
#endif
2020

21+
#include "nvs.h"
22+
2123
/**
2224
* @brief Initialize NVS flash storage with layout given in the partition table.
2325
*
@@ -30,6 +32,17 @@ extern "C" {
3032
esp_err_t nvs_flash_init(void);
3133

3234

35+
/**
36+
* @brief Erase NVS partition
37+
*
38+
* This function erases all contents of NVS partition
39+
*
40+
* @return
41+
* - ESP_OK on success
42+
* - ESP_ERR_NOT_FOUND if there is no NVS partition in the partition table
43+
*/
44+
esp_err_t nvs_flash_erase(void);
45+
3346
#ifdef __cplusplus
3447
}
3548
#endif

components/nvs_flash/src/nvs_api.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ extern "C" esp_err_t nvs_flash_init(void)
8686
return nvs_flash_init_custom(partition->address / SPI_FLASH_SEC_SIZE,
8787
partition->size / SPI_FLASH_SEC_SIZE);
8888
}
89+
90+
extern "C" esp_err_t nvs_flash_erase()
91+
{
92+
const esp_partition_t* partition = esp_partition_find_first(
93+
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
94+
if (partition == NULL) {
95+
return ESP_ERR_NOT_FOUND;
96+
}
97+
98+
return esp_partition_erase_range(partition, 0, partition->size);
99+
}
89100
#endif
90101

91102
static esp_err_t nvs_find_ns_handle(nvs_handle handle, HandleEntry& entry)

examples/bluetooth/a2dp_sink/main/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ static void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
4343

4444
void app_main()
4545
{
46-
nvs_flash_init();
46+
/* Initialize NVS — it is used to store PHY calibration data */
47+
esp_err_t ret = nvs_flash_init();
48+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
49+
ESP_ERROR_CHECK(nvs_flash_erase());
50+
ret = nvs_flash_init();
51+
}
52+
ESP_ERROR_CHECK( ret );
53+
4754

4855
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
4956
if (esp_bt_controller_init(&bt_cfg) != ESP_OK) {

examples/bluetooth/ble_adv/main/app_bt.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "freertos/task.h"
1919
#include "bt.h"
2020
#include "esp_log.h"
21+
#include "nvs_flash.h"
2122

2223
static const char *tag = "BLE_ADV";
2324

@@ -217,6 +218,13 @@ void bleAdvtTask(void *pvParameters)
217218

218219
void app_main()
219220
{
221+
/* Initialize NVS — it is used to store PHY calibration data */
222+
esp_err_t ret = nvs_flash_init();
223+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
224+
ESP_ERROR_CHECK(nvs_flash_erase());
225+
ret = nvs_flash_init();
226+
}
227+
ESP_ERROR_CHECK( ret );
220228
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
221229

222230
if (esp_bt_controller_init(&bt_cfg) != ESP_OK) {

examples/bluetooth/blufi/main/blufi_example_main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,14 @@ void app_main()
321321
{
322322
esp_err_t ret;
323323

324-
ESP_ERROR_CHECK( nvs_flash_init() );
324+
// Initialize NVS
325+
ret = nvs_flash_init();
326+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
327+
ESP_ERROR_CHECK(nvs_flash_erase());
328+
ret = nvs_flash_init();
329+
}
330+
ESP_ERROR_CHECK( ret );
331+
325332
initialise_wifi();
326333

327334
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();

examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#include <stdio.h>
1616
#include <string.h>
17-
17+
#include "nvs_flash.h"
1818
#include "bt.h"
1919
#include "driver/uart.h"
2020
#include "esp_log.h"
@@ -34,6 +34,15 @@ void app_main()
3434
{
3535
esp_err_t ret;
3636

37+
/* Initialize NVS — it is used to store PHY calibration data */
38+
ret = nvs_flash_init();
39+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
40+
ESP_ERROR_CHECK(nvs_flash_erase());
41+
ret = nvs_flash_init();
42+
}
43+
ESP_ERROR_CHECK( ret );
44+
45+
3746
/* As the UART1/2 pin conflict with flash pin, so do matrix of the signal and pin */
3847
uart_gpio_reset();
3948

examples/bluetooth/gatt_client/main/gattc_demo.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <string.h>
2525
#include <stdbool.h>
2626
#include <stdio.h>
27+
#include "nvs.h"
28+
#include "nvs_flash.h"
2729
#include "controller.h"
2830

2931
#include "bt.h"
@@ -409,6 +411,14 @@ void gattc_client_test(void)
409411

410412
void app_main()
411413
{
414+
// Initialize NVS.
415+
esp_err_t ret = nvs_flash_init();
416+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
417+
ESP_ERROR_CHECK(nvs_flash_erase());
418+
ret = nvs_flash_init();
419+
}
420+
ESP_ERROR_CHECK( ret );
421+
412422
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
413423
esp_bt_controller_init(&bt_cfg);
414424
esp_bt_controller_enable(ESP_BT_MODE_BTDM);

examples/bluetooth/gatt_security_server/main/example_ble_sec_gatts_demo.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,15 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
366366
void app_main()
367367
{
368368
esp_err_t ret;
369+
370+
// Initialize NVS.
371+
ret = nvs_flash_init();
372+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
373+
ESP_ERROR_CHECK(nvs_flash_erase());
374+
ret = nvs_flash_init();
375+
}
376+
ESP_ERROR_CHECK( ret );
377+
369378
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
370379
ret = esp_bt_controller_init(&bt_cfg);
371380
if (ret) {

examples/bluetooth/gatt_server/main/gatts_demo.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,14 @@ void app_main()
503503
{
504504
esp_err_t ret;
505505

506+
// Initialize NVS.
507+
ret = nvs_flash_init();
508+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
509+
ESP_ERROR_CHECK(nvs_flash_erase());
510+
ret = nvs_flash_init();
511+
}
512+
ESP_ERROR_CHECK( ret );
513+
506514
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
507515
ret = esp_bt_controller_init(&bt_cfg);
508516
if (ret) {

examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,14 @@ void app_main()
318318
{
319319
esp_err_t ret;
320320

321+
// Initialize NVS.
322+
ret = nvs_flash_init();
323+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
324+
ESP_ERROR_CHECK(nvs_flash_erase());
325+
ret = nvs_flash_init();
326+
}
327+
ESP_ERROR_CHECK( ret );
328+
321329
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
322330
ret = esp_bt_controller_init(&bt_cfg);
323331
if (ret) {

examples/storage/nvs_rw_blob/main/nvs_blob_example_main.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "freertos/FreeRTOS.h"
1414
#include "freertos/task.h"
1515
#include "esp_system.h"
16-
#include "esp_partition.h"
1716
#include "nvs_flash.h"
1817
#include "nvs.h"
1918
#include "driver/gpio.h"
@@ -150,11 +149,8 @@ void app_main()
150149
esp_err_t err = nvs_flash_init();
151150
if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
152151
// NVS partition was truncated and needs to be erased
153-
const esp_partition_t* nvs_partition = esp_partition_find_first(
154-
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
155-
assert(nvs_partition && "partition table must have an NVS partition");
156-
ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
157152
// Retry nvs_flash_init
153+
ESP_ERROR_CHECK(nvs_flash_erase());
158154
err = nvs_flash_init();
159155
}
160156
ESP_ERROR_CHECK( err );

examples/storage/nvs_rw_value/main/nvs_value_example_main.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "freertos/FreeRTOS.h"
1414
#include "freertos/task.h"
1515
#include "esp_system.h"
16-
#include "esp_partition.h"
1716
#include "nvs_flash.h"
1817
#include "nvs.h"
1918

@@ -23,11 +22,8 @@ void app_main()
2322
esp_err_t err = nvs_flash_init();
2423
if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
2524
// NVS partition was truncated and needs to be erased
26-
const esp_partition_t* nvs_partition = esp_partition_find_first(
27-
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
28-
assert(nvs_partition && "partition table must have an NVS partition");
29-
ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
3025
// Retry nvs_flash_init
26+
ESP_ERROR_CHECK(nvs_flash_erase());
3127
err = nvs_flash_init();
3228
}
3329
ESP_ERROR_CHECK( err );

examples/system/ota/main/ota_example_main.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "esp_event_loop.h"
2020
#include "esp_log.h"
2121
#include "esp_ota_ops.h"
22-
#include "esp_partition.h"
2322

2423
#include "nvs.h"
2524
#include "nvs_flash.h"
@@ -286,10 +285,7 @@ void app_main()
286285
// OTA app partition table has a smaller NVS partition size than the non-OTA
287286
// partition table. This size mismatch may cause NVS initialization to fail.
288287
// If this happens, we erase NVS partition and initialize NVS again.
289-
const esp_partition_t* nvs_partition = esp_partition_find_first(
290-
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
291-
assert(nvs_partition && "partition table must have an NVS partition");
292-
ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
288+
ESP_ERROR_CHECK(nvs_flash_erase());
293289
err = nvs_flash_init();
294290
}
295291
ESP_ERROR_CHECK( err );

examples/wifi/power_save/main/power_save.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "esp_wifi.h"
1818
#include "esp_log.h"
1919
#include "esp_event_loop.h"
20+
#include "nvs_flash.h"
2021

2122
/*set the ssid and password via "make menuconfig"*/
2223
#define DEFAULT_SSID CONFIG_WIFI_SSID
@@ -79,5 +80,13 @@ static void wifi_power_save(void)
7980

8081
void app_main()
8182
{
83+
// Initialize NVS
84+
esp_err_t ret = nvs_flash_init();
85+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
86+
ESP_ERROR_CHECK(nvs_flash_erase());
87+
ret = nvs_flash_init();
88+
}
89+
ESP_ERROR_CHECK( ret );
90+
8291
wifi_power_save();
8392
}

examples/wifi/wps/main/wps.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "esp_log.h"
2121
#include "esp_wps.h"
2222
#include "esp_event_loop.h"
23+
#include "nvs_flash.h"
2324

2425

2526
/*set wps mode via "make menuconfig"*/
@@ -106,5 +107,13 @@ static void start_wps(void)
106107

107108
void app_main()
108109
{
110+
/* Initialize NVS — it is used to store PHY calibration data */
111+
esp_err_t ret = nvs_flash_init();
112+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
113+
ESP_ERROR_CHECK(nvs_flash_erase());
114+
ret = nvs_flash_init();
115+
}
116+
ESP_ERROR_CHECK( ret );
117+
109118
start_wps();
110119
}

0 commit comments

Comments
 (0)