From d47264143006c058ce0293c51f65af8c74464fd6 Mon Sep 17 00:00:00 2001 From: Xia Xiaotian Date: Fri, 22 May 2020 17:15:28 +0800 Subject: [PATCH] esp_wifi: refactor PHY access - Simplify PHY access API - Move coexist initializing and deinitializing out from PHY API to Wi-Fi and Bluetooth - Remove coexist pause and resume for they are no longer needed. --- components/bt/controller/bt.c | 75 ++-- components/esp_wifi/esp32/esp_adapter.c | 26 +- .../esp_wifi/include/esp_coexist_internal.h | 12 - components/esp_wifi/include/esp_phy_init.h | 90 +---- .../include/esp_private/wifi_os_adapter.h | 10 +- components/esp_wifi/src/phy_init.c | 330 +++--------------- components/esp_wifi/test/test_phy_rtc.c | 7 +- 7 files changed, 104 insertions(+), 446 deletions(-) diff --git a/components/bt/controller/bt.c b/components/bt/controller/bt.c index 4fba12cff1..e87f982cf2 100644 --- a/components/bt/controller/bt.c +++ b/components/bt/controller/bt.c @@ -902,8 +902,7 @@ static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles) static void btdm_sleep_enter_phase2_wrapper(void) { if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) { - esp_modem_sleep_enter(MODEM_BLE_MODULE); - esp_modem_sleep_enter(MODEM_CLASSIC_BT_MODULE); + esp_phy_disable(); #ifdef CONFIG_PM_ENABLE if (s_pm_lock_acquired) { esp_pm_lock_release(s_pm_lock); @@ -911,7 +910,7 @@ static void btdm_sleep_enter_phase2_wrapper(void) } #endif } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) { - esp_modem_sleep_enter(MODEM_BLE_MODULE); + esp_phy_disable(); // pause bluetooth baseband periph_module_disable(PERIPH_BT_BASEBAND_MODULE); } @@ -927,8 +926,7 @@ static void btdm_sleep_exit_phase3_wrapper(void) #endif if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) { - esp_modem_sleep_exit(MODEM_BLE_MODULE); - esp_modem_sleep_exit(MODEM_CLASSIC_BT_MODULE); + esp_phy_enable(); btdm_check_and_init_bb(); #ifdef CONFIG_PM_ENABLE esp_timer_stop(s_btdm_slp_tmr); @@ -936,7 +934,7 @@ static void btdm_sleep_exit_phase3_wrapper(void) } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) { // resume bluetooth baseband periph_module_enable(PERIPH_BT_BASEBAND_MODULE); - esp_modem_sleep_exit(MODEM_BLE_MODULE); + esp_phy_enable(); } } @@ -1560,7 +1558,7 @@ static void bt_shutdown(void) } esp_bt_controller_shutdown(); - esp_phy_rf_deinit(PHY_BT_MODULE); + esp_phy_disable(); return; } @@ -1586,21 +1584,11 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) esp_pm_lock_acquire(s_pm_lock); #endif - esp_phy_load_cal_and_init(PHY_BT_MODULE); + esp_phy_enable(); - if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_NONE) { - //Just register to sleep module, make the modem sleep modules check BT sleep status when sleep enter. - //Thus, it will prevent WIFI from disabling RF when BT is not in sleep but is using RF. - esp_modem_sleep_register(MODEM_BLE_MODULE); - esp_modem_sleep_register(MODEM_CLASSIC_BT_MODULE); - esp_modem_sleep_exit(MODEM_BLE_MODULE); - esp_modem_sleep_exit(MODEM_CLASSIC_BT_MODULE); - } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) { - esp_modem_sleep_register(MODEM_BLE_MODULE); - esp_modem_sleep_register(MODEM_CLASSIC_BT_MODULE); - } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) { - esp_modem_sleep_register(MODEM_BLE_MODULE); - } +#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE + coex_init(); +#endif if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) { btdm_controller_enable_sleep(true); @@ -1610,15 +1598,11 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) btdm_check_and_init_bb(); ret = btdm_controller_enable(mode); - if (ret) { - if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_NONE - || btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) { - esp_modem_sleep_deregister(MODEM_BLE_MODULE); - esp_modem_sleep_deregister(MODEM_CLASSIC_BT_MODULE); - } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) { - esp_modem_sleep_deregister(MODEM_BLE_MODULE); - } - esp_phy_rf_deinit(PHY_BT_MODULE); + if (ret != 0) { +#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE + coex_deinit(); +#endif + esp_phy_disable(); #ifdef CONFIG_PM_ENABLE if (!s_btdm_allow_light_sleep) { esp_pm_lock_release(s_light_sleep_pm_lock); @@ -1654,14 +1638,11 @@ esp_err_t esp_bt_controller_disable(void) btdm_controller_disable(); - if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_NONE - || btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) { - esp_modem_sleep_deregister(MODEM_BLE_MODULE); - esp_modem_sleep_deregister(MODEM_CLASSIC_BT_MODULE); - } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) { - esp_modem_sleep_deregister(MODEM_BLE_MODULE); - } - esp_phy_rf_deinit(PHY_BT_MODULE); +#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE + coex_deinit(); +#endif + + esp_phy_disable(); btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; esp_unregister_shutdown_handler(bt_shutdown); @@ -1728,13 +1709,8 @@ esp_err_t esp_bt_sleep_enable (void) if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) { return ESP_ERR_INVALID_STATE; } - if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) { - esp_modem_sleep_register(MODEM_BLE_MODULE); - esp_modem_sleep_register(MODEM_CLASSIC_BT_MODULE); - btdm_controller_enable_sleep (true); - status = ESP_OK; - } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) { - esp_modem_sleep_register(MODEM_BLE_MODULE); + if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG || + btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) { btdm_controller_enable_sleep (true); status = ESP_OK; } else { @@ -1750,13 +1726,8 @@ esp_err_t esp_bt_sleep_disable (void) if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) { return ESP_ERR_INVALID_STATE; } - if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) { - esp_modem_sleep_deregister(MODEM_BLE_MODULE); - esp_modem_sleep_deregister(MODEM_CLASSIC_BT_MODULE); - btdm_controller_enable_sleep (false); - status = ESP_OK; - } else if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) { - esp_modem_sleep_deregister(MODEM_BLE_MODULE); + if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG || + btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_EVED) { btdm_controller_enable_sleep (false); status = ESP_OK; } else { diff --git a/components/esp_wifi/esp32/esp_adapter.c b/components/esp_wifi/esp32/esp_adapter.c index 79eb14d35f..83b803c73e 100644 --- a/components/esp_wifi/esp32/esp_adapter.c +++ b/components/esp_wifi/esp32/esp_adapter.c @@ -461,6 +461,22 @@ static void * IRAM_ATTR zalloc_internal_wrapper(size_t size) return ptr; } +static int coex_init_wrapper(void) +{ +#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE + return coex_init(); +#else + return 0; +#endif +} + +static void coex_deinit_wrapper(void) +{ +#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE + coex_deinit(); +#endif +} + static uint32_t coex_status_get_wrapper(void) { #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE @@ -625,8 +641,8 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._rand = esp_random, ._dport_access_stall_other_cpu_start_wrap = s_esp_dport_access_stall_other_cpu_start, ._dport_access_stall_other_cpu_end_wrap = s_esp_dport_access_stall_other_cpu_end, - ._phy_rf_deinit = esp_phy_rf_deinit, - ._phy_load_cal_and_init = esp_phy_load_cal_and_init, + ._phy_disable = esp_phy_disable, + ._phy_enable = esp_phy_enable, ._phy_common_clock_enable = esp_phy_common_clock_enable, ._phy_common_clock_disable = esp_phy_common_clock_disable, ._phy_update_country_info = esp_phy_update_country_info, @@ -668,10 +684,8 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._wifi_zalloc = wifi_zalloc_wrapper, ._wifi_create_queue = wifi_create_queue_wrapper, ._wifi_delete_queue = wifi_delete_queue_wrapper, - ._modem_sleep_enter = esp_modem_sleep_enter, - ._modem_sleep_exit = esp_modem_sleep_exit, - ._modem_sleep_register = esp_modem_sleep_register, - ._modem_sleep_deregister = esp_modem_sleep_deregister, + ._coex_init = coex_init_wrapper, + ._coex_deinit = coex_deinit_wrapper, ._coex_status_get = coex_status_get_wrapper, ._coex_condition_set = coex_condition_set_wrapper, ._coex_wifi_request = coex_wifi_request_wrapper, diff --git a/components/esp_wifi/include/esp_coexist_internal.h b/components/esp_wifi/include/esp_coexist_internal.h index 9f279ce3b1..400872778b 100644 --- a/components/esp_wifi/include/esp_coexist_internal.h +++ b/components/esp_wifi/include/esp_coexist_internal.h @@ -53,18 +53,6 @@ esp_err_t coex_init(void); */ void coex_deinit(void); -/** - * @brief Pause software coexist - * extern function for internal use. - */ -void coex_pause(void); - -/** - * @brief Resume software coexist - * extern function for internal use. - */ -void coex_resume(void); - /** * @brief Get software coexist version string * extern function for internal use. diff --git a/components/esp_wifi/include/esp_phy_init.h b/components/esp_wifi/include/esp_phy_init.h index 1cf3c13d2a..453f87b7bc 100644 --- a/components/esp_wifi/include/esp_phy_init.h +++ b/components/esp_wifi/include/esp_phy_init.h @@ -48,21 +48,6 @@ typedef enum { PHY_RF_CAL_FULL = 0x00000002 /*!< Do full RF calibration. Produces best results, but also consumes a lot of time and current. Suggested to be used once. */ } esp_phy_calibration_mode_t; - -/** - * @brief Modules for modem sleep - */ -typedef enum{ - MODEM_BLE_MODULE, //!< BLE controller used - MODEM_CLASSIC_BT_MODULE, //!< Classic BT controller used - MODEM_WIFI_STATION_MODULE, //!< Wi-Fi Station used - MODEM_WIFI_SOFTAP_MODULE, //!< Wi-Fi SoftAP used - MODEM_WIFI_SNIFFER_MODULE, //!< Wi-Fi Sniffer used - MODEM_WIFI_NULL_MODULE, //!< Wi-Fi Null mode used - MODEM_USER_MODULE, //!< User used - MODEM_MODULE_COUNT //!< Number of items -}modem_sleep_module_t; - #if CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN /** * @brief PHY init data type @@ -87,30 +72,6 @@ typedef enum { } phy_init_data_type_t; #endif -/** - * @brief Module WIFI mask for medem sleep - */ -#define MODEM_BT_MASK ((1<= PHY_MODULE_COUNT){ - ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \ - module count(%d)", __func__, module, PHY_MODULE_COUNT); - return ESP_ERR_INVALID_ARG; - } + _lock_acquire(&s_phy_access_lock); - _lock_acquire(&s_phy_rf_init_lock); - uint32_t s_module_phy_rf_init_old = s_module_phy_rf_init; - bool is_wifi_or_bt_enabled = !!(s_module_phy_rf_init_old & (BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE))); - esp_err_t status = ESP_OK; - s_module_phy_rf_init |= BIT(module); + if (s_phy_access_ref == 0) { +#if CONFIG_IDF_TARGET_ESP32 + // Update time stamp + s_phy_rf_en_ts = esp_timer_get_time(); + // Update WiFi MAC time before WiFi/BT common clock is enabled + phy_update_wifi_mac_time(false, s_phy_rf_en_ts); +#endif + esp_phy_common_clock_enable(); + phy_set_wifi_mode_only(0); - if ((is_wifi_or_bt_enabled == false) && (module == PHY_MODEM_MODULE)){ - status = ESP_FAIL; - } - else if (s_is_phy_rf_en == true) { - } - else { - /* If Wi-Fi, BT all disabled, modem sleep should not take effect; - * If either Wi-Fi or BT is enabled, should allow modem sleep requires - * to enter sleep; - * If Wi-Fi, BT co-exist, it is disallowed that only one module - * support modem sleep, E,g. BT support modem sleep but Wi-Fi not - * support modem sleep; - */ - if (is_wifi_or_bt_enabled == false){ - if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){ - s_is_phy_rf_en = true; - } + if (s_is_phy_calibrated == false) { + esp_phy_load_cal_and_init(); + s_is_phy_calibrated = true; } else { - if (module == PHY_MODEM_MODULE){ - s_is_phy_rf_en = true; - } - else if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){ - /* New module (BT or Wi-Fi) can init RF according to modem_sleep_exit */ - } - } - if (s_is_phy_rf_en == true){ -#if CONFIG_IDF_TARGET_ESP32 - // Update time stamp - s_phy_rf_en_ts = esp_timer_get_time(); - // Update WiFi MAC time before WiFi/BT common clock is enabled - phy_update_wifi_mac_time(false, s_phy_rf_en_ts); -#endif - esp_phy_common_clock_enable(); - phy_set_wifi_mode_only(0); - #if CONFIG_IDF_TARGET_ESP32S2 - if (module == PHY_MODEM_MODULE) { - phy_wakeup_init(); - phy_digital_regs_load(); - } - else + phy_wakeup_init(); +#elif CONFIG_IDF_TARGET_ESP32 + register_chipv7_phy(NULL, NULL, PHY_RF_CAL_NONE); #endif - if (ESP_CAL_DATA_CHECK_FAIL == register_chipv7_phy(init_data, calibration_data, mode)) { - ESP_LOGW(TAG, "saving new calibration data because of checksum failure, mode(%d)", mode); -#ifdef CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE - if (mode != PHY_RF_CAL_FULL) { - esp_phy_store_cal_data_to_nvs(calibration_data); - } -#endif - } + } #if CONFIG_IDF_TARGET_ESP32 - coex_bt_high_prio(); + coex_bt_high_prio(); #endif - } } + s_phy_access_ref++; -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){ - uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE); - if ((s_module_phy_rf_init & phy_bt_wifi_mask) == phy_bt_wifi_mask) { //both wifi & bt enabled - coex_init(); - coex_resume(); - } - } -#endif - - _lock_release(&s_phy_rf_init_lock); - return status; + _lock_release(&s_phy_access_lock); } -esp_err_t esp_phy_rf_deinit(phy_rf_module_t module) +void esp_phy_disable(void) { - /* 3 modules may call phy_init: Wi-Fi, BT, Modem Sleep */ - if (module >= PHY_MODULE_COUNT){ - ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \ - module count(%d)", __func__, module, PHY_MODULE_COUNT); - return ESP_ERR_INVALID_ARG; - } + _lock_acquire(&s_phy_access_lock); - _lock_acquire(&s_phy_rf_init_lock); - uint32_t s_module_phy_rf_init_old = s_module_phy_rf_init; - uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE); - bool is_wifi_or_bt_enabled = !!(s_module_phy_rf_init_old & phy_bt_wifi_mask); - bool is_both_wifi_bt_enabled = ((s_module_phy_rf_init_old & phy_bt_wifi_mask) == phy_bt_wifi_mask); - s_module_phy_rf_init &= ~BIT(module); - esp_err_t status = ESP_OK; - -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){ - if (is_both_wifi_bt_enabled == true) { - coex_deinit(); - } - } -#endif - - if ((is_wifi_or_bt_enabled == false) && (module == PHY_MODEM_MODULE)){ - /* Modem sleep should not take effect in this case */ - status = ESP_FAIL; - } - else if (s_is_phy_rf_en == false) { - //do nothing - } - else { - if (is_wifi_or_bt_enabled == false){ - if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){ - s_is_phy_rf_en = false; - ESP_LOGE(TAG, "%s, RF should not be in enabled state if both Wi-Fi and BT are disabled", __func__); - } - } - else { - if (module == PHY_MODEM_MODULE){ - s_is_phy_rf_en = false; - } - else if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){ - s_is_phy_rf_en = is_both_wifi_bt_enabled ? true : false; - } - } - - if (s_is_phy_rf_en == false) { - phy_digital_regs_store(); - // Disable PHY and RF. - phy_close_rf(); + s_phy_access_ref--; + if (s_phy_access_ref == 0) { + // Disable PHY and RF. + phy_close_rf(); #if CONFIG_IDF_TARGET_ESP32 - // Update WiFi MAC time before disalbe WiFi/BT common peripheral clock - phy_update_wifi_mac_time(true, esp_timer_get_time()); + // Update WiFi MAC time before disalbe WiFi/BT common peripheral clock + phy_update_wifi_mac_time(true, esp_timer_get_time()); #endif - // Disable WiFi/BT common peripheral clock. Do not disable clock for hardware RNG - esp_phy_common_clock_disable(); - } + // Disable WiFi/BT common peripheral clock. Do not disable clock for hardware RNG + esp_phy_common_clock_disable(); } - _lock_release(&s_phy_rf_init_lock); - return status; + _lock_release(&s_phy_access_lock); } - - -esp_err_t esp_modem_sleep_enter(modem_sleep_module_t module) -{ -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE); -#endif - - if (module >= MODEM_MODULE_COUNT){ - ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \ - module count(%d)", __func__, module, MODEM_MODULE_COUNT); - return ESP_ERR_INVALID_ARG; - } - else if (!(s_modem_sleep_module_register & BIT(module))){ - ESP_LOGW(TAG, "%s, module (%d) has not been registered", __func__, module); - return ESP_ERR_INVALID_ARG; - } - else { - _lock_acquire(&s_modem_sleep_lock); - s_modem_sleep_module_enter |= BIT(module); -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - _lock_acquire(&s_phy_rf_init_lock); - if (((s_module_phy_rf_init & phy_bt_wifi_mask) == phy_bt_wifi_mask) //both wifi & bt enabled - && (s_modem_sleep_module_enter & (MODEM_BT_MASK | MODEM_WIFI_MASK)) != 0){ - coex_pause(); - } - _lock_release(&s_phy_rf_init_lock); -#endif - if (!s_is_modem_sleep_en && (s_modem_sleep_module_enter == s_modem_sleep_module_register)){ - esp_err_t status = esp_phy_rf_deinit(PHY_MODEM_MODULE); - if (status == ESP_OK){ - s_is_modem_sleep_en = true; - } - } - _lock_release(&s_modem_sleep_lock); - return ESP_OK; - } -} - -esp_err_t esp_modem_sleep_exit(modem_sleep_module_t module) -{ -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE); -#endif - - if (module >= MODEM_MODULE_COUNT){ - ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \ - module count(%d)", __func__, module, MODEM_MODULE_COUNT); - return ESP_ERR_INVALID_ARG; - } - else if (!(s_modem_sleep_module_register & BIT(module))){ - ESP_LOGW(TAG, "%s, module (%d) has not been registered", __func__, module); - return ESP_ERR_INVALID_ARG; - } - else { - _lock_acquire(&s_modem_sleep_lock); - s_modem_sleep_module_enter &= ~BIT(module); - if (s_is_modem_sleep_en){ - esp_err_t status = esp_phy_rf_init(NULL,PHY_RF_CAL_NONE,NULL, PHY_MODEM_MODULE); - if (status == ESP_OK){ - s_is_modem_sleep_en = false; - } - } -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - _lock_acquire(&s_phy_rf_init_lock); - if (((s_module_phy_rf_init & phy_bt_wifi_mask) == phy_bt_wifi_mask) //both wifi & bt enabled - && (s_modem_sleep_module_enter & (MODEM_BT_MASK | MODEM_WIFI_MASK)) == 0){ - coex_resume(); - } - _lock_release(&s_phy_rf_init_lock); -#endif - _lock_release(&s_modem_sleep_lock); - return ESP_OK; - } - return ESP_OK; -} - -esp_err_t esp_modem_sleep_register(modem_sleep_module_t module) -{ - if (module >= MODEM_MODULE_COUNT){ - ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \ - module count(%d)", __func__, module, MODEM_MODULE_COUNT); - return ESP_ERR_INVALID_ARG; - } - else if (s_modem_sleep_module_register & BIT(module)){ - ESP_LOGI(TAG, "%s, multiple registration of module (%d)", __func__, module); - return ESP_OK; - } - else{ - _lock_acquire(&s_modem_sleep_lock); - s_modem_sleep_module_register |= BIT(module); - /* The module is set to enter modem sleep by default, otherwise will prevent - * other modules from entering sleep mode if this module never call enter sleep function - * in the future */ - s_modem_sleep_module_enter |= BIT(module); - _lock_release(&s_modem_sleep_lock); - return ESP_OK; - } -} - -esp_err_t esp_modem_sleep_deregister(modem_sleep_module_t module) -{ - if (module >= MODEM_MODULE_COUNT){ - ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \ - module count(%d)", __func__, module, MODEM_MODULE_COUNT); - return ESP_ERR_INVALID_ARG; - } - else if (!(s_modem_sleep_module_register & BIT(module))){ - ESP_LOGI(TAG, "%s, module (%d) has not been registered", __func__, module); - return ESP_OK; - } - else{ - _lock_acquire(&s_modem_sleep_lock); - s_modem_sleep_module_enter &= ~BIT(module); - s_modem_sleep_module_register &= ~BIT(module); - if (s_modem_sleep_module_register == 0){ - s_modem_sleep_module_enter = 0; - /* Once all module are de-registered and current state - * is modem sleep mode, we need to turn off modem sleep - */ - if (s_is_modem_sleep_en == true){ - s_is_modem_sleep_en = false; - esp_phy_rf_init(NULL,PHY_RF_CAL_NONE,NULL, PHY_MODEM_MODULE); - } - } - _lock_release(&s_modem_sleep_lock); - return ESP_OK; - } -} - - // PHY init data handling functions #if CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION #include "esp_partition.h" @@ -764,7 +522,7 @@ static void __attribute((unused)) esp_phy_reduce_tx_power(esp_phy_init_data_t* i } #endif -void esp_phy_load_cal_and_init(phy_rf_module_t module) +void esp_phy_load_cal_and_init(void) { char * phy_version = get_phy_version_str(); ESP_LOGI(TAG, "phy_version %s", phy_version); @@ -822,15 +580,19 @@ void esp_phy_load_cal_and_init(phy_rf_module_t module) esp_efuse_mac_get_default(sta_mac); memcpy(cal_data->mac, sta_mac, 6); - esp_phy_rf_init(init_data, calibration_mode, cal_data, module); + esp_err_t ret = register_chipv7_phy(init_data, cal_data, calibration_mode); + if (ret == ESP_CAL_DATA_CHECK_FAIL) { + ESP_LOGW(TAG, "saving new calibration data because of checksum failure, mode(%d)", calibration_mode); + } - if (calibration_mode != PHY_RF_CAL_NONE && err != ESP_OK) { + if ((calibration_mode != PHY_RF_CAL_NONE && err != ESP_OK) || + (calibration_mode != PHY_RF_CAL_FULL && ret == ESP_CAL_DATA_CHECK_FAIL)) { err = esp_phy_store_cal_data_to_nvs(cal_data); } else { err = ESP_OK; } #else - esp_phy_rf_init(init_data, PHY_RF_CAL_FULL, cal_data, module); + register_chipv7_phy(init_data, cal_data, PHY_RF_CAL_FULL); #endif #if CONFIG_ESP32_REDUCE_PHY_TX_POWER diff --git a/components/esp_wifi/test/test_phy_rtc.c b/components/esp_wifi/test/test_phy_rtc.c index 53ba4fbc49..1db2fbab09 100644 --- a/components/esp_wifi/test/test_phy_rtc.c +++ b/components/esp_wifi/test/test_phy_rtc.c @@ -45,10 +45,7 @@ static void test_phy_rtc_init(void) } TEST_ESP_OK(ret); -#ifdef SOC_BT_SUPPORTED - esp_phy_load_cal_and_init(PHY_BT_MODULE); -#endif - esp_phy_load_cal_and_init(PHY_WIFI_MODULE); + esp_phy_enable(); //must run here, not blocking in above code TEST_ASSERT(1); @@ -100,4 +97,4 @@ TEST_CASE("Test PHY/RTC functions called when cache is disabled", "[phy_rtc][cac vSemaphoreDelete(semphr_done); } -#endif \ No newline at end of file +#endif