No invented data: shared structure starts with nothing valid, cleared on CAN stop and bus silence; DEFAULT_* removed from the shared header
Firmware / build (push) Successful in 7m19s

This commit is contained in:
2026-09-28 09:30:59 +02:00
parent ae9327d91b
commit 63d64362e8
4 changed files with 34 additions and 32 deletions
+19
View File
@@ -42,6 +42,7 @@ 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;
@@ -53,6 +54,22 @@ static bool g_verbose_decode = false;
// Guarded by g_data_mutex along with g_automotive_data.
static driveline_t g_driveline = {0};
/**
* Forget every CAN-derived value: nothing may be shown or sent that the bus
* did not say recently. Called when CAN stops and when the bus goes silent.
*/
static void clearSharedData() {
if (xSemaphoreTake(UARTManager::g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
uint8_t dbg = UARTManager::g_automotive_data.debug_enabled;
memset(&UARTManager::g_automotive_data, 0, sizeof(UARTManager::g_automotive_data));
UARTManager::g_automotive_data.gear_lever = LEVER_UNKNOWN;
UARTManager::g_automotive_data.debug_enabled = dbg;
xSemaphoreGive(UARTManager::g_data_mutex);
}
memset(&g_driveline, 0, sizeof(g_driveline));
g_driveline.lever = GEAR_UNKNOWN;
}
// Last gear LEVER code seen on 0x1D2, for the lever-vs-gearbox cross-check.
static gear_position_t g_last_lever = GEAR_UNKNOWN;
// When the current disagreement started (0 = currently in agreement).
@@ -523,6 +540,7 @@ static void canTask(void* parameter) {
deinitTWAI();
g_can_active = false;
g_uart_auto_stopped = false; // Reset auto-stop flag
clearSharedData();
// Auto-stop UART transmission (CAN drives UART)
UARTManager::linkToCANState(false);
@@ -595,6 +613,7 @@ static void canTask(void* parameter) {
// CAN data is stale - auto-stop UART to prevent transmitting stale data
ESP_LOGW("CANTask", "⚠ CAN timeout (%dms since last message), auto-stopping UART", time_since_last_rx);
UARTManager::linkToCANState(false);
clearSharedData(); // stale values must not come back as "valid" later
g_uart_auto_stopped = true;
}
}
+8 -21
View File
@@ -50,30 +50,17 @@ static volatile bool g_port_ready = false; // Serial1.begin() done (in the tas
// ============================================================================
/**
* @brief Seed the shared structure until CAN data arrives.
* @note The temperatures/voltage are marked valid so a bench without CAN still
* exercises B's display. RPM and gear nibbles are NOT marked valid: the
* tach output must never run on invented data.
* @brief Start from NOTHING valid. Until a CAN frame fills a field, its valid
* flag stays clear and B shows no value for it (user decision
* 2026-09-28: no invented numbers, ever - the old bench defaults of
* 89 / 85 degC and 12.45 V looked like real readings).
*/
static void initializeDefaultData() {
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
g_automotive_data.gear_lever = DEFAULT_GEAR_LEVER;
g_automotive_data.gear_nibbles = 0;
g_automotive_data.engine_rpm = 0;
g_automotive_data.gearbox_oil_temp = DEFAULT_GEARBOX_TEMP;
g_automotive_data.engine_oil_temp = DEFAULT_ENGINE_OIL_TEMP;
g_automotive_data.coolant_temp = DEFAULT_COOLANT_TEMP;
g_automotive_data.battery_voltage = DEFAULT_BATTERY_VOLTAGE;
g_automotive_data.alerts = DEFAULT_ALERTS;
g_automotive_data.valid_flags = VALID_FLAG_GEAR_POS |
VALID_FLAG_GEARBOX_TEMP |
VALID_FLAG_ENGINE_TEMP |
VALID_FLAG_COOLANT_TEMP |
VALID_FLAG_BATTERY_VOLT |
VALID_FLAG_ALERTS;
g_automotive_data.debug_enabled = 0;
uint8_t dbg = g_automotive_data.debug_enabled;
memset(&g_automotive_data, 0, sizeof(g_automotive_data));
g_automotive_data.gear_lever = LEVER_UNKNOWN;
g_automotive_data.debug_enabled = dbg;
xSemaphoreGive(g_data_mutex);
}
}