From f956968cb422879d38caa66e396eb96c98a3e0cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 1 Jun 2020 20:45:27 +0200 Subject: [PATCH] Make reading the global ms tick atomic in order to prevent glitches --- stm-firmware/include/reflow-controller/systick.h | 2 +- stm-firmware/systick.c | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/stm-firmware/include/reflow-controller/systick.h b/stm-firmware/include/reflow-controller/systick.h index 5060096..fa1568c 100644 --- a/stm-firmware/include/reflow-controller/systick.h +++ b/stm-firmware/include/reflow-controller/systick.h @@ -45,7 +45,7 @@ extern volatile uint32_t wait_tick_ms; * @brief Systemclock in milliseconds. * * This value must not be reset during the whole runtime. - * + * @warning In order to use this, you must assure that the read access is atomic. */ extern volatile uint64_t global_tick_ms; diff --git a/stm-firmware/systick.c b/stm-firmware/systick.c index b96c245..c998f6e 100644 --- a/stm-firmware/systick.c +++ b/stm-firmware/systick.c @@ -43,7 +43,13 @@ void systick_wait_ms(uint32_t ms) uint64_t systick_get_global_tick() { - return global_tick_ms; + uint64_t temp; + + __disable_irq(); + temp = global_tick_ms; + __enable_irq(); + + return temp; } bool __attribute__((optimize("O3"))) systick_ticks_have_passed(uint64_t start_timestamp, uint64_t ticks)