Description
Hardware:
Board: Adafruit Huzzah32 Feather
Core Installation/update date: 19-Feb-2018
IDE name: Arduino 1.8.5
Flash Frequency: 80Mhz
Upload Speed: 921600
Description:
I am trying to use a run-of-the-mill Arduino e-paper display library that happens to watch a certain "busy" pin, and reports debug info via Serial.printf() when the pin appears busy for too long. The code is a delay(1) spin loop, and it is almost instantly reporting timeout.
This caused me to write some of my own debug code to test the behavior of the micros() implementation in the HAL.
Here is the code test code:
void test() {
unsigned long startUs = micros();
unsigned long startMs = millis();
while (millis() < startMs + 10000)
delay(1);
unsigned long nowUs = micros();
unsigned long nowMs = millis();
Serial.printf("milliseconds elapsed: %ld\n", nowMs - startMs);
Serial.printf("microseconds elapsed: %ld\n", nowUs - startUs);
}
When I call this test() function near program initialization, this is what I get:
milliseconds elapsed: 10000
microseconds elapsed: 9999563
...which is correct, of course. But later, at the moment I get one of these timeouts, I add a call to this test method, which then returns the following amazingly strange result:
milliseconds elapsed: 10000
microseconds elapsed: 27895357
The value of microseconds elapsed in that period of time is quite repeatable - i.e. in various calls it will get values such as 27895064, 27895697, 27895548, and so on.
Again, if I just do the call to the test function in other parts of the code - even after this strange result has happened - it returns the correct value of ~10k mS elapsed and ~10M uS elapsed.
This is just a simple display driver that is using SPI, and there are no ISRs involved. It's just sending commands to the SPI device and is waiting in the delay(1) spin loop until they return.
I'm looking for some feedback here as to what might potentially be happening to make the microsecond clock appear to be counting at 2.7x faster than it should be, but only for brief instants. So strange.
Thanks in advance for any ideas you might have.