STM32 core 3.0.1-dev
Arduino core for STM32 based boards: API reference
← STM32duino documentation
Loading...
Searching...
No Matches
Uart Class Reference

STM32 hardware UART. More...

#include <Serial.h>

Inheritance diagram for Uart:

Public Member Functions

 Uart (pin_size_t _rx, pin_size_t _tx, pin_size_t _rts=NUM_DIGITAL_PINS, pin_size_t _cts=NUM_DIGITAL_PINS)
 Create a UART from Arduino digital pin numbers.
 
 Uart (PinName _rx, PinName _tx, PinName _rts=NC, PinName _cts=NC)
 Create a UART from STM32 pin names.
 
 Uart (void *peripheral, HalfDuplexMode_t halfDuplex=HALF_DUPLEX_DISABLED)
 Create a UART from an STM32 USART/UART peripheral.
 
 Uart (pin_size_t _rxtx)
 Create a half-duplex UART on a single digital pin.
 
 Uart (PinName _rxtx)
 Create a half-duplex UART on a single STM32 pin.
 
void begin (unsigned long baud) override
 Start the UART with 8 data bits, no parity and 1 stop bit (SERIAL_8N1).
 
void begin (unsigned long baud, uint16_t config) override
 Start the UART with the given baud rate and frame format.
 
void end () override
 Wait for pending transmission, stop the UART, release its pins and clear the receive buffer.
 
int available (void) override
 Get the number of bytes available to read.
 
int peek (void) override
 Read the next byte without removing it from the receive buffer.
 
int read (void) override
 Read and remove the next byte from the receive buffer.
 
void flush () override
 Wait until all queued bytes are transmitted.
 
size_t write (uint8_t c) override
 Write one byte.
 
 operator bool () override
 Check whether the UART is started.
 
int availableForWrite (void)
 Get the number of bytes that can be written without blocking.
 
void flush (uint32_t timeout)
 Wait until all queued bytes are transmitted, or until a timeout elapses.
 
size_t write (unsigned long n)
 Write one byte, truncating the value to 8 bits.
 
size_t write (long n)
 Write one byte, truncating the value to 8 bits.
 
size_t write (unsigned int n)
 Write one byte, truncating the value to 8 bits.
 
size_t write (int n)
 Write one byte, truncating the value to 8 bits.
 
size_t write (const uint8_t *buffer, size_t size)
 Write a buffer of bytes.
 
void setRx (pin_size_t _rx)
 Set the RX pin by Arduino pin number.
 
void setTx (pin_size_t _tx)
 Set the TX pin by Arduino pin number.
 
void setRx (PinName _rx)
 Set the RX pin by STM32 pin name.
 
void setTx (PinName _tx)
 Set the TX pin by STM32 pin name.
 
void setRts (pin_size_t _rts)
 Enable hardware RTS flow control on a digital pin.
 
void setCts (pin_size_t _cts)
 Enable hardware CTS flow control on a digital pin.
 
void setRtsCts (pin_size_t _rts, pin_size_t _cts)
 Enable hardware RTS and CTS flow control on digital pins.
 
void setRts (PinName _rts)
 Enable hardware RTS flow control on an STM32 pin.
 
void setCts (PinName _cts)
 Enable hardware CTS flow control on an STM32 pin.
 
void setRtsCts (PinName _rts, PinName _cts)
 Enable hardware RTS and CTS flow control on STM32 pins.
 
void setHalfDuplex (void)
 Enable half-duplex mode by setting the RX pin to NC.
 
bool isHalfDuplex (void) const
 Check whether the UART is in half-duplex mode.
 
void enableHalfDuplexRx (void)
 Enable reception in half-duplex mode after the pending transmission ends.
 
void setRxInvert (void)
 Invert the RX line in hardware.
 
void setTxInvert (void)
 Invert the TX line in hardware.
 
void setDataInvert (void)
 Invert the data bits in hardware.
 
UART_HandleTypeDef * getHandle (void)
 Get the underlying HAL UART handle.
 

Static Public Member Functions

static void _rx_complete_irq (serial_t *obj)
 RX interrupt handler.
 
static int _tx_complete_irq (serial_t *obj)
 TX interrupt handler.
 

Protected Attributes

bool _written
 Has any byte been written to the UART since begin().
 
bool _rx_invert
 Hardware inversion of the RX line, set by setRxInvert().
 
bool _tx_invert
 Hardware inversion of the TX line, set by setTxInvert().
 
bool _data_invert
 Hardware inversion of the data bits, set by setDataInvert().
 
unsigned char _rx_buffer [SERIAL_RX_BUFFER_SIZE]
 Receive ring buffer, filled by _rx_complete_irq() and emptied by read().
 
unsigned char _tx_buffer [SERIAL_TX_BUFFER_SIZE]
 Transmit ring buffer, filled by write() and emptied by _tx_complete_irq().
 
serial_t _serial
 Low-level serial object: pins, ring buffer indexes and HAL handle.
 

Friends

class STM32LowPower
 

Detailed Description

STM32 hardware UART.

Implements the Arduino Stream and HardwareSerial API.

Use one of the constructors to bind a UART to pins or to a peripheral, then call begin().

Constructor & Destructor Documentation

◆ Uart() [1/5]

Uart::Uart ( pin_size_t  _rx,
pin_size_t  _tx,
pin_size_t  _rts = NUM_DIGITAL_PINS,
pin_size_t  _cts = NUM_DIGITAL_PINS 
)

Create a UART from Arduino digital pin numbers.

Parameters
_rxDigital pin used for RX.
_txDigital pin used for TX.
_rtsDigital pin used for RTS flow control, NUM_DIGITAL_PINS if unused.
_ctsDigital pin used for CTS flow control, NUM_DIGITAL_PINS if unused.

◆ Uart() [2/5]

Uart::Uart ( PinName  _rx,
PinName  _tx,
PinName  _rts = NC,
PinName  _cts = NC 
)

Create a UART from STM32 pin names.

Parameters
_rxPin used for RX.
_txPin used for TX.
_rtsPin used for RTS flow control, NC if unused.
_ctsPin used for CTS flow control, NC if unused.

◆ Uart() [3/5]

Uart::Uart ( void *  peripheral,
HalfDuplexMode_t  halfDuplex = HALF_DUPLEX_DISABLED 
)

Create a UART from an STM32 USART/UART peripheral.

Pins are taken from the variant.

Parameters
peripheralUSART/UART peripheral instance, e.g. USART1.
halfDuplexHALF_DUPLEX_ENABLED to use the TX pin only.

◆ Uart() [4/5]

Uart::Uart ( pin_size_t  _rxtx)

Create a half-duplex UART on a single digital pin.

The pin is used as TX and RX is NC.

Parameters
_rxtxDigital pin used for TX.

◆ Uart() [5/5]

Uart::Uart ( PinName  _rxtx)

Create a half-duplex UART on a single STM32 pin.

The pin is used as TX and RX is NC.

Parameters
_rxtxPin used for TX.

Member Function Documentation

◆ _rx_complete_irq()

void Uart::_rx_complete_irq ( serial_t *  obj)
static

RX interrupt handler.

Stores the received byte in the receive buffer.

Note
Called from the UART interrupt context: it must stay short and never block. If the receive buffer is full, the received byte is dropped.
Parameters
objUART object that received the byte.

◆ _tx_complete_irq()

int Uart::_tx_complete_irq ( serial_t *  obj)
static

TX interrupt handler.

Starts the next chunk of queued data, if any.

Note
Called from the UART interrupt context when a transfer completes: it must stay short and never block. A chunk never wraps around the end of the transmit buffer, so a wrapped buffer is sent in two transfers.
Parameters
objUART object whose transfer completed.
Returns
-1 if a new transfer was started, 0 if the transmit buffer is empty.

◆ available()

int Uart::available ( void  )
override

Get the number of bytes available to read.

Returns
Number of bytes in the receive buffer.

◆ availableForWrite()

int Uart::availableForWrite ( void  )

Get the number of bytes that can be written without blocking.

Returns
Free space in the transmit buffer, in bytes.

◆ begin() [1/2]

void Uart::begin ( unsigned long  baud)
inlineoverride

Start the UART with 8 data bits, no parity and 1 stop bit (SERIAL_8N1).

Parameters
baudBaud rate in bits per second.

◆ begin() [2/2]

void Uart::begin ( unsigned long  baud,
uint16_t  config 
)
override

Start the UART with the given baud rate and frame format.

Parameters
baudBaud rate in bits per second.
configFrame format, e.g. SERIAL_8N1 or SERIAL_8E1.

◆ flush()

void Uart::flush ( uint32_t  timeout)

Wait until all queued bytes are transmitted, or until a timeout elapses.

Parameters
timeoutMaximum wait in milliseconds. 0 waits without limit. On timeout, the pending transmit data is discarded.

◆ getHandle()

UART_HandleTypeDef * Uart::getHandle ( void  )
inline

Get the underlying HAL UART handle.

Returns
Pointer to the HAL UART handle.

◆ isHalfDuplex()

bool Uart::isHalfDuplex ( void  ) const

Check whether the UART is in half-duplex mode.

Returns
true if the RX pin is NC.

◆ operator bool()

Uart::operator bool ( )
inlineoverride

Check whether the UART is started.

Returns
true if begin() was called and the UART is ready.

◆ peek()

int Uart::peek ( void  )
override

Read the next byte without removing it from the receive buffer.

Returns
Next byte, or -1 if none is available.

◆ read()

int Uart::read ( void  )
override

Read and remove the next byte from the receive buffer.

Returns
Next byte, or -1 if none is available.

◆ setCts() [1/2]

void Uart::setCts ( pin_size_t  _cts)

Enable hardware CTS flow control on a digital pin.

Call before begin().

Parameters
_ctsDigital pin used for CTS.

◆ setCts() [2/2]

void Uart::setCts ( PinName  _cts)

Enable hardware CTS flow control on an STM32 pin.

Call before begin().

Parameters
_ctsPin used for CTS.

◆ setDataInvert()

void Uart::setDataInvert ( void  )

Invert the data bits in hardware.

Call before begin().

◆ setHalfDuplex()

void Uart::setHalfDuplex ( void  )

Enable half-duplex mode by setting the RX pin to NC.

Call before begin().

◆ setRts() [1/2]

void Uart::setRts ( pin_size_t  _rts)

Enable hardware RTS flow control on a digital pin.

Call before begin().

Parameters
_rtsDigital pin used for RTS.

◆ setRts() [2/2]

void Uart::setRts ( PinName  _rts)

Enable hardware RTS flow control on an STM32 pin.

Call before begin().

Parameters
_rtsPin used for RTS.

◆ setRtsCts() [1/2]

void Uart::setRtsCts ( pin_size_t  _rts,
pin_size_t  _cts 
)

Enable hardware RTS and CTS flow control on digital pins.

Call before begin().

Parameters
_rtsDigital pin used for RTS.
_ctsDigital pin used for CTS.

◆ setRtsCts() [2/2]

void Uart::setRtsCts ( PinName  _rts,
PinName  _cts 
)

Enable hardware RTS and CTS flow control on STM32 pins.

Call before begin().

Parameters
_rtsPin used for RTS.
_ctsPin used for CTS.

◆ setRx() [1/2]

void Uart::setRx ( pin_size_t  _rx)

Set the RX pin by Arduino pin number.

Call before begin().

Parameters
_rxDigital pin used for RX.

◆ setRx() [2/2]

void Uart::setRx ( PinName  _rx)

Set the RX pin by STM32 pin name.

Call before begin().

Parameters
_rxPin used for RX.

◆ setRxInvert()

void Uart::setRxInvert ( void  )

Invert the RX line in hardware.

Call before begin().

◆ setTx() [1/2]

void Uart::setTx ( pin_size_t  _tx)

Set the TX pin by Arduino pin number.

Call before begin().

Parameters
_txDigital pin used for TX.

◆ setTx() [2/2]

void Uart::setTx ( PinName  _tx)

Set the TX pin by STM32 pin name.

Call before begin().

Parameters
_txPin used for TX.

◆ setTxInvert()

void Uart::setTxInvert ( void  )

Invert the TX line in hardware.

Call before begin().

◆ write() [1/6]

size_t Uart::write ( const uint8_t *  buffer,
size_t  size 
)

Write a buffer of bytes.

Parameters
bufferData to write.
sizeNumber of bytes in buffer.
Returns
Number of bytes written, 0 if the UART is not started.

◆ write() [2/6]

size_t Uart::write ( int  n)
inline

Write one byte, truncating the value to 8 bits.

Parameters
nValue to write.
Returns
Number of bytes written.

◆ write() [3/6]

size_t Uart::write ( long  n)
inline

Write one byte, truncating the value to 8 bits.

Parameters
nValue to write.
Returns
Number of bytes written.

◆ write() [4/6]

size_t Uart::write ( uint8_t  c)
override

Write one byte.

Parameters
cByte to write.
Returns
Number of bytes written: 1, or 0 if the UART is not started.

◆ write() [5/6]

size_t Uart::write ( unsigned int  n)
inline

Write one byte, truncating the value to 8 bits.

Parameters
nValue to write.
Returns
Number of bytes written.

◆ write() [6/6]

size_t Uart::write ( unsigned long  n)
inline

Write one byte, truncating the value to 8 bits.

Parameters
nValue to write.
Returns
Number of bytes written.

Member Data Documentation

◆ _data_invert

bool Uart::_data_invert
protected

Hardware inversion of the data bits, set by setDataInvert().

Applied by begin().

◆ _rx_invert

bool Uart::_rx_invert
protected

Hardware inversion of the RX line, set by setRxInvert().

Applied by begin().

◆ _tx_invert

bool Uart::_tx_invert
protected

Hardware inversion of the TX line, set by setTxInvert().

Applied by begin().

◆ _written

bool Uart::_written
protected

Has any byte been written to the UART since begin().

Used by flush() to skip the wait.


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