diff --git a/components/bt/controller/esp32c2/Kconfig.in b/components/bt/controller/esp32c2/Kconfig.in index 1076cf24ba..2faa9c2077 100644 --- a/components/bt/controller/esp32c2/Kconfig.in +++ b/components/bt/controller/esp32c2/Kconfig.in @@ -281,94 +281,103 @@ config BT_LE_CONTROLLER_TASK_STACK_SIZE help This configures stack size of NimBLE controller task -menuconfig BT_LE_CONTROLLER_LOG_ENABLED - bool "Controller log enable" - default n - help - Enable controller log +menu "Controller debug features" + menuconfig BT_LE_CONTROLLER_LOG_ENABLED + bool "Controller log enable" + default n + help + Enable controller log -config BT_LE_CONTROLLER_LOG_CTRL_ENABLED - bool "enable controller log module" - depends on BT_LE_CONTROLLER_LOG_ENABLED - default y - help - Enable controller log module + config BT_LE_CONTROLLER_LOG_CTRL_ENABLED + bool "enable controller log module" + depends on BT_LE_CONTROLLER_LOG_ENABLED + default y + help + Enable controller log module -config BT_LE_CONTROLLER_LOG_HCI_ENABLED - bool "enable HCI log module" - depends on BT_LE_CONTROLLER_LOG_ENABLED - default y - help - Enable hci log module + config BT_LE_CONTROLLER_LOG_HCI_ENABLED + bool "enable HCI log module" + depends on BT_LE_CONTROLLER_LOG_ENABLED + default y + help + Enable hci log module -config BT_LE_CONTROLLER_LOG_DUMP_ONLY - bool "Controller log dump mode only" - depends on BT_LE_CONTROLLER_LOG_ENABLED - default y - help - Only operate in dump mode + config BT_LE_CONTROLLER_LOG_DUMP_ONLY + bool "Controller log dump mode only" + depends on BT_LE_CONTROLLER_LOG_ENABLED + default y + help + Only operate in dump mode -config BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED - bool "Output ble controller logs to SPI bus (Experimental)" - depends on BT_LE_CONTROLLER_LOG_ENABLED - depends on !BT_LE_CONTROLLER_LOG_DUMP_ONLY - select BT_BLE_LOG_SPI_OUT_ENABLED - default n - help - Output ble controller logs to SPI bus + config BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED + bool "Output ble controller logs to SPI bus (Experimental)" + depends on BT_LE_CONTROLLER_LOG_ENABLED + depends on !BT_LE_CONTROLLER_LOG_DUMP_ONLY + select BT_BLE_LOG_SPI_OUT_ENABLED + default n + help + Output ble controller logs to SPI bus -config BT_LE_CONTROLLER_LOG_STORAGE_ENABLE - bool "Store ble controller logs to flash(Experimental)" - depends on !BT_LE_CONTROLLER_LOG_DUMP_ONLY - depends on BT_LE_CONTROLLER_LOG_ENABLED - default n - help - Store ble controller logs to flash memory. + config BT_LE_CONTROLLER_LOG_STORAGE_ENABLE + bool "Store ble controller logs to flash(Experimental)" + depends on !BT_LE_CONTROLLER_LOG_DUMP_ONLY + depends on BT_LE_CONTROLLER_LOG_ENABLED + default n + help + Store ble controller logs to flash memory. -config BT_LE_CONTROLLER_LOG_PARTITION_SIZE - int "size of ble controller log partition(Multiples of 4K)" - depends on BT_LE_CONTROLLER_LOG_STORAGE_ENABLE - default 65536 - help - The size of ble controller log partition shall be a multiples of 4K. - The name of log partition shall be "bt_ctrl_log". - The partition type shall be ESP_PARTITION_TYPE_DATA. - The partition sub_type shall be ESP_PARTITION_SUBTYPE_ANY. + config BT_LE_CONTROLLER_LOG_PARTITION_SIZE + int "size of ble controller log partition(Multiples of 4K)" + depends on BT_LE_CONTROLLER_LOG_STORAGE_ENABLE + default 65536 + help + The size of ble controller log partition shall be a multiples of 4K. + The name of log partition shall be "bt_ctrl_log". + The partition type shall be ESP_PARTITION_TYPE_DATA. + The partition sub_type shall be ESP_PARTITION_SUBTYPE_ANY. -config BT_LE_LOG_CTRL_BUF1_SIZE - int "size of the first BLE controller LOG buffer" - depends on BT_LE_CONTROLLER_LOG_ENABLED - default 4096 - help - Configure the size of the first BLE controller LOG buffer. + config BT_LE_LOG_CTRL_BUF1_SIZE + int "size of the first BLE controller LOG buffer" + depends on BT_LE_CONTROLLER_LOG_ENABLED + default 4096 + help + Configure the size of the first BLE controller LOG buffer. -config BT_LE_LOG_CTRL_BUF2_SIZE - int "size of the second BLE controller LOG buffer" - depends on BT_LE_CONTROLLER_LOG_ENABLED - default 1024 - help - Configure the size of the second BLE controller LOG buffer. + config BT_LE_LOG_CTRL_BUF2_SIZE + int "size of the second BLE controller LOG buffer" + depends on BT_LE_CONTROLLER_LOG_ENABLED + default 1024 + help + Configure the size of the second BLE controller LOG buffer. -config BT_LE_LOG_HCI_BUF_SIZE - int "size of the BLE HCI LOG buffer" - depends on BT_LE_CONTROLLER_LOG_ENABLED - default 4096 - help - Configure the size of the BLE HCI LOG buffer. + config BT_LE_LOG_HCI_BUF_SIZE + int "size of the BLE HCI LOG buffer" + depends on BT_LE_CONTROLLER_LOG_ENABLED + default 4096 + help + Configure the size of the BLE HCI LOG buffer. -config BT_LE_CONTROLLER_LOG_WRAP_PANIC_HANDLER_ENABLE - bool "Enable wrap panic handler" - depends on BT_LE_CONTROLLER_LOG_ENABLED - default n - help - Wrap esp_panic_handler to get controller logs when PC pointer exception crashes. + config BT_LE_CONTROLLER_LOG_WRAP_PANIC_HANDLER_ENABLE + bool "Enable wrap panic handler" + depends on BT_LE_CONTROLLER_LOG_ENABLED + default n + help + Wrap esp_panic_handler to get controller logs when PC pointer exception crashes. + + config BT_LE_CONTROLLER_LOG_TASK_WDT_USER_HANDLER_ENABLE + bool "Enable esp_task_wdt_isr_user_handler implementation" + depends on BT_LE_CONTROLLER_LOG_ENABLED + default n + help + Implement esp_task_wdt_isr_user_handler to get controller logs when task wdt issue is triggered. + + config BT_LE_MEM_CHECK_ENABLED + bool "Enable memory allocation check" + default n + help + Used in internal tests only. Enable the memory allocation check. +endmenu -config BT_LE_CONTROLLER_LOG_TASK_WDT_USER_HANDLER_ENABLE - bool "Enable esp_task_wdt_isr_user_handler implementation" - depends on BT_LE_CONTROLLER_LOG_ENABLED - default n - help - Implement esp_task_wdt_isr_user_handler to get controller logs when task wdt issue is triggered. config BT_LE_LL_RESOLV_LIST_SIZE int "BLE LL Resolving list size" range 1 5 diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index e2a39427c6..fa310ac0be 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -1571,3 +1571,10 @@ int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv) #endif // CONFIG_BT_LE_SM_LEGACY || CONFIG_BT_LE_SM_SC #endif // (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED) + +#if CONFIG_BT_LE_MEM_CHECK_ENABLED +void ble_memory_count_limit_set(uint16_t count_limit) +{ + bt_osi_mem_count_limit_set(count_limit); +} +#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED diff --git a/components/bt/include/esp32c2/include/esp_bt.h b/components/bt/include/esp32c2/include/esp_bt.h index dcb7ccd757..320ae238d0 100644 --- a/components/bt/include/esp32c2/include/esp_bt.h +++ b/components/bt/include/esp32c2/include/esp_bt.h @@ -446,6 +446,10 @@ uint32_t esp_bt_get_lpclk_freq(void); void esp_bt_set_lpclk_freq(uint32_t clk_freq); +#if CONFIG_BT_LE_MEM_CHECK_ENABLED +void ble_memory_count_limit_set(uint16_t count_limit); +#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED + #ifdef __cplusplus } #endif diff --git a/components/bt/porting/include/bt_osi_mem.h b/components/bt/porting/include/bt_osi_mem.h index 9ab06d0ab4..cf5f60956e 100644 --- a/components/bt/porting/include/bt_osi_mem.h +++ b/components/bt/porting/include/bt_osi_mem.h @@ -19,3 +19,7 @@ void *bt_osi_mem_malloc_internal(size_t size); void *bt_osi_mem_calloc_internal(size_t n, size_t size); void bt_osi_mem_free(void *ptr); + +#if CONFIG_BT_LE_MEM_CHECK_ENABLED +void bt_osi_mem_count_limit_set(uint16_t count_limit); +#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED diff --git a/components/bt/porting/mem/bt_osi_mem.c b/components/bt/porting/mem/bt_osi_mem.c index 612099d277..cbedf8c96d 100644 --- a/components/bt/porting/mem/bt_osi_mem.c +++ b/components/bt/porting/mem/bt_osi_mem.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -11,6 +11,10 @@ #include static uint8_t log_count; +#if CONFIG_BT_LE_MEM_CHECK_ENABLED +static uint16_t mem_count_limit = 0; +static uint16_t curr_mem_count; +#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED IRAM_ATTR void *bt_osi_mem_malloc(size_t size) { void *mem = NULL; @@ -49,11 +53,27 @@ IRAM_ATTR void *bt_osi_mem_calloc(size_t n, size_t size) IRAM_ATTR void *bt_osi_mem_malloc_internal(size_t size) { +#if CONFIG_BT_LE_MEM_CHECK_ENABLED + if (mem_count_limit) { + if (curr_mem_count > mem_count_limit) { + return NULL; + } + curr_mem_count ++; + } +#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED return heap_caps_malloc(size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT|MALLOC_CAP_DMA); } IRAM_ATTR void *bt_osi_mem_calloc_internal(size_t n, size_t size) { +#if CONFIG_BT_LE_MEM_CHECK_ENABLED + if (mem_count_limit) { + if (curr_mem_count > mem_count_limit) { + return NULL; + } + curr_mem_count ++; + } +#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED return heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT|MALLOC_CAP_DMA); } @@ -61,3 +81,11 @@ IRAM_ATTR void bt_osi_mem_free(void *ptr) { heap_caps_free(ptr); } + +#if CONFIG_BT_LE_MEM_CHECK_ENABLED +void bt_osi_mem_count_limit_set(uint16_t count_limit) +{ + mem_count_limit = count_limit; + curr_mem_count = 0; +} +#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED