diff --git a/components/bt/controller/esp32c3/bt.c b/components/bt/controller/esp32c3/bt.c index 9296b86ec4..f757fac3c7 100644 --- a/components/bt/controller/esp32c3/bt.c +++ b/components/bt/controller/esp32c3/bt.c @@ -1244,6 +1244,9 @@ esp_err_t esp_bt_controller_deinit(void) phy_init_flag(); esp_bt_power_domain_off(); +#if CONFIG_MAC_BB_PD + esp_mac_bb_pd_mem_deinit(); +#endif free(osi_funcs_p); osi_funcs_p = NULL; diff --git a/components/esp_phy/include/esp_phy_init.h b/components/esp_phy/include/esp_phy_init.h index b2562ef11e..56054d82cd 100644 --- a/components/esp_phy/include/esp_phy_init.h +++ b/components/esp_phy/include/esp_phy_init.h @@ -181,6 +181,11 @@ void esp_phy_load_cal_and_init(void); */ void esp_mac_bb_pd_mem_init(void); +/** + * @brief Deinitialize backup memory for MAC and Baseband power up/down + */ +void esp_mac_bb_pd_mem_deinit(void); + /** * @brief Power up MAC and Baseband */ diff --git a/components/esp_phy/src/phy_init.c b/components/esp_phy/src/phy_init.c index fcfb4cd68c..0be0acd881 100644 --- a/components/esp_phy/src/phy_init.c +++ b/components/esp_phy/src/phy_init.c @@ -79,6 +79,8 @@ static uint32_t* s_phy_digital_regs_mem = NULL; #if CONFIG_MAC_BB_PD uint32_t* s_mac_bb_pd_mem = NULL; +/* Reference count of MAC BB backup memory */ +static uint8_t s_backup_mem_ref = 0; #endif #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN @@ -317,6 +319,7 @@ void esp_mac_bb_pd_mem_init(void) { _lock_acquire(&s_phy_access_lock); + s_backup_mem_ref++; if (s_mac_bb_pd_mem == NULL) { s_mac_bb_pd_mem = (uint32_t *)heap_caps_malloc(SOC_MAC_BB_PD_MEM_SIZE, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); } @@ -324,6 +327,19 @@ void esp_mac_bb_pd_mem_init(void) _lock_release(&s_phy_access_lock); } +void esp_mac_bb_pd_mem_deinit(void) +{ + _lock_acquire(&s_phy_access_lock); + + s_backup_mem_ref--; + if (s_backup_mem_ref == 0) { + free(s_mac_bb_pd_mem); + s_mac_bb_pd_mem = NULL; + } + + _lock_release(&s_phy_access_lock); +} + IRAM_ATTR void esp_mac_bb_power_up(void) { if (s_mac_bb_pd_mem == NULL) { diff --git a/components/esp_wifi/src/wifi_init.c b/components/esp_wifi/src/wifi_init.c index 351f8a5600..eb6d4945c6 100644 --- a/components/esp_wifi/src/wifi_init.c +++ b/components/esp_wifi/src/wifi_init.c @@ -129,6 +129,9 @@ esp_err_t esp_wifi_deinit(void) phy_init_flag(); #endif esp_wifi_power_domain_off(); +#if CONFIG_MAC_BB_PD + esp_mac_bb_pd_mem_deinit(); +#endif return err; }