Rename -> Inst. Cluster (user); power manager: deep sleep on link silence, wake on IO1 or 60 s probe boot; Ota::isBusy; sleep bit in shared header
Firmware / build (push) Successful in 2m59s

This commit is contained in:
2026-09-28 16:38:23 +02:00
parent 43697f3cc8
commit 6854aa1c46
33 changed files with 266 additions and 97 deletions
+12 -11
View File
@@ -4,7 +4,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
## Project Overview
**Controller B** of the BMW M57N → Nissan Patrol swap display system, running on a **Waveshare ESP32-S3-LCD-1.3** (1.3" 240×240 ST7789VW, shown with a circular mask). It is a slave/extension of Controller A (ESP32-S3-Touch-LCD-5B, sibling repo `../ESP32-S3-Touch-LCD-5B`): it receives protocol v2 frames over UART1 **and** ESP-NOW at 5 Hz, shows them, and drives the Patrol cluster's tachometer and coolant gauges from them on IO7 / IO8.
**Inst. Cluster** of the BMW M57N → Nissan Patrol swap display system, running on a **Waveshare ESP32-S3-LCD-1.3** (1.3" 240×240 ST7789VW, shown with a circular mask). It is a slave/extension of Main CTRL (ESP32-S3-Touch-LCD-5B, sibling repo `../ESP32-S3-Touch-LCD-5B`): it receives protocol v2 frames over UART1 **and** ESP-NOW at 5 Hz, shows them, and drives the Patrol cluster's tachometer and coolant gauges from them on IO7 / IO8.
**Hardware**: ESP32-S3R8 dual-core @ 240 MHz, 16 MB flash, 8 MB OPI PSRAM, CH343 USB-serial bridge (not native USB-CDC), Wi-Fi + BLE 5, 13 free-function GPIOs on two 8-pin headers.
@@ -34,7 +34,7 @@ Serial goes through the onboard **CH343 USB-UART chip on UART0**, so the port ap
- **Core 1**: `lvglTask()` calls `DisplayInit::update()` every 5 ms (`lv_timer_handler()` under the LVGL lock; the tick comes from `millis()` via `LV_TICK_CUSTOM`). The Arduino `loop()` also runs on Core 1 (`ARDUINO_RUNNING_CORE=1`) and drives the screen: every 200 ms it calls `GaugeScreen::update()` and `DebugMonitor::tick()`.
- **Core 0**: the UART receiver task (byte-level framing state machine) and the Wi-Fi/ESP-NOW receive callback. Both hand complete frames to `UARTReceiver::processFrame()`, which validates, dedupes DATA frames on `packet_id`, and caches CONFIG frames. Neither touches LVGL (the debug flag is only *recorded* there). Shared data is guarded by `g_data_mutex` inside `uart_receiver.cpp`.
**LVGL lock** (added 2026-09-27, same rule as Controller A's `lvgl_port_lock()`): `DisplayInit::lock(timeout_ms)` / `unlock()`, a recursive mutex. `lvglTask` holds it while rendering; every `lv_*` call anywhere else must hold it too. Before it existed, `loop()` wrote labels while the higher-priority LVGL task could preempt mid-call - the bug class that froze A.
**LVGL lock** (added 2026-09-27, same rule as Main CTRL's `lvgl_port_lock()`): `DisplayInit::lock(timeout_ms)` / `unlock()`, a recursive mutex. `lvglTask` holds it while rendering; every `lv_*` call anywhere else must hold it too. Before it existed, `loop()` wrote labels while the higher-priority LVGL task could preempt mid-call - the bug class that froze A.
### Modules
@@ -50,7 +50,8 @@ src/
debug_monitor.cpp # shows/hides LVGL perf+mem monitors from A's debug_enabled flag (recorded by the receiver, applied in loop)
link_peer.cpp # maintenance side of the link: STATUS to A, PAIR from the wire, SECURE (AES-GCM) frames from A -> Wi-Fi creds / OTA commands
ota_update.cpp # pulls this repo's signed Gitea release over Wi-Fi (port of A's module), 'ota' console
console.cpp # serial line reader + handler chain ('help', 'link', 'wifi', 'ota')
console.cpp # serial line reader + handler chain ('help', 'link', 'wifi', 'ota', 'sleep')
power_manager.cpp # deep sleep 60 s after the link goes silent (CFG_SLEEP_WHEN_SILENT from A); wake on IO1 or a 60 s probe boot
assets/
assets.h # LV_FONT_DECLARE / LV_IMG_DECLARE for the files below
fonts/ # Ubuntu Light 35 (values) and Ubuntu Bold 110 (gear) - plain LVGL font arrays
@@ -59,7 +60,7 @@ include/
config/lv_conf.h # LVGL 8.4.0 config (RGB565, dark theme, 128 KB pool in PSRAM, millis() tick, log off)
config/ota_config.h # FW_VERSION (CI bakes it, "dev" locally), OTA_TARGET "patrol-b", repo URLs, CA roots, signing public key
modules/uart_receiver.h, espnow_receiver.h, gauge_outputs.h, debug_monitor.h, link_peer.h, ota_update.h, console.h
protocol/automotive_data.h # SHARED packet contract — copied from Controller A, must match its version
protocol/automotive_data.h # SHARED packet contract — copied from Main CTRL, must match its version
protocol/link_secure.h # SHARED too: AES-256-GCM envelope for SECURE frames (byte-identical in both repos)
```
@@ -75,15 +76,15 @@ include/
**GaugeOutputs** — `init()`, `applyConfig(cfg, persist)`, `update(data, link_ok)` at 5 Hz, `printStatus()`. Tach = `rpm × ppr / 60 × (1 + trim)` at 50 % duty on LEDC channel 2 (timer 1); coolant = `slope × T + offset` 8-bit duty at `temp_pwm_hz` on channel 4 (timer 2). Channels 0/1 (timer 0) belong to the LovyanGFX backlight — never reuse them. Both outputs low when the link is down, the value is invalid, the output is disabled, or T is below `temp_min_c`; A's override forces fixed values. Last config cached in NVS namespace `gaugecfg`.
**DebugMonitor** — `init()`, `setVisible(bool)` (records only; called from the receiver), `tick()` (applies under the LVGL lock; called from loop), `isVisible()`. Driven by the `debug_enabled` byte in each packet, so Controller A's debug toggle controls B's overlays.
**DebugMonitor** — `init()`, `setVisible(bool)` (records only; called from the receiver), `tick()` (applies under the LVGL lock; called from loop), `isVisible()`. Driven by the `debug_enabled` byte in each packet, so Main CTRL's debug toggle controls B's overlays.
**LinkPeer** (2026-09-27) — `init()` loads the pairing key + A's MAC (NVS `linkpeer`) and the Wi-Fi credentials A handed over (NVS `wifi`, same keys as A's). `onPairFrame()` is called by `UARTReceiver::processFrame()` for `PAIR` frames **from the UART source only**; `onRadioFrame()` by `EspNowReceiver` for every non-DATA/CONFIG frame. Both just fill a one-slot mailbox; `tick()` (loop task) decrypts, checks the sender MAC against the paired A, writes NVS, and sends a `STATUS` frame to A every 5 s or on any phase change (`EspNowReceiver::sendTo()`, A's MAC from its data frames). `setOtaStatus()` / `setOtaCommandHandler()` are the updater's hooks. Nonce replay window of 4 for OTA commands. Console: `link status | unpair`, `wifi status | forget`. The password is never printed.
**Ota** (2026-09-27) — port of Controller A's `ota_update.cpp`: `ota status | check | install [force] | rollback` and A's `OTA_CMD` land in `startJob()`; the job runs in `tick()` (loop task, blocks the gauges - parked anyway). Gate: link data says rpm 0 and lever P/N/unknown, or the link is silent (A off); credentials present. Joins Wi-Fi only for the job and `WiFi.disconnect(false)` afterwards so the ESP-NOW receiver hops back to A. Manifest signature (ECDSA P-256 over `patrol-b|version|sha256`) checked before download, streamed sha256 checked after, `esp_ota_end()` image check, slot switch, reboot; `verifyRollbackLater()` overridden so the new image stays pending until `tick()` confirms it after `OTA_CONFIRM_MS` (30 s). Progress: `GaugeScreen::setNotice("Updating vX N%")` and a STATUS pumped to A at most once a second from inside the download loop. Never put secrets in source - the repo and its releases are public.
**Ota** (2026-09-27) — port of Main CTRL's `ota_update.cpp`: `ota status | check | install [force] | rollback` and A's `OTA_CMD` land in `startJob()`; the job runs in `tick()` (loop task, blocks the gauges - parked anyway). Gate: link data says rpm 0 and lever P/N/unknown, or the link is silent (A off); credentials present. Joins Wi-Fi only for the job and `WiFi.disconnect(false)` afterwards so the ESP-NOW receiver hops back to A. Manifest signature (ECDSA P-256 over `patrol-b|version|sha256`) checked before download, streamed sha256 checked after, `esp_ota_end()` image check, slot switch, reboot; `verifyRollbackLater()` overridden so the new image stays pending until `tick()` confirms it after `OTA_CONFIRM_MS` (30 s). Progress: `GaugeScreen::setNotice("Updating vX N%")` and a STATUS pumped to A at most once a second from inside the download loop. Never put secrets in source - the repo and its releases are public.
**Console** — `Console::addHandler()` chain, `tick()` from loop; `help` asks every handler to print its lines.
### UART packet (shared with Controller A)
### UART packet (shared with Main CTRL)
Protocol **v2**: `0xAA | 0x02 | type | length | payload | XOR | 0x55`. DATA (21-byte `automotive_data_t`: lever, target/engaged gear nibbles, rpm, temps, battery, alerts, valid flags) every 200 ms; CONFIG (27-byte `link_config_t`: gauge calibration + alert thresholds) on change and every 5 s. Same frames on UART1 and ESP-NOW. Spec: sibling repo `docs/UART_PROTOCOL.md`. `include/protocol/automotive_data.h` (and `link_secure.h`) must stay byte-identical to A's; B rejects other versions and counts them.
@@ -93,7 +94,7 @@ Alert thresholds live on A; B turns icons red from the `alerts` bits only. The g
### Firmware updates (OTA)
B updates itself from this repo's Gitea releases; A triggers it from Settings → Updates (Controller B card) or B's console does (`ota check`, `ota install`). CI: `.gitea/workflows/firmware.yml` - push to `master` → unsigned pre-release `main-latest`; tag `v*` → signed release (`manifest.txt`, target `patrol-b`), needs the repo secret `BMW_NISSAN_FIRMWARE_SIGNING_KEY` (same key as A's). Release with one push: `git tag -a vX.Y.Z -m "..." && git push --follow-tags origin master`. Version strings ≤ 15 chars. First pairing (`link pair` on A, wire connected) and the procedure: `wiki/pages/controller-b-ota.md`.
B updates itself from this repo's Gitea releases; A triggers it from Settings → Updates (Inst. Cluster card) or B's console does (`ota check`, `ota install`). CI: `.gitea/workflows/firmware.yml` - push to `master` → unsigned pre-release `main-latest`; tag `v*` → signed release (`manifest.txt`, target `patrol-b`), needs the repo secret `BMW_NISSAN_FIRMWARE_SIGNING_KEY` (same key as A's). Release with one push: `git tag -a vX.Y.Z -m "..." && git push --follow-tags origin master`. Version strings ≤ 15 chars. First pairing (`link pair` on A, wire connected) and the procedure: `wiki/pages/controller-b-ota.md`.
## Hardware-Specific Details
@@ -105,7 +106,7 @@ B updates itself from this repo's Gitea releases; A triggers it from Settings
| 38 / 39 / 42 | LCD DC / CS / RST |
| 20 | LCD backlight, LEDC PWM |
| 43 / 44 | UART0 → CH343 USB bridge (debug console) — not available for the link |
| IO1 | UART1 RX from Controller A (left header, pin 4) |
| IO1 | UART1 RX from Main CTRL (left header, pin 4) |
| IO2 | UART1 TX — unused, link is receive-only |
| IO7 | Tachometer pulse output (GaugeOutputs, hardware-timer ISR, phase-continuous) |
| IO8 | Coolant gauge PWM output (GaugeOutputs, LEDC ch 4) |
@@ -127,7 +128,7 @@ Pins are set in `src/modules/display_init.cpp` and `platformio.ini` (`-DUART_RX_
- Init order: `DisplayInit::init()` → `GaugeScreen::init()` → create `lvglTask`.
- Hold `DisplayInit::lock()` around every `lv_*` call outside `lvglTask`; never call LVGL from the receiver task or the ESP-NOW callback.
- Write a label or style only when the value changed (`setText()` / `setIcon()` in `gauge_screen.cpp` do this).
- Screens are built once and kept; do not delete and rebuild them on the fly (dangling pointers froze Controller A).
- Screens are built once and kept; do not delete and rebuild them on the fly (dangling pointers froze Main CTRL).
- Colour depth stays `LV_COLOR_DEPTH 16` (the icon arrays are RGB565 with alpha).
## Troubleshooting
@@ -143,6 +144,6 @@ Pins are set in `src/modules/display_init.cpp` and `platformio.ini` (`-DUART_RX_
## Important Notes
- This board is the **only** one of the two with free hardware-PWM GPIO; Controller A has none. Gauge driving belongs here.
- This board is the **only** one of the two with free hardware-PWM GPIO; Main CTRL has none. Gauge driving belongs here.
- Bench wiring: A's `485_A` straight into IO1 plus common GND (verified 2026-09-21). In the car, A's GPIO 44 only exists as the RS485 pair, so use a transceiver on B or rely on the ESP-NOW leg. See `wiki/pages/controller-link.md`.
- ESP-NOW is the redundant leg (implemented 2026-09-21); BLE remains an option — `wiki/pages/link-redundancy.md`.