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:
commit
e289f354a6
2 changed files with 46 additions and 1 deletions
|
@ -24,6 +24,7 @@
|
||||||
#include "light_led.h"
|
#include "light_led.h"
|
||||||
#include "sound_beeper.h"
|
#include "sound_beeper.h"
|
||||||
#include "nvic.h"
|
#include "nvic.h"
|
||||||
|
#include "build/atomic.h"
|
||||||
|
|
||||||
#include "system.h"
|
#include "system.h"
|
||||||
|
|
||||||
|
@ -42,15 +43,57 @@ void cycleCounterInit(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SysTick
|
// SysTick
|
||||||
|
|
||||||
|
static volatile int sysTickPending = 0;
|
||||||
|
|
||||||
void SysTick_Handler(void)
|
void SysTick_Handler(void)
|
||||||
{
|
{
|
||||||
|
ATOMIC_BLOCK(NVIC_PRIO_MAX) {
|
||||||
sysTickUptime++;
|
sysTickUptime++;
|
||||||
|
sysTickPending = 0;
|
||||||
|
(void)(SysTick->CTRL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return system uptime in microseconds (rollover in 70minutes)
|
// 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)
|
uint32_t micros(void)
|
||||||
{
|
{
|
||||||
register uint32_t ms, cycle_cnt;
|
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 {
|
do {
|
||||||
ms = sysTickUptime;
|
ms = sysTickUptime;
|
||||||
cycle_cnt = SysTick->VAL;
|
cycle_cnt = SysTick->VAL;
|
||||||
|
@ -60,6 +103,7 @@ uint32_t micros(void)
|
||||||
*/
|
*/
|
||||||
asm volatile("\tnop\n");
|
asm volatile("\tnop\n");
|
||||||
} while (ms != sysTickUptime);
|
} while (ms != sysTickUptime);
|
||||||
|
|
||||||
return (ms * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks;
|
return (ms * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ void delayMicroseconds(uint32_t us);
|
||||||
void delay(uint32_t ms);
|
void delay(uint32_t ms);
|
||||||
|
|
||||||
uint32_t micros(void);
|
uint32_t micros(void);
|
||||||
|
uint32_t microsISR(void);
|
||||||
uint32_t millis(void);
|
uint32_t millis(void);
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue