DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
rtc.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file rtc.h
6 * @brief I2C real-time-clock driver (DS1307 / DS3231) - a battery-backed time source.
7 *
8 * A DS1307 or DS3231 keeps the wall-clock time running from a coin cell when the ESP32 is off
9 * or offline. This reads it (and can set it) over I2C, and plugs into the time-source chain so
10 * `detws_time_now()` - and the NTP server - can use it: GPS when locked, the RTC when GPS and
11 * the internet are gone, upstream NTP otherwise. Both chips expose the same seven BCD time
12 * registers at address 0x68, so one driver serves both. Zero heap; gated by DETWS_ENABLE_RTC.
13 *
14 * The BCD <-> Unix-epoch conversion (12/24-hour, leap years, range validation) is pure and
15 * host-tested; only the register read/write (Wire) touches hardware.
16 *
17 * @author Douglas Quigg (dstroy0)
18 * @date 2026
19 */
20
21#ifndef DETERMINISTICESPASYNCWEBSERVER_RTC_H
22#define DETERMINISTICESPASYNCWEBSERVER_RTC_H
23
24#include <stddef.h>
25#include <stdint.h>
26
27/** @brief Number of time registers read from the RTC (seconds..year). */
28#define RTC_REG_COUNT 7
29
30/**
31 * @brief Convert the 7 raw RTC time registers (BCD: sec, min, hour, dow, date, month, year) to
32 * a Unix timestamp. Pure - no I2C. Handles both 24-hour and 12-hour (AM/PM) hour encodings and
33 * masks the DS1307 clock-halt / DS3231 century bits.
34 * @param regs the 7 register bytes as read from register 0.
35 * @param epoch out: seconds since 1970-01-01 UTC.
36 * @return true on a valid time; false if a field is out of range (bad/uninitialized RTC).
37 */
38bool rtc_regs_to_epoch(const uint8_t regs[RTC_REG_COUNT], uint32_t *epoch);
39
40/**
41 * @brief Convert a Unix timestamp to the 7 RTC time registers (BCD, 24-hour). Pure - no I2C.
42 * The day-of-week register is filled (1=Mon..7=Sun) for completeness.
43 */
44void rtc_epoch_to_regs(uint32_t epoch, uint8_t regs[RTC_REG_COUNT]);
45
46/** @brief Initialize the I2C bus for the RTC. @return true on a host build (no-op) or on ESP32. */
47bool rtc_begin();
48
49/**
50 * @brief Read the current time from the RTC over I2C.
51 * @return seconds since 1970-01-01 UTC, or 0 if the RTC is absent / holds an invalid time.
52 */
53uint32_t rtc_read_epoch();
54
55/** @brief Set the RTC to @p epoch over I2C. @return true if the write succeeded. */
56bool rtc_set_epoch(uint32_t epoch);
57
58/**
59 * @brief A ::TimeSourceFn wrapper (returns rtc_read_epoch()) to register with
60 * detws_time_source_add(). @return the RTC time, or 0 when unavailable.
61 */
62uint32_t rtc_time_source();
63
64#endif // DETERMINISTICESPASYNCWEBSERVER_RTC_H
uint32_t rtc_time_source()
A TimeSourceFn wrapper (returns rtc_read_epoch()) to register with detws_time_source_add().
#define RTC_REG_COUNT
Number of time registers read from the RTC (seconds..year).
Definition rtc.h:28
uint32_t rtc_read_epoch()
Read the current time from the RTC over I2C.
bool rtc_set_epoch(uint32_t epoch)
Set the RTC to epoch over I2C.
bool rtc_regs_to_epoch(const uint8_t regs[RTC_REG_COUNT], uint32_t *epoch)
Convert the 7 raw RTC time registers (BCD: sec, min, hour, dow, date, month, year) to a Unix timestam...
void rtc_epoch_to_regs(uint32_t epoch, uint8_t regs[RTC_REG_COUNT])
Convert a Unix timestamp to the 7 RTC time registers (BCD, 24-hour). Pure - no I2C....
bool rtc_begin()
Initialize the I2C bus for the RTC.