150 lines
8.2 KiB
Markdown
150 lines
8.2 KiB
Markdown
# Link Protocol Specification (v2)
|
||
|
||
**Version:** 2 (`PACKET_VERSION 0x02`)
|
||
**Last Updated:** 2026-09-27
|
||
**Source of truth:** `include/protocol/automotive_data.h` (+ `link_secure.h`) — byte-identical in both repos.
|
||
**Direction:** gauge data Controller A (ESP32-S3-Touch-LCD-5B) → Controller B (ESP32-S3-LCD-1.3). Since 2026-09-27 B answers with `STATUS` frames over ESP-NOW and A sends B maintenance frames (`PAIR` on the wire, `SECURE` over ESP-NOW) — see *Maintenance frames* below.
|
||
|
||
---
|
||
|
||
## 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, `0x03` STATUS, `0x04` PAIR, `0x05` SECURE |
|
||
| length | 1 | payload bytes (21 DATA, 27 CONFIG, 78 STATUS, 42 PAIR, 29–160 SECURE); `PACKET_MAX_PAYLOAD` = 160 |
|
||
| 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: Driveline → swipe right → **Gauge Tuning** (−/+ per field, enable switches, override sliders); the Driveline button returns.
|
||
|
||
The same console also carries the OBD-II diagnostic probe (`diag pids | pid <hex> | vin | ecu | status | help`), which is unrelated to the link: it transmits on the vehicle CAN bus. See `wiki/pages/obd-diagnostic-polling.md`.
|
||
|
||
---
|
||
|
||
## Maintenance frames (2026-09-27) — for Controller B's firmware updates
|
||
|
||
`PACKET_VERSION` did not change: DATA and CONFIG are byte-identical, and a B built before these types counts them as type errors and carries on.
|
||
|
||
| Type | Direction | Transport | Payload | Purpose |
|
||
|---|---|---|---|---|
|
||
| `STATUS` 0x03 | B → A | ESP-NOW unicast to A's MAC (learned from A's data frames) | `link_status_t` (78 B): B's version, OTA phase (`LINK_OTA_*`, same numbering as A's `Ota::phase_t`), progress %, flags (paired / have Wi-Fi / joined / pending verify), `key_fp`, uptime, free heap, latest tag, message | every 5 s and on every phase change; A's Updates page "Controller B" card |
|
||
| `PAIR` 0x04 | A → B | **UART wire only** — B drops a PAIR heard by radio | `link_pair_t` (42 B): magic `"PAIR"`, 32-byte random key, A's STA MAC | `link pair` on A's console, sent 3×; both sides keep the key in NVS. The only secret in the system (both repos are public) |
|
||
| `SECURE` 0x05 | A → B | ESP-NOW unicast | `nonce[12] \| tag[16] \| AES-256-GCM(key, nonce, inner_type \| inner)` — helpers in `link_secure.h` | inner `LINK_INNER_OTA_CMD` 0x11 (`link_ota_cmd_t`: check / install [force] / rollback + random nonce, B ignores a repeat of the last 4) or `LINK_INNER_WIFI_CREDS` 0x12 (`link_wifi_creds_t`: SET/FORGET, ssid, pass). B also requires the sender MAC to be the paired A |
|
||
|
||
B reports `key_fp` = first two bytes of SHA-256(key) so A can tell "paired with my key" from "paired with an old one" and re-pair.
|
||
|
||
## 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` if DATA or CONFIG change; adding a new frame type does not need a bump (a stale B ignores unknown types).
|
||
2. Copy the header (and `link_secure.h`) to B unchanged (`cmp` them).
|
||
3. Update this document.
|
||
4. Flash both. A stale B logs version errors and applies nothing.
|