Browse Source

Optimize micros()

teensy4-core
PaulStoffregen 5 years ago
parent
commit
041bfbe9ed
3 changed files with 10 additions and 5 deletions
  1. +1
    -0
      teensy4/clockspeed.c
  2. +1
    -0
      teensy4/core_pins.h
  3. +8
    -5
      teensy4/delay.c

+ 1
- 0
teensy4/clockspeed.c View File



F_CPU_ACTUAL = frequency; F_CPU_ACTUAL = frequency;
F_BUS_ACTUAL = frequency / div_ipg; 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); printf("New Frequency: ARM=%u, IPG=%u\n", frequency, frequency / div_ipg);



+ 1
- 0
teensy4/core_pins.h View File



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


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

+ 8
- 5
teensy4/delay.c View File

//volatile uint32_t F_BUS = 132000000; //volatile uint32_t F_BUS = 132000000;
volatile uint32_t systick_millis_count = 0; volatile uint32_t systick_millis_count = 0;
volatile uint32_t systick_cycle_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 uint32_t systick_safe_read; // micros() synchronization


// page 411 says "24 MHz XTALOSC can be the external clock source of SYSTICK" // page 411 says "24 MHz XTALOSC can be the external clock source of SYSTICK"


uint32_t micros(void) uint32_t micros(void)
{ {
uint32_t ccdelta, usec, smc, scc;
uint32_t smc, scc;
do { do {
__LDREXW(&systick_safe_read); __LDREXW(&systick_safe_read);
smc = systick_millis_count; smc = systick_millis_count;
scc = systick_cycle_count; scc = systick_cycle_count;
} while ( __STREXW(1, &systick_safe_read)); } 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; return usec;
} }



Loading…
Cancel
Save