Earlier mlu reported 0.9usec per call to micros() on 96MHz Teensy 3.0. I tried a similar test on Teensy 3.1 (using Arduino IDE 1.8.4 and Teensyduino 1.38):
Code:
void loop() {
long looptime;
long starttime = micros();
for (int n=0;n<1000;n++)
looptime=micros()-starttime;
String s = String(looptime,DEC) + "\n";
Serial.print(s);
delay(500);
}
96 MHz : 333 to 337 usec
72 MHz : 444 to 448 usec
(The time scales with inverse processor speed, unsurprisingly.)
So that's almost 3 times as fast as reported by mlu for the 3.0.
The scope trace shows occasional breaks of 1 usec and 1.7 usec (at 72 MHz), presumably due to other interrupts, or due to micros() periodically taking a different execution path. This implies that an approach like this is not guaranteed to see every microsecond value from micros(). And even though most of the time the resolution is better than 0.5 usec, sometimes it's only about 2 usec.
Another experiment:
Code:
elapsedMicros elapsed;
void loop() {
long next_elapsed = 0;
cli();
while (true) {
while (elapsed < next_elapsed ) { }
next_elapsed += 1;
digitalWriteFast(ledPin, HIGH); // set the LED on
digitalWriteFast(ledPin, LOW); // set the LED off
}
}
At 72MHz, this produces a pulse every 1 usec, with occasional lapses of say 0.5 to 1.5 usec, but the total count of pulses catches up within a few usec. The catchup happens because next_elapsed is relentlessly incremented by 1, and the loop time most of the time is well below 1 usec.
So, the cli() didn't get rid of the jitter. On the other hand, I was surprised to see that this worked (at least for several 10's of minutes) despite the cli(). I had expected some counter to rollover and not get serviced, debilitating elapsedMicros.