8585#endif
8686#include "esp_intr_alloc.h"
8787#include "esp_ble_adapter.h"
88+ #ifdef CONFIG_PM
89+ # include "include/esp_pm.h"
90+ # include "esp_sleep.h"
91+ #endif
8892
8993/****************************************************************************
9094 * Pre-processor Definitions
9195 ****************************************************************************/
9296
9397#define BTDM_MIN_TIMER_UNCERTAINTY_US (1800)
9498
99+ #define BTDM_RTC_SLOW_CLK_RC_DRIFT_PERCENT (7)
100+
95101/* Sleep and wakeup interval control */
96102
97103#define BTDM_MIN_SLEEP_DURATION (24) /* Threshold of interval in half slots to allow to fall into sleep mode */
@@ -603,13 +609,14 @@ static DRAM_ATTR void * g_wakeup_req_sem = NULL;
603609
604610/* wakeup timer */
605611
606- static DRAM_ATTR esp_timer_handle_t g_btdm_slp_tmr ;
612+ static DRAM_ATTR esp_timer_handle_t g_btdm_slp_tmr = NULL ;
607613
608614#ifdef CONFIG_PM
615+ static DRAM_ATTR esp_pm_lock_handle_t g_pm_lock ;
609616
610617/* pm_lock to prevent light sleep due to incompatibility currently */
611618
612- static DRAM_ATTR void * g_light_sleep_pm_lock ;
619+ static DRAM_ATTR esp_pm_lock_handle_t g_light_sleep_pm_lock ;
613620#endif
614621
615622/* BT interrupt private data */
@@ -1677,16 +1684,26 @@ static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles)
16771684 DEBUGASSERT (us_to_sleep > BTDM_MIN_TIMER_UNCERTAINTY_US );
16781685 uncertainty = (us_to_sleep >> 11 );
16791686
1687+ #ifdef CONFIG_BT_CTRL_MAIN_XTAL_PU_DURING_LIGHT_SLEEP
1688+ /* Recalculate clock drift when Bluetooth keeps
1689+ * main XTAL on during light sleep
1690+ */
1691+
1692+ if (rtc_clk_slow_freq_get () == SOC_RTC_SLOW_CLK_SRC_RC_SLOW )
1693+ {
1694+ uncertainty = us_to_sleep * BTDM_RTC_SLOW_CLK_RC_DRIFT_PERCENT / 100 ;
1695+ }
1696+ #endif
1697+
16801698 if (uncertainty < BTDM_MIN_TIMER_UNCERTAINTY_US )
16811699 {
16821700 uncertainty = BTDM_MIN_TIMER_UNCERTAINTY_US ;
16831701 }
16841702
16851703 DEBUGASSERT (g_lp_stat .wakeup_timer_started == 0 );
16861704
1687- ets_timer_arm_us ((void * )& g_btdm_slp_tmr ,
1688- us_to_sleep - uncertainty ,
1689- false);
1705+ esp_timer_start_once (g_btdm_slp_tmr ,
1706+ us_to_sleep - uncertainty );
16901707 g_lp_stat .wakeup_timer_started = true;
16911708}
16921709
@@ -1721,6 +1738,7 @@ static void btdm_sleep_enter_phase2_wrapper(void)
17211738#ifdef CONFIG_PM
17221739 if (g_lp_stat .pm_lock_released == 0 )
17231740 {
1741+ esp_pm_lock_release (g_pm_lock );
17241742 g_lp_stat .pm_lock_released = 1 ;
17251743 }
17261744#endif
@@ -1746,6 +1764,7 @@ static void btdm_sleep_exit_phase3_wrapper(void)
17461764#ifdef CONFIG_PM
17471765 if (g_lp_stat .pm_lock_released )
17481766 {
1767+ esp_pm_lock_acquire (g_pm_lock );
17491768 g_lp_stat .pm_lock_released = 0 ;
17501769 }
17511770#endif
@@ -1761,7 +1780,7 @@ static void btdm_sleep_exit_phase3_wrapper(void)
17611780
17621781 if (g_lp_cntl .wakeup_timer_required && g_lp_stat .wakeup_timer_started )
17631782 {
1764- ets_timer_disarm (( void * ) & g_btdm_slp_tmr );
1783+ esp_timer_stop ( g_btdm_slp_tmr );
17651784 g_lp_stat .wakeup_timer_started = 0 ;
17661785 }
17671786
@@ -2211,8 +2230,8 @@ static void esp_update_time(struct timespec *timespec, uint32_t ticks)
22112230static void IRAM_ATTR btdm_slp_tmr_callback (void * arg )
22122231{
22132232#ifdef CONFIG_PM
2214- btdm_vnd_offload_post (BTDM_VND_OL_SIG_WAKEUP_TMR ,
2215- (void * )BTDM_ASYNC_WAKEUP_SRC_TMR );
2233+ r_btdm_vnd_offload_post (BTDM_VND_OL_SIG_WAKEUP_TMR ,
2234+ (void * )BTDM_ASYNC_WAKEUP_SRC_TMR );
22162235#endif
22172236}
22182237
@@ -2265,6 +2284,7 @@ static void IRAM_ATTR btdm_sleep_exit_phase0(void *param)
22652284#ifdef CONFIG_PM
22662285 if (g_lp_stat .pm_lock_released )
22672286 {
2287+ esp_pm_lock_acquire (g_pm_lock );
22682288 g_lp_stat .pm_lock_released = 0 ;
22692289 }
22702290#endif
@@ -2279,7 +2299,7 @@ static void IRAM_ATTR btdm_sleep_exit_phase0(void *param)
22792299
22802300 if (g_lp_cntl .wakeup_timer_required && g_lp_stat .wakeup_timer_started )
22812301 {
2282- ets_timer_disarm (( void * ) & g_btdm_slp_tmr );
2302+ esp_timer_stop ( g_btdm_slp_tmr );
22832303 g_lp_stat .wakeup_timer_started = 0 ;
22842304 }
22852305
@@ -2406,9 +2426,7 @@ static esp_err_t btdm_low_power_mode_init(esp_bt_controller_config_t *cfg)
24062426 .name = "btSlp" ,
24072427 };
24082428
2409- ets_timer_setfn ((void * )& g_btdm_slp_tmr ,
2410- (void * )btdm_slp_tmr_callback ,
2411- & create_args );
2429+ esp_timer_create (& create_args , & g_btdm_slp_tmr );
24122430 }
24132431
24142432 /* set default bluetooth sleep clock cycle and its
@@ -2470,7 +2488,7 @@ static esp_err_t btdm_low_power_mode_init(esp_bt_controller_config_t *cfg)
24702488 if (g_lp_cntl .lpclk_sel == ESP_BT_SLEEP_CLOCK_MAIN_XTAL )
24712489 {
24722490#ifdef CONFIG_BT_CTRL_MAIN_XTAL_PU_DURING_LIGHT_SLEEP
2473- ASSERT ( esp_sleep_pd_config (ESP_PD_DOMAIN_XTAL , ESP_PD_OPTION_ON ) );
2491+ esp_sleep_pd_config (ESP_PD_DOMAIN_XTAL , ESP_PD_OPTION_ON );
24742492 g_lp_cntl .main_xtal_pu = 1 ;
24752493#endif
24762494 select_src_ret = btdm_lpclk_select_src (BTDM_LPCLK_SEL_XTAL );
@@ -2511,7 +2529,28 @@ static esp_err_t btdm_low_power_mode_init(esp_bt_controller_config_t *cfg)
25112529#endif
25122530
25132531#ifdef CONFIG_PM
2514- g_lp_stat .pm_lock_released = 1 ;
2532+ if (g_lp_cntl .no_light_sleep )
2533+ {
2534+ err = esp_pm_lock_create (ESP_PM_NO_LIGHT_SLEEP , 0 ,
2535+ "ble" , & g_light_sleep_pm_lock );
2536+ if (err != OK )
2537+ {
2538+ break ;
2539+ }
2540+
2541+ wlwarn ("Light sleep mode will not be able to"
2542+ "apply when bluetooth is enabled." );
2543+ }
2544+
2545+ err = esp_pm_lock_create (ESP_PM_APB_FREQ_MAX , 0 , "ble" , & g_pm_lock );
2546+ if (err != OK )
2547+ {
2548+ break ;
2549+ }
2550+ else
2551+ {
2552+ g_lp_stat .pm_lock_released = 1 ;
2553+ }
25152554#endif
25162555 }
25172556 while (0 );
@@ -2607,20 +2646,28 @@ static void btdm_low_power_mode_deinit(void)
26072646 {
26082647 if (g_light_sleep_pm_lock != NULL )
26092648 {
2649+ esp_pm_lock_delete (g_light_sleep_pm_lock );
26102650 g_light_sleep_pm_lock = NULL ;
26112651 }
26122652 }
2653+
2654+ if (g_pm_lock != NULL )
2655+ {
2656+ esp_pm_lock_delete (g_pm_lock );
2657+ g_pm_lock = NULL ;
2658+ g_lp_stat .pm_lock_released = 0 ;
2659+ }
26132660#endif
26142661
26152662 if (g_lp_cntl .wakeup_timer_required && g_btdm_slp_tmr != NULL )
26162663 {
26172664 if (g_lp_stat .wakeup_timer_started )
26182665 {
2619- ets_timer_disarm (( void * ) & g_btdm_slp_tmr );
2666+ esp_timer_stop ( g_btdm_slp_tmr );
26202667 }
26212668
26222669 g_lp_stat .wakeup_timer_started = 0 ;
2623- ets_timer_done (( void * ) & g_btdm_slp_tmr );
2670+ esp_timer_delete ( g_btdm_slp_tmr );
26242671 g_btdm_slp_tmr = NULL ;
26252672 }
26262673
@@ -2639,7 +2686,7 @@ static void btdm_low_power_mode_deinit(void)
26392686#ifdef CONFIG_BT_CTRL_MAIN_XTAL_PU_DURING_LIGHT_SLEEP
26402687 if (g_lp_cntl .main_xtal_pu )
26412688 {
2642- ASSERT ( esp_sleep_pd_config (ESP_PD_DOMAIN_XTAL , ESP_PD_OPTION_OFF ) );
2689+ esp_sleep_pd_config (ESP_PD_DOMAIN_XTAL , ESP_PD_OPTION_OFF );
26432690 g_lp_cntl .main_xtal_pu = 0 ;
26442691 }
26452692#endif
@@ -2695,9 +2742,10 @@ static bool async_wakeup_request(int event)
26952742 if (!btdm_power_state_active ())
26962743 {
26972744 do_wakeup_request = true;
2698- #if CONFIG_PM
2745+ #ifdef CONFIG_PM
26992746 if (g_lp_stat .pm_lock_released )
27002747 {
2748+ esp_pm_lock_acquire (g_pm_lock );
27012749 g_lp_stat .pm_lock_released = 0 ;
27022750 }
27032751#endif
@@ -2707,7 +2755,7 @@ static bool async_wakeup_request(int event)
27072755 if (g_lp_cntl .wakeup_timer_required &&
27082756 g_lp_stat .wakeup_timer_started )
27092757 {
2710- ets_timer_disarm (( void * ) & g_btdm_slp_tmr );
2758+ esp_timer_stop ( g_btdm_slp_tmr );
27112759 g_lp_stat .wakeup_timer_started = 0 ;
27122760 }
27132761 }
@@ -3194,26 +3242,32 @@ int esp_bt_controller_enable(esp_bt_mode_t mode)
31943242#ifdef CONFIG_PM
31953243 /* enable low power mode */
31963244
3245+ if (g_lp_cntl .no_light_sleep )
3246+ {
3247+ esp_pm_lock_acquire (g_light_sleep_pm_lock );
3248+ }
3249+
3250+ esp_pm_lock_acquire (g_pm_lock );
31973251 g_lp_stat .pm_lock_released = 0 ;
31983252#endif
31993253
32003254#if CONFIG_MAC_BB_PD
32013255 if (esp_register_mac_bb_pd_callback (btdm_mac_bb_power_down_cb ) != 0 )
32023256 {
3203- err = - EINVAL ;
3257+ ret = - EINVAL ;
32043258 goto error ;
32053259 }
32063260
32073261 if (esp_register_mac_bb_pu_callback (btdm_mac_bb_power_up_cb ) != 0 )
32083262 {
3209- err = - EINVAL ;
3263+ ret = - EINVAL ;
32103264 goto error ;
32113265 }
32123266#endif
32133267
32143268 if (g_lp_cntl .enable )
32153269 {
3216- btdm_controller_enable_sleep (true);
3270+ btdm_controller_enable_sleep (true);
32173271 }
32183272
32193273 /* Disable pll track by default in BLE controller on ESP32-C3 and
@@ -3246,8 +3300,14 @@ int esp_bt_controller_enable(esp_bt_mode_t mode)
32463300 btdm_controller_enable_sleep (false);
32473301
32483302#ifdef CONFIG_PM
3303+ if (g_lp_cntl .no_light_sleep )
3304+ {
3305+ esp_pm_lock_release (g_light_sleep_pm_lock );
3306+ }
3307+
32493308 if (g_lp_stat .pm_lock_released == 0 )
32503309 {
3310+ esp_pm_lock_release (g_pm_lock );
32513311 g_lp_stat .pm_lock_released = 1 ;
32523312 }
32533313#endif
@@ -3316,8 +3376,14 @@ int esp_bt_controller_disable(void)
33163376#endif
33173377
33183378#ifdef CONFIG_PM
3379+ if (g_lp_cntl .no_light_sleep )
3380+ {
3381+ esp_pm_lock_release (g_light_sleep_pm_lock );
3382+ }
3383+
33193384 if (g_lp_stat .pm_lock_released == 0 )
33203385 {
3386+ esp_pm_lock_release (g_pm_lock );
33213387 g_lp_stat .pm_lock_released = 1 ;
33223388 }
33233389 else
0 commit comments