HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
StmUart Class Reference

STM32 UART implementation. More...

#include <StmUart.h>

Inheritance diagram for StmUart:
[legend]
Collaboration diagram for StmUart:
[legend]

Public Member Functions

 StmUart (UART_HandleTypeDef *huart, hf_port_num_t port=0) noexcept
 Construct from HAL UART handle.
 
 StmUart (const hf_stm32_uart_config_t &config) noexcept
 Construct from config struct.
 
 ~StmUart () noexcept override
 
 StmUart (const StmUart &)=delete
 
StmUartoperator= (const StmUart &)=delete
 
bool Initialize () noexcept override
 Initialize the UART driver.
 
bool Deinitialize () noexcept override
 Deinitialize the UART driver.
 
hf_uart_err_t Write (const hf_u8_t *data, hf_u16_t length, hf_u32_t timeout_ms=0) noexcept override
 Write data to the UART.
 
hf_uart_err_t Read (hf_u8_t *data, hf_u16_t length, hf_u32_t timeout_ms=0) noexcept override
 Read data from the UART.
 
hf_u16_t BytesAvailable () noexcept override
 Get the number of bytes available to read.
 
hf_uart_err_t FlushTx () noexcept override
 Flush the transmit buffer.
 
hf_uart_err_t FlushRx () noexcept override
 Flush the receive buffer.
 
int Printf (const char *format,...) noexcept override
 Printf-style formatted output.
 
UART_HandleTypeDef * GetHalHandle () const noexcept
 
void EnableRxBuffer (hf_u8_t *buffer, hf_u16_t size) noexcept
 Enable a software RX ring buffer.
 
void FeedRxByte (hf_u8_t byte) noexcept
 Feed a byte into the RX ring buffer (call from ISR).
 
void FeedRxBlock (const hf_u8_t *data, hf_u16_t length) noexcept
 Feed a block of bytes into the RX ring buffer (call from ISR/DMA).
 
- Public Member Functions inherited from BaseUart
virtual ~BaseUart () noexcept=default
 Virtual destructor ensures proper cleanup in derived classes.
 
 BaseUart (const BaseUart &)=delete
 
BaseUartoperator= (const BaseUart &)=delete
 
 BaseUart (BaseUart &&)=delete
 
BaseUartoperator= (BaseUart &&)=delete
 
bool EnsureInitialized () noexcept
 Ensures that the UART is initialized (lazy initialization).
 
bool EnsureDeinitialized () noexcept
 Ensures that the UART is deinitialized (lazy deinitialization).
 
bool IsInitialized () const noexcept
 Checks if the driver is initialized.
 
hf_port_num_t GetPort () const noexcept
 Get the UART port number.
 
virtual bool Open () noexcept
 Open the UART (alias for Initialize).
 
virtual bool Close () noexcept
 Close the UART (alias for Deinitialize).
 
virtual bool WriteString (const char *str) noexcept
 Write a string to the UART.
 
virtual bool WriteByte (hf_u8_t byte) noexcept
 Write a single byte to the UART.
 
virtual hf_uart_err_t ResetStatistics () noexcept
 Reset UART operation statistics.
 
virtual hf_uart_err_t ResetDiagnostics () noexcept
 Reset UART diagnostic information.
 
virtual hf_uart_err_t GetStatistics (hf_uart_statistics_t &statistics) const noexcept
 Get UART operation statistics.
 
virtual hf_uart_err_t GetDiagnostics (hf_uart_diagnostics_t &diagnostics) const noexcept
 Get UART diagnostic information.
 

Private Attributes

UART_HandleTypeDef * huart_
 
hf_u32_t default_timeout_ms_
 
hf_u8_trx_buf_
 
hf_u16_t rx_buf_size_
 
volatile hf_u16_t rx_head_
 Write index (ISR writes)
 
volatile hf_u16_t rx_tail_
 Read index (user reads)
 

Additional Inherited Members

- Protected Member Functions inherited from BaseUart
 BaseUart (hf_port_num_t port) noexcept
 Protected constructor with port.
 
- Protected Attributes inherited from BaseUart
hf_port_num_t port_
 UART port number.
 
bool initialized_
 Initialization status.
 
hf_uart_statistics_t statistics_
 UART operation statistics.
 
hf_uart_diagnostics_t diagnostics_
 UART diagnostic information.
 

Detailed Description

STM32 UART implementation.

Design:

  • Accepts a CubeMX-configured UART_HandleTypeDef*.
  • Blocking TX via HAL_UART_Transmit.
  • Blocking RX via HAL_UART_Receive.
  • Optional software circular RX buffer fed by idle-line DMA.
  • Printf via vsnprintf + Write.

Constructor & Destructor Documentation

◆ StmUart() [1/3]

StmUart::StmUart ( UART_HandleTypeDef * huart,
hf_port_num_t port = 0 )
explicitnoexcept

Construct from HAL UART handle.

Parameters
huartCubeMX UART handle.
portLogical port number (for base class).

◆ StmUart() [2/3]

StmUart::StmUart ( const hf_stm32_uart_config_t & config)
explicitnoexcept

Construct from config struct.

◆ ~StmUart()

StmUart::~StmUart ( )
overridenoexcept

◆ StmUart() [3/3]

StmUart::StmUart ( const StmUart & )
delete

Member Function Documentation

◆ BytesAvailable()

hf_u16_t StmUart::BytesAvailable ( )
overridevirtualnoexcept

Get the number of bytes available to read.

Returns
Number of bytes available in the receive buffer

Implements BaseUart.

◆ Deinitialize()

bool StmUart::Deinitialize ( )
overridevirtualnoexcept

Deinitialize the UART driver.

Returns
true if successful, false otherwise

Implements BaseUart.

◆ EnableRxBuffer()

void StmUart::EnableRxBuffer ( hf_u8_t * buffer,
hf_u16_t size )
noexcept

Enable a software RX ring buffer.

Parameters
bufferUser-provided buffer.
sizeBuffer size in bytes.

When enabled, Read() pulls from this ring buffer instead of calling HAL_UART_Receive directly. The user must still call FeedRxByte() from HAL_UART_RxCpltCallback or a DMA half-transfer ISR.

◆ FeedRxBlock()

void StmUart::FeedRxBlock ( const hf_u8_t * data,
hf_u16_t length )
noexcept

Feed a block of bytes into the RX ring buffer (call from ISR/DMA).

◆ FeedRxByte()

void StmUart::FeedRxByte ( hf_u8_t byte)
noexcept

Feed a byte into the RX ring buffer (call from ISR).

◆ FlushRx()

hf_uart_err_t StmUart::FlushRx ( )
overridevirtualnoexcept

Flush the receive buffer.

Returns
hf_uart_err_t result code

Implements BaseUart.

◆ FlushTx()

hf_uart_err_t StmUart::FlushTx ( )
overridevirtualnoexcept

Flush the transmit buffer.

Returns
hf_uart_err_t result code

Implements BaseUart.

◆ GetHalHandle()

UART_HandleTypeDef * StmUart::GetHalHandle ( ) const
inlinenoexcept

◆ Initialize()

bool StmUart::Initialize ( )
overridevirtualnoexcept

Initialize the UART driver.

Returns
true if successful, false otherwise

Implements BaseUart.

◆ operator=()

StmUart & StmUart::operator= ( const StmUart & )
delete

◆ Printf()

int StmUart::Printf ( const char * format,
... )
overridevirtualnoexcept

Printf-style formatted output.

Parameters
formatFormat string
...Format arguments
Returns
Number of characters written, or -1 on error

Implements BaseUart.

◆ Read()

hf_uart_err_t StmUart::Read ( hf_u8_t * data,
hf_u16_t length,
hf_u32_t timeout_ms = 0 )
overridevirtualnoexcept

Read data from the UART.

Parameters
dataBuffer to store received data
lengthNumber of bytes to read
timeout_msTimeout in milliseconds (0 = use default)
Returns
hf_uart_err_t result code

Implements BaseUart.

◆ Write()

hf_uart_err_t StmUart::Write ( const hf_u8_t * data,
hf_u16_t length,
hf_u32_t timeout_ms = 0 )
overridevirtualnoexcept

Write data to the UART.

Parameters
dataData buffer to transmit
lengthNumber of bytes to write
timeout_msTimeout in milliseconds (0 = use default)
Returns
hf_uart_err_t result code

Implements BaseUart.

Member Data Documentation

◆ default_timeout_ms_

hf_u32_t StmUart::default_timeout_ms_
private

◆ huart_

UART_HandleTypeDef* StmUart::huart_
private

◆ rx_buf_

hf_u8_t* StmUart::rx_buf_
private

◆ rx_buf_size_

hf_u16_t StmUart::rx_buf_size_
private

◆ rx_head_

volatile hf_u16_t StmUart::rx_head_
private

Write index (ISR writes)

◆ rx_tail_

volatile hf_u16_t StmUart::rx_tail_
private

Read index (user reads)


The documentation for this class was generated from the following files: