This commit is contained in:
2026-09-21 10:28:35 +02:00
commit b6fb01ba73
54 changed files with 33982 additions and 0 deletions
+313
View File
@@ -0,0 +1,313 @@
/**
* UART Receiver Module Implementation
*
* Receives automotive data packets from Controller A via UART1.
* Uses state machine for robust packet parsing and framing.
*/
#include "modules/uart_receiver.h"
#include "modules/debug_monitor.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
namespace UARTReceiver {
// UART Configuration
// Note: UART_RX_PIN, UART_TX_PIN, and UART_BAUD_RATE are defined in platformio.ini
#define UART_SERIAL_PORT Serial1 // Hardware UART1
#define UART_RX_BUFFER_SIZE 256 // Receive buffer size
// Packet framing state machine states
typedef enum {
STATE_WAIT_START, // Waiting for start byte (0xAA)
STATE_READ_VERSION, // Reading protocol version
STATE_READ_LENGTH, // Reading data length
STATE_READ_DATA, // Reading data payload
STATE_READ_CHECKSUM, // Reading checksum byte
STATE_READ_END // Reading end byte (0x55)
} rx_state_t;
// Private state variables
static automotive_data_t g_latest_data = {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;
/**
* @brief UART reception task - runs on Core 0
*
* Implements state machine for packet parsing:
* 1. Wait for start byte (0xAA)
* 2. Read version and length
* 3. Read data payload
* 4. Read checksum
* 5. Read end byte (0x55)
* 6. Validate packet
* 7. Update shared data structure
*/
static void uartRxTask(void* parameter) {
uint8_t rx_buffer[sizeof(uart_packet_t)];
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");
// Initialize UART1 hardware
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, TX=GPIO%d\n",
UART_BAUD_RATE, UART_RX_PIN, UART_TX_PIN);
Serial.printf("[UARTReceiver] Waiting for packets from Controller A...\n");
while (true) {
// Check if data available
if (UART_SERIAL_PORT.available()) {
uint8_t byte = UART_SERIAL_PORT.read();
if (xSemaphoreTake(g_data_mutex, 0) == pdTRUE) {
g_rx_stats.bytes_received++;
xSemaphoreGive(g_data_mutex);
}
switch (state) {
case STATE_WAIT_START:
if (byte == PACKET_START_BYTE) {
rx_buffer[0] = byte;
bytes_read = 1;
state = STATE_READ_VERSION;
} else {
// Ignore byte, still waiting for start
if (xSemaphoreTake(g_data_mutex, 0) == pdTRUE) {
g_rx_stats.framing_errors++;
xSemaphoreGive(g_data_mutex);
}
}
break;
case STATE_READ_VERSION:
rx_buffer[bytes_read++] = byte;
state = STATE_READ_LENGTH;
break;
case STATE_READ_LENGTH:
rx_buffer[bytes_read++] = byte;
expected_length = byte;
// Validate length is reasonable
if (expected_length > sizeof(automotive_data_t)) {
// Invalid length, reset to wait for start
Serial.printf("[UARTReceiver] ERROR: Invalid length %d (max: %d)\n",
expected_length, sizeof(automotive_data_t));
state = STATE_WAIT_START;
bytes_read = 0;
if (xSemaphoreTake(g_data_mutex, 0) == pdTRUE) {
g_rx_stats.framing_errors++;
xSemaphoreGive(g_data_mutex);
}
} else {
state = STATE_READ_DATA;
}
break;
case STATE_READ_DATA:
rx_buffer[bytes_read++] = byte;
// Check if we've read all data bytes
// Packet structure: start(1) + version(1) + length(1) + data(expected_length)
if (bytes_read >= (3 + 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;
if (byte == PACKET_END_BYTE) {
// Complete packet received - validate it
uart_packet_t* packet = (uart_packet_t*)rx_buffer;
if (validate_packet(packet)) {
// Valid packet - update latest data
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
memcpy(&g_latest_data, &packet->data, sizeof(automotive_data_t));
g_rx_stats.packets_received++;
g_rx_stats.last_packet_time = millis();
xSemaphoreGive(g_data_mutex);
}
// Update debug monitor visibility based on received flag
DebugMonitor::setVisible(packet->data.debug_enabled != 0);
// Debug output every 20 packets
if (g_rx_stats.packets_received % 20 == 0) {
char gearbox_buf[16], engine_buf[16], coolant_buf[16], voltage_buf[16];
Serial.printf("[UARTReceiver] RX #%d | Gear:%s | GB:%s | Eng:%s | Cool:%s | Batt:%s | Valid:0x%02X | Debug:%s\n",
packet->data.packet_id,
gear_to_string((gear_position_t)packet->data.gear_position),
format_temperature(packet->data.gearbox_oil_temp, gearbox_buf),
format_temperature(packet->data.engine_oil_temp, engine_buf),
format_temperature(packet->data.coolant_temp, coolant_buf),
format_voltage(packet->data.battery_voltage, voltage_buf),
packet->data.valid_flags,
packet->data.debug_enabled ? "ON" : "OFF");
}
} else {
// Invalid checksum
if (xSemaphoreTake(g_data_mutex, 0) == pdTRUE) {
g_rx_stats.checksum_errors++;
xSemaphoreGive(g_data_mutex);
}
Serial.printf("[UARTReceiver] Checksum error (packet #%d)\n",
((uart_packet_t*)rx_buffer)->data.packet_id);
}
} else {
// Invalid end byte
if (xSemaphoreTake(g_data_mutex, 0) == pdTRUE) {
g_rx_stats.framing_errors++;
xSemaphoreGive(g_data_mutex);
}
Serial.printf("[UARTReceiver] Framing error: End byte 0x%02X (expected 0x55)\n", byte);
}
// Reset to wait for next packet
state = STATE_WAIT_START;
bytes_read = 0;
break;
}
}
// Small delay to prevent CPU hogging
vTaskDelay(pdMS_TO_TICKS(1));
}
}
bool init() {
if (g_initialized) {
Serial.println("[UARTReceiver] Already initialized");
return true;
}
Serial.println("[UARTReceiver] Initializing UART receiver...");
// Create mutex for thread-safe data access
g_data_mutex = xSemaphoreCreateMutex();
if (g_data_mutex == NULL) {
Serial.println("[UARTReceiver] ERROR: Failed to create mutex");
return false;
}
// Create UART reception task on Core 0
BaseType_t result = xTaskCreatePinnedToCore(
uartRxTask,
"UART_RX_Task",
4096, // Stack size
NULL, // Parameters
1, // Priority (lower than LVGL task)
&g_uart_task_handle,
0 // Core 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 != NULL) {
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
memcpy(&data, &g_latest_data, sizeof(automotive_data_t));
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 != NULL) {
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
memcpy(&stats, &g_rx_stats, sizeof(uart_rx_stats_t));
xSemaphoreGive(g_data_mutex);
}
}
return stats;
}
bool isConnected(uint32_t timeout_ms) {
if (!g_initialized) {
return false;
}
bool connected = false;
if (g_data_mutex != NULL) {
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
if (g_rx_stats.packets_received > 0) {
uint32_t time_since_last = millis() - g_rx_stats.last_packet_time;
connected = (time_since_last < timeout_ms);
}
xSemaphoreGive(g_data_mutex);
}
}
return connected;
}
void resetStats() {
if (g_data_mutex != NULL) {
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
g_rx_stats.packets_received = 0;
g_rx_stats.checksum_errors = 0;
g_rx_stats.framing_errors = 0;
g_rx_stats.timeout_errors = 0;
g_rx_stats.bytes_received = 0;
// Keep last_packet_time
xSemaphoreGive(g_data_mutex);
}
}
Serial.println("[UARTReceiver] Statistics reset");
}
uint32_t getTimeSinceLastPacket() {
if (!g_initialized) {
return UINT32_MAX;
}
uint32_t time_since = UINT32_MAX;
if (g_data_mutex != NULL) {
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
if (g_rx_stats.packets_received > 0) {
time_since = millis() - g_rx_stats.last_packet_time;
}
xSemaphoreGive(g_data_mutex);
}
}
return time_since;
}
} // namespace UARTReceiver