1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-24 16:55:36 +03:00

Merge pull request #6344 from DieHertz/bfdev-diehertz-fix-micros-race-condition

Ensured micros() doesn't return a smaller value on millisecond bound
This commit is contained in:
Michael Keller 2018-07-13 20:46:53 +12:00 committed by GitHub
commit 14e3da855f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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