ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
rtc.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#ifndef PROTOCORE_RTC_H
5#define PROTOCORE_RTC_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
9EMBED_BEGIN_DECLS
10
11/**
12 * @file rtc.h
13 * @brief I2C real-time-clock driver (DS1307 / DS3231) - a battery-backed time source.
14 *
15 * A DS1307 or DS3231 keeps the wall-clock time running from a coin cell when the ESP32 is off
16 * or offline. This reads it (and can set it) over I2C, and plugs into the time-source chain so
17 * `protocore_time_now()` - and the NTP server - can use it: GPS when locked, the RTC when GPS and
18 * the internet are gone, upstream NTP otherwise. Both chips expose the same seven BCD time
19 * registers at address 0x68, so one driver serves both. Zero heap; gated by PROTOCORE_ENABLE_RTC.
20 *
21 * The BCD <-> Unix-epoch conversion (12/24-hour, leap years, range validation) is pure and
22 * host-tested; only the register read/write touches hardware, over the shared I2C bus owner.
23 *
24 * @c work is PROTOCORE_I2C_DEVICE_BORROW bytes the CALLER took, at an address it knows. It is not held past the call,
25 * so nothing here aliases it. How those bytes are carved is this module's and is never named here.
26 *
27 * @author Douglas Quigg (dstroy0)
28 * @date 2026
29 */
30
31#define RTC_REG_COUNT 7
32
33/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
34typedef struct
35{
36 embed_bool (*regs_to_epoch)(const uint8_t *, const uint8_t *, uint32_t *);
37 void (*epoch_to_regs)(const uint8_t *, uint32_t, uint8_t *);
38 embed_bool (*begin)(const uint8_t *);
39 uint32_t (*read_epoch)(uint8_t *);
40 embed_bool (*set_epoch)(uint8_t *, uint32_t);
41} RtcNs;
42PROTOCORE_NS_LAYOUT(RtcNs, regs_to_epoch, epoch_to_regs, begin, read_epoch, set_epoch);
43
44/**
45 * @brief Convert the 7 raw RTC time registers (BCD: sec, min, hour, dow, .
46 * @param work PROTOCORE_RTC_BORROW bytes the caller took. Not held past the call.
47 * @param regs the 7 register bytes as read from register 0 RTC_REG_COUNT bytes
48 * @param epoch out: seconds since 1970-01-01 UTC
49 * @return EMBED_TRUE on success.
50 */
51embed_bool protocore_rtc_regs_to_epoch(const uint8_t *work, const uint8_t *regs, uint32_t *epoch);
52/**
53 * @brief Convert a Unix timestamp to the 7 RTC time registers (BCD, .
54 * @param work PROTOCORE_RTC_BORROW bytes the caller took. Not held past the call.
55 * @param epoch Epoch
56 * @param regs RTC_REG_COUNT bytes
57 */
58void protocore_rtc_epoch_to_regs(const uint8_t *work, uint32_t epoch, uint8_t *regs);
59/**
60 * @brief Initialize the I2C bus for the RTC. true; with no bus seam it is a .
61 * @param work PROTOCORE_RTC_BORROW bytes the caller took. Not held past the call.
62 * @return EMBED_TRUE on success.
63 */
64embed_bool protocore_rtc_begin(const uint8_t *work);
65/**
66 * @brief Read the current time from the RTC over I2C.
67 * @param work PROTOCORE_RTC_BORROW bytes the caller took. Not held past the call.
68 * @return The uint32_t.
69 */
70uint32_t protocore_rtc_read_epoch(uint8_t *work);
71/**
72 * @brief Set the RTC to epoch over I2C. true if the write succeeded.
73 * @param work PROTOCORE_RTC_BORROW bytes the caller took. Not held past the call.
74 * @param epoch Epoch
75 * @return EMBED_TRUE on success.
76 */
77embed_bool protocore_rtc_set_epoch(uint8_t *work, uint32_t epoch);
78/**
79 * @brief The RTC as a ::TimeSourceFn: protocore_rtc_read_epoch() over this module's own borrow.
80 * @return the RTC's Unix time, or 0 when it cannot be read.
81 */
83
84/**
85 * @brief The PROTOCORE_I2C_DEVICE_BORROW bytes this module's state lives in.
86 *
87 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
88 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
89 * walks, so the state lasts the life of the program.
90 *
91 * @return the span.
92 */
93uint8_t *protocore_rtc_span(void);
94
95/** @brief Module namespace. */
97 .epoch_to_regs = protocore_rtc_epoch_to_regs,
98 .begin = protocore_rtc_begin,
99 .read_epoch = protocore_rtc_read_epoch,
100 .set_epoch = protocore_rtc_set_epoch};
101
102EMBED_END_DECLS
103
104#endif // PROTOCORE_RTC_H
#define PROTOCORE_UNUSED
Suppress the unused warning. Every module ends in a namespace most callers only partly use.
Definition ns_contract.h:36
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
embed_bool protocore_rtc_regs_to_epoch(const uint8_t *work, const uint8_t *regs, uint32_t *epoch)
Convert the 7 raw RTC time registers (BCD: sec, min, hour, dow, .
uint32_t protocore_rtc_read_epoch(uint8_t *work)
Read the current time from the RTC over I2C.
embed_bool protocore_rtc_begin(const uint8_t *work)
Initialize the I2C bus for the RTC. true; with no bus seam it is a .
void protocore_rtc_epoch_to_regs(const uint8_t *work, uint32_t epoch, uint8_t *regs)
Convert a Unix timestamp to the 7 RTC time registers (BCD, .
uint32_t protocore_rtc_time_source(void)
The RTC as a TimeSourceFn: protocore_rtc_read_epoch() over this module's own borrow.
embed_bool protocore_rtc_set_epoch(uint8_t *work, uint32_t epoch)
Set the RTC to epoch over I2C. true if the write succeeded.
uint8_t * protocore_rtc_span(void)
The PROTOCORE_I2C_DEVICE_BORROW bytes this module's state lives in.
Dispatch table. Addressed by offset, so the layout is asserted below.
Definition rtc.h:35
embed_bool(* set_epoch)(uint8_t *, uint32_t)
Definition rtc.h:40
uint32_t(* read_epoch)(uint8_t *)
Definition rtc.h:39
embed_bool(* regs_to_epoch)(const uint8_t *, const uint8_t *, uint32_t *)
Definition rtc.h:36
embed_bool(* begin)(const uint8_t *)
Definition rtc.h:38
void(* epoch_to_regs)(const uint8_t *, uint32_t, uint8_t *)
Definition rtc.h:37