Files
ESP32-S3-LCD-1.3/wiki/pages/controller-link.md
T

100 lines
9.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
type: concept
tags: [link, uart, protocol, architecture]
updated: 2026-09-21
---
# Controller Link (A → B)
The system is two ESP32-S3 boards. Main CTRL (Waveshare ESP32-S3-Touch-LCD-5B, sibling repo) reads the BMW PT-CAN bus and owns all decoding; Inst. Cluster ([[esp32-s3-lcd-1-3]], this repo) is a **slave / extension**: it consumes a compact packet, displays it, and will drive the Patrol gauges from it ([[patrol-gauge-outputs]]).
```
BMW PT-CAN ──▶ Main CTRL (5B) ──UART 5 Hz──▶ Inst. Cluster (1.3)
╰── BLE (planned, redundant) ──╯ → [[link-redundancy]]
```
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.
## Maintenance frames (2026-09-27) — the link is no longer one-way
Three frame types were added for [[controller-b-ota]] without bumping `PACKET_VERSION` (DATA/CONFIG bytes unchanged): `STATUS` 0x03 (B → A, ESP-NOW unicast to A's MAC, every 5 s: B's version, update phase, heap), `PAIR` 0x04 (A → B, **UART wire only**: the shared key), `SECURE` 0x05 (A → B, ESP-NOW unicast, AES-256-GCM envelope for Wi-Fi credentials and OTA commands). `PACKET_MAX_PAYLOAD` grew 64 → 160. B's ESP-NOW receiver routes non-DATA/CONFIG types to `LinkPeer` and remembers A's MAC from the data frames; A's `EspNowLink` now has a receive callback and `sendTo()`. Verified 2026-09-27: B's STATUS reaches A (`link status` on A lists B's MAC, version, heap).
## Wire-level contract — protocol v2 `(repo, 2026-09-21; spec: 5B repo docs/UART_PROTOCOL.md)`
- 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)
| Field | Source on A | Status |
|---|---|---|
| `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_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 |
| `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
- A transmits on **GPIO 44** (`uart_manager.cpp`, `UART_TX_PIN 44`) `(repo)`; RX 43 unused
- B receives on **IO1** (left header, pin 4), TX IO2 unused `(repo)`
> [!warning] Two docs said A transmits on GPIO 43 (both fixed or deleted 2026-09-21)
> 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. **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]].
## Bench wiring (recommended, 2026-09-21 — unverified until run)
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
>
> Same byte on the wire, different meanings: a value of 4 is "Sport" to B and "1st gear" to A. One side must be changed and `PACKET_VERSION` bumped. Which side is correct depends on what A's CAN parser actually emits now — check its `can_driver.cpp` before touching B.
Also note A's protocol header now says all CAN IDs are 11-bit and the old placeholder map is dead; B's copy of the header still carries the placeholder CAN section. Harmless (B never parses CAN) but confusing — strip it when the enums are reconciled.
## Change discipline
Any new signal (RPM first) means, in order: add the field and a `valid_flags` bit on A → bump `PACKET_VERSION` → copy the header to B → B rejects packets with an unexpected version → B's UI/gauge code consumes the field. The version byte exists precisely so a stale B fails loudly instead of misreading bytes.
## Related
[[esp32-s3-lcd-1-3]] · [[link-redundancy]] · [[patrol-gauge-outputs]] · [[open-questions]]