Skip to content

Commit 0b133be

Browse files
committed
stm32 ticker: change th eplace where timer init in done, fix overflow issue with 16-bit timer
- Move back the 16/32bit timer initialization in HAL_InitTick() and not in us_ticker_init() - Use ticker_read_us() and us_ticker_read() in HAL_GetTick() to fix potential overflow issue with the 16bit timer ==> These corrections allow timer, rtc, sleep, tick tests to PASS
1 parent fc50e28 commit 0b133be

File tree

2 files changed

+77
-39
lines changed

2 files changed

+77
-39
lines changed

targets/TARGET_STM/hal_tick_overrides.c

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2006-2013 ARM Limited
2+
* Copyright (c) 2006-2018 ARM Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,18 +14,58 @@
1414
* limitations under the License.
1515
*/
1616
#include "hal/us_ticker_api.h"
17+
#include "us_ticker_data.h"
18+
19+
// This variable is set to 1 at the of mbed_sdk_init function.
20+
// The ticker_read_us function must not be called until the mbed_sdk_init is terminated.
21+
extern int mbed_sdk_inited;
22+
23+
// Defined in us_ticker.c
24+
void init_16bit_timer(void);
25+
void init_32bit_timer(void);
26+
27+
#if TIM_MST_BIT_WIDTH == 16
28+
// Variables also reset in us_ticker_init()
29+
uint32_t prev_time = 0;
30+
uint32_t elapsed_time = 0;
31+
#endif
1732

1833
// Overwrite default HAL functions defined as "weak"
1934

2035
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
2136
{
22-
us_ticker_init();
37+
#if TIM_MST_BIT_WIDTH == 16
38+
init_16bit_timer();
39+
#else
40+
init_32bit_timer();
41+
#endif
2342
return HAL_OK;
2443
}
2544

2645
uint32_t HAL_GetTick()
2746
{
28-
return 0;
47+
#if TIM_MST_BIT_WIDTH == 16
48+
uint32_t new_time;
49+
if (mbed_sdk_inited) {
50+
// Apply the latest time recorded just before the sdk is inited
51+
new_time = ticker_read_us(get_us_ticker_data()) + prev_time;
52+
prev_time = 0; // Use this time only once
53+
return (new_time / 1000);
54+
}
55+
else {
56+
new_time = us_ticker_read();
57+
elapsed_time += (new_time - prev_time) & 0xFFFF; // Only use the lower 16 bits
58+
prev_time = new_time;
59+
return (elapsed_time / 1000);
60+
}
61+
#else // 32-bit timer
62+
if (mbed_sdk_inited) {
63+
return (ticker_read_us(get_us_ticker_data()) / 1000);
64+
}
65+
else {
66+
return (us_ticker_read() / 1000);
67+
}
68+
#endif
2969
}
3070

3171
void HAL_SuspendTick(void)

targets/TARGET_STM/us_ticker.c

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2006-2016 ARM Limited
2+
* Copyright (c) 2006-2018 ARM Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,9 @@
2020

2121
TIM_HandleTypeDef TimMasterHandle;
2222

23-
bool us_ticker_initialized = false;
23+
uint32_t timer_cnt_reg;
24+
uint32_t timer_ccr1_reg;
25+
uint32_t timer_dier_reg;
2426

2527
const ticker_info_t *us_ticker_get_info()
2628
{
@@ -36,6 +38,9 @@ void us_ticker_irq_handler(void);
3638
// ************************************ 16-bit timer ************************************
3739
#if TIM_MST_BIT_WIDTH == 16
3840

41+
extern uint32_t prev_time;
42+
extern uint32_t elapsed_time;
43+
3944
#if defined(TARGET_STM32F0)
4045
void timer_update_irq_handler(void)
4146
{
@@ -60,32 +65,8 @@ void timer_oc_irq_handler(void)
6065
}
6166
}
6267

63-
// ************************************ 32-bit timer ************************************
64-
#else
65-
66-
void timer_irq_handler(void)
68+
void init_16bit_timer(void)
6769
{
68-
TimMasterHandle.Instance = TIM_MST;
69-
if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
70-
if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
71-
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
72-
us_ticker_irq_handler();
73-
}
74-
}
75-
}
76-
77-
#endif // 16-bit/32-bit timer
78-
79-
void us_ticker_init(void)
80-
{
81-
if (us_ticker_initialized) {
82-
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
83-
__HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
84-
return;
85-
}
86-
87-
// ************************************ 16-bit timer ************************************
88-
#if TIM_MST_BIT_WIDTH == 16
8970
// Enable timer clock
9071
TIM_MST_RCC;
9172

@@ -137,9 +118,27 @@ void us_ticker_init(void)
137118

138119
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
139120

121+
// Used by HAL_GetTick()
122+
prev_time = 0;
123+
elapsed_time = 0;
124+
}
125+
140126
// ************************************ 32-bit timer ************************************
141127
#else
142128

129+
void timer_irq_handler(void)
130+
{
131+
TimMasterHandle.Instance = TIM_MST;
132+
if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
133+
if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
134+
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
135+
us_ticker_irq_handler();
136+
}
137+
}
138+
}
139+
140+
void init_32bit_timer(void)
141+
{
143142
RCC_ClkInitTypeDef RCC_ClkInitStruct;
144143
uint32_t PclkFreq;
145144

@@ -162,8 +161,8 @@ void us_ticker_init(void)
162161
TIM_MST_RESET_OFF;
163162

164163
// Configure time base
165-
TimMasterHandle.Instance = TIM_MST;
166-
TimMasterHandle.Init.Period = 0xFFFFFFFF;
164+
TimMasterHandle.Instance = TIM_MST;
165+
TimMasterHandle.Init.Period = 0xFFFFFFFF;
167166

168167
// TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
169168
#if TIM_MST_PCLK == 1
@@ -199,11 +198,14 @@ void us_ticker_init(void)
199198
#endif
200199

201200
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
201+
}
202202

203203
#endif // 16-bit/32-bit timer
204204

205-
us_ticker_initialized = true;
206-
205+
void us_ticker_init(void)
206+
{
207+
// Timer is already initialized in HAL_InitTick()
208+
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
207209
}
208210

209211
uint32_t us_ticker_read()
@@ -241,10 +243,6 @@ void us_ticker_clear_interrupt(void)
241243
__HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
242244
}
243245

244-
uint32_t timer_cnt_reg;
245-
uint32_t timer_ccr1_reg;
246-
uint32_t timer_dier_reg;
247-
248246
void save_timer_ctx(void)
249247
{
250248
timer_cnt_reg = __HAL_TIM_GET_COUNTER(&TimMasterHandle);
@@ -257,4 +255,4 @@ void restore_timer_ctx(void)
257255
__HAL_TIM_SET_COUNTER(&TimMasterHandle, timer_cnt_reg);
258256
__HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, timer_ccr1_reg);
259257
TIM_MST->DIER = timer_dier_reg;
260-
}
258+
}

0 commit comments

Comments
 (0)