Skip to content

Commit

Permalink
Merge branch 'bugfix/bugfix_for_esp32c2_esp32c6_esp32h2' into 'master'
Browse files Browse the repository at this point in the history
fix(ble): fixed crash when memory is insufficient on esp32c2

Closes BLERP-9, BLERP-10, BLERP-11, BLERP-13, and BLERP-12

See merge request espressif/esp-idf!25823
  • Loading branch information
jack0c committed Sep 14, 2023
2 parents 8d48ff5 + fa4f34e commit ca78f8b
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 64 deletions.
33 changes: 33 additions & 0 deletions components/bt/controller/esp32c2/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,36 @@ config BT_LE_USE_ESP_TIMER
default y
help
Set this option to use Esp Timer which has higher priority timer instead of FreeRTOS timer

config BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_SUPP
bool "BLE adv report flow control supported"
default y
help
The function is mainly used to enable flow control for advertising reports. When it is enabled,
advertising reports will be discarded by the controller if the number of unprocessed advertising
reports exceeds the size of BLE adv report flow control.

config BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_NUM
int "BLE adv report flow control number"
depends on BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_SUPP
range 50 1000
default 100
help
The number of unprocessed advertising report that bluetooth host can save.If you set
`BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_NUM` to a small value, this may cause adv packets lost.
If you set `BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_NUM` to a large value, bluetooth host may cache a
lot of adv packets and this may cause system memory run out. For example, if you set
it to 50, the maximum memory consumed by host is 35 * 50 bytes. Please set
`BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_NUM` according to your system free memory and handle adv
packets as fast as possible, otherwise it will cause adv packets lost.

config BT_CTRL_BLE_ADV_REPORT_DISCARD_THRSHOLD
int "BLE adv lost event threshold value"
depends on BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_SUPP
range 1 1000
default 20
help
When adv report flow control is enabled, The ADV lost event will be generated when the number
of ADV packets lost in the controller reaches this threshold. It is better to set a larger value.
If you set `BT_CTRL_BLE_ADV_REPORT_DISCARD_THRSHOLD` to a small value or printf every adv lost event, it
may cause adv packets lost more.
65 changes: 42 additions & 23 deletions components/bt/controller/esp32c2/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
#include "hci/hci_hal.h"
#endif

#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
#include "esp_private/sleep_modem.h"
#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

Expand Down Expand Up @@ -139,6 +143,9 @@ extern uint32_t r_os_cputime_get32(void);
extern uint32_t r_os_cputime_ticks_to_usecs(uint32_t ticks);
extern void r_ble_lll_rfmgmt_set_sleep_cb(void *s_cb, void *w_cb, void *s_arg, void *w_arg, uint32_t us_to_enabled);
extern void r_ble_rtc_wake_up_state_clr(void);
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
extern void esp_ble_set_wakeup_overhead(uint32_t overhead);
#endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */
extern int os_msys_init(void);
extern void os_msys_buf_free(void);
extern int ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x,
Expand Down Expand Up @@ -378,23 +385,6 @@ static int esp_ecc_gen_dh_key(const uint8_t *peer_pub_key_x, const uint8_t *peer
return rc;
}

#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end)
{
if (!end) {
for (int i = 0; i < len; i++) {
esp_rom_printf("%02x,", addr[i]);
}

} else {
for (int i = 0; i < len; i++) {
esp_rom_printf("%02x,", addr[i]);
}
esp_rom_printf("\n");
}
}
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED

#ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART
static void hci_uart_start_tx_wrapper(int uart_no)
{
Expand Down Expand Up @@ -459,6 +449,13 @@ static int esp_intr_free_wrapper(void **ret_handle)
return rc;
}

#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
void sleep_modem_light_sleep_overhead_set(uint32_t overhead)
{
esp_ble_set_wakeup_overhead(overhead);
}
#endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */

IRAM_ATTR void controller_sleep_cb(uint32_t enable_tick, void *arg)
{
if (!s_ble_active) {
Expand Down Expand Up @@ -504,32 +501,40 @@ esp_err_t controller_sleep_init(void)
if (rc != ESP_OK) {
goto error;
}

#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
esp_sleep_enable_bt_wakeup();
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "Enable light sleep, the wake up source is BLE timer");

rc = esp_pm_register_inform_out_light_sleep_overhead_callback(sleep_modem_light_sleep_overhead_set);
if (rc != ESP_OK) {
goto error;
}
#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE
return rc;

error:
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
esp_sleep_disable_bt_wakeup();
esp_pm_unregister_inform_out_light_sleep_overhead_callback(sleep_modem_light_sleep_overhead_set);
#endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */
/*lock should release first and then delete*/
if (s_pm_lock != NULL) {
esp_pm_lock_delete(s_pm_lock);
s_pm_lock = NULL;
}

esp_sleep_disable_bt_wakeup();

#endif //CONFIG_PM_ENABLE
return rc;
}

void controller_sleep_deinit(void)
{
#ifdef CONFIG_PM_ENABLE
#ifdef CONFIG_FREERTOS_USE_TICKLESS_IDLE
r_ble_rtc_wake_up_state_clr();
esp_sleep_disable_bt_wakeup();
esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_AUTO);

esp_pm_unregister_inform_out_light_sleep_overhead_callback(sleep_modem_light_sleep_overhead_set);
#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE
#ifdef CONFIG_PM_ENABLE
/*lock should release first and then delete*/
esp_pm_lock_delete(s_pm_lock);
s_pm_lock = NULL;
Expand Down Expand Up @@ -984,11 +989,25 @@ uint8_t esp_ble_get_chip_rev_version(void)
}

#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end)
{
for (int i = 0; i < len; i++) {
esp_rom_printf("%02x,", addr[i]);
}
if (end) {
esp_rom_printf("\n");
}
}

void esp_ble_controller_log_dump_all(bool output)
{
portMUX_TYPE spinlock;

portENTER_CRITICAL_SAFE(&spinlock);
BT_ASSERT_PRINT("\r\n[DUMP_START:");
ble_log_async_output_dump_all(output);
BT_ASSERT_PRINT("]\r\n");
portEXIT_CRITICAL_SAFE(&spinlock);
}
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED

Expand Down
31 changes: 14 additions & 17 deletions components/bt/controller/esp32c6/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,23 +377,6 @@ static int esp_ecc_gen_dh_key(const uint8_t *peer_pub_key_x, const uint8_t *peer
return rc;
}

#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end)
{
if (!end) {
for (int i = 0; i < len; i++) {
esp_rom_printf("%02x,", addr[i]);
}

} else {
for (int i = 0; i < len; i++) {
esp_rom_printf("%02x,", addr[i]);
}
esp_rom_printf("\n");
}
}
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED

#ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART
static void hci_uart_start_tx_wrapper(int uart_no)
{
Expand Down Expand Up @@ -1174,11 +1157,25 @@ esp_power_level_t esp_ble_tx_power_get_enhanced(esp_ble_enhanced_power_type_t po
}

#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end)
{
for (int i = 0; i < len; i++) {
esp_rom_printf("%02x,", addr[i]);
}
if (end) {
esp_rom_printf("\n");
}
}

void esp_ble_controller_log_dump_all(bool output)
{
portMUX_TYPE spinlock;

portENTER_CRITICAL_SAFE(&spinlock);
BT_ASSERT_PRINT("\r\n[DUMP_START:");
ble_log_async_output_dump_all(output);
BT_ASSERT_PRINT("]\r\n");
portEXIT_CRITICAL_SAFE(&spinlock);
}
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED

Expand Down
31 changes: 14 additions & 17 deletions components/bt/controller/esp32h2/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,23 +374,6 @@ static int esp_ecc_gen_dh_key(const uint8_t *peer_pub_key_x, const uint8_t *peer
return rc;
}

#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end)
{
if (!end) {
for (int i = 0; i < len; i++) {
esp_rom_printf("%02x,", addr[i]);
}

} else {
for (int i = 0; i < len; i++) {
esp_rom_printf("%02x,", addr[i]);
}
esp_rom_printf("\n");
}
}
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED

#ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART
static void hci_uart_start_tx_wrapper(int uart_no)
{
Expand Down Expand Up @@ -1161,11 +1144,25 @@ esp_power_level_t esp_ble_tx_power_get_enhanced(esp_ble_enhanced_power_type_t po
}

#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end)
{
for (int i = 0; i < len; i++) {
esp_rom_printf("%02x,", addr[i]);
}
if (end) {
esp_rom_printf("\n");
}
}

void esp_ble_controller_log_dump_all(bool output)
{
portMUX_TYPE spinlock;

portENTER_CRITICAL_SAFE(&spinlock);
BT_ASSERT_PRINT("\r\n[DUMP_START:");
ble_log_async_output_dump_all(output);
BT_ASSERT_PRINT("]\r\n");
portEXIT_CRITICAL_SAFE(&spinlock);
}
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED

Expand Down
2 changes: 1 addition & 1 deletion components/bt/controller/lib_esp32c2/esp32c2-bt-lib
2 changes: 1 addition & 1 deletion components/bt/controller/lib_esp32c6/esp32c6-bt-lib
2 changes: 1 addition & 1 deletion components/bt/controller/lib_esp32h2/esp32h2-bt-lib
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@

#endif //CONFIG_IDF_TARGET_ESP32

#if (CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2)
#if (CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2)
//BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP
#ifdef CONFIG_BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_SUPP
#define UC_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP CONFIG_BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_SUPP
Expand All @@ -244,7 +244,7 @@
#define UC_BTDM_BLE_ADV_REPORT_DISCARD_THRSHOLD 20
#endif

#endif //(CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2)
#endif //(CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2)

//BT ACL CONNECTIONS
#ifdef CONFIG_BT_ACL_CONNECTIONS
Expand Down
2 changes: 1 addition & 1 deletion components/bt/include/esp32c6/include/esp_bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ extern int esp_ble_hw_get_static_addr(esp_ble_addr_t *addr);

#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
/**
* @brief dump all controller log information cached in buffer
* @brief dump all log information cached in buffers.
* @param output : true for log dump, false will take no effect
*/
void esp_ble_controller_log_dump_all(bool output);
Expand Down
2 changes: 1 addition & 1 deletion components/bt/include/esp32h2/include/esp_bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ extern int esp_ble_hw_get_static_addr(esp_ble_addr_t *addr);

#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
/**
* @brief dump all controller log information cached in buffer
* @brief dump all log information cached in buffers.
* @param output : true for log dump, false will take no effect
*/
void esp_ble_controller_log_dump_all(bool output);
Expand Down

0 comments on commit ca78f8b

Please sign in to comment.