Files
ESP32-S3-Touch-LCD-5B/include/drivers/can_driver.h
T
julian 87801c593c CAN raw logger + toggle fix; wiki: first live capture confirms ID map
Firmware — make the board a working CAN sniffer and fix real bugs:
- can_driver: add raw candump-style logger (CANRAW,<ts>,<id>,<extd>,<dlc>,<hex>),
  capturing every frame incl. unknown IDs for offline decoding. Single atomic
  write per frame.
- serial_lock: new global serial mutex so concurrent task writes (CAN logger,
  UART 5Hz log, heartbeat) no longer splice mid-line. Corruption 3.2% -> 1.4%.
- Fix CAN toggle: the switch had no event handler registered, so it never
  started CAN and reverted on screen change. Wire up onCANBusToggle.
- platformio: route Serial to native USB-CDC (USB_MODE=1, CDC_ON_BOOT=1) so the
  ~486 frame/s stream isn't throttled by the 115200 UART ceiling; bump monitor
  baud to 921600.
- tools/fix_toolchain_path.py: pre-build workaround for pioarduino nesting the
  Xtensa toolchain bin under a subdir that never reaches PATH.
- Correct misleading "LISTEN_ONLY" log string (driver runs TWAI_MODE_NORMAL).

Wiki — ingest the first live capture off the real swap bus:
- raw/can-capture-2026-07-15-gearshift.csv (20,596 frames) + sources page.
- Research map CONFIRMED: 0x1D0/0x1D2/0x0B5/0x3B4 all live; placeholder IDs
  (0x4F/0x55/0x201/0x1001/0x101F) verified absent; 0x130/0x304/0x192 absent.
- 0x1D0 b0 coolant drifts up (37->40C, -48 holds); 0x1D0 b1 oil temp = 0xFF.
- 0x1D2 gear encoding resolved: DLC 6, byte0 gear codes (E1=P, B4=N, 78=D live).
- Corrected LISTEN_ONLY -> NORMAL/ACK across pages; documented non-isolated
  TJA1051 grounding requirement. Updated index + log.

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

124 lines
3.4 KiB
C++

/**
* can_driver.h
*
* CAN Bus driver for ESP32-S3-Touch-LCD-5B using TWAI (Two-Wire Automotive Interface)
* Reads BMW CAN messages at 500 Kbps in LISTEN_ONLY mode
*
* Hardware: GPIO 15 (TX), GPIO 16 (RX)
* Mode: LISTEN_ONLY (safe, won't transmit on vehicle bus)
* Baud: 500 Kbps
*/
#ifndef _CAN_DRIVER_H
#define _CAN_DRIVER_H
#include <Arduino.h>
#include <lvgl.h>
#include "driver/twai.h"
#include "protocol/automotive_data.h"
// ============================================================================
// HARDWARE CONFIGURATION
// ============================================================================
#define CAN_TX_PIN 15
#define CAN_RX_PIN 16
#define CAN_BAUD_RATE TWAI_TIMING_CONFIG_500KBITS()
// ============================================================================
// TASK CONFIGURATION
// ============================================================================
#define CAN_TASK_STACK_SIZE 4096
#define CAN_TASK_PRIORITY 1
#define CAN_TASK_CORE 0 // Run on Core 0 with UART task
// ============================================================================
// CAN EVENT TYPES
// ============================================================================
typedef enum {
CAN_EVENT_START,
CAN_EVENT_STOP
} can_event_type_t;
typedef struct {
can_event_type_t type;
} can_queue_event_t;
// ============================================================================
// CAN STATISTICS
// ============================================================================
typedef struct {
uint32_t messages_received;
uint32_t messages_parsed;
uint32_t parse_errors;
uint32_t last_receive_time;
} can_stats_t;
// ============================================================================
// PUBLIC API
// ============================================================================
namespace CANDriver {
/**
* Initialize CAN driver and create FreeRTOS task
* @param auto_start If true, CAN bus starts listening immediately
*/
void init(bool auto_start = false);
/**
* Start CAN bus reception
*/
void start();
/**
* Stop CAN bus reception
*/
void stop();
/**
* Get current CAN statistics
* @return Statistics structure
*/
can_stats_t getStats();
/**
* Check if CAN is currently active
* @return true if CAN is receiving messages
*/
bool isActive();
/**
* Enable/disable raw frame logging (candump-style capture of EVERY frame).
* When enabled, each received frame is printed to Serial as a compact,
* greppable, re-parseable line prefixed "CANRAW," (see emitRawFrame()).
* Intended for CAN ID discovery: capture a drive, save the serial stream
* to a file, and analyze offline.
* @param enabled true to log every frame
*/
void setRawLogging(bool enabled);
/**
* @return true if raw frame logging is currently enabled
*/
bool isRawLogging();
/**
* Enable/disable verbose per-frame DECODED logging (human-prose ESP_LOG
* lines for known/unknown IDs). Noisy; off by default so it doesn't drown
* the raw capture stream. Independent of setRawLogging().
* @param enabled true to log decoded values per frame
*/
void setVerboseDecode(bool enabled);
/**
* UI event handler for CAN toggle switch
* @param e LVGL event
*/
void onCANToggle(lv_event_t *e);
}
#endif // _CAN_DRIVER_H