The routines:
Code: Select all
// Get an initial time, for use as a baseline
__attribute__((always_inline))
uint32_t getBaseTime()
{
return xthal_get_ccount();
}
// Busy-wait until at least the given amount of time has elapsed. Note that if that
// amount of time has already elapsed, returns immediately.
// NOTE: won't work if you have already waited more than about 17 seconds since the baseline, due to counter roll-over.
__attribute__((always_inline))
void waitForElapsedNs(const uint32_t baseline, const uint32_t elapsedTime_ns)
{
const uint32_t cyclesPerUs = XT_CLOCK_FREQ/(1000*1000);
const uint32_t elapsedCcounts = (elapsedTime_ns > 1000*1000) ? (elapsedTime_ns / 1000 * cyclesPerUs) : ((elapsedTime_ns * cyclesPerUs) / 1000);
while ((xthal_get_ccount()-baseline) < elapsedCcounts) {};
}
Code: Select all
uint32_t base = getBaseTime();
unsigned long startTime = micros();
waitForElapsedNs(base, 3000);
unsigned long plus3us = micros();
waitForElapsedNs(base, 3000*1000);
unsigned long plus3ms = micros();
waitForElapsedNs(base, 3000);
unsigned long immediately = micros();
waitForElapsedNs(base, 1000*1000*1000);
unsigned long plus1s = micros();
waitForElapsedNs(base, 3000);
unsigned long another = micros();
Serial.print("3us later "); Serial.println(plus3us - startTime);
Serial.print("3ms laster "); Serial.println(plus3ms - startTime);
Serial.print("Immediately return "); Serial.println(immediately - startTime);
Serial.print("1 second "); Serial.println(plus1s - startTime);
Serial.print("Immediately "); Serial.println(another - startTime);
Thoughts?
P.S. The micros() calls are really quick, so don't seem to be the issue.