Skip to content

Commit 3202841

Browse files
committed
Make HAL & US tickers IRQ and idle safe
1 parent 953b925 commit 3202841

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

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/sleep.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,25 @@ extern void HAL_ResumeTick(void);
4040

4141
void hal_sleep(void)
4242
{
43+
// Disable IRQs
44+
core_util_critical_section_enter();
45+
4346
// Stop HAL tick to avoid to exit sleep in 1ms
4447
HAL_SuspendTick();
4548
// Request to enter SLEEP mode
4649
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
47-
4850
// Restart HAL tick
4951
HAL_ResumeTick();
52+
53+
// Enable IRQs
54+
core_util_critical_section_exit();
5055
}
5156

5257
void hal_deepsleep(void)
5358
{
59+
// Disable IRQs
60+
core_util_critical_section_enter();
61+
5462
// Stop HAL tick
5563
HAL_SuspendTick();
5664
uint32_t EnterTimeUS = us_ticker_read();
@@ -82,6 +90,9 @@ void hal_deepsleep(void)
8290
// Restart HAL tick
8391
HAL_ResumeTick();
8492

93+
// Enable IRQs
94+
core_util_critical_section_exit();
95+
8596
// After wake-up from STOP reconfigure the PLL
8697
SetSysClock();
8798

targets/TARGET_STM/us_ticker_32b.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,30 @@
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+
/* Disable global IRQs */
39+
core_util_critical_section_enter();
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
5044
__HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp);
5145
// Enable IT
5246
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
47+
48+
/* Enable global IRQs */
49+
core_util_critical_section_exit();
5350
}
5451

5552
void us_ticker_fire_interrupt(void)
@@ -59,14 +56,16 @@ void us_ticker_fire_interrupt(void)
5956

6057
void us_ticker_disable_interrupt(void)
6158
{
62-
TimMasterHandle.Instance = TIM_MST;
59+
core_util_critical_section_enter();
6360
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
61+
core_util_critical_section_exit();
6462
}
6563

6664
void us_ticker_clear_interrupt(void)
6765
{
68-
TimMasterHandle.Instance = TIM_MST;
66+
core_util_critical_section_enter();
6967
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
68+
core_util_critical_section_exit();
7069
}
7170

7271
#endif // !TIM_MST_16BIT

0 commit comments

Comments
 (0)