@@ -72,9 +72,9 @@ 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_lp_ticks_min ;
7676
77- static bool
77+ static inline bool
7878cmac_sleep_is_switch_allowed (void )
7979{
8080 return (ble_phy_xcvr_state_get () == 0 ) &&
@@ -168,44 +168,34 @@ cmac_sleep_wait4xtal(void)
168168 * (volatile uint32_t * )0x5000001c = 1 ;
169169}
170170
171- #define T_USEC (_t ) (_t)
172- #define T_LPTICK ( _t ) ((_t) * cmac_timer_slp_tick_us())
173- #define T_LPTICK_U (_t ) (T_LPTICK(_t) * 15 / 10 )
171+ #define T_USEC (_t ) ((( _t) + cmac_timer_slp_tick_us() - 1) / \
172+ cmac_timer_slp_tick_us())
173+ #define T_LPTICK (_t ) (_t )
174174
175- static void
176- cmac_sleep_calculate_wakeup_time ( void )
175+ void
176+ cmac_sleep_wakeup_time_update ( uint16_t wakeup_lpclk_ticks )
177177{
178- assert (g_cmac_shm_ctrl .xtal32m_settle_us );
178+ if (wakeup_lpclk_ticks == 0 ) {
179+ g_mcu_sleep_lp_ticks_min = 0 ;
180+ return ;
181+ }
179182
180- g_mcu_wakeup_usecs_min =
183+ g_mcu_sleep_lp_ticks_min =
181184 /*
182- * We need ~12us to prepare for sleep before starting switch to SLP.
185+ * We need ~15us to prepare for sleep before starting switch to SLP.
183186 * Switch to SLP is done by switching SLP clock to LPCLK first and then
184187 * enabling SLP. The former has to be synchronized with negative edge of
185188 * LPCLK and the latter happens on positive edge of LPCLK so we just
186189 * assume 2 LPCLK ticks in worst case.
187190 */
188- T_USEC (12 ) + T_LPTICK (2 ) +
191+ T_USEC (15 ) + T_LPTICK (2 ) +
189192 /*
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.
193+ * After wakeup (this includes XTAL32M settling) we need to switch back
194+ * to LLT. This is done by disabling SLP and then switching SLP clock to
195+ * PCLK. Both actions are synchronized with LPCLK negative edge so take
196+ * 2 LPCLK ticks in worst case. Finally, LLT compensation takes ~50us.
197197 */
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- }
198+ T_LPTICK (wakeup_lpclk_ticks ) + T_LPTICK (2 ) + T_USEC (50 );
209199}
210200
211201extern bool ble_rf_try_recalibrate (uint32_t idle_time_us );
@@ -228,25 +218,21 @@ cmac_sleep(void)
228218 cmac_pdc_ack_all ();
229219
230220 wakeup_at = cmac_timer_next_at ();
221+ sleep_usecs = wakeup_at - cmac_timer_read32 ();
231222
232- /*
233- * At this point in time we know exactly when next LLT interrupt should
234- * happen so need to make sure we can be up and running on time.
235- */
223+ if (ble_rf_try_recalibrate (sleep_usecs )) {
224+ goto skip_sleep ;
225+ }
236226
237- sleep_usecs = wakeup_at - cmac_timer_read32 () - g_mcu_wakeup_usecs_min ;
238- if ((int32_t )sleep_usecs <= 0 ) {
227+ if (g_mcu_sleep_lp_ticks_min == 0 ) {
239228 switch_to_slp = false;
240229 deep_sleep = false;
241230 goto do_sleep ;
242231 }
243232
244- if (ble_rf_try_recalibrate (sleep_usecs )) {
245- goto skip_sleep ;
246- }
247-
248- sleep_lp_ticks = cmac_timer_usecs_to_lp_ticks (sleep_usecs );
249- if (sleep_lp_ticks <= 1 ) {
233+ sleep_lp_ticks = cmac_timer_usecs_to_lp_ticks (sleep_usecs ) -
234+ g_mcu_sleep_lp_ticks_min ;
235+ if ((int32_t )sleep_lp_ticks <= 1 ) {
250236 switch_to_slp = false;
251237 deep_sleep = false;
252238 goto do_sleep ;
0 commit comments