Browse Source

Make sure micros() is monotonic

main
PaulStoffregen 5 years ago
parent
commit
bd4a9e2cfb
1 changed files with 6 additions and 4 deletions
  1. +6
    -4
      teensy4/delay.c

+ 6
- 4
teensy4/delay.c View File

uint32_t micros(void) uint32_t micros(void)
{ {
uint32_t ccdelta, usec, smc, scc; uint32_t ccdelta, usec, smc, scc;
do {
__LDREXW(&systick_safe_read);
do {
__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; ccdelta = ARM_DWT_CYCCNT - scc;
usec = 1000*smc + (ccdelta/(F_CPU_ACTUAL/1000000));
uint32_t frac = ccdelta/(F_CPU_ACTUAL/1000000);
if (frac > 999) frac = 999;
usec = 1000*smc + frac;
return usec; return usec;
} }



Loading…
Cancel
Save