The kernel keeps one monotonic clock, the tick count, derived from the
system clock source, and the wall clock on top of it. pbl/kernel/time.h
reads them and the CPU cycle counter; pbl/kernel/delay.h waits on them;
pbl/kernel/timezone.h converts the wall clock to local time.
Two devices from pbl/drivers/timer.h keep time, the roles
SYSTEM_CLOCKSOURCE and SYSTEM_CLOCKEVENT the board gives:
The clock source is a free-running counter. The kernel extends it to 64 bits and converts it to ticks in fixed point, so its rate need not divide the tick rate and may change at run time, as a calibrated RC oscillator’s does, without time jumping.
The clock event device raises an interrupt once, a given number of its cycles from now. While threads run, the kernel programs it for the next tick; when the CPU idles, for the next timeout. Ticks are counted from the clock source on every event, so a late or missed interrupt loses no time.
Drivers know nothing about ticks: a clock source reads its counter, a clock event device programs its compare register.
SoC |
Clock source |
Clock event device |
|---|---|---|
nRF52 |
RTC1, 32.768 kHz |
RTC1 compare |
SF32LB52 |
LPTIM1, calibrated RC10K |
LPTIM1 compare, wakes from deep sleep |
QEMU |
QEMU RTC 1 kHz counter |
SysTick |
POSIX |
host monotonic clock |
host thread |
pbl_uptime_ticks() and pbl_uptime_ms() count from the scheduler start in
64 bits, so they do not wrap during the life of a device. The 32-bit
pbl_tick_t remains for relative timeouts only.
pbl_cycle_get_32() reads the CPU cycle counter (DWT CYCCNT on Cortex-M) at
pbl_cycles_per_sec(), the current CPU clock, which the SoC and its clock
driver report with pbl_cycles_per_sec_set(). Use it to time short sections:
it wraps within seconds and stops while the CPU sleeps.
pbl_realtime() and pbl_realtime_ms() return UTC time since the epoch,
kept as an offset from the uptime with microsecond resolution: the wall clock
runs at the clock source rate and only jumps when pbl_realtime_set() sets
it. The kernel keeps it in RAM only. The clock service restores it at boot
from the board’s RTC (pbl/drivers/rtc.h), and writes it back whenever the
time is set and every CONFIG_SERVICE_CLOCK_RTC_SAVE_INTERVAL seconds.
SoC |
RTC |
|---|---|
nRF52 |
Retained RAM: the time last saved, which does not count while off |
SF32LB52 |
Calendar RTC on the calibrated RC10K |
QEMU |
QEMU RTC, following the host clock |
POSIX |
Host clock |
The kernel keeps one time zone for the whole system, set with
pbl_tz_set(): its offset from UTC, its standard and daylight saving time
(DST) abbreviations, the offset DST adds, and two DST periods, the current or
next one and the one before it, so recent past times convert correctly too.
Until it is set, local time is UTC. Whoever learns the time zone, e.g. from a
paired phone or a time zone database, sets it; the kernel does not compute DST
rules itself.
pbl_localtime_r() breaks a UTC time down into local time, with
tm_isdst, tm_gmtoff (DST included) and tm_zone filled in.
pbl_tz_utc_to_local() and pbl_tz_local_to_utc() convert seconds;
pbl_tz_is_dst(), pbl_tz_gmtoff(), pbl_tz_dst_offset() and
pbl_tz_dst_start() let calendar code handle DST transitions. Local times in
the hour repeated when DST ends are ambiguous: pbl_tz_local_to_utc() picks
the earlier of the two.
Need |
Use |
Resolution |
Context |
|---|---|---|---|
A few microseconds, or no thread |
|
cycles |
anywhere |
Microseconds, being late is fine |
|
one tick |
thread |
Milliseconds and longer |
|
one tick |
thread |
A timeout or a deadline in ticks |
|
one tick |
thread |
A condition, spinning |
|
cycles |
anywhere |
A condition, sleeping |
|
one tick |
thread |
The pbl_sleep_*() and pbl_busy_wait_us() calls wait at least the requested
time, which is what hardware timings from a datasheet need: sleeps round up to
whole ticks and add one for the tick already in progress. pbl_thread_sleep()
counts tick boundaries instead, so its first tick may be partial.
The polling macros return 0 once the condition holds and -ETIMEDOUT
otherwise, evaluating it once more after the timeout so that preemption cannot
turn a ready condition into a false timeout, as Linux read_poll_timeout()
does.
Busy waiting keeps the CPU awake and starves lower priority threads; past a tick or so, sleep instead.
Generated from PebbleOS 7416f78a1. This page is maintained in the pebbleos repository: docs/kernel/time.md.