diff --git a/components/bt/controller/bt.c b/components/bt/controller/bt.c index d3f3b3ecf5..b04108de5b 100644 --- a/components/bt/controller/bt.c +++ b/components/bt/controller/bt.c @@ -217,11 +217,11 @@ extern int bredr_txpwr_get(int *min_power_level, int *max_power_level); extern void bredr_sco_datapath_set(uint8_t data_path); extern void btdm_controller_scan_duplicate_list_clear(void); /* Coexistence */ -extern int coex_bt_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration); -extern int coex_bt_release_wrapper(uint32_t event); -extern int coex_register_bt_cb_wrapper(coex_func_cb_t cb); -extern uint32_t coex_bb_reset_lock_wrapper(void); -extern void coex_bb_reset_unlock_wrapper(uint32_t restore); +extern int coex_bt_request(uint32_t event, uint32_t latency, uint32_t duration); +extern int coex_bt_release(uint32_t event); +extern int coex_register_bt_cb(coex_func_cb_t cb); +extern uint32_t coex_bb_reset_lock(void); +extern void coex_bb_reset_unlock(uint32_t restore); extern void coex_ble_adv_priority_high_set(bool high); extern char _bss_start_btdm; @@ -288,6 +288,11 @@ static void IRAM_ATTR btdm_sleep_exit_phase1_wrapper(void); static void btdm_sleep_exit_phase3_wrapper(void); static bool coex_bt_wakeup_request(void); static void coex_bt_wakeup_request_end(void); +static int coex_bt_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration); +static int coex_bt_release_wrapper(uint32_t event); +static int coex_register_bt_cb_wrapper(coex_func_cb_t cb); +static uint32_t coex_bb_reset_lock_wrapper(void); +static void coex_bb_reset_unlock_wrapper(uint32_t restore); /* Local variable definition *************************************************************************** @@ -850,14 +855,13 @@ 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 esp_pm_lock_release(s_pm_lock); semphr_give_wrapper(s_pm_lock_sem); #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); } @@ -875,8 +879,7 @@ static void IRAM_ATTR btdm_sleep_exit_phase1_wrapper(void) static void btdm_sleep_exit_phase3_wrapper(void) { 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); @@ -884,7 +887,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(); } } @@ -963,6 +966,49 @@ static void coex_bt_wakeup_request_end(void) return; } +int IRAM_ATTR coex_bt_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_bt_request(event, latency, duration); +#else + return 0; +#endif +} + +int IRAM_ATTR coex_bt_release_wrapper(uint32_t event) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_bt_release(event); +#else + return 0; +#endif +} + +int coex_register_bt_cb_wrapper(coex_func_cb_t cb) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_register_bt_cb(cb); +#else + return 0; +#endif +} + +uint32_t IRAM_ATTR coex_bb_reset_lock_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_bb_reset_lock(); +#else + return 0; +#endif +} + +void IRAM_ATTR coex_bb_reset_unlock_wrapper(uint32_t restore) +{ +#if CONFIG_SW_COEXIST_ENABLE + coex_bb_reset_unlock(restore); +#endif +} + bool esp_vhci_host_check_send_available(void) { return API_vhci_host_check_send_available(); @@ -1273,6 +1319,10 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) } #endif +#if CONFIG_SW_COEXIST_ENABLE + coex_init(); +#endif + btdm_cfg_mask = btdm_config_mask_load(); if (btdm_controller_init(btdm_cfg_mask, cfg) != 0) { @@ -1375,21 +1425,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_SW_COEXIST_ENABLE + coex_enable(); +#endif if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) { btdm_controller_enable_sleep(true); @@ -1399,15 +1439,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_SW_COEXIST_ENABLE + coex_disable(); +#endif + esp_phy_disable(); #ifdef CONFIG_PM_ENABLE if (!s_btdm_allow_light_sleep) { esp_pm_lock_release(s_light_sleep_pm_lock); @@ -1441,14 +1477,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_SW_COEXIST_ENABLE + coex_disable(); +#endif + + esp_phy_disable(); btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; #ifdef CONFIG_PM_ENABLE @@ -1515,13 +1548,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 { @@ -1537,13 +1565,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/bt/controller/lib b/components/bt/controller/lib index d2fd8129aa..97375274e9 160000 --- a/components/bt/controller/lib +++ b/components/bt/controller/lib @@ -1 +1 @@ -Subproject commit d2fd8129aaea47408a76590508443192d82fe2f1 +Subproject commit 97375274e9446551835697c23357e23f3cf23146 diff --git a/components/esp_system/startup.c b/components/esp_system/startup.c index 6edb9359f9..fac1a41538 100644 --- a/components/esp_system/startup.c +++ b/components/esp_system/startup.c @@ -399,12 +399,10 @@ esp_pm_config_esp32s2_t cfg = { #endif #endif -#if CONFIG_IDF_TARGET_ESP32 -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE +#if CONFIG_SW_COEXIST_ENABLE esp_coex_adapter_register(&g_coex_adapter_funcs); coex_pre_init(); #endif -#endif #ifdef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE const esp_partition_t *efuse_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_EFUSE_EM, NULL); diff --git a/components/esp_wifi/esp32/esp_adapter.c b/components/esp_wifi/esp32/esp_adapter.c index 09c283ba7e..d6e17d013e 100644 --- a/components/esp_wifi/esp32/esp_adapter.c +++ b/components/esp_wifi/esp32/esp_adapter.c @@ -51,6 +51,11 @@ #define TAG "esp_adapter" +#ifdef CONFIG_PM_ENABLE +extern void wifi_apb80m_request(void); +extern void wifi_apb80m_release(void); +#endif + static void IRAM_ATTR s_esp_dport_access_stall_other_cpu_start(void) { DPORT_STALL_OTHER_CPU_START(); @@ -174,6 +179,25 @@ static void wifi_delete_queue_wrapper(void *queue) wifi_delete_queue(queue); } +static bool IRAM_ATTR env_is_chip_wrapper(void) +{ +#ifdef CONFIG_IDF_ENV_FPGA + return false; +#else + return true; +#endif +} + +static void set_intr_wrapper(int32_t cpu_no, uint32_t intr_source, uint32_t intr_num, int32_t intr_prio) +{ + intr_matrix_set(cpu_no, intr_source, intr_num); +} + +static void clear_intr_wrapper(uint32_t intr_source, uint32_t intr_num) +{ + +} + static void set_isr_wrapper(int32_t n, void *f, void *arg) { xt_set_interrupt_handler(n, (xt_handler)f, arg); @@ -213,7 +237,7 @@ static void IRAM_ATTR wifi_int_restore_wrapper(void *wifi_int_mux, uint32_t tmp) static bool IRAM_ATTR is_from_isr_wrapper(void) { - return xPortInIsrContext(); + return !xPortCanYield(); } static void IRAM_ATTR task_yield_from_isr_wrapper(void) @@ -391,6 +415,20 @@ static int32_t esp_event_post_wrapper(const char* event_base, int32_t event_id, } } +static void IRAM_ATTR wifi_apb80m_request_wrapper(void) +{ +#ifdef CONFIG_PM_ENABLE + wifi_apb80m_request(); +#endif +} + +static void IRAM_ATTR wifi_apb80m_release_wrapper(void) +{ +#ifdef CONFIG_PM_ENABLE + wifi_apb80m_release(); +#endif +} + static void IRAM_ATTR timer_arm_wrapper(void *timer, uint32_t tmout, bool repeat) { ets_timer_arm(timer, tmout, repeat); @@ -461,9 +499,41 @@ static void * IRAM_ATTR zalloc_internal_wrapper(size_t size) return ptr; } -static uint32_t coex_status_get_wrapper(void) +static int coex_init_wrapper(void) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE +#if CONFIG_SW_COEXIST_ENABLE + return coex_init(); +#else + return 0; +#endif +} + +static void coex_deinit_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE + coex_deinit(); +#endif +} + +static int coex_enable_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_enable(); +#else + return 0; +#endif +} + +static void coex_disable_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE + coex_disable(); +#endif +} + +static IRAM_ATTR uint32_t coex_status_get_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE return coex_status_get(); #else return 0; @@ -479,65 +549,118 @@ static void coex_condition_set_wrapper(uint32_t type, bool dissatisfy) static int coex_wifi_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE +#if CONFIG_SW_COEXIST_ENABLE return coex_wifi_request(event, latency, duration); #else return 0; #endif } -static int coex_wifi_release_wrapper(uint32_t event) +static IRAM_ATTR int coex_wifi_release_wrapper(uint32_t event) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE +#if CONFIG_SW_COEXIST_ENABLE return coex_wifi_release(event); #else return 0; #endif } -int IRAM_ATTR coex_bt_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration) +static int coex_wifi_channel_set_wrapper(uint8_t primary, uint8_t secondary) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - return coex_bt_request(event, latency, duration); +#if CONFIG_SW_COEXIST_ENABLE + return coex_wifi_channel_set(primary, secondary); #else return 0; #endif } -int IRAM_ATTR coex_bt_release_wrapper(uint32_t event) +static IRAM_ATTR int coex_event_duration_get_wrapper(uint32_t event, uint32_t *duration) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - return coex_bt_release(event); +#if CONFIG_SW_COEXIST_ENABLE + return coex_event_duration_get(event, duration); #else return 0; #endif } -int coex_register_bt_cb_wrapper(coex_func_cb_t cb) +static int coex_pti_get_wrapper(uint32_t event, uint8_t *pti) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - return coex_register_bt_cb(cb); + return 0; +} + +static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status) +{ +#if CONFIG_SW_COEXIST_ENABLE + coex_schm_status_bit_clear(type, status); +#endif +} + +static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status) +{ +#if CONFIG_SW_COEXIST_ENABLE + coex_schm_status_bit_set(type, status); +#endif +} + +static IRAM_ATTR int coex_schm_interval_set_wrapper(uint32_t interval) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_schm_interval_set(interval); #else return 0; #endif } -uint32_t IRAM_ATTR coex_bb_reset_lock_wrapper(void) +static uint32_t coex_schm_interval_get_wrapper(void) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - return coex_bb_reset_lock(); +#if CONFIG_SW_COEXIST_ENABLE + return coex_schm_interval_get(); #else return 0; #endif } -void IRAM_ATTR coex_bb_reset_unlock_wrapper(uint32_t restore) +static uint8_t coex_schm_curr_period_get_wrapper(void) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - coex_bb_reset_unlock(restore); +#if CONFIG_SW_COEXIST_ENABLE + return coex_schm_curr_period_get(); +#else + return 0; #endif } +static void * coex_schm_curr_phase_get_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_schm_curr_phase_get(); +#else + return NULL; +#endif +} + +static int coex_schm_curr_phase_idx_set_wrapper(int idx) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_schm_curr_phase_idx_set(idx); +#else + return 0; +#endif +} + +static int coex_schm_curr_phase_idx_get_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_schm_curr_phase_idx_get(); +#else + return 0; +#endif +} + +static void IRAM_ATTR esp_empty_wrapper(void) +{ + +} + int32_t IRAM_ATTR coex_is_in_isr_wrapper(void) { return !xPortCanYield(); @@ -545,9 +668,13 @@ int32_t IRAM_ATTR coex_is_in_isr_wrapper(void) wifi_osi_funcs_t g_wifi_osi_funcs = { ._version = ESP_WIFI_OS_ADAPTER_VERSION, + ._env_is_chip = env_is_chip_wrapper, + ._set_intr = set_intr_wrapper, + ._clear_intr = clear_intr_wrapper, ._set_isr = set_isr_wrapper, ._ints_on = xt_ints_on, ._ints_off = xt_ints_off, + ._is_from_isr = is_from_isr_wrapper, ._spin_lock_create = spin_lock_create_wrapper, ._spin_lock_delete = free, ._wifi_int_disable = wifi_int_disable_wrapper, @@ -590,8 +717,10 @@ 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, + ._wifi_apb80m_request = wifi_apb80m_request_wrapper, + ._wifi_apb80m_release = wifi_apb80m_release_wrapper, + ._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, @@ -604,6 +733,8 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._wifi_reset_mac = wifi_reset_mac_wrapper, ._wifi_clock_enable = wifi_clock_enable_wrapper, ._wifi_clock_disable = wifi_clock_disable_wrapper, + ._wifi_rtc_enable_iso = esp_empty_wrapper, + ._wifi_rtc_disable_iso = esp_empty_wrapper, ._esp_timer_get_time = esp_timer_get_time, ._nvs_set_i8 = nvs_set_i8, ._nvs_get_i8 = nvs_get_i8, @@ -633,15 +764,25 @@ 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_enable = coex_enable_wrapper, + ._coex_disable = coex_disable_wrapper, ._coex_status_get = coex_status_get_wrapper, ._coex_condition_set = coex_condition_set_wrapper, ._coex_wifi_request = coex_wifi_request_wrapper, ._coex_wifi_release = coex_wifi_release_wrapper, - ._is_from_isr = is_from_isr_wrapper, + ._coex_wifi_channel_set = coex_wifi_channel_set_wrapper, + ._coex_event_duration_get = coex_event_duration_get_wrapper, + ._coex_pti_get = coex_pti_get_wrapper, + ._coex_schm_status_bit_clear = coex_schm_status_bit_clear_wrapper, + ._coex_schm_status_bit_set = coex_schm_status_bit_set_wrapper, + ._coex_schm_interval_set = coex_schm_interval_set_wrapper, + ._coex_schm_interval_get = coex_schm_interval_get_wrapper, + ._coex_schm_curr_period_get = coex_schm_curr_period_get_wrapper, + ._coex_schm_curr_phase_get = coex_schm_curr_phase_get_wrapper, + ._coex_schm_curr_phase_idx_set = coex_schm_curr_phase_idx_set_wrapper, + ._coex_schm_curr_phase_idx_get = coex_schm_curr_phase_idx_get_wrapper, ._magic = ESP_WIFI_OS_ADAPTER_MAGIC, }; diff --git a/components/esp_wifi/esp32s2/esp_adapter.c b/components/esp_wifi/esp32s2/esp_adapter.c index 2399f81c46..af6ea90bc3 100644 --- a/components/esp_wifi/esp32s2/esp_adapter.c +++ b/components/esp_wifi/esp32s2/esp_adapter.c @@ -46,11 +46,14 @@ #include "nvs.h" #include "os.h" #include "esp_smartconfig.h" -#include "esp_coexist_internal.h" -#include "esp_coexist_adapter.h" #define TAG "esp_adapter" +#ifdef CONFIG_PM_ENABLE +extern void wifi_apb80m_request(void); +extern void wifi_apb80m_release(void); +#endif + /* If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly. If failed, try to allocate it in internal memory then. @@ -164,6 +167,25 @@ static void wifi_delete_queue_wrapper(void *queue) wifi_delete_queue(queue); } +static bool IRAM_ATTR env_is_chip_wrapper(void) +{ +#ifdef CONFIG_IDF_ENV_FPGA + return false; +#else + return true; +#endif +} + +static void set_intr_wrapper(int32_t cpu_no, uint32_t intr_source, uint32_t intr_num, int32_t intr_prio) +{ + intr_matrix_set(cpu_no, intr_source, intr_num); +} + +static void clear_intr_wrapper(uint32_t intr_source, uint32_t intr_num) +{ + +} + static void set_isr_wrapper(int32_t n, void *f, void *arg) { xt_set_interrupt_handler(n, (xt_handler)f, arg); @@ -203,7 +225,7 @@ static void IRAM_ATTR wifi_int_restore_wrapper(void *wifi_int_mux, uint32_t tmp) static bool IRAM_ATTR is_from_isr_wrapper(void) { - return xPortInIsrContext(); + return !xPortCanYield(); } static void IRAM_ATTR task_yield_from_isr_wrapper(void) @@ -256,16 +278,6 @@ static void * wifi_thread_semphr_get_wrapper(void) return (void*)sem; } -static int32_t IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw) -{ - return (int32_t)xSemaphoreTakeFromISR(semphr, hptw); -} - -static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw) -{ - return (int32_t)xSemaphoreGiveFromISR(semphr, hptw); -} - static int32_t semphr_take_wrapper(void *semphr, uint32_t block_time_tick) { if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) { @@ -381,6 +393,20 @@ static int32_t esp_event_post_wrapper(const char* event_base, int32_t event_id, } } +static void IRAM_ATTR wifi_apb80m_request_wrapper(void) +{ +#ifdef CONFIG_PM_ENABLE + wifi_apb80m_request(); +#endif +} + +static void IRAM_ATTR wifi_apb80m_release_wrapper(void) +{ +#ifdef CONFIG_PM_ENABLE + wifi_apb80m_release(); +#endif +} + static void IRAM_ATTR timer_arm_wrapper(void *timer, uint32_t tmout, bool repeat) { ets_timer_arm(timer, tmout, repeat); @@ -459,9 +485,41 @@ static void * IRAM_ATTR zalloc_internal_wrapper(size_t size) return ptr; } -static uint32_t coex_status_get_wrapper(void) +static int coex_init_wrapper(void) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE +#if CONFIG_SW_COEXIST_ENABLE + return coex_init(); +#else + return 0; +#endif +} + +static void coex_deinit_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE + coex_deinit(); +#endif +} + +static int coex_enable_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_enable(); +#else + return 0; +#endif +} + +static void coex_disable_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE + coex_disable(); +#endif +} + +static IRAM_ATTR uint32_t coex_status_get_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE return coex_status_get(); #else return 0; @@ -477,7 +535,7 @@ static void coex_condition_set_wrapper(uint32_t type, bool dissatisfy) static int coex_wifi_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE +#if CONFIG_SW_COEXIST_ENABLE return coex_wifi_request(event, latency, duration); #else return 0; @@ -486,53 +544,101 @@ static int coex_wifi_request_wrapper(uint32_t event, uint32_t latency, uint32_t static int coex_wifi_release_wrapper(uint32_t event) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE +#if CONFIG_SW_COEXIST_ENABLE return coex_wifi_release(event); #else return 0; #endif } -int IRAM_ATTR coex_bt_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration) +static int coex_wifi_channel_set_wrapper(uint8_t primary, uint8_t secondary) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - return coex_bt_request(event, latency, duration); +#if CONFIG_SW_COEXIST_ENABLE + return coex_wifi_channel_set(primary, secondary); #else return 0; #endif } -int IRAM_ATTR coex_bt_release_wrapper(uint32_t event) +static IRAM_ATTR int coex_event_duration_get_wrapper(uint32_t event, uint32_t *duration) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - return coex_bt_release(event); +#if CONFIG_SW_COEXIST_ENABLE + return coex_event_duration_get(event, duration); #else return 0; #endif } -int coex_register_bt_cb_wrapper(coex_func_cb_t cb) +static int coex_pti_get_wrapper(uint32_t event, uint8_t *pti) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - return coex_register_bt_cb(cb); + return 0; +} + +static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status) +{ +#if CONFIG_SW_COEXIST_ENABLE + coex_schm_status_bit_clear(type, status); +#endif +} + +static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status) +{ +#if CONFIG_SW_COEXIST_ENABLE + coex_schm_status_bit_set(type, status); +#endif +} + +static IRAM_ATTR int coex_schm_interval_set_wrapper(uint32_t interval) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_schm_interval_set(interval); #else return 0; #endif } -uint32_t IRAM_ATTR coex_bb_reset_lock_wrapper(void) +static uint32_t coex_schm_interval_get_wrapper(void) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - return coex_bb_reset_lock(); +#if CONFIG_SW_COEXIST_ENABLE + return coex_schm_interval_get(); #else return 0; #endif } -void IRAM_ATTR coex_bb_reset_unlock_wrapper(uint32_t restore) +static uint8_t coex_schm_curr_period_get_wrapper(void) { -#if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE - coex_bb_reset_unlock(restore); +#if CONFIG_SW_COEXIST_ENABLE + return coex_schm_curr_period_get(); +#else + return 0; +#endif +} + +static void * coex_schm_curr_phase_get_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_schm_curr_phase_get(); +#else + return NULL; +#endif +} + +static int coex_schm_curr_phase_idx_set_wrapper(int idx) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_schm_curr_phase_idx_set(idx); +#else + return 0; +#endif +} + +static int coex_schm_curr_phase_idx_get_wrapper(void) +{ +#if CONFIG_SW_COEXIST_ENABLE + return coex_schm_curr_phase_idx_get(); +#else + return 0; #endif } @@ -543,9 +649,13 @@ static void IRAM_ATTR esp_empty_wrapper(void) wifi_osi_funcs_t g_wifi_osi_funcs = { ._version = ESP_WIFI_OS_ADAPTER_VERSION, + ._env_is_chip = env_is_chip_wrapper, + ._set_intr = set_intr_wrapper, + ._clear_intr = clear_intr_wrapper, ._set_isr = set_isr_wrapper, ._ints_on = xt_ints_on, ._ints_off = xt_ints_off, + ._is_from_isr = is_from_isr_wrapper, ._spin_lock_create = spin_lock_create_wrapper, ._spin_lock_delete = free, ._wifi_int_disable = wifi_int_disable_wrapper, @@ -588,8 +698,10 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._rand = esp_random, ._dport_access_stall_other_cpu_start_wrap = esp_empty_wrapper, ._dport_access_stall_other_cpu_end_wrap = esp_empty_wrapper, - ._phy_rf_deinit = esp_phy_rf_deinit, - ._phy_load_cal_and_init = esp_phy_load_cal_and_init, + ._wifi_apb80m_request = wifi_apb80m_request_wrapper, + ._wifi_apb80m_release = wifi_apb80m_release_wrapper, + ._phy_disable = esp_phy_disable, + ._phy_enable = esp_phy_enable, ._phy_update_country_info = esp_phy_update_country_info, ._read_mac = esp_read_mac, ._timer_arm = timer_arm_wrapper, @@ -600,6 +712,8 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._wifi_reset_mac = wifi_reset_mac_wrapper, ._wifi_clock_enable = wifi_clock_enable_wrapper, ._wifi_clock_disable = wifi_clock_disable_wrapper, + ._wifi_rtc_enable_iso = esp_empty_wrapper, + ._wifi_rtc_disable_iso = esp_empty_wrapper, ._esp_timer_get_time = esp_timer_get_time, ._nvs_set_i8 = nvs_set_i8, ._nvs_get_i8 = nvs_get_i8, @@ -616,9 +730,7 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._get_random = os_get_random, ._get_time = get_time_wrapper, ._random = os_random, -#if CONFIG_IDF_TARGET_ESP32S2 ._slowclk_cal_get = esp_clk_slowclk_cal_get_wrapper, -#endif ._log_write = esp_log_write, ._log_writev = esp_log_writev, ._log_timestamp = esp_log_timestamp, @@ -632,38 +744,24 @@ 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_enable = coex_enable_wrapper, + ._coex_disable = coex_disable_wrapper, ._coex_status_get = coex_status_get_wrapper, ._coex_condition_set = coex_condition_set_wrapper, ._coex_wifi_request = coex_wifi_request_wrapper, ._coex_wifi_release = coex_wifi_release_wrapper, - ._is_from_isr = is_from_isr_wrapper, + ._coex_wifi_channel_set = coex_wifi_channel_set_wrapper, + ._coex_event_duration_get = coex_event_duration_get_wrapper, + ._coex_pti_get = coex_pti_get_wrapper, + ._coex_schm_status_bit_clear = coex_schm_status_bit_clear_wrapper, + ._coex_schm_status_bit_set = coex_schm_status_bit_set_wrapper, + ._coex_schm_interval_set = coex_schm_interval_set_wrapper, + ._coex_schm_interval_get = coex_schm_interval_get_wrapper, + ._coex_schm_curr_period_get = coex_schm_curr_period_get_wrapper, + ._coex_schm_curr_phase_get = coex_schm_curr_phase_get_wrapper, + ._coex_schm_curr_phase_idx_set = coex_schm_curr_phase_idx_set_wrapper, + ._coex_schm_curr_phase_idx_get = coex_schm_curr_phase_idx_get_wrapper, ._magic = ESP_WIFI_OS_ADAPTER_MAGIC, }; - -coex_adapter_funcs_t g_coex_adapter_funcs = { - ._version = COEX_ADAPTER_VERSION, - ._spin_lock_create = spin_lock_create_wrapper, - ._spin_lock_delete = free, - ._int_disable = wifi_int_disable_wrapper, - ._int_enable = wifi_int_restore_wrapper, - ._task_yield_from_isr = task_yield_from_isr_wrapper, - ._semphr_create = semphr_create_wrapper, - ._semphr_delete = semphr_delete_wrapper, - ._semphr_take_from_isr = semphr_take_from_isr_wrapper, - ._semphr_give_from_isr = semphr_give_from_isr_wrapper, - ._semphr_take = semphr_take_wrapper, - ._semphr_give = semphr_give_wrapper, - ._is_in_isr = xPortInIsrContext, - ._malloc_internal = malloc_internal_wrapper, - ._free = free, - ._timer_disarm = timer_disarm_wrapper, - ._timer_done = timer_done_wrapper, - ._timer_setfn = timer_setfn_wrapper, - ._timer_arm_us = timer_arm_us_wrapper, - ._esp_timer_get_time = esp_timer_get_time, - ._magic = COEX_ADAPTER_MAGIC, -}; diff --git a/components/esp_wifi/include/esp_coexist_adapter.h b/components/esp_wifi/include/esp_coexist_adapter.h index f2a38c31e6..ecf5f6e67a 100644 --- a/components/esp_wifi/include/esp_coexist_adapter.h +++ b/components/esp_wifi/include/esp_coexist_adapter.h @@ -21,17 +21,19 @@ extern "C" { #endif -#define COEX_ADAPTER_VERSION 0x00000001 +#define COEX_ADAPTER_VERSION 0x00000002 #define COEX_ADAPTER_MAGIC 0xDEADBEAF #define COEX_ADAPTER_FUNCS_TIME_BLOCKING 0xffffffff typedef struct { int32_t _version; +#if CONFIG_IDF_TARGET_ESP32 void *(* _spin_lock_create)(void); void (* _spin_lock_delete)(void *lock); uint32_t (*_int_disable)(void *mux); void (*_int_enable)(void *mux, uint32_t tmp); +#endif void (*_task_yield_from_isr)(void); void *(*_semphr_create)(uint32_t max, uint32_t init); void (*_semphr_delete)(void *semphr); @@ -42,10 +44,12 @@ typedef struct { int32_t (* _is_in_isr)(void); void * (* _malloc_internal)(size_t size); void (* _free)(void *p); +#if CONFIG_IDF_TARGET_ESP32 void (* _timer_disarm)(void *timer); 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); +#endif int64_t (* _esp_timer_get_time)(void); int32_t _magic; } coex_adapter_funcs_t; diff --git a/components/esp_wifi/include/esp_coexist_internal.h b/components/esp_wifi/include/esp_coexist_internal.h index bdd023a93d..848e6d11ab 100644 --- a/components/esp_wifi/include/esp_coexist_internal.h +++ b/components/esp_wifi/include/esp_coexist_internal.h @@ -54,16 +54,18 @@ esp_err_t coex_init(void); void coex_deinit(void); /** - * @brief Pause software coexist + * @brief Enable software coexist * extern function for internal use. + * + * @return Enable ok or failed. */ -void coex_pause(void); +esp_err_t coex_enable(void); /** - * @brief Resume software coexist + * @brief Disable software coexist * extern function for internal use. */ -void coex_resume(void); +void coex_disable(void); /** * @brief Get software coexist version string @@ -112,44 +114,82 @@ int coex_wifi_request(uint32_t event, uint32_t latency, uint32_t duration); int coex_wifi_release(uint32_t event); /** - * @brief Blue tooth requests coexistence. + * @brief Set WiFi channel to coexistence module. * - * @param event : blue tooth event - * @param latency : blue tooth will request coexistence after latency - * @param duration : duration for blue tooth to request coexistence + * @param primary : WiFi primary channel + * @param secondary : WiFi secondary channel * @return : 0 - success, other - failed */ -int coex_bt_request(uint32_t event, uint32_t latency, uint32_t duration); +int coex_wifi_channel_set(uint8_t primary, uint8_t secondary); /** - * @brief Blue tooth release coexistence. + * @brief Get coexistence event duration. * - * @param event : blue tooth event + * @param event : Coexistence event + * @param duration: Coexistence event duration * @return : 0 - success, other - failed */ -int coex_bt_release(uint32_t event); +int coex_event_duration_get(uint32_t event, uint32_t *duration); /** - * @brief Register callback function for blue tooth. + * @brief Clear coexistence status. * - * @param cb : callback function + * @param type : Coexistence status type + * @param status: Coexistence status + */ +void coex_schm_status_bit_clear(uint32_t type, uint32_t status); + +/** + * @brief Set coexistence status. + * + * @param type : Coexistence status type + * @param status: Coexistence status + */ +void coex_schm_status_bit_set(uint32_t type, uint32_t status); + +/** + * @brief Set coexistence scheme interval. + * + * @param interval : Coexistence scheme interval * @return : 0 - success, other - failed */ -int coex_register_bt_cb(coex_func_cb_t cb); +int coex_schm_interval_set(uint32_t interval); /** - * @brief Lock before reset base band. + * @brief Get coexistence scheme interval. * - * @return : lock value + * @return : Coexistence scheme interval */ -uint32_t coex_bb_reset_lock(void); +uint32_t coex_schm_interval_get(void); /** - * @brief Unlock after reset base band. + * @brief Get current coexistence scheme period. * - * @param restore : lock value + * @return : Coexistence scheme period */ -void coex_bb_reset_unlock(uint32_t restore); +uint8_t coex_schm_curr_period_get(void); + +/** + * @brief Get current coexistence scheme phase. + * + * @return : Coexistence scheme phase + */ +void * coex_schm_curr_phase_get(void); + +/** + * @brief Set current coexistence scheme phase index. + * + * @param interval : Coexistence scheme phase index + * @return : 0 - success, other - failed + */ +int coex_schm_curr_phase_idx_set(int idx); + +/** + * @brief Get current coexistence scheme phase index. + * + * @return : Coexistence scheme phase index + */ +int coex_schm_curr_phase_idx_get(void); /** * @brief Register coexistence adapter functions. diff --git a/components/esp_wifi/include/esp_phy_init.h b/components/esp_wifi/include/esp_phy_init.h index 2e5953a081..6bc0601fae 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(); - } - 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" @@ -709,7 +469,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) { esp_phy_calibration_data_t* cal_data = (esp_phy_calibration_data_t*) calloc(sizeof(esp_phy_calibration_data_t), 1); @@ -760,15 +520,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/src/wifi_init.c b/components/esp_wifi/src/wifi_init.c index 0159e48fa4..cbea8077b9 100644 --- a/components/esp_wifi/src/wifi_init.c +++ b/components/esp_wifi/src/wifi_init.c @@ -24,6 +24,7 @@ #include "esp_netif.h" #include "tcpip_adapter_compatible/tcpip_adapter_compat.h" #include "driver/adc2_wifi_private.h" +#include "esp_coexist_internal.h" #if (CONFIG_ESP32_WIFI_RX_BA_WIN > CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM) #error "WiFi configuration check: WARNING, WIFI_RX_BA_WIN should not be larger than WIFI_DYNAMIC_RX_BUFFER_NUM!" @@ -199,6 +200,9 @@ esp_err_t esp_wifi_init(const wifi_init_config_t *config) if (err != ESP_OK) { ESP_LOGW(TAG, "Failed to set default Wi-Fi event handlers (0x%x)", err); } +#endif +#if CONFIG_SW_COEXIST_ENABLE + coex_init(); #endif esp_err_t result = esp_wifi_init_internal(config); if (result == ESP_OK) { 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 diff --git a/components/wpa_supplicant/src/esp_supplicant/esp_wifi_driver.h b/components/wpa_supplicant/src/esp_supplicant/esp_wifi_driver.h index 2b2a7ca008..140d6d1e8a 100644 --- a/components/wpa_supplicant/src/esp_supplicant/esp_wifi_driver.h +++ b/components/wpa_supplicant/src/esp_supplicant/esp_wifi_driver.h @@ -40,7 +40,7 @@ enum { WIFI_WPA_ALG_GCMP }; -enum { +typedef enum { WIFI_APPIE_PROBEREQ = 0, WIFI_APPIE_ASSOC_REQ, WIFI_APPIE_ASSOC_RESP, @@ -53,7 +53,7 @@ enum { WIFI_APPIE_ESP_MANUFACTOR, WIFI_APPIE_COUNTRY, WIFI_APPIE_MAX, -}; +} wifi_appie_t; enum { NONE_AUTH = 0x01, @@ -195,6 +195,7 @@ void esp_wifi_deauthenticate_internal(u8 reason_code); bool esp_wifi_sta_is_running_internal(void); bool esp_wifi_auth_done_internal(void); int esp_wifi_set_ap_key_internal(int alg, const u8 *addr, int idx, u8 *key, size_t key_len); +int esp_wifi_get_sta_hw_key_idx_internal(int key_idx); int esp_wifi_set_sta_key_internal(int alg, u8 *addr, int key_idx, int set_tx, u8 *seq, size_t seq_len, u8 *key, size_t key_len, int key_entry_valid); int esp_wifi_get_sta_key_internal(uint8_t *ifx, int *alg, u8 *addr, int *key_idx, diff --git a/components/wpa_supplicant/src/rsn_supp/wpa.c b/components/wpa_supplicant/src/rsn_supp/wpa.c index 064bd59a6f..deafceac15 100644 --- a/components/wpa_supplicant/src/rsn_supp/wpa.c +++ b/components/wpa_supplicant/src/rsn_supp/wpa.c @@ -672,7 +672,7 @@ int wpa_supplicant_install_ptk(struct wpa_sm *sm) } //now only use keyentry 0 for pairwise key - sm->key_entry_valid = 5; + sm->key_entry_valid = esp_wifi_get_sta_hw_key_idx_internal(0); //KEY_IDX_STA_PTK if (wpa_sm_set_key(&(sm->install_ptk), alg, sm->bssid, 0, 1, (sm->install_ptk).seq, WPA_KEY_RSC_LEN, (u8 *) sm->ptk.tk1, keylen,sm->key_entry_valid) < 0) { @@ -808,7 +808,7 @@ int wpa_supplicant_install_gtk(struct wpa_sm *sm, _gtk = gtk_buf; } //now only use keycache entry1 for group key - sm->key_entry_valid = gd->keyidx; + sm->key_entry_valid = esp_wifi_get_sta_hw_key_idx_internal(gd->keyidx); if (sm->pairwise_cipher == WPA_CIPHER_NONE) { if (wpa_sm_set_key(&(sm->install_gtk), gd->alg, sm->bssid, //(u8 *) "\xff\xff\xff\xff\xff\xff",