Skip to content

Commit 7ef90d4

Browse files
feat(ble): support memory allocation check debug feature on ESP32-C2
1 parent 3d45e85 commit 7ef90d4

File tree

5 files changed

+130
-78
lines changed

5 files changed

+130
-78
lines changed

components/bt/controller/esp32c2/Kconfig.in

Lines changed: 86 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -281,94 +281,103 @@ config BT_LE_CONTROLLER_TASK_STACK_SIZE
281281
help
282282
This configures stack size of NimBLE controller task
283283

284-
menuconfig BT_LE_CONTROLLER_LOG_ENABLED
285-
bool "Controller log enable"
286-
default n
287-
help
288-
Enable controller log
284+
menu "Controller debug features"
285+
menuconfig BT_LE_CONTROLLER_LOG_ENABLED
286+
bool "Controller log enable"
287+
default n
288+
help
289+
Enable controller log
289290

290-
config BT_LE_CONTROLLER_LOG_CTRL_ENABLED
291-
bool "enable controller log module"
292-
depends on BT_LE_CONTROLLER_LOG_ENABLED
293-
default y
294-
help
295-
Enable controller log module
291+
config BT_LE_CONTROLLER_LOG_CTRL_ENABLED
292+
bool "enable controller log module"
293+
depends on BT_LE_CONTROLLER_LOG_ENABLED
294+
default y
295+
help
296+
Enable controller log module
296297

297-
config BT_LE_CONTROLLER_LOG_HCI_ENABLED
298-
bool "enable HCI log module"
299-
depends on BT_LE_CONTROLLER_LOG_ENABLED
300-
default y
301-
help
302-
Enable hci log module
298+
config BT_LE_CONTROLLER_LOG_HCI_ENABLED
299+
bool "enable HCI log module"
300+
depends on BT_LE_CONTROLLER_LOG_ENABLED
301+
default y
302+
help
303+
Enable hci log module
303304

304-
config BT_LE_CONTROLLER_LOG_DUMP_ONLY
305-
bool "Controller log dump mode only"
306-
depends on BT_LE_CONTROLLER_LOG_ENABLED
307-
default y
308-
help
309-
Only operate in dump mode
305+
config BT_LE_CONTROLLER_LOG_DUMP_ONLY
306+
bool "Controller log dump mode only"
307+
depends on BT_LE_CONTROLLER_LOG_ENABLED
308+
default y
309+
help
310+
Only operate in dump mode
310311

311-
config BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
312-
bool "Output ble controller logs to SPI bus (Experimental)"
313-
depends on BT_LE_CONTROLLER_LOG_ENABLED
314-
depends on !BT_LE_CONTROLLER_LOG_DUMP_ONLY
315-
select BT_BLE_LOG_SPI_OUT_ENABLED
316-
default n
317-
help
318-
Output ble controller logs to SPI bus
312+
config BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
313+
bool "Output ble controller logs to SPI bus (Experimental)"
314+
depends on BT_LE_CONTROLLER_LOG_ENABLED
315+
depends on !BT_LE_CONTROLLER_LOG_DUMP_ONLY
316+
select BT_BLE_LOG_SPI_OUT_ENABLED
317+
default n
318+
help
319+
Output ble controller logs to SPI bus
319320

320-
config BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
321-
bool "Store ble controller logs to flash(Experimental)"
322-
depends on !BT_LE_CONTROLLER_LOG_DUMP_ONLY
323-
depends on BT_LE_CONTROLLER_LOG_ENABLED
324-
default n
325-
help
326-
Store ble controller logs to flash memory.
321+
config BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
322+
bool "Store ble controller logs to flash(Experimental)"
323+
depends on !BT_LE_CONTROLLER_LOG_DUMP_ONLY
324+
depends on BT_LE_CONTROLLER_LOG_ENABLED
325+
default n
326+
help
327+
Store ble controller logs to flash memory.
327328

328-
config BT_LE_CONTROLLER_LOG_PARTITION_SIZE
329-
int "size of ble controller log partition(Multiples of 4K)"
330-
depends on BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
331-
default 65536
332-
help
333-
The size of ble controller log partition shall be a multiples of 4K.
334-
The name of log partition shall be "bt_ctrl_log".
335-
The partition type shall be ESP_PARTITION_TYPE_DATA.
336-
The partition sub_type shall be ESP_PARTITION_SUBTYPE_ANY.
329+
config BT_LE_CONTROLLER_LOG_PARTITION_SIZE
330+
int "size of ble controller log partition(Multiples of 4K)"
331+
depends on BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
332+
default 65536
333+
help
334+
The size of ble controller log partition shall be a multiples of 4K.
335+
The name of log partition shall be "bt_ctrl_log".
336+
The partition type shall be ESP_PARTITION_TYPE_DATA.
337+
The partition sub_type shall be ESP_PARTITION_SUBTYPE_ANY.
338+
339+
config BT_LE_LOG_CTRL_BUF1_SIZE
340+
int "size of the first BLE controller LOG buffer"
341+
depends on BT_LE_CONTROLLER_LOG_ENABLED
342+
default 4096
343+
help
344+
Configure the size of the first BLE controller LOG buffer.
337345

338-
config BT_LE_LOG_CTRL_BUF1_SIZE
339-
int "size of the first BLE controller LOG buffer"
340-
depends on BT_LE_CONTROLLER_LOG_ENABLED
341-
default 4096
342-
help
343-
Configure the size of the first BLE controller LOG buffer.
346+
config BT_LE_LOG_CTRL_BUF2_SIZE
347+
int "size of the second BLE controller LOG buffer"
348+
depends on BT_LE_CONTROLLER_LOG_ENABLED
349+
default 1024
350+
help
351+
Configure the size of the second BLE controller LOG buffer.
344352

345-
config BT_LE_LOG_CTRL_BUF2_SIZE
346-
int "size of the second BLE controller LOG buffer"
347-
depends on BT_LE_CONTROLLER_LOG_ENABLED
348-
default 1024
349-
help
350-
Configure the size of the second BLE controller LOG buffer.
353+
config BT_LE_LOG_HCI_BUF_SIZE
354+
int "size of the BLE HCI LOG buffer"
355+
depends on BT_LE_CONTROLLER_LOG_ENABLED
356+
default 4096
357+
help
358+
Configure the size of the BLE HCI LOG buffer.
351359

352-
config BT_LE_LOG_HCI_BUF_SIZE
353-
int "size of the BLE HCI LOG buffer"
354-
depends on BT_LE_CONTROLLER_LOG_ENABLED
355-
default 4096
356-
help
357-
Configure the size of the BLE HCI LOG buffer.
360+
config BT_LE_CONTROLLER_LOG_WRAP_PANIC_HANDLER_ENABLE
361+
bool "Enable wrap panic handler"
362+
depends on BT_LE_CONTROLLER_LOG_ENABLED
363+
default n
364+
help
365+
Wrap esp_panic_handler to get controller logs when PC pointer exception crashes.
358366

359-
config BT_LE_CONTROLLER_LOG_WRAP_PANIC_HANDLER_ENABLE
360-
bool "Enable wrap panic handler"
361-
depends on BT_LE_CONTROLLER_LOG_ENABLED
362-
default n
363-
help
364-
Wrap esp_panic_handler to get controller logs when PC pointer exception crashes.
367+
config BT_LE_CONTROLLER_LOG_TASK_WDT_USER_HANDLER_ENABLE
368+
bool "Enable esp_task_wdt_isr_user_handler implementation"
369+
depends on BT_LE_CONTROLLER_LOG_ENABLED
370+
default n
371+
help
372+
Implement esp_task_wdt_isr_user_handler to get controller logs when task wdt issue is triggered.
373+
374+
config BT_LE_MEM_CHECK_ENABLED
375+
bool "Enable memory allocation check"
376+
default n
377+
help
378+
Used in internal tests only. Enable the memory allocation check.
379+
endmenu
365380

366-
config BT_LE_CONTROLLER_LOG_TASK_WDT_USER_HANDLER_ENABLE
367-
bool "Enable esp_task_wdt_isr_user_handler implementation"
368-
depends on BT_LE_CONTROLLER_LOG_ENABLED
369-
default n
370-
help
371-
Implement esp_task_wdt_isr_user_handler to get controller logs when task wdt issue is triggered.
372381
config BT_LE_LL_RESOLV_LIST_SIZE
373382
int "BLE LL Resolving list size"
374383
range 1 5

components/bt/controller/esp32c2/bt.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,3 +1573,10 @@ int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv)
15731573

15741574
#endif // CONFIG_BT_LE_SM_LEGACY || CONFIG_BT_LE_SM_SC
15751575
#endif // (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED)
1576+
1577+
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
1578+
void ble_memory_count_limit_set(uint16_t count_limit)
1579+
{
1580+
bt_osi_mem_count_limit_set(count_limit);
1581+
}
1582+
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED

components/bt/include/esp32c2/include/esp_bt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,10 @@ uint32_t esp_bt_get_lpclk_freq(void);
446446

447447
void esp_bt_set_lpclk_freq(uint32_t clk_freq);
448448

449+
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
450+
void ble_memory_count_limit_set(uint16_t count_limit);
451+
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED
452+
449453
#ifdef __cplusplus
450454
}
451455
#endif

components/bt/porting/include/bt_osi_mem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ void *bt_osi_mem_malloc_internal(size_t size);
1919
void *bt_osi_mem_calloc_internal(size_t n, size_t size);
2020

2121
void bt_osi_mem_free(void *ptr);
22+
23+
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
24+
void bt_osi_mem_count_limit_set(uint16_t count_limit);
25+
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED

components/bt/porting/mem/bt_osi_mem.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -11,6 +11,10 @@
1111
#include <assert.h>
1212

1313
static uint8_t log_count;
14+
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
15+
static uint16_t mem_count_limit = 0;
16+
static uint16_t curr_mem_count;
17+
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED
1418
IRAM_ATTR void *bt_osi_mem_malloc(size_t size)
1519
{
1620
void *mem = NULL;
@@ -49,15 +53,39 @@ IRAM_ATTR void *bt_osi_mem_calloc(size_t n, size_t size)
4953

5054
IRAM_ATTR void *bt_osi_mem_malloc_internal(size_t size)
5155
{
56+
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
57+
if (mem_count_limit) {
58+
if (curr_mem_count > mem_count_limit) {
59+
return NULL;
60+
}
61+
curr_mem_count ++;
62+
}
63+
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED
5264
return heap_caps_malloc(size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT|MALLOC_CAP_DMA);
5365
}
5466

5567
IRAM_ATTR void *bt_osi_mem_calloc_internal(size_t n, size_t size)
5668
{
69+
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
70+
if (mem_count_limit) {
71+
if (curr_mem_count > mem_count_limit) {
72+
return NULL;
73+
}
74+
curr_mem_count ++;
75+
}
76+
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED
5777
return heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT|MALLOC_CAP_DMA);
5878
}
5979

6080
IRAM_ATTR void bt_osi_mem_free(void *ptr)
6181
{
6282
heap_caps_free(ptr);
6383
}
84+
85+
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
86+
void bt_osi_mem_count_limit_set(uint16_t count_limit)
87+
{
88+
mem_count_limit = count_limit;
89+
curr_mem_count = 0;
90+
}
91+
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED

0 commit comments

Comments
 (0)