save/restore timer registers before/after deepsleep

pull/7352/head
bcostm 2018-07-04 16:44:00 +02:00
parent 9523bcf8c9
commit a838cb21d1
2 changed files with 23 additions and 8 deletions

View File

@ -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();
}

View File

@ -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;
}