From 89c036498e4348396d1bc4a3fce52af4f4131127 Mon Sep 17 00:00:00 2001 From: Xia Xiaotian Date: Fri, 22 May 2020 17:15:28 +0800 Subject: [PATCH 1/5] 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 | 328 +++--------------- components/esp_wifi/test/test_phy_rtc.c | 7 +- 7 files changed, 104 insertions(+), 444 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 070d981103..c7687cf0db 100644 --- a/components/esp_wifi/esp32/esp_adapter.c +++ b/components/esp_wifi/esp32/esp_adapter.c @@ -437,6 +437,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 @@ -600,8 +616,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, ._read_mac = esp_read_mac, @@ -641,10 +657,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 e217a8ed43..4ce684f73e 100644 --- a/components/esp_wifi/include/esp_phy_init.h +++ b/components/esp_wifi/include/esp_phy_init.h @@ -48,45 +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; - -/** - * @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_ESP32S2BETA - if (module == PHY_MODEM_MODULE) { - phy_wakeup_init(); - } - 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) { - // 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" @@ -638,7 +398,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) { #if CONFIG_IDF_TARGET_ESP32 char * phy_version = get_phy_version_str(); @@ -694,15 +454,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 e52b4b3966..d112f909a4 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 From 002b273aa3bcaa13e36b54b05179d976ea693436 Mon Sep 17 00:00:00 2001 From: zhangyanjiao Date: Fri, 28 Jan 2022 14:49:50 +0800 Subject: [PATCH 2/5] call pm_wakeup_init in esp_phy_enable() on esp32 --- components/esp_wifi/include/phy.h | 2 -- components/esp_wifi/src/phy_init.c | 4 ---- 2 files changed, 6 deletions(-) diff --git a/components/esp_wifi/include/phy.h b/components/esp_wifi/include/phy.h index b101200411..c1e4639369 100644 --- a/components/esp_wifi/include/phy.h +++ b/components/esp_wifi/include/phy.h @@ -59,12 +59,10 @@ void phy_set_wifi_mode_only(bool wifi_only); */ void coex_bt_high_prio(void); -#if CONFIG_IDF_TARGET_ESP32S2BETA /** * @brief Open PHY and RF. */ void phy_wakeup_init(void); -#endif /** * @brief Shutdown PHY and RF. diff --git a/components/esp_wifi/src/phy_init.c b/components/esp_wifi/src/phy_init.c index ab08848e0e..8b8052a313 100644 --- a/components/esp_wifi/src/phy_init.c +++ b/components/esp_wifi/src/phy_init.c @@ -141,11 +141,7 @@ void esp_phy_enable(void) s_is_phy_calibrated = true; } else { -#if CONFIG_IDF_TARGET_ESP32S2BETA phy_wakeup_init(); -#elif CONFIG_IDF_TARGET_ESP32 - register_chipv7_phy(NULL, NULL, PHY_RF_CAL_NONE); -#endif } #if CONFIG_IDF_TARGET_ESP32 From 5056892b9bf6d493958c4bf9ed14cd8d67796a7c Mon Sep 17 00:00:00 2001 From: Xia Xiaotian Date: Tue, 19 Jan 2021 19:36:06 +0800 Subject: [PATCH 3/5] esp_wifi: store PHY digital registers before disabling PHY and load them after enabling PHY --- components/esp_wifi/include/phy.h | 10 +++++++++ components/esp_wifi/src/phy_init.c | 24 +++++++++++++++++++++ components/soc/esp32/include/soc/soc_caps.h | 4 ++++ 3 files changed, 38 insertions(+) diff --git a/components/esp_wifi/include/phy.h b/components/esp_wifi/include/phy.h index c1e4639369..55746ffecb 100644 --- a/components/esp_wifi/include/phy.h +++ b/components/esp_wifi/include/phy.h @@ -69,6 +69,16 @@ void phy_wakeup_init(void); */ void phy_close_rf(void); +/** + * @brief Store and load PHY digital registers. + * + * @param backup_en if backup_en is true, store PHY digital registers to memory. Otherwise load PHY digital registers from memory + * @param mem_addr Memory address to store and load PHY digital registers + * + * @return memory size + */ +uint8_t phy_dig_reg_backup(bool backup_en, uint32_t *mem_addr); + #ifdef __cplusplus } #endif diff --git a/components/esp_wifi/src/phy_init.c b/components/esp_wifi/src/phy_init.c index 8b8052a313..4a1af3fa9c 100644 --- a/components/esp_wifi/src/phy_init.c +++ b/components/esp_wifi/src/phy_init.c @@ -34,6 +34,7 @@ #include "esp_coexist_internal.h" #include "driver/periph_ctrl.h" #include "esp_private/wifi.h" +#include "soc/soc_caps.h" #if CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/ets_sys.h" @@ -65,6 +66,9 @@ static int64_t s_phy_rf_en_ts = 0; /* PHY spinlock for libphy.a */ static DRAM_ATTR portMUX_TYPE s_phy_int_mux = portMUX_INITIALIZER_UNLOCKED; +/* Memory to store PHY digital registers */ +static uint32_t* s_phy_digital_regs_mem = NULL; + uint32_t IRAM_ATTR phy_enter_critical(void) { if (xPortInIsrContext()) { @@ -122,6 +126,24 @@ IRAM_ATTR void esp_phy_common_clock_disable(void) wifi_bt_common_module_disable(); } +static inline void phy_digital_regs_store(void) +{ + if (s_phy_digital_regs_mem == NULL) { + s_phy_digital_regs_mem = (uint32_t *)malloc(SOC_PHY_DIG_REGS_MEM_SIZE); + } + + if (s_phy_digital_regs_mem != NULL) { + phy_dig_reg_backup(true, s_phy_digital_regs_mem); + } +} + +static inline void phy_digital_regs_load(void) +{ + if (s_phy_digital_regs_mem != NULL) { + phy_dig_reg_backup(false, s_phy_digital_regs_mem); + } +} + void esp_phy_enable(void) { _lock_acquire(&s_phy_access_lock); @@ -142,6 +164,7 @@ void esp_phy_enable(void) } else { phy_wakeup_init(); + phy_digital_regs_load(); } #if CONFIG_IDF_TARGET_ESP32 @@ -159,6 +182,7 @@ void esp_phy_disable(void) s_phy_access_ref--; if (s_phy_access_ref == 0) { + phy_digital_regs_store(); // Disable PHY and RF. phy_close_rf(); #if CONFIG_IDF_TARGET_ESP32 diff --git a/components/soc/esp32/include/soc/soc_caps.h b/components/soc/esp32/include/soc/soc_caps.h index bbc8e8f19f..5bb7b3a386 100644 --- a/components/soc/esp32/include/soc/soc_caps.h +++ b/components/soc/esp32/include/soc/soc_caps.h @@ -10,3 +10,7 @@ #define SOC_BT_SUPPORTED 1 #define SOC_SDIO_SLAVE_SUPPORTED 1 #define SOC_CAN_SUPPORTED 1 + +/*--------------- PHY REGISTER AND MEMORY SIZE CAPS --------------------------*/ +#define SOC_PHY_DIG_REGS_MEM_SIZE (21*4) + From 900dc2ada627ee3245b4c209bb177827e8823f7e Mon Sep 17 00:00:00 2001 From: Xia Xiaotian Date: Wed, 19 Feb 2020 19:16:42 +0800 Subject: [PATCH 4/5] esp_wifi: define wifi_clock_enable() on esp32 --- components/esp_wifi/esp32/esp_adapter.c | 23 +++++++++++++++++-- .../include/esp_private/wifi_os_adapter.h | 5 ++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/components/esp_wifi/esp32/esp_adapter.c b/components/esp_wifi/esp32/esp_adapter.c index c7687cf0db..80bd879aed 100644 --- a/components/esp_wifi/esp32/esp_adapter.c +++ b/components/esp_wifi/esp32/esp_adapter.c @@ -38,6 +38,8 @@ #include "esp_private/wifi_os_adapter.h" #include "esp_private/wifi.h" #include "esp_phy_init.h" +#include "soc/dport_reg.h" +#include "soc/syscon_reg.h" #include "driver/periph_ctrl.h" #include "nvs.h" #include "os.h" @@ -408,6 +410,22 @@ static void IRAM_ATTR timer_arm_us_wrapper(void *ptimer, uint32_t us, bool repea ets_timer_arm_us(ptimer, us, repeat); } +static void wifi_reset_mac_wrapper(void) +{ + DPORT_SET_PERI_REG_MASK(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST); + DPORT_CLEAR_PERI_REG_MASK(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST); +} + +static void wifi_clock_enable_wrapper(void) +{ + periph_module_enable(PERIPH_WIFI_MODULE); +} + +static void wifi_clock_disable_wrapper(void) +{ + periph_module_disable(PERIPH_WIFI_MODULE); +} + static int get_time_wrapper(void *t) { return os_get_time(t); @@ -626,8 +644,9 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._timer_done = timer_done_wrapper, ._timer_setfn = timer_setfn_wrapper, ._timer_arm_us = timer_arm_us_wrapper, - ._periph_module_enable = periph_module_enable, - ._periph_module_disable = periph_module_disable, + ._wifi_reset_mac = wifi_reset_mac_wrapper, + ._wifi_clock_enable = wifi_clock_enable_wrapper, + ._wifi_clock_disable = wifi_clock_disable_wrapper, ._esp_timer_get_time = esp_timer_get_time, ._nvs_set_i8 = nvs_set_i8, ._nvs_get_i8 = nvs_get_i8, diff --git a/components/esp_wifi/include/esp_private/wifi_os_adapter.h b/components/esp_wifi/include/esp_private/wifi_os_adapter.h index fa083ee2c0..5be7f8e6c8 100644 --- a/components/esp_wifi/include/esp_private/wifi_os_adapter.h +++ b/components/esp_wifi/include/esp_private/wifi_os_adapter.h @@ -87,8 +87,9 @@ typedef struct { void (* _timer_done)(void *ptimer); void (* _timer_setfn)(void *ptimer, void *pfunction, void *parg); void (* _timer_arm_us)(void *ptimer, uint32_t us, bool repeat); - void (* _periph_module_enable)(uint32_t periph); - void (* _periph_module_disable)(uint32_t periph); + void (* _wifi_reset_mac)(void); + void (* _wifi_clock_enable)(void); + void (* _wifi_clock_disable)(void); int64_t (* _esp_timer_get_time)(void); int32_t (* _nvs_set_i8)(uint32_t handle, const char* key, int8_t value); int32_t (* _nvs_get_i8)(uint32_t handle, const char* key, int8_t* out_value); From dad8d00bdf2726c2a02a77c6b5b7a5fa80850d5e Mon Sep 17 00:00:00 2001 From: zhangyanjiao Date: Fri, 28 Jan 2022 16:42:19 +0800 Subject: [PATCH 5/5] esp_wifi: refactor modem sleep access phy --- components/esp_wifi/lib | 2 +- components/esp_wifi/src/phy_init.c | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/components/esp_wifi/lib b/components/esp_wifi/lib index 2d8942782c..a16da20796 160000 --- a/components/esp_wifi/lib +++ b/components/esp_wifi/lib @@ -1 +1 @@ -Subproject commit 2d8942782c28a3621e6d73eb4dab20e8635569f1 +Subproject commit a16da2079608f5d30bd759a94d56a3adf81ee1c8 diff --git a/components/esp_wifi/src/phy_init.c b/components/esp_wifi/src/phy_init.c index 4a1af3fa9c..e911956236 100644 --- a/components/esp_wifi/src/phy_init.c +++ b/components/esp_wifi/src/phy_init.c @@ -156,7 +156,6 @@ void esp_phy_enable(void) phy_update_wifi_mac_time(false, s_phy_rf_en_ts); #endif esp_phy_common_clock_enable(); - phy_set_wifi_mode_only(0); if (s_is_phy_calibrated == false) { esp_phy_load_cal_and_init();