Ensured micros() doesn't return a smaller value on millisecond bound

This commit is contained in:
Andrey Mironov 2018-07-12 16:40:02 +03:00
parent 86b36b9488
commit a0d2f444ac
1 changed files with 3 additions and 6 deletions

View File

@ -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;
}