Ensured micros() doesn't return a smaller value on millisecond bound
This commit is contained in:
parent
86b36b9488
commit
a0d2f444ac
|
@ -35,6 +35,7 @@
|
||||||
static uint32_t usTicks = 0;
|
static uint32_t usTicks = 0;
|
||||||
// current uptime for 1kHz systick timer. will rollover after 49 days. hopefully we won't care.
|
// 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 sysTickUptime = 0;
|
||||||
|
static volatile uint32_t sysTickValStamp = 0;
|
||||||
// cached value of RCC->CSR
|
// cached value of RCC->CSR
|
||||||
uint32_t cachedRccCsrValue;
|
uint32_t cachedRccCsrValue;
|
||||||
|
|
||||||
|
@ -57,6 +58,7 @@ void SysTick_Handler(void)
|
||||||
{
|
{
|
||||||
ATOMIC_BLOCK(NVIC_PRIO_MAX) {
|
ATOMIC_BLOCK(NVIC_PRIO_MAX) {
|
||||||
sysTickUptime++;
|
sysTickUptime++;
|
||||||
|
sysTickValStamp = SysTick->VAL;
|
||||||
sysTickPending = 0;
|
sysTickPending = 0;
|
||||||
(void)(SysTick->CTRL);
|
(void)(SysTick->CTRL);
|
||||||
}
|
}
|
||||||
|
@ -108,12 +110,7 @@ uint32_t micros(void)
|
||||||
do {
|
do {
|
||||||
ms = sysTickUptime;
|
ms = sysTickUptime;
|
||||||
cycle_cnt = SysTick->VAL;
|
cycle_cnt = SysTick->VAL;
|
||||||
/*
|
} while (ms != sysTickUptime || cycle_cnt >= sysTickValStamp);
|
||||||
* 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);
|
|
||||||
|
|
||||||
return (ms * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks;
|
return (ms * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue