Skip to content

Commit a838cb2

Browse files
committed
save/restore timer registers before/after deepsleep
1 parent 9523bcf commit a838cb2

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

targets/TARGET_STM/sleep.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "mbed_error.h"
3737

3838
extern void rtc_synchronize(void);
39+
extern void save_timer_ctx(void);
40+
extern void restore_timer_ctx(void);
3941

4042
/* Wait loop - assuming tick is 1 us */
4143
static void wait_loop(uint32_t timeout)
@@ -161,8 +163,7 @@ void hal_deepsleep(void)
161163
// Disable IRQs
162164
core_util_critical_section_enter();
163165

164-
// Save the timer counter value in order to reprogram it after deepsleep
165-
uint32_t EnterTimeUS = us_ticker_read();
166+
save_timer_ctx();
166167

167168
// Request to enter STOP mode with regulator in low power mode
168169
#if TARGET_STM32L4
@@ -187,6 +188,7 @@ void hal_deepsleep(void)
187188
#else /* TARGET_STM32L4 */
188189
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
189190
#endif /* TARGET_STM32L4 */
191+
190192
// Verify Clock Out of Deep Sleep
191193
ForceClockOutofDeepSleep();
192194

@@ -199,12 +201,7 @@ void hal_deepsleep(void)
199201
* deep sleep */
200202
wait_loop(500);
201203

202-
// Reprogram the timer counter value saved before the deepsleep
203-
TIM_HandleTypeDef TimMasterHandle;
204-
TimMasterHandle.Instance = TIM_MST;
205-
__HAL_TIM_SET_COUNTER(&TimMasterHandle, EnterTimeUS);
206-
__HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
207-
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
204+
restore_timer_ctx();
208205

209206
#if DEVICE_RTC
210207
/* Wait for RTC RSF bit synchro if RTC is configured */
@@ -216,6 +213,7 @@ void hal_deepsleep(void)
216213
rtc_synchronize();
217214
}
218215
#endif
216+
219217
// Enable IRQs
220218
core_util_critical_section_exit();
221219
}

targets/TARGET_STM/us_ticker.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,20 @@ void us_ticker_clear_interrupt(void)
7878
__HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
7979
}
8080

81+
uint32_t timer_cnt_reg;
82+
uint32_t timer_ccr1_reg;
83+
uint32_t timer_dier_reg;
84+
85+
void save_timer_ctx(void)
86+
{
87+
timer_cnt_reg = __HAL_TIM_GET_COUNTER(&TimMasterHandle);
88+
timer_ccr1_reg = __HAL_TIM_GET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1);
89+
timer_dier_reg = TIM_MST->DIER;
90+
}
91+
92+
void restore_timer_ctx(void)
93+
{
94+
__HAL_TIM_SET_COUNTER(&TimMasterHandle, timer_cnt_reg);
95+
__HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, timer_ccr1_reg);
96+
TIM_MST->DIER = timer_dier_reg;
97+
}

0 commit comments

Comments
 (0)