diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index 2588d0f5b35..6cec63b4682 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -869,7 +869,6 @@ if(CONFIG_BT_ENABLED) "host/nimble/nimble/porting/nimble/src/nimble_port.c" "host/nimble/nimble/porting/npl/freertos/src/nimble_port_freertos.c" "host/nimble/port/src/nvs_port.c" - "host/nimble/port/src/esp_nimble_mem.c" ) list(APPEND include_dirs diff --git a/components/bt/host/bluedroid/hci/include/hci/hci_hal.h b/components/bt/host/bluedroid/hci/include/hci/hci_hal.h index f50bef28cd4..aec926b6afb 100644 --- a/components/bt/host/bluedroid/hci/include/hci/hci_hal.h +++ b/components/bt/host/bluedroid/hci/include/hci/hci_hal.h @@ -24,7 +24,9 @@ #include "common/bt_target.h" #include "osi/pkt_queue.h" #include "stack/bt_types.h" - +#if ((BT_CONTROLLER_INCLUDED == TRUE) && SOC_ESP_NIMBLE_CONTROLLER) +#include "os/os_mbuf.h" +#endif typedef enum { DATA_TYPE_COMMAND = 1, DATA_TYPE_ACL = 2, @@ -85,6 +87,11 @@ typedef struct hci_hal_t { // Gets the correct hal implementation, as compiled for. const hci_hal_t *hci_hal_h4_get_interface(void); +#if ((BT_CONTROLLER_INCLUDED == TRUE) && SOC_ESP_NIMBLE_CONTROLLER) +int ble_hs_hci_rx_evt(uint8_t *hci_ev, void *arg); + +int ble_hs_rx_data(struct os_mbuf *om, void *arg); +#endif #endif /* _HCI_HAL_H */ diff --git a/components/bt/host/nimble/port/include/esp_nimble_mem.h b/components/bt/host/nimble/port/include/esp_nimble_mem.h index dd60c741cfe..31d9557a094 100644 --- a/components/bt/host/nimble/port/include/esp_nimble_mem.h +++ b/components/bt/host/nimble/port/include/esp_nimble_mem.h @@ -13,95 +13,9 @@ extern "C" { #endif -void *nimble_mem_malloc(size_t size); - -void *nimble_mem_calloc(size_t n, size_t size); - -void nimble_mem_free(void *ptr); - -#if CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED -size_t nimble_mem_used_size_get(void); -#endif // CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED - -#if CONFIG_BT_NIMBLE_MEM_DEBUG -/** - * @brief Initialize NimBLE memory debug module - */ -void nimble_mem_dbg_init(void); - -/** - * @brief Record memory allocation information - * - * @param p Pointer to allocated memory - * @param size Size of allocation - * @param func Function name where allocation occurred - * @param line Line number where allocation occurred - */ -void nimble_mem_dbg_record(void *p, int size, const char *func, int line); - -/** - * @brief Clean up memory allocation record - * - * @param p Pointer to memory being freed - * @param func Function name where free occurred - * @param line Line number where free occurred - */ -void nimble_mem_dbg_clean(void *p, const char *func, int line); - -/** - * @brief Display all memory debug information - */ -void nimble_mem_dbg_show(void); - -/** - * @brief Get maximum memory size used - * - * @return Maximum memory size in bytes - */ -uint32_t nimble_mem_dbg_get_max_size(void); - -/** - * @brief Get current memory size in use - * - * @return Current memory size in bytes - */ -uint32_t nimble_mem_dbg_get_current_size(void); - -/** - * @brief Start tracking memory usage for a specific section - * - * @param index Section index (0 to NIMBLE_MEM_DBG_MAX_SECTION_NUM-1) - */ -void nimble_mem_dbg_set_section_start(uint8_t index); - -/** - * @brief Stop tracking memory usage for a specific section - * - * @param index Section index (0 to NIMBLE_MEM_DBG_MAX_SECTION_NUM-1) - */ -void nimble_mem_dbg_set_section_end(uint8_t index); - -/** - * @brief Get maximum memory size used in a specific section - * - * @param index Section index (0 to NIMBLE_MEM_DBG_MAX_SECTION_NUM-1) - * @return Maximum memory size in bytes for this section - */ -uint32_t nimble_mem_dbg_get_max_size_section(uint8_t index); - -/** - * @brief Reallocate memory with debug tracking - * - * @param ptr Pointer to memory to reallocate - * @param new_size New size of allocation - * @param func Function name where realloc occurred - * @param line Line number where realloc occurred - * @return Pointer to reallocated memory - */ -void *nimble_mem_dbg_realloc(void *ptr, size_t new_size, const char *func, int line); - -#endif // CONFIG_BT_NIMBLE_MEM_DEBUG +// #pragma message "This file should be replaced with bt_osi_mem.h, used here for compatibility" +#include "bt_osi_mem.h" #if CONFIG_BT_NIMBLE_MEM_DEBUG @@ -109,7 +23,7 @@ void *nimble_mem_dbg_realloc(void *ptr, size_t new_size, const char *func, int l ({ \ void *p; \ do { \ - p = nimble_mem_malloc(size); \ + p = bt_osi_mem_malloc(size); \ nimble_mem_dbg_record(p, size, __func__, __LINE__); \ } while (0); \ p; \ @@ -119,7 +33,7 @@ void *nimble_mem_dbg_realloc(void *ptr, size_t new_size, const char *func, int l ({ \ void *p; \ do { \ - p = nimble_mem_calloc(count, size); \ + p = bt_osi_mem_calloc(count, size); \ nimble_mem_dbg_record(p, (count) * (size), __func__, __LINE__); \ } while (0); \ p; \ @@ -135,18 +49,18 @@ void *nimble_mem_dbg_realloc(void *ptr, size_t new_size, const char *func, int l }) #define nimble_platform_mem_free(ptr) \ -do { \ - void *tmp_ptr = (void *)(ptr); \ +do { \ + void *tmp_ptr = (void *)(ptr); \ nimble_mem_dbg_clean(tmp_ptr, __func__, __LINE__); \ - nimble_mem_free(tmp_ptr); \ + bt_osi_mem_free(tmp_ptr); \ } while (0) #else -#define nimble_platform_mem_malloc nimble_mem_malloc -#define nimble_platform_mem_calloc nimble_mem_calloc +#define nimble_platform_mem_malloc bt_osi_mem_malloc +#define nimble_platform_mem_calloc bt_osi_mem_calloc #define nimble_platform_mem_realloc realloc -#define nimble_platform_mem_free nimble_mem_free +#define nimble_platform_mem_free bt_osi_mem_free #endif // CONFIG_BT_NIMBLE_MEM_DEBUG diff --git a/components/bt/host/nimble/port/src/esp_nimble_mem.c b/components/bt/host/nimble/port/src/esp_nimble_mem.c deleted file mode 100644 index a2177686684..00000000000 --- a/components/bt/host/nimble/port/src/esp_nimble_mem.c +++ /dev/null @@ -1,340 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "esp_attr.h" -#include "esp_heap_caps.h" -#include "sdkconfig.h" -#include "esp_log.h" -#include - -static uint8_t log_count; -#if CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED -static size_t host_mem_used_size = 0; -#endif // CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED - -#if CONFIG_BT_NIMBLE_MEM_DEBUG - -#define NIMBLE_MEM_DBG_INFO_MAX (1024*3) -typedef struct { - void *p; - int size; - const char *func; - int line; -} nimble_mem_dbg_info_t; - -static uint32_t nimble_mem_dbg_count = 0; -static nimble_mem_dbg_info_t nimble_mem_dbg_info[NIMBLE_MEM_DBG_INFO_MAX]; -static uint32_t nimble_mem_dbg_current_size = 0; -static uint32_t nimble_mem_dbg_max_size = 0; - -#define NIMBLE_MEM_DBG_MAX_SECTION_NUM 5 -typedef struct { - bool used; - uint32_t max_size; -} nimble_mem_dbg_max_size_section_t; -static nimble_mem_dbg_max_size_section_t nimble_mem_dbg_max_size_section[NIMBLE_MEM_DBG_MAX_SECTION_NUM]; - -void nimble_mem_dbg_init(void) -{ - int i; - - for (i = 0; i < NIMBLE_MEM_DBG_INFO_MAX; i++) { - nimble_mem_dbg_info[i].p = NULL; - nimble_mem_dbg_info[i].size = 0; - nimble_mem_dbg_info[i].func = NULL; - nimble_mem_dbg_info[i].line = 0; - } - nimble_mem_dbg_count = 0; - nimble_mem_dbg_current_size = 0; - nimble_mem_dbg_max_size = 0; - - for (i = 0; i < NIMBLE_MEM_DBG_MAX_SECTION_NUM; i++){ - nimble_mem_dbg_max_size_section[i].used = false; - nimble_mem_dbg_max_size_section[i].max_size = 0; - } -} - -void nimble_mem_dbg_record(void *p, int size, const char *func, int line) -{ - int i; - - if (!p || size == 0) { - ESP_LOGE("BT_NIMBLE_MEM", "%s invalid !!\n", __func__); - return; - } - - for (i = 0; i < NIMBLE_MEM_DBG_INFO_MAX; i++) { - if (nimble_mem_dbg_info[i].p == NULL) { - nimble_mem_dbg_info[i].p = p; - nimble_mem_dbg_info[i].size = size; - nimble_mem_dbg_info[i].func = func; - nimble_mem_dbg_info[i].line = line; - nimble_mem_dbg_count++; - break; - } - } - - if (i >= NIMBLE_MEM_DBG_INFO_MAX) { - ESP_LOGE("BT_NIMBLE_MEM", "%s full %s %d !!\n", __func__, func, line); - return; - } - - nimble_mem_dbg_current_size += size; - if(nimble_mem_dbg_max_size < nimble_mem_dbg_current_size) { - nimble_mem_dbg_max_size = nimble_mem_dbg_current_size; - } - - for (i = 0; i < NIMBLE_MEM_DBG_MAX_SECTION_NUM; i++){ - if (nimble_mem_dbg_max_size_section[i].used) { - if(nimble_mem_dbg_max_size_section[i].max_size < nimble_mem_dbg_current_size) { - nimble_mem_dbg_max_size_section[i].max_size = nimble_mem_dbg_current_size; - } - } - } -} - -void nimble_mem_dbg_clean(void *p, const char *func, int line) -{ - int i; - - if (!p) { - ESP_LOGE("BT_NIMBLE_MEM", "%s %d free->%p invalid\n", func, line, p); - return; - } - - for (i = 0; i < NIMBLE_MEM_DBG_INFO_MAX; i++) { - if (nimble_mem_dbg_info[i].p == p) { - nimble_mem_dbg_current_size -= nimble_mem_dbg_info[i].size; - nimble_mem_dbg_info[i].p = NULL; - nimble_mem_dbg_info[i].size = 0; - nimble_mem_dbg_info[i].func = NULL; - nimble_mem_dbg_info[i].line = 0; - nimble_mem_dbg_count--; - break; - } - } - - if (i >= NIMBLE_MEM_DBG_INFO_MAX) { - ESP_LOGE("BT_NIMBLE_MEM", "%s not found %s %d !!\n", __func__, func, line); - } -} - -void nimble_mem_dbg_show(void) -{ - int i; - - for (i = 0; i < NIMBLE_MEM_DBG_INFO_MAX; i++) { - if (nimble_mem_dbg_info[i].p || nimble_mem_dbg_info[i].size != 0 ) { - ESP_LOGE("BT_NIMBLE_MEM", "--> p %p, s %d, f %s, l %d\n", - nimble_mem_dbg_info[i].p, nimble_mem_dbg_info[i].size, - nimble_mem_dbg_info[i].func, nimble_mem_dbg_info[i].line); - } - } - ESP_LOGE("BT_NIMBLE_MEM", "--> count %ld\n", nimble_mem_dbg_count); - ESP_LOGE("BT_NIMBLE_MEM", "--> size %ldB\n--> max size %ldB\n", - nimble_mem_dbg_current_size, nimble_mem_dbg_max_size); -} - -uint32_t nimble_mem_dbg_get_max_size(void) -{ - return nimble_mem_dbg_max_size; -} - -uint32_t nimble_mem_dbg_get_current_size(void) -{ - return nimble_mem_dbg_current_size; -} - -void nimble_mem_dbg_set_section_start(uint8_t index) -{ - if (index >= NIMBLE_MEM_DBG_MAX_SECTION_NUM) { - ESP_LOGE("BT_NIMBLE_MEM", "Then range of index should be between 0 and %d, current index is %d.\n", - NIMBLE_MEM_DBG_MAX_SECTION_NUM - 1, index); - return; - } - - if (nimble_mem_dbg_max_size_section[index].used) { - ESP_LOGW("BT_NIMBLE_MEM", "This index(%d) has been started, restart it.\n", index); - } - - nimble_mem_dbg_max_size_section[index].used = true; - nimble_mem_dbg_max_size_section[index].max_size = nimble_mem_dbg_current_size; -} - -void nimble_mem_dbg_set_section_end(uint8_t index) -{ - if (index >= NIMBLE_MEM_DBG_MAX_SECTION_NUM) { - ESP_LOGE("BT_NIMBLE_MEM", "Then range of index should be between 0 and %d, current index is %d.\n", - NIMBLE_MEM_DBG_MAX_SECTION_NUM - 1, index); - return; - } - - if (!nimble_mem_dbg_max_size_section[index].used) { - ESP_LOGE("BT_NIMBLE_MEM", "This index(%d) has not been started.\n", index); - return; - } - - nimble_mem_dbg_max_size_section[index].used = false; -} - -uint32_t nimble_mem_dbg_get_max_size_section(uint8_t index) -{ - if (index >= NIMBLE_MEM_DBG_MAX_SECTION_NUM){ - ESP_LOGE("BT_NIMBLE_MEM", "Then range of index should be between 0 and %d, current index is %d.\n", - NIMBLE_MEM_DBG_MAX_SECTION_NUM - 1, index); - return 0; - } - - return nimble_mem_dbg_max_size_section[index].max_size; -} - -void *nimble_mem_dbg_realloc(void *ptr, size_t new_size, const char *func, int line) -{ - size_t old_size = 0; - int i; - - void *new_ptr = realloc(ptr, new_size); - if (new_ptr == NULL && new_size > 0) { - // realloc failed, keep old ptr record - return NULL; - } - - // Find and clean old record if ptr is not NULL - if (ptr != NULL) { - for (i = 0; i < NIMBLE_MEM_DBG_INFO_MAX; i++) { - if (nimble_mem_dbg_info[i].p == ptr) { - old_size = nimble_mem_dbg_info[i].size; - nimble_mem_dbg_current_size -= old_size; - - nimble_mem_dbg_info[i].p = NULL; - nimble_mem_dbg_info[i].size = 0; - nimble_mem_dbg_info[i].func = NULL; - nimble_mem_dbg_info[i].line = 0; - nimble_mem_dbg_count--; - break; - } - } - } - - // Record the new allocation if new_size > 0 - if (new_ptr != NULL && new_size > 0) { - for (i = 0; i < NIMBLE_MEM_DBG_INFO_MAX; i++) { - if (nimble_mem_dbg_info[i].p == NULL) { - nimble_mem_dbg_info[i].p = new_ptr; - nimble_mem_dbg_info[i].size = new_size; - nimble_mem_dbg_info[i].func = func; - nimble_mem_dbg_info[i].line = line; - nimble_mem_dbg_count++; - break; - } - } - - if (i >= NIMBLE_MEM_DBG_INFO_MAX) { - ESP_LOGE("BT_NIMBLE_MEM", "%s full %s %d !!\n", __func__, func, line); - } - - nimble_mem_dbg_current_size += new_size; - if (nimble_mem_dbg_max_size < nimble_mem_dbg_current_size) { - nimble_mem_dbg_max_size = nimble_mem_dbg_current_size; - } - - for (i = 0; i < NIMBLE_MEM_DBG_MAX_SECTION_NUM; i++) { - if (nimble_mem_dbg_max_size_section[i].used && - nimble_mem_dbg_max_size_section[i].max_size < nimble_mem_dbg_current_size) { - nimble_mem_dbg_max_size_section[i].max_size = nimble_mem_dbg_current_size; - } - } - } - - return new_ptr; -} -#endif // CONFIG_BT_NIMBLE_MEM_DEBUG - -#if !CONFIG_BT_NIMBLE_LOW_SPEED_MODE -IRAM_ATTR -#endif -void *nimble_mem_malloc(size_t size) -{ - void *mem = NULL; -#ifdef CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_INTERNAL - mem = heap_caps_malloc(size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); -#elif CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_EXTERNAL - mem = heap_caps_malloc(size, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT); -#elif CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_IRAM_8BIT - mem = heap_caps_malloc_prefer(size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); -#else - mem = malloc(size); -#endif - if (!mem) { - log_count ++; - if ((log_count % 100) == 0) { - ESP_EARLY_LOGI("ESP_LOG_INFO","malloc failed (size %zu)",size); - log_count = 0; - } - assert(mem != NULL); - } -#if CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED - if(mem) { - host_mem_used_size += heap_caps_get_allocated_size(mem); - } -#endif // CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED - return mem; -} - -#if !CONFIG_BT_NIMBLE_LOW_SPEED_MODE -IRAM_ATTR -#endif -void *nimble_mem_calloc(size_t n, size_t size) -{ - void *mem = NULL; -#ifdef CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_INTERNAL - mem = heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); -#elif CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_EXTERNAL - mem = heap_caps_calloc(n, size, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT); -#elif CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_IRAM_8BIT - mem = heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); -#else - mem = calloc(n, size); -#endif -#if CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED - if(mem) { - host_mem_used_size += heap_caps_get_allocated_size(mem); - } -#endif // CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED - return mem; -} - -#if !CONFIG_BT_NIMBLE_LOW_SPEED_MODE -IRAM_ATTR -#endif -void nimble_mem_free(void *ptr) -{ -#if CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED - if (ptr) { - size_t alloc_size = heap_caps_get_allocated_size(ptr); - // assert(host_mem_used_size >= alloc_size); - host_mem_used_size -= alloc_size; - } -#endif // CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED - if (ptr) { - heap_caps_free(ptr); - } -} - -#if CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED -size_t -nimble_mem_used_size_get(void) -{ - return host_mem_used_size; -} - -#if CONFIG_BT_NIMBLE_ENABLED -uint32_t esp_host_used_heap_size_get(void) -{ - return nimble_mem_used_size_get(); -} -#endif // CONFIG_BT_NIMBLE_ENABLED -#endif // CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED diff --git a/components/bt/porting/include/bt_osi_mem.h b/components/bt/porting/include/bt_osi_mem.h index fc35c482bab..d19b3cf5c04 100644 --- a/components/bt/porting/include/bt_osi_mem.h +++ b/components/bt/porting/include/bt_osi_mem.h @@ -10,21 +10,101 @@ #include "esp_attr.h" #include "esp_heap_caps.h" - void *bt_osi_mem_malloc(size_t size); -void bt_osi_mem_free(void *ptr); +void *bt_osi_mem_calloc(size_t n, size_t size); 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_internal(void *ptr); +void bt_osi_mem_free(void *ptr); +void bt_osi_mem_free_internal(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 #if CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED size_t bt_osi_mem_internal_used_size_get(void); +size_t bt_osi_mem_used_size_get(void); #endif // CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED + +#if CONFIG_BT_NIMBLE_MEM_DEBUG +/** + * @brief Initialize NimBLE memory debug module + */ +void nimble_mem_dbg_init(void); + +/** + * @brief Record memory allocation information + * + * @param p Pointer to allocated memory + * @param size Size of allocation + * @param func Function name where allocation occurred + * @param line Line number where allocation occurred + */ +void nimble_mem_dbg_record(void *p, int size, const char *func, int line); + +/** + * @brief Clean up memory allocation record + * + * @param p Pointer to memory being freed + * @param func Function name where free occurred + * @param line Line number where free occurred + */ +void nimble_mem_dbg_clean(void *p, const char *func, int line); + +/** + * @brief Display all memory debug information + */ +void nimble_mem_dbg_show(void); + +/** + * @brief Get maximum memory size used + * + * @return Maximum memory size in bytes + */ +uint32_t nimble_mem_dbg_get_max_size(void); + +/** + * @brief Get current memory size in use + * + * @return Current memory size in bytes + */ +uint32_t nimble_mem_dbg_get_current_size(void); + +/** + * @brief Start tracking memory usage for a specific section + * + * @param index Section index (0 to NIMBLE_MEM_DBG_MAX_SECTION_NUM-1) + */ +void nimble_mem_dbg_set_section_start(uint8_t index); + +/** + * @brief Stop tracking memory usage for a specific section + * + * @param index Section index (0 to NIMBLE_MEM_DBG_MAX_SECTION_NUM-1) + */ +void nimble_mem_dbg_set_section_end(uint8_t index); + +/** + * @brief Get maximum memory size used in a specific section + * + * @param index Section index (0 to NIMBLE_MEM_DBG_MAX_SECTION_NUM-1) + * @return Maximum memory size in bytes for this section + */ +uint32_t nimble_mem_dbg_get_max_size_section(uint8_t index); + +/** + * @brief Reallocate memory with debug tracking + * + * @param ptr Pointer to memory to reallocate + * @param new_size New size of allocation + * @param func Function name where realloc occurred + * @param line Line number where realloc occurred + * @return Pointer to reallocated memory + */ +void *nimble_mem_dbg_realloc(void *ptr, size_t new_size, const char *func, int line); + +#endif // CONFIG_BT_NIMBLE_MEM_DEBUG diff --git a/components/bt/porting/mem/bt_osi_mem.c b/components/bt/porting/mem/bt_osi_mem.c index 537dc8def1e..ba670d900f1 100644 --- a/components/bt/porting/mem/bt_osi_mem.c +++ b/components/bt/porting/mem/bt_osi_mem.c @@ -10,17 +10,311 @@ #include "esp_log.h" #include +static uint8_t log_count; #if CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED static size_t controller_mem_used_size = 0; +static size_t host_mem_used_size = 0; #endif // CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED - #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 -void * -bt_osi_mem_malloc_internal(size_t size) +#if CONFIG_BT_NIMBLE_MEM_DEBUG + +#define NIMBLE_MEM_DBG_INFO_MAX 1024*3 +typedef struct { + void *p; + int size; + const char *func; + int line; +} nimble_mem_dbg_info_t; + +static uint32_t nimble_mem_dbg_count = 0; +static nimble_mem_dbg_info_t nimble_mem_dbg_info[NIMBLE_MEM_DBG_INFO_MAX]; +static uint32_t nimble_mem_dbg_current_size = 0; +static uint32_t nimble_mem_dbg_max_size = 0; + +#define NIMBLE_MEM_DBG_MAX_SECTION_NUM 5 +typedef struct { + bool used; + uint32_t max_size; +} nimble_mem_dbg_max_size_section_t; +static nimble_mem_dbg_max_size_section_t nimble_mem_dbg_max_size_section[NIMBLE_MEM_DBG_MAX_SECTION_NUM]; + +void nimble_mem_dbg_init(void) +{ + int i; + + for (i = 0; i < NIMBLE_MEM_DBG_INFO_MAX; i++) { + nimble_mem_dbg_info[i].p = NULL; + nimble_mem_dbg_info[i].size = 0; + nimble_mem_dbg_info[i].func = NULL; + nimble_mem_dbg_info[i].line = 0; + } + nimble_mem_dbg_count = 0; + nimble_mem_dbg_current_size = 0; + nimble_mem_dbg_max_size = 0; + + for (i = 0; i < NIMBLE_MEM_DBG_MAX_SECTION_NUM; i++){ + nimble_mem_dbg_max_size_section[i].used = false; + nimble_mem_dbg_max_size_section[i].max_size = 0; + } +} + +void nimble_mem_dbg_record(void *p, int size, const char *func, int line) +{ + int i; + + if (!p || size == 0) { + ESP_LOGE("BT_NIMBLE_MEM", "%s invalid !!\n", __func__); + return; + } + + for (i = 0; i < NIMBLE_MEM_DBG_INFO_MAX; i++) { + if (nimble_mem_dbg_info[i].p == NULL) { + nimble_mem_dbg_info[i].p = p; + nimble_mem_dbg_info[i].size = size; + nimble_mem_dbg_info[i].func = func; + nimble_mem_dbg_info[i].line = line; + nimble_mem_dbg_count++; + break; + } + } + + if (i >= NIMBLE_MEM_DBG_INFO_MAX) { + ESP_LOGE("BT_NIMBLE_MEM", "%s full %s %d !!\n", __func__, func, line); + } + + nimble_mem_dbg_current_size += size; + if(nimble_mem_dbg_max_size <= nimble_mem_dbg_current_size) { + nimble_mem_dbg_max_size = nimble_mem_dbg_current_size; + } + + for (i = 0; i < NIMBLE_MEM_DBG_MAX_SECTION_NUM; i++){ + if (nimble_mem_dbg_max_size_section[i].used) { + if(nimble_mem_dbg_max_size_section[i].max_size < nimble_mem_dbg_current_size) { + nimble_mem_dbg_max_size_section[i].max_size = nimble_mem_dbg_current_size; + } + } + } +} + +void nimble_mem_dbg_clean(void *p, const char *func, int line) +{ + int i; + + if (!p) { + ESP_LOGE("BT_NIMBLE_MEM", "%s %d free->%p invalid\n", func, line, p); + return; + } + + for (i = 0; i < NIMBLE_MEM_DBG_INFO_MAX; i++) { + if (nimble_mem_dbg_info[i].p == p) { + nimble_mem_dbg_current_size -= nimble_mem_dbg_info[i].size; + nimble_mem_dbg_info[i].p = NULL; + nimble_mem_dbg_info[i].size = 0; + nimble_mem_dbg_info[i].func = NULL; + nimble_mem_dbg_info[i].line = 0; + nimble_mem_dbg_count--; + break; + } + } + + if (i >= NIMBLE_MEM_DBG_INFO_MAX) { + ESP_LOGE("BT_NIMBLE_MEM", "%s full %s %d !!\n", __func__, func, line); + } +} + +void nimble_mem_dbg_show(void) +{ + int i; + + for (i = 0; i < NIMBLE_MEM_DBG_INFO_MAX; i++) { + if (nimble_mem_dbg_info[i].p || nimble_mem_dbg_info[i].size != 0 ) { + ESP_LOGE("BT_NIMBLE_MEM", "--> p %p, s %d, f %s, l %d\n", + nimble_mem_dbg_info[i].p, nimble_mem_dbg_info[i].size, + nimble_mem_dbg_info[i].func, nimble_mem_dbg_info[i].line); + } + } + ESP_LOGE("BT_NIMBLE_MEM", "--> count %ld\n", nimble_mem_dbg_count); + ESP_LOGE("BT_NIMBLE_MEM", "--> size %ldB\n--> max size %ldB\n", + nimble_mem_dbg_current_size, nimble_mem_dbg_max_size); +} + +uint32_t nimble_mem_dbg_get_max_size(void) +{ + return nimble_mem_dbg_max_size; +} + +uint32_t nimble_mem_dbg_get_current_size(void) +{ + return nimble_mem_dbg_current_size; +} + +void nimble_mem_dbg_set_section_start(uint8_t index) +{ + if (index >= NIMBLE_MEM_DBG_MAX_SECTION_NUM) { + ESP_LOGE("BT_NIMBLE_MEM", "Then range of index should be between 0 and %d, current index is %d.\n", + NIMBLE_MEM_DBG_MAX_SECTION_NUM - 1, index); + return; + } + + if (nimble_mem_dbg_max_size_section[index].used) { + ESP_LOGW("BT_NIMBLE_MEM", "This index(%d) has been started, restart it.\n", index); + } + + nimble_mem_dbg_max_size_section[index].used = true; + nimble_mem_dbg_max_size_section[index].max_size = nimble_mem_dbg_current_size; +} + +void nimble_mem_dbg_set_section_end(uint8_t index) +{ + if (index >= NIMBLE_MEM_DBG_MAX_SECTION_NUM) { + ESP_LOGE("BT_NIMBLE_MEM", "Then range of index should be between 0 and %d, current index is %d.\n", + NIMBLE_MEM_DBG_MAX_SECTION_NUM - 1, index); + return; + } + + if (!nimble_mem_dbg_max_size_section[index].used) { + ESP_LOGE("BT_NIMBLE_MEM", "This index(%d) has not been started.\n", index); + return; + } + + nimble_mem_dbg_max_size_section[index].used = false; +} + +uint32_t nimble_mem_dbg_get_max_size_section(uint8_t index) +{ + if (index >= NIMBLE_MEM_DBG_MAX_SECTION_NUM){ + ESP_LOGE("BT_NIMBLE_MEM", "Then range of index should be between 0 and %d, current index is %d.\n", + NIMBLE_MEM_DBG_MAX_SECTION_NUM - 1, index); + return 0; + } + + return nimble_mem_dbg_max_size_section[index].max_size; +} + +void *nimble_mem_dbg_realloc(void *ptr, size_t new_size, const char *func, int line) +{ + size_t old_size = 0; + int i; + + void *new_ptr = realloc(ptr, new_size); + if (new_ptr == NULL && new_size > 0) { + // realloc failed, keep old ptr record + return NULL; + } + + // Find and clean old record if ptr is not NULL + if (ptr != NULL) { + for (i = 0; i < NIMBLE_MEM_DBG_INFO_MAX; i++) { + if (nimble_mem_dbg_info[i].p == ptr) { + old_size = nimble_mem_dbg_info[i].size; + nimble_mem_dbg_current_size -= old_size; + + nimble_mem_dbg_info[i].p = NULL; + nimble_mem_dbg_info[i].size = 0; + nimble_mem_dbg_info[i].func = NULL; + nimble_mem_dbg_info[i].line = 0; + nimble_mem_dbg_count--; + break; + } + } + } + + // Record the new allocation if new_size > 0 + if (new_ptr != NULL && new_size > 0) { + for (i = 0; i < NIMBLE_MEM_DBG_INFO_MAX; i++) { + if (nimble_mem_dbg_info[i].p == NULL) { + nimble_mem_dbg_info[i].p = new_ptr; + nimble_mem_dbg_info[i].size = new_size; + nimble_mem_dbg_info[i].func = func; + nimble_mem_dbg_info[i].line = line; + nimble_mem_dbg_count++; + break; + } + } + + if (i >= NIMBLE_MEM_DBG_INFO_MAX) { + ESP_LOGE("BT_NIMBLE_MEM", "%s full %s %d !!\n", __func__, func, line); + } + + nimble_mem_dbg_current_size += new_size; + if (nimble_mem_dbg_max_size < nimble_mem_dbg_current_size) { + nimble_mem_dbg_max_size = nimble_mem_dbg_current_size; + } + + for (i = 0; i < NIMBLE_MEM_DBG_MAX_SECTION_NUM; i++) { + if (nimble_mem_dbg_max_size_section[i].used && + nimble_mem_dbg_max_size_section[i].max_size < nimble_mem_dbg_current_size) { + nimble_mem_dbg_max_size_section[i].max_size = nimble_mem_dbg_current_size; + } + } + } + + return new_ptr; +} +#endif // CONFIG_BT_NIMBLE_MEM_DEBUG + +#if !CONFIG_BT_NIMBLE_LOW_SPEED_MODE +IRAM_ATTR +#endif +void *bt_osi_mem_malloc(size_t size) +{ + void *mem = NULL; +#ifdef CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_INTERNAL + mem = heap_caps_malloc(size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_EXTERNAL + mem = heap_caps_malloc(size, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT); +#elif CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_IRAM_8BIT + mem = heap_caps_malloc_prefer(size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#else + mem = malloc(size); +#endif + if (!mem) { + log_count ++; + if ((log_count % 100) == 0) { + ESP_EARLY_LOGI("ESP_LOG_INFO","malloc failed (size %zu)",size); + log_count = 0; + } + assert(mem != NULL); + } +#if CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED + if(mem) { + host_mem_used_size += heap_caps_get_allocated_size(mem); + } +#endif // CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED + return mem; +} + +#if !CONFIG_BT_NIMBLE_LOW_SPEED_MODE +IRAM_ATTR +#endif +void *bt_osi_mem_calloc(size_t n, size_t size) +{ + void *mem = NULL; +#ifdef CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_INTERNAL + mem = heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_EXTERNAL + mem = heap_caps_calloc(n, size, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT); +#elif CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_IRAM_8BIT + mem = heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#else + mem = calloc(n, size); +#endif +#if CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED + if(mem) { + host_mem_used_size += heap_caps_get_allocated_size(mem); + } +#endif // CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED + return mem; +} + +#if !CONFIG_BT_NIMBLE_LOW_SPEED_MODE +IRAM_ATTR +#endif +void *bt_osi_mem_malloc_internal(size_t size) { void *mem_ptr; #if CONFIG_BT_LE_MEM_CHECK_ENABLED @@ -40,9 +334,10 @@ bt_osi_mem_malloc_internal(size_t size) return mem_ptr; } - -void * -bt_osi_mem_calloc_internal(size_t n, size_t size) +#if !CONFIG_BT_NIMBLE_LOW_SPEED_MODE +IRAM_ATTR +#endif +void *bt_osi_mem_calloc_internal(size_t n, size_t size) { void *mem_ptr; #if CONFIG_BT_LE_MEM_CHECK_ENABLED @@ -62,8 +357,10 @@ bt_osi_mem_calloc_internal(size_t n, size_t size) return mem_ptr; } -void -bt_osi_mem_free_internal(void *ptr) +#if !CONFIG_BT_NIMBLE_LOW_SPEED_MODE +IRAM_ATTR +#endif +void bt_osi_mem_free_internal(void *ptr) { #if CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED if (ptr) { @@ -77,35 +374,21 @@ bt_osi_mem_free_internal(void *ptr) } } -void * -bt_osi_mem_malloc(size_t size) +#if !CONFIG_BT_NIMBLE_LOW_SPEED_MODE +IRAM_ATTR +#endif +void bt_osi_mem_free(void *ptr) { - void *mem_ptr; - -#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 - - mem_ptr = heap_caps_malloc(size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); - #if CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED - if (mem_ptr) { - controller_mem_used_size += heap_caps_get_allocated_size(mem_ptr); + if (ptr) { + size_t alloc_size = heap_caps_get_allocated_size(ptr); + // assert(host_mem_used_size >= alloc_size); + host_mem_used_size -= alloc_size; } #endif // CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED - - return mem_ptr; -} - -void -bt_osi_mem_free(void *ptr) -{ - bt_osi_mem_free_internal(ptr); + if (ptr) { + heap_caps_free(ptr); + } } #if CONFIG_BT_LE_MEM_CHECK_ENABLED @@ -123,8 +406,21 @@ bt_osi_mem_internal_used_size_get(void) return controller_mem_used_size; } +size_t +bt_osi_mem_used_size_get(void) +{ + return host_mem_used_size; +} + uint32_t esp_ble_controller_used_heap_size_get(void) { return bt_osi_mem_internal_used_size_get(); } + +#if CONFIG_BT_NIMBLE_ENABLED +uint32_t esp_host_used_heap_size_get(void) +{ + return bt_osi_mem_used_size_get(); +} +#endif // CONFIG_BT_NIMBLE_ENABLED #endif // CONFIG_BT_LE_USED_MEM_STATISTICS_ENABLED diff --git a/components/bt/porting/mem/os_mempool.c b/components/bt/porting/mem/os_mempool.c index 6f8314c69dc..6893ec4a0e4 100644 --- a/components/bt/porting/mem/os_mempool.c +++ b/components/bt/porting/mem/os_mempool.c @@ -513,11 +513,9 @@ os_memblock_get(struct os_mempool *mp) // Should not happen OS_ENTER_CRITICAL(sr); mp->mp_num_free++; -#if MYNEWT_VAL(MP_BLOCK_REUSED) if (mp->mp_flags & OS_MEMPOOL_F_REUSED) { mp->mp_alloc_blocks--; } -#endif OS_EXIT_CRITICAL(sr); } } else if (block) { diff --git a/components/bt/porting/mem/os_msys_init.c b/components/bt/porting/mem/os_msys_init.c index c4574ca5ae4..5465e41e535 100644 --- a/components/bt/porting/mem/os_msys_init.c +++ b/components/bt/porting/mem/os_msys_init.c @@ -12,6 +12,7 @@ #include "mem_api.h" #include "bt_osi_mem.h" #include "esp_err.h" +#include "esp_nimble_mem.h" #if CONFIG_BT_NIMBLE_ENABLED #include "syscfg/syscfg.h" @@ -121,13 +122,11 @@ ble_os_msys_ensure_ctx(void) return 0; } - os_msys_ctx = bt_osi_mem_malloc(sizeof(*os_msys_ctx)); + os_msys_ctx = nimble_platform_mem_calloc(1, sizeof(*os_msys_ctx)); if(!os_msys_ctx) { return -1; } - memset(os_msys_ctx, 0, sizeof(*os_msys_ctx)); - return 0; } #endif // !CONFIG_BT_LE_MSYS_INIT_IN_CONTROLLER && CONFIG_BT_NIMBLE_STATIC_TO_DYNAMIC @@ -231,9 +230,9 @@ os_msys_buf_alloc(void) #if CONFIG_BT_NIMBLE_STATIC_TO_DYNAMIC #if OS_MSYS_1_BLOCK_COUNT > 0 if (!os_msys_ctx->init_1_data) { - os_msys_ctx->init_1_data = bt_osi_mem_malloc((sizeof(os_membuf_t) * SYSINIT_MSYS_1_MEMPOOL_SIZE)); + os_msys_ctx->init_1_data = nimble_platform_mem_calloc(1, (sizeof(os_membuf_t) * SYSINIT_MSYS_1_MEMPOOL_SIZE)); if(!os_msys_ctx->init_1_data){ - bt_osi_mem_free(os_msys_ctx); + nimble_platform_mem_free(os_msys_ctx); os_msys_ctx = NULL; return ESP_FAIL; } @@ -242,13 +241,13 @@ os_msys_buf_alloc(void) #if OS_MSYS_2_BLOCK_COUNT > 0 if (!os_msys_ctx->init_2_data) { - os_msys_ctx->init_2_data = bt_osi_mem_malloc((sizeof(os_membuf_t) * SYSINIT_MSYS_2_MEMPOOL_SIZE)); + os_msys_ctx->init_2_data = nimble_platform_mem_calloc(1, (sizeof(os_membuf_t) * SYSINIT_MSYS_2_MEMPOOL_SIZE)); if(!os_msys_ctx->init_2_data) { #if OS_MSYS_1_BLOCK_COUNT > 0 - bt_osi_mem_free(os_msys_ctx->init_1_data); + nimble_platform_mem_free(os_msys_ctx->init_1_data); os_msys_ctx->init_1_data = NULL; #endif - bt_osi_mem_free(os_msys_ctx); + nimble_platform_mem_free(os_msys_ctx); os_msys_ctx = NULL; return ESP_FAIL; } @@ -257,17 +256,17 @@ os_msys_buf_alloc(void) #endif #else #if OS_MSYS_1_BLOCK_COUNT > 0 - os_msys_init_1_data = (os_membuf_t *)bt_osi_mem_malloc((sizeof(os_membuf_t) * SYSINIT_MSYS_1_MEMPOOL_SIZE)); + os_msys_init_1_data = (os_membuf_t *)nimble_platform_mem_calloc(1, (sizeof(os_membuf_t) * SYSINIT_MSYS_1_MEMPOOL_SIZE)); if (!os_msys_init_1_data) { return ESP_ERR_NO_MEM; } #endif #if OS_MSYS_2_BLOCK_COUNT > 0 - os_msys_init_2_data = (os_membuf_t *)bt_osi_mem_malloc((sizeof(os_membuf_t) * SYSINIT_MSYS_2_MEMPOOL_SIZE)); + os_msys_init_2_data = (os_membuf_t *)nimble_platform_mem_calloc(1, (sizeof(os_membuf_t) * SYSINIT_MSYS_2_MEMPOOL_SIZE)); if (!os_msys_init_2_data) { #if OS_MSYS_1_BLOCK_COUNT > 0 - bt_osi_mem_free(os_msys_init_1_data); + nimble_platform_mem_free(os_msys_init_1_data); os_msys_init_1_data = NULL; #endif return ESP_ERR_NO_MEM; @@ -285,32 +284,32 @@ os_msys_buf_free(void) if (os_msys_ctx) { #if OS_MSYS_1_BLOCK_COUNT > 0 if (os_msys_ctx->init_1_data) { - bt_osi_mem_free(os_msys_ctx->init_1_data); + nimble_platform_mem_free(os_msys_ctx->init_1_data); os_msys_ctx->init_1_data = NULL; } os_mempool_unregister(&os_msys_ctx->init_1_mempool); #endif #if OS_MSYS_2_BLOCK_COUNT > 0 if (os_msys_ctx->init_2_data) { - bt_osi_mem_free(os_msys_ctx->init_2_data); + nimble_platform_mem_free(os_msys_ctx->init_2_data); os_msys_ctx->init_2_data = NULL; } os_mempool_unregister(&os_msys_ctx->init_2_mempool); #endif - bt_osi_mem_free(os_msys_ctx); + nimble_platform_mem_free(os_msys_ctx); os_msys_ctx = NULL; } #else #if OS_MSYS_1_BLOCK_COUNT > 0 - bt_osi_mem_free(os_msys_init_1_data); + nimble_platform_mem_free(os_msys_init_1_data); os_msys_init_1_data = NULL; os_mempool_unregister(&os_msys_init_1_mempool); #endif #if OS_MSYS_2_BLOCK_COUNT > 0 - bt_osi_mem_free(os_msys_init_2_data); + nimble_platform_mem_free(os_msys_init_2_data); os_msys_init_2_data = NULL; os_mempool_unregister(&os_msys_init_1_mempool); #endif diff --git a/components/bt/porting/npl/freertos/src/npl_os_freertos.c b/components/bt/porting/npl/freertos/src/npl_os_freertos.c index d4b8fc883c5..cdcc14f6a3a 100644 --- a/components/bt/porting/npl/freertos/src/npl_os_freertos.c +++ b/components/bt/porting/npl/freertos/src/npl_os_freertos.c @@ -323,7 +323,8 @@ IRAM_ATTR npl_freertos_eventq_remove(struct ble_npl_eventq *evq, portYIELD_FROM_ISR(); } } else { - portENTER_CRITICAL(&ble_port_mutex); + portMUX_TYPE ble_npl_mut = portMUX_INITIALIZER_UNLOCKED; + portENTER_CRITICAL(&ble_npl_mut); count = uxQueueMessagesWaiting(eventq->q); for (i = 0; i < count; i++) { @@ -338,7 +339,7 @@ IRAM_ATTR npl_freertos_eventq_remove(struct ble_npl_eventq *evq, BLE_LL_ASSERT(ret == pdPASS); } - portEXIT_CRITICAL(&ble_port_mutex); + portEXIT_CRITICAL(&ble_npl_mut); } event->queued = 0; diff --git a/examples/bluetooth/esp_hid_device/main/esp_hid_device_main.c b/examples/bluetooth/esp_hid_device/main/esp_hid_device_main.c index d0081b843be..4da5f37f67e 100644 --- a/examples/bluetooth/esp_hid_device/main/esp_hid_device_main.c +++ b/examples/bluetooth/esp_hid_device/main/esp_hid_device_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -966,6 +966,9 @@ void app_main(void) ble_hs_cfg.store_status_cb = ble_store_util_status_rr; /* Starting nimble task after gatts is initialized*/ - nimble_port_freertos_init(ble_hid_device_host_task); + ret = esp_nimble_enable(ble_hid_device_host_task); + if (ret) { + ESP_LOGE(TAG, "esp_nimble_enable failed: %d", ret); + } #endif } diff --git a/examples/bluetooth/esp_hid_host/main/esp_hid_host_main.c b/examples/bluetooth/esp_hid_host/main/esp_hid_host_main.c index ab9d8aa21eb..8934b60c1aa 100644 --- a/examples/bluetooth/esp_hid_host/main/esp_hid_host_main.c +++ b/examples/bluetooth/esp_hid_host/main/esp_hid_host_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -238,7 +238,10 @@ void app_main(void) ble_hs_cfg.store_status_cb = ble_store_util_status_rr; /* Starting nimble task after gatts is initialized*/ - nimble_port_freertos_init(ble_hid_host_task); + ret = esp_nimble_enable(ble_hid_host_task); + if (ret) { + ESP_LOGE(TAG, "esp_nimble_enable failed: %d", ret); + } vTaskDelay(200);