Files
ESP32-S3-Touch-LCD-5B/include/protocol/automotive_data.h
T
julian 0a58afca9a Gear decode VERIFIED: real 0x1D2 map in firmware + wiki
The gear lever is fully decoded and confirmed on the actual swap bus
(raw/gear_box_can_revealings.csv): 0x1D2 byte0 = P 0xE1 / R 0xD2 / N 0xB4 /
D 0x78, matching the documented BMW codes exactly. Byte4 splits D vs S/M
(0xF0 auto) and carries manual +/- shifts.

Firmware:
- automotive_data.h: replace placeholder gear map (0x1001) with CAN_ID_GEAR_LEVER
  0x1D2, verified byte-0 codes, byte-4 mode define, and can_to_gear_lever().
- can_driver.cpp: decode 0x1D2 byte0 into gear_position via the enum; reject
  unrecognized codes instead of copying the raw byte.
- Scope: P/R/N/D only (verified). S/M shows as D for now (no enum slot; engaged
  gear number needs a drive capture) — TODO'd, not faked. Coolant/oil/battery
  left as flagged placeholders pending a cold-start capture.

Wiki:
- New source page gear-box-can-revealings + raw CSV filed.
- can-id-candidates: 0x1D2 gear row -> VERIFIED; encoding "dispute" section
  rewritten with the full lever+mode decode table.
- Updated index + log.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-16 14:54:06 +02:00

264 lines
10 KiB
C

/**
* automotive_data.h
*
* Shared data structure and protocol definitions for automotive monitoring system
* ESP32-S3-LCD-5B (Controller A) -> ESP32-S3-LCD-1.3 (Controller B) communication
*
* This header defines the data structure for 6 automotive sensor values and the
* simple packet framing protocol for UART transmission at 115200 baud.
*/
#ifndef _AUTOMOTIVE_DATA_H
#define _AUTOMOTIVE_DATA_H
#include <stdint.h>
// ============================================================================
// UART PROTOCOL CONFIGURATION
// ============================================================================
// Communication parameters (hardware-independent)
#define UART_BAUD_RATE 115200 // Reliable automotive-grade baud rate
#define UART_UPDATE_RATE_MS 200 // 5Hz transmission rate
// NOTE: UART pin assignments (TX/RX) are controller-specific and defined in:
// - Controller A (5B): uart_manager.cpp
// - Controller B (1.3): uart_receiver.cpp
// Packet framing bytes
#define PACKET_START_BYTE 0xAA // Start of packet marker
#define PACKET_END_BYTE 0x55 // End of packet marker
#define PACKET_VERSION 0x01 // Protocol version for future compatibility
// Packet structure offsets
#define PACKET_MIN_SIZE 8 // Start(1) + Version(1) + Length(1) + Data(min 1) + Checksum(1) + End(1)
#define PACKET_MAX_SIZE 64 // Maximum packet size to prevent buffer overruns
// ============================================================================
// AUTOMOTIVE DATA STRUCTURE
// ============================================================================
// Gear position enumeration
typedef enum {
GEAR_PARK = 0,
GEAR_REVERSE = 1,
GEAR_NEUTRAL = 2,
GEAR_DRIVE = 3,
GEAR_MANUAL_1 = 4,
GEAR_MANUAL_2 = 5,
GEAR_MANUAL_3 = 6,
GEAR_MANUAL_4 = 7,
GEAR_MANUAL_5 = 8,
GEAR_MANUAL_6 = 9,
GEAR_UNKNOWN = 0xFF
} gear_position_t;
// Main automotive data structure (packed for efficient transmission)
typedef struct __attribute__((packed)) {
// Identification and versioning
uint8_t packet_id; // Incremental packet ID for debugging
uint32_t timestamp_ms; // Milliseconds since boot
// Sensor data (6 monitored values)
uint8_t gear_position; // Current gear (see gear_position_t enum)
int16_t gearbox_oil_temp; // Gearbox oil temperature in 0.1°C (-3276.8 to +3276.7°C)
int16_t engine_oil_temp; // Engine oil temperature in 0.1°C
int16_t coolant_temp; // Engine coolant temperature in 0.1°C
uint16_t battery_voltage; // Battery voltage in 0.01V (0 to 655.35V)
uint16_t fault_codes; // Bitmap of active fault codes (16 possible codes)
// Data validity flags (1 bit per sensor)
uint8_t valid_flags; // Bit 0: gear, Bit 1: gearbox temp, etc.
// Debug mode flag (for Controller B to enable/disable monitors)
uint8_t debug_enabled; // 0 = debug off, 1 = debug on
} automotive_data_t;
// Size of the data structure
#define AUTOMOTIVE_DATA_SIZE sizeof(automotive_data_t)
// Valid flags bit positions
#define VALID_FLAG_GEAR_POS (1 << 0)
#define VALID_FLAG_GEARBOX_TEMP (1 << 1)
#define VALID_FLAG_ENGINE_TEMP (1 << 2)
#define VALID_FLAG_COOLANT_TEMP (1 << 3)
#define VALID_FLAG_BATTERY_VOLT (1 << 4)
#define VALID_FLAG_FAULT_CODES (1 << 5)
// ============================================================================
// CAN BUS ID MAPPINGS (BMW-Specific)
// ============================================================================
// BMW CAN message IDs and byte positions
#define CAN_ID_COOLANT_TEMP 0x4F // Engine coolant temperature
#define CAN_BYTE_COOLANT_TEMP 0 // Byte 0
#define CAN_ID_ENGINE_OIL_TEMP 0x55 // Engine oil temperature
#define CAN_BYTE_ENGINE_OIL_TEMP 7 // Byte 7
#define CAN_ID_GEARBOX_TEMP 0x201 // Gearbox oil temperature
#define CAN_BYTE_GEARBOX_TEMP 3 // Byte 3
// Gear lever position — EGS "TransmissionDataDisplay", DLC 6.
// VERIFIED on the swap bus 2026-07-16 (see wiki/sources/gear-box-can-revealings.md).
#define CAN_ID_GEAR_LEVER 0x1D2 // Gear lever position message
#define CAN_BYTE_GEAR_LEVER 0 // Byte 0: lever code (see can_to_gear_lever)
#define CAN_BYTE_GEAR_MODE 4 // Byte 4: 0xF0=Drive(auto), 0xF1=Sport/Manual, 0xF2=downshift(-)
// Raw byte-0 lever codes (whole-byte encoding, all verified against the live bus)
#define GEAR_CODE_PARK 0xE1
#define GEAR_CODE_REVERSE 0xD2
#define GEAR_CODE_NEUTRAL 0xB4
#define GEAR_CODE_DRIVE 0x78 // also S/M side-gate — split via CAN_BYTE_GEAR_MODE
#define GEAR_MODE_DRIVE_AUTO 0xF0 // byte 4 value when in plain Drive (else Sport/Manual)
#define CAN_ID_BATTERY_VOLTAGE 0x101F // Battery voltage (16-bit)
#define CAN_BYTE_BATTERY_VOLT_H 0 // Byte 0 (high byte)
#define CAN_BYTE_BATTERY_VOLT_L 1 // Byte 1 (low byte)
// CAN ID type: Mix of standard (0x4F, 0x55, 0x201) and extended (0x1001, 0x101F)
#define USE_EXTENDED_CAN_IDS true // Using 29-bit extended IDs for some messages
// ============================================================================
// PACKET STRUCTURE FOR UART TRANSMISSION
// ============================================================================
typedef struct __attribute__((packed)) {
uint8_t start_byte; // PACKET_START_BYTE (0xAA)
uint8_t version; // Protocol version
uint8_t length; // Length of data payload
automotive_data_t data; // Actual sensor data
uint8_t checksum; // XOR checksum of all bytes except start/end
uint8_t end_byte; // PACKET_END_BYTE (0x55)
} uart_packet_t;
// ============================================================================
// HELPER FUNCTIONS
// ============================================================================
/**
* Calculate XOR checksum for data buffer
* @param data Pointer to data buffer
* @param length Length of data to checksum
* @return XOR checksum value
*/
static inline uint8_t calculate_checksum(const uint8_t* data, size_t length) {
uint8_t checksum = 0;
for (size_t i = 0; i < length; i++) {
checksum ^= data[i];
}
return checksum;
}
/**
* Validate packet checksum
* @param packet Pointer to complete packet
* @return true if checksum is valid, false otherwise
*/
static inline bool validate_packet(const uart_packet_t* packet) {
if (packet->start_byte != PACKET_START_BYTE ||
packet->end_byte != PACKET_END_BYTE) {
return false;
}
// Calculate checksum over version, length, and data
size_t checksum_size = 2 + packet->length; // version(1) + length(1) + data(length)
uint8_t calculated = calculate_checksum((uint8_t*)&packet->version, checksum_size);
return (calculated == packet->checksum);
}
/**
* Convert temperature from CAN format to internal format
* @param can_value Raw CAN bus value
* @param offset Temperature offset (usually -40 for automotive)
* @return Temperature in 0.1°C units
*/
static inline int16_t can_to_temperature(uint8_t can_value, int16_t offset = -40) {
return (int16_t)((can_value + offset) * 10); // Convert to 0.1°C units
}
/**
* Convert voltage from CAN format to internal format
* @param can_value Raw CAN bus value (typically in 0.1V)
* @return Voltage in 0.01V units
*/
static inline uint16_t can_to_voltage(uint16_t can_value) {
return can_value * 10; // Convert 0.1V to 0.01V units
}
/**
* Map a raw 0x1D2 byte-0 lever code to a gear_position_t.
* D and S/M share code 0x78 — they're distinguished by byte 4 (CAN_BYTE_GEAR_MODE),
* not surfaced here yet (no enum slot; the engaged manual gear number needs a drive
* capture — see wiki/pages/open-questions.md).
* @param d0 Byte 0 of a 0x1D2 frame
* @return Gear position, or GEAR_UNKNOWN if the code isn't recognized
*/
static inline gear_position_t can_to_gear_lever(uint8_t d0) {
switch (d0) {
case GEAR_CODE_PARK: return GEAR_PARK;
case GEAR_CODE_REVERSE: return GEAR_REVERSE;
case GEAR_CODE_NEUTRAL: return GEAR_NEUTRAL;
case GEAR_CODE_DRIVE: return GEAR_DRIVE; // D or S/M
default: return GEAR_UNKNOWN;
}
}
/**
* Get human-readable gear position string
* @param gear Gear position enum value
* @return String representation of gear
*/
static inline const char* gear_to_string(gear_position_t gear) {
switch (gear) {
case GEAR_PARK: return "P";
case GEAR_REVERSE: return "R";
case GEAR_NEUTRAL: return "N";
case GEAR_DRIVE: return "D";
case GEAR_MANUAL_1: return "1";
case GEAR_MANUAL_2: return "2";
case GEAR_MANUAL_3: return "3";
case GEAR_MANUAL_4: return "4";
case GEAR_MANUAL_5: return "5";
case GEAR_MANUAL_6: return "6";
default: return "?";
}
}
/**
* Format temperature for display
* @param temp_x10 Temperature in 0.1°C units
* @param buffer Output buffer (minimum 10 characters)
* @return Pointer to buffer
*/
static inline char* format_temperature(int16_t temp_x10, char* buffer) {
sprintf(buffer, "%d.%d°C", temp_x10 / 10, abs(temp_x10 % 10));
return buffer;
}
/**
* Format voltage for display
* @param voltage_x100 Voltage in 0.01V units
* @param buffer Output buffer (minimum 10 characters)
* @return Pointer to buffer
*/
static inline char* format_voltage(uint16_t voltage_x100, char* buffer) {
sprintf(buffer, "%d.%02dV", voltage_x100 / 100, voltage_x100 % 100);
return buffer;
}
// ============================================================================
// DEFAULT/TEST VALUES
// ============================================================================
// Default values for testing (before CAN data is available)
#define DEFAULT_GEAR_POS GEAR_PARK
#define DEFAULT_GEARBOX_TEMP 850 // 85.0°C
#define DEFAULT_ENGINE_OIL_TEMP 920 // 92.0°C
#define DEFAULT_COOLANT_TEMP 890 // 89.0°C
#define DEFAULT_BATTERY_VOLTAGE 1245 // 12.45V
#define DEFAULT_FAULT_CODES 0x0000 // No faults
#endif // _AUTOMOTIVE_DATA_H