Page 1 of 1

ESP32C3蓝牙反初始化出错,反初始化后再次初始化显示内存错误

Posted: Sun Aug 24, 2025 1:49 pm
by jungsl
使用的是gatt_server_service_table示例进行修改,
反初始化的代码如下
void esp_ble_deinit(void)
{
esp_err_t ret;
ESP_LOGI(TAG, "Starting BLE deinitialization");

// 1. 停止广播
esp_ble_gap_stop_advertising();
vTaskDelay(pdMS_TO_TICKS(200));

// 2. 反注册GATT应用(仅在已注册的情况下)
if (heart_rate_profile_tab[PROFILE_APP_IDX].gatts_if != ESP_GATT_IF_NONE)
{
ret = esp_ble_gatts_app_unregister(heart_rate_profile_tab[PROFILE_APP_IDX].gatts_if);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "GATT app unregister failed: 0x%x", ret);
}
vTaskDelay(pdMS_TO_TICKS(100));
}

// 3. 禁用Bluedroid
if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_ENABLED)
{
esp_bluedroid_disable();
ESP_LOGI(TAG, "Bluedroid disabled");
vTaskDelay(pdMS_TO_TICKS(200));
}

// 4. 反初始化Bluedroid
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_UNINITIALIZED)
{
esp_bluedroid_deinit();
ESP_LOGI(TAG, "Bluedroid deinitialized");
vTaskDelay(pdMS_TO_TICKS(200));
}

// 5. 禁用蓝牙控制器
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED)
{
esp_bt_controller_disable();
ESP_LOGI(TAG, "BT controller disabled");
vTaskDelay(pdMS_TO_TICKS(200));
}

// 6. 反初始化蓝牙控制器
if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE)
{
esp_bt_controller_deinit();
ESP_LOGI(TAG, "BT controller deinitialized");
vTaskDelay(pdMS_TO_TICKS(200));
}

// 7. 释放蓝牙内存
esp_bt_controller_mem_release(ESP_BT_MODE_BLE);

// 8. 清理准备写环境
if (prepare_write_env.prepare_buf != NULL)
{
free(prepare_write_env.prepare_buf);
prepare_write_env.prepare_buf = NULL;
prepare_write_env.prepare_len = 0;
ESP_LOGI(TAG, "Prepare write buffer freed");
}

// 9. 重置profile状态
heart_rate_profile_tab[PROFILE_APP_IDX].gatts_if = ESP_GATT_IF_NONE;
ESP_LOGI(TAG, "Profile state reset");

// 10. 执行垃圾收集释放可能的内存碎片
heap_caps_free(heap_caps_malloc(1, MALLOC_CAP_8BIT)); // 触发GC
vTaskDelay(pdMS_TO_TICKS(100));

ESP_LOGI(TAG, "BLE deinitialization completed. Free heap: %d bytes", esp_get_free_heap_size());
}

反初始化是在任务中进行的,反初始化后的打印如下:
I (19064) ble: Starting BLE deinitialization
I (19064) GATTS_TABLE_DEMO: Stop adv successfully
W (19364) BT_APPL: bta_dm_disable BTA_DISABLE_DELAY set to 200 ms
I (19564) ble: Bluedroid disabled
I (19764) ble: Bluedroid deinitialized
I (19964) ble: BT controller disabled
I (20164) ble: BT controller deinitialized
I (20364) ble: Profile state reset
I (20464) ble: BLE deinitialization completed. Free heap: 90352 bytes
上面是不连续的打印,因为中间有其他任务的打印,

反初始化完成后,再次使用示例中的蓝牙初始化代码,出现如下错误:

btdm: bss start 0x3fcdf0a8, len 40
btdm: data start 0x3fcdf09c, data start rom 0x400591fc, len 12
MAGIC fadebead VERSION 00010008
I (26034) BLE_INIT: BT controller compile version [c23ab4c]
MAGIC fadebead VERSION 00010001
E (26044) BLE_INIT: Malloc failed
assert ke_mem.c 417, param 0000ffff 3fca45f4
E (55024) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
E (55024) task_wdt: - Daemon (CPU 0)
E (55024) task_wdt: Tasks currently running:
E (55024) task_wdt: CPU 0: Daemon
E (55024) task_wdt: Aborting.


Core 0 register dump:
MEPC : 0x40002da4 RA : 0x40002d90 SP : 0x3fcb87f0 GP : 0x3fc98400
--- 0x40002da4: r_ble_util_buf_env_init in ROM
0x40002d90: btdm_controller_get_compile_version_impl in ROM

TP : 0x3fc7bdcc T0 : 0x0000201d T1 : 0x00000000 T2 : 0x4206948c
--- 0x4206948c: hci_le_rd_max_data_len_cmd_handler at llm_hci.o:?

S0/FP : 0x3fca45f0 S1 : 0x3fca45f4 A0 : 0x0000002d A1 : 0x0000000a
A2 : 0x00000000 A3 : 0x00000001 A4 : 0x00000001 A5 : 0x3fcdf9f8
A6 : 0x80000000 A7 : 0x00000010 S2 : 0x3fce0000 S3 : 0x3fca45f0
S4 : 0x3fcdf96c S5 : 0x3fce0000 S6 : 0x00044480 S7 : 0x3fca4698
S8 : 0x3fca4698 S9 : 0x00000000 S10 : 0x00000000 S11 : 0x00000000
T3 : 0x00000000 T4 : 0x42069a60 T5 : 0x0000201e T6 : 0x42069ae6
--- 0x42069a60: hci_le_tx_test_cmd_handler at llm_hci.o:?
0x42069ae6: hci_le_rx_test_cmd_handler at llm_hci.o:?

MSTATUS : 0x00000000 MTVEC : 0x3fce0000 MCAUSE : 0x3fcdf96c MTVAL : 0x4000a18a
--- 0x4000a18a: r_ke_free in ROM

MHARTID : 0x3fce0000

Stack memory:
3fcb87f0: 0x00000000 0x3fce0000 0x3fcdf96c 0x4000a18a 0x3fce0000 0x3fce0000 0x3fca4698 0x3fca4698
--- 0x4000a18a: r_ke_free in ROM

3fcb8810: 0x00044480 0x3fce0000 0x3fcdf96c 0x3fce0000 0x3fce0000 0x3fce0000 0x3fca45f0 0x4206ad9c
--- 0x4206ad9c: llm_util_flush_list at ??:?

3fcb8830: 0x00000808 0x3fce0000 0x3fca45e0 0x4206b17c 0x3c129278 0x000001ee 0x3fce0000 0x4206b33a
--- 0x4206b17c: r_llm_duplicate_list_clear at ??:?
0x4206b33a: r_llm_env_deinit at ??:?

3fcb8850: 0x3fcb88d0 0x3fce0000 0x3fce0000 0x42062cfe 0x00000000 0x000000f0 0x3fca4710 0x3fca1000
--- 0x42062cfe: btdm_controller_init at ??:?

3fcb8870: 0x3fc9c1c8 0x000065b2 0x3fcb88d0 0x40380dbe 0x3fcdf980 0x000--- 0x40380dbe: esp_clk_xtal_freq at D:/espidf/v5.2.2/esp-idf/components/esp_hw_support/esp_clk.c:102 (discriminator 1)

00004 0x3c127000 0x42010006
--- 0x42010006: queue_recv_wrapper at D:/espidf/v5.2.2/esp-idf/components/bt/controller/esp32c3/bt.c:644

3fcb8890: 0x00000808 0xfadebead 0x3fc9c0b0 0x3fc9c150 0x3c127000 0x00000001 0x3c127000 0x3fca1000
3fcb88b0: 0x3fc9c1c8 0x00000000 0x3fcb88d0 0x420109f6 0x3c121c48 0x00000000 0x3c122000 0x4200bbfe
--- 0x420109f6: esp_bt_controller_init at D:/espidf/v5.2.2/esp-idf/components/bt/controller/esp32c3/bt.c:1403
0x4200bbfe: ble_init at C:/Users/wsh/Desktop/lvgl/esp_http_client/components/application/ble/ble.c:686

3fcb88d0: 0x5a5aa5a5 0x02404010 0x00171000 0x00000601 0x001e0000 0x01000000 0x00000000 0x000b0000
3fcb88f0: 0x00000001 0x00640000 0x00000000 0x01010000 0x00140005 0x00000000 0x01000000 0x00000001
3fcb8910: 0x3fc9c1c8 0x3fc9c150 0x3fc9c198 0x4200c0fa 0x00000018 0x00000000 0x00000000 0x00000000
--- 0x4200c0fa: run_init_jobs at C:/Users/wsh/Desktop/lvgl/esp_http_client/components/application/daemon/daemon.c:138 (discriminator 1)
(inlined by) daemon at C:/Users/wsh/Desktop/lvgl/esp_http_client/components/application/daemon/daemon.c:241 (discriminator 1)

3fcb8930: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcb8950: 0x00000000 0x00000000 0x00000000 0x4038c41c 0x00000000 0x00000000 0x00000000 0x00000000
--- 0x4038c41c: vPortTaskWrapper at D:/espidf/v5.2.2/esp-idf/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c:258

3fcb8970: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fcb8990: 0xa5a5a5a5 0x00000150 0x3fcb8770 0x000009fa 0x3fc9b6bc 0x3fc9b6bc 0x3fcb8998 0x3fc9b6b4
3fcb89b0: 0x00000017 0x3fcabfa0 0x3fcabfa0 0x3fcb8998 0x00000000 0x00000002 0x3fcac130 0x6d656144
3fcb89d0: 0x00006e6f 0x00000000 0x00000000 0x3fcb8990 0x00000002 0x00000000 0x3fcbc794 0x420003a8
--- 0x420003a8: pthread_cleanup_thread_specific_data_callback at D:/espidf/v5.2.2/esp-idf/components/pthread/pthread_local_storage.c:126

3fcb89f0: 0x00000000 0x3fca4eb8 0x3fca4f20 0x3fca4f88 0x00000000 0x00000000 0x00000001 0x00000000
3fcb8a10: 0x00000000 0x00000000 0x42004fba 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
--- 0x42004fba: esp_cleanup_r at D:/espidf/v5.2.2/esp-idf/components/newlib/newlib_init.c:60

3fcb8a30: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcb8a50: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcb8a70: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcb8a90: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcb8ab0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcb8ad0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00002800 0xa5a5a5a5
3fcb8af0: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fcb8b10: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fcb8b30: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fcb8b50: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fcb8b70: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fcb8b90: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fcb8bb0: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fcb8bd0: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5



ELF file SHA256: a367b32b8

Rebooting...
ESP-ROM:esp32c3-api1-20210207
Build:Feb 7 2021
rst:0xc (RTC_SW_CPU_RST),boot:0xd (SPI_FAST_FLASH_BOOT)
Saved PC:0x403806bc
--- 0x403806bc: esp_restart_noos at D:/espidf/v5.2.2/esp-idf/components/esp_system/port/soc/esp32c3/system_internal.c:111

SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5820,len:0x1738
load:0x403cc710,len:0xb9c
load:0x403ce710,len:0x2e40
entry 0x403cc71a


还请了解该情况的相关人员给予指点,多谢!