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