HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
BaseUart.h
Go to the documentation of this file.
1
19#pragma once
20
21#include "HardwareTypes.h"
22#include <cstdint>
23#include <cstring>
24#include <string_view>
25
31//--------------------------------------
32// HardFOC UART Error Codes (Table)
33//--------------------------------------
42#define HF_UART_ERR_LIST(X) \
43 /* Success codes */ \
44 X(UART_SUCCESS, 0, "Success") \
45 /* General errors */ \
46 X(UART_ERR_FAILURE, 1, "General failure") \
47 X(UART_ERR_NOT_INITIALIZED, 2, "Not initialized") \
48 X(UART_ERR_ALREADY_INITIALIZED, 3, "Already initialized") \
49 X(UART_ERR_INVALID_PARAMETER, 4, "Invalid parameter") \
50 X(UART_ERR_NULL_POINTER, 5, "Null pointer") \
51 X(UART_ERR_OUT_OF_MEMORY, 6, "Out of memory") \
52 /* Communication errors */ \
53 X(UART_ERR_TIMEOUT, 7, "Operation timeout") \
54 X(UART_ERR_BUFFER_FULL, 8, "Buffer full") \
55 X(UART_ERR_BUFFER_EMPTY, 9, "Buffer empty") \
56 X(UART_ERR_TRANSMISSION_FAILED, 10, "Transmission failed") \
57 X(UART_ERR_RECEPTION_FAILED, 11, "Reception failed") \
58 /* Frame errors */ \
59 X(UART_ERR_FRAME_ERROR, 12, "Frame error") \
60 X(UART_ERR_PARITY_ERROR, 13, "Parity error") \
61 X(UART_ERR_OVERRUN_ERROR, 14, "Overrun error") \
62 X(UART_ERR_NOISE_ERROR, 15, "Noise error") \
63 X(UART_ERR_BREAK_DETECTED, 16, "Break condition detected") \
64 /* Hardware errors */ \
65 X(UART_ERR_HARDWARE_FAULT, 17, "Hardware fault") \
66 X(UART_ERR_COMMUNICATION_FAILURE, 18, "Communication failure") \
67 X(UART_ERR_DEVICE_NOT_RESPONDING, 19, "Device not responding") \
68 X(UART_ERR_VOLTAGE_OUT_OF_RANGE, 20, "Voltage out of range") \
69 /* Configuration errors */ \
70 X(UART_ERR_INVALID_CONFIGURATION, 21, "Invalid configuration") \
71 X(UART_ERR_UNSUPPORTED_OPERATION, 22, "Unsupported operation") \
72 X(UART_ERR_INVALID_BAUD_RATE, 23, "Invalid baud rate") \
73 X(UART_ERR_INVALID_DATA_BITS, 24, "Invalid data bits") \
74 X(UART_ERR_INVALID_PARITY, 25, "Invalid parity") \
75 X(UART_ERR_INVALID_STOP_BITS, 26, "Invalid stop bits") \
76 X(UART_ERR_PIN_CONFIGURATION_ERROR, 27, "Pin configuration error") \
77 X(UART_ERR_FLOW_CONTROL_ERROR, 28, "Flow control error") \
78 /* System errors */ \
79 X(UART_ERR_SYSTEM_ERROR, 29, "System error") \
80 X(UART_ERR_PERMISSION_DENIED, 30, "Permission denied") \
81 X(UART_ERR_OPERATION_ABORTED, 31, "Operation aborted") \
82 X(UART_ERR_UNKNOWN, 32, "Unknown error")
83
84enum class hf_uart_err_t : hf_u8_t {
85#define X(NAME, VALUE, DESC) NAME = VALUE,
87#undef X
88};
89
95constexpr std::string_view HfUartErrToString(hf_uart_err_t err) noexcept {
96 switch (err) {
97#define X(NAME, VALUE, DESC) \
98 case hf_uart_err_t::NAME: \
99 return DESC;
101#undef X
102 default:
104 }
105}
106
107//--------------------------------------
108// UART Configuration Structure
109//--------------------------------------
110
136
161
162//--------------------------------------
163// Abstract Base Class
164//--------------------------------------
165
191class BaseUart {
192public:
196 virtual ~BaseUart() noexcept = default;
197
198 // Non-copyable, non-movable (can be changed in derived classes if needed)
199 BaseUart(const BaseUart&) = delete;
200 BaseUart& operator=(const BaseUart&) = delete;
201 BaseUart(BaseUart&&) = delete;
202 BaseUart& operator=(BaseUart&&) = delete;
203
208 bool EnsureInitialized() noexcept {
209 if (!initialized_) {
211 }
212 return initialized_;
213 }
214
219 bool EnsureDeinitialized() noexcept {
220 if (initialized_) {
222 }
223 return !initialized_;
224 }
225
230 [[nodiscard]] bool IsInitialized() const noexcept {
231 return initialized_;
232 }
233
238 [[nodiscard]] hf_port_num_t GetPort() const noexcept {
239 return port_;
240 }
241
242 //==============================================//
243 // PURE VIRTUAL FUNCTIONS - MUST BE OVERRIDDEN //
244 //==============================================//
245
250 virtual bool Initialize() noexcept = 0;
251
256 virtual bool Deinitialize() noexcept = 0;
257
265 virtual hf_uart_err_t Write(const hf_u8_t* data, hf_u16_t length,
266 hf_u32_t timeout_ms = 0) noexcept = 0;
267
275 virtual hf_uart_err_t Read(hf_u8_t* data, hf_u16_t length, hf_u32_t timeout_ms = 0) noexcept = 0;
276
281 virtual hf_u16_t BytesAvailable() noexcept = 0;
282
287 virtual hf_uart_err_t FlushTx() noexcept = 0;
288
293 virtual hf_uart_err_t FlushRx() noexcept = 0;
294
301 virtual int Printf(const char* format, ...) noexcept = 0;
302
303 //==============================================//
304 // CONVENIENCE METHODS WITH DEFAULT IMPLEMENTATIONS
305 //==============================================//
306
311 virtual bool Open() noexcept {
312 return Initialize();
313 }
314
319 virtual bool Close() noexcept {
320 return Deinitialize();
321 }
322
323 // Removed duplicate Write method to avoid overload conflicts
324
325 // Removed duplicate Read method to avoid overload conflicts
326
332 virtual bool WriteString(const char* str) noexcept {
333 if (!str)
334 return false;
335 return Write(reinterpret_cast<const hf_u8_t*>(str), static_cast<hf_u16_t>(strlen(str))) ==
337 }
338
344 virtual bool WriteByte(hf_u8_t byte) noexcept {
345 return Write(&byte, 1) == hf_uart_err_t::UART_SUCCESS;
346 }
347
348 // Removed duplicate FlushTx and FlushRx methods to avoid overload conflicts
349
350 //==============================================//
351 // STATISTICS AND DIAGNOSTICS
352 //==============================================//
353
359 virtual hf_uart_err_t ResetStatistics() noexcept {
360 statistics_ = hf_uart_statistics_t{}; // Reset statistics to default values
362 }
363
369 virtual hf_uart_err_t ResetDiagnostics() noexcept {
370 diagnostics_ = hf_uart_diagnostics_t{}; // Reset diagnostics to default values
372 }
373
379 virtual hf_uart_err_t GetStatistics(hf_uart_statistics_t& statistics) const noexcept {
380 statistics = statistics_; // Return statistics by default
382 }
383
389 virtual hf_uart_err_t GetDiagnostics(hf_uart_diagnostics_t& diagnostics) const noexcept {
390 diagnostics = diagnostics_; // Return diagnostics by default
392 }
393
394protected:
399 BaseUart(hf_port_num_t port) noexcept
400 : port_(port), initialized_(false), statistics_{}, diagnostics_{} {}
401
406};
hf_uart_err_t
Definition BaseUart.h:84
@ UART_ERR_UNSUPPORTED_OPERATION
#define X(NAME, VALUE, DESC)
Definition BaseUart.h:85
constexpr std::string_view HfUartErrToString(hf_uart_err_t err) noexcept
Convert hf_uart_err_t to human-readable string.
Definition BaseUart.h:95
Platform-agnostic hardware type definitions for the HardFOC system.
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
hf_u32_t hf_port_num_t
Platform-agnostic port/controller identifier type.
Definition HardwareTypes.h:120
uint64_t hf_u64_t
Platform-agnostic 64-bit unsigned integer type.
Definition HardwareTypes.h:58
uint16_t hf_u16_t
Platform-agnostic 16-bit unsigned integer type.
Definition HardwareTypes.h:46
Abstract base class for UART driver implementations.
Definition BaseUart.h:191
bool EnsureDeinitialized() noexcept
Ensures that the UART is deinitialized (lazy deinitialization).
Definition BaseUart.h:219
virtual hf_uart_err_t FlushRx() noexcept=0
Flush the receive buffer.
hf_uart_statistics_t statistics_
UART operation statistics.
Definition BaseUart.h:404
bool EnsureInitialized() noexcept
Ensures that the UART is initialized (lazy initialization).
Definition BaseUart.h:208
virtual hf_uart_err_t Write(const hf_u8_t *data, hf_u16_t length, hf_u32_t timeout_ms=0) noexcept=0
Write data to the UART.
virtual hf_uart_err_t GetStatistics(hf_uart_statistics_t &statistics) const noexcept
Get UART operation statistics.
Definition BaseUart.h:379
virtual bool Deinitialize() noexcept=0
Deinitialize the UART driver.
virtual hf_uart_err_t GetDiagnostics(hf_uart_diagnostics_t &diagnostics) const noexcept
Get UART diagnostic information.
Definition BaseUart.h:389
virtual bool Open() noexcept
Open the UART (alias for Initialize).
Definition BaseUart.h:311
bool initialized_
Initialization status.
Definition BaseUart.h:403
virtual hf_u16_t BytesAvailable() noexcept=0
Get the number of bytes available to read.
virtual hf_uart_err_t FlushTx() noexcept=0
Flush the transmit buffer.
virtual bool WriteByte(hf_u8_t byte) noexcept
Write a single byte to the UART.
Definition BaseUart.h:344
virtual int Printf(const char *format,...) noexcept=0
Printf-style formatted output.
virtual hf_uart_err_t Read(hf_u8_t *data, hf_u16_t length, hf_u32_t timeout_ms=0) noexcept=0
Read data from the UART.
bool IsInitialized() const noexcept
Checks if the driver is initialized.
Definition BaseUart.h:230
virtual hf_uart_err_t ResetStatistics() noexcept
Reset UART operation statistics.
Definition BaseUart.h:359
virtual bool WriteString(const char *str) noexcept
Write a string to the UART.
Definition BaseUart.h:332
hf_port_num_t port_
UART port number.
Definition BaseUart.h:402
virtual hf_uart_err_t ResetDiagnostics() noexcept
Reset UART diagnostic information.
Definition BaseUart.h:369
hf_port_num_t GetPort() const noexcept
Get the UART port number.
Definition BaseUart.h:238
virtual bool Initialize() noexcept=0
Initialize the UART driver.
virtual ~BaseUart() noexcept=default
Virtual destructor ensures proper cleanup in derived classes.
virtual bool Close() noexcept
Close the UART (alias for Deinitialize).
Definition BaseUart.h:319
hf_uart_diagnostics_t diagnostics_
UART diagnostic information.
Definition BaseUart.h:405
BaseUart(hf_port_num_t port) noexcept
Protected constructor with port.
Definition BaseUart.h:399
#define HF_UART_ERR_LIST(X)
HardFOC UART error codes.
Definition BaseUart.h:42
UART diagnostic information.
Definition BaseUart.h:140
hf_u32_t rx_buffer_usage
RX buffer usage percentage.
Definition BaseUart.h:152
bool flow_control_active
Flow control status.
Definition BaseUart.h:148
hf_u64_t last_error_timestamp
Timestamp of last error (microseconds)
Definition BaseUart.h:144
hf_u32_t tx_buffer_usage
TX buffer usage percentage.
Definition BaseUart.h:151
bool is_initialized
Initialization status.
Definition BaseUart.h:145
hf_u32_t error_reset_count
Number of times error state was reset.
Definition BaseUart.h:143
hf_uart_diagnostics_t() noexcept
Definition BaseUart.h:155
hf_uart_err_t last_error
Last error that occurred.
Definition BaseUart.h:141
bool pattern_detection_active
Pattern detection status.
Definition BaseUart.h:149
hf_u32_t consecutive_errors
Number of consecutive errors.
Definition BaseUart.h:142
bool is_receiving
Reception status.
Definition BaseUart.h:147
bool wakeup_enabled
Wakeup status.
Definition BaseUart.h:150
hf_u32_t event_queue_usage
Event queue usage percentage.
Definition BaseUart.h:153
bool is_transmitting
Transmission status.
Definition BaseUart.h:146
UART operation statistics.
Definition BaseUart.h:114
hf_u32_t break_count
Break condition count.
Definition BaseUart.h:123
hf_u64_t last_activity_timestamp
Last activity timestamp (microseconds)
Definition BaseUart.h:127
hf_u32_t rx_byte_count
Total bytes received.
Definition BaseUart.h:116
hf_u64_t initialization_timestamp
Initialization timestamp (microseconds)
Definition BaseUart.h:128
hf_u32_t tx_error_count
Transmission error count.
Definition BaseUart.h:117
hf_u32_t timeout_count
Timeout occurrence count.
Definition BaseUart.h:124
hf_u32_t wakeup_count
Wakeup event count.
Definition BaseUart.h:126
hf_u32_t frame_error_count
Frame error count.
Definition BaseUart.h:119
hf_u32_t tx_byte_count
Total bytes transmitted.
Definition BaseUart.h:115
hf_u32_t rx_error_count
Reception error count.
Definition BaseUart.h:118
hf_u32_t parity_error_count
Parity error count.
Definition BaseUart.h:120
hf_u32_t pattern_detect_count
Pattern detection count.
Definition BaseUart.h:125
hf_u32_t noise_error_count
Noise error count.
Definition BaseUart.h:122
hf_uart_statistics_t() noexcept
Definition BaseUart.h:130
hf_u32_t overrun_error_count
Overrun error count.
Definition BaseUart.h:121