85 lines
2.1 KiB
C++
85 lines
2.1 KiB
C++
/**
|
|
* UART Receiver Module for ESP32-S3-LCD-1.3 (Controller B)
|
|
*
|
|
* Receives automotive data packets from Controller A (ESP32-S3-Touch-LCD-5B)
|
|
* via UART at 115200 baud, 5Hz update rate.
|
|
*
|
|
* Features:
|
|
* - State machine-based packet parsing
|
|
* - XOR checksum validation
|
|
* - Thread-safe data access
|
|
* - Connection monitoring and timeout detection
|
|
* - Error statistics tracking
|
|
*/
|
|
|
|
#ifndef UART_RECEIVER_H
|
|
#define UART_RECEIVER_H
|
|
|
|
#include <Arduino.h>
|
|
#include "protocol/automotive_data.h"
|
|
|
|
namespace UARTReceiver {
|
|
|
|
/**
|
|
* @brief UART RX statistics structure
|
|
*/
|
|
typedef struct {
|
|
uint32_t packets_received; // Total valid packets received
|
|
uint32_t checksum_errors; // Packets with invalid checksum
|
|
uint32_t framing_errors; // Packets with framing errors
|
|
uint32_t timeout_errors; // Connection timeout events
|
|
uint32_t last_packet_time; // millis() when last valid packet received
|
|
uint32_t bytes_received; // Total bytes received
|
|
} uart_rx_stats_t;
|
|
|
|
/**
|
|
* @brief Initialize UART receiver module
|
|
*
|
|
* Initializes Serial1 hardware and creates FreeRTOS reception task on Core 0.
|
|
*
|
|
* @return true if initialization successful, false otherwise
|
|
*/
|
|
bool init();
|
|
|
|
/**
|
|
* @brief Get the latest received automotive data
|
|
*
|
|
* Thread-safe access to the most recently received valid packet.
|
|
*
|
|
* @return Copy of the latest automotive_data_t structure
|
|
*/
|
|
automotive_data_t getLatestData();
|
|
|
|
/**
|
|
* @brief Get UART reception statistics
|
|
*
|
|
* @return Copy of current RX statistics
|
|
*/
|
|
uart_rx_stats_t getStats();
|
|
|
|
/**
|
|
* @brief Check if connection is active
|
|
*
|
|
* Returns true if a valid packet was received within the last timeout period.
|
|
*
|
|
* @param timeout_ms Timeout in milliseconds (default: 1000ms)
|
|
* @return true if connection is active, false if timed out
|
|
*/
|
|
bool isConnected(uint32_t timeout_ms = 1000);
|
|
|
|
/**
|
|
* @brief Reset statistics counters
|
|
*/
|
|
void resetStats();
|
|
|
|
/**
|
|
* @brief Get time since last valid packet in milliseconds
|
|
*
|
|
* @return Milliseconds since last packet, or UINT32_MAX if no packets received
|
|
*/
|
|
uint32_t getTimeSinceLastPacket();
|
|
|
|
} // namespace UARTReceiver
|
|
|
|
#endif // UART_RECEIVER_H
|