The code in wirish_time.h for micros() has a bug - it sometimes return a value 1000 uS more than the actual microsecond count due to an unprotected systick overflow.
Original code:
static inline uint32 micros(void) {
uint32 ms;
uint32 cycle_cnt;
uint32 res;
do {
cycle_cnt = systick_get_count();
ms = millis();
} while (ms != millis());
/* SYSTICK_RELOAD_VAL is 1 less than the number of cycles it
actually takes to complete a SysTick reload */
res = (fms * US_PER_MS) +
(SYSTICK_RELOAD_VAL + 1 - cycle_cnt) / CYCLES_PER_MICROSECOND;
return res;
}
Fixed code:
static inline uint32 micros(void) {
uint32 fms, lms;
uint32 cycle_cnt;
uint32 res;
do {
// make sure millis() return the same value before and after
// getting the systick count
fms = millis();
cycle_cnt = systick_get_count();
lms = millis();
} while (lms != fms);
/* SYSTICK_RELOAD_VAL is 1 less than the number of cycles it
actually takes to complete a SysTick reload */
res = (fms * US_PER_MS) +
(SYSTICK_RELOAD_VAL + 1 - cycle_cnt) / CYCLES_PER_MICROSECOND;
return res;
}