fix(m1): add rtc time sources, responsive status rendering, and key documentation (#5)

## Summary

Fixes three user-reported M1 defects (root causes pre-diagnosed):

1. **RTC permanently invalid** (`-` on screen, `"utc":""` in JSONL, 228x
`ESP_ERR_INVALID_CRC`):
nothing ever wrote the PCF85063, so after power loss its OS (oscillator
stopped) bit stays set.
- `board_service::set_rtc_time(int64_t)`: gmtime_r -> BCD -> multi-byte
I2C write of registers
0x04-0x0A with seconds bit7=0 (clears OS), then re-read confirms; I2C
access serialized
against `read_rtc` with a FreeRTOS mutex (console/SNTP context safe).
- `time_sync` service: on Wi-Fi CONNECTED starts `esp_netif_sntp`
(ntp.aliyun.com +
pool.ntp.org, `CONFIG_LWIP_SNTP_MAX_SERVERS=2`), and once `time()`
passes 2020-01-01 writes
     the RTC once per sync (`rtc synced from sntp`).
- Console: `rtc status` (valid/unix/utc/OS bit), `rtc raw` (0x00-0x0A
hex + decode),
`rtc set YYYY-MM-DD HH:MM:SS` (strptime, UTC, range + calendar
round-trip validation).

2. **KEY feels slow**: display rendering was throttled to 10 s.
`display_service::run` now keeps
the 10 s full-render heartbeat (with memory refresh) but adds a 500 ms
fast path that
re-renders as soon as a signature over volatile fields (key counters,
key_pressed, wifi/storage
state, tf_logging_ok, ap_active, ssid_configured, rtc valid+minute)
changes. KEY counter now
   appears within ~0.5-1 s.

3. **Which button?**: README now documents that only KEY (GPIO18) is
monitored; RST reboots and
BOOT (GPIO0) does nothing, plus the `rtc` commands, SNTP behavior, and
the CR1220 coin-cell
   note (without a battery every power loss clears the clock again).

## On-device evidence

Single-session serial on /dev/ttyACM0 (see
`.omo/evidence/m1wp/rtc-render-fix-finish-m1-wifi-provisioning.log`):

- Post-flash boot auto-connected Wi-Fi and SNTP-synced the RTC before
the console attached
(pre-manual-set `rtc status` already showed real UTC 2026-08-31
05:17:55, OS bit clear).
- `rtc set 2026-08-31 12:00:00` -> `ESP_OK`; `rtc status` valid and
advancing across polls
(12:14:59 -> 12:15:49); JSONL `utc_valid:true` + non-empty `utc` with 10
s cadence and crc32c.
- Explicit RTS reset round captured live: `frame-time: sntp started
(ntp.aliyun.com,
pool.ntp.org)` and `frame-time: rtc synced from sntp (unix 1788154496)`
7.8 s after reset;
  device-vs-host time delta 4 s.
- Pre-fix invalid state (OS bit set -> ESP_ERR_INVALID_CRC) is
documented in the prior soak
  evidence (F3 log); the fix clears it on the first SNTP sync.
- KEY latency: no user press within the two assist windows;
console-observer polling at 500 ms
shows counters served immediately from the shared snapshot the display
fast path renders
(CITED: `volatile_signature` + 500 ms check in `display_service.cc`), 10
s heartbeat captured
  via memory-metric refresh.
- Regression: `status --json` tf_logging=true, wifi=CONNECTED, selftest
`wifi_config: ok`.

Build 0 warnings; clang-format 18 clean;
pytest/ruff/frame-evidence/frame-doc-links green.

Co-authored-by: ania-lain <[email protected]>
This commit is contained in:
Lain
2026-08-31 13:42:06 +08:00
committed by GitHub
co-authored by lain
parent 5bbd2fe6cc
commit e9f8b7386b
11 changed files with 398 additions and 11 deletions
+83 -3
View File
@@ -1,5 +1,7 @@
#include "board_service.hh"
#include <ctime>
#include "board_config.hh"
#include "driver/gpio.h"
#include "esp_log.h"
@@ -13,6 +15,8 @@ constexpr uint16_t kSensorWake = 0x3517;
constexpr uint16_t kSensorMeasure = 0x7866;
constexpr uint16_t kSensorSleep = 0xB098;
constexpr uint8_t kRtcSecondsRegister = 0x04;
constexpr uint8_t kRtcFirstRegister = 0x00;
constexpr std::size_t kRtcRegisterCount = 11;
// KEY release bands: <1s short press, 1s..<3s long press, >=3s provisioning portal command.
constexpr int64_t kKeyLongPressUs = 1000000;
constexpr int64_t kKeyPortalHoldUs = 3000000;
@@ -31,6 +35,10 @@ uint8_t from_bcd(uint8_t value) {
return static_cast<uint8_t>((value >> 4U) * 10U + (value & 0x0FU));
}
uint8_t to_bcd(unsigned value) {
return static_cast<uint8_t>(((value / 10U) << 4U) | (value % 10U));
}
} // namespace
board_service::~board_service() {
@@ -132,11 +140,15 @@ esp_err_t board_service::read_rtc() {
if (rtc_ == nullptr) {
return ESP_ERR_NOT_FOUND;
}
if (i2c_mutex_ == nullptr || xSemaphoreTake(i2c_mutex_, pdMS_TO_TICKS(500)) != pdTRUE) {
return ESP_ERR_INVALID_STATE;
}
uint8_t registers[7]{};
esp_err_t result = i2c_master_transmit(rtc_, &kRtcSecondsRegister, 1, 100);
if (result == ESP_OK) {
result = i2c_master_receive(rtc_, registers, sizeof(registers), 100);
}
xSemaphoreGive(i2c_mutex_);
if (result != ESP_OK) {
status_.update([](status_snapshot& value) { value.rtc.valid = false; });
return result;
@@ -152,9 +164,8 @@ esp_err_t board_service::read_rtc() {
const bool plausible = !oscillator_stopped && second < 60 && minute < 60 && hour < 24 &&
day >= 1 && day <= 31 && month >= 1 && month <= 12 && year <= 2099;
const int64_t unix_seconds = days_from_civil(year, month, day) * 86400LL +
static_cast<int64_t>(hour) * 3600LL +
static_cast<int64_t>(minute) * 60LL + static_cast<int64_t>(second);
const int64_t unix_seconds =
unix_from_utc(static_cast<int>(year), month, day, hour, minute, second);
const bool valid = plausible && unix_seconds > 0;
status_.update([&](status_snapshot& value) {
value.rtc = {.unix_seconds = valid ? unix_seconds : 0,
@@ -164,6 +175,75 @@ esp_err_t board_service::read_rtc() {
return valid ? ESP_OK : ESP_ERR_INVALID_CRC;
}
esp_err_t board_service::read_rtc_raw(uint8_t registers[11]) {
if (rtc_ == nullptr) {
return ESP_ERR_NOT_FOUND;
}
if (i2c_mutex_ == nullptr || xSemaphoreTake(i2c_mutex_, pdMS_TO_TICKS(500)) != pdTRUE) {
return ESP_ERR_INVALID_STATE;
}
esp_err_t result = i2c_master_transmit(rtc_, &kRtcFirstRegister, 1, 100);
if (result == ESP_OK) {
result = i2c_master_receive(rtc_, registers, kRtcRegisterCount, 100);
}
xSemaphoreGive(i2c_mutex_);
return result;
}
esp_err_t board_service::set_rtc_time(int64_t unix_seconds) {
if (rtc_ == nullptr) {
return ESP_ERR_NOT_FOUND;
}
time_t seconds = static_cast<time_t>(unix_seconds);
struct tm utc_time {};
if (unix_seconds <= 0 || gmtime_r(&seconds, &utc_time) == nullptr) {
return ESP_ERR_INVALID_ARG;
}
const int year = utc_time.tm_year + 1900;
if (year < 2000 || year > 2099) {
return ESP_ERR_INVALID_ARG;
}
const unsigned weekday = utc_time.tm_wday == 0 ? 7U : static_cast<unsigned>(utc_time.tm_wday);
// Multi-byte write at 0x04; seconds bit7=0 clears the PCF85063 OS (oscillator stopped) flag.
const uint8_t payload[]{
kRtcSecondsRegister,
to_bcd(static_cast<unsigned>(utc_time.tm_sec)),
to_bcd(static_cast<unsigned>(utc_time.tm_min)),
to_bcd(static_cast<unsigned>(utc_time.tm_hour)),
to_bcd(static_cast<unsigned>(utc_time.tm_mday)),
to_bcd(weekday),
to_bcd(static_cast<unsigned>(utc_time.tm_mon) + 1U),
to_bcd(static_cast<unsigned>(year - 2000)),
};
if (i2c_mutex_ == nullptr || xSemaphoreTake(i2c_mutex_, pdMS_TO_TICKS(500)) != pdTRUE) {
return ESP_ERR_INVALID_STATE;
}
const esp_err_t result = i2c_master_transmit(rtc_, payload, sizeof(payload), 100);
xSemaphoreGive(i2c_mutex_);
if (result != ESP_OK) {
return result;
}
// Confirm by re-reading; the oscillator may tick up to two seconds meanwhile.
const esp_err_t confirm = read_rtc();
if (confirm != ESP_OK) {
return confirm;
}
const status_snapshot value = status_.snapshot();
if (!value.rtc.valid || value.rtc.unix_seconds < unix_seconds ||
value.rtc.unix_seconds > unix_seconds + 2) {
return ESP_ERR_INVALID_CRC;
}
ESP_LOGI(kTag, "rtc time set to unix %" PRId64, unix_seconds);
return ESP_OK;
}
int64_t board_service::unix_from_utc(int year, unsigned month, unsigned day, unsigned hour,
unsigned minute, unsigned second) {
return days_from_civil(year, month, day) * 86400LL + static_cast<int64_t>(hour) * 3600LL +
static_cast<int64_t>(minute) * 60LL + static_cast<int64_t>(second);
}
esp_err_t board_service::sample_sensor() {
const uint8_t wake[]{static_cast<uint8_t>(kSensorWake >> 8U),
static_cast<uint8_t>(kSensorWake)};