diff --git a/targets/TARGET_STM/sleep.c b/targets/TARGET_STM/sleep.c index eadd637a72..168b9c70bc 100644 --- a/targets/TARGET_STM/sleep.c +++ b/targets/TARGET_STM/sleep.c @@ -36,6 +36,8 @@ #include "mbed_error.h" extern void rtc_synchronize(void); +extern void save_timer_ctx(void); +extern void restore_timer_ctx(void); /* Wait loop - assuming tick is 1 us */ static void wait_loop(uint32_t timeout) @@ -161,8 +163,7 @@ void hal_deepsleep(void) // Disable IRQs core_util_critical_section_enter(); - // Save the timer counter value in order to reprogram it after deepsleep - uint32_t EnterTimeUS = us_ticker_read(); + save_timer_ctx(); // Request to enter STOP mode with regulator in low power mode #if TARGET_STM32L4 @@ -187,6 +188,7 @@ void hal_deepsleep(void) #else /* TARGET_STM32L4 */ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); #endif /* TARGET_STM32L4 */ + // Verify Clock Out of Deep Sleep ForceClockOutofDeepSleep(); @@ -199,12 +201,7 @@ void hal_deepsleep(void) * deep sleep */ wait_loop(500); - // Reprogram the timer counter value saved before the deepsleep - TIM_HandleTypeDef TimMasterHandle; - TimMasterHandle.Instance = TIM_MST; - __HAL_TIM_SET_COUNTER(&TimMasterHandle, EnterTimeUS); - __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1); - __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1); + restore_timer_ctx(); #if DEVICE_RTC /* Wait for RTC RSF bit synchro if RTC is configured */ @@ -216,6 +213,7 @@ void hal_deepsleep(void) rtc_synchronize(); } #endif + // Enable IRQs core_util_critical_section_exit(); } diff --git a/targets/TARGET_STM/us_ticker.c b/targets/TARGET_STM/us_ticker.c index db4a9e96ab..ff1e777a8b 100644 --- a/targets/TARGET_STM/us_ticker.c +++ b/targets/TARGET_STM/us_ticker.c @@ -78,3 +78,20 @@ void us_ticker_clear_interrupt(void) __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1); } +uint32_t timer_cnt_reg; +uint32_t timer_ccr1_reg; +uint32_t timer_dier_reg; + +void save_timer_ctx(void) +{ + timer_cnt_reg = __HAL_TIM_GET_COUNTER(&TimMasterHandle); + timer_ccr1_reg = __HAL_TIM_GET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1); + timer_dier_reg = TIM_MST->DIER; +} + +void restore_timer_ctx(void) +{ + __HAL_TIM_SET_COUNTER(&TimMasterHandle, timer_cnt_reg); + __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, timer_ccr1_reg); + TIM_MST->DIER = timer_dier_reg; +}