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:
@@ -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