Kleidos API Search

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::platform::clock::Deadline). For lengths there is a count of 64 bits.

const uint32_t t0 = kleidos::platform::clock::millis32();
// ... work ...

The wall clock is Clock & Time Zone.

See also
RTOS Facade, Clock & Time Zone

Files

file src/platform/base/clock.cpp
Monotonic time-source implementation (microseconds and milliseconds).
file src/platform/base/clock.h
Monotonic time-source facade (microseconds and milliseconds since boot).

Classes

class kleidos::platform::clock::Deadline
A one-shot window on millis32(): armed with a length, then reached. More about kleidos::platform::clock::Deadline

Typedefs

usingkleidos::platform::clock::MillisProvider
Test hook signature: millisecond source.
usingkleidos::platform::clock::MicrosProvider
Test hook signature: microsecond source.
usingkleidos::platform::clock::SpinHook
Test hook signature: one spinUs wait.

Functions

voidkleidos::platform::clock::setTestMillisProvider (MillisProvider provider)
Install a test millisecond source.
voidkleidos::platform::clock::clearTestMillisProvider ()
Remove any installed test source.
voidkleidos::platform::clock::setTestMicrosProvider (MicrosProvider provider)
Install a test microsecond source.
voidkleidos::platform::clock::clearTestMicrosProvider ()
Remove any installed microsecond source.
voidkleidos::platform::clock::setTestSpinHook (SpinHook hook)
Route every spinUs to hook instead of spinning.
voidkleidos::platform::clock::clearTestSpinHook ()
Make spinUs spin on the host clock again.

Short busy-waits

constexpr uint32_tkleidos::platform::clock::kMaxSpinUs
voidkleidos::platform::clock::spinUs (uint32_t us)
Busy-wait at least us microseconds without yielding the core.

Monotonic time

uint64_tkleidos::platform::clock::monotonicMicros ()
Microseconds since boot (64-bit, monotonic).
uint64_tkleidos::platform::clock::monotonicMillis ()
Milliseconds since boot (64-bit, monotonic).
uint32_tkleidos::platform::clock::micros32 ()
Low 32 bits of the monotonic microsecond counter.
uint32_tkleidos::platform::clock::millis32 ()
Low 32 bits of the monotonic millisecond counter.
uint32_tkleidos::platform::clock::micros32FromIsr ()
ISR-safe 32-bit microsecond counter (placed in IRAM on target).

Sleep-surviving time

uint64_tkleidos::platform::clock::rtcTimerMicros ()
Microseconds on the RTC timer (64-bit).
int64_tkleidos::platform::clock::wallClockMicros ()
The system wall clock in microseconds, whatever its trust.

Deadlines across the millis32 wrap

constexpr boolkleidos::platform::clock::deadlineReached (uint32_t nowMs, uint32_t deadlineMs)
Whether deadlineMs has arrived at nowMs, across the millis32() wrap.
constexpr uint32_tkleidos::platform::clock::msUntil (uint32_t nowMs, uint32_t deadlineMs)
Milliseconds left until deadlineMs, across the millis32() wrap.
constexpr uint32_tkleidos::platform::clock::msSince (uint32_t nowMs, uint32_t stampMs)
Milliseconds since stampMs, across the millis32() wrap.

Function Documentation

deadlineReached() #

bool kleidos::platform::clock::deadlineReached(uint32_t nowMs, uint32_t deadlineMs)
constexpr

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
NameDirectionMeaning
nowMsinCurrent millis32() reading.
deadlineMsinThe deadline, a millis32() value.
Returns
true once nowMs is at or past deadlineMs.

micros32() #

uint32_t kleidos::platform::clock::micros32()

Low 32 bits of the monotonic microsecond counter.

Returns
32-bit microsecond timestamp; wraps approximately every 71 minutes.

micros32FromIsr() #

uint32_t kleidos::platform::clock::micros32FromIsr()

ISR-safe 32-bit microsecond counter (placed in IRAM on target).

Returns
32-bit microsecond timestamp suitable for use in ISR context.

millis32() #

uint32_t kleidos::platform::clock::millis32()

Low 32 bits of the monotonic millisecond counter.

Returns
32-bit millisecond timestamp; wraps approximately every 49 days.

monotonicMicros() #

uint64_t kleidos::platform::clock::monotonicMicros()

Microseconds since boot (64-bit, monotonic).

Returns
64-bit microsecond count since boot; never wraps.

monotonicMillis() #

uint64_t kleidos::platform::clock::monotonicMillis()

Milliseconds since boot (64-bit, monotonic).

Returns
64-bit millisecond count since boot; never wraps.

msSince() #

uint32_t kleidos::platform::clock::msSince(uint32_t nowMs, uint32_t stampMs)
constexpr

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
NameDirectionMeaning
nowMsinCurrent millis32() reading.
stampMsinWhen the event happened, a millis32() value.
Returns
The milliseconds since stampMs; 0 when the stamp is newer than nowMs.

msUntil() #

uint32_t kleidos::platform::clock::msUntil(uint32_t nowMs, uint32_t deadlineMs)
constexpr

Milliseconds left until deadlineMs, across the millis32() wrap.

Valid over the same 2^31 ms span as deadlineReached.

Parameters
NameDirectionMeaning
nowMsinCurrent millis32() reading.
deadlineMsinThe deadline, a millis32() value.
Returns
The milliseconds still to run; 0 once the deadline has arrived.

rtcTimerMicros() #

uint64_t kleidos::platform::clock::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() #

void kleidos::platform::clock::setTestMicrosProvider(MicrosProvider provider)

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
NameMeaning
providerFunction pointer returning a fake microsecond value.

setTestMillisProvider() #

void kleidos::platform::clock::setTestMillisProvider(MillisProvider provider)

Install a test millisecond source.

Parameters
NameMeaning
providerFunction pointer returning a fake millisecond value.

setTestSpinHook() #

void kleidos::platform::clock::setTestSpinHook(SpinHook hook)

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
NameMeaning
hookFunction receiving each requested wait, in microseconds.

spinUs() #

void kleidos::platform::clock::spinUs(uint32_t us)

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
NameDirectionMeaning
usinMinimum wait in microseconds; 0 returns at once, and anything above kMaxSpinUs is cut to it.

wallClockMicros() #

int64_t kleidos::platform::clock::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 #

uint32_t kleidos::platform::clock::kMaxSpinUs
inlineconstexpr

Longest wait spinUs performs: the core spins for the whole of it, and no active code path may block longer than 50 ms.