Skip to content

Commit bcec185

Browse files
author
Cruz Monrreal
authored
Merge pull request #7352 from bcostm/fix_rtc_ticker
STM32: Fix RTC test issue on targets using a 16-bit timer for us_ticker
2 parents 8f43223 + a838cb2 commit bcec185

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

targets/TARGET_STM/hal_tick_16b.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#if TIM_MST_16BIT
2020

2121
extern TIM_HandleTypeDef TimMasterHandle;
22+
extern uint32_t prev_time;
23+
extern uint32_t elapsed_time;
2224

2325
volatile uint32_t PreviousVal = 0;
2426

@@ -108,6 +110,10 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
108110
TIM_MST_DBGMCU_FREEZE;
109111
#endif
110112

113+
// Used by HAL_GetTick()
114+
prev_time = 0;
115+
elapsed_time = 0;
116+
111117
return HAL_OK;
112118
}
113119

targets/TARGET_STM/hal_tick_common.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,45 @@
1414
* limitations under the License.
1515
*/
1616
#include "hal/us_ticker_api.h"
17+
#include "hal_tick.h"
1718

1819
// Overwrite default HAL functions defined as "weak"
1920

21+
// This variable is set to 1 at the of mbed_sdk_init function.
22+
// The ticker_read_us function must not be called until the mbed_sdk_init is terminated.
23+
extern int mbed_sdk_inited;
24+
25+
#if TIM_MST_16BIT
26+
// Variables also reset in HAL_InitTick()
27+
uint32_t prev_time = 0;
28+
uint32_t elapsed_time = 0;
29+
#endif
30+
31+
// 1 ms tick is required for ST HAL driver
2032
uint32_t HAL_GetTick()
2133
{
22-
return ticker_read_us(get_us_ticker_data()) / 1000; // 1 ms tick is required for ST HAL
34+
#if TIM_MST_16BIT
35+
uint32_t new_time;
36+
if (mbed_sdk_inited) {
37+
// Apply the latest time recorded just before the sdk is inited
38+
new_time = ticker_read_us(get_us_ticker_data()) + prev_time;
39+
prev_time = 0; // Use this time only once
40+
return (new_time / 1000);
41+
}
42+
else {
43+
new_time = us_ticker_read();
44+
elapsed_time += (new_time - prev_time) & 0xFFFF; // Only use the lower 16 bits
45+
prev_time = new_time;
46+
return (elapsed_time / 1000);
47+
}
48+
#else // 32-bit timer
49+
if (mbed_sdk_inited) {
50+
return (ticker_read_us(get_us_ticker_data()) / 1000);
51+
}
52+
else {
53+
return (us_ticker_read() / 1000);
54+
}
55+
#endif
2356
}
2457

2558
void HAL_SuspendTick(void)

targets/TARGET_STM/mbed_overrides.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
*/
2828
#include "cmsis.h"
2929

30+
int mbed_sdk_inited = 0;
31+
3032
// This function is called after RAM initialization and before main.
3133
void mbed_sdk_init()
3234
{
@@ -51,4 +53,6 @@ void mbed_sdk_init()
5153
AHB/APBx prescalers and Flash settings */
5254
SetSysClock();
5355
SystemCoreClockUpdate();
56+
57+
mbed_sdk_inited = 1;
5458
}

targets/TARGET_STM/sleep.c

Lines changed: 6 additions & 4 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,7 +163,7 @@ void hal_deepsleep(void)
161163
// Disable IRQs
162164
core_util_critical_section_enter();
163165

164-
uint32_t EnterTimeUS = us_ticker_read();
166+
save_timer_ctx();
165167

166168
// Request to enter STOP mode with regulator in low power mode
167169
#if TARGET_STM32L4
@@ -186,6 +188,7 @@ void hal_deepsleep(void)
186188
#else /* TARGET_STM32L4 */
187189
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
188190
#endif /* TARGET_STM32L4 */
191+
189192
// Verify Clock Out of Deep Sleep
190193
ForceClockOutofDeepSleep();
191194

@@ -198,9 +201,7 @@ void hal_deepsleep(void)
198201
* deep sleep */
199202
wait_loop(500);
200203

201-
TIM_HandleTypeDef TimMasterHandle;
202-
TimMasterHandle.Instance = TIM_MST;
203-
__HAL_TIM_SET_COUNTER(&TimMasterHandle, EnterTimeUS);
204+
restore_timer_ctx();
204205

205206
#if DEVICE_RTC
206207
/* Wait for RTC RSF bit synchro if RTC is configured */
@@ -212,6 +213,7 @@ void hal_deepsleep(void)
212213
rtc_synchronize();
213214
}
214215
#endif
216+
215217
// Enable IRQs
216218
core_util_critical_section_exit();
217219
}

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)