From e524d8399abc076190d6320811d7f7ff54e49a85 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Wed, 15 Sep 2021 11:51:17 +0200
Subject: [PATCH] fix(pycardium): Fix potential race when systick counts from 1
 to 0

---
 pycardium/mphalport.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/pycardium/mphalport.c b/pycardium/mphalport.c
index c4ba6bdb1..ab3e0857c 100644
--- a/pycardium/mphalport.c
+++ b/pycardium/mphalport.c
@@ -166,13 +166,16 @@ void SysTick_Handler(void)
 static uint64_t systick_get_us()
 {
 	uint32_t val, count;
+	uint32_t irqsaved = __get_PRIMASK();
 
 	/* The asynchronous/slow clocking of the systick means that
 	 * its value can jump to 0 before the interrupt is triggered.
 	 * Simply wait until it is not 0 and then read the count. */
 	do {
+		__set_PRIMASK(0);
 		val   = SysTick->VAL;
 		count = systick_count;
+		__set_PRIMASK(irqsaved);
 	} while (val == 0);
 
 	uint64_t us = count * SYSTICK_INTERVAL_US +
-- 
GitLab