Files
julian 974082cb3e Add diag console probe (OBD-II mode 01/09 on 0x7DF); wiki: diagnostic polling, two sources
Firmware:
- CANDriver::transmit(): the single sanctioned transmit path (single-shot,
  echoed as CANTX lines, TX_FAILED alert counted); CAN-task warnings now take
  the serial lock
- DiagProbe module: `diag pids` supported-PID bitmap scan, `diag pid <hex>`,
  `diag vin`, `diag ecu`, `diag status`; ISO-TP reassembly with flow control;
  one request in flight, refused for 3 s after any foreign tester frame
- Console dispatch chained via Tunables::setExtraCommandHandler()
- CLAUDE.md and docs/UART_PROTOCOL.md updated

Wiki:
- New page obd-diagnostic-polling (broadcast vs polled, 0x6F1->0x612 addressing,
  Route A generic OBD PID table, Route B, test-day procedure, firmware probe)
- New sources: google-ai-dde7m57-pids (clipping, unverified, 0x612 direction
  contradicted), dwr-335d-torque-pids (thread + raw/BMW 335d.csv decoded)
- Clippings/ folder added to the schema (Obsidian Web Clipper, treated as raw/)
- bmw-m57n-engine, can-bus-integration, open-questions, index, log updated
2026-09-21 19:51:49 +02:00

138 lines
6.3 KiB
Markdown
Raw Permalink 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: 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`.
---
## 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.