diff --git a/components/bt/bluedroid/api/esp_bt_main.c b/components/bt/bluedroid/api/esp_bt_main.c index f96cae1b3..612a9e020 100644 --- a/components/bt/bluedroid/api/esp_bt_main.c +++ b/components/bt/bluedroid/api/esp_bt_main.c @@ -17,12 +17,11 @@ #include "btc_task.h" #include "btc_main.h" #include "future.h" +#include "esp_phy_init.h" static bool esp_already_enable = false; static bool esp_already_init = false; -extern esp_err_t esp_phy_deinit(void); - esp_bluedroid_status_t esp_bluedroid_get_status(void) { if (esp_already_init) { @@ -166,7 +165,7 @@ esp_err_t esp_bluedroid_deinit(void) esp_already_init = false; - esp_phy_deinit(); + esp_phy_rf_deinit(); return ESP_OK; } diff --git a/components/bt/bt.c b/components/bt/bt.c index 4378b4da6..2390456af 100644 --- a/components/bt/bt.c +++ b/components/bt/bt.c @@ -27,12 +27,11 @@ #include "esp_task.h" #include "esp_intr.h" #include "esp_attr.h" +#include "esp_phy_init.h" #include "bt.h" #if CONFIG_BT_ENABLED -extern void do_phy_init(void); - /* not for user call, so don't put to include file */ extern void btdm_osi_funcs_register(void *osi_funcs); extern void btdm_controller_init(void); @@ -148,7 +147,7 @@ static void bt_controller_task(void *pvParam) { btdm_osi_funcs_register(&osi_funcs); - do_phy_init(); + esp_phy_load_cal_and_init(); btdm_controller_init(); } diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index 5338e5f45..d3441fa01 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -507,6 +507,16 @@ config PHY_ENABLED menu PHY visible if PHY_ENABLED + +config ESP32_STORE_PHY_CAL_DATA_INTO_NVS + bool "Store PHY calibration data into NVS" + depends on PHY_ENABLED + default y + help + If this option is enabled, PHY initialization will also calibrate PHY data, and + store it into NVS. + + If unsure, choose 'y'. config ESP32_PHY_INIT_DATA_IN_PARTITION bool "Use a partition to store PHY init data" diff --git a/components/esp32/include/esp_phy_init.h b/components/esp32/include/esp_phy_init.h index 55ce145e9..5ad06ee89 100644 --- a/components/esp32/include/esp_phy_init.h +++ b/components/esp32/include/esp_phy_init.h @@ -192,7 +192,7 @@ void esp_phy_release_init_data(const esp_phy_init_data_t* data); * mechanism for loading calibration data, disable * "Initialize PHY in startup code" option in menuconfig and call esp_phy_init * function from the application. For an example usage of esp_phy_init and - * this function, see do_phy_init function in cpu_start.c + * this function, see esp_phy_store_cal_data_to_nvs function in cpu_start.c * * @param out_cal_data pointer to calibration data structure to be filled with * loaded data. @@ -220,10 +220,10 @@ esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_dat esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data); /** - * @brief Initialize PHY module + * @brief Initialize PHY and RF module * - * PHY module should be initialized in order to use WiFi or BT. - * Now PHY initializing job is done automatically when start WiFi or BT. Users should not + * PHY and RF module should be initialized in order to use WiFi or BT. + * Now PHY and RF initializing job is done automatically when start WiFi or BT. Users should not * call this API in their application. * * @param init_data PHY parameters. Default set of parameters can @@ -231,23 +231,28 @@ esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_da * function. * @param mode Calibration mode (Full, partial, or no calibration) * @param[inout] calibration_data - * @param WiFi is_Waked up from sleep or not + * @param is_sleep WiFi wakes up from sleep or not * @return ESP_OK on success. * @return ESP_FAIL on fail. */ -esp_err_t esp_phy_init(const void* init_data, - int mode, void* calibration_data, bool is_sleep); +esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data, + esp_phy_calibration_mode_t mode, esp_phy_calibration_data_t* calibration_data, bool is_sleep); /** - * @brief De-initialize PHY module + * @brief De-initialize PHY and RF module * * PHY module should be de-initialized in order to shutdown WiFi or BT. - * Now PHY de-initializing job is done automatically when stop WiFi or BT. Users should not + * Now PHY and RF de-initializing job is done automatically when stop WiFi or BT. Users should not * call this API in their application. * * @return ESP_OK on success. */ -esp_err_t esp_phy_deinit(void); +esp_err_t esp_phy_rf_deinit(void); + +/** + * @brief Load calibration data from NVS and initialize PHY and RF module + */ +void esp_phy_load_cal_and_init(void); #ifdef __cplusplus } diff --git a/components/esp32/lib b/components/esp32/lib index c0d942036..bc16e8c07 160000 --- a/components/esp32/lib +++ b/components/esp32/lib @@ -1 +1 @@ -Subproject commit c0d94203602f7dd3d755bb1180a1640c3715c3ae +Subproject commit bc16e8c0749adefcd5bf44c9024849a504b8e839 diff --git a/components/esp32/phy_init.c b/components/esp32/phy_init.c index 6026da6ac..1c67a404a 100644 --- a/components/esp32/phy_init.c +++ b/components/esp32/phy_init.c @@ -17,11 +17,7 @@ #include #include -#include "freertos/FreeRTOS.h" -#include "freertos/semphr.h" -#include "freertos/xtensa_api.h" -#include "freertos/task.h" -#include "freertos/ringbuf.h" +#include #include "rom/ets_sys.h" #include "rom/rtc.h" @@ -43,59 +39,47 @@ static const char* TAG = "phy_init"; /* Count value to indicate if there is peripheral that has initialized PHY and RF */ -int g_phy_rf_init_count = 0; +static int s_phy_rf_init_count = 0; -static xSemaphoreHandle g_phy_rf_init_mux = NULL; +static _lock_t s_phy_rf_init_lock; -esp_err_t esp_phy_init(const void* init_data, - int mode, void* calibration_data, bool is_sleep) +esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data, + esp_phy_calibration_mode_t mode, esp_phy_calibration_data_t* calibration_data, bool is_sleep) { - esp_phy_init_data_t* data = (esp_phy_init_data_t *)init_data; - esp_phy_calibration_mode_t cal_mode = (esp_phy_calibration_mode_t)mode; - esp_phy_calibration_data_t* cal_data = (esp_phy_calibration_data_t *)calibration_data; + assert((s_phy_rf_init_count <= 1) && (s_phy_rf_init_count >= 0)); - assert((g_phy_rf_init_count <= 1) && (g_phy_rf_init_count >= 0)); - - if (g_phy_rf_init_mux == NULL) { - g_phy_rf_init_mux = xSemaphoreCreateMutex(); - if (g_phy_rf_init_mux == NULL) { - ESP_LOGE(TAG, "Create PHY RF mutex fail"); - return ESP_FAIL; + _lock_acquire(&s_phy_rf_init_lock); + if (s_phy_rf_init_count == 0) { + if (is_sleep == false) { + REG_SET_BIT(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST); + REG_CLR_BIT(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST); } - } - - xSemaphoreTake(g_phy_rf_init_mux, portMAX_DELAY); - if (g_phy_rf_init_count == 0) { - if (is_sleep == false) { - REG_SET_BIT(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST); - REG_CLR_BIT(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST); - } - // Enable WiFi peripheral clock - SET_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, 0x87cf); + // Enable WiFi peripheral clock + SET_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, 0x87cf); ESP_LOGV(TAG, "register_chipv7_phy, init_data=%p, cal_data=%p, mode=%d", init_data, calibration_data, mode); phy_set_wifi_mode_only(0); - register_chipv7_phy(data, cal_data, cal_mode); + register_chipv7_phy(init_data, calibration_data, mode); coex_bt_high_prio(); } - g_phy_rf_init_count++; - xSemaphoreGive(g_phy_rf_init_mux); + s_phy_rf_init_count++; + _lock_release(&s_phy_rf_init_lock); return ESP_OK; } -esp_err_t esp_phy_deinit(void) +esp_err_t esp_phy_rf_deinit(void) { - assert((g_phy_rf_init_count <= 2) && (g_phy_rf_init_count >= 1)); + assert((s_phy_rf_init_count <= 2) && (s_phy_rf_init_count >= 1)); - xSemaphoreTake(g_phy_rf_init_mux, portMAX_DELAY); - if (g_phy_rf_init_count == 1) { - // Disable PHY and RF. This is a teporary function. + _lock_acquire(&s_phy_rf_init_lock); + if (s_phy_rf_init_count == 1) { + // Disable PHY and RF. This is a teporary function. pm_close_rf(); - // Disable WiFi peripheral clock - CLEAR_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, 0x87cf); + // Disable WiFi peripheral clock + CLEAR_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, 0x87cf); } - g_phy_rf_init_count--; - xSemaphoreGive(g_phy_rf_init_mux); + s_phy_rf_init_count--; + _lock_release(&s_phy_rf_init_lock); return ESP_OK; } @@ -267,10 +251,11 @@ static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle, return err; } -void do_phy_init(void) +void esp_phy_load_cal_and_init(void) { - esp_phy_calibration_mode_t calibration_mode = PHY_RF_CAL_PARTIAL; +#ifdef CONFIG_ESP32_STORE_PHY_CAL_DATA_INTO_NVS nvs_flash_init(); + esp_phy_calibration_mode_t calibration_mode = PHY_RF_CAL_PARTIAL; if (rtc_get_reset_reason(0) == DEEPSLEEP_RESET) { calibration_mode = PHY_RF_CAL_NONE; } @@ -291,15 +276,18 @@ void do_phy_init(void) calibration_mode = PHY_RF_CAL_FULL; } - esp_phy_init(init_data, calibration_mode, cal_data, false); + esp_phy_rf_init(init_data, calibration_mode, cal_data, false); - if (calibration_mode != PHY_RF_CAL_NONE) { + if (calibration_mode != PHY_RF_CAL_NONE && err != ESP_OK) { err = esp_phy_store_cal_data_to_nvs(cal_data); } else { err = ESP_OK; } esp_phy_release_init_data(init_data); free(cal_data); // PHY maintains a copy of calibration data, so we can free this +#else + esp_phy_rf_init(NULL, PHY_RF_CAL_NONE, NULL, false); +#endif } #endif // CONFIG_PHY_ENABLED diff --git a/components/freertos/port.c b/components/freertos/port.c index ba4da3e28..3c26edfab 100644 --- a/components/freertos/port.c +++ b/components/freertos/port.c @@ -407,7 +407,7 @@ void vPortSetStackWatchpoint( void* pxStackStart ) { } uint32_t xPortGetTickRateHz(void) { - return (uint32_t)configTICK_RATE_HZ; + return (uint32_t)configTICK_RATE_HZ; }