Skip to content

Commit 98720b0

Browse files
Merge pull request #4809 from betzw/betzw_ticker_github_wb
STM32: Align HAL & US tickers
2 parents 25bcc27 + 4fe8e8c commit 98720b0

File tree

4 files changed

+15
-31
lines changed

4 files changed

+15
-31
lines changed

targets/TARGET_STM/hal_tick_16b.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
161161
return HAL_OK;
162162
}
163163

164+
/* NOTE: must be called with interrupts disabled! */
164165
void HAL_SuspendTick(void)
165166
{
166-
TimMasterHandle.Instance = TIM_MST;
167167
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2);
168168
}
169169

170+
/* NOTE: must be called with interrupts disabled! */
170171
void HAL_ResumeTick(void)
171172
{
172-
TimMasterHandle.Instance = TIM_MST;
173173
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
174174
}
175175

targets/TARGET_STM/hal_tick_32b.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
131131
return HAL_OK;
132132
}
133133

134+
/* NOTE: must be called with interrupts disabled! */
134135
void HAL_SuspendTick(void)
135136
{
136-
TimMasterHandle.Instance = TIM_MST;
137137
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2);
138138
}
139139

140+
/* NOTE: must be called with interrupts disabled! */
140141
void HAL_ResumeTick(void)
141142
{
142-
TimMasterHandle.Instance = TIM_MST;
143143
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
144144
}
145145

targets/TARGET_STM/us_ticker_16b.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,13 @@ TIM_HandleTypeDef TimMasterHandle;
2626
volatile uint32_t SlaveCounter = 0;
2727
volatile uint32_t oc_int_part = 0;
2828

29-
static int us_ticker_inited = 0;
30-
3129
void us_ticker_init(void)
3230
{
33-
if (us_ticker_inited) return;
34-
us_ticker_inited = 1;
35-
36-
TimMasterHandle.Instance = TIM_MST;
37-
38-
HAL_InitTick(0); // The passed value is not used
31+
/* NOTE: assuming that HAL tick has already been initialized! */
3932
}
4033

4134
uint32_t us_ticker_read()
4235
{
43-
if (!us_ticker_inited) us_ticker_init();
44-
4536
uint16_t cntH_old, cntH, cntL;
4637
do {
4738
cntH_old = SlaveCounter;
@@ -73,7 +64,7 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
7364
{
7465
// NOTE: This function must be called with interrupts disabled to keep our
7566
// timer interrupt setup atomic
76-
TimMasterHandle.Instance = TIM_MST;
67+
7768
// Set new output compare value
7869
__HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, timestamp & 0xFFFF);
7970
// Ensure the compare event starts clear
@@ -178,19 +169,18 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
178169

179170
void us_ticker_fire_interrupt(void)
180171
{
181-
TimMasterHandle.Instance = TIM_MST;
182172
HAL_TIM_GenerateEvent(&TimMasterHandle, TIM_EVENTSOURCE_CC1);
183173
}
184174

175+
/* NOTE: must be called with interrupts disabled! */
185176
void us_ticker_disable_interrupt(void)
186177
{
187-
TimMasterHandle.Instance = TIM_MST;
188178
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
189179
}
190180

181+
/* NOTE: must be called with interrupts disabled! */
191182
void us_ticker_clear_interrupt(void)
192183
{
193-
TimMasterHandle.Instance = TIM_MST;
194184
__HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
195185
}
196186

targets/TARGET_STM/us_ticker_32b.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,21 @@
2323

2424
TIM_HandleTypeDef TimMasterHandle;
2525

26-
static int us_ticker_inited = 0;
27-
2826
void us_ticker_init(void)
2927
{
30-
if (us_ticker_inited) return;
31-
us_ticker_inited = 1;
32-
33-
TimMasterHandle.Instance = TIM_MST;
34-
35-
HAL_InitTick(0); // The passed value is not used
28+
/* NOTE: assuming that HAL tick has already been initialized! */
3629
}
3730

3831
uint32_t us_ticker_read()
3932
{
40-
if (!us_ticker_inited) us_ticker_init();
4133
return TIM_MST->CNT;
4234
}
4335

4436
void us_ticker_set_interrupt(timestamp_t timestamp)
4537
{
46-
TimMasterHandle.Instance = TIM_MST;
38+
// NOTE: This function must be called with interrupts disabled to keep our
39+
// timer interrupt setup atomic
40+
4741
// disable IT while we are handling the correct timestamp
4842
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
4943
// Set new output compare value
@@ -57,16 +51,16 @@ void us_ticker_fire_interrupt(void)
5751
LL_TIM_GenerateEvent_CC1(TimMasterHandle.Instance);
5852
}
5953

54+
/* NOTE: must be called with interrupts disabled! */
6055
void us_ticker_disable_interrupt(void)
6156
{
62-
TimMasterHandle.Instance = TIM_MST;
6357
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
6458
}
6559

60+
/* NOTE: must be called with interrupts disabled! */
6661
void us_ticker_clear_interrupt(void)
6762
{
68-
TimMasterHandle.Instance = TIM_MST;
69-
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
63+
__HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
7064
}
7165

7266
#endif // !TIM_MST_16BIT

0 commit comments

Comments
 (0)