HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
EspUart.h
Go to the documentation of this file.
1
21#pragma once
22
23#include "BaseUart.h"
24#include "McuSelect.h"
25#include "PlatformMutex.h"
26#include "utils/EspTypes.h"
27
28#include <array>
29#include <atomic>
30#include <memory>
31#include <vector>
32
33// ESP-IDF C headers must be wrapped in extern "C" for C++ compatibility
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38#include "driver/gpio.h"
39#include "driver/uart.h"
40#include "esp_log.h"
41#include "esp_timer.h"
42
43#include "freertos/FreeRTOS.h"
44#include "freertos/queue.h"
45#include "freertos/task.h"
46
47#ifdef __cplusplus
48}
49#endif
50
104class EspUart : public BaseUart {
105public:
106 //==============================================================================
107 // CONSTANTS
108 //==============================================================================
109
110 static constexpr hf_u8_t MAX_PORTS = 3;
111 static constexpr hf_u32_t MAX_BAUD_RATE = 5000000;
112 static constexpr hf_u32_t MIN_BAUD_RATE = 110;
113 static constexpr hf_u32_t DEFAULT_BAUD_RATE = 115200;
114 static constexpr hf_u16_t MAX_BUFFER_SIZE = 1024;
115 static constexpr hf_u16_t DEFAULT_BUFFER_SIZE = 256;
116
117 //==============================================================================
118 // CONSTRUCTOR AND DESTRUCTOR
119 //==============================================================================
120
126 explicit EspUart(const hf_uart_config_t& config) noexcept;
127
131 virtual ~EspUart() noexcept override;
132
133 // Prevent copying and moving
134 EspUart(const EspUart&) = delete;
135 EspUart& operator=(const EspUart&) = delete;
136 EspUart(EspUart&&) = delete;
137 EspUart& operator=(EspUart&&) = delete;
138
139 //==============================================================================
140 // LIFECYCLE (BaseUart Interface)
141 //==============================================================================
142
143 // Note: EnsureInitialized() is inherited from BaseUart and provides lazy initialization
144 // Thread safety is handled in Initialize() and Deinitialize() methods
145
146 //==============================================================================
147 // BASIC UART OPERATIONS (BaseUart Interface)
148 //==============================================================================
149
157 hf_uart_err_t Write(const uint8_t* data, uint16_t length,
158 uint32_t timeout_ms = 0) noexcept override;
159
167 hf_uart_err_t Read(uint8_t* data, uint16_t length, uint32_t timeout_ms = 0) noexcept override;
168
174 bool WriteByte(uint8_t byte) noexcept override;
175
181 bool SetBaudRate(uint32_t baud_rate) noexcept;
182
188 hf_uart_err_t SetFlowControl(bool enable) noexcept;
189
195 hf_uart_err_t SetRTS(bool active) noexcept;
196
202 hf_uart_err_t SendBreak(uint32_t duration_ms) noexcept;
203
209 hf_uart_err_t SetLoopback(bool enable) noexcept;
210
216 bool WaitTransmitComplete(uint32_t timeout_ms) noexcept;
217
218 //==============================================================================
219 // ADVANCED UART FEATURES
220 //==============================================================================
221
228
237 uint16_t ReadUntil(uint8_t* data, uint16_t max_length, uint8_t terminator,
238 uint32_t timeout_ms) noexcept;
239
247 uint16_t ReadLine(char* buffer, uint16_t max_length, uint32_t timeout_ms) noexcept;
248
255
261 hf_uart_err_t ConfigureRS485(const hf_uart_rs485_config_t& rs485_config) noexcept;
262
268 hf_uart_err_t ConfigureIrDA(const hf_uart_irda_config_t& irda_config) noexcept;
269
277 hf_uart_err_t ConfigureSoftwareFlowControl(bool enable, uint8_t xon_threshold = 20,
278 uint8_t xoff_threshold = 80) noexcept;
279
285 hf_uart_err_t ConfigureWakeup(const hf_uart_wakeup_config_t& wakeup_config) noexcept;
286
292 hf_uart_err_t SetSignalInversion(uint32_t inverse_mask) noexcept;
293
294 //==============================================================================
295 // EVENT QUEUE ACCESS (User Creates Own Tasks)
296 //==============================================================================
297
304 QueueHandle_t GetEventQueue() const noexcept;
305
310 bool IsEventQueueAvailable() const noexcept;
311
320 hf_uart_err_t ConfigureInterrupts(uint32_t intr_enable_mask, uint8_t rxfifo_full_thresh = 100,
321 uint8_t rx_timeout_thresh = 10) noexcept;
322
328
329 //==============================================================================
330 // PATTERN DETECTION (ESP-IDF v5.5 Feature)
331 //==============================================================================
332
343 hf_uart_err_t EnablePatternDetection(char pattern_chr, uint8_t chr_num = 1, int chr_tout = 9,
344 int post_idle = 0, int pre_idle = 0) noexcept;
345
351
357 hf_uart_err_t ResetPatternQueue(int queue_length = 32) noexcept;
358
364 int PopPatternPosition() noexcept;
365
370 int PeekPatternPosition() noexcept;
371
372 //==============================================================================
373 // STATUS AND INFORMATION
374 //==============================================================================
375
381 hf_uart_err_t GetStatistics(hf_uart_statistics_t& statistics) const noexcept override;
382
388 hf_uart_err_t GetDiagnostics(hf_uart_diagnostics_t& diagnostics) const noexcept override;
389
394 hf_uart_err_t GetLastError() const noexcept;
395
400 const hf_uart_config_t& GetPortConfig() const noexcept;
401
407
412 hf_uart_mode_t GetCommunicationMode() const noexcept;
413
418 bool IsWakeupEnabled() const noexcept;
419
424 bool IsTransmitting() const noexcept;
425
430 bool IsReceiving() const noexcept;
431
436 bool IsBreakDetected() noexcept;
437
442 uint16_t TxBytesWaiting() noexcept;
443
444 //==============================================================================
445 // PRINTF SUPPORT
446 //==============================================================================
447
454 int Printf(const char* format, ...) noexcept;
455
462 int VPrintf(const char* format, va_list args) noexcept;
463
464private:
465 //==============================================================================
466 // INITIALIZATION AND DEINITIALIZATION
467 //==============================================================================
468
474 bool Initialize() noexcept override;
475
480 bool Deinitialize() noexcept override;
481
482 //==============================================================================
483 // INTERNAL STATE STRUCTURES
484 //==============================================================================
485
503
504 //==============================================================================
505 // INTERNAL METHODS
506 //==============================================================================
507
512 hf_uart_err_t ValidateConfiguration() const noexcept;
513
519
525
530 hf_uart_err_t InstallDriver() noexcept;
531
537
542 hf_uart_err_t ConfigureUart() noexcept;
543
548 hf_uart_err_t ConfigurePins() noexcept;
549
555 hf_uart_err_t ConvertPlatformError(int32_t platform_error) noexcept;
556
563 hf_uart_err_t UpdateStatistics(hf_uart_err_t result, uint64_t start_time_us) noexcept;
564
569 void UpdateDiagnostics(hf_uart_err_t error) noexcept;
570
576 [[nodiscard]] uint32_t GetTimeoutMs(uint32_t timeout_ms) const noexcept;
577
584 int InternalPrintf(const char* format, va_list args) noexcept;
585
586 //==============================================================================
587 // MEMBER VARIABLES
588 //==============================================================================
589
592 std::atomic<bool> initialized_;
593 uart_port_t uart_port_;
594
595 // Event queue (user creates own tasks)
596 QueueHandle_t event_queue_;
597
598 // Operating mode and communication state
605
606 // Error tracking
608
609 // Statistics and diagnostics
612
613 // Printf buffer
614 char printf_buffer_[256];
615
616public:
617 // Add missing overrides for BaseUart pure virtuals
618 uint16_t BytesAvailable() noexcept override;
619 hf_uart_err_t FlushTx() noexcept override;
620 hf_uart_err_t FlushRx() noexcept override;
621 bool IsTxBusy() noexcept;
622};
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.
Cross-platform RTOS mutex and synchronization primitives.
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
Definition PlatformMutex.h:78
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