From e3aac6f7035b2aa18a76ce1b412924a06f0b4e0b Mon Sep 17 00:00:00 2001 From: zhanghaipeng Date: Thu, 19 Mar 2026 14:41:41 +0800 Subject: [PATCH] fix(ble): Fixed BLE assert in "emi.c" when low memory --- components/bt/controller/esp32/bt.c | 26 +++++++++++++++----------- components/bt/controller/esp32c3/bt.c | 18 ++++++++++++------ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/components/bt/controller/esp32/bt.c b/components/bt/controller/esp32/bt.c index beb5c228b2..1e06d46246 100644 --- a/components/bt/controller/esp32/bt.c +++ b/components/bt/controller/esp32/bt.c @@ -1692,32 +1692,31 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) ESP_LOGI(BTDM_LOG_TAG, "BT controller compile version [%s]", btdm_controller_get_compile_version()); - s_wakeup_req_sem = semphr_create_wrapper(1, 0); - if (s_wakeup_req_sem == NULL) { - err = ESP_ERR_NO_MEM; - goto error; - } - esp_phy_modem_init(); esp_bt_power_domain_on(); btdm_controller_mem_init(); + /* Must call periph_module_enable(BT) before any step that may goto error, + * otherwise bt_controller_deinit_internal() will call periph_module_disable(BT) + * when ref is still 0, causing ref underflow (0-1=255) and subsequent + * init/enable failures (EM BASE MISMATCH, BLE assert, etc.) */ periph_module_enable(PERIPH_BT_MODULE); periph_module_reset(PERIPH_BT_MODULE); + s_wakeup_req_sem = semphr_create_wrapper(1, 0); + if (s_wakeup_req_sem == NULL) { + err = ESP_ERR_NO_MEM; + goto error; + } + #if CONFIG_BTDM_CTRL_HCI_UART_FLOW_CTRL_EN sdk_config_set_uart_flow_ctrl_enable(true); #else sdk_config_set_uart_flow_ctrl_enable(false); #endif - if ((err = btdm_low_power_mode_init()) != ESP_OK) { - ESP_LOGE(BTDM_LOG_TAG, "Low power module initialization failed"); - goto error; - } - #if CONFIG_SW_COEXIST_ENABLE coex_init(); #endif @@ -1738,6 +1737,11 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) #endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED #endif /* CONFIG_BLE_LOG_ENABLED */ + if ((err = btdm_low_power_mode_init()) != ESP_OK) { + ESP_LOGE(BTDM_LOG_TAG, "Low power module initialization failed"); + goto error; + } + btdm_cfg_mask = btdm_config_mask_load(); err = btdm_controller_init(btdm_cfg_mask, cfg); diff --git a/components/bt/controller/esp32c3/bt.c b/components/bt/controller/esp32c3/bt.c index 56c95f2484..32b81d7fa5 100644 --- a/components/bt/controller/esp32c3/bt.c +++ b/components/bt/controller/esp32c3/bt.c @@ -1867,15 +1867,18 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) ESP_LOGI(BT_LOG_TAG,"Put all controller code in flash"); #endif - if ((err = btdm_low_power_mode_init(cfg)) != ESP_OK) { - ESP_LOGE(BT_LOG_TAG, "Low power module initialization failed"); - goto error; - } #if CONFIG_SW_COEXIST_ENABLE coex_init(); #endif + /* Must call periph_module_enable(BT) before any step that may goto error, + * otherwise bt_controller_deinit_internal() will call periph_module_disable(BT) + * when ref is still 0, causing ref underflow (0-1=255) and subsequent + * init/enable failures (EM BASE MISMATCH, BLE assert, etc.) */ + periph_module_enable(PERIPH_BT_MODULE); + periph_module_reset(PERIPH_BT_MODULE); + #if CONFIG_BLE_LOG_ENABLED if (!ble_log_init()) { ESP_LOGE(BT_LOG_TAG, "BLE Log v2 init failed"); @@ -1892,8 +1895,11 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) #endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED #endif /* CONFIG_BLE_LOG_ENABLED */ - periph_module_enable(PERIPH_BT_MODULE); - periph_module_reset(PERIPH_BT_MODULE); +if ((err = btdm_low_power_mode_init(cfg)) != ESP_OK) { + ESP_LOGE(BT_LOG_TAG, "Low power module initialization failed"); + goto error; +} + #if CONFIG_BT_CTRL_LE_LOG_EN err = esp_bt_controller_log_init(log_output_mode);