Skip to content

Commit 3d92af5

Browse files
committed
Add delay to let clock stabilize when out of deep sleep
Tests have shown that there is hich-up on MSI clock during the setup phase. If this stabilization phase happens when application has restarted again this can have side effects, like grambled UART characters typically. So we're adding a delay before hading-over back to application. With this modification, on NCULEO_L476RG, the wake-up time is increased from 2ms to 2,5ms. If possible this should be improved in the future to save 500 microseconds of wak-up time. See TODO
1 parent 8007b1d commit 3d92af5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

targets/TARGET_STM/sleep.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
extern void HAL_SuspendTick(void);
3636
extern void HAL_ResumeTick(void);
3737

38+
/* Wait loop - assuming tick is 1 us */
39+
static void wait_loop(uint32_t timeout)
40+
{
41+
uint32_t t1, t2, elapsed = 0;
42+
t1 = us_ticker_read();
43+
do {
44+
t2 = us_ticker_read();
45+
elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t)t2 + 0xFFFFFFFF - t1 + 1);
46+
} while (elapsed < timeout);
47+
return;
48+
}
49+
3850
// On L4 platforms we've seen unstable PLL CLK configuraiton
3951
// when DEEP SLEEP exits just few µs after being entered
4052
// So we need to force MSI usage before setting clocks again
@@ -174,6 +186,12 @@ void hal_deepsleep(void)
174186
// After wake-up from STOP reconfigure the PLL
175187
SetSysClock();
176188

189+
/* Wait for clock to be stabilized.
190+
* TO DO: a better way of doing this, would be to rely on
191+
* HW Flag. At least this ensures proper operation out of
192+
* deep sleep */
193+
wait_loop(500);
194+
177195
TIM_HandleTypeDef TimMasterHandle;
178196
TimMasterHandle.Instance = TIM_MST;
179197
__HAL_TIM_SET_COUNTER(&TimMasterHandle, EnterTimeUS);

0 commit comments

Comments
 (0)