Parcourir la source

Optimize micros()

main
PaulStoffregen il y a 5 ans
Parent
révision
041bfbe9ed
3 fichiers modifiés avec 10 ajouts et 5 suppressions
  1. +1
    -0
      teensy4/clockspeed.c
  2. +1
    -0
      teensy4/core_pins.h
  3. +8
    -5
      teensy4/delay.c

+ 1
- 0
teensy4/clockspeed.c Voir le fichier

@@ -160,6 +160,7 @@ uint32_t set_arm_clock(uint32_t frequency)

F_CPU_ACTUAL = frequency;
F_BUS_ACTUAL = frequency / div_ipg;
scale_cpu_cycles_to_microseconds = 0xFFFFFFFFu / (uint32_t)(frequency / 1000000u);

printf("New Frequency: ARM=%u, IPG=%u\n", frequency, frequency / div_ipg);


+ 1
- 0
teensy4/core_pins.h Voir le fichier

@@ -836,6 +836,7 @@ void delay(uint32_t msec);

extern volatile uint32_t F_CPU_ACTUAL;
extern volatile uint32_t F_BUS_ACTUAL;
extern volatile uint32_t scale_cpu_cycles_to_microseconds;
extern volatile uint32_t systick_millis_count;

static inline uint32_t millis(void) __attribute__((always_inline, unused));

+ 8
- 5
teensy4/delay.c Voir le fichier

@@ -5,6 +5,7 @@
//volatile uint32_t F_BUS = 132000000;
volatile uint32_t systick_millis_count = 0;
volatile uint32_t systick_cycle_count = 0;
volatile uint32_t scale_cpu_cycles_to_microseconds = 0;
uint32_t systick_safe_read; // micros() synchronization

// page 411 says "24 MHz XTALOSC can be the external clock source of SYSTICK"
@@ -66,16 +67,18 @@ void delay(uint32_t msec)

uint32_t micros(void)
{
uint32_t ccdelta, usec, smc, scc;
uint32_t smc, scc;
do {
__LDREXW(&systick_safe_read);
smc = systick_millis_count;
scc = systick_cycle_count;
} while ( __STREXW(1, &systick_safe_read));
ccdelta = ARM_DWT_CYCCNT - scc;
uint32_t frac = ccdelta/(F_CPU_ACTUAL/1000000);
if (frac > 999) frac = 999;
usec = 1000*smc + frac;
uint32_t cyccnt = ARM_DWT_CYCCNT;
asm volatile("" : : : "memory");
uint32_t ccdelta = cyccnt - scc;
uint32_t frac = ((uint64_t)ccdelta * scale_cpu_cycles_to_microseconds) >> 32;
if (frac > 1000) frac = 1000;
uint32_t usec = 1000*smc + frac;
return usec;
}


Chargement…
Annuler
Enregistrer