From ed86fdbdf6e2d3eb9a118d3476ce4c370ec48b06 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 21 Apr 2024 12:45:36 +0200 Subject: [PATCH] samd/mphalport: Fix an execution order bug in mp_hal_ticks_us_64(). The upper 32 bit of the 64 bit ticks register was taken before disabling the interrupts. That may have caused a wrong return values. Besides that, the function may cause trouble when called in an IRQ context, because it unconditionally enables IRQ. Signed-off-by: robert-hh --- ports/samd/mphalport.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/samd/mphalport.c b/ports/samd/mphalport.c index 80904804c..3a33fdd6b 100644 --- a/ports/samd/mphalport.c +++ b/ports/samd/mphalport.c @@ -94,10 +94,10 @@ void mp_hal_delay_us(mp_uint_t us) { } uint64_t mp_hal_ticks_us_64(void) { - uint32_t us64_upper = ticks_us64_upper; uint32_t us64_lower; uint8_t intflag; __disable_irq(); + uint32_t us64_upper = ticks_us64_upper; #if defined(MCU_SAMD21) us64_lower = REG_TC4_COUNT32_COUNT; intflag = TC4->COUNT32.INTFLAG.reg;