diff --git a/components/bt/common/Kconfig.in b/components/bt/common/Kconfig.in index c4ce35ef90..2be8aca8bb 100644 --- a/components/bt/common/Kconfig.in +++ b/components/bt/common/Kconfig.in @@ -8,7 +8,6 @@ config BT_ALARM_MAX_NUM config BT_BLE_LOG_SPI_OUT_ENABLED bool "Output ble logs to SPI bus (Experimental)" default n - select SPI_MASTER_IN_IRAM help Output ble logs to SPI bus diff --git a/components/bt/common/ble_log/ble_log_spi_out.c b/components/bt/common/ble_log/ble_log_spi_out.c index cdf498dc9e..0e51430b54 100644 --- a/components/bt/common/ble_log/ble_log_spi_out.c +++ b/components/bt/common/ble_log/ble_log_spi_out.c @@ -140,14 +140,16 @@ static int spi_out_init_trans(spi_out_trans_cb_t **trans_cb, uint16_t buf_size) if (!(*trans_cb)) { return -1; } + memset(*trans_cb, 0, sizeof(spi_out_trans_cb_t)); + uint8_t *buf = (uint8_t *)heap_caps_malloc(buf_size, MALLOC_CAP_DMA); if (!buf) { free(*trans_cb); + *trans_cb = NULL; return -1; } // Initialization - memset(*trans_cb, 0, sizeof(spi_out_trans_cb_t)); (*trans_cb)->buf_size = buf_size; (*trans_cb)->trans.tx_buffer = buf; return 0; @@ -158,8 +160,13 @@ static void spi_out_deinit_trans(spi_out_trans_cb_t **trans_cb) if (!(*trans_cb)) { return; } + if ((*trans_cb)->trans.tx_buffer) { + // Do not free buffer until recycled + while ((*trans_cb)->flag == SPI_OUT_TRANS_CB_FLAG_IN_QUEUE) {} + free((uint8_t *)(*trans_cb)->trans.tx_buffer); + (*trans_cb)->trans.tx_buffer = NULL; + } - free((uint8_t *)(*trans_cb)->trans.tx_buffer); free(*trans_cb); *trans_cb = NULL; return; @@ -174,7 +181,7 @@ IRAM_ATTR static void spi_out_tx_done_cb(spi_transaction_t *ret_trans) IRAM_ATTR static inline int spi_out_append_trans(spi_out_trans_cb_t *trans_cb) { - if (trans_cb->flag != SPI_OUT_TRANS_CB_FLAG_NEED_QUEUE) { + if (trans_cb->flag != SPI_OUT_TRANS_CB_FLAG_NEED_QUEUE || !trans_cb->length) { return -1; } @@ -185,6 +192,8 @@ IRAM_ATTR static inline int spi_out_append_trans(spi_out_trans_cb_t *trans_cb) trans_cb->flag = SPI_OUT_TRANS_CB_FLAG_IN_QUEUE; return 0; } else { + trans_cb->length = 0; + trans_cb->flag = SPI_OUT_TRANS_CB_FLAG_AVAILABLE; return -1; } } @@ -224,6 +233,7 @@ static void spi_out_log_cb_deinit(spi_out_log_cb_t **log_cb) } } free(*log_cb); + *log_cb = NULL; return; } @@ -578,7 +588,7 @@ static void spi_out_ts_sync_deinit(void) } // CRITICAL: This function is called in ESP Timer task -IRAM_ATTR static void esp_timer_cb_ts_sync(void) +static void esp_timer_cb_ts_sync(void) { // Initialize variables uint32_t lc_ts = 0; @@ -627,7 +637,9 @@ int ble_log_spi_out_init(void) .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = SPI_OUT_MAX_TRANSFER_SIZE, +#if CONFIG_SPI_MASTER_ISR_IN_IRAM .intr_flags = ESP_INTR_FLAG_IRAM +#endif // CONFIG_SPI_MASTER_ISR_IN_IRAM }; spi_device_interface_config_t dev_config = { .clock_speed_hz = SPI_MASTER_FREQ_20M, @@ -781,7 +793,7 @@ IRAM_ATTR void ble_log_spi_out_ll_log_ev_proc(void) } #endif // CONFIG_BT_BLE_LOG_SPI_OUT_LL_ENABLED -IRAM_ATTR int ble_log_spi_out_write(uint8_t source, const uint8_t *addr, uint16_t len) +int ble_log_spi_out_write(uint8_t source, const uint8_t *addr, uint16_t len) { if (!ul_log_inited) { return -1; @@ -797,7 +809,7 @@ IRAM_ATTR int ble_log_spi_out_write(uint8_t source, const uint8_t *addr, uint16_ return ret; } -IRAM_ATTR int ble_log_spi_out_printf(uint8_t source, const char *format, ...) +int ble_log_spi_out_printf(uint8_t source, const char *format, ...) { if (!ul_log_inited) { return -1; @@ -844,7 +856,7 @@ IRAM_ATTR int ble_log_spi_out_printf(uint8_t source, const char *format, ...) return ret; } -IRAM_ATTR int ble_log_spi_out_printf_enh(uint8_t source, uint8_t level, const char *tag, const char *format, ...) +int ble_log_spi_out_printf_enh(uint8_t source, uint8_t level, const char *tag, const char *format, ...) { if (!ul_log_inited) { return -1; @@ -896,7 +908,7 @@ IRAM_ATTR int ble_log_spi_out_printf_enh(uint8_t source, uint8_t level, const ch return ret; } -IRAM_ATTR int ble_log_spi_out_write_with_ts(uint8_t source, const uint8_t *addr, uint16_t len) +int ble_log_spi_out_write_with_ts(uint8_t source, const uint8_t *addr, uint16_t len) { if (!ul_log_inited) { return -1; diff --git a/components/bt/controller/esp32c2/Kconfig.in b/components/bt/controller/esp32c2/Kconfig.in index 14f0824fb3..10e6f6edf1 100644 --- a/components/bt/controller/esp32c2/Kconfig.in +++ b/components/bt/controller/esp32c2/Kconfig.in @@ -681,6 +681,11 @@ config BT_LE_PLACE_CONN_RELATED_INTO_IRAM depends on BT_CTRL_RUN_IN_FLASH_ONLY default n +config BT_LE_PLACE_SLEEP_RELATED_INTO_IRAM + bool + depends on BT_CTRL_RUN_IN_FLASH_ONLY && BT_LE_SLEEP_ENABLE + default y + config BT_LE_CTRL_CHECK_CONNECT_IND_ACCESS_ADDRESS bool "Enable enhanced Access Address check in CONNECT_IND" default n diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index a62e529a59..75aa35e299 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -590,13 +590,6 @@ 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 */ - ble_rtc_slow_clk_src_t esp_bt_get_lpclk_src(void) { return s_bt_lpclk_src; @@ -611,7 +604,7 @@ void esp_bt_set_lpclk_src(ble_rtc_slow_clk_src_t clk_src) s_bt_lpclk_src = clk_src; } -IRAM_ATTR void controller_sleep_cb(uint32_t enable_tick, void *arg) +void controller_sleep_cb(uint32_t enable_tick, void *arg) { if (!s_ble_active) { return; @@ -625,7 +618,7 @@ IRAM_ATTR void controller_sleep_cb(uint32_t enable_tick, void *arg) s_ble_active = false; } -IRAM_ATTR void controller_wakeup_cb(void *arg) +void controller_wakeup_cb(void *arg) { if (s_ble_active) { return; @@ -665,7 +658,7 @@ esp_err_t controller_sleep_init(ble_rtc_slow_clk_src_t slow_clk_src) 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); + rc = esp_pm_register_inform_out_light_sleep_overhead_callback(esp_ble_set_wakeup_overhead); if (rc != ESP_OK) { goto error; } @@ -675,7 +668,7 @@ esp_err_t controller_sleep_init(ble_rtc_slow_clk_src_t slow_clk_src) 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); + esp_pm_unregister_inform_out_light_sleep_overhead_callback(esp_ble_set_wakeup_overhead); #endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */ /*lock should release first and then delete*/ if (s_pm_lock != NULL) { @@ -692,7 +685,7 @@ void controller_sleep_deinit(void) 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); + esp_pm_unregister_inform_out_light_sleep_overhead_callback(esp_ble_set_wakeup_overhead); #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE #ifdef CONFIG_PM_ENABLE /*lock should release first and then delete*/ diff --git a/components/bt/controller/lib_esp32c2/esp32c2-bt-lib b/components/bt/controller/lib_esp32c2/esp32c2-bt-lib index c0d98a9a03..a69fafcc0a 160000 --- a/components/bt/controller/lib_esp32c2/esp32c2-bt-lib +++ b/components/bt/controller/lib_esp32c2/esp32c2-bt-lib @@ -1 +1 @@ -Subproject commit c0d98a9a03c266828a25bed38c0174ecfc882aea +Subproject commit a69fafcc0a002a63f757bd4741a5d844d5e8dbf2 diff --git a/components/bt/linker_esp32c2.lf b/components/bt/linker_esp32c2.lf index 75f0d59680..c9ab4c2e49 100644 --- a/components/bt/linker_esp32c2.lf +++ b/components/bt/linker_esp32c2.lf @@ -10,6 +10,10 @@ entries: entries: .conn_iram1+ +[sections:bt_sleep_iram_text] +entries: + .sleep_iram1+ + [sections:bt_bss] entries: .bss+ @@ -39,6 +43,10 @@ entries: else: bt_conn_iram_text -> flash_text bt_isr_iram_text -> flash_text + if BT_LE_PLACE_SLEEP_RELATED_INTO_IRAM = y: + bt_sleep_iram_text -> iram0_bt_text + else: + bt_sleep_iram_text -> flash_text else: bt_iram_text -> iram0_bt_text bt_bss -> dram0_bt_bss @@ -47,6 +55,7 @@ entries: bt_conn_iram_text -> iram0_bt_text bt_isr_iram_text -> iram0_bt_text + bt_sleep_iram_text -> iram0_bt_text # For the following fragments, order matters for # 'ALIGN(4) ALIGN(4, post) SURROUND(sym)', which generates: 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 715862b3d9..572043bbaa 100644 --- a/components/bt/porting/npl/freertos/src/npl_os_freertos.c +++ b/components/bt/porting/npl/freertos/src/npl_os_freertos.c @@ -110,7 +110,9 @@ IRAM_ATTR npl_freertos_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn void IRAM_ATTR npl_freertos_event_deinit(struct ble_npl_event *ev) { - BLE_LL_ASSERT(ev->event); + if (!ev->event) { + return; + } #if OS_MEM_ALLOC os_memblock_put(&ble_freertos_ev_pool,ev->event); #else