From 699dde8a388a1d6bbceae9d3ecaa7057e34951dd Mon Sep 17 00:00:00 2001 From: Shen Weilong Date: Tue, 17 Jan 2023 18:47:12 +0800 Subject: [PATCH] Fixed the bluedroid hci crash due insufficient memory on ESP32C2 --- components/bt/controller/esp32c2/Kconfig.in | 9 ++++- components/bt/controller/esp32h2/Kconfig.in | 10 ++++- components/bt/host/nimble/Kconfig.in | 4 +- .../freertos/include/nimble/npl_freertos.h | 11 +++++- .../npl/freertos/src/npl_os_freertos.c | 38 +++++++++---------- 5 files changed, 47 insertions(+), 25 deletions(-) diff --git a/components/bt/controller/esp32c2/Kconfig.in b/components/bt/controller/esp32c2/Kconfig.in index 4c2ac499a9..9db1547fb6 100644 --- a/components/bt/controller/esp32c2/Kconfig.in +++ b/components/bt/controller/esp32c2/Kconfig.in @@ -207,7 +207,7 @@ menu "Memory Settings" config BT_LE_ACL_BUF_COUNT int "ACL Buffer count" - default 24 + default 10 help The number of ACL data buffers. @@ -387,3 +387,10 @@ choice BT_LE_WAKEUP_SOURCE help Use BLE rtc timer to wakeup CPU endchoice + +config BT_LE_USE_ESP_TIMER + bool "Use Esp Timer for callout" + depends on !BT_NIMBLE_ENABLED + default y + help + Set this option to use Esp Timer which has higher priority timer instead of FreeRTOS timer diff --git a/components/bt/controller/esp32h2/Kconfig.in b/components/bt/controller/esp32h2/Kconfig.in index bdd492b7b0..3f42b493ba 100644 --- a/components/bt/controller/esp32h2/Kconfig.in +++ b/components/bt/controller/esp32h2/Kconfig.in @@ -207,7 +207,7 @@ menu "Memory Settings" config BT_LE_ACL_BUF_COUNT int "ACL Buffer count" - default 24 + default 10 help The number of ACL data buffers. @@ -381,5 +381,11 @@ choice BT_LE_WAKEUP_SOURCE bool "Use ESP timer to wakeup CPU" help Use esp timer to wakeup CPU - endchoice + +config BT_LE_USE_ESP_TIMER + bool "Use Esp Timer for callout" + depends on !BT_NIMBLE_ENABLED + default y + help + Set this option to use Esp Timer which has higher priority timer instead of FreeRTOS timer diff --git a/components/bt/host/nimble/Kconfig.in b/components/bt/host/nimble/Kconfig.in index 8d54a185ca..204e5cc0c8 100644 --- a/components/bt/host/nimble/Kconfig.in +++ b/components/bt/host/nimble/Kconfig.in @@ -64,10 +64,10 @@ config BT_NIMBLE_LOG_LEVEL config BT_NIMBLE_MAX_CONNECTIONS int "Maximum number of concurrent connections" - range 1 8 if IDF_TARGET_ESP32H4 + range 1 8 if IDF_TARGET_ESP32H2 range 1 2 if IDF_TARGET_ESP32C2 range 1 9 if !SOC_ESP_NIMBLE_CONTROLLER - default 3 if (IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32S3 || IDF_TARGET_ESP32H4) + default 3 if (IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32S3 || IDF_TARGET_ESP32H2) default 2 if IDF_TARGET_ESP32C2 depends on BT_NIMBLE_ENABLED help diff --git a/components/bt/porting/npl/freertos/include/nimble/npl_freertos.h b/components/bt/porting/npl/freertos/include/nimble/npl_freertos.h index 0c06c431ad..2ddbbff782 100644 --- a/components/bt/porting/npl/freertos/include/nimble/npl_freertos.h +++ b/components/bt/porting/npl/freertos/include/nimble/npl_freertos.h @@ -12,6 +12,15 @@ #ifdef __cplusplus extern "C" { #endif +#include "sdkconfig.h" + +#if ((defined(CONFIG_BT_NIMBLE_USE_ESP_TIMER) && CONFIG_BT_NIMBLE_USE_ESP_TIMER) || \ + (defined(CONFIG_BT_LE_USE_ESP_TIMER) && CONFIG_BT_LE_USE_ESP_TIMER)) +/* Use esp timer instead of FreeRTOS timer to implement the callout. */ +#define BLE_NPL_USE_ESP_TIMER (1) +#else +#define BLE_NPL_USE_ESP_TIMER (0) +#endif typedef void ble_npl_event_fn(struct ble_npl_event *ev); @@ -26,7 +35,7 @@ struct ble_npl_eventq_freertos { }; struct ble_npl_callout_freertos { -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER esp_timer_handle_t handle; #else TimerHandle_t handle; 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 12a596e61e..ef591a58f9 100644 --- a/components/bt/porting/npl/freertos/src/npl_os_freertos.c +++ b/components/bt/porting/npl/freertos/src/npl_os_freertos.c @@ -27,7 +27,7 @@ portMUX_TYPE ble_port_mutex = portMUX_INITIALIZER_UNLOCKED; -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER static const char *TAG = "Timer"; #endif @@ -540,7 +540,7 @@ IRAM_ATTR npl_freertos_sem_release(struct ble_npl_sem *sem) return BLE_NPL_OK; } -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER static void IRAM_ATTR ble_npl_event_fn_wrapper(void *arg) { @@ -606,7 +606,7 @@ npl_freertos_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq memset(callout, 0, sizeof(*callout)); ble_npl_event_init(&callout->ev, ev_cb, ev_arg); -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER callout->evq = evq; esp_timer_create_args_t create_args = { @@ -630,7 +630,7 @@ npl_freertos_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq co->co = NULL; return -1; } -#endif // CONFIG_BT_NIMBLE_USE_ESP_TIMER +#endif // BLE_NPL_USE_ESP_TIMER } else { callout = (struct ble_npl_callout_freertos *)co->co; BLE_LL_ASSERT(callout); @@ -649,7 +649,7 @@ npl_freertos_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq memset(callout, 0, sizeof(*callout)); ble_npl_event_init(&callout->ev, ev_cb, ev_arg); -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER callout->evq = evq; esp_timer_create_args_t create_args = { @@ -673,7 +673,7 @@ npl_freertos_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq co->co = NULL; return -1; } -#endif // CONFIG_BT_NIMBLE_USE_ESP_TIMER +#endif // BLE_NPL_USE_ESP_TIMER } else { callout = (struct ble_npl_callout_freertos *)co->co; @@ -700,7 +700,7 @@ npl_freertos_callout_deinit(struct ble_npl_callout *co) } ble_npl_event_deinit(&callout->ev); -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER esp_err_t err = esp_timer_stop(callout->handle); if(err != ESP_OK) { if (err != ESP_ERR_INVALID_STATE) { // ESP_ERR_INVALID_STATE is expected when timer is already stopped @@ -718,7 +718,7 @@ npl_freertos_callout_deinit(struct ble_npl_callout *co) #else free((void *)callout); #endif // OS_MEM_ALLOC -#endif // CONFIG_BT_NIMBLE_USE_ESP_TIMER +#endif // BLE_NPL_USE_ESP_TIMER co->co = NULL; memset(co, 0, sizeof(struct ble_npl_callout)); } @@ -735,7 +735,7 @@ ble_npl_error_t IRAM_ATTR npl_freertos_callout_reset(struct ble_npl_callout *co, ble_npl_time_t ticks) { struct ble_npl_callout_freertos *callout = (struct ble_npl_callout_freertos *)co->co; -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER esp_timer_stop(callout->handle); return esp_err_to_npl_error(esp_timer_start_once(callout->handle, ticks*1000)); @@ -773,7 +773,7 @@ IRAM_ATTR npl_freertos_callout_stop(struct ble_npl_callout *co) return; } -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER esp_timer_stop(callout->handle); #else xTimerStop(callout->handle, portMAX_DELAY); @@ -784,7 +784,7 @@ bool IRAM_ATTR npl_freertos_callout_is_active(struct ble_npl_callout *co) { struct ble_npl_callout_freertos *callout = (struct ble_npl_callout_freertos *)co->co; -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER return esp_timer_is_active(callout->handle); #else return xTimerIsTimerActive(callout->handle) == pdTRUE; @@ -794,7 +794,7 @@ IRAM_ATTR npl_freertos_callout_is_active(struct ble_npl_callout *co) ble_npl_time_t IRAM_ATTR npl_freertos_callout_get_ticks(struct ble_npl_callout *co) { -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER /* Currently, esp_timer does not support an API which gets the expiry time for * current timer. * Returning 0 from here should not cause any effect. @@ -819,7 +819,7 @@ IRAM_ATTR npl_freertos_callout_remaining_ticks(struct ble_npl_callout *co, struct ble_npl_callout_freertos *callout = (struct ble_npl_callout_freertos *)co->co; -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) uint64_t expiry = 0; esp_err_t err; @@ -862,7 +862,7 @@ IRAM_ATTR npl_freertos_callout_set_arg(struct ble_npl_callout *co, void *arg) uint32_t IRAM_ATTR npl_freertos_time_get(void) { -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER return esp_timer_get_time() / 1000; #else return xTaskGetTickCountFromISR(); @@ -873,7 +873,7 @@ ble_npl_error_t IRAM_ATTR npl_freertos_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks) { uint64_t ticks; -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER ticks = (uint64_t)ms; #else ticks = ((uint64_t)ms * configTICK_RATE_HZ) / 1000; @@ -891,7 +891,7 @@ ble_npl_error_t IRAM_ATTR npl_freertos_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms) { uint64_t ms; -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER ms = ((uint64_t)ticks); #else ms = ((uint64_t)ticks * 1000) / configTICK_RATE_HZ; @@ -908,7 +908,7 @@ IRAM_ATTR npl_freertos_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms) ble_npl_time_t IRAM_ATTR npl_freertos_time_ms_to_ticks32(uint32_t ms) { -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER return ms; #else return ms * configTICK_RATE_HZ / 1000; @@ -918,7 +918,7 @@ IRAM_ATTR npl_freertos_time_ms_to_ticks32(uint32_t ms) uint32_t IRAM_ATTR npl_freertos_time_ticks_to_ms32(ble_npl_time_t ticks) { -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER return ticks; #else return ticks * 1000 / configTICK_RATE_HZ; @@ -928,7 +928,7 @@ IRAM_ATTR npl_freertos_time_ticks_to_ms32(ble_npl_time_t ticks) void IRAM_ATTR npl_freertos_time_delay(ble_npl_time_t ticks) { -#if CONFIG_BT_NIMBLE_USE_ESP_TIMER +#if BLE_NPL_USE_ESP_TIMER vTaskDelay(ticks / portTICK_PERIOD_MS); #else vTaskDelay(ticks);