From 2a7e9fc4852a2cf1c174a173bc3443c7379cf2d3 Mon Sep 17 00:00:00 2001 From: xiongweichao Date: Thu, 11 Jan 2024 14:39:25 +0800 Subject: [PATCH 1/2] fix(ble): Fixed Bluetooth not waking up due to clock drift --- components/bt/controller/esp32c3/bt.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/components/bt/controller/esp32c3/bt.c b/components/bt/controller/esp32c3/bt.c index a53df45e6d8..43f2270864c 100644 --- a/components/bt/controller/esp32c3/bt.c +++ b/components/bt/controller/esp32c3/bt.c @@ -763,19 +763,26 @@ static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles) return; } - // start a timer to wake up and acquire the pm_lock before modem_sleep awakes uint32_t us_to_sleep = btdm_lpcycles_2_hus(lpcycles, NULL) >> 1; #define BTDM_MIN_TIMER_UNCERTAINTY_US (1800) +#define BTDM_RTC_SLOW_CLK_RC_DRIFT (7 / 100) assert(us_to_sleep > BTDM_MIN_TIMER_UNCERTAINTY_US); // allow a maximum time uncertainty to be about 488ppm(1/2048) at least as clock drift // and set the timer in advance uint32_t uncertainty = (us_to_sleep >> 11); +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE + if (rtc_clk_slow_freq_get() == RTC_SLOW_FREQ_RTC) { + uncertainty = us_to_sleep * BTDM_RTC_SLOW_CLK_RC_DRIFT; + } +#endif + if (uncertainty < BTDM_MIN_TIMER_UNCERTAINTY_US) { uncertainty = BTDM_MIN_TIMER_UNCERTAINTY_US; } assert (s_lp_stat.wakeup_timer_started == 0); + // start a timer to wake up and acquire the pm_lock before modem_sleep awakes if (esp_timer_start_once(s_btdm_slp_tmr, us_to_sleep - uncertainty) == ESP_OK) { s_lp_stat.wakeup_timer_started = 1; } else { @@ -794,12 +801,12 @@ static void btdm_sleep_enter_phase2_wrapper(void) assert(0); } - if (s_lp_stat.pm_lock_released == 0) { #ifdef CONFIG_PM_ENABLE + if (s_lp_stat.pm_lock_released == 0) { esp_pm_lock_release(s_pm_lock); -#endif s_lp_stat.pm_lock_released = 1; } +#endif } } From 8e41d56dbed76f2606e0e8be3b11849c35f44b1f Mon Sep 17 00:00:00 2001 From: xiongweichao Date: Mon, 29 Jan 2024 11:29:01 +0800 Subject: [PATCH 2/2] fix(ble): Fixed macro definition error for 136K clock drift --- components/bt/controller/esp32c3/bt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/bt/controller/esp32c3/bt.c b/components/bt/controller/esp32c3/bt.c index 43f2270864c..7ac77f3ea10 100644 --- a/components/bt/controller/esp32c3/bt.c +++ b/components/bt/controller/esp32c3/bt.c @@ -766,14 +766,14 @@ static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles) uint32_t us_to_sleep = btdm_lpcycles_2_hus(lpcycles, NULL) >> 1; #define BTDM_MIN_TIMER_UNCERTAINTY_US (1800) -#define BTDM_RTC_SLOW_CLK_RC_DRIFT (7 / 100) +#define BTDM_RTC_SLOW_CLK_RC_DRIFT_PERCENT 7 assert(us_to_sleep > BTDM_MIN_TIMER_UNCERTAINTY_US); // allow a maximum time uncertainty to be about 488ppm(1/2048) at least as clock drift // and set the timer in advance uint32_t uncertainty = (us_to_sleep >> 11); #if CONFIG_FREERTOS_USE_TICKLESS_IDLE if (rtc_clk_slow_freq_get() == RTC_SLOW_FREQ_RTC) { - uncertainty = us_to_sleep * BTDM_RTC_SLOW_CLK_RC_DRIFT; + uncertainty = us_to_sleep * BTDM_RTC_SLOW_CLK_RC_DRIFT_PERCENT / 100; } #endif