소스 검색

Optimize micros()

main
PaulStoffregen 5 년 전
부모
커밋
041bfbe9ed
3개의 변경된 파일10개의 추가작업 그리고 5개의 파일을 삭제
  1. +1
    -0
      teensy4/clockspeed.c
  2. +1
    -0
      teensy4/core_pins.h
  3. +8
    -5
      teensy4/delay.c

+ 1
- 0
teensy4/clockspeed.c 파일 보기

@@ -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 파일 보기

@@ -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 파일 보기

@@ -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;
}


Loading…
취소
저장