bt: call nvs_flash_init in examples, show error if NVS is not initialized
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.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user