To help a user, I ended up looking at the source of micros()
http://forums.leaflabs.com/topic.php?id=1082&page=2#post-6792
To save folks some time, here is the source of micros() again:
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 = (ms * US_PER_MS) +
(SYSTICK_RELOAD_VAL + 1 - cycle_cnt) / CYCLES_PER_MICROSECOND;
return res;
}
I think there is a bug.
I think
do {
cycle_cnt = systick_get_count();
ms = millis();
} while (ms != millis());
should be
do {
ms = millis();
cycle_cnt = systick_get_count();
} while (ms != millis());
I think the idea is to detect if systick has wrapped around, and hence incremented millis(), hence the second one would be right.
What do you think?