Kleidos API Search

In development. It changes without notice.

Topic

GPS Time Sync

The time from a GPS receiver: one sync when the owner asks for it, and the receiver at rest otherwise.

A task opens the receiver, accepts a time only from a valid fix, and sets the clock. The receiver's supply is not switched with the board, so a sync and a sleep both end by parking it: putting it in its lowest state and hearing it fall silent. On a board without a receiver every function is there and does nothing.

See also
Hardware Abstraction Layer, GPS Receiver Drivers, Clock & Time Zone

Files

file src/hal/gps/gps_sync_logic.h
Pure one-shot GPS time-sync state machine (host-testable core).
file src/hal/gps/gps_time_sync.h
One-shot GPS time-sync engine (device wiring over kleidos::hal::gps::SyncLogic).

Classes

class kleidos::hal::gps::SyncLogic
One-shot GPS time-sync state machine (pure logic; see file docs). More about kleidos::hal::gps::SyncLogic
struct kleidos::hal::gps::ParkReport
What the last verified park found, for a diagnostic. More about kleidos::hal::gps::ParkReport
class kleidos::hal::gps::Session
A held-open receiver, parked again when the scope ends. More about kleidos::hal::gps::Session
struct kleidos::hal::gps::GpsDiag
Live receiver-state snapshot gathered by diagnose. More about kleidos::hal::gps::GpsDiag

Typedefs

usingkleidos::hal::gps::GpsDiagLineSink
Callback invoked once per raw NMEA line (NUL-terminated, CRLF stripped).
usingkleidos::hal::gps::GpsDiagProgressSink

Enumerations

enum class kleidos::hal::gps::SyncPhase : uint8_t { kleidos::hal::gps::SyncPhase::Idle, kleidos::hal::gps::SyncPhase::Waiting, kleidos::hal::gps::SyncPhase::Done, kleidos::hal::gps::SyncPhase::TimedOut, kleidos::hal::gps::SyncPhase::Cancelled }
Lifecycle phase of a one-shot GPS time sync. More about kleidos::hal::gps::SyncPhase
enum class kleidos::hal::gps::ParkCost : uint8_t { kleidos::hal::gps::ParkCost::Cheap , kleidos::hal::gps::ParkCost::Resolved }
What a park may spend to find out which standby frame to send. More about kleidos::hal::gps::ParkCost

Functions

boolkleidos::hal::gps::available ()
True when this board has a GPS receiver with wired UART pins.
boolkleidos::hal::gps::portOpen ()
True while something holds the receiver's UART port open.
boolkleidos::hal::gps::syncActive ()
True while a one-shot time sync owns the receiver.
boolkleidos::hal::gps::parked ()
True while the module is in standby, as the verified park last heard it.
boolkleidos::hal::gps::park (ParkCost cost)
Park the receiver, and know it is parked.
boolkleidos::hal::gps::parkBeforeSleep (uint32_t budgetMs)
Park before a deep sleep, waiting for the proof within a budget.
ParkReportkleidos::hal::gps::lastPark ()
Report what the last verified park found.
boolkleidos::hal::gps::resume ()
Re-open the receiver after park, without arming a sync.
boolkleidos::hal::gps::forgetIdentity ()
Forget the persisted receiver identity, port-safely.
boolkleidos::hal::gps::syncBegin (uint32_t timeoutMs)
Power the receiver and arm a one-shot sync attempt.
SyncPhasekleidos::hal::gps::syncPoll ()
Drain pending UART bytes, advance the machine, commit + power down.
voidkleidos::hal::gps::syncCancel ()
Cancel a running sync immediately and power the receiver down.
uint32_tkleidos::hal::gps::syncElapsedMs ()
Milliseconds elapsed since syncBegin (clamped to the timeout).
uint32_tkleidos::hal::gps::syncTimeoutMs ()
The deadline configured by syncBegin.
GpsDiagkleidos::hal::gps::diagnose (uint32_t windowMs, GpsDiagLineSink onLine, uint32_t baudOverride=0, GpsDiagProgressSink onProgress=nullptr)
Power the receiver, probe its live state for a bounded window, off.

Variables

constexpr uint32_tkleidos::hal::gps::kDefaultSyncTimeoutMs
Default sync deadline: generous headroom over a cold-start TTFF.

Accepted fix year range

Mirrors the manual set-clock widget range (ui::kDtYearMin / ui::kDtYearMax). The NMEA parser alone admits RMC years 2000-2099 and setClockEpochValid only floors at 2001, so the sync engine must bound the year itself before any clock write.

constexpr uint16_tkleidos::hal::gps::kMinFixYear
Oldest acceptable fix year.
constexpr uint16_tkleidos::hal::gps::kMaxFixYear
Newest acceptable fix year.

Back to the top

Typedef Documentation

GpsDiagProgressSink #

using kleidos::hal::gps::GpsDiagProgressSink

Callback invoked at each progress tick with the elapsed ms and running tally — turns diagnose into a live acquisition monitor over a long window.

Back to the top

Enumeration Type Documentation

ParkCost #

enum class kleidos::hal::gps::ParkCost : uint8_t

What a park may spend to find out which standby frame to send.

Enumerator
Cheap 

One step of the verified park per call (gps_park_logic.h), never more than the few milliseconds a frame takes to leave the UART: the idle tick calls it every frame until the receiver is heard silent. It listens before it writes, sends the stored family's frame only to a module that is talking, identifies an unknown one from its own stream, and keeps what it learned.

Resolved 

Resolve the fitted model through the factory first, which may listen (a fast-path window, or a full baud sweep of up to four rates), then prove the park, blocking for up to a few seconds. For an operator who asked for one.

SyncPhase #

enum class kleidos::hal::gps::SyncPhase : uint8_t

Lifecycle phase of a one-shot GPS time sync.

Enumerator
Idle 

No sync ever started (terminal phases latch; only start() re-arms).

Waiting 

Powered and waiting for a valid time fix.

Done 

A valid fix was decoded; the clock value is ready.

TimedOut 

No fix within the deadline; abort.

Cancelled 

User abort.

Back to the top

Function Documentation

available() #

bool kleidos::hal::gps::available()

True when this board has a GPS receiver with wired UART pins.

Return values
ValueMeaning
trueHAS_GPS build with valid TX/RX pin assignments.
falseNo receiver on this variant (all other calls are no-ops).

diagnose() #

GpsDiag kleidos::hal::gps::diagnose(uint32_t windowMs, GpsDiagLineSink onLine, uint32_t baudOverride = 0, GpsDiagProgressSink onProgress = nullptr)

Power the receiver, probe its live state for a bounded window, off.

One-shot read-only diagnostic (does NOT set the clock). Powers the GPS UART, polls a UBX-MON-VER identity request, then for windowMs reads the stream: echoes every raw NMEA line through onLine (may be nullptr) and tallies the returned GpsDiag. Always powers the receiver back down on exit, on every path (mirrors the sync power contract). Refuses (GpsDiag::busy) while a real syncBegin is active.

Parameters
NameDirectionMeaning
windowMsinCapture window in milliseconds.
onLineinPer-line sink for raw NMEA echo, or nullptr.
baudOverrideinUART baud to probe at, or 0 for the driver default (9600) — lets the console sweep candidate rates when a module ships at a non-default baud.
onProgressinProgress sink called ~every 5 s of the window with the running tally, or nullptr — lets a long window act as a live acquisition monitor without power-cycling the receiver between reads.
Returns
The gathered snapshot.

< NMEA 0183 maximum sentence length.

forgetIdentity() #

bool kleidos::hal::gps::forgetIdentity()

Forget the persisted receiver identity, port-safely.

What a re-detect wants: the receiver is parked and its port closed first, so the next createGps sweeps a free UART instead of re-opening one this unit still believes it holds. Erases the stored receiver model and baud rate.

Refused while a sync owns the receiver, rather than cancelling it as a side effect: the park this runs first would otherwise abort a time sync the user started, and the surface that asked for a re-detect would report one it did not do. The refusal lives here instead of in each caller, because three of the four had no guard of their own.

Return values
ValueMeaning
trueThe identity is gone and the receiver is parked.
falseA sync is in flight; nothing was erased or touched.

lastPark() #

ParkReport kleidos::hal::gps::lastPark()

Report what the last verified park found.

Returns
The report; a default one where no receiver is fitted.

park() #

bool kleidos::hal::gps::park(ParkCost cost)

Park the receiver, and know it is parked.

Independent of a sync, and the only thing that takes the module out of acquisition — no rail the board switches reaches the receiver's supply. A sync in flight is cancelled first (so the machine is never left latched in SyncPhase::Waiting), and a held port is powered down; either sends the standby frame, and the following calls prove it landed. Free while parked holds — it returns at once without touching the bus.

Parameters
NameDirectionMeaning
costinWhat this park may spend (ParkCost). No default: ParkCost::Resolved blocks for seconds, so a caller states what it is willing to spend.
Return values
ValueMeaning
trueParked, or (ParkCost::Cheap) a park is under way.
falseNo receiver on this board, the port could not be opened, or the receiver kept talking through every standby frame.

parkBeforeSleep() #

bool kleidos::hal::gps::parkBeforeSleep(uint32_t budgetMs)

Park before a deep sleep, waiting for the proof within a budget.

Steps the verified park until the receiver is heard silent, feeding the task watchdog, for at most budgetMs, then releases the port whatever the outcome. Stores no identity it learns (the sleep path must not open NVS). Nearly always free: the idle tick verified the park long before the board went idle enough to sleep.

Parameters
NameDirectionMeaning
budgetMsinLongest wait, in milliseconds.
Return values
ValueMeaning
trueThe receiver sleeps parked.
falseIt could not be heard silent in time, or no receiver is fitted.

parked() #

bool kleidos::hal::gps::parked()

True while the module is in standby, as the verified park last heard it.

Set only by a park that listened and heard the receiver silent (gps_park_logic.h): already silent before anything was written, or silent after its standby frame. Cleared by everything that may drive the UART before it does so, and by a power-down, which sends the frame but leaves the proof to the next park. Not a read-back on demand: the module has no status line and a question would wake it, so a repeated park is free while this holds.

Return values
ValueMeaning
trueThe receiver was heard silent and nothing has driven it since.
falseIt is awake, not yet verified, or no receiver is fitted.

portOpen() #

bool kleidos::hal::gps::portOpen()

True while something holds the receiver's UART port open.

The one predicate that is safe to gate a park on: it reads this unit's own bookkeeping and touches no bus. True for a running sync, for a live Session, and after a resume — which is the distinction syncActive does not make, and the reason the two are separate names.

Return values
ValueMeaning
trueThe port is held; the module is awake.
falseNothing holds it (parked, or never opened).

resume() #

bool kleidos::hal::gps::resume()

Re-open the receiver after park, without arming a sync.

Re-resolves the driver, which re-opens the UART and nudges a u-blox module out of backup. A genuine restore rather than a rail poke: the receiver is answering again when this returns true, and portOpen says so, so the idle park stands off until something closes it again.

Return values
ValueMeaning
trueThe port is open and the module answered.
falseNo receiver on this board, something already holds the port, or the open failed.

syncActive() #

bool kleidos::hal::gps::syncActive()

True while a one-shot time sync owns the receiver.

Return values
ValueMeaning
truesyncBegin armed a sync that has not reached a terminal phase.
falseNo sync in flight (the port may still be held — see portOpen).

syncBegin() #

bool kleidos::hal::gps::syncBegin(uint32_t timeoutMs)

Power the receiver and arm a one-shot sync attempt.

Parameters
NameDirectionMeaning
timeoutMsinDeadline in milliseconds (see kDefaultSyncTimeoutMs).
Return values
ValueMeaning
trueUART up, machine armed; poll with syncPoll.
falseAlready active, something else holds the port, no GPS on this board, or UART begin failed (the pins are restored to their idle input state).

syncCancel() #

void kleidos::hal::gps::syncCancel()

Cancel a running sync immediately and power the receiver down.

Note
Not self-serializing: every sync call — including the deep-sleep hook's cancel — runs on the owning Core-1 loop task.

syncElapsedMs() #

uint32_t kleidos::hal::gps::syncElapsedMs()

Milliseconds elapsed since syncBegin (clamped to the timeout).

Returns
Elapsed milliseconds of the current/last sync attempt.

syncPoll() #

SyncPhase kleidos::hal::gps::syncPoll()

Drain pending UART bytes, advance the machine, commit + power down.

On the first valid fix: atomic rtc::setDateTime write, source tag rtc::ClockSource::Gps, then power-down. On timeout: power-down. Call from the owning task's loop while the phase is SyncPhase::Waiting.

Returns
The phase after this poll (terminal phases are latched).

syncTimeoutMs() #

uint32_t kleidos::hal::gps::syncTimeoutMs()

The deadline configured by syncBegin.

Returns
Timeout in milliseconds (0 when never started).

Back to the top