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" #include "mbed_error.h"
extern void rtc_synchronize(void); extern void rtc_synchronize(void);
extern void save_timer_ctx(void);
extern void restore_timer_ctx(void);
/* Wait loop - assuming tick is 1 us */ /* Wait loop - assuming tick is 1 us */
static void wait_loop(uint32_t timeout) static void wait_loop(uint32_t timeout)
@ -161,8 +163,7 @@ void hal_deepsleep(void)
// Disable IRQs // Disable IRQs
core_util_critical_section_enter(); core_util_critical_section_enter();
// Save the timer counter value in order to reprogram it after deepsleep save_timer_ctx();
uint32_t EnterTimeUS = us_ticker_read();
// Request to enter STOP mode with regulator in low power mode // Request to enter STOP mode with regulator in low power mode
#if TARGET_STM32L4 #if TARGET_STM32L4
@ -187,6 +188,7 @@ void hal_deepsleep(void)
#else /* TARGET_STM32L4 */ #else /* TARGET_STM32L4 */
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
#endif /* TARGET_STM32L4 */ #endif /* TARGET_STM32L4 */
// Verify Clock Out of Deep Sleep // Verify Clock Out of Deep Sleep
ForceClockOutofDeepSleep(); ForceClockOutofDeepSleep();
@ -199,12 +201,7 @@ void hal_deepsleep(void)
* deep sleep */ * deep sleep */
wait_loop(500); wait_loop(500);
// Reprogram the timer counter value saved before the deepsleep restore_timer_ctx();
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);
#if DEVICE_RTC #if DEVICE_RTC
/* Wait for RTC RSF bit synchro if RTC is configured */ /* Wait for RTC RSF bit synchro if RTC is configured */
@ -216,6 +213,7 @@ void hal_deepsleep(void)
rtc_synchronize(); rtc_synchronize();
} }
#endif #endif
// Enable IRQs // Enable IRQs
core_util_critical_section_exit(); 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); __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;
}