Ryan Malloy 8ba53630c0 Add OBD-II K-line support (ISO 9141/14230) with scanner example
New protocol handler alongside BMW I/K-Bus:
- KLineObd2: 5-baud slow init, fast init (TiniPulse), request/response
  with half-duplex echo clearing, PID convenience wrapper
- Obd2Pids.h: ~20 common PIDs with SAE J1979 decode helpers
- obd2_scanner.cpp: polls RPM, speed, coolant, throttle, voltage

Build config changes:
- config.h: KLINE_* defaults (10400/8N1/MOD256/no idle detect)
- platformio.ini: build_src_filter separates sketches, new
  [env:obd2-scanner] environment with KLINE_TX_INVERT=0
2026-02-13 05:46:07 -07:00

97 lines
2.3 KiB
C

#pragma once
// BMW I/K-Bus interface configuration
// All values can be overridden via platformio.ini build_flags (-D flags)
// --- UART pin assignments ---
#ifndef IBUS_RX_PIN
#define IBUS_RX_PIN 16
#endif
#ifndef IBUS_TX_PIN
#define IBUS_TX_PIN 17
#endif
#ifndef IBUS_LED_PIN
#define IBUS_LED_PIN 2
#endif
#ifndef IBUS_UART_NUM
#define IBUS_UART_NUM 1
#endif
// --- Protocol timing ---
// Bus must be quiet for this long before we can transmit (microseconds)
#ifndef IBUS_IDLE_TIMEOUT_US
#define IBUS_IDLE_TIMEOUT_US 1500
#endif
// Minimum gap between our own transmitted packets (milliseconds)
#ifndef IBUS_PACKET_GAP_MS
#define IBUS_PACKET_GAP_MS 10
#endif
// Periodic timer resolution for idle detection (microseconds)
// Smaller = more responsive but more CPU overhead. 250us gives
// worst-case detection at idle_timeout + 250us = 1.75ms, well
// within the ~10ms inter-packet budget on a busy bus.
#ifndef IBUS_IDLE_CHECK_US
#define IBUS_IDLE_CHECK_US 250
#endif
// --- Buffer sizes ---
#ifndef IBUS_RX_BUFFER_SIZE
#define IBUS_RX_BUFFER_SIZE 256
#endif
#ifndef IBUS_TX_BUFFER_SIZE
#define IBUS_TX_BUFFER_SIZE 128
#endif
// --- Protocol constants ---
#define IBUS_BAUD 9600
#define IBUS_FRAMING SERIAL_8E1
// Message length byte must be between these values (inclusive)
#define IBUS_MIN_LENGTH 0x03
#define IBUS_MAX_LENGTH 0x24
// Maximum raw message size (source + length + up to 36 body bytes)
#define IBUS_MAX_MSG 40
// ===================================================================
// OBD-II K-line configuration (ISO 9141 / ISO 14230)
// ===================================================================
// --- UART pin assignments (defaults to same as IBUS — shared hardware) ---
#ifndef KLINE_RX_PIN
#define KLINE_RX_PIN IBUS_RX_PIN
#endif
#ifndef KLINE_TX_PIN
#define KLINE_TX_PIN IBUS_TX_PIN
#endif
#ifndef KLINE_LED_PIN
#define KLINE_LED_PIN IBUS_LED_PIN
#endif
#ifndef KLINE_UART_NUM
#define KLINE_UART_NUM IBUS_UART_NUM
#endif
// --- Protocol constants ---
#ifndef KLINE_BAUD
#define KLINE_BAUD 10400
#endif
#ifndef KLINE_FRAMING
#define KLINE_FRAMING SERIAL_8N1
#endif
// TX inversion: 1 for optocoupler (PC817), 0 for transistor circuit
#ifndef KLINE_TX_INVERT
#define KLINE_TX_INVERT 0
#endif
// ISO 9141 default ECU address for 5-baud slow init
#ifndef KLINE_INIT_ADDR
#define KLINE_INIT_ADDR 0x33
#endif