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:
@@ -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) {
|
||||
|
||||
+12
-1
@@ -8,6 +8,7 @@
|
||||
#include "modules/settings_manager.h" // Settings manager module
|
||||
#include "modules/ui_state_manager.h" // UI state manager module
|
||||
#include "modules/meters_manager.h" // Meters display manager module
|
||||
#include "modules/serial_lock.h" // Global serial write mutex
|
||||
|
||||
// ============================================================================
|
||||
// MINIMAL LVGL + UI SETTINGS + WIFI + UART
|
||||
@@ -166,9 +167,17 @@ void initializeHomeScreenMeters() {
|
||||
// ============================================================================
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
// Baud is ignored under native USB CDC (Serial = HWCDC); kept high so the
|
||||
// CANRAW dump isn't throttled if Serial ever falls back to UART0.
|
||||
// NOTE: do NOT add `while(!Serial)` — the board must boot headless in-car
|
||||
// with no host attached.
|
||||
Serial.begin(921600);
|
||||
delay(2000); // Longer delay for serial to stabilize
|
||||
|
||||
// Create the global serial write mutex before any task starts, so CAN raw
|
||||
// frames and other tasks' log lines never interleave on the shared port.
|
||||
SerialLock::init();
|
||||
|
||||
// Use ESP_LOG instead of Serial.println to match library output
|
||||
ESP_LOGI("MAIN", "=== ESP32-S3-LCD-5B Minimal Setup ===");
|
||||
ESP_LOGI("MAIN", "Firmware compiled: %s %s", __DATE__, __TIME__);
|
||||
@@ -229,7 +238,9 @@ void loop()
|
||||
|
||||
// Debug: Print heartbeat every 5 seconds
|
||||
if (millis() - last_print > 5000) {
|
||||
SerialLock::lock();
|
||||
ESP_LOGI("LOOP", "Heartbeat - system running");
|
||||
SerialLock::unlock();
|
||||
last_print = millis();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* serial_lock.cpp
|
||||
*
|
||||
* Implementation of the global serial write mutex. See serial_lock.h.
|
||||
*/
|
||||
|
||||
#include "modules/serial_lock.h"
|
||||
|
||||
namespace SerialLock {
|
||||
|
||||
static SemaphoreHandle_t g_serial_mutex = NULL;
|
||||
|
||||
void init() {
|
||||
if (g_serial_mutex == NULL) {
|
||||
g_serial_mutex = xSemaphoreCreateMutex();
|
||||
}
|
||||
}
|
||||
|
||||
bool lock(TickType_t timeout) {
|
||||
// Not initialized yet (early boot): don't block, just proceed.
|
||||
if (g_serial_mutex == NULL) {
|
||||
return true;
|
||||
}
|
||||
return xSemaphoreTake(g_serial_mutex, timeout) == pdTRUE;
|
||||
}
|
||||
|
||||
void unlock() {
|
||||
if (g_serial_mutex != NULL) {
|
||||
xSemaphoreGive(g_serial_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace SerialLock
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* serial_lock.h
|
||||
*
|
||||
* A single global mutex that serializes writes to the USB-CDC `Serial` port.
|
||||
*
|
||||
* Multiple FreeRTOS tasks (CAN raw logger, UART task, main loop heartbeat, ...)
|
||||
* write to the same serial output. Without coordination their writes interleave
|
||||
* and splice mid-line, corrupting the CANRAW capture stream. Every frequent
|
||||
* writer takes this lock around its full line so lines never interleave.
|
||||
*
|
||||
* Usage:
|
||||
* SerialLock::lock();
|
||||
* Serial.write(...); // or ESP_LOGx(...)
|
||||
* SerialLock::unlock();
|
||||
*
|
||||
* Call SerialLock::init() once early in setup(), before any task starts. Before
|
||||
* init (or if creation fails) lock()/unlock() are safe no-ops.
|
||||
*/
|
||||
|
||||
#ifndef _SERIAL_LOCK_H
|
||||
#define _SERIAL_LOCK_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
namespace SerialLock {
|
||||
/** Create the mutex. Idempotent; call once in setup() before tasks start. */
|
||||
void init();
|
||||
|
||||
/**
|
||||
* Acquire the serial lock.
|
||||
* @param timeout ticks to wait (default: wait forever)
|
||||
* @return true if acquired (also true if the lock isn't initialized yet)
|
||||
*/
|
||||
bool lock(TickType_t timeout = portMAX_DELAY);
|
||||
|
||||
/** Release the serial lock (no-op if not held / not initialized). */
|
||||
void unlock();
|
||||
}
|
||||
|
||||
#endif // _SERIAL_LOCK_H
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "uart_manager.h"
|
||||
#include "settings_manager.h"
|
||||
#include "serial_lock.h"
|
||||
|
||||
namespace UARTManager {
|
||||
|
||||
@@ -115,6 +116,8 @@ static bool sendUARTPacket() {
|
||||
|
||||
// Debug output - first 5 packets, then every 10
|
||||
if (g_uart_stats.packets_sent <= 5 || g_uart_stats.packets_sent % 10 == 0) {
|
||||
// Hold the serial lock so this line can't splice into a CANRAW frame
|
||||
SerialLock::lock();
|
||||
ESP_LOGI("UARTTask", "✓ TX Packet #%d (Gear:%s, Temps:%d.%d/%d.%d/%d.%d°C, Batt:%d.%02dV, Debug:%s) [%d bytes on GPIO%d]",
|
||||
packet.data.packet_id,
|
||||
gear_to_string((gear_position_t)packet.data.gear_position),
|
||||
@@ -124,6 +127,7 @@ static bool sendUARTPacket() {
|
||||
packet.data.battery_voltage / 100, packet.data.battery_voltage % 100,
|
||||
packet.data.debug_enabled ? "ON" : "OFF",
|
||||
bytes_written, UART_TX_PIN);
|
||||
SerialLock::unlock();
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user