Files
ESP32-S3-Touch-LCD-5B/docs/UART_PROTOCOL.md
T
julian f60bdac268 Link protocol v2, ESP-NOW leg, gauge tunables + tuning screen; remove Home screen
Protocol v2 (shared header, byte-identical with the 1.3 repo):
- typed DATA/CONFIG frames: 0xAA | ver | type | len | payload | XOR | 0x55
- gear split into lever (P/R/N/D/S/M) + target/engaged nibbles, engine rpm,
  alert bitmap decided here from tunable thresholds, link_config_t tunables
- docs/UART_PROTOCOL.md rewritten for v2

Controller A modules:
- tunables: gauge calibration + alert thresholds in NVS, 'cfg' serial console,
  bench override (never persisted)
- espnow_link: every frame also broadcast on ESP-NOW ch 1; radio stays in STA
  (WiFiManager no longer powers it off)
- uart_manager: DATA at 5 Hz + CONFIG on change/5 s on both transports
- can_driver: publishes gear pieces and rpm, evaluates alerts
- tuning_screen: hand-coded Gauge Tuning page (swipe right from Driveline),
  -/+ rows, enable switches, override sliders, Driveline/Defaults buttons

UI: SquareLine Home screen (3 meters + battery) and meters_manager removed;
Driveline is the boot screen; Settings back-gesture calls app_show_driveline().

tools/fix_penv_deps.ps1: repairs the shared PlatformIO penv after a 1.3 build.
Bench-verified with Controller B: both transports alive, dedupe, override
rpm 2000 -> 133.3 Hz tach output.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-09-21 19:12:31 +02:00

136 lines
6.0 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.
# Link Protocol Specification (v2)
**Version:** 2 (`PACKET_VERSION 0x02`)
**Last Updated:** 2026-09-21
**Source of truth:** `include/protocol/automotive_data.h` — byte-identical in both repos.
**Direction:** Controller A (ESP32-S3-Touch-LCD-5B) → Controller B (ESP32-S3-LCD-1.3), one way.
---
## Transports
The same frames are sent on two paths at once. B accepts either and deduplicates DATA frames on `packet_id` + `timestamp_ms`.
| Path | A side | B side | Notes |
|---|---|---|---|
| UART1 | GPIO 44 TX → SP3485 → terminal `485_A` / `485_B` | IO1 RX (left header pin 4) | 115200 8N1. Bench: `485_A` straight into IO1 + GND works (unterminated, biased high). Car: RS485 transceiver on B, or rely on ESP-NOW. |
| ESP-NOW | broadcast, Wi-Fi channel `ESPNOW_CHANNEL` (1) | listens; hops channels 1–13 while silent, locks when frames arrive | A's radio stays in STA mode permanently. If A joins an access point the channel follows the AP; B finds it by hopping. |
---
## Frame format (both transports)
```
0xAA | version | type | length | payload[length] | checksum | 0x55
```
| Field | Size | Value |
|---|---|---|
| start | 1 | `0xAA` |
| version | 1 | `0x02` — B rejects any other value (counted as version error) |
| type | 1 | `0x01` DATA, `0x02` CONFIG |
| length | 1 | payload bytes (21 for DATA, 27 for CONFIG) |
| payload | length | see below, little-endian, packed |
| checksum | 1 | XOR of version, type, length and every payload byte |
| end | 1 | `0x55` |
DATA frame = 27 bytes, CONFIG frame = 33 bytes. Helpers: `frame_build()`, `frame_parse()` in the header.
---
## DATA payload — `automotive_data_t` (21 bytes, every 200 ms)
| Offset | Size | Field | Unit / encoding |
|---|---|---|---|
| 0 | 1 | `packet_id` | rolling counter |
| 1 | 4 | `timestamp_ms` | A's `millis()` |
| 5 | 1 | `gear_lever` | `gear_lever_t`: 0 P, 1 R, 2 N, 3 D, 4 S, 5 M, 0xFF unknown |
| 6 | 1 | `gear_nibbles` | high nibble = gearbox **target** gear (CAN 0x0BA), low nibble = **engaged** gear (CAN 0x1D2 byte 1); 0 = none/P/R/N, 1–6 forward gears. `target != engaged` ⇒ shift in progress |
| 7 | 2 | `engine_rpm` | 1/min (CAN 0x0AA) |
| 9 | 2 | `gearbox_oil_temp` | 0.1 °C signed |
| 11 | 2 | `engine_oil_temp` | 0.1 °C signed — no sensor on this M57N, never valid |
| 13 | 2 | `coolant_temp` | 0.1 °C signed |
| 15 | 2 | `battery_voltage` | 0.01 V |
| 17 | 2 | `alerts` | bitmap, decided on A (below) |
| 19 | 1 | `valid_flags` | bitmap (below) |
| 20 | 1 | `debug_enabled` | 0/1, B mirrors A's debug overlay toggle |
`valid_flags`: bit0 gear, bit1 gearbox temp, bit2 engine oil temp, bit3 coolant, bit4 battery, bit5 alerts, bit6 rpm. A field is meaningful only when its bit is set. On the bench without CAN, A sends default temperatures/voltage marked valid but **never** marks rpm or gear nibbles valid.
`alerts` (thresholds come from A's tunables):
| Bit | Name | Condition |
|---|---|---|
| 0 | `ALERT_GEAR_MISMATCH` | lever (0x1D2) and gearbox target (0x0BA) disagree beyond the debounce |
| 1 | `ALERT_COOLANT_HOT` | coolant ≥ `coolant_hot_c` |
| 2 | `ALERT_GEARBOX_HOT` | gearbox oil ≥ `gearbox_hot_c` |
| 3 | `ALERT_GEARBOX_COOLING` | gearbox's own cooling demand (0x0B5 ST_OTMP_GRB ≥ 1) |
| 4 | `ALERT_BATTERY_LOW` | battery < `battery_low_x100` |
Severity for display: bits 0, 1, 2, 4 are red alarms; bit 3 is amber.
---
## CONFIG payload — `link_config_t` (27 bytes; on start, on every change, and every 5 s)
Tunables edited on A (serial console `cfg ...`, NVS-backed), executed on B's gauge outputs.
| Field | Size | Default | Meaning |
|---|---|---|---|
| `cfg_version` | 1 | 1 | bumped on every change; B applies when it changes |
| `tach_ppr` | 1 | 4 | tach pulses per engine revolution |
| `tach_trim_pct` | 1 | 0 | fine trim −50…+50 % |
| `tach_min_hz` | 2 | 0 | 0 = no clamp |
| `tach_max_hz` | 2 | 600 | 0 = no clamp |
| `temp_pwm_hz` | 2 | 1000 | coolant PWM frequency |
| `temp_slope_x100` | 2 | 150 | duty per °C ×100 |
| `temp_offset` | 2 | 50 | duty at 0 °C |
| `temp_min_c` | 2 | 40 | output off below this coolant temperature |
| `temp_duty_max` | 1 | 255 | clamp |
| `out_enable` | 1 | 0x03 | bit0 tach, bit1 temp |
| `override_mode` | 1 | 0 | bit0 force tach rpm, bit1 force temp duty (bench tool, never persisted) |
| `override_rpm` | 2 | 0 | |
| `override_duty` | 1 | 0 | |
| `coolant_hot_c` | 2 | 100 | alert threshold |
| `gearbox_hot_c` | 2 | 130 | alert threshold |
| `battery_low_x100` | 2 | 1100 | alert threshold (0.01 V) |
Coolant duty law: `duty = temp_slope_x100 × T°C / 100 + temp_offset`, clamped to `0…temp_duty_max`, 0 while `T < temp_min_c`. Defaults reproduce the user's earlier CANNY program (`1.5·T + 50`).
Tach: `f = rpm × tach_ppr / 60 × (1 + trim/100)`, clamped, 50 % duty. Output low when rpm is invalid, zero, the link is down, or the output is disabled.
---
## Controller A console commands (USB serial)
```
cfg show
cfg set <name> <value> tach_ppr tach_trim tach_min_hz tach_max_hz
temp_hz temp_slope temp_offset temp_min temp_duty_max
tach_en temp_en coolant_hot gearbox_hot batt_low
cfg override off | rpm <n> | duty <n> | both <rpm> <duty>
cfg reset
```
`set` values are saved to NVS and pushed to B immediately. `override` is pushed but not saved on either side.
The same tunables are editable on A's touchscreen: Home → swipe right → Driveline → swipe right → **Gauge Tuning** (−/+ per field, enable switches, override sliders).
---
## Receiver behaviour (B)
- Link considered lost after 1000 ms without an applied DATA frame on any transport; display shows N/A and both gauge outputs go low.
- Gear label: letter from `gear_lever`; digit = engaged gear, or the target gear (pulsing) while target ≠ engaged.
- Icons turn red from the `alerts` bits, never from local thresholds.
- Last CONFIG is cached in B's NVS so a B reboot drives the gauges before A resends.
---
## Changing the protocol
1. Edit `automotive_data.h` on A, bump `PACKET_VERSION`.
2. Copy the header to B unchanged.
3. Update this document.
4. Flash both. A stale B logs version errors and applies nothing.