From 99197d4a022b35bc49a83bc4d0523cd97d1fb92b Mon Sep 17 00:00:00 2001 From: zwl Date: Fri, 18 Aug 2023 11:55:48 +0800 Subject: [PATCH 1/5] feat(ble): supported trace function and adv report flow control on esp32c2 --- components/bt/controller/esp32c2/Kconfig.in | 13 +++ components/bt/controller/esp32c2/bt.c | 96 +++++++++++++++---- .../bt/controller/lib_esp32c2/esp32c2-bt-lib | 2 +- .../bt/include/esp32c2/include/esp_bt.h | 8 ++ components/esp_rom/esp32c2/ld/esp32c2.rom.ld | 11 --- 5 files changed, 100 insertions(+), 30 deletions(-) diff --git a/components/bt/controller/esp32c2/Kconfig.in b/components/bt/controller/esp32c2/Kconfig.in index cd1ec8edad..70ebc00def 100644 --- a/components/bt/controller/esp32c2/Kconfig.in +++ b/components/bt/controller/esp32c2/Kconfig.in @@ -253,6 +253,19 @@ config BT_LE_CONTROLLER_TASK_STACK_SIZE help This configures stack size of NimBLE controller task +config BT_LE_CONTROLLER_LOG_ENABLED + bool "Controller log enable" + default n + help + Enable controller 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_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 b52d9480e7..f15f9ed87a 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -72,12 +72,12 @@ #ifdef CONFIG_BT_BLUEDROID_ENABLED /* ACL_DATA_MBUF_LEADINGSPCAE: The leadingspace in user info header for ACL data */ #define ACL_DATA_MBUF_LEADINGSPCAE 4 -#endif +#endif // CONFIG_BT_BLUEDROID_ENABLED + /* Types definition ************************************************************************ */ - struct osi_coex_funcs_t { uint32_t _magic; uint32_t _version; @@ -110,14 +110,21 @@ struct ext_funcs_t { uint32_t magic; }; +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +typedef void (*interface_func_t) (uint32_t len, const uint8_t*addr, bool end); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED /* External functions or variables ************************************************************************ */ - extern int ble_osi_coex_funcs_register(struct osi_coex_funcs_t *coex_funcs); extern int coex_core_ble_conn_dyn_prio_get(bool *low, bool *high); extern int ble_controller_init(esp_bt_controller_config_t *cfg); +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +extern int ble_log_init_async(interface_func_t bt_controller_log_interface, bool task_create); +extern int ble_log_deinit_async(void); +extern void ble_log_async_output_dump_all(bool output); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED extern int ble_controller_deinit(void); extern int ble_controller_enable(uint8_t mode); extern int ble_controller_disable(void); @@ -168,8 +175,9 @@ static int hci_uart_config_wrapper(int uart_no, int32_t speed, uint8_t databits, static int hci_uart_close_wrapper(int uart_no); static void hci_uart_blocking_tx_wrapper(int port, uint8_t data); static int hci_uart_init_wrapper(int uart_no, void *cfg); -#endif -static int esp_intr_alloc_wrapper(int source, int flags, intr_handler_t handler, void *arg, void **ret_handle_in); +#endif // CONFIG_BT_LE_HCI_INTERFACE_USE_UART +static int esp_intr_alloc_wrapper(int source, int flags, intr_handler_t handler, + void *arg, void **ret_handle_in); static int esp_intr_free_wrapper(void **ret_handle); static void osi_assert_wrapper(const uint32_t ln, const char *fn, uint32_t param1, uint32_t param2); static uint32_t osi_random_wrapper(void); @@ -177,10 +185,12 @@ static void esp_reset_rpa_moudle(void); static int esp_ecc_gen_key_pair(uint8_t *pub, uint8_t *priv); static int esp_ecc_gen_dh_key(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_y, const uint8_t *our_priv_key, uint8_t *out_dhkey); +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED /* Local variable definition *************************************************************************** */ - /* Static variable declare */ static DRAM_ATTR esp_bt_controller_status_t ble_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE; @@ -189,7 +199,7 @@ static bool s_ble_active = false; #ifdef CONFIG_PM_ENABLE static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock = NULL; #define BTDM_MIN_TIMER_UNCERTAINTY_US (200) -#endif /* #ifdef CONFIG_PM_ENABLE */ +#endif // CONFIG_PM_ENABLE #define BLE_RTC_DELAY_US (1800) @@ -234,8 +244,12 @@ static void IRAM_ATTR esp_reset_rpa_moudle(void) DPORT_CLEAR_PERI_REG_MASK(SYSTEM_CORE_RST_EN_REG, BLE_RPA_REST_BIT); } -static void IRAM_ATTR osi_assert_wrapper(const uint32_t ln, const char *fn, uint32_t param1, uint32_t param2) +static void IRAM_ATTR osi_assert_wrapper(const uint32_t ln, const char *fn, + uint32_t param1, uint32_t param2) { +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED + esp_ble_controller_log_dump_all(true); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED BT_ASSERT_PRINT("BLE assert: line %d in function %s, param: 0x%x, 0x%x", ln, fn, param1, param2); assert(0); } @@ -249,17 +263,17 @@ static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status) { #if CONFIG_SW_COEXIST_ENABLE coex_schm_status_bit_set(type, status); -#endif +#endif // CONFIG_SW_COEXIST_ENABLE } static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status) { #if CONFIG_SW_COEXIST_ENABLE coex_schm_status_bit_clear(type, status); -#endif +#endif // CONFIG_SW_COEXIST_ENABLE } -#ifdef CONFIG_BT_BLUEDROID_ENABLED +#ifdef CONFIG_BT_BLUEDROID_ENABLED bool esp_vhci_host_check_send_available(void) { if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) { @@ -323,7 +337,6 @@ void esp_vhci_host_send_packet(uint8_t *data, uint16_t len) assert(os_mbuf_append(om, &data[1], len - 1) == 0); ble_hci_trans_hs_acl_tx(om); } - } esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback) @@ -336,8 +349,7 @@ esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callba return ESP_OK; } - -#endif +#endif // CONFIG_BT_BLUEDROID_ENABLED static int task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id) { return (uint32_t)xTaskCreatePinnedToCore(task_func, name, stack_depth, param, prio, task_handle, (core_id < portNUM_PROCESSORS ? core_id : tskNO_AFFINITY)); @@ -367,6 +379,23 @@ 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) { @@ -382,8 +411,9 @@ static int hci_uart_init_cbs_wrapper(int uart_no, hci_uart_tx_char tx_func, } -static int hci_uart_config_wrapper(int port_num, int32_t baud_rate, uint8_t data_bits, uint8_t stop_bits, - uart_parity_t parity, uart_hw_flowcontrol_t flow_ctl) +static int hci_uart_config_wrapper(int port_num, int32_t baud_rate, uint8_t data_bits, + uint8_t stop_bits,uart_parity_t parity, + uart_hw_flowcontrol_t flow_ctl) { int rc = -1; rc = hci_uart_config(port_num, baud_rate, data_bits, stop_bits, parity, flow_ctl); @@ -599,7 +629,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) if (ble_osi_coex_funcs_register((struct osi_coex_funcs_t *)&s_osi_coex_funcs_ro) != 0) { ESP_LOGW(NIMBLE_PORT_LOG_TAG, "osi coex funcs reg failed"); ret = ESP_ERR_INVALID_ARG; - goto free_controller; + goto modem_deint; } #if CONFIG_SW_COEXIST_ENABLE @@ -608,9 +638,23 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) ret = ble_controller_init(cfg); if (ret != ESP_OK) { ESP_LOGW(NIMBLE_PORT_LOG_TAG, "ble_controller_init failed %d", ret); - goto free_controller; + goto modem_deint; } +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED + interface_func_t bt_controller_log_interface; + bt_controller_log_interface = esp_bt_controller_log_interface; +#if CONFIG_BT_LE_CONTROLLER_LOG_DUMP_ONLY + ret = ble_log_init_async(bt_controller_log_interface, false); +#else + ret = ble_log_init_async(bt_controller_log_interface, true); +#endif // CONFIG_BT_CONTROLLER_LOG_DUMP + if (ret != ESP_OK) { + ESP_LOGW(NIMBLE_PORT_LOG_TAG, "ble_controller_log_init failed %d", ret); + goto controller_init_err; + } +#endif // CONFIG_BT_CONTROLLER_LOG_ENABLED + ret = controller_sleep_init(); if (ret != ESP_OK) { ESP_LOGW(NIMBLE_PORT_LOG_TAG, "controller_sleep_init failed %d", ret); @@ -631,7 +675,12 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) return ESP_OK; free_controller: controller_sleep_deinit(); +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +controller_init_err: + ble_log_deinit_async(); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED ble_controller_deinit(); +modem_deint: esp_phy_modem_deinit(); #if CONFIG_BT_NIMBLE_ENABLED ble_npl_eventq_deinit(nimble_port_get_dflt_eventq()); @@ -654,6 +703,9 @@ esp_err_t esp_bt_controller_deinit(void) controller_sleep_deinit(); +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED + ble_log_deinit_async(); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED ble_controller_deinit(); #if CONFIG_BT_NIMBLE_ENABLED @@ -933,6 +985,14 @@ uint8_t esp_ble_get_chip_rev_version(void) return efuse_ll_get_chip_wafer_version_minor(); } +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +void esp_ble_controller_log_dump_all(bool output) +{ + BT_ASSERT_PRINT("\r\n[DUMP_START:"); + ble_log_async_output_dump_all(output); + BT_ASSERT_PRINT("]\r\n"); +} +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED #if (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED == true) diff --git a/components/bt/controller/lib_esp32c2/esp32c2-bt-lib b/components/bt/controller/lib_esp32c2/esp32c2-bt-lib index d17188c9f6..9da8ad3ebd 160000 --- a/components/bt/controller/lib_esp32c2/esp32c2-bt-lib +++ b/components/bt/controller/lib_esp32c2/esp32c2-bt-lib @@ -1 +1 @@ -Subproject commit d17188c9f61400792a8248bf53378ee92e6f21a4 +Subproject commit 9da8ad3ebd7932ea6f92578fb1337c7df5000ac7 diff --git a/components/bt/include/esp32c2/include/esp_bt.h b/components/bt/include/esp32c2/include/esp_bt.h index ecf908d5de..9707caaebb 100644 --- a/components/bt/include/esp32c2/include/esp_bt.h +++ b/components/bt/include/esp32c2/include/esp_bt.h @@ -415,6 +415,14 @@ esp_err_t esp_bt_mem_release(esp_bt_mode_t mode); */ 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 + * @param output : true for log dump, false will take no effect + */ +void esp_ble_controller_log_dump_all(bool output); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED + #ifdef __cplusplus } #endif diff --git a/components/esp_rom/esp32c2/ld/esp32c2.rom.ld b/components/esp_rom/esp32c2/ld/esp32c2.rom.ld index a7d6347479..0151bd937b 100644 --- a/components/esp_rom/esp32c2/ld/esp32c2.rom.ld +++ b/components/esp_rom/esp32c2/ld/esp32c2.rom.ld @@ -563,10 +563,6 @@ bt_rf_coex_dft_pti_get_default = 0x40000ab4; bt_rf_coex_hooks_p_set = 0x40000ab8; r__os_mbuf_copypkthdr = 0x40000abc; r_ble_controller_get_rom_compile_version = 0x40000ac4; -r_ble_hci_ram_hs_acl_tx = 0x40000ac8; -r_ble_hci_ram_hs_cmd_tx = 0x40000acc; -r_ble_hci_ram_ll_acl_tx = 0x40000ad0; -r_ble_hci_ram_ll_evt_tx = 0x40000ad4; r_ble_hci_ram_reset = 0x40000ad8; r_ble_hci_ram_set_acl_free_cb = 0x40000adc; r_ble_hci_trans_acl_buf_alloc = 0x40000ae0; @@ -579,10 +575,6 @@ r_ble_hci_uart_acl_tx = 0x40000b00; r_ble_hci_uart_cmdevt_tx = 0x40000b04; r_ble_hci_uart_config = 0x40000b08; r_ble_hci_uart_free_pkt = 0x40000b0c; -r_ble_hci_uart_hs_acl_tx = 0x40000b10; -r_ble_hci_uart_hs_cmd_tx = 0x40000b14; -r_ble_hci_uart_ll_acl_tx = 0x40000b18; -r_ble_hci_uart_ll_evt_tx = 0x40000b1c; r_ble_hci_uart_rx_acl = 0x40000b20; r_ble_hci_uart_rx_char = 0x40000b24; r_ble_hci_uart_rx_cmd = 0x40000b28; @@ -825,7 +817,6 @@ r_ble_ll_hci_cb_set_ctrlr_to_host_fc = 0x40000f9c; r_ble_ll_hci_cb_set_event_mask = 0x40000fa0; r_ble_ll_hci_cb_set_event_mask2 = 0x40000fa4; r_ble_ll_hci_chk_phy_masks = 0x40000fa8; -r_ble_ll_hci_cmd_proc = 0x40000fac; r_ble_ll_hci_cmd_rx = 0x40000fb0; r_ble_ll_hci_disconnect = 0x40000fbc; r_ble_ll_hci_ev_conn_update = 0x40000fc4; @@ -979,7 +970,6 @@ r_ble_ll_scan_reset = 0x40001258; r_ble_ll_scan_rx_pkt_in = 0x4000125c; r_ble_ll_scan_rx_pkt_in_restore_addr_data = 0x40001268; r_ble_ll_scan_rxed = 0x4000126c; -r_ble_ll_scan_send_adv_report = 0x40001270; r_ble_ll_scan_send_truncated = 0x40001274; r_ble_ll_scan_set_peer_rpa = 0x4000127c; r_ble_ll_scan_set_perfer_addr = 0x40001280; @@ -1116,7 +1106,6 @@ r_ble_lll_conn_coex_dpc_update_on_event_scheduled = 0x400014e4; r_ble_lll_conn_coex_dpc_update_on_event_started = 0x400014e8; r_ble_lll_conn_cth_flow_alloc_credit = 0x400014ec; r_ble_lll_conn_current_sm_over = 0x400014f4; -r_ble_lll_conn_event_end = 0x40001504; r_ble_lll_conn_event_end_timer_cb = 0x40001508; r_ble_lll_conn_event_is_over = 0x40001510; r_ble_lll_conn_event_start_cb = 0x40001514; From dc417eeab9d552f13da7c045b5582281074b509e Mon Sep 17 00:00:00 2001 From: zwl Date: Thu, 7 Sep 2023 16:38:03 +0800 Subject: [PATCH 2/5] feat(ble): enable adv report flow control on esp32c2 --- components/bt/controller/esp32c2/Kconfig.in | 32 +++++++++++++++++++ .../include/common/bluedroid_user_config.h | 4 +-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/components/bt/controller/esp32c2/Kconfig.in b/components/bt/controller/esp32c2/Kconfig.in index 70ebc00def..dac5534a58 100644 --- a/components/bt/controller/esp32c2/Kconfig.in +++ b/components/bt/controller/esp32c2/Kconfig.in @@ -392,3 +392,35 @@ 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. diff --git a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h index 05cb6fab27..7cb51f14e4 100644 --- a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h +++ b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h @@ -211,7 +211,7 @@ #endif //CONFIG_IDF_TARGET_ESP32 -#if (CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3) +#if (CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || 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 @@ -233,7 +233,7 @@ #define UC_BTDM_BLE_ADV_REPORT_DISCARD_THRSHOLD 20 #endif -#endif //(CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3) +#endif //(CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C2) //BT ACL CONNECTIONS #ifdef CONFIG_BT_ACL_CONNECTIONS From a29eaf058e0617ff85662263bc825d0c8bfc1957 Mon Sep 17 00:00:00 2001 From: cjin Date: Thu, 31 Aug 2023 10:38:00 +0800 Subject: [PATCH 3/5] feat: Support esp32c2 BLE power save example --- .../bluetooth/nimble/power_save/README.md | 14 +++++++++++++- .../nimble/power_save/main/Kconfig.projbuild | 15 +++++++++++++-- .../bluetooth/nimble/power_save/main/main.c | 2 ++ .../power_save/sdkconfig.defaults.esp32c2 | 19 +++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32c2 diff --git a/examples/bluetooth/nimble/power_save/README.md b/examples/bluetooth/nimble/power_save/README.md index 20c6d83c43..8b338c12d4 100644 --- a/examples/bluetooth/nimble/power_save/README.md +++ b/examples/bluetooth/nimble/power_save/README.md @@ -16,6 +16,7 @@ This example contains some build configurations. For each configuration, a few c - `sdkconfig.40m.esp32c3`: ESP32C3 uses main XTAL as low power clock in light sleep enabled. - `sdkconfig.defaults.esp32s3`: ESP32S3 uses 32kHz XTAL as low power clock in light sleep enabled. - `sdkconfig.40m.esp32s3`: ESP32S3 uses main XTAL as low power clock in light sleep enabled. +- `sdkconfig.defaults.esp32c2`: ESP32C2 uses main XTAL as low power clock in light sleep enabled. ## How to use example ### Hardware Required @@ -48,6 +49,15 @@ idf.py menuconfig 7. Enable power up main XTAL during light sleep: - `Component config > Bluetooth > Controller Options > MODEM SLEEP Options > [*] power up main XTAL during light sleep` +#### For Chip ESP32-C2 + +4. Enable bluetooth modem sleep: + - `Component config > Bluetooth > Controller Options` + - `[*] Enable BLE sleep` +5. Power down flash during light sleep: + - `Component config > Hardware Settings > Sleep Config` + - `[*] Power down flash in light sleep when there is no SPIRAM` + ### Build and Flash ``` @@ -107,8 +117,10 @@ I (463) NimBLE: | ESP32 | 231 mA | 14.1 mA | X | 1.9 mA | | ESP32C3 | 262 mA | 12 mA | 2.3 mA | 140 uA | | ESP32S3 | 240 mA | 17.9 mA | 3.3 mA | 230 uA | +| ESP32C2 | 130 mA | 18.0 mA | 2.5 mA | X | X: This feature is currently not supported. ## Example Breakdown -- ESP32 does not support the use of main XTAL in light sleep mode, so an external 32kHz crystal is required. \ No newline at end of file +- ESP32 does not support the use of main XTAL in light sleep mode, so an external 32kHz crystal is required. +- ESP32C2 does not support the use of 32KHz XTAL in light sleep mode, the XTAL frequency is set to 26MHz in default. \ No newline at end of file diff --git a/examples/bluetooth/nimble/power_save/main/Kconfig.projbuild b/examples/bluetooth/nimble/power_save/main/Kconfig.projbuild index 782afa509e..912236c456 100644 --- a/examples/bluetooth/nimble/power_save/main/Kconfig.projbuild +++ b/examples/bluetooth/nimble/power_save/main/Kconfig.projbuild @@ -2,7 +2,8 @@ menu "Example Configuration" choice EXAMPLE_MAX_CPU_FREQ prompt "Maximum CPU frequency" - default EXAMPLE_MAX_CPU_FREQ_160 + default EXAMPLE_MAX_CPU_FREQ_160 if !IDF_TARGET_ESP32C2 + default EXAMPLE_MAX_CPU_FREQ_120 if IDF_TARGET_ESP32C2 depends on PM_ENABLE help Maximum CPU frequency to use for dynamic frequency scaling. @@ -11,6 +12,9 @@ menu "Example Configuration" bool "80 MHz" config EXAMPLE_MAX_CPU_FREQ_160 bool "160 MHz" + config EXAMPLE_MAX_CPU_FREQ_120 + bool "120 MHz" + depends on IDF_TARGET_ESP32C2 config EXAMPLE_MAX_CPU_FREQ_240 bool "240 MHz" depends on IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3 @@ -19,12 +23,14 @@ menu "Example Configuration" config EXAMPLE_MAX_CPU_FREQ_MHZ int default 80 if EXAMPLE_MAX_CPU_FREQ_80 + default 120 if EXAMPLE_MAX_CPU_FREQ_120 default 160 if EXAMPLE_MAX_CPU_FREQ_160 default 240 if EXAMPLE_MAX_CPU_FREQ_240 choice EXAMPLE_MIN_CPU_FREQ prompt "Minimum CPU frequency" - default EXAMPLE_MIN_CPU_FREQ_40M + default EXAMPLE_MIN_CPU_FREQ_40M if !IDF_TARGET_ESP32C2 + default EXAMPLE_MIN_CPU_FREQ_26M if IDF_TARGET_ESP32C2 depends on PM_ENABLE help Minimum CPU frequency to use for dynamic frequency scaling. @@ -42,6 +48,10 @@ menu "Example Configuration" config EXAMPLE_MIN_CPU_FREQ_40M bool "40 MHz (use with 40MHz XTAL)" depends on XTAL_FREQ_40 || XTAL_FREQ_AUTO + config EXAMPLE_MIN_CPU_FREQ_26M + bool "26 MHz (use with 26MHz XTAL)" + depends on IDF_TARGET_ESP32C2 + depends on XTAL_FREQ_26 || XTAL_FREQ_AUTO config EXAMPLE_MIN_CPU_FREQ_20M bool "20 MHz (use with 40MHz XTAL)" depends on XTAL_FREQ_40 || XTAL_FREQ_AUTO @@ -54,6 +64,7 @@ menu "Example Configuration" int default 80 if EXAMPLE_MIN_CPU_FREQ_80M default 40 if EXAMPLE_MIN_CPU_FREQ_40M + default 26 if EXAMPLE_MIN_CPU_FREQ_26M default 20 if EXAMPLE_MIN_CPU_FREQ_20M default 10 if EXAMPLE_MIN_CPU_FREQ_10M diff --git a/examples/bluetooth/nimble/power_save/main/main.c b/examples/bluetooth/nimble/power_save/main/main.c index 93b9392250..605651fd06 100644 --- a/examples/bluetooth/nimble/power_save/main/main.c +++ b/examples/bluetooth/nimble/power_save/main/main.c @@ -524,6 +524,8 @@ app_main(void) esp_pm_config_esp32c3_t pm_config = { #elif CONFIG_IDF_TARGET_ESP32S3 esp_pm_config_esp32s3_t pm_config = { +#elif CONFIG_IDF_TARGET_ESP32C2 + esp_pm_config_esp32c2_t pm_config = { #endif .max_freq_mhz = CONFIG_EXAMPLE_MAX_CPU_FREQ_MHZ, .min_freq_mhz = CONFIG_EXAMPLE_MIN_CPU_FREQ_MHZ, diff --git a/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32c2 b/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32c2 new file mode 100644 index 0000000000..9f941613f7 --- /dev/null +++ b/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32c2 @@ -0,0 +1,19 @@ +CONFIG_IDF_TARGET="esp32h2" + +# Bluetooth Low Power Config +CONFIG_BT_LE_SLEEP_ENABLE=y + +# +# Power Management +# +CONFIG_PM_ENABLE=y +CONFIG_PM_DFS_INIT_AUTO=y + +# XTAL Freq Config +CONFIG_XTAL_FREQ_26=y +CONFIG_XTAL_FREQ=26 + +# +# Sleep Config +# +CONFIG_ESP_SLEEP_POWER_DOWN_FLASH=y From e741807b5a4c5fe0f4fa4f5a0714569d72f07759 Mon Sep 17 00:00:00 2001 From: cjin Date: Fri, 8 Sep 2023 15:50:28 +0800 Subject: [PATCH 4/5] ble: support esp32c2 wakeup overhead --- components/bt/controller/esp32c2/bt.c | 34 ++++++++++++++++++++++----- components/esp_pm/pm_impl.c | 2 +- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index f15f9ed87a..b709beaad5 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -46,6 +46,10 @@ #include "hci/hci_hal.h" #endif +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE +#include "esp_private/pm_impl.h" +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE + #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -140,6 +144,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, @@ -460,6 +467,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) { @@ -506,32 +520,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; diff --git a/components/esp_pm/pm_impl.c b/components/esp_pm/pm_impl.c index 5fb337558d..34b711dea8 100644 --- a/components/esp_pm/pm_impl.c +++ b/components/esp_pm/pm_impl.c @@ -874,7 +874,7 @@ void esp_pm_impl_waiti(void) #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE } -#define PERIPH_INFORM_OUT_LIGHT_SLEEP_OVERHEAD_NO 1 +#define PERIPH_INFORM_OUT_LIGHT_SLEEP_OVERHEAD_NO 2 /* Inform peripherals of light sleep wakeup overhead time */ static inform_out_light_sleep_overhead_cb_t s_periph_inform_out_light_sleep_overhead_cb[PERIPH_INFORM_OUT_LIGHT_SLEEP_OVERHEAD_NO]; From 79b277310222c298b8e18a53dce76cf149840401 Mon Sep 17 00:00:00 2001 From: luomanruo Date: Wed, 6 Sep 2023 21:51:45 +0800 Subject: [PATCH 5/5] change(ble): update controller log print interface --- components/bt/controller/esp32c2/bt.c | 31 ++++++++++++--------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index b709beaad5..d27f645e1c 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -386,23 +386,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) { @@ -1008,11 +991,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