Cannot deinitialize then reinitialize nimBLE HID without crashing

jatkins2431
Posts: 7
Joined: Mon May 20, 2024 2:18 am

Cannot deinitialize then reinitialize nimBLE HID without crashing

Postby jatkins2431 » Tue Aug 26, 2025 2:54 am

Hello! I am developing a product with ESP32-C5 on ESP-IDF v5.5. I'm having an issue where I am unable to deinitialize and then reinitialize Bluetooth nimBLE as a HID device without crashing with what is usually a Guru Meditation Error.

On first initialization everything works fine, then I can deinitialize fine to preserve power until the user wants to use Bluetooth again, but if I initialize again after that I get the crash so the system has to reboot before I can successfully initialize Bluetooth again. I got most my info from the esp_hid_device example, but there is nothing in there about deinitializing Bluetooth to use again later. Being able to deinitialize it completely is important to my application to save power since it could be a long time until the user wants to use Bluetooth again. Any help would be greatly appreciated!

Below is my full log. I am aware it says it is already initialized, but I have been trying many different things for days and even if I implement my own hid_gap_deinit function I will always get some sort of crash or problem when reinitializing. Most of the time this is a Guru Meditation Error if I remove ESP_ERROR_CHECKs.

Code: Select all

I (11412) BLE_INIT: Using main XTAL as clock source
I (11412) BLE_INIT: ble controller commit:[35fe65f]
I (11412) BLE_INIT: Bluetooth MAC: d0:cf:13:e0:a7:2e
I (11412) phy_init: phy_version 102,171bf417,Jun 12 2025,15:57:12
I (12132) phy: libbtbb version: 09fb4d6, Jun 12 2025, 15:57:24
I (12142) NimBLE: GAP procedure initiated: stop advertising.

I (12142) NimBLE: GAP procedure initiated: stop advertising.

I (12142) NimBLE: GAP procedure initiated: advertise; 
I (12152) NimBLE: disc_mode=2
I (12152) NimBLE:  adv_channel_map=0 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=48 adv_itvl_max=80
I (12162) NimBLE: 

I (12422) ESP_HID_GAP: connection established; status=0
I (12482) NimBLE: encryption change event; status=0 
I (12482) ESP_HID_GAP: subscribe event; conn_handle=0 attr_handle=11 reason=3 prevn=0 curn=0 previ=0 curi=1

I (12482) ESP_HID_GAP: subscribe event; conn_handle=0 attr_handle=21 reason=3 prevn=0 curn=1 previ=0 curi=0

I (12492) ESP_HID_GAP: subscribe event; conn_handle=0 attr_handle=40 reason=3 prevn=0 curn=1 previ=0 curi=0

I (12502) ESP_HID_GAP: subscribe event; conn_handle=0 attr_handle=47 reason=3 prevn=0 curn=1 previ=0 curi=0

I (12512) ESP_HID_GAP: mtu update event; conn_handle=0 cid=4 mtu=256
I (14932) NimBLE: GAP procedure initiated: stop advertising.

I (14932) NimBLE: GAP procedure initiated: stop advertising.

I (14932) NimBLE: GAP procedure initiated: terminate connection; conn_handle=0 hci_reason=19

I (14952) ESP_HID_GAP: subscribe event; conn_handle=0 attr_handle=11 reason=2 prevn=0 curn=0 previ=1 curi=0

I (14952) ESP_HID_GAP: subscribe event; conn_handle=0 attr_handle=21 reason=2 prevn=1 curn=0 previ=0 curi=0

I (14962) ESP_HID_GAP: subscribe event; conn_handle=0 attr_handle=40 reason=2 prevn=1 curn=0 previ=0 curi=0

I (14972) ESP_HID_GAP: subscribe event; conn_handle=0 attr_handle=47 reason=2 prevn=1 curn=0 previ=0 curi=0

I (14982) ESP_HID_GAP: disconnect; reason=534
I (14992) BLUETOOTH_FUNCS: Bluetooth fully disabled
E (18012) ESP_HID_GAP: Already initialised
ESP_ERROR_CHECK failed: esp_err_t 0xffffffff (ESP_FAIL) at 0x4206cc4c
--- 0x4206cc4c: bluetooth_init at C:/Projects/ESP/PolyCast5/components/bluetooth/src/bluetooth_funcs.c:903
file: "./components/bluetooth/src/bluetooth_funcs.c" line 903
func: bluetooth_init
expression: ret

abort() was called at PC 0x408152ad on core 0
--- 0x408152ad: _esp_error_check_failed at C:/Espressif/frameworks/esp-idf-v5.5/components/esp_system/esp_err.c:49
Core  0 register dump:
MEPC    : 0x408008b0  RA      : 0x408152b8  SP      : 0x40856cf0  GP      : 0x40823304
--- 0x408008b0: panic_abort at C:/Espressif/frameworks/esp-idf-v5.5/components/esp_system/panic.c:483
--- 0x408152b8: __ubsan_include at C:/Espressif/frameworks/esp-idf-v5.5/components/esp_system/ubsan.c:311
TP      : 0x40856ff0  T0      : 0x37363534  T1      : 0x7271706f  T2      : 0x33323130
S0/FP   : 0x00000004  S1      : 0x40856d54  A0      : 0x40856d1c  A1      : 0x40856d52
A2      : 0x00000000  A3      : 0x40856d49  A4      : 0x00000001  A5      : 0x40833000
A6      : 0x00000000  A7      : 0x76757473  S2      : 0x00000000  S3      : 0x00000000
S4      : 0x00000000  S5      : 0x00000000  S6      : 0x00000000  S7      : 0x00000000
S8      : 0x00000000  S9      : 0x00000000  S10     : 0x00000000  S11     : 0x00000000
T3      : 0x6e6d6c6b  T4      : 0x6a696867  T5      : 0x66656463  T6      : 0x62613938
MSTATUS : 0x00001881  MTVEC   : 0x40800003  MCAUSE  : 0x00000002  MTVAL   : 0x00000000
--- 0x40800003: _vector_table at ??:?
MHARTID : 0x00000000

Stack memory:
40856cf0: 0x4219e154 0x4206cc50 0x40856d50 0x4081cdce 0x00000000 0x00000000 0x00000000 0x4082e358
--- 0x4206cc50: bluetooth_init at C:/Projects/ESP/PolyCast5/components/bluetooth/src/bluetooth_funcs.c:906
--- 0x4081cdce: abort at C:/Espressif/frameworks/esp-idf-v5.5/components/newlib/src/abort.c:35
40856d10: 0x40856d54 0x4082e374 0x40856d50 0x726f6261 0x20292874 0x20736177 0x6c6c6163 0x61206465
40856d30: 0x43502074 0x34783020 0x35313830 0x20646132 0x63206e6f 0x2065726f 0x00000030 0x00000000
40856d50: 0x00000030 0x31383034 0x64613235 0x00000000 0x00000000 0x00000516 0x00000000 0x408152b0
--- 0x408152b0: esp_system_abort at C:/Espressif/frameworks/esp-idf-v5.5/components/esp_system/port/esp_system_chip.c:86
40856d70: 0x00000000 0x00000516 0x00000000 0x4206cc50 0x00000000 0x00000516 0x00000000 0x4206a82c
--- 0x4206cc50: bluetooth_init at C:/Projects/ESP/PolyCast5/components/bluetooth/src/bluetooth_funcs.c:906
--- 0x4206a82c: bluetooth_task at C:/Projects/ESP/PolyCast5/components/bluetooth/src/bluetooth_task.c:41
40856d90: 0xa5a5a5a5 0xa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0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
40856f10: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
40856f30: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
40856f50: 0x408165ce 0x00000000 0x00000000 0x40823304 0x40856ff0 0x00000000 0x00000000 0x00000000
--- 0x408165ce: vPortTaskWrapper at C:/Espressif/frameworks/esp-idf-v5.5/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c:251
40856f70: 0x00000000 0x00000000 0x4206a6d2 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
--- 0x4206a6d2: bluetooth_task at C:/Projects/ESP/PolyCast5/components/bluetooth/src/bluetooth_task.c:29
40856f90: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
40856fb0: 0x00000000 0x00000000 0x00000000 0x408165da 0x00000000 0x00000000 0x00000000 0x00000000
--- 0x408165da: vPortTaskWrapper at C:/Espressif/frameworks/esp-idf-v5.5/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c:258
40856fd0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
40856ff0: 0xa5a5a5a5 0xa5a5a5a5 0x00000150 0x40856bb0 0x00000699 0x4082fb04 0x4084e5b8 0x40856ffc
40857010: 0x4082fafc 0x00000018 0x4083b470 0x4083b470 0x40856ffc 0x00000000 0x00000001 0x40855ff8
40857030: 0x65756c62 0x746f6f74 0x61745f68 0x00006b73 0x40856ff0 0x00000001 0x00000000 0x00000000
40857050: 0x00000000 0x00000000 0x42227b40 0x42227ba8 0x42227c10 0x00000000 0x00000000 0x00000001
40857070: 0x00000000 0x00000000 0x00000000 0x420073f4 0x00000000 0x00000000 0x00000000 0x00000000
--- 0x420073f4: esp_cleanup_r at C:/Espressif/frameworks/esp-idf-v5.5/components/newlib/src/newlib_init.c:42
40857090: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
408570b0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
408570d0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000



ELF file SHA256: 1d289725e

Rebooting...
ESP-ROM:esp32c5-eco2-20250121
Build:Jan 21 2025
rst:0xc (SW_CPU),boot:0x5e (SPI_FAST_FLASH_BOOT)
Core0 Saved PC:0x4080089c
--- 0x4080089c: esp_restart_noos at C:/Espressif/frameworks/esp-idf-v5.5/components/esp_system/port/soc/esp32c5/system_internal.c:144
SPI mode:DIO, clock div:1
load:0x408556b0,len:0x170c
load:0x4084bba0,len:0xd70
load:0x4084e5a0,len:0x30a4
entry 0x4084bbaa
I am using these functions:

Code: Select all

static void ble_hid_device_host_task(void *param)
{
	// This function will return only when nimble_port_stop() is executed 
	nimble_port_run();
	nimble_port_freertos_deinit(); // esp_nimble_disable
}

// Declaration of extern esp function
void ble_store_config_init(void);`

void bluetooth_init(void)
{
	esp_err_t ret;

	ret = esp_hid_gap_init(HID_DEV_MODE);
	ESP_ERROR_CHECK(ret);

	ret = esp_hid_ble_gap_adv_init(ESP_HID_APPEARANCE_KEYBOARD, DEVICE_NAME);
	ESP_ERROR_CHECK(ret);

	// Register the standard Battery Service (0x180F)
	ble_svc_bas_init();

	ret = esp_hidd_dev_init(&ble_hid_config, ESP_HID_TRANSPORT_BLE, ble_hidd_event_callback, &ble_hid_param.hid_dev);
	ESP_ERROR_CHECK(ret);

	ble_store_config_init();
	ble_hs_cfg.store_status_cb = ble_store_util_status_rr;

	ret = esp_nimble_enable(ble_hid_device_host_task);
	if (ret) {
		ESP_LOGE(TAG, "esp_nimble_enable failed: %d", ret);
	}
}

void bluetooth_deinit(void)
{
	ble_gap_adv_stop(); // Stop advertising

	if (nimble_port_stop()) {
		ESP_LOGE(TAG, "nimble_port_stop failed");
		return;
	}

	if (nimble_port_deinit() != ESP_OK) {
		ESP_LOGE(TAG, "nimble_port_deinit failed");
		return;
	}

	if (ble_hid_param.hid_dev) {
		esp_hidd_dev_deinit(ble_hid_param.hid_dev);
		ble_hid_param.hid_dev = NULL;
	}

	#ifdef POLYCAST5_DEBUG
		ESP_LOGI(TAG, "Bluetooth fully disabled");
	#endif
}
Thank you for your time and assistance. This has been driving me crazy for awhile. If there is a easy fix for this is may be helpful for others to see a deinit function in the example: https://github.com/espressif/esp-idf/tr ... hid_device

jatkins2431
Posts: 7
Joined: Mon May 20, 2024 2:18 am

Re: Cannot deinitialize then reinitialize nimBLE HID without crashing

Postby jatkins2431 » Thu Aug 28, 2025 3:23 am

For anyone:

I've applied this patch. Now, I can init and deinit like before, but when I reinit after, I get 'Write on Protocol Mode Failed: 5' instead of crashing and am unable to send Bluetooth commands.

Here is my log from deinit to reinit:

Code: Select all

I (21052) ESP_HID_GAP: disconnect; reason=534
I (21062) BLUETOOTH_FUNCS: Bluetooth fully disabled
I (25472) BLE_INIT: Using main XTAL as clock source
I (25482) BLE_INIT: ble controller commit:[35fe65f]
I (25482) BLE_INIT: Bluetooth MAC: d0:cf:13:e0:a7:2e
I (25482) phy: libbtbb version: 09fb4d6, Jun 12 2025, 15:57:24
I (25512) NimBLE: GAP procedure initiated: stop advertising.

I (25512) NimBLE: GAP procedure initiated: stop advertising.

I (25512) NimBLE: GAP procedure initiated: advertise; 
I (25512) NimBLE: disc_mode=2
I (25522) NimBLE:  adv_channel_map=0 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=48 adv_itvl_max=80
I (25532) NimBLE: 

E (25842) NIMBLE_HIDD: Write on Protocol Mode Failed: 5
I (25852) ESP_HID_GAP: connection established; status=0
I (25892) NimBLE: encryption change event; status=0 
I (25902) ESP_HID_GAP: subscribe event; conn_handle=0 attr_handle=11 reason=3 prevn=0 curn=0 previ=0 curi=1

I (25902) ESP_HID_GAP: subscribe event; conn_handle=0 attr_handle=21 reason=3 prevn=0 curn=1 previ=0 curi=0

I (25912) ESP_HID_GAP: subscribe event; conn_handle=0 attr_handle=40 reason=3 prevn=0 curn=1 previ=0 curi=0

I (25922) ESP_HID_GAP: subscribe event; conn_handle=0 attr_handle=47 reason=3 prevn=0 curn=1 previ=0 curi=0

I (25932) ESP_HID_GAP: mtu update event; conn_handle=0 cid=4 mtu=256
I (28782) BLUETOOTH_TASK: Sending command
E (28782) NIMBLE_HIDD: Unable to fetch protocol_mode

E (28792) NIMBLE_HIDD: Unable to fetch protocol_mode
My new functions:

Code: Select all

static void ble_hid_device_host_task(void *param)
{
	// This function will return only when nimble_port_stop() is executed 
	nimble_port_run();
	nimble_port_freertos_deinit(); // esp_nimble_disable
}

// Declaration of extern esp function
void ble_store_config_init(void);

void bluetooth_init(void)
{
	esp_err_t ret;

	ret = esp_hid_gap_init(HID_DEV_MODE);
	ESP_ERROR_CHECK(ret);

	ret = esp_hid_ble_gap_adv_init(ESP_HID_APPEARANCE_KEYBOARD, DEVICE_NAME);
	ESP_ERROR_CHECK(ret);

	// Register the standard Battery Service (0x180F)
	ble_svc_bas_init();

	ret = esp_hidd_dev_init(&ble_hid_config, ESP_HID_TRANSPORT_BLE, ble_hidd_event_callback, &ble_hid_param.hid_dev);
	ESP_ERROR_CHECK(ret);

	ble_store_config_init();
	ble_hs_cfg.store_status_cb = ble_store_util_status_rr;

	ret = esp_nimble_enable(ble_hid_device_host_task);
	if (ret) {
		ESP_LOGE(TAG, "esp_nimble_enable failed: %d", ret);
	}
}

extern esp_err_t esp_hid_gap_deinit(void);

void bluetooth_deinit(void)
{
    esp_hid_gap_deinit();

	ble_gap_adv_stop(); // Stop advertising

	ble_gatts_reset();

   if (nimble_port_stop()) {
           ESP_LOGE(TAG, "nimble_port_stop failed");
           return;
   }

   if (nimble_port_deinit() != ESP_OK) {
           ESP_LOGE(TAG, "nimble_port_deinit failed");
           return;
   }

   if (ble_hid_param.hid_dev) {
           esp_hidd_dev_deinit(ble_hid_param.hid_dev);
           ble_hid_param.hid_dev = NULL;
   }

	#ifdef POLYCAST5_DEBUG
		ESP_LOGI(TAG, "Bluetooth fully disabled");
	#endif
}
esp_hid_gap.c:

Code: Select all

esp_err_t esp_hid_gap_deinit(void)
{
    if (bt_hidh_cb_semaphore != NULL) {
        vSemaphoreDelete(bt_hidh_cb_semaphore);
        bt_hidh_cb_semaphore = NULL;
    }

    if (ble_hidh_cb_semaphore != NULL) {
        vSemaphoreDelete(ble_hidh_cb_semaphore);
        ble_hidh_cb_semaphore = NULL;
    }

    return ESP_OK;
}
Thanks for your reply.

Justin Atkins

jatkins2431
Posts: 7
Joined: Mon May 20, 2024 2:18 am

Re: Cannot deinitialize then reinitialize nimBLE HID without crashing

Postby jatkins2431 » Fri Aug 29, 2025 7:59 pm

The problem has been solved! See the solution here: https://github.com/espressif/esp-idf/issues/17493

Who is online

Users browsing this forum: PetalBot and 2 guests