In development. It changes without notice.
Topic
Monotonic Clock & Deadlines
The time since boot, and deadlines that stay right when it wraps.
A count of 32 bits wraps after about 71 minutes in microseconds and 49 days in milliseconds. A deadline on it is tested with a function made for that, and a window that opens once carries whether it is open (kleidos::
The wall clock is Clock & Time Zone.
- See also
- RTOS Facade, Clock & Time Zone
Files | |
| file | src/ |
| Monotonic time-source implementation (microseconds and milliseconds). | |
| file | src/ |
| Monotonic time-source facade (microseconds and milliseconds since boot). | |
Classes | |
| class | kleidos:: |
A one-shot window on millis32(): armed with a length, then reached. More about kleidos:: | |
Typedefs | |
| using | kleidos:: |
| Test hook signature: millisecond source. | |
| using | kleidos:: |
| Test hook signature: microsecond source. | |
| using | kleidos:: |
| Test hook signature: one spinUs wait. | |
Functions | |
| void | kleidos:: |
| Install a test millisecond source. | |
| void | kleidos:: |
| Remove any installed test source. | |
| void | kleidos:: |
| Install a test microsecond source. | |
| void | kleidos:: |
| Remove any installed microsecond source. | |
| void | kleidos:: |
Route every spinUs to hook instead of spinning. | |
| void | kleidos:: |
| Make spinUs spin on the host clock again. | |
Short busy-waits | |
| constexpr uint32_t | kleidos:: |
| void | kleidos:: |
Busy-wait at least us microseconds without yielding the core. | |
Monotonic time | |
| uint64_t | kleidos:: |
| Microseconds since boot (64-bit, monotonic). | |
| uint64_t | kleidos:: |
| Milliseconds since boot (64-bit, monotonic). | |
| uint32_t | kleidos:: |
| Low 32 bits of the monotonic microsecond counter. | |
| uint32_t | kleidos:: |
| Low 32 bits of the monotonic millisecond counter. | |
| uint32_t | kleidos:: |
| ISR-safe 32-bit microsecond counter (placed in IRAM on target). | |
Sleep-surviving time | |
| uint64_t | kleidos:: |
| Microseconds on the RTC timer (64-bit). | |
| int64_t | kleidos:: |
| The system wall clock in microseconds, whatever its trust. | |
Deadlines across the millis32 wrap | |
| constexpr bool | kleidos:: |
Whether deadlineMs has arrived at nowMs, across the millis32() wrap. | |
| constexpr uint32_t | kleidos:: |
Milliseconds left until deadlineMs, across the millis32() wrap. | |
| constexpr uint32_t | kleidos:: |
Milliseconds since stampMs, across the millis32() wrap. | |
Function Documentation
deadlineReached() #
Whether deadlineMs has arrived at nowMs, across the millis32() wrap.
A deadline stored as millis32() + window wraps with the counter, so a plain nowMs >= deadlineMs fires at once when the sum wraps and never fires when the clock does. The signed difference stays right through both. It holds while nowMs and deadlineMs are less than 2^31 ms (about 24.8 days) apart: a deadline must be armed less than that ahead and polled within that span of falling due.
- Parameters
-
Name Direction Meaning nowMs in Current millis32()reading.deadlineMs in The deadline, a millis32()value.
- Returns
trueoncenowMsis at or pastdeadlineMs.
micros32() #
Low 32 bits of the monotonic microsecond counter.
- Returns
- 32-bit microsecond timestamp; wraps approximately every 71 minutes.
micros32FromIsr() #
ISR-safe 32-bit microsecond counter (placed in IRAM on target).
- Returns
- 32-bit microsecond timestamp suitable for use in ISR context.
millis32() #
Low 32 bits of the monotonic millisecond counter.
- Returns
- 32-bit millisecond timestamp; wraps approximately every 49 days.
monotonicMicros() #
Microseconds since boot (64-bit, monotonic).
- Returns
- 64-bit microsecond count since boot; never wraps.
monotonicMillis() #
Milliseconds since boot (64-bit, monotonic).
- Returns
- 64-bit millisecond count since boot; never wraps.
msSince() #
Milliseconds since stampMs, across the millis32() wrap.
Another task may write a stamp after the caller read nowMs, so the stamp can be a little newer than the reading; that reads as no time at all rather than as a 49-day span. Valid while the two are less than 2^31 ms apart.
- Parameters
-
Name Direction Meaning nowMs in Current millis32()reading.stampMs in When the event happened, a millis32()value.
- Returns
- The milliseconds since
stampMs; 0 when the stamp is newer thannowMs.
msUntil() #
Milliseconds left until deadlineMs, across the millis32() wrap.
Valid over the same 2^31 ms span as deadlineReached.
- Parameters
-
Name Direction Meaning nowMs in Current millis32()reading.deadlineMs in The deadline, a millis32()value.
- Returns
- The milliseconds still to run; 0 once the deadline has arrived.
rtcTimerMicros() #
Microseconds on the RTC timer (64-bit).
Unlike monotonicMicros, this counter keeps running through deep sleep and across software resets: it restarts only when the chip loses power. The difference of two readings taken on either side of a deep sleep is therefore the length of that sleep, whether or not the wall clock was ever set. It runs from the RTC slow clock, so its accuracy is that clock's (the internal RC oscillator drifts with temperature).
- Returns
- RTC-timer microseconds since the last power-on; the native build returns monotonicMicros.
setTestMicrosProvider() #
Install a test microsecond source.
It takes precedence over a millisecond source, and every reading here is derived from it: a fake bus that keeps virtual time in microseconds gives a driver that paces itself on monotonicMicros the same clock it checks the gaps against.
- Parameters
-
Name Meaning provider Function pointer returning a fake microsecond value.
setTestMillisProvider() #
Install a test millisecond source.
- Parameters
-
Name Meaning provider Function pointer returning a fake millisecond value.
setTestSpinHook() #
Route every spinUs to hook instead of spinning.
A fake bus that keeps its own virtual time advances it here, so a test can check that a driver left the gap a chip needs without waiting for it.
- Parameters
-
Name Meaning hook Function receiving each requested wait, in microseconds.
spinUs() #
Busy-wait at least us microseconds without yielding the core.
For the sub-millisecond bus pacing a chip's datasheet asks for between two accesses, such as the 450 µs a BMI270 needs after a register write while its advanced power save is on. A scheduler delay cannot give that: the kernel tick is 1 ms and a one-tick delay can end almost at once. The wait is timed on the microsecond timer, so it holds across a CPU clock change on the other core. The calling core spins for the whole wait, so keep it to a few hundred microseconds; a wait of a millisecond or more belongs to rtos::delayMs.
- Parameters
-
Name Direction Meaning us in Minimum wait in microseconds; 0 returns at once, and anything above kMaxSpinUs is cut to it.
wallClockMicros() #
The system wall clock in microseconds, whatever its trust.
The clock gettimeofday reads: microseconds since the Unix epoch when it was set, since a boot-time origin when it never was. ESP-IDF keeps it through deep sleep on the RTC timer. This is a raw diagnostic read; the clock module (Clock & Time Zone) owns setting it and deciding whether it can be trusted.
- Returns
- Wall-clock microseconds; the native build reads the host's clock.
Variable Documentation
kMaxSpinUs #
Longest wait spinUs performs: the core spins for the whole of it, and no active code path may block longer than 50 ms.