CCmicros - micros version using Cycle counterteensy4-core
| @@ -336,8 +336,11 @@ void MillisTimer::runFromTimer() | |||
| // TODO: this doesn't work for IMXRT - no longer using predefined names | |||
| extern "C" volatile uint32_t systick_millis_count; | |||
| extern "C" volatile uint32_t systick_cycle_count; | |||
| extern "C" uint32_t systick_safe_read; // micros() synchronization | |||
| extern "C" void systick_isr(void) | |||
| { | |||
| systick_cycle_count = ARM_DWT_CYCCNT; | |||
| systick_millis_count++; | |||
| MillisTimer::runFromTimer(); | |||
| } | |||
| @@ -1,8 +1,11 @@ | |||
| #include "core_pins.h" | |||
| #include "arm_math.h" // micros() synchronization | |||
| //volatile uint32_t F_CPU = 396000000; | |||
| //volatile uint32_t F_BUS = 132000000; | |||
| volatile uint32_t systick_millis_count = 0; | |||
| volatile uint32_t systick_cycle_count = 0; | |||
| uint32_t systick_safe_read; // micros() synchronization | |||
| // page 411 says "24 MHz XTALOSC can be the external clock source of SYSTICK" | |||
| // Testing shows the frequency is actually 100 kHz - but how? Did NXP really | |||
| @@ -61,6 +64,20 @@ void delay(uint32_t msec) | |||
| // TODO... | |||
| } | |||
| uint32_t micros(void) | |||
| { | |||
| uint32_t ccdelta, usec, 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; | |||
| usec = 1000*smc + (ccdelta/(F_CPU_ACTUAL/1000000)); | |||
| return usec; | |||
| } | |||
| #if 0 // kept to compare test to cycle count micro() | |||
| uint32_t micros(void) | |||
| { | |||
| uint32_t msec, tick, elapsed, istatus, usec; | |||
| @@ -118,4 +135,4 @@ uint32_t micros(void) | |||
| prev_usec = usec; | |||
| return usec; | |||
| } | |||
| #endif | |||
| @@ -111,6 +111,7 @@ void ResetHandler(void) | |||
| // the ARM clock to run at different speeds. | |||
| #define SYSTICK_EXT_FREQ 100000 | |||
| extern volatile uint32_t systick_cycle_count; | |||
| static void configure_systick(void) | |||
| { | |||
| _VectorsRam[14] = pendablesrvreq_isr; | |||
| @@ -121,6 +122,7 @@ static void configure_systick(void) | |||
| SCB_SHPR3 = 0x20000000; // Systick = priority 32 | |||
| ARM_DEMCR |= ARM_DEMCR_TRCENA; | |||
| ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA; // turn on cycle counter | |||
| systick_cycle_count = ARM_DWT_CYCCNT; // compiled 0, corrected w/1st systick | |||
| } | |||
| @@ -246,6 +248,7 @@ void usb_pll_start() | |||
| // R2 | |||
| // R1 | |||
| // R0 | |||
| __attribute__((weak)) | |||
| void unused_interrupt_vector(void) | |||
| { | |||
| // TODO: polling Serial to complete buffered transmits | |||