5fe313b7fa
- protocol header v2 copied byte-identical from the 5B repo - uart_receiver: processFrame() shared by the UART framing task and ESP-NOW, version check, dedupe on packet_id+timestamp, per-transport liveness, CONFIG cache with sequence number - espnow_receiver: STA mode, channel hopping while silent, locks on frames - gauge_outputs: tach on IO7 (LEDC ch2, 14-bit), coolant PWM on IO8 (LEDC ch4, 8-bit), config cached in NVS, overrides applied but not saved - main: gear label from lever + engaged/target (pulses while shifting, red on mismatch), icons driven by A's alert bits, gauge update at 5 Hz, link stats - wiki: temp_stage.cfd (user's old CANNY program) decoded as prior art; controller-link, link-redundancy, patrol-gauge-outputs, open-questions updated with the bench-verified v2 results and calibration procedure Co-Authored-By: Claude <noreply@anthropic.com>
344 lines
12 KiB
C++
344 lines
12 KiB
C++
/**
|
|
* Link Receiver Module Implementation (Controller B)
|
|
*/
|
|
|
|
#include "modules/uart_receiver.h"
|
|
#include "modules/debug_monitor.h"
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/task.h>
|
|
#include <freertos/semphr.h>
|
|
|
|
namespace UARTReceiver {
|
|
|
|
#define UART_SERIAL_PORT Serial1
|
|
#define UART_RX_BUFFER_SIZE 256
|
|
|
|
// UART framing state machine
|
|
typedef enum {
|
|
STATE_WAIT_START,
|
|
STATE_READ_VERSION,
|
|
STATE_READ_TYPE,
|
|
STATE_READ_LENGTH,
|
|
STATE_READ_PAYLOAD,
|
|
STATE_READ_CHECKSUM,
|
|
STATE_READ_END
|
|
} rx_state_t;
|
|
|
|
static automotive_data_t g_latest_data = {0};
|
|
static link_config_t g_config;
|
|
static uint32_t g_config_seq = 0;
|
|
static uart_rx_stats_t g_rx_stats = {0};
|
|
static SemaphoreHandle_t g_data_mutex = NULL;
|
|
static TaskHandle_t g_uart_task_handle = NULL;
|
|
static bool g_initialized = false;
|
|
|
|
// Dedupe key of the last applied DATA frame
|
|
static bool g_have_last = false;
|
|
static uint8_t g_last_id = 0;
|
|
static uint32_t g_last_ts = 0;
|
|
|
|
static const char* sourceName(link_source_t s) {
|
|
switch (s) {
|
|
case LINK_SOURCE_UART: return "uart";
|
|
case LINK_SOURCE_ESPNOW: return "espnow";
|
|
default: return "none";
|
|
}
|
|
}
|
|
|
|
static void bump(uint32_t* counter) {
|
|
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
|
|
(*counter)++;
|
|
xSemaphoreGive(g_data_mutex);
|
|
}
|
|
}
|
|
|
|
void processFrame(const uint8_t* frame, size_t len, link_source_t source) {
|
|
uint8_t type = 0, plen = 0;
|
|
const uint8_t* payload = NULL;
|
|
frame_status_t st = frame_parse(frame, len, &type, &payload, &plen);
|
|
|
|
if (st != FRAME_OK) {
|
|
switch (st) {
|
|
case FRAME_ERR_VERSION:
|
|
bump(&g_rx_stats.version_errors);
|
|
if (g_rx_stats.version_errors <= 3 || g_rx_stats.version_errors % 50 == 0) {
|
|
Serial.printf("[Link] Version mismatch on %s: got v%u, this firmware speaks v%u (%lu so far)\n",
|
|
sourceName(source), frame[1], PACKET_VERSION,
|
|
(unsigned long)g_rx_stats.version_errors);
|
|
}
|
|
break;
|
|
case FRAME_ERR_CHECKSUM:
|
|
bump(&g_rx_stats.checksum_errors);
|
|
Serial.printf("[Link] Checksum error on %s\n", sourceName(source));
|
|
break;
|
|
default:
|
|
bump(&g_rx_stats.framing_errors);
|
|
Serial.printf("[Link] Framing error %d on %s (len %u)\n", (int)st, sourceName(source), (unsigned)len);
|
|
break;
|
|
}
|
|
return;
|
|
}
|
|
|
|
uint32_t now = millis();
|
|
|
|
if (type == PACKET_TYPE_CONFIG) {
|
|
link_config_t cfg;
|
|
memcpy(&cfg, payload, sizeof(cfg));
|
|
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(50)) == pdTRUE) {
|
|
bool changed = (g_config_seq == 0) || (cfg.cfg_version != g_config.cfg_version);
|
|
if (changed) {
|
|
g_config = cfg;
|
|
g_config_seq++;
|
|
}
|
|
g_rx_stats.config_received++;
|
|
if (source == LINK_SOURCE_UART) g_rx_stats.last_uart_time = now;
|
|
else if (source == LINK_SOURCE_ESPNOW) g_rx_stats.last_espnow_time = now;
|
|
xSemaphoreGive(g_data_mutex);
|
|
if (changed) {
|
|
Serial.printf("[Link] CONFIG v%u received on %s\n", cfg.cfg_version, sourceName(source));
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// DATA
|
|
automotive_data_t d;
|
|
memcpy(&d, payload, sizeof(d));
|
|
|
|
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(50)) != pdTRUE) return;
|
|
|
|
if (source == LINK_SOURCE_UART) { g_rx_stats.packets_uart++; g_rx_stats.last_uart_time = now; }
|
|
else if (source == LINK_SOURCE_ESPNOW) { g_rx_stats.packets_espnow++; g_rx_stats.last_espnow_time = now; }
|
|
|
|
bool dup = g_have_last && d.packet_id == g_last_id && d.timestamp_ms == g_last_ts;
|
|
if (dup) {
|
|
g_rx_stats.duplicates++;
|
|
xSemaphoreGive(g_data_mutex);
|
|
return;
|
|
}
|
|
|
|
g_have_last = true;
|
|
g_last_id = d.packet_id;
|
|
g_last_ts = d.timestamp_ms;
|
|
g_latest_data = d;
|
|
g_rx_stats.packets_received++;
|
|
g_rx_stats.last_packet_time = now;
|
|
g_rx_stats.last_source = source;
|
|
uint32_t n = g_rx_stats.packets_received;
|
|
xSemaphoreGive(g_data_mutex);
|
|
|
|
DebugMonitor::setVisible(d.debug_enabled != 0);
|
|
|
|
if (n % 25 == 0) {
|
|
char gear[8], gearbox_buf[16], coolant_buf[16], voltage_buf[16];
|
|
gear_compose(d.gear_lever, gear_engaged(d.gear_nibbles), gear, sizeof(gear));
|
|
Serial.printf("[Link] RX #%u via %s | Gear:%s (t%u/e%u) | RPM:%u | GB:%s | Cool:%s | Batt:%s | Alerts:0x%02X | Valid:0x%02X\n",
|
|
d.packet_id, sourceName(source), gear,
|
|
gear_target(d.gear_nibbles), gear_engaged(d.gear_nibbles),
|
|
d.engine_rpm,
|
|
format_temperature(d.gearbox_oil_temp, gearbox_buf),
|
|
format_temperature(d.coolant_temp, coolant_buf),
|
|
format_voltage(d.battery_voltage, voltage_buf),
|
|
d.alerts, d.valid_flags);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* UART1 byte-stream framing task (Core 0)
|
|
*/
|
|
static void uartRxTask(void* parameter) {
|
|
uint8_t rx_buffer[PACKET_MAX_SIZE];
|
|
rx_state_t state = STATE_WAIT_START;
|
|
size_t bytes_read = 0;
|
|
uint8_t expected_length = 0;
|
|
|
|
Serial.println("[UARTReceiver] Task started on Core 0");
|
|
|
|
UART_SERIAL_PORT.setRxBufferSize(UART_RX_BUFFER_SIZE);
|
|
UART_SERIAL_PORT.begin(UART_BAUD_RATE, SERIAL_8N1, UART_RX_PIN, UART_TX_PIN);
|
|
Serial.printf("[UARTReceiver] UART1 initialized: %d baud, RX=GPIO%d, protocol v%u\n",
|
|
UART_BAUD_RATE, UART_RX_PIN, PACKET_VERSION);
|
|
|
|
while (true) {
|
|
while (UART_SERIAL_PORT.available()) {
|
|
uint8_t byte = UART_SERIAL_PORT.read();
|
|
g_rx_stats.bytes_received++; // statistic only; a torn read is harmless
|
|
|
|
switch (state) {
|
|
case STATE_WAIT_START:
|
|
if (byte == PACKET_START_BYTE) {
|
|
rx_buffer[0] = byte;
|
|
bytes_read = 1;
|
|
state = STATE_READ_VERSION;
|
|
} else {
|
|
bump(&g_rx_stats.framing_errors);
|
|
}
|
|
break;
|
|
|
|
case STATE_READ_VERSION:
|
|
rx_buffer[bytes_read++] = byte;
|
|
state = STATE_READ_TYPE;
|
|
break;
|
|
|
|
case STATE_READ_TYPE:
|
|
rx_buffer[bytes_read++] = byte;
|
|
state = STATE_READ_LENGTH;
|
|
break;
|
|
|
|
case STATE_READ_LENGTH:
|
|
rx_buffer[bytes_read++] = byte;
|
|
expected_length = byte;
|
|
if (expected_length > PACKET_MAX_PAYLOAD) {
|
|
bump(&g_rx_stats.framing_errors);
|
|
state = STATE_WAIT_START;
|
|
bytes_read = 0;
|
|
} else if (expected_length == 0) {
|
|
state = STATE_READ_CHECKSUM;
|
|
} else {
|
|
state = STATE_READ_PAYLOAD;
|
|
}
|
|
break;
|
|
|
|
case STATE_READ_PAYLOAD:
|
|
rx_buffer[bytes_read++] = byte;
|
|
if (bytes_read >= (size_t)(PACKET_HEADER_SIZE + expected_length)) {
|
|
state = STATE_READ_CHECKSUM;
|
|
}
|
|
break;
|
|
|
|
case STATE_READ_CHECKSUM:
|
|
rx_buffer[bytes_read++] = byte;
|
|
state = STATE_READ_END;
|
|
break;
|
|
|
|
case STATE_READ_END:
|
|
rx_buffer[bytes_read++] = byte;
|
|
processFrame(rx_buffer, bytes_read, LINK_SOURCE_UART);
|
|
state = STATE_WAIT_START;
|
|
bytes_read = 0;
|
|
break;
|
|
}
|
|
}
|
|
vTaskDelay(pdMS_TO_TICKS(2));
|
|
}
|
|
}
|
|
|
|
bool init() {
|
|
if (g_initialized) {
|
|
Serial.println("[UARTReceiver] Already initialized");
|
|
return true;
|
|
}
|
|
|
|
Serial.println("[UARTReceiver] Initializing link receiver...");
|
|
|
|
link_config_defaults(&g_config);
|
|
|
|
g_data_mutex = xSemaphoreCreateMutex();
|
|
if (g_data_mutex == NULL) {
|
|
Serial.println("[UARTReceiver] ERROR: Failed to create mutex");
|
|
return false;
|
|
}
|
|
|
|
BaseType_t result = xTaskCreatePinnedToCore(
|
|
uartRxTask, "UART_RX_Task", 4096, NULL, 1, &g_uart_task_handle, 0);
|
|
if (result != pdPASS) {
|
|
Serial.println("[UARTReceiver] ERROR: Failed to create task");
|
|
return false;
|
|
}
|
|
|
|
g_initialized = true;
|
|
Serial.println("[UARTReceiver] Initialization complete");
|
|
return true;
|
|
}
|
|
|
|
automotive_data_t getLatestData() {
|
|
automotive_data_t data;
|
|
memset(&data, 0, sizeof(data));
|
|
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
|
data = g_latest_data;
|
|
xSemaphoreGive(g_data_mutex);
|
|
}
|
|
return data;
|
|
}
|
|
|
|
uart_rx_stats_t getStats() {
|
|
uart_rx_stats_t stats;
|
|
memset(&stats, 0, sizeof(stats));
|
|
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
|
stats = g_rx_stats;
|
|
xSemaphoreGive(g_data_mutex);
|
|
}
|
|
return stats;
|
|
}
|
|
|
|
bool isConnected(uint32_t timeout_ms) {
|
|
if (!g_initialized) return false;
|
|
bool connected = false;
|
|
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
|
if (g_rx_stats.packets_received > 0) {
|
|
connected = (millis() - g_rx_stats.last_packet_time) < timeout_ms;
|
|
}
|
|
xSemaphoreGive(g_data_mutex);
|
|
}
|
|
return connected;
|
|
}
|
|
|
|
bool isSourceAlive(link_source_t source, uint32_t timeout_ms) {
|
|
if (!g_initialized) return false;
|
|
bool alive = false;
|
|
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
|
uint32_t t = (source == LINK_SOURCE_UART) ? g_rx_stats.last_uart_time
|
|
: (source == LINK_SOURCE_ESPNOW) ? g_rx_stats.last_espnow_time : 0;
|
|
alive = (t != 0) && ((millis() - t) < timeout_ms);
|
|
xSemaphoreGive(g_data_mutex);
|
|
}
|
|
return alive;
|
|
}
|
|
|
|
link_config_t getConfig() {
|
|
link_config_t c;
|
|
link_config_defaults(&c);
|
|
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
|
c = g_config;
|
|
xSemaphoreGive(g_data_mutex);
|
|
}
|
|
return c;
|
|
}
|
|
|
|
uint32_t getConfigSeq() {
|
|
uint32_t s = 0;
|
|
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
|
s = g_config_seq;
|
|
xSemaphoreGive(g_data_mutex);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
void resetStats() {
|
|
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
|
uint32_t keep_last = g_rx_stats.last_packet_time;
|
|
uint32_t keep_uart = g_rx_stats.last_uart_time;
|
|
uint32_t keep_espnow = g_rx_stats.last_espnow_time;
|
|
link_source_t keep_src = g_rx_stats.last_source;
|
|
memset(&g_rx_stats, 0, sizeof(g_rx_stats));
|
|
g_rx_stats.last_packet_time = keep_last;
|
|
g_rx_stats.last_uart_time = keep_uart;
|
|
g_rx_stats.last_espnow_time = keep_espnow;
|
|
g_rx_stats.last_source = keep_src;
|
|
xSemaphoreGive(g_data_mutex);
|
|
}
|
|
Serial.println("[UARTReceiver] Statistics reset");
|
|
}
|
|
|
|
uint32_t getTimeSinceLastPacket() {
|
|
if (!g_initialized) return UINT32_MAX;
|
|
uint32_t t = UINT32_MAX;
|
|
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
|
if (g_rx_stats.packets_received > 0) t = millis() - g_rx_stats.last_packet_time;
|
|
xSemaphoreGive(g_data_mutex);
|
|
}
|
|
return t;
|
|
}
|
|
|
|
} // namespace UARTReceiver
|