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:
@@ -0,0 +1,62 @@
|
||||
#include "time_sync.hh"
|
||||
|
||||
#include <cinttypes>
|
||||
#include <ctime>
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif_sntp.h"
|
||||
|
||||
namespace frame::m1 {
|
||||
namespace {
|
||||
|
||||
constexpr char kTag[] = "frame-time";
|
||||
constexpr const char* kSntpServers[] = {"ntp.aliyun.com", "pool.ntp.org"};
|
||||
constexpr int kPollIntervalMs = 1000;
|
||||
// SNTP timestamps before 2020-01-01 mean the system clock never synced.
|
||||
constexpr time_t kSyncThreshold = 1577836800;
|
||||
|
||||
} // namespace
|
||||
|
||||
esp_err_t time_sync::start() {
|
||||
const BaseType_t created =
|
||||
xTaskCreatePinnedToCore(&time_sync::task_entry, "frame-time", 4096, this, 5, &task_, 0);
|
||||
return created == pdPASS ? ESP_OK : ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
void time_sync::task_entry(void* context) { static_cast<time_sync*>(context)->run(); }
|
||||
|
||||
void time_sync::run() {
|
||||
for (;;) {
|
||||
vTaskDelay(pdMS_TO_TICKS(kPollIntervalMs));
|
||||
|
||||
const status_snapshot value = status_.snapshot();
|
||||
if (!sntp_started_ && value.wifi == wifi_state::connected) {
|
||||
esp_sntp_config_t config{};
|
||||
config.start = true;
|
||||
config.wait_for_sync = false;
|
||||
config.num_of_servers = 2;
|
||||
config.servers[0] = kSntpServers[0];
|
||||
config.servers[1] = kSntpServers[1];
|
||||
const esp_err_t result = esp_netif_sntp_init(&config);
|
||||
if (result == ESP_OK) {
|
||||
sntp_started_ = true;
|
||||
ESP_LOGI(kTag, "sntp started (%s, %s)", kSntpServers[0], kSntpServers[1]);
|
||||
} else {
|
||||
ESP_LOGW(kTag, "sntp start failed: %s", esp_err_to_name(result));
|
||||
}
|
||||
}
|
||||
|
||||
if (sntp_started_ && !rtc_synced_ && time(nullptr) > kSyncThreshold) {
|
||||
const int64_t unix_seconds = static_cast<int64_t>(time(nullptr));
|
||||
const esp_err_t result = board_.set_rtc_time(unix_seconds);
|
||||
if (result == ESP_OK) {
|
||||
rtc_synced_ = true;
|
||||
ESP_LOGI(kTag, "rtc synced from sntp (unix %" PRId64 ")", unix_seconds);
|
||||
} else {
|
||||
ESP_LOGW(kTag, "rtc sync from sntp failed: %s", esp_err_to_name(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace frame::m1
|
||||
Reference in New Issue
Block a user