diff --git a/src/main/drivers/system.c b/src/main/drivers/system.c index 982a7fb66..98bc25b3f 100644 --- a/src/main/drivers/system.c +++ b/src/main/drivers/system.c @@ -35,6 +35,7 @@ static uint32_t usTicks = 0; // current uptime for 1kHz systick timer. will rollover after 49 days. hopefully we won't care. static volatile uint32_t sysTickUptime = 0; +static volatile uint32_t sysTickValStamp = 0; // cached value of RCC->CSR uint32_t cachedRccCsrValue; @@ -57,6 +58,7 @@ void SysTick_Handler(void) { ATOMIC_BLOCK(NVIC_PRIO_MAX) { sysTickUptime++; + sysTickValStamp = SysTick->VAL; sysTickPending = 0; (void)(SysTick->CTRL); } @@ -108,12 +110,7 @@ uint32_t micros(void) do { ms = sysTickUptime; cycle_cnt = SysTick->VAL; - /* - * If the SysTick timer expired during the previous instruction, we need to give it a little time for that - * interrupt to be delivered before we can recheck sysTickUptime: - */ - asm volatile("\tnop\n"); - } while (ms != sysTickUptime); + } while (ms != sysTickUptime || cycle_cnt >= sysTickValStamp); return (ms * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks; }