Skip to content

Commit a1707a0

Browse files
committed
Remove critical section in us_ticker_set_interrupt() & extend modifications to 16bit timer
Note: This is the result of discussions on GitHub with Martin Kojtal, Vincent Coubard, et al. (see #4768)
1 parent 3202841 commit a1707a0

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
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/us_ticker_16b.c

Lines changed: 7 additions & 15 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,20 +169,21 @@ 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

185175
void us_ticker_disable_interrupt(void)
186176
{
187-
TimMasterHandle.Instance = TIM_MST;
177+
core_util_critical_section_enter();
188178
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
179+
core_util_critical_section_exit();
189180
}
190181

191182
void us_ticker_clear_interrupt(void)
192183
{
193-
TimMasterHandle.Instance = TIM_MST;
194-
__HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
184+
core_util_critical_section_enter();
185+
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
186+
core_util_critical_section_exit();
195187
}
196188

197189
#endif // TIM_MST_16BIT

targets/TARGET_STM/us_ticker_32b.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,15 @@ uint32_t us_ticker_read()
3535

3636
void us_ticker_set_interrupt(timestamp_t timestamp)
3737
{
38-
/* Disable global IRQs */
39-
core_util_critical_section_enter();
38+
// NOTE: This function must be called with interrupts disabled to keep our
39+
// timer interrupt setup atomic
4040

4141
// disable IT while we are handling the correct timestamp
4242
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
4343
// Set new output compare value
4444
__HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp);
4545
// Enable IT
4646
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
47-
48-
/* Enable global IRQs */
49-
core_util_critical_section_exit();
5047
}
5148

5249
void us_ticker_fire_interrupt(void)

0 commit comments

Comments
 (0)