Files
ZephCore/zephcore/adapters/clock/ZephyrRTCDiscover.h
T
Pieter ViljoenandClaude Opus 5.5 9b68b7e1ae rtc: write the RTC time only for years 2000-2099
ZephCore writes the year as two BCD digits with no century, as y % 100.
A time before 2000, accepted by a node whose clock had no time yet, was
stored as a year near the end of the century and restored at the next
boot above the 2025 floor: `time 900000000` (1998) came back as 2098.

rtc_time_block() now fails outside 2000-2099, and zephcore_rtc_save()
then logs and writes no time, for every chip; the chip keeps the time it
has. An RV3028 write retry stops if its run-on time leaves the range.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 07:09:40 -07:00

50 lines
1.7 KiB
C

/*
* SPDX-License-Identifier: MIT
*
* Boot-time hardware-RTC auto-discovery (compact raw-I2C reader).
*
* Probes every I2C RTC chip declared with the "zephcore,rtc-i2c" binding
* (boards/common/rtc-i2c.dtsi, or a board's own overlay). Chips that aren't
* physically present fail the probe and are skipped — like the environment
* sensors.
*
* If a present chip holds a valid time, zephcore_rtc_restore() returns it so
* the soft clock can be seeded at boot (shown tagged "L" — local). Every
* ZephyrRTCClock::setCurrentTime() within 2000-2099 writes it back via
* zephcore_rtc_save(), so time survives the next power-off.
*/
#pragma once
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Probe the declared RTC chips in order, stopping at the first that holds a
* sane time (year >= 2025 and its power-loss flag clear): store its Unix
* epoch in *epoch_out and return true. The first chip found (valid time or
* not) is remembered as the write-back target. Returns false if none is
* present or none holds a trustworthy time.
*
* A descriptor with rv3028-eeprom-config also has that config stored in the
* chip's EEPROM, retried on the system work queue if it fails.
*/
bool zephcore_rtc_restore(uint32_t *epoch_out);
/*
* Persist an authoritative epoch to the discovered RTC chip and clear its
* power-loss flag. No-op if no RTC was discovered. Intended for real syncs
* (GPS/app/CLI), not per-packet clock nudges. Call only from the system work
* queue: an unconfirmed write is repeated there and shares state with this
* call. A time outside 2000-2099 is not written.
*/
void zephcore_rtc_save(uint32_t epoch);
#ifdef __cplusplus
}
#endif