From db90c8145b923a693f50e76b369b17561d2acf97 Mon Sep 17 00:00:00 2001 From: h2zero Date: Sat, 7 Mar 2026 16:52:01 -0700 Subject: [PATCH] [Bugfix] Crash when scanning with esp32c6/c5/c2/h2 In a busy environment the number of buffers could be insufficent causing a crash. This increases the buffer count to avoid this issue. Also this will use the controller allocated buffers when nimble is enabled in the Arduino core to prevent extra memory being allocated. --- src/nimble/esp_port/esp-hci/src/na_hci_transport.c | 2 +- src/nimble/nimble/transport/esp_ipc/src/hci_esp_ipc.c | 3 ++- src/nimble/porting/nimble/src/nimble_port.c | 8 ++------ src/nimble/porting/npl/freertos/src/npl_os_freertos.c | 4 +++- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/nimble/esp_port/esp-hci/src/na_hci_transport.c b/src/nimble/esp_port/esp-hci/src/na_hci_transport.c index 4ac05ca46..2307c9753 100644 --- a/src/nimble/esp_port/esp-hci/src/na_hci_transport.c +++ b/src/nimble/esp_port/esp-hci/src/na_hci_transport.c @@ -6,7 +6,7 @@ #ifdef ESP_PLATFORM #include "syscfg/syscfg.h" -#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT +#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT && CONFIG_BT_BLUEDROID_ENABLED #include #include diff --git a/src/nimble/nimble/transport/esp_ipc/src/hci_esp_ipc.c b/src/nimble/nimble/transport/esp_ipc/src/hci_esp_ipc.c index 6622b6155..0ff29d26d 100644 --- a/src/nimble/nimble/transport/esp_ipc/src/hci_esp_ipc.c +++ b/src/nimble/nimble/transport/esp_ipc/src/hci_esp_ipc.c @@ -6,7 +6,8 @@ #include #ifdef ESP_PLATFORM -#if !defined(CONFIG_IDF_TARGET_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S3) +# if !defined(CONFIG_IDF_TARGET_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32C3) && \ + !defined(CONFIG_IDF_TARGET_ESP32S3) && CONFIG_BT_BLUEDROID_ENABLED #include #include diff --git a/src/nimble/porting/nimble/src/nimble_port.c b/src/nimble/porting/nimble/src/nimble_port.c index d72c86b6f..936112484 100644 --- a/src/nimble/porting/nimble/src/nimble_port.c +++ b/src/nimble/porting/nimble/src/nimble_port.c @@ -99,14 +99,12 @@ esp_err_t esp_nimble_init(void) os_mempool_module_init(); os_msys_init(); -#elif CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT +#elif CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT && CONFIG_BT_BLUEDROID_ENABLED hci_transport_deinit(); na_hci_transport_init(HCI_TRANSPORT_VHCI); int na_npl_freertos_mempool_init(void); na_npl_freertos_mempool_init(); -#ifdef CONFIG_BT_BLUEDROID_ENABLED ble_npl_eventq_init(&g_eventq_dflt); -#endif #endif ble_transport_ll_init(); @@ -143,13 +141,11 @@ esp_err_t esp_nimble_deinit(void) ble_transport_ll_deinit(); -#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT +#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT && CONFIG_BT_BLUEDROID_ENABLED na_hci_transport_deinit(); void na_npl_freertos_mempool_deinit(void); na_npl_freertos_mempool_deinit(); -#ifdef CONFIG_BT_BLUEDROID_ENABLED ble_npl_eventq_deinit(&g_eventq_dflt); -#endif #endif return ESP_OK; } diff --git a/src/nimble/porting/npl/freertos/src/npl_os_freertos.c b/src/nimble/porting/npl/freertos/src/npl_os_freertos.c index 4366db7ce..57d6517dc 100644 --- a/src/nimble/porting/npl/freertos/src/npl_os_freertos.c +++ b/src/nimble/porting/npl/freertos/src/npl_os_freertos.c @@ -661,11 +661,12 @@ static os_membuf_t *ble_freertos_mutex_buf = NULL; static uint16_t ble_freertos_total_event_cnt = 0; +#if CONFIG_BT_BLUEDROID_ENABLED int na_npl_freertos_mempool_init(void) { int rc = -1; // These values taken from esp-idf: bt/nimble/porting/npl/freertos/src/npl_os_freertos.c - uint16_t ble_total_evt_count = 19; + uint16_t ble_total_evt_count = 80; uint16_t ble_total_co_count = 8; uint16_t ble_total_evtq_count = 3; uint16_t ble_total_sem_count = 10; @@ -816,5 +817,6 @@ na_npl_freertos_eventq_init(struct ble_npl_eventq *evq) xQueueReset(eventq->q); } } +#endif /* CONFIG_BT_BLUEDROID_ENABLED */ #endif