38#include "driver/gpio.h"
39#include "driver/uart.h"
43#include "freertos/FreeRTOS.h"
44#include "freertos/queue.h"
45#include "freertos/task.h"
131 virtual ~EspUart() noexcept override;
158 uint32_t timeout_ms = 0) noexcept override;
167 hf_uart_err_t Read(uint8_t* data, uint16_t length, uint32_t timeout_ms = 0) noexcept override;
174 bool WriteByte(uint8_t
byte) noexcept override;
237 uint16_t
ReadUntil(uint8_t* data, uint16_t max_length, uint8_t terminator,
238 uint32_t timeout_ms) noexcept;
247 uint16_t
ReadLine(
char* buffer, uint16_t max_length, uint32_t timeout_ms) noexcept;
278 uint8_t xoff_threshold = 80) noexcept;
321 uint8_t rx_timeout_thresh = 10) noexcept;
344 int post_idle = 0,
int pre_idle = 0) noexcept;
454 int Printf(const
char* format, ...) noexcept;
462 int VPrintf(const
char* format, va_list args) noexcept;
498 : configured(false), enabled(false), config(),
576 [[nodiscard]] uint32_t
GetTimeoutMs(uint32_t timeout_ms) const noexcept;
Abstract base class for UART driver implementations in the HardFOC system.
hf_uart_err_t
Definition BaseUart.h:84
Consolidated MCU-specific type definitions for hardware abstraction (hf_* types).
hf_uart_operating_mode_t
ESP32 UART operating mode.
Definition EspTypes_UART.h:101
@ HF_UART_MODE_POLLING
Polling mode.
hf_uart_mode_t
ESP32 UART operating modes.
Definition EspTypes_UART.h:54
@ HF_UART_MODE_UART
Standard UART mode.
uint32_t hf_u32_t
Platform-agnostic 32-bit unsigned integer type.
Definition HardwareTypes.h:52
uint8_t hf_u8_t
Platform-agnostic 8-bit unsigned integer type.
Definition HardwareTypes.h:40
uint16_t hf_u16_t
Platform-agnostic 16-bit unsigned integer type.
Definition HardwareTypes.h:46
Centralized MCU platform selection and configuration header.
Abstract base class for UART driver implementations.
Definition BaseUart.h:191
ESP32 UART implementation class.
Definition EspUart.h:104
hf_uart_config_t port_config_
Port configuration.
Definition EspUart.h:591
hf_uart_err_t ValidateConfiguration() const noexcept
Validate configuration.
Definition EspUart.cpp:1030
hf_uart_err_t last_error_
Last error that occurred.
Definition EspUart.h:607
hf_uart_err_t ResetEventQueue() noexcept
Reset event queue (clear all pending events).
Definition EspUart.cpp:834
hf_uart_err_t GetStatistics(hf_uart_statistics_t &statistics) const noexcept override
Get UART statistics.
Definition EspUart.cpp:949
hf_uart_err_t FlushRx() noexcept override
Flush the receive buffer.
Definition EspUart.cpp:258
hf_uart_err_t SendBreak(uint32_t duration_ms) noexcept
Send a break condition.
Definition EspUart.cpp:485
hf_uart_err_t ConfigureIrDA(const hf_uart_irda_config_t &irda_config) noexcept
Configure IrDA mode.
Definition EspUart.cpp:684
hf_uart_err_t ResetPatternQueue(int queue_length=32) noexcept
Reset pattern detection queue.
Definition EspUart.cpp:893
hf_uart_err_t SetCommunicationMode(hf_uart_mode_t mode) noexcept
Set UART communication mode (UART/RS485/IrDA).
Definition EspUart.cpp:622
hf_uart_err_t GetDiagnostics(hf_uart_diagnostics_t &diagnostics) const noexcept override
Get UART diagnostics.
Definition EspUart.cpp:955
hf_uart_err_t UninstallDriver() noexcept
Uninstall UART driver.
Definition EspUart.cpp:1175
int InternalPrintf(const char *format, va_list args) noexcept
Internal printf implementation.
Definition EspUart.cpp:1258
hf_uart_diagnostics_t diagnostics_
UART diagnostics.
Definition EspUart.h:611
char printf_buffer_[256]
Printf buffer.
Definition EspUart.h:614
hf_uart_statistics_t statistics_
UART statistics.
Definition EspUart.h:610
static constexpr hf_u16_t DEFAULT_BUFFER_SIZE
Default buffer size.
Definition EspUart.h:115
PlatformMutex mutex_
Thread safety mutex.
Definition EspUart.h:590
hf_uart_mode_t communication_mode_
Current communication mode.
Definition EspUart.h:600
hf_uart_err_t ConfigureRS485(const hf_uart_rs485_config_t &rs485_config) noexcept
Configure RS485 mode.
Definition EspUart.cpp:663
hf_uart_err_t SetRTS(bool active) noexcept
Set RTS line state.
Definition EspUart.cpp:467
static constexpr hf_u16_t MAX_BUFFER_SIZE
Maximum buffer size.
Definition EspUart.h:114
uint16_t BytesAvailable() noexcept override
Get the number of bytes available to read.
Definition EspUart.cpp:298
hf_uart_err_t UpdateStatistics(hf_uart_err_t result, uint64_t start_time_us) noexcept
Update statistics.
Definition EspUart.cpp:1227
const hf_uart_config_t & GetPortConfig() const noexcept
Get current UART configuration.
Definition EspUart.cpp:966
hf_uart_err_t DisablePatternDetection() noexcept
Disable pattern detection.
Definition EspUart.cpp:874
hf_uart_err_t InstallDriver() noexcept
Install UART driver.
Definition EspUart.cpp:1088
bool IsTransmitting() const noexcept
Check if transmission is in progress.
Definition EspUart.cpp:986
hf_uart_err_t ConfigurePins() noexcept
Configure UART pins.
Definition EspUart.cpp:1193
int VPrintf(const char *format, va_list args) noexcept
Print formatted string to UART with va_list.
Definition EspUart.cpp:1017
bool wakeup_enabled_
Wakeup enabled.
Definition EspUart.h:602
bool IsBreakDetected() noexcept
Check if break condition was detected.
Definition EspUart.cpp:996
hf_uart_err_t PlatformInitialize() noexcept
Platform-specific initialization.
Definition EspUart.cpp:1055
hf_uart_err_t ConvertPlatformError(int32_t platform_error) noexcept
Convert platform error to HardFOC error.
Definition EspUart.cpp:1206
static constexpr hf_u32_t DEFAULT_BAUD_RATE
Default baud rate.
Definition EspUart.h:113
QueueHandle_t GetEventQueue() const noexcept
Get the ESP-IDF event queue handle for user task creation.
Definition EspUart.cpp:795
QueueHandle_t event_queue_
ESP-IDF UART event queue handle.
Definition EspUart.h:596
hf_uart_err_t ConfigureInterrupts(uint32_t intr_enable_mask, uint8_t rxfifo_full_thresh=100, uint8_t rx_timeout_thresh=10) noexcept
Configure UART interrupt settings.
Definition EspUart.cpp:805
int PeekPatternPosition() noexcept
Peek pattern position without consuming.
Definition EspUart.cpp:930
bool WriteByte(uint8_t byte) noexcept override
Write a single byte to the UART.
Definition EspUart.cpp:229
uint16_t ReadLine(char *buffer, uint16_t max_length, uint32_t timeout_ms) noexcept
Read a line of text (until newline).
Definition EspUart.cpp:578
hf_uart_err_t SetLoopback(bool enable) noexcept
Enable or disable loopback mode.
Definition EspUart.cpp:557
uart_port_t uart_port_
Native UART port handle.
Definition EspUart.h:593
static constexpr hf_u32_t MAX_BAUD_RATE
Maximum baud rate.
Definition EspUart.h:111
void UpdateDiagnostics(hf_uart_err_t error) noexcept
Update diagnostics.
Definition EspUart.cpp:1239
static constexpr hf_u32_t MIN_BAUD_RATE
Minimum baud rate.
Definition EspUart.h:112
bool IsTxBusy() noexcept
Definition EspUart.cpp:315
bool IsEventQueueAvailable() const noexcept
Check if event queue is available.
Definition EspUart.cpp:800
hf_uart_err_t Read(uint8_t *data, uint16_t length, uint32_t timeout_ms=0) noexcept override
Read data from the UART.
Definition EspUart.cpp:192
bool Deinitialize() noexcept override
Deinitialize the UART driver.
Definition EspUart.cpp:112
bool software_flow_enabled_
Software flow control enabled.
Definition EspUart.h:601
uint16_t ReadUntil(uint8_t *data, uint16_t max_length, uint8_t terminator, uint32_t timeout_ms) noexcept
Read data until a specific terminator is found.
Definition EspUart.cpp:350
hf_uart_err_t ConfigureSoftwareFlowControl(bool enable, uint8_t xon_threshold=20, uint8_t xoff_threshold=80) noexcept
Configure software flow control (XON/XOFF).
Definition EspUart.cpp:702
bool IsWakeupEnabled() const noexcept
Check if wakeup is enabled.
Definition EspUart.cpp:981
uint16_t TxBytesWaiting() noexcept
Get number of bytes waiting in TX buffer.
Definition EspUart.cpp:340
bool break_detected_
Break condition detected.
Definition EspUart.h:603
hf_uart_err_t SetFlowControl(bool enable) noexcept
Enable or disable hardware flow control.
Definition EspUart.cpp:441
virtual ~EspUart() noexcept override
Destructor - ensures clean shutdown.
Definition EspUart.cpp:61
bool Initialize() noexcept override
Initialize the UART driver.
Definition EspUart.cpp:73
EspUart(const hf_uart_config_t &config) noexcept
Constructor for ESP32 UART controller.
Definition EspUart.cpp:43
hf_uart_err_t Write(const uint8_t *data, uint16_t length, uint32_t timeout_ms=0) noexcept override
Write data to the UART.
Definition EspUart.cpp:139
hf_uart_err_t SetOperatingMode(hf_uart_operating_mode_t mode) noexcept
Set UART operating mode.
Definition EspUart.cpp:402
hf_uart_err_t PlatformDeinitialize() noexcept
Platform-specific deinitialization.
Definition EspUart.cpp:1082
bool tx_in_progress_
Transmission in progress.
Definition EspUart.h:604
bool WaitTransmitComplete(uint32_t timeout_ms) noexcept
Wait for transmission to complete.
Definition EspUart.cpp:273
hf_uart_operating_mode_t GetOperatingMode() const noexcept
Get current operating mode.
Definition EspUart.cpp:971
hf_uart_err_t GetLastError() const noexcept
Get the last error that occurred.
Definition EspUart.cpp:961
hf_uart_mode_t GetCommunicationMode() const noexcept
Get current communication mode.
Definition EspUart.cpp:976
bool IsReceiving() const noexcept
Check if reception is active.
Definition EspUart.cpp:991
static constexpr hf_u8_t MAX_PORTS
Maximum UART ports.
Definition EspUart.h:110
bool SetBaudRate(uint32_t baud_rate) noexcept
Set the baud rate.
Definition EspUart.cpp:416
int Printf(const char *format,...) noexcept
Print formatted string to UART.
Definition EspUart.cpp:1005
hf_uart_err_t SetSignalInversion(uint32_t inverse_mask) noexcept
Set signal inversion mask.
Definition EspUart.cpp:773
uint32_t GetTimeoutMs(uint32_t timeout_ms) const noexcept
Get timeout value in milliseconds.
Definition EspUart.cpp:1251
hf_uart_err_t FlushTx() noexcept override
Flush the transmit buffer.
Definition EspUart.cpp:247
int PopPatternPosition() noexcept
Pop pattern position from queue (consumes entry).
Definition EspUart.cpp:912
hf_uart_err_t ConfigureUart() noexcept
Configure UART parameters.
Definition EspUart.cpp:1187
std::atomic< bool > initialized_
Initialization state (atomic for lazy init)
Definition EspUart.h:592
hf_uart_operating_mode_t operating_mode_
Current operating mode.
Definition EspUart.h:599
hf_uart_err_t ConfigureWakeup(const hf_uart_wakeup_config_t &wakeup_config) noexcept
Configure UART wakeup from light sleep.
Definition EspUart.cpp:736
hf_uart_err_t EnablePatternDetection(char pattern_chr, uint8_t chr_num=1, int chr_tout=9, int post_idle=0, int pre_idle=0) noexcept
Enable pattern detection for repeated byte sequences.
Definition EspUart.cpp:851
UART state tracking structure.
Definition EspUart.h:489
hf_uart_operating_mode_t operating_mode
Current operating mode.
Definition EspUart.h:493
hf_uart_config_t config
Current configuration.
Definition EspUart.h:492
hf_uart_mode_t communication_mode
Current communication mode.
Definition EspUart.h:494
UartState() noexcept
Definition EspUart.h:497
bool configured
UART is configured.
Definition EspUart.h:490
hf_uart_err_t last_error
Last error for this UART.
Definition EspUart.h:495
bool enabled
UART is enabled.
Definition EspUart.h:491
ESP32 UART port configuration.
Definition EspTypes_UART.h:114
UART diagnostic information.
Definition BaseUart.h:140
ESP32 UART IrDA configuration.
Definition EspTypes_UART.h:217
ESP32 UART RS485 configuration.
Definition EspTypes_UART.h:199
UART operation statistics.
Definition BaseUart.h:114
ESP32 UART wakeup configuration.
Definition EspTypes_UART.h:230