Skip to content

Commit

Permalink
save/restore timer registers before/after deepsleep
Browse files Browse the repository at this point in the history
  • Loading branch information
bcostm committed Jul 4, 2018
1 parent 9523bcf commit a838cb2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
14 changes: 6 additions & 8 deletions targets/TARGET_STM/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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();

Expand All @@ -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 */
Expand All @@ -216,6 +213,7 @@ void hal_deepsleep(void)
rtc_synchronize();
}
#endif

// Enable IRQs
core_util_critical_section_exit();
}
Expand Down
17 changes: 17 additions & 0 deletions targets/TARGET_STM/us_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit a838cb2

Please sign in to comment.