Link protocol v2 receiver, ESP-NOW leg, Patrol gauge outputs, gear pulse

- protocol header v2 copied byte-identical from the 5B repo
- uart_receiver: processFrame() shared by the UART framing task and ESP-NOW,
  version check, dedupe on packet_id+timestamp, per-transport liveness,
  CONFIG cache with sequence number
- espnow_receiver: STA mode, channel hopping while silent, locks on frames
- gauge_outputs: tach on IO7 (LEDC ch2, 14-bit), coolant PWM on IO8 (LEDC
  ch4, 8-bit), config cached in NVS, overrides applied but not saved
- main: gear label from lever + engaged/target (pulses while shifting, red on
  mismatch), icons driven by A's alert bits, gauge update at 5 Hz, link stats
- wiki: temp_stage.cfd (user's old CANNY program) decoded as prior art;
  controller-link, link-redundancy, patrol-gauge-outputs, open-questions
  updated with the bench-verified v2 results and calibration procedure

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-09-21 19:12:38 +02:00
parent 3613387d41
commit 5fe313b7fa
18 changed files with 1658 additions and 514 deletions
+42 -11
View File
@@ -15,12 +15,20 @@ BMW PT-CAN ──▶ Controller A (5B) ──UART 5 Hz──▶ Controller B (1.
Architecture rule: **B never touches CAN.** Anything B needs must be put into the packet by A. See `(sibling: 5B wiki → dual-controller-architecture)` for A's view of the same link.
## Wire-level contract (repo, both repos' `include/protocol/automotive_data.h` and `docs/UART_PROTOCOL.md` (5B repo))
## Wire-level contract — protocol v2 `(repo, 2026-09-21; spec: 5B repo docs/UART_PROTOCOL.md)`
- 115200 8N1, 3.3 V TTL, one direction A → B, 5 Hz (every 200 ms)
- Frame: `0xAA` · version `0x01` · length `0x12` · 18-byte payload · XOR checksum · `0x55` = **23 bytes**
- Payload (little-endian): `packet_id` u8 · `timestamp_ms` u32 · `gear_position` u8 · `gearbox_oil_temp` i16 (0.1 °C) · `engine_oil_temp` i16 (0.1 °C) · `coolant_temp` i16 (0.1 °C) · `battery_voltage` u16 (0.01 V) · `fault_codes` u16 bitmap · `valid_flags` u8 · `debug_enabled` u8
- B declares the link **lost after 1000 ms** without a valid packet and shows a no-data state `(repo)`; A only transmits while its CAN task is running (CAN toggle ON → UART auto-starts) `(repo)`
- One direction A → B, on **two transports at once**: UART1 115200 8N1 and **ESP-NOW broadcast** (channel 1; B hops 1–13 while silent and locks when frames arrive). B dedupes DATA frames on `packet_id` + `timestamp_ms`.
- Frame: `0xAA | version 0x02 | type | length | payload | XOR | 0x55`. Types: **DATA** (21-byte payload, every 200 ms) and **CONFIG** (27-byte payload, on start, on every tunables change, and every 5 s).
- DATA payload: `packet_id` · `timestamp_ms` · `gear_lever` (P/R/N/D/S/M) · `gear_nibbles` (target ≪ 4 | engaged) · `engine_rpm` · gearbox / engine-oil / coolant temps (0.1 °C) · `battery_voltage` (0.01 V) · `alerts` bitmap · `valid_flags` · `debug_enabled`.
- CONFIG payload = `link_config_t`: tach ppr/trim/clamps, coolant PWM Hz + duty law + min temp, enables, bench override, alert thresholds — edited on A (`cfg` serial console, NVS), executed on B ([[patrol-gauge-outputs]]).
- Alerts are decided on A from its thresholds; B only renders the bits (icons red, gear label red on mismatch).
- B declares the link **lost after 1000 ms** without an applied DATA frame on any transport and drops both gauge outputs; A transmits while its CAN task runs `(repo)`.
**Bench-verified 2026-09-21** `(verified)`: 42 frames / 10 s applied, 42 seen on UART, 42 on ESP-NOW, 42 duplicates dropped, 0 checksum / framing / version errors; ESP-NOW locked on channel 1; CONFIG frames received on both paths; `cfg override rpm 2000` typed on A's console reached B and produced a 133.3 Hz tach output within a second, and clearing it dropped the output.
### v1 for reference
23-byte single-type frame with one `gear_position` byte and a `fault_codes` word. Retired 2026-09-21; B counts any non-v2 frame as a version error.
### What each field actually carries today (sibling: 5B wiki → can-id-candidates)
@@ -29,10 +37,10 @@ Architecture rule: **B never touches CAN.** Anything B needs must be put into th
| `coolant_temp` | CAN 0x1D0 byte 0, raw − 48 | verified live |
| `gearbox_oil_temp` | CAN 0x0B5 byte 7, raw − 40 | verified live |
| `engine_oil_temp` | CAN 0x1D0 byte 1 | **dead — sensor not fitted on this M57N, reads 0xFF**; B will never get a real value |
| `gear_position` | CAN 0x1D2 lever/mode + 0x0BA target gear | verified live |
| `gear_lever` + `gear_nibbles` | CAN 0x1D2 lever/mode + byte 1 engaged gear; 0x0BA target gear | verified live; `target != engaged` = shift in progress (B pulses the digit) |
| `battery_voltage` | CAN 0x3B4, 12-bit × 0.015 V | verified live |
| `fault_codes` | A's own derived faults (bit 0 = lever vs gearbox mismatch), **not** DDE DTCs | `(repo)` |
| RPM | **not in the packet** — A has it verified from CAN 0x0AA but the protocol has no field | needed by [[patrol-gauge-outputs]] |
| `engine_rpm` | CAN 0x0AA | verified live on A; on the wire since v2 (2026-09-21) |
| `alerts` | A's thresholds (`link_config_t`) + CAN status bits | v2 |
## Pins
@@ -43,11 +51,34 @@ Architecture rule: **B never touches CAN.** Anything B needs must be put into th
> The 5B repo's root `CLAUDE.md` (corrected 2026-09-21) and this repo's former `.docs/UART_WIRING_GUIDE.md` (deleted 2026-09-21) both said "GPIO 43 TX". The 5B firmware and `docs/UART_PROTOCOL.md` (5B repo) say **44**. The code wins `(repo)`.
> [!warning] GPIO 44 on the 5B is not a bare pin — it feeds an RS485 transceiver
> On the 5B schematic, IO44 is the `RS485_TXD` net into an SP3485 with automatic direction control, and only the differential **A/B pair** reaches the terminal block `(sibling: 5B wiki → esp32-s3-touch-lcd-5b)`. Both wiring guides describe a direct "GPIO 44 → IO1 + GND" TTL wire, which the 5B does not physically expose. So either (a) the RS485 A line is being fed straight into B's TTL input and happens to work, (b) there is an RS485→TTL module on the B side that the docs don't mention, or (c) the wire is soldered to the module pin. **Which one is true is unknown** `(unverified)` and it decides how robust the wired link is in the car → [[open-questions]] #1, [[link-redundancy]].
> On the 5B schematic, IO44 is the `RS485_TXD` net into an SP3485 with automatic direction control, and only the differential **A/B pair** reaches the terminal block `(sibling: 5B wiki → esp32-s3-touch-lcd-5b)`. Both wiring guides describe a direct "GPIO 44 → IO1 + GND" TTL wire, which the 5B does not physically expose. So either (a) the RS485 A line is being fed straight into B's TTL input and happens to work, (b) there is an RS485→TTL module on the B side that the docs don't mention, or (c) the wire is soldered to the module pin. **Answered 2026-09-21: it is (a)** — the bare A line into IO1 works on the bench (verified, see bench wiring below). Whether it survives a car-length run is a separate question → [[open-questions]] #1, [[link-redundancy]].
## Protocol drift between the two repos
## Bench wiring (recommended, 2026-09-21 — unverified until run)
> [!warning] The `gear_position_t` enums differ `(repo, 2026-09-21)`
The 5B exposes UART1 only as the RS485 pair on its terminal block (`485_A`, `485_B`), so B needs a **3.3 V RS485→TTL module** (MAX3485 / SP3485; not a 5 V MAX485 without a divider on RO — ESP32 inputs are not 5 V tolerant).
| 5B terminal | → | Module | → | 1.3 left header |
|---|---|---|---|---|
| `485_A` | | A | | |
| `485_B` | | B | | |
| `GND` | | GND | | pin 2 GND |
| | | VCC | | pin 3 3V3 |
| | | RO | | pin 4 IO1 (UART1 RX) |
| | | DI / DE / RE | | unconnected, or DE+RE tied low (receive-only) |
Termination: the 5B's single 120 Ω switch serves CAN *and* RS485 — may be ON on the bench with no vehicle bus, **must be OFF before the 5B meets the car's CAN**. Desk-length runs don't care.
**Zero-parts bench alternative (no RS485 module available, 2026-09-21):** `485_A` → IO1 plus GND → GND. On the 5B schematic the SP3485 runs from 3.3 V (A can never exceed 3.3 V), the auto-direction circuit enables the driver only while sending a 0, and 4.7 kΩ bias resistors hold A high at idle — so with no 120 Ω load, A carries a plain 3.3 V UART signal with GPIO 44's polarity. **(verified on the bench 2026-09-21: 38 packets / 10 s, 0 checksum, 0 framing errors, all valid flags set; both boards on USB, 120 Ω switch OFF.)** This is almost certainly what the old wiring guide's "GPIO 44 → IO1" line described. **Bench only**: the bias pull-up will not hold up over a car-length run; the car gets a transceiver or the wireless leg. Keep the 5B's 120 Ω switch OFF for this.
Other zero-parts paths considered: ESP-NOW / BLE (the planned wireless leg, → [[link-redundancy]]) and B as an I2C slave on the 5B's I2C terminal (fiddly — A's bus is owned by the display library and shared with touch + RTC; not pursued).
Bench check before any protocol work: both boards on USB, monitors at 115200; CAN toggle ON on the 5B (its UART starts with the CAN task, no bus needed, default values at 5 Hz); B should report the connection established and packet counts rising with zero checksum/framing errors. Framing errors = A/B swapped; nothing at all = module not in receive or no common ground.
## Protocol drift between the two repos — resolved by v2
> [!note] Resolved 2026-09-21: the wire no longer carries `gear_position_t`. v2 sends `gear_lever` plus target/engaged nibbles, and the header is copied byte-identical to B (`cmp`-checked). The history below is kept for the record.
> [!warning] The `gear_position_t` enums differed `(repo, before v2)`
> - **B's header** (matches `docs/UART_PROTOCOL.md` (5B repo)): 0 P · 1 R · 2 N · 3 D · **4 Sport · 5 Manual** · 6–13 M1–M8 · 14 Error
> - **A's header** (rewritten during CAN decoding): 0 P · 1 R · 2 N · 3 D · **4–9 M1–M6** · 0xFF Unknown
>