![]() |
Arduino Core for STM32
1.0
|
This class is for handling UART periphery. More...
#include <HardwareSerial.h>
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... | |
![]() | |
Stream () | |
Constructor. More... | |
Stream & | operator>> (char &c) |
This operator (>>) applied to an input stream is known as extraction operator, it can be used same as cin>> operator. More... | |
Stream & | operator>> (char *buffer) |
This operator (>>) applied to an input stream is known as extraction operator, it can be used same as cin>> operator. More... | |
Stream & | operator>> (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... | |
![]() | |
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 > | |
Print & | operator<< (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... | |
![]() | |
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... | |
![]() | |
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... | |
![]() | |
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 HardwareSerial * | instances [] |
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 | |
![]() | |
int | base |
This class is for handling UART periphery.
HardwareSerial::HardwareSerial | ( | UART_HandleTypeDef & | uart | ) |
Constructor.
HardwareSerial::~HardwareSerial | ( | ) |
Destructor.
bool HardwareSerial::connectDMAtoRx | ( | DMA_HandleTypeDef & | DMA_handle | ) |
Use this method to connect DMA channel to UART Rx programmatically.
DMA_handle | DMA channel handle structure, you want to connect. |
bool HardwareSerial::connectDMAtoTx | ( | DMA_HandleTypeDef & | DMA_handle | ) |
Use this method to connect DMA channel to UART Tx programmatically.
DMA_handle | DMA channel handle structure, you want to connect. |
|
inline |
Disconnects DMA channel from UART Rx.
bool HardwareSerial::disconnectDMAfromTx | ( | ) |
Disconnects DMA channel from UART Tx.
bool HardwareSerial::isDMAconnectedToRx | ( | void | ) |
Checks if DMA channel is connected to UART Rx and can be used for data receiving.
bool HardwareSerial::isDMAconnectedToTx | ( | void | ) |
Checks if DMA channel is connected to UART Tx and can be used for data transmitting.
bool HardwareSerial::begin | ( | ) |
Initialize and enable UART periphery and it's clock.
bool HardwareSerial::begin | ( | unsigned long | baud | ) |
Initialize and enable UART periphery and it's clock.
baud | Baud rate in bits/sec. |
bool HardwareSerial::begin | ( | unsigned long | baud, |
SerialMode | mode | ||
) |
Initialize and enable UART periphery and it's clock.
baud | Baud rate in bits/sec. |
mode | Serial direction transfer mode. See SerialMode. |
bool HardwareSerial::begin | ( | SerialConfig | config | ) |
Initialize and enable UART periphery and it's clock.
config | UART configuration structure. See SerialConfig. |
bool HardwareSerial::begin | ( | unsigned long | baud, |
SerialConfig | config | ||
) |
Initialize and enable UART periphery and it's clock.
baud | Baud rate in bits/sec. |
config | UART configuration structure. See SerialConfig. |
bool HardwareSerial::begin | ( | SerialConfig | config, |
AdvSerialConfig | advConfig | ||
) |
Initialize and enable UART periphery and it's clock.
config | UART configuration structure. See SerialConfig. |
advConfig | UART advanced configuration structure. See AdvSerialConfig. |
bool HardwareSerial::begin | ( | unsigned long | baud, |
AdvSerialConfig | advConfig | ||
) |
Initialize and enable UART periphery and it's clock.
baud | Baud rate in bits/sec. |
advConfig | UART advanced configuration structure. See AdvSerialConfig. |
bool HardwareSerial::begin | ( | unsigned long | baud, |
SerialConfig | config, | ||
AdvSerialConfig | advConfig | ||
) |
Initialize and enable UART periphery and it's clock.
baud | Baud rate in bits/sec. |
config | UART configuration structure. See SerialConfig. |
advConfig | UART advanced configuration structure. See AdvSerialConfig. |
bool HardwareSerial::begin | ( | unsigned long | baud, |
SerialMode | mode, | ||
AdvSerialConfig | advConfig | ||
) |
Initialize and enable UART periphery and it's clock.
baud | Baud rate in bits/sec. |
mode | Serial direction transfer mode. See SerialMode. |
advConfig | UART advanced configuration structure. See AdvSerialConfig. |
bool HardwareSerial::begin | ( | unsigned long | baud, |
SerialConfig | config, | ||
SerialMode | mode, | ||
AdvSerialConfig | advConfig | ||
) |
Initialize and enable UART periphery and it's clock.
baud | Baud rate in bits/sec. |
config | UART configuration structure. See SerialConfig. |
mode | Serial direction transfer mode. See SerialMode. |
advConfig | UART advanced configuration structure. See AdvSerialConfig. |
void HardwareSerial::end | ( | ) |
Deinitialization and disabling UART periphery, it can be used for "sleep mode".
bool HardwareSerial::updateBaudRate | ( | unsigned long | baud | ) |
Updates baud rate at runtime.
This also works after calling begin() method.
baud | New baud rate to set in bits/sec. |
|
inline |
Gets the size of Rx buffer.
|
overridevirtual |
Gets the count of bytes (characters) available for reading from the Rx buffer.
Implements Stream.
bool HardwareSerial::bufferOverflowed | ( | ) |
Returns true when Rx buffer has overflowed.
|
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().
Implements Stream.
|
overridevirtual |
Reads incoming serial data.
It inherits from the Stream utility class.
Implements Stream.
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.
buffer | Your buffer where data from Rx buffer will be copied. Allowed data types: array of char or byte. |
size | The number of bytes to read. |
|
inline |
Copies the number of data from Rx buffer to your buffer.
Same as readBytes(), without timeout.
buffer | Your buffer where data from Rx buffer will be copied. Allowed data types: array of char or byte. |
size | The number of bytes to read. |
|
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()).
buffer | Your buffer where data from Rx buffer will be copied. Allowed data types: array of char or byte. |
size | The number of bytes to read. |
Reimplemented from Stream.
|
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()).
buffer | Your buffer where data from Rx buffer will be copied. Allowed data types: array of char or byte. |
size | The number of bytes to read. |
Reimplemented from Stream.
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.
timeout | Waiting timeout in ms, that starts counting when transmission began. If set to 0, no timeout is set. |
|
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.
c | One character to write. |
Implements Print.
|
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.
buffer | Data buffer to write. |
size | The number of bytes to write. |
Reimplemented from Print.
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.
buffer | Data buffer to write. Allowed data types: array of char or byte. |
size | The number of bytes to write. |
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.
buffer | Data buffer to write. Allowed data types: array of char or byte. |
size | The number of bytes to write. |
bool HardwareSerial::isSending | ( | ) |
Checks if data are sending right now and if periphery is busy.
|
inline |
Checks if UART instance exists.
|
inline |
Checks if UART instance exists.
|
inline |
Checks if transmitting mode is enabled.
|
inline |
Checks if receiving mode is enabled.
uint32_t HardwareSerial::getClockInstFrequency | ( | ) |
Gets UART periphery clock frequency.
|
inline |
Gets UART baud rate that was set in method begin() or updateBaudRate().
|
inline |
Checks if faulty byte was received.
void HardwareSerial::setWriteDoneCallback | ( | void(*)() | WriteDoneCallback_ | ) |
Sets callback function, that will be called when all data using writeNonBlocking() method were send.
WriteDoneCallback_ | Callback function. |
|
inlinestatic |
Handles RxFull callback function.
This method have to be included in HAL RxFull callback function.
instance | UART instance which callback function will be called. (values: UASRT0, UART1, LPUART2, ...) |
|
inlinestatic |
Handles RxFull callback function.
This method have to be included in HAL RxFull callback function.
UARTIndex | UART instance index which callback function will be called. (values: UASRT0_INDEX, UART1_INDEX, ...) |
|
inlinestatic |
Handles TxDone callback function.
This method have to be included in HAL TxDone callback function.
instance | UART instance which callback function will be called. (values: UASRT0, UART1, LPUART2, ...) |
|
inlinestatic |
Handles TxDone callback function.
This method have to be included in HAL TxDone callback function.
UARTIndex | UART instance index which callback function will be called. (values: UASRT0_INDEX, UART1_INDEX, ...) |
|
static |
Gets UART instance index.
instance | Instance of UART to get index of. (e.g. USART1, UART1, ...) |
|
static |
Gets array of all created UART instances (just from this class, not UART structure).
|
static |
Gets available UARTs instances count.
|
inline |
Returns UART structure, that is used for handling UART.
|
protected |
Aborts Rx IT Transfer.
|
protected |
Aborts Tx IT Transfer.
|
protected |
Aborts Rx DMA Transfer.
|
protected |
Aborts Tx DMA Transfer.
|
protected |
Disconnects DMA channel from UART Rx.
startRxIT | True to start IT receiving. (Defaut value is true). |
|
protected |
This method is called when Rx Buffer is full.
|
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.
|
protected |
Removes this instance from instances array.
|
overrideprotectedvirtual |
virtual size_t Print::write |
Writes one unsigned byte (character).
byte | Byte you want to write. |
|
inline |
Writes character array (string).
str | String you want to write. |
size_t Print::write |
Writes byte array (string).
buffer | Data you want to write. |
size | Size of buffer (length of data). |
|
inline |
Writes character array (string).
buffer | Data you want to write. |
size | Size of buffer (length of data). |
|
protected |
UART periphery handle structure.
|
protected |
DMA or IT receiving buffer.
|
protected |
Rx buffer overflow count, restarts when read() was called.
|
protected |
Rading position in Rx Buffer.
|
protected |
User callback function, that can be set using setWriteDoneCallback().
|
staticprotected |
Array of all created UART instances (just from this class, not UART structure).
|
staticprotected |
Available UART instances count.