1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-14 20:10:18 +03:00

Merge pull request #1264 from jflyper/fix-micros-final

Fix micros using COUNTFLAG
This commit is contained in:
borisbstyle 2016-10-09 00:52:44 +02:00 committed by GitHub
commit e289f354a6
2 changed files with 46 additions and 1 deletions

View file

@ -24,6 +24,7 @@
#include "light_led.h"
#include "sound_beeper.h"
#include "nvic.h"
#include "build/atomic.h"
#include "system.h"
@ -42,15 +43,57 @@ void cycleCounterInit(void)
}
// SysTick
static volatile int sysTickPending = 0;
void SysTick_Handler(void)
{
sysTickUptime++;
ATOMIC_BLOCK(NVIC_PRIO_MAX) {
sysTickUptime++;
sysTickPending = 0;
(void)(SysTick->CTRL);
}
}
// Return system uptime in microseconds (rollover in 70minutes)
uint32_t microsISR(void)
{
register uint32_t ms, pending, cycle_cnt;
ATOMIC_BLOCK(NVIC_PRIO_MAX) {
cycle_cnt = SysTick->VAL;
if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) {
// Update pending.
// Record it for multiple calls within the same rollover period
// (Will be cleared when serviced).
// Note that multiple rollovers are not considered.
sysTickPending = 1;
// Read VAL again to ensure the value is read after the rollover.
cycle_cnt = SysTick->VAL;
}
ms = sysTickUptime;
pending = sysTickPending;
}
return ((ms + pending) * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks;
}
uint32_t micros(void)
{
register uint32_t ms, cycle_cnt;
// Call microsISR() in interrupt and elevated (non-zero) BASEPRI context
if ((SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) || (__get_BASEPRI())) {
return microsISR();
}
do {
ms = sysTickUptime;
cycle_cnt = SysTick->VAL;
@ -60,6 +103,7 @@ uint32_t micros(void)
*/
asm volatile("\tnop\n");
} while (ms != sysTickUptime);
return (ms * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks;
}

View file

@ -22,6 +22,7 @@ void delayMicroseconds(uint32_t us);
void delay(uint32_t ms);
uint32_t micros(void);
uint32_t microsISR(void);
uint32_t millis(void);
typedef enum {