Arduino Core for STM32  1.0
HardwareSerial Class Reference

This class is for handling UART periphery. More...

#include <HardwareSerial.h>

Inheritance diagram for HardwareSerial:
Collaboration diagram for HardwareSerial:

Public Member Functions

 HardwareSerial (UART_HandleTypeDef &uart)
 Constructor. More...
 
 ~HardwareSerial ()
 Destructor. More...
 
bool connectDMAtoRx (DMA_HandleTypeDef &DMA_handle)
 Use this method to connect DMA channel to UART Rx programmatically. More...
 
bool connectDMAtoTx (DMA_HandleTypeDef &DMA_handle)
 Use this method to connect DMA channel to UART Tx programmatically. More...
 
bool disconnectDMAfromRx ()
 Disconnects DMA channel from UART Rx. More...
 
bool disconnectDMAfromTx ()
 Disconnects DMA channel from UART Tx. More...
 
bool isDMAconnectedToRx (void)
 Checks if DMA channel is connected to UART Rx and can be used for data receiving. More...
 
bool isDMAconnectedToTx (void)
 Checks if DMA channel is connected to UART Tx and can be used for data transmitting. More...
 
bool begin ()
 Initialize and enable UART periphery and it's clock. More...
 
bool begin (unsigned long baud)
 Initialize and enable UART periphery and it's clock. More...
 
bool begin (unsigned long baud, SerialMode mode)
 Initialize and enable UART periphery and it's clock. More...
 
bool begin (SerialConfig config)
 Initialize and enable UART periphery and it's clock. More...
 
bool begin (unsigned long baud, SerialConfig config)
 Initialize and enable UART periphery and it's clock. More...
 
bool begin (SerialConfig config, AdvSerialConfig advConfig)
 Initialize and enable UART periphery and it's clock. More...
 
bool begin (unsigned long baud, AdvSerialConfig advConfig)
 Initialize and enable UART periphery and it's clock. More...
 
bool begin (unsigned long baud, SerialConfig config, AdvSerialConfig advConfig)
 Initialize and enable UART periphery and it's clock. More...
 
bool begin (unsigned long baud, SerialMode mode, AdvSerialConfig advConfig)
 Initialize and enable UART periphery and it's clock. More...
 
bool begin (unsigned long baud, SerialConfig config, SerialMode mode, AdvSerialConfig advConfig)
 Initialize and enable UART periphery and it's clock. More...
 
void end ()
 Deinitialization and disabling UART periphery, it can be used for "sleep mode". More...
 
bool updateBaudRate (unsigned long baud)
 Updates baud rate at runtime. More...
 
size_t getRxBufferSize ()
 Gets the size of Rx buffer. More...
 
int available (void) override
 Gets the count of bytes (characters) available for reading from the Rx buffer. More...
 
bool bufferOverflowed ()
 Returns true when Rx buffer has overflowed. More...
 
int peek (void) override
 Returns the next byte (character) of incoming serial data without removing it from the Rx buffer. More...
 
int read (void) override
 Reads incoming serial data. More...
 
size_t read (char *buffer, size_t size)
 Copies the number of data from Rx buffer to your buffer. More...
 
size_t read (uint8_t *buffer, size_t size)
 Copies the number of data from Rx buffer to your buffer. More...
 
size_t readBytes (char *buffer, size_t size) override
 @Reads characters from the Rx buffer into your buffer. More...
 
size_t readBytes (uint8_t *buffer, size_t size) override
 @Reads characters from the Rx buffer into your buffer. More...
 
void flush (uint32_t timeout=0)
 Waits for the transmission of outgoing serial data to complete. More...
 
size_t write (uint8_t c) override
 Writes binary data to the serial port. More...
 
size_t write (const uint8_t *buffer, size_t size) override
 Writes binary data to the serial port. More...
 
size_t writeNonBlocking (const uint8_t *buffer, size_t size)
 Writes binary data to the serial port. More...
 
size_t writeNonBlocking (const char *buffer, size_t size)
 Writes binary data to the serial port. More...
 
bool isSending ()
 Checks if data are sending right now and if periphery is busy. More...
 
 operator bool () const
 Checks if UART instance exists. More...
 
bool instanceExists ()
 Checks if UART instance exists. More...
 
bool isTxEnabled (void)
 Checks if transmitting mode is enabled. More...
 
bool isRxEnabled (void)
 Checks if receiving mode is enabled. More...
 
uint32_t getClockInstFrequency ()
 Gets UART periphery clock frequency. More...
 
int baudRate (void)
 Gets UART baud rate that was set in method begin() or updateBaudRate(). More...
 
bool hasError (void)
 Checks if faulty byte was received. More...
 
void setWriteDoneCallback (void(*WriteDoneCallback_)())
 Sets callback function, that will be called when all data using writeNonBlocking() method were send. More...
 
UART_HandleTypeDef & getHAL_UART_Struct ()
 Returns UART structure, that is used for handling UART. More...
 
virtual size_t write (uint8_t byte)=0
 Writes one unsigned byte (character). More...
 
size_t write (const char *str)
 Writes character array (string). More...
 
virtual size_t write (const uint8_t *buffer, size_t size)
 Writes byte array (string). More...
 
size_t write (const char *buffer, size_t size)
 Writes character array (string). More...
 
- Public Member Functions inherited from Stream
 Stream ()
 Constructor. More...
 
Streamoperator>> (char &c)
 This operator (>>) applied to an input stream is known as extraction operator, it can be used same as cin>> operator. More...
 
Streamoperator>> (char *buffer)
 This operator (>>) applied to an input stream is known as extraction operator, it can be used same as cin>> operator. More...
 
Streamoperator>> (uint8_t *buffer)
 This operator (>>) applied to an input stream is known as extraction operator, it can be used same as cin>> operator. More...
 
 __INTEGER_ONLY__ (T, Stream &) operator>>(T &val)
 This operator (>>) applied to an input stream is known as extraction operator, it can be used same as cin>> operator. More...
 
 __FLOATING_ONLY__ (T, Stream &) operator>>(T &val)
 This operator (>>) applied to an input stream is known as extraction operator, it can be used same as cin>> operator. More...
 
void setTimeout (unsigned long timeout)
 Sets the maximum milliseconds to wait for data transmitting or receiving. More...
 
virtual void width (size_t width_)
 Sets receiving width, that is applied when using operators >> (same as cin.width() method). More...
 
bool find (const char *target)
 Reads and removes data from the stream until the target string is found. More...
 
bool find (uint8_t *target)
 Reads and removes data from the stream until the target string is found. More...
 
bool find (const char *target, size_t length)
 Reads and removes data from the stream until the target string is found. More...
 
bool find (const uint8_t *target, size_t length)
 Reads and removes data from the stream until the target string is found. More...
 
bool find (char target)
 Reads and removes data from the stream until the target character is found. More...
 
bool findUntil (const char *target, const char *terminator)
 Reads data from the stream until a target string or terminator string is found. More...
 
bool findUntil (const uint8_t *target, const char *terminator)
 Reads data from the stream until a target string or terminator string is found. More...
 
bool findUntil (const char *target, size_t targetLen, const char *terminate, size_t termLen)
 Reads data from the stream until a target string or terminator string is found. More...
 
bool findUntil (const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen)
 Reads data from the stream until a target string or terminator string is found. More...
 
long parseInt ()
 Parses the first valid (long) integer number from the stream. More...
 
 __INTEGER_ONLY__ (T, T) inline parseNum()
 Parses the first valid integer number from the stream. More...
 
float parseFloat ()
 Parses the first valid floating point number from the stream. More...
 
 __FLOATING_ONLY__ (T, T) inline parseNum()
 Parses the first valid floating point number from the stream. More...
 
size_t readBytesUntil (char terminator, char *buffer, size_t length)
 Reads characters from the stream into a buffer. More...
 
size_t readBytesUntil (char terminator, uint8_t *buffer, size_t length)
 Reads characters from the stream into a buffer. More...
 
- Public Member Functions inherited from Print
 Print ()
 Constructor. More...
 
int getWriteError ()
 Gets write error. More...
 
void clearWriteError ()
 Clears write error. More...
 
size_t write (const char *str)
 Writes character array (string). More...
 
size_t write (const char *buffer, size_t size)
 Writes character array (string). More...
 
 __INTEGER_ONLY__ (T, size_t) inline write(T val)
 Converts integer type variable to unsigned byte and writes it. More...
 
virtual int availableForWrite ()
 Gets how many bytes can be written, meaning "a single write may block". More...
 
size_t printf (const char *format,...) __attribute__((format(printf
 Prints formatted data. More...
 
size_t size_t print (const String &s)
 Prints string. More...
 
size_t print (const char str[])
 Prints character array (string). More...
 
size_t print (char c)
 Prints one character. More...
 
 __SIGNED_INTEGER_ONLY__ (T, size_t) print(T n
 Converts integer number to character array and prints it. More...
 
size_t println (const String &s)
 Prints string followed by carriage return and new line. More...
 
size_t println (const char[])
 Prints character array (string) followed by carriage return and new line. More...
 
size_t println (char)
 Prints one character followed by carriage return and new line. More...
 
 __INTEGER_ONLY__ (T, size_t) inline println(T n
 Converts integer number to character array followed by carriage return and new line and prints it. More...
 
size_t println (void)
 Prints carriage return and new line. More...
 
template<class T >
Printoperator<< (T value)
 This operator (<<) applied to an output stream is known as insertion operator, it can be used same as cout<< operator. More...
 

Static Public Member Functions

static void handleRxFullCallback (USART_TypeDef *instance)
 Handles RxFull callback function. More...
 
static void handleRxFullCallback (uint32_t UARTIndex)
 Handles RxFull callback function. More...
 
static void handleTxDoneCallback (USART_TypeDef *instance)
 Handles TxDone callback function. More...
 
static void handleTxDoneCallback (uint32_t UARTIndex)
 Handles TxDone callback function. More...
 
static uint32_t getUARTIndex (USART_TypeDef *instance)
 Gets UART instance index. More...
 
static HardwareSerial ** getUARTInstances ()
 Gets array of all created UART instances (just from this class, not UART structure). More...
 
static uint8_t getAvailableUARTsCount ()
 Gets available UARTs instances count. More...
 

Protected Member Functions

void UART_EndRxTransfer (UART_HandleTypeDef *huart)
 Aborts Rx IT Transfer. More...
 
void UART_EndTxTransfer (UART_HandleTypeDef *huart)
 Aborts Tx IT Transfer. More...
 
void abortDMARx ()
 Aborts Rx DMA Transfer. More...
 
void abortDMATx ()
 Aborts Tx DMA Transfer. More...
 
bool disconnectDMAfromRx (bool startRxIT=true)
 Disconnects DMA channel from UART Rx. More...
 
void UART_RxFullCallback ()
 This method is called when Rx Buffer is full. More...
 
void addThisToInstArr ()
 Writes new class instance to array of all timer instances. More...
 
void removeThisFromInstArr ()
 Removes this instance from instances array. More...
 
size_t widthCheck (size_t width_) override
 Overriden width check function from Stream class. More...
 
- Protected Member Functions inherited from Stream
int timedRead ()
 Private method to read stream with timeout. More...
 
int timedPeek ()
 Private method to peek stream with timeout. More...
 
int peekNextDigit ()
 Gets the next numeric digit in the stream or -1 if timeout. More...
 
long parseInt (char skipChar)
 Parses the first valid (long) integer number from the stream. More...
 
 __INTEGER_ONLY__ (T, T) parseNum(char skipChar)
 Parses the first valid integer number from the stream. More...
 
float parseFloat (char skipChar)
 Parses the first valid floating point number from the stream. More...
 
 __FLOATING_ONLY__ (T, T) parseNum(char skipChar)
 Parses the first valid floating point number from the stream. More...
 
- Protected Member Functions inherited from Print
void setWriteError (int err=1)
 Sets write error. More...
 

Protected Attributes

UART_HandleTypeDef * _uart
 UART periphery handle structure. More...
 
uint8_t rxBuf [HS_RX_BUF_SIZE]
 DMA or IT receiving buffer. More...
 
volatile uint16_t rxBufOVFCNT = 0
 Rx buffer overflow count, restarts when read() was called. More...
 
uint16_t RxBufferReadPos = 0
 Rading position in Rx Buffer. More...
 
void(* WriteDoneCallback )() = NULL
 User callback function, that can be set using setWriteDoneCallback(). More...
 
- Protected Attributes inherited from Stream
unsigned long _timeout = 1000
 Number of milliseconds to wait for the next char before aborting timed read. More...
 
unsigned long _startMillis
 Used for timeout measurement. More...
 
size_t _width = INT32_MAX
 Maximum operator >> read width. More...
 

Static Protected Attributes

static HardwareSerialinstances []
 Array of all created UART instances (just from this class, not UART structure). More...
 
static uint8_t availableUARTsCount
 Available UART instances count. More...
 

Additional Inherited Members

- Public Attributes inherited from Print
int base
 

Detailed Description

This class is for handling UART periphery.

See also
Stream
Print
HardwareSerial.h
SerialMode
SerialConfig
SSerialConfig
AdvSerialConfig
Examples
HarwareSerialExample.

Constructor & Destructor Documentation

◆ HardwareSerial()

HardwareSerial::HardwareSerial ( UART_HandleTypeDef &  uart)

Constructor.

◆ ~HardwareSerial()

HardwareSerial::~HardwareSerial ( )

Destructor.

Member Function Documentation

◆ connectDMAtoRx()

bool HardwareSerial::connectDMAtoRx ( DMA_HandleTypeDef &  DMA_handle)

Use this method to connect DMA channel to UART Rx programmatically.

Note
This method also switch from IT receiving mode to DMA receiving mode.
You don't have to connect DMA channel to UART Rx when you have already done it in .ioc file.
Parameters
DMA_handleDMA channel handle structure, you want to connect.
Returns
Returns true when connection was done successfully.

◆ connectDMAtoTx()

bool HardwareSerial::connectDMAtoTx ( DMA_HandleTypeDef &  DMA_handle)

Use this method to connect DMA channel to UART Tx programmatically.

Note
This method also switch from IT transmitting mode to DMA transmitting mode.
You don't have to connect DMA channel to UART Tx when you have already done it in .ioc file.
Parameters
DMA_handleDMA channel handle structure, you want to connect.
Returns
Returns true when connection was done successfully.

◆ disconnectDMAfromRx() [1/2]

bool HardwareSerial::disconnectDMAfromRx ( )
inline

Disconnects DMA channel from UART Rx.

Returns
Returns true when DMA was disconnected successfully.
Note
This method also switch from DMA receiving mode to IT receiving mode.

◆ disconnectDMAfromTx()

bool HardwareSerial::disconnectDMAfromTx ( )

Disconnects DMA channel from UART Tx.

Returns
Returns true when DMA was disconnected successfully.
Note
This method also switch from DMA transmitting mode to IT transmitting mode.

◆ isDMAconnectedToRx()

bool HardwareSerial::isDMAconnectedToRx ( void  )

Checks if DMA channel is connected to UART Rx and can be used for data receiving.

Returns
Return true if DMA channel is connected to UART Rx.

◆ isDMAconnectedToTx()

bool HardwareSerial::isDMAconnectedToTx ( void  )

Checks if DMA channel is connected to UART Tx and can be used for data transmitting.

Returns
Return true if DMA channel is connected to UART Tx.

◆ begin() [1/10]

bool HardwareSerial::begin ( )

Initialize and enable UART periphery and it's clock.

Returns
Returns true if initialization was successful.
Note
Call this method without parameters first time, to apply changes from .ioc file or call it when you want to re-initialize this periphery again (after calling end() method).

◆ begin() [2/10]

bool HardwareSerial::begin ( unsigned long  baud)

Initialize and enable UART periphery and it's clock.

Parameters
baudBaud rate in bits/sec.
Returns
Returns true if initialization was successful.

◆ begin() [3/10]

bool HardwareSerial::begin ( unsigned long  baud,
SerialMode  mode 
)

Initialize and enable UART periphery and it's clock.

Parameters
baudBaud rate in bits/sec.
modeSerial direction transfer mode. See SerialMode.
Returns
Returns true if initialization was successful.

◆ begin() [4/10]

bool HardwareSerial::begin ( SerialConfig  config)

Initialize and enable UART periphery and it's clock.

Parameters
configUART configuration structure. See SerialConfig.
Returns
Returns true if initialization was successful.

◆ begin() [5/10]

bool HardwareSerial::begin ( unsigned long  baud,
SerialConfig  config 
)

Initialize and enable UART periphery and it's clock.

Parameters
baudBaud rate in bits/sec.
configUART configuration structure. See SerialConfig.
Returns
Returns true if initialization was successful.

◆ begin() [6/10]

bool HardwareSerial::begin ( SerialConfig  config,
AdvSerialConfig  advConfig 
)

Initialize and enable UART periphery and it's clock.

Parameters
configUART configuration structure. See SerialConfig.
advConfigUART advanced configuration structure. See AdvSerialConfig.
Returns
Returns true if initialization was successful.
Note
Advanced serial configuration cannot be done on STM32F1xx MCUs.

◆ begin() [7/10]

bool HardwareSerial::begin ( unsigned long  baud,
AdvSerialConfig  advConfig 
)

Initialize and enable UART periphery and it's clock.

Parameters
baudBaud rate in bits/sec.
advConfigUART advanced configuration structure. See AdvSerialConfig.
Returns
Returns true if initialization was successful.
Note
Advanced serial configuration cannot be done on STM32F1xx MCUs.

◆ begin() [8/10]

bool HardwareSerial::begin ( unsigned long  baud,
SerialConfig  config,
AdvSerialConfig  advConfig 
)

Initialize and enable UART periphery and it's clock.

Parameters
baudBaud rate in bits/sec.
configUART configuration structure. See SerialConfig.
advConfigUART advanced configuration structure. See AdvSerialConfig.
Returns
Returns true if initialization was successful.
Note
Advanced serial configuration cannot be done on STM32F1xx MCUs.

◆ begin() [9/10]

bool HardwareSerial::begin ( unsigned long  baud,
SerialMode  mode,
AdvSerialConfig  advConfig 
)

Initialize and enable UART periphery and it's clock.

Parameters
baudBaud rate in bits/sec.
modeSerial direction transfer mode. See SerialMode.
advConfigUART advanced configuration structure. See AdvSerialConfig.
Returns
Returns true if initialization was successful.
Note
Advanced serial configuration cannot be done on STM32F1xx MCUs.

◆ begin() [10/10]

bool HardwareSerial::begin ( unsigned long  baud,
SerialConfig  config,
SerialMode  mode,
AdvSerialConfig  advConfig 
)

Initialize and enable UART periphery and it's clock.

Parameters
baudBaud rate in bits/sec.
configUART configuration structure. See SerialConfig.
modeSerial direction transfer mode. See SerialMode.
advConfigUART advanced configuration structure. See AdvSerialConfig.
Returns
Returns true if initialization was successful.
Note
Advanced serial configuration cannot be done on STM32F1xx MCUs.

◆ end()

void HardwareSerial::end ( )

Deinitialization and disabling UART periphery, it can be used for "sleep mode".

Note
To wake up from "sleep mode", call begin() without any parameter, to re-initialize old settings and to re-enable UART periphery.

◆ updateBaudRate()

bool HardwareSerial::updateBaudRate ( unsigned long  baud)

Updates baud rate at runtime.

This also works after calling begin() method.

Parameters
baudNew baud rate to set in bits/sec.
Returns
Returns true when baud rate was set successfully.

◆ getRxBufferSize()

size_t HardwareSerial::getRxBufferSize ( )
inline

Gets the size of Rx buffer.

Returns
Return the Rx buffer size.
Note
Try to not send more than HS_RX_BUF_SIZE value bytes count to this MCU, because Rx buffer can overflow before data reading. To send large data, try to split those data to multiple small packets. Then send one of those packets, wait from some form of acknowledgement from this MCU and then send next packet.

◆ available()

int HardwareSerial::available ( void  )
overridevirtual

Gets the count of bytes (characters) available for reading from the Rx buffer.

Returns
The number of bytes available to read.

Implements Stream.

◆ bufferOverflowed()

bool HardwareSerial::bufferOverflowed ( )

Returns true when Rx buffer has overflowed.

Returns
Returns true when Rx buffer has overflowed.

◆ peek()

int HardwareSerial::peek ( void  )
overridevirtual

Returns the next byte (character) of incoming serial data without removing it from the Rx buffer.

That is, successive calls to peek() will return the same character, as will the next call to read().

Returns
The first byte of incoming serial data available (or -1 if no data is available)

Implements Stream.

◆ read() [1/3]

int HardwareSerial::read ( void  )
overridevirtual

Reads incoming serial data.

It inherits from the Stream utility class.

Returns
The first byte of incoming serial data available (or -1 if no data is available).
Note
Try to not send more than HS_RX_BUF_SIZE value bytes count to this MCU, because Rx buffer can overflow before data reading. To send large data, try to split those data to multiple small packets. Then send one of those packets, wait from some form of acknowledgement from this MCU and then send next packet.

Implements Stream.

◆ read() [2/3]

size_t HardwareSerial::read ( char *  buffer,
size_t  size 
)

Copies the number of data from Rx buffer to your buffer.

Same as readBytes(), without timeout.

Returns
Number of successfully read data.
Parameters
bufferYour buffer where data from Rx buffer will be copied. Allowed data types: array of char or byte.
sizeThe number of bytes to read.
Note
The buffer size must be at least as large as the size parameter value.
If parameter size value is more than available data in Rx buffer, only available data will be read.
Try to not send more than HS_RX_BUF_SIZE value bytes count to this MCU, because Rx buffer can overflow before data reading. To send large data, try to split those data to multiple small packets. Then send one of those packets, wait from some form of acknowledgement from this MCU and then send next packet.

◆ read() [3/3]

size_t HardwareSerial::read ( uint8_t *  buffer,
size_t  size 
)
inline

Copies the number of data from Rx buffer to your buffer.

Same as readBytes(), without timeout.

Returns
Number of successfully read data.
Parameters
bufferYour buffer where data from Rx buffer will be copied. Allowed data types: array of char or byte.
sizeThe number of bytes to read.
Note
The buffer size must be at least as large as the size parameter value.
If parameter size value is more than available data in Rx buffer, only available data will be read.
Try to not send more than HS_RX_BUF_SIZE value bytes count to this MCU, because Rx buffer can overflow before data reading. To send large data, try to split those data to multiple small packets. Then send one of those packets, wait from some form of acknowledgement from this MCU and then send next packet.
Here is the call graph for this function:

◆ readBytes() [1/2]

size_t HardwareSerial::readBytes ( char *  buffer,
size_t  size 
)
overridevirtual

@Reads characters from the Rx buffer into your buffer.

The function terminates if the determined length has been read, or it times out (see setTimeout()).

Returns
Returns the number of characters placed in the buffer. A 0 means no valid data was found.
Parameters
bufferYour buffer where data from Rx buffer will be copied. Allowed data types: array of char or byte.
sizeThe number of bytes to read.
Note
The buffer size must be at least as large as the size parameter value.
Same as read(char* buffer, size_t size), but with timeout.
Try to not send more than HS_RX_BUF_SIZE value bytes count to this MCU, because Rx buffer can overflow before data reading. To send large data, try to split those data to multiple small packets. Then send one of those packets, wait from some form of acknowledgement from this MCU and then send next packet.

Reimplemented from Stream.

◆ readBytes() [2/2]

size_t HardwareSerial::readBytes ( uint8_t *  buffer,
size_t  size 
)
inlineoverridevirtual

@Reads characters from the Rx buffer into your buffer.

The function terminates if the determined length has been read, or it times out (see setTimeout()).

Returns
Returns the number of characters placed in the buffer. A 0 means no valid data was found.
Parameters
bufferYour buffer where data from Rx buffer will be copied. Allowed data types: array of char or byte.
sizeThe number of bytes to read.
Note
The buffer size must be at least as large as the size parameter value.
Same as read(char* buffer, size_t size), but with timeout.
Try to not send more than HS_RX_BUF_SIZE value bytes count to this MCU, because Rx buffer can overflow before data reading. To send large data, try to split those data to multiple small packets. Then send one of those packets, wait from some form of acknowledgement from this MCU and then send next packet.

Reimplemented from Stream.

Here is the call graph for this function:

◆ flush()

void HardwareSerial::flush ( uint32_t  timeout = 0)

Waits for the transmission of outgoing serial data to complete.

Waiting is canceled when timeout exceeds and transmission is ABORTED.

Parameters
timeoutWaiting timeout in ms, that starts counting when transmission began. If set to 0, no timeout is set.

◆ write() [1/6]

size_t HardwareSerial::write ( uint8_t  c)
overridevirtual

Writes binary data to the serial port.

This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.

Returns
Returns the number of bytes written.
Parameters
cOne character to write.
Note
This metod is blocking.

Implements Print.

◆ write() [2/6]

size_t HardwareSerial::write ( const uint8_t *  buffer,
size_t  size 
)
overridevirtual

Writes binary data to the serial port.

This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.

Returns
Returns the number of bytes written.
Parameters
bufferData buffer to write.
sizeThe number of bytes to write.
Note
This method is blocking.

Reimplemented from Print.

◆ writeNonBlocking() [1/2]

size_t HardwareSerial::writeNonBlocking ( const uint8_t *  buffer,
size_t  size 
)

Writes binary data to the serial port.

This data is sent as a byte or series of bytes, to send the characters representing the digits of a number use the print() function instead.

Returns
Returns the number of bytes written.
Parameters
bufferData buffer to write. Allowed data types: array of char or byte.
sizeThe number of bytes to write.
Note
This method isn´t blocking.

◆ writeNonBlocking() [2/2]

size_t HardwareSerial::writeNonBlocking ( const char *  buffer,
size_t  size 
)

Writes binary data to the serial port.

This data is sent as a byte or series of bytes, to send the characters representing the digits of a number use the print() function instead.

Returns
Returns the number of bytes written.
Parameters
bufferData buffer to write. Allowed data types: array of char or byte.
sizeThe number of bytes to write.
Note
This method isn´t blocking.

◆ isSending()

bool HardwareSerial::isSending ( )

Checks if data are sending right now and if periphery is busy.

Returns
True if data are sending right now.

◆ operator bool()

HardwareSerial::operator bool ( ) const
inline

Checks if UART instance exists.

◆ instanceExists()

bool HardwareSerial::instanceExists ( )
inline

Checks if UART instance exists.

Returns
Returns true if UART instance exists.

◆ isTxEnabled()

bool HardwareSerial::isTxEnabled ( void  )
inline

Checks if transmitting mode is enabled.

Returns
Returns true if transmitting mode is enabled.
See also
SerialMode

◆ isRxEnabled()

bool HardwareSerial::isRxEnabled ( void  )
inline

Checks if receiving mode is enabled.

Returns
Returns true if receiving mode is enabled.
See also
SerialMode

◆ getClockInstFrequency()

uint32_t HardwareSerial::getClockInstFrequency ( )

Gets UART periphery clock frequency.

Returns
Returns UART periphery clock frequency in Hz.

◆ baudRate()

int HardwareSerial::baudRate ( void  )
inline

Gets UART baud rate that was set in method begin() or updateBaudRate().

Returns
Returns baud rate in bits/sec.
Note
If autoBaudRate is enabled, this method will not return baud rate set by autoBaudRate.
Todo:
Try to read baud rate right from register, to get baud rate from autoBaudRate detection.

◆ hasError()

bool HardwareSerial::hasError ( void  )
inline

Checks if faulty byte was received.

Returns
Returns true if faulty byte was received.

◆ setWriteDoneCallback()

void HardwareSerial::setWriteDoneCallback ( void(*)()  WriteDoneCallback_)

Sets callback function, that will be called when all data using writeNonBlocking() method were send.

Parameters
WriteDoneCallback_Callback function.

◆ handleRxFullCallback() [1/2]

static void HardwareSerial::handleRxFullCallback ( USART_TypeDef *  instance)
inlinestatic

Handles RxFull callback function.

This method have to be included in HAL RxFull callback function.

Parameters
instanceUART instance which callback function will be called. (values: UASRT0, UART1, LPUART2, ...)

◆ handleRxFullCallback() [2/2]

static void HardwareSerial::handleRxFullCallback ( uint32_t  UARTIndex)
inlinestatic

Handles RxFull callback function.

This method have to be included in HAL RxFull callback function.

Note
This overload method is the fastest one.
Parameters
UARTIndexUART instance index which callback function will be called. (values: UASRT0_INDEX, UART1_INDEX, ...)

◆ handleTxDoneCallback() [1/2]

static void HardwareSerial::handleTxDoneCallback ( USART_TypeDef *  instance)
inlinestatic

Handles TxDone callback function.

This method have to be included in HAL TxDone callback function.

Parameters
instanceUART instance which callback function will be called. (values: UASRT0, UART1, LPUART2, ...)

◆ handleTxDoneCallback() [2/2]

static void HardwareSerial::handleTxDoneCallback ( uint32_t  UARTIndex)
inlinestatic

Handles TxDone callback function.

This method have to be included in HAL TxDone callback function.

Note
This overload method is the fastest one.
Parameters
UARTIndexUART instance index which callback function will be called. (values: UASRT0_INDEX, UART1_INDEX, ...)

◆ getUARTIndex()

static uint32_t HardwareSerial::getUARTIndex ( USART_TypeDef *  instance)
static

Gets UART instance index.

Parameters
instanceInstance of UART to get index of. (e.g. USART1, UART1, ...)
Returns
Returns index of UART instance that starts from 0. UART instances that are not available in this chip are skipped.

◆ getUARTInstances()

static HardwareSerial** HardwareSerial::getUARTInstances ( )
static

Gets array of all created UART instances (just from this class, not UART structure).

Returns
Returns array of all created UART instances (just from this class, not UART structure).

◆ getAvailableUARTsCount()

static uint8_t HardwareSerial::getAvailableUARTsCount ( )
static

Gets available UARTs instances count.

Returns
Returns available UARTs instances count.

◆ getHAL_UART_Struct()

UART_HandleTypeDef& HardwareSerial::getHAL_UART_Struct ( )
inline

Returns UART structure, that is used for handling UART.

Returns
Returns UART structure, that is used for handling UART

◆ UART_EndRxTransfer()

void HardwareSerial::UART_EndRxTransfer ( UART_HandleTypeDef *  huart)
protected

Aborts Rx IT Transfer.

◆ UART_EndTxTransfer()

void HardwareSerial::UART_EndTxTransfer ( UART_HandleTypeDef *  huart)
protected

Aborts Tx IT Transfer.

◆ abortDMARx()

void HardwareSerial::abortDMARx ( )
protected

Aborts Rx DMA Transfer.

◆ abortDMATx()

void HardwareSerial::abortDMATx ( )
protected

Aborts Tx DMA Transfer.

◆ disconnectDMAfromRx() [2/2]

bool HardwareSerial::disconnectDMAfromRx ( bool  startRxIT = true)
protected

Disconnects DMA channel from UART Rx.

Parameters
startRxITTrue to start IT receiving. (Defaut value is true).
Returns
Returns true when DMA was disconnected successfully.
Note
Try never to set startRxIT parameter value to false, because it will not start IT receiving mode and Rx channel will be in idle state. This parameter value is set to false only in end() method, to disable receiving.
This method also switch from DMA receiving mode to IT receiving mode, but only when startRxIT is true.

◆ UART_RxFullCallback()

void HardwareSerial::UART_RxFullCallback ( )
protected

This method is called when Rx Buffer is full.

◆ addThisToInstArr()

void HardwareSerial::addThisToInstArr ( )
protected

Writes new class instance to array of all timer instances.

We need to know all created instances to call period elapsed callback of right timer instance.

◆ removeThisFromInstArr()

void HardwareSerial::removeThisFromInstArr ( )
protected

Removes this instance from instances array.

◆ widthCheck()

size_t HardwareSerial::widthCheck ( size_t  width_)
overrideprotectedvirtual

Overriden width check function from Stream class.

Parameters
width_Width to check.
Returns
Returns corrected width value.

Reimplemented from Stream.

◆ write() [3/6]

virtual size_t Print::write

Writes one unsigned byte (character).

Parameters
byteByte you want to write.
Returns
Returns count of successfully written bytes.

◆ write() [4/6]

size_t Print::write
inline

Writes character array (string).

Parameters
strString you want to write.
Note
String you want to write have to ends with \0, else it can cause unexpected errors.
Returns
Returns count of successfully written bytes (characters).

◆ write() [5/6]

size_t Print::write

Writes byte array (string).

Parameters
bufferData you want to write.
sizeSize of buffer (length of data).
Returns
Returns count of successfully written bytes.

◆ write() [6/6]

size_t Print::write
inline

Writes character array (string).

Parameters
bufferData you want to write.
sizeSize of buffer (length of data).
Returns
Returns count of successfully written bytes (characters).

Member Data Documentation

◆ _uart

UART_HandleTypeDef* HardwareSerial::_uart
protected

UART periphery handle structure.

◆ rxBuf

uint8_t HardwareSerial::rxBuf[HS_RX_BUF_SIZE]
protected

DMA or IT receiving buffer.

◆ rxBufOVFCNT

volatile uint16_t HardwareSerial::rxBufOVFCNT = 0
protected

Rx buffer overflow count, restarts when read() was called.

◆ RxBufferReadPos

uint16_t HardwareSerial::RxBufferReadPos = 0
protected

Rading position in Rx Buffer.

◆ WriteDoneCallback

void(* HardwareSerial::WriteDoneCallback) () = NULL
protected

User callback function, that can be set using setWriteDoneCallback().

◆ instances

HardwareSerial* HardwareSerial::instances[]
staticprotected

Array of all created UART instances (just from this class, not UART structure).

◆ availableUARTsCount

uint8_t HardwareSerial::availableUARTsCount
staticprotected

Available UART instances count.


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