Skip to content

Commit 38b9e8e

Browse files
committed
STM: prevent task switch errors in HAL_GetTick
1 parent 0c6753b commit 38b9e8e

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

targets/TARGET_STM/hal_tick_overrides.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "hal/us_ticker_api.h"
2121
#include "us_ticker_defines.h"
22+
#include "mbed_critical.h"
2223

2324
// This variable is set to 1 at the of mbed_sdk_init function.
2425
// The ticker_read_us function must not be called until the mbed_sdk_init is terminated.
@@ -50,6 +51,9 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
5051

5152
uint32_t HAL_GetTick()
5253
{
54+
// Prevent a task switch causing errors since this function isn't re-entrant
55+
core_util_critical_section_enter();
56+
5357
uint32_t new_time = us_ticker_read();
5458
uint32_t elapsed_time = (((new_time - prev_time) & US_TICKER_MASK) + prev_tick_remainder);
5559
prev_time = new_time;
@@ -67,6 +71,8 @@ uint32_t HAL_GetTick()
6771
prev_tick_remainder = elapsed_time % 1000;
6872
}
6973
total_ticks += elapsed_ticks;
74+
75+
core_util_critical_section_exit();
7076
return total_ticks;
7177
}
7278

0 commit comments

Comments
 (0)