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>
This commit is contained in:
2026-07-16 12:24:25 +02:00
parent f7a2d1c3bd
commit 87801c593c
19 changed files with 20929 additions and 26 deletions
+70 -6
View File
@@ -8,6 +8,7 @@
#include "drivers/can_driver.h"
#include "modules/uart_manager.h"
#include "modules/settings_manager.h"
#include "modules/serial_lock.h"
#include "esp_log.h"
// Import shared data from UARTManager namespace
@@ -33,6 +34,13 @@ static QueueHandle_t g_can_event_queue = NULL;
static can_stats_t g_can_stats = {0};
static bool g_uart_auto_stopped = false; // Track if UART was auto-stopped due to CAN timeout
// Raw frame logging: capture EVERY frame (candump-style) for offline CAN ID
// discovery. Default ON since discovery is the current priority.
static bool g_raw_logging = true;
// Verbose per-frame DECODED prose logging. Default OFF: it's redundant with
// (and would drown) the raw capture stream. Enable only for quick eyeballing.
static bool g_verbose_decode = false;
// ============================================================================
// PRIVATE FUNCTIONS
// ============================================================================
@@ -120,6 +128,44 @@ static bool parseCANMessage(const twai_message_t& message) {
return parsed;
}
/**
* @brief Emit one raw CAN frame as a compact, greppable, re-parseable line.
*
* Format (CSV, one frame per line):
* CANRAW,<ts_ms>,<id_hex>,<extd>,<dlc>,<data_hex>
* where:
* ts_ms = millis() timestamp (relative to boot)
* id_hex = arbitration ID in hex, no 0x prefix (3 digits min for 11-bit)
* extd = 1 if 29-bit extended ID, 0 if 11-bit standard
* dlc = data length code (0..8)
* data_hex = payload bytes, uppercase hex, no separators (e.g. 1122AABB)
*
* The "CANRAW," prefix lets you filter these out of the mixed ESP_LOG serial
* stream: pio device monitor > drive.log then grep CANRAW, drive.log
*
* @param message Received TWAI message
*/
static void emitRawFrame(const twai_message_t& message) {
char buf[72];
int n = snprintf(buf, sizeof(buf), "CANRAW,%lu,%03lX,%u,%u,",
(unsigned long)millis(),
(unsigned long)message.identifier,
(unsigned)(message.extd ? 1 : 0),
(unsigned)message.data_length_code);
for (int i = 0; i < message.data_length_code && n < (int)sizeof(buf) - 4; i++) {
n += snprintf(buf + n, sizeof(buf) - n, "%02X", message.data[i]);
}
// Append the line terminator into the SAME buffer so the whole frame goes
// out in one Serial.write() — no other task can splice into the middle.
buf[n++] = '\r';
buf[n++] = '\n';
// Serialize against all other serial writers so lines never interleave.
SerialLock::lock();
Serial.write((const uint8_t*)buf, n);
SerialLock::unlock();
}
/**
* @brief Handle received CAN message
* @param message TWAI message structure
@@ -128,6 +174,11 @@ static void handleRxMessage(twai_message_t& message) {
g_can_stats.messages_received++;
g_can_stats.last_receive_time = millis();
// Raw capture FIRST, before parse — logs every frame including unknown IDs
if (g_raw_logging) {
emitRawFrame(message);
}
// Auto-restart UART if it was stopped due to CAN timeout
if (g_uart_auto_stopped) {
ESP_LOGI("CANDrv", "✓ CAN messages resumed, auto-restarting UART");
@@ -139,9 +190,8 @@ static void handleRxMessage(twai_message_t& message) {
if (parseCANMessage(message)) {
g_can_stats.messages_parsed++;
// DETAILED LOGGING (TEMPORARY FOR DEBUGGING)
// Log EVERY parsed message with actual values
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
// Per-frame decoded prose — off by default (redundant with raw capture)
if (g_verbose_decode && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
ESP_LOGI("CANDrv", "✓ RX CAN ID=0x%X → Coolant:%d.%d°C, Oil:%d.%d°C, Gearbox:%d.%d°C, Batt:%d.%02dV, Gear:%d [%d/%d]",
message.identifier,
g_automotive_data.coolant_temp / 10, abs(g_automotive_data.coolant_temp % 10),
@@ -153,8 +203,8 @@ static void handleRxMessage(twai_message_t& message) {
g_can_stats.messages_received);
xSemaphoreGive(g_data_mutex);
}
} else {
// Unknown message ID - log ALL unknown messages for debugging
} else if (g_verbose_decode) {
// Unknown message ID (raw capture already logged it; this is just prose)
ESP_LOGW("CANDrv", "⚠ Unknown CAN ID=0x%X (DLC=%d) [%d total]",
message.identifier,
message.data_length_code,
@@ -190,7 +240,7 @@ static bool initTWAI() {
ESP_LOGE("CANDrv", "Failed to install TWAI driver");
return false;
}
ESP_LOGI("CANDrv", "TWAI driver installed (GPIO%d/GPIO%d, 500Kbps, LISTEN_ONLY)",
ESP_LOGI("CANDrv", "TWAI driver installed (GPIO%d/GPIO%d, 500Kbps, NORMAL/ACK)",
CAN_TX_PIN, CAN_RX_PIN);
// Start TWAI driver
@@ -390,6 +440,20 @@ bool isActive() {
return g_can_active;
}
void setRawLogging(bool enabled) {
g_raw_logging = enabled;
ESP_LOGI("CANDrv", "Raw frame logging %s", enabled ? "ENABLED" : "disabled");
}
bool isRawLogging() {
return g_raw_logging;
}
void setVerboseDecode(bool enabled) {
g_verbose_decode = enabled;
ESP_LOGI("CANDrv", "Verbose decode logging %s", enabled ? "ENABLED" : "disabled");
}
void onCANToggle(lv_event_t *e) {
lv_obj_t *target = lv_event_get_target(e);
if (target == NULL) {