@@ -72,10 +72,10 @@ static uint32_t g_retained_regs_val[ ARRAY_SIZE(retained_regs) ];
72
72
static uint32_t g_mcu_wait_for_swd_start ;
73
73
74
74
/* 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 ;
76
76
77
- static bool
78
- cmac_sleep_is_switch_allowed (void )
77
+ static inline bool
78
+ cmac_sleep_is_slp_allowed (void )
79
79
{
80
80
return (ble_phy_xcvr_state_get () == 0 ) &&
81
81
!os_arch_cmac_pending_irq () &&
@@ -98,6 +98,10 @@ sub27(uint32_t x, uint32_t y)
98
98
static bool
99
99
cmac_sleep_is_deep_sleep_allowed (void )
100
100
{
101
+ if (g_mcu_sleep_time_us_min == 0 ) {
102
+ return false;
103
+ }
104
+
101
105
/*
102
106
* We wait for SWD attach until high part of LL Timer increases by 2 which
103
107
* is anywhere in 1-2ms range which is enough.
@@ -170,42 +174,31 @@ cmac_sleep_wait4xtal(void)
170
174
171
175
#define T_USEC (_t ) (_t)
172
176
#define T_LPTICK (_t ) ((_t) * cmac_timer_slp_tick_us())
173
- #define T_LPTICK_U (_t ) (T_LPTICK(_t) * 15 / 10)
174
177
175
- static void
176
- cmac_sleep_calculate_wakeup_time ( void )
178
+ void
179
+ cmac_sleep_wakeup_time_update ( uint16_t wakeup_time_us )
177
180
{
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
+ }
179
185
180
- g_mcu_wakeup_usecs_min =
186
+ g_mcu_sleep_time_us_min =
181
187
/*
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.
183
189
* Switch to SLP is done by switching SLP clock to LPCLK first and then
184
190
* enabling SLP. The former has to be synchronized with negative edge of
185
191
* LPCLK and the latter happens on positive edge of LPCLK so we just
186
192
* assume 2 LPCLK ticks in worst case.
187
193
*/
188
- T_USEC (12 ) + T_LPTICK (2 ) +
194
+ T_USEC (15 ) + T_LPTICK (2 ) +
189
195
/*
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.
197
200
*/
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 );
209
202
}
210
203
211
204
extern bool ble_rf_try_recalibrate (uint32_t idle_time_us );
@@ -234,7 +227,7 @@ cmac_sleep(void)
234
227
* happen so need to make sure we can be up and running on time.
235
228
*/
236
229
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 ;
238
231
if ((int32_t )sleep_usecs <= 0 ) {
239
232
switch_to_slp = false;
240
233
deep_sleep = false;
@@ -245,14 +238,15 @@ cmac_sleep(void)
245
238
goto skip_sleep ;
246
239
}
247
240
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 ) {
250
244
switch_to_slp = false;
251
245
deep_sleep = false;
252
246
goto do_sleep ;
253
247
}
254
248
255
- if (!cmac_sleep_is_switch_allowed ()) {
249
+ if (!cmac_sleep_is_slp_allowed ()) {
256
250
switch_to_slp = false;
257
251
deep_sleep = false;
258
252
} else if (!cmac_sleep_is_deep_sleep_allowed ()) {
0 commit comments