@@ -72,10 +72,10 @@ static uint32_t g_retained_regs_val[ ARRAY_SIZE(retained_regs) ];
7272static uint32_t g_mcu_wait_for_swd_start ;
7373
7474/* Minimum time required to go to sleep (until switch to SLP) and then wake up */
75- static uint32_t g_mcu_wakeup_usecs_min ;
75+ static uint32_t g_mcu_sleep_time_us_min ;
7676
77- static bool
78- cmac_sleep_is_switch_allowed (void )
77+ static inline bool
78+ cmac_sleep_is_slp_allowed (void )
7979{
8080 return (ble_phy_xcvr_state_get () == 0 ) &&
8181 !os_arch_cmac_pending_irq () &&
@@ -98,6 +98,10 @@ sub27(uint32_t x, uint32_t y)
9898static bool
9999cmac_sleep_is_deep_sleep_allowed (void )
100100{
101+ if (g_mcu_sleep_time_us_min == 0 ) {
102+ return false;
103+ }
104+
101105 /*
102106 * We wait for SWD attach until high part of LL Timer increases by 2 which
103107 * is anywhere in 1-2ms range which is enough.
@@ -170,42 +174,31 @@ cmac_sleep_wait4xtal(void)
170174
171175#define T_USEC (_t ) (_t)
172176#define T_LPTICK (_t ) ((_t) * cmac_timer_slp_tick_us())
173- #define T_LPTICK_U (_t ) (T_LPTICK(_t) * 15 / 10)
174177
175- static void
176- cmac_sleep_calculate_wakeup_time ( void )
178+ void
179+ cmac_sleep_wakeup_time_update ( uint16_t wakeup_time_us )
177180{
178- assert (g_cmac_shm_ctrl .xtal32m_settle_us );
181+ if (wakeup_time_us == 0 ) {
182+ g_mcu_sleep_time_us_min = 0 ;
183+ return ;
184+ }
179185
180- g_mcu_wakeup_usecs_min =
186+ g_mcu_sleep_time_us_min =
181187 /*
182- * We need ~12us to prepare for sleep before starting switch to SLP.
188+ * We need ~15us to prepare for sleep before starting switch to SLP.
183189 * Switch to SLP is done by switching SLP clock to LPCLK first and then
184190 * enabling SLP. The former has to be synchronized with negative edge of
185191 * LPCLK and the latter happens on positive edge of LPCLK so we just
186192 * assume 2 LPCLK ticks in worst case.
187193 */
188- T_USEC (12 ) + T_LPTICK (2 ) +
194+ T_USEC (15 ) + T_LPTICK (2 ) +
189195 /*
190- * On wake up we assume fast wake up mode which has 3 phases that take
191- * up to 2, 2 and 3 LPCLK ticks respectively (need to add some margin
192- * here for worst-worst case). XTAL32M is started at 3rd phase and we
193- * need to wait for it to settle before switch back to LLT. This is done
194- * by disabling SLP and then switching SLP clock to PCLK. Both actions
195- * are synchronized with LPCLK negative edge so take 2 LPCLK ticks in
196- * worst case. Finally, LLP compensation takes around 50us.
196+ * After wakeup (this includes XTAL32M settling) we need to switch back
197+ * to LLT. This is done by disabling SLP and then switching SLP clock to
198+ * PCLK. Both actions are synchronized with LPCLK negative edge so take
199+ * 2 LPCLK ticks in worst case. Finally, LLT compensation takes ~50us.
197200 */
198- T_LPTICK_U (2 ) + T_LPTICK_U (2 ) +
199- max (T_LPTICK_U (3 ), T_USEC (g_cmac_shm_ctrl .xtal32m_settle_us )) +
200- T_LPTICK (2 ) + T_USEC (50 );
201- }
202-
203- void
204- cmac_sleep_recalculate (void )
205- {
206- if (cmac_timer_slp_update ()) {
207- cmac_sleep_calculate_wakeup_time ();
208- }
201+ T_USEC (wakeup_time_us ) + T_LPTICK (2 ) + T_USEC (50 );
209202}
210203
211204extern bool ble_rf_try_recalibrate (uint32_t idle_time_us );
@@ -234,7 +227,7 @@ cmac_sleep(void)
234227 * happen so need to make sure we can be up and running on time.
235228 */
236229
237- sleep_usecs = wakeup_at - cmac_timer_read32 () - g_mcu_wakeup_usecs_min ;
230+ sleep_usecs = wakeup_at - cmac_timer_read32 () - g_mcu_sleep_time_us_min ;
238231 if ((int32_t )sleep_usecs <= 0 ) {
239232 switch_to_slp = false;
240233 deep_sleep = false;
@@ -245,14 +238,15 @@ cmac_sleep(void)
245238 goto skip_sleep ;
246239 }
247240
248- sleep_lp_ticks = cmac_timer_usecs_to_lp_ticks (sleep_usecs );
249- if (sleep_lp_ticks <= 1 ) {
241+ /* XXX off by one? */
242+ sleep_lp_ticks = cmac_timer_usecs_to_lp_ticks (sleep_usecs ) - 1 ;
243+ if ((int32_t )sleep_lp_ticks <= 1 ) {
250244 switch_to_slp = false;
251245 deep_sleep = false;
252246 goto do_sleep ;
253247 }
254248
255- if (!cmac_sleep_is_switch_allowed ()) {
249+ if (!cmac_sleep_is_slp_allowed ()) {
256250 switch_to_slp = false;
257251 deep_sleep = false;
258252 } else if (!cmac_sleep_is_deep_sleep_allowed ()) {
0 commit comments