Files
ESP32-S3-Touch-LCD-5B/src/modules/uart_manager.h
T

134 lines
3.5 KiB
C++

/*
* UART Manager Module
*
* Handles UART communication for the Waveshare ESP32-S3-Touch-LCD-5B:
* - Periodic automotive data transmission @ 5Hz
* - FreeRTOS task on Core 0 for async UART operations
* - Automotive data packet framing and checksums
* - Default data initialization
* - CAN bus integration - UART transmits only when CAN is active
*/
#pragma once
#include <Arduino.h>
#include <lvgl.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include "protocol/automotive_data.h"
namespace UARTManager {
// ============================================================================
// CONFIGURATION
// ============================================================================
#define UART_TASK_STACK_SIZE 4096
#define UART_TASK_PRIORITY 1
#define UART_TASK_CORE 0 // Run UART task on Core 0 (same as WiFi)
// ============================================================================
// PUBLIC TYPES
// ============================================================================
/**
* @brief UART state machine states
*/
typedef enum {
UART_STATE_OFF = 0,
UART_STATE_TRANSMITTING,
UART_STATE_ERROR
} uart_state_t;
/**
* @brief UART event types for FreeRTOS queue
*/
typedef enum {
UART_EVENT_START,
UART_EVENT_STOP,
UART_EVENT_SEND_DATA
} uart_event_type_t;
/**
* @brief UART queue event structure
*/
typedef struct {
uart_event_type_t type;
void* data;
} uart_queue_event_t;
/**
* @brief UART statistics structure
*/
typedef struct {
uint32_t packets_sent;
uint32_t send_errors;
uint32_t last_send_time;
} uart_stats_t;
// ============================================================================
// SHARED DATA (Accessible by CAN driver)
// ============================================================================
// External declarations for shared automotive data
extern SemaphoreHandle_t g_data_mutex;
extern automotive_data_t g_automotive_data;
// ============================================================================
// PUBLIC API
// ============================================================================
/**
* @brief Initialize the UART manager subsystem
*
* This function:
* - Creates FreeRTOS mutex and queue
* - Spawns UART task on Core 0
* - Optionally auto-starts UART based on saved state
*
* @param auto_start If true, automatically start UART based on NVS saved state
*/
void init(bool auto_start);
/**
* @brief Get current UART state
* @return Current uart_state_t value
*/
uart_state_t getState();
/**
* @brief Get UART statistics
* @return Current uart_stats_t structure
*/
uart_stats_t getStats();
/**
* @brief Set debug mode flag in transmitted packets
* @param enabled True to enable debug mode on receiver, false to disable
*/
void setDebugEnabled(bool enabled);
/**
* @brief Link UART transmission state to CAN driver state
* @param can_active True when CAN starts, false when CAN stops
* @note Called by CAN driver to automatically start/stop UART
*/
void linkToCANState(bool can_active);
/**
* @brief Settings-screen switch: start/stop sending to Controller B (UART1 +
* ESP-NOW) and remember the choice in NVS.
*/
void setEnabled(bool enabled);
/**
* @brief Send one complete frame on the UART wire ONLY (never ESP-NOW).
* Used for the PAIR frame, which must not go over the air. One write
* call, so it cannot interleave with the task's own frames.
* @return true if the port is up and every byte was written
*/
bool sendWire(const uint8_t* frame, size_t len);
} // namespace UARTManager