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
@@ -15,6 +15,10 @@ namespace {
constexpr char kSelftestSsid[] = "frame-selftest";
constexpr char kSelftestPassword[] = "frame-selftest-pass";
uint8_t from_bcd(uint8_t value) {
return static_cast<uint8_t>((value >> 4U) * 10U + (value & 0x0FU));
}
} // namespace
esp_err_t console_service::start() {
@@ -69,6 +73,15 @@ esp_err_t console_service::start() {
.func_w_context = nullptr,
.context = nullptr,
};
const esp_console_cmd_t rtc_cmd{
.command = "rtc",
.help = "rtc status | rtc raw | rtc set YYYY-MM-DD HH:MM:SS (UTC)",
.hint = nullptr,
.func = &console_service::rtc_command,
.argtable = nullptr,
.func_w_context = nullptr,
.context = nullptr,
};
esp_err_t result = esp_console_cmd_register(&status_cmd);
if (result == ESP_OK) {
@@ -83,6 +96,9 @@ esp_err_t console_service::start() {
if (result == ESP_OK) {
result = esp_console_cmd_register(&selftest_cmd);
}
if (result == ESP_OK) {
result = esp_console_cmd_register(&rtc_cmd);
}
if (result != ESP_OK) {
return result;
}
@@ -217,6 +233,111 @@ int console_service::selftest_command(int argc, char** argv) {
return 0;
}
int console_service::rtc_command(int argc, char** argv) {
if (argc == 2 && std::strcmp(argv[1], "status") == 0) {
instance_->print_rtc_status();
return 0;
}
if (argc == 2 && std::strcmp(argv[1], "raw") == 0) {
return instance_->print_rtc_raw();
}
if (argc == 4 && std::strcmp(argv[1], "set") == 0) {
return instance_->run_rtc_set(argv[2], argv[3]);
}
std::printf("usage: rtc status | rtc raw | rtc set YYYY-MM-DD HH:MM:SS\n");
return 1;
}
void console_service::print_rtc_status() const {
const status_snapshot value = status_.snapshot();
std::printf("RTC: %s\n", value.rtc_present ? "present (PCF85063 @0x51)" : "missing (0x51)");
uint8_t registers[11]{};
const esp_err_t raw_result = board_.read_rtc_raw(registers);
const char* os_state = "unknown (raw read failed)";
if (raw_result == ESP_OK) {
os_state = (registers[4] & 0x80U) != 0U ? "set (oscillator stopped)" : "clear";
}
std::printf("valid: %s unix: %" PRId64 "\n", value.rtc.valid ? "true" : "false",
value.rtc.unix_seconds);
char utc[32]{};
if (value.rtc.valid) {
const time_t seconds = static_cast<time_t>(value.rtc.unix_seconds);
struct tm utc_time {};
if (gmtime_r(&seconds, &utc_time) != nullptr) {
const unsigned year = static_cast<unsigned>(utc_time.tm_year + 1900);
std::snprintf(
utc, sizeof(utc), "%04u-%02u-%02u %02u:%02u:%02u", year,
static_cast<unsigned>(utc_time.tm_mon + 1), static_cast<unsigned>(utc_time.tm_mday),
static_cast<unsigned>(utc_time.tm_hour), static_cast<unsigned>(utc_time.tm_min),
static_cast<unsigned>(utc_time.tm_sec));
}
}
std::printf("utc: %s\n", utc[0] == '\0' ? "-" : utc);
std::printf("OS bit: %s\n", os_state);
}
int console_service::print_rtc_raw() const {
uint8_t registers[11]{};
const esp_err_t result = board_.read_rtc_raw(registers);
if (result != ESP_OK) {
std::printf("rtc raw: %s\n", esp_err_to_name(result));
return 1;
}
static const char* names[11] = {"control_1", "control_2", "offset", "ram",
"seconds", "minutes", "hours", "days",
"weekdays", "months", "years"};
for (unsigned index = 0; index < 11; ++index) {
std::printf(" 0x%02X %s\n", registers[index], names[index]);
}
std::printf("OS bit (0x04 bit7): %s\n", (registers[4] & 0x80U) != 0U ? "set" : "clear");
std::printf("decode: %04u-%02u-%02u %02u:%02u:%02u weekday %u\n",
2000 + from_bcd(registers[10]), from_bcd(registers[9] & 0x1FU),
from_bcd(registers[7] & 0x3FU), from_bcd(registers[6] & 0x3FU),
from_bcd(registers[5] & 0x7FU), from_bcd(registers[4] & 0x7FU),
from_bcd(registers[8] & 0x07U));
return 0;
}
int console_service::run_rtc_set(const char* date_text, const char* time_text) {
char text[40]{};
if (std::snprintf(text, sizeof(text), "%s %s", date_text, time_text) >=
static_cast<int>(sizeof(text))) {
std::printf("rtc set: input too long\n");
return 1;
}
struct tm parsed {};
if (strptime(text, "%Y-%m-%d %H:%M:%S", &parsed) == nullptr) {
std::printf("rtc set: unparseable, expected YYYY-MM-DD HH:MM:SS (UTC)\n");
return 1;
}
const int year = parsed.tm_year + 1900;
const unsigned month = static_cast<unsigned>(parsed.tm_mon) + 1U;
if (year < 2000 || year > 2099 || month < 1 || month > 12 || parsed.tm_mday < 1 ||
parsed.tm_mday > 31 || parsed.tm_hour < 0 || parsed.tm_hour > 23 || parsed.tm_min < 0 ||
parsed.tm_min > 59 || parsed.tm_sec < 0 || parsed.tm_sec > 59) {
std::printf("rtc set: value out of range\n");
return 1;
}
const int64_t unix_seconds = board_service::unix_from_utc(
year, month, static_cast<unsigned>(parsed.tm_mday), static_cast<unsigned>(parsed.tm_hour),
static_cast<unsigned>(parsed.tm_min), static_cast<unsigned>(parsed.tm_sec));
time_t check_seconds = static_cast<time_t>(unix_seconds);
struct tm check {};
if (gmtime_r(&check_seconds, &check) == nullptr || check.tm_year != parsed.tm_year ||
check.tm_mon != parsed.tm_mon || check.tm_mday != parsed.tm_mday ||
check.tm_hour != parsed.tm_hour || check.tm_min != parsed.tm_min ||
check.tm_sec != parsed.tm_sec) {
std::printf("rtc set: impossible calendar date\n");
return 1;
}
const esp_err_t result = board_.set_rtc_time(unix_seconds);
std::printf("rtc set: %s (unix %" PRId64 ")\n", esp_err_to_name(result), unix_seconds);
return result == ESP_OK ? 0 : 1;
}
void console_service::run_wifi_config_selftest() const {
char prior_ssid[33]{};
char prior_password[64]{};