From c656e7ec0745ec908d7f3307cbda0c20076e4d0c Mon Sep 17 00:00:00 2001 From: zwj Date: Thu, 8 Dec 2022 11:54:26 +0800 Subject: [PATCH] Fixed some memory was not released after bluetooth controller initialization failed on ESP32-C3 and ESP32-S3 --- components/bt/controller/esp32c3/bt.c | 115 ++++++-------------- components/bt/controller/esp32s3/bt.c | 114 ++++++------------- components/bt/controller/lib_esp32c3_family | 2 +- 3 files changed, 72 insertions(+), 159 deletions(-) diff --git a/components/bt/controller/esp32c3/bt.c b/components/bt/controller/esp32c3/bt.c index 410f63f6f06a..5d9844479211 100644 --- a/components/bt/controller/esp32c3/bt.c +++ b/components/bt/controller/esp32c3/bt.c @@ -314,6 +314,8 @@ static void btdm_slp_tmr_callback(void *arg); static esp_err_t try_heap_caps_add_region(intptr_t start, intptr_t end); +static void bt_controller_deinit_internal(void); + /* Local variable definition *************************************************************************** */ @@ -1272,73 +1274,9 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) return ESP_OK; error: - if (s_lp_stat.phy_enabled) { - esp_phy_disable(); - s_lp_stat.phy_enabled = 0; - } - - do { - // deinit low power control resources -#ifdef CONFIG_PM_ENABLE - if (s_lp_cntl.no_light_sleep) { - if (s_light_sleep_pm_lock != NULL) { - esp_pm_lock_delete(s_light_sleep_pm_lock); - s_light_sleep_pm_lock = NULL; - } - } - if (s_pm_lock != NULL) { - esp_pm_lock_delete(s_pm_lock); - s_pm_lock = NULL; - s_lp_stat.pm_lock_released = 0; - } - -#endif - if (s_lp_cntl.wakeup_timer_required && s_btdm_slp_tmr != NULL) { - esp_timer_delete(s_btdm_slp_tmr); - s_btdm_slp_tmr = NULL; - } - -#if CONFIG_MAC_BB_PD - if (s_lp_cntl.mac_bb_pd) { - btdm_deep_sleep_mem_deinit(); - s_lp_cntl.mac_bb_pd = 0; - } -#endif - if (s_lp_cntl.enable) { - btdm_vnd_offload_task_deregister(BTDM_VND_OL_SIG_WAKEUP_TMR); - if (s_wakeup_req_sem != NULL) { - semphr_delete_wrapper(s_wakeup_req_sem); - s_wakeup_req_sem = NULL; - } - } - - if (s_lp_cntl.lpclk_sel == BTDM_LPCLK_SEL_XTAL) { -#ifdef CONFIG_BT_CTRL_MAIN_XTAL_PU_DURING_LIGHT_SLEEP - if (s_lp_cntl.main_xtal_pu) { - ESP_ERROR_CHECK(esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_OFF)); - s_lp_cntl.main_xtal_pu = 0; - } -#endif - btdm_lpclk_select_src(BTDM_LPCLK_SEL_RTC_SLOW); - btdm_lpclk_set_div(0); -#if CONFIG_SW_COEXIST_ENABLE - coex_update_lpclk_interval(); -#endif - } - btdm_lpcycle_us = 0; - } while (0); - -#if CONFIG_MAC_BB_PD - esp_unregister_mac_bb_pd_callback(btdm_mac_bb_power_down_cb); + bt_controller_deinit_internal(); - esp_unregister_mac_bb_pu_callback(btdm_mac_bb_power_up_cb); -#endif - - if (osi_funcs_p != NULL) { - free(osi_funcs_p); - osi_funcs_p = NULL; - } return err; } @@ -1349,31 +1287,47 @@ esp_err_t esp_bt_controller_deinit(void) } btdm_controller_deinit(); + + bt_controller_deinit_internal(); + + return ESP_OK; +} + +static void bt_controller_deinit_internal(void) +{ periph_module_disable(PERIPH_BT_MODULE); if (s_lp_stat.phy_enabled) { esp_phy_disable(); s_lp_stat.phy_enabled = 0; - } else { - assert(0); } // deinit low power control resources do { + #if CONFIG_MAC_BB_PD - btdm_deep_sleep_mem_deinit(); + if (s_lp_cntl.mac_bb_pd) { + btdm_deep_sleep_mem_deinit(); + s_lp_cntl.mac_bb_pd = 0; + } #endif #ifdef CONFIG_PM_ENABLE if (s_lp_cntl.no_light_sleep) { - esp_pm_lock_delete(s_light_sleep_pm_lock); - s_light_sleep_pm_lock = NULL; + if (s_light_sleep_pm_lock != NULL) { + esp_pm_lock_delete(s_light_sleep_pm_lock); + s_light_sleep_pm_lock = NULL; + } + } + + if (s_pm_lock != NULL) { + esp_pm_lock_delete(s_pm_lock); + s_pm_lock = NULL; + s_lp_stat.pm_lock_released = 0; } - esp_pm_lock_delete(s_pm_lock); - s_pm_lock = NULL; - s_lp_stat.pm_lock_released = 0; #endif + if (s_lp_cntl.wakeup_timer_required) { if (s_lp_stat.wakeup_timer_started) { esp_timer_stop(s_btdm_slp_tmr); @@ -1385,10 +1339,12 @@ esp_err_t esp_bt_controller_deinit(void) if (s_lp_cntl.enable) { btdm_vnd_offload_task_deregister(BTDM_VND_OL_SIG_WAKEUP_TMR); - - semphr_delete_wrapper(s_wakeup_req_sem); - s_wakeup_req_sem = NULL; + if (s_wakeup_req_sem != NULL) { + semphr_delete_wrapper(s_wakeup_req_sem); + s_wakeup_req_sem = NULL; + } } + if (s_lp_cntl.lpclk_sel == BTDM_LPCLK_SEL_XTAL) { #ifdef CONFIG_BT_CTRL_MAIN_XTAL_PU_DURING_LIGHT_SLEEP if (s_lp_cntl.main_xtal_pu) { @@ -1417,11 +1373,12 @@ esp_err_t esp_bt_controller_deinit(void) #endif esp_phy_modem_deinit(); - free(osi_funcs_p); - osi_funcs_p = NULL; + if (osi_funcs_p != NULL) { + free(osi_funcs_p); + osi_funcs_p = NULL; + } btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE; - return ESP_OK; } esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) diff --git a/components/bt/controller/esp32s3/bt.c b/components/bt/controller/esp32s3/bt.c index 49414877006d..9e625dc05bc3 100644 --- a/components/bt/controller/esp32s3/bt.c +++ b/components/bt/controller/esp32s3/bt.c @@ -318,6 +318,8 @@ static void btdm_slp_tmr_callback(void *arg); static esp_err_t try_heap_caps_add_region(intptr_t start, intptr_t end); +static void bt_controller_deinit_internal(void); + /* Local variable definition *************************************************************************** */ @@ -1318,73 +1320,9 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) return ESP_OK; error: - if (s_lp_stat.phy_enabled) { - esp_phy_disable(); - s_lp_stat.phy_enabled = 0; - } - do { - // deinit low power control resources -#ifdef CONFIG_PM_ENABLE - if (s_lp_cntl.no_light_sleep) { - if (s_light_sleep_pm_lock != NULL) { - esp_pm_lock_delete(s_light_sleep_pm_lock); - s_light_sleep_pm_lock = NULL; - } - } - if (s_pm_lock != NULL) { - esp_pm_lock_delete(s_pm_lock); - s_pm_lock = NULL; - s_lp_stat.pm_lock_released = 0; - } - -#endif - if (s_lp_cntl.wakeup_timer_required && s_btdm_slp_tmr != NULL) { - esp_timer_delete(s_btdm_slp_tmr); - s_btdm_slp_tmr = NULL; - } - -#if CONFIG_MAC_BB_PD - if (s_lp_cntl.mac_bb_pd) { - btdm_deep_sleep_mem_deinit(); - s_lp_cntl.mac_bb_pd = 0; - } -#endif - if (s_lp_cntl.enable) { - btdm_vnd_offload_task_deregister(BTDM_VND_OL_SIG_WAKEUP_TMR); - if (s_wakeup_req_sem != NULL) { - semphr_delete_wrapper(s_wakeup_req_sem); - s_wakeup_req_sem = NULL; - } - } + bt_controller_deinit_internal(); - if (s_lp_cntl.lpclk_sel == BTDM_LPCLK_SEL_XTAL) { -#ifdef CONFIG_BT_CTRL_MAIN_XTAL_PU_DURING_LIGHT_SLEEP - if (s_lp_cntl.main_xtal_pu) { - ESP_ERROR_CHECK(esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_OFF)); - s_lp_cntl.main_xtal_pu = 0; - } -#endif - btdm_lpclk_select_src(BTDM_LPCLK_SEL_RTC_SLOW); - btdm_lpclk_set_div(0); -#if CONFIG_SW_COEXIST_ENABLE - coex_update_lpclk_interval(); -#endif - } - - btdm_lpcycle_us = 0; - } while (0); - -#if CONFIG_MAC_BB_PD - esp_unregister_mac_bb_pd_callback(btdm_mac_bb_power_down_cb); - - esp_unregister_mac_bb_pu_callback(btdm_mac_bb_power_up_cb); -#endif - - if (osi_funcs_p != NULL) { - free(osi_funcs_p); - osi_funcs_p = NULL; - } return err; } @@ -1395,31 +1333,47 @@ esp_err_t esp_bt_controller_deinit(void) } btdm_controller_deinit(); + + bt_controller_deinit_internal(); + + return ESP_OK; +} + +static void bt_controller_deinit_internal(void) +{ periph_module_disable(PERIPH_BT_MODULE); if (s_lp_stat.phy_enabled) { esp_phy_disable(); s_lp_stat.phy_enabled = 0; - } else { - assert(0); } // deinit low power control resources do { + #if CONFIG_MAC_BB_PD - btdm_deep_sleep_mem_deinit(); + if (s_lp_cntl.mac_bb_pd) { + btdm_deep_sleep_mem_deinit(); + s_lp_cntl.mac_bb_pd = 0; + } #endif #ifdef CONFIG_PM_ENABLE if (s_lp_cntl.no_light_sleep) { - esp_pm_lock_delete(s_light_sleep_pm_lock); - s_light_sleep_pm_lock = NULL; + if (s_light_sleep_pm_lock != NULL) { + esp_pm_lock_delete(s_light_sleep_pm_lock); + s_light_sleep_pm_lock = NULL; + } + } + + if (s_pm_lock != NULL) { + esp_pm_lock_delete(s_pm_lock); + s_pm_lock = NULL; + s_lp_stat.pm_lock_released = 0; } - esp_pm_lock_delete(s_pm_lock); - s_pm_lock = NULL; - s_lp_stat.pm_lock_released = 0; #endif + if (s_lp_cntl.wakeup_timer_required) { if (s_lp_stat.wakeup_timer_started) { esp_timer_stop(s_btdm_slp_tmr); @@ -1431,9 +1385,10 @@ esp_err_t esp_bt_controller_deinit(void) if (s_lp_cntl.enable) { btdm_vnd_offload_task_deregister(BTDM_VND_OL_SIG_WAKEUP_TMR); - - semphr_delete_wrapper(s_wakeup_req_sem); - s_wakeup_req_sem = NULL; + if (s_wakeup_req_sem != NULL) { + semphr_delete_wrapper(s_wakeup_req_sem); + s_wakeup_req_sem = NULL; + } } if (s_lp_cntl.lpclk_sel == BTDM_LPCLK_SEL_XTAL) { @@ -1464,11 +1419,12 @@ esp_err_t esp_bt_controller_deinit(void) #endif esp_phy_modem_deinit(); - free(osi_funcs_p); - osi_funcs_p = NULL; + if (osi_funcs_p != NULL) { + free(osi_funcs_p); + osi_funcs_p = NULL; + } btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE; - return ESP_OK; } esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) diff --git a/components/bt/controller/lib_esp32c3_family b/components/bt/controller/lib_esp32c3_family index 79152b519023..723f44c70cee 160000 --- a/components/bt/controller/lib_esp32c3_family +++ b/components/bt/controller/lib_esp32c3_family @@ -1 +1 @@ -Subproject commit 79152b519023f26462498f3ef8805cff2a80e193 +Subproject commit 723f44c70ceea734bb9075b3c59993103d1dac23