 |
Arduino Core for STM32
1.0
|
Go to the documentation of this file.
19 #ifndef HardwareSerial_h
20 #define HardwareSerial_h
32 #if defined(STM32F103xB)
35 #elif defined(STM32F031x6) || defined(STM32F070xB) || defined(STM32F070x6) || defined(STM32F030x6)
38 #elif defined(STM32L031xx)
41 #elif defined(STM32L432xx)
45 #error "This library does not supports selected MCU." //This is here only, because some methods may not works with that chip.
48 #if defined(HAL_UART_MODULE_ENABLED) || defined(DOXYGEN_FORCED)
53 #define HS_RX_BUF_SIZE 128
56 #ifndef IS_LPUART_INSTANCE
57 #define IS_LPUART_INSTANCE(instance) (true)
81 #ifdef __STM32F1xx_HAL_USART_H //code for STM32F1xx
86 typedef USART_InitTypeDef SSerialConfig;
89 #if !defined(__STM32F1xx_HAL_UART_H) && !defined(__STM32F1xx_HAL_USART_H) //code not for STM32F1xx
118 #ifdef __STM32F1xx_HAL_USART_H //code for STM32F1xx
187 bool begin(
unsigned long baud);
197 #if defined(__STM32F1xx_HAL_USART_H) //code for STM32F1xx
204 bool begin(SSerialConfig config);
213 bool begin(
unsigned long baud, SSerialConfig config);
231 #if !defined(__STM32F1xx_HAL_UART_H) && !defined(__STM32F1xx_HAL_USART_H) //code not for STM32F1xx
327 int peek(
void)
override;
336 int read(
void)
override;
349 size_t read(
char* buffer,
size_t size);
362 size_t read(uint8_t* buffer,
size_t size)
364 return read((
char*)buffer, size);
378 size_t readBytes(
char* buffer,
size_t size)
override;
400 void flush(uint32_t timeout = 0);
408 size_t write(uint8_t c)
override;
417 size_t write(
const uint8_t *buffer,
size_t size)
override;
450 operator bool()
const
452 #ifdef __STM32F1xx_HAL_USART_H //code for STM32F1xx
454 return (_usart != NULL) && ((IS_UART_INSTANCE(_usart->Instance)) || (
IS_LPUART_INSTANCE(_usart->Instance)));
469 #ifdef __STM32F1xx_HAL_USART_H //code for STM32F1xx
471 return (_usart != NULL) && ((IS_UART_INSTANCE(_usart->Instance)) || (
IS_LPUART_INSTANCE(_usart->Instance)));
488 #ifdef __STM32F1xx_HAL_USART_H //code for STM32F1xx
508 #ifdef __STM32F1xx_HAL_USART_H //code for STM32F1xx
534 #ifdef __STM32F1xx_HAL_USART_H //code for STM32F1xx
536 return _usart->Init.BaudRate;
539 return _uart->Init.BaudRate;
542 return _uart->Init.BaudRate;
552 #ifdef __STM32F1xx_HAL_USART_H //code for STM32F1xx
554 return _usart->ErrorCode != HAL_USART_ERROR_NONE;
557 return _uart->ErrorCode != HAL_UART_ERROR_NONE;
560 return _uart->ErrorCode != HAL_UART_ERROR_NONE;
626 #ifdef __STM32F1xx_HAL_USART_H //code for STM32F1xx
632 USART_HandleTypeDef& getHAL_USART_Struct(){
644 #ifdef __STM32F1xx_HAL_USART_H //code for STM32F1xx
649 USART_HandleTypeDef* _usart;
677 #ifdef __STM32F1xx_HAL_USART_H //code for STM32F1xx
765 #ifdef __STM32F1xx_HAL_USART_H //code for STM32F1xx
HardwareSerial(UART_HandleTypeDef &uart)
Constructor.
bool isTxEnabled(void)
Checks if transmitting mode is enabled.
Definition: HardwareSerial.h:486
int available(void) override
Gets the count of bytes (characters) available for reading from the Rx buffer.
UART_InitTypeDef SerialConfig
HAL library configuration structure.
Definition: HardwareSerial.h:79
static void handleRxFullCallback(USART_TypeDef *instance)
Handles RxFull callback function.
volatile uint16_t rxBufOVFCNT
Rx buffer overflow count, restarts when read() was called.
Definition: HardwareSerial.h:669
void UART_RxFullCallback()
This method is called when Rx Buffer is full.
bool begin()
Initialize and enable UART periphery and it's clock.
bool bufferOverflowed()
Returns true when Rx buffer has overflowed.
void flush(uint32_t timeout=0)
Waits for the transmission of outgoing serial data to complete.
size_t widthCheck(size_t width_) override
Overriden width check function from Stream class.
bool updateBaudRate(unsigned long baud)
Updates baud rate at runtime.
bool isDMAconnectedToRx(void)
Checks if DMA channel is connected to UART Rx and can be used for data receiving.
void UART_EndRxTransfer(UART_HandleTypeDef *huart)
Aborts Rx IT Transfer.
size_t readBytes(uint8_t *buffer, size_t size) override
@Reads characters from the Rx buffer into your buffer.
Definition: HardwareSerial.h:391
uint16_t RxBufferReadPos
Rading position in Rx Buffer.
Definition: HardwareSerial.h:675
#define IS_LPUART_INSTANCE(instance)
Definition: HardwareSerial.h:57
@ SERIAL_FULL
Transmitting and receiving mode
Definition: HardwareSerial.h:69
bool connectDMAtoTx(DMA_HandleTypeDef &DMA_handle)
Use this method to connect DMA channel to UART Tx programmatically.
void(* WriteDoneCallback)()
User callback function, that can be set using setWriteDoneCallback().
Definition: HardwareSerial.h:723
UART_AdvFeatureInitTypeDef AdvSerialConfig
HAL library advanced configuration structure.
Definition: HardwareSerial.h:94
void setWriteDoneCallback(void(*WriteDoneCallback_)())
Sets callback function, that will be called when all data using writeNonBlocking() method were send.
static uint32_t getUARTIndex(USART_TypeDef *instance)
Gets UART instance index.
bool isSending()
Checks if data are sending right now and if periphery is busy.
bool instanceExists()
Checks if UART instance exists.
Definition: HardwareSerial.h:468
static uint8_t availableUARTsCount
Available UART instances count.
Definition: HardwareSerial.h:750
bool disconnectDMAfromTx()
Disconnects DMA channel from UART Tx.
uint8_t rxBuf[HS_RX_BUF_SIZE]
DMA or IT receiving buffer.
Definition: HardwareSerial.h:663
void HS_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
Copied from HAL UART library.
void addThisToInstArr()
Writes new class instance to array of all timer instances.
int read(void) override
Reads incoming serial data.
void abortDMARx()
Aborts Rx DMA Transfer.
#define HS_RX_BUF_SIZE
Rx buffer size.
Definition: HardwareSerial.h:53
This class is for receiving and transmitting data.
Definition: Stream.h:56
SerialMode
Enumeration of 3 UART direction modes.
Definition: HardwareSerial.h:67
void end()
Deinitialization and disabling UART periphery, it can be used for "sleep mode".
size_t getRxBufferSize()
Gets the size of Rx buffer.
Definition: HardwareSerial.h:305
@ SERIAL_RX_ONLY
Only receiving mode
Definition: HardwareSerial.h:71
bool hasError(void)
Checks if faulty byte was received.
Definition: HardwareSerial.h:550
uint32_t getClockInstFrequency()
Gets UART periphery clock frequency.
bool connectDMAtoRx(DMA_HandleTypeDef &DMA_handle)
Use this method to connect DMA channel to UART Rx programmatically.
bool disconnectDMAfromRx()
Disconnects DMA channel from UART Rx.
Definition: HardwareSerial.h:152
This file contains basic (Arduino like) GPIO manipulation functions, delay, random,...
static void handleTxDoneCallback(USART_TypeDef *instance)
Handles TxDone callback function.
static HardwareSerial ** getUARTInstances()
Gets array of all created UART instances (just from this class, not UART structure).
~HardwareSerial()
Destructor.
int peek(void) override
Returns the next byte (character) of incoming serial data without removing it from the Rx buffer.
bool isDMAconnectedToTx(void)
Checks if DMA channel is connected to UART Tx and can be used for data transmitting.
void removeThisFromInstArr()
Removes this instance from instances array.
bool isRxEnabled(void)
Checks if receiving mode is enabled.
Definition: HardwareSerial.h:506
UART_HandleTypeDef * _uart
UART periphery handle structure.
Definition: HardwareSerial.h:642
void UART_EndTxTransfer(UART_HandleTypeDef *huart)
Aborts Tx IT Transfer.
int baudRate(void)
Gets UART baud rate that was set in method begin() or updateBaudRate().
Definition: HardwareSerial.h:532
UART_HandleTypeDef & getHAL_UART_Struct()
Returns UART structure, that is used for handling UART.
Definition: HardwareSerial.h:622
size_t writeNonBlocking(const uint8_t *buffer, size_t size)
Writes binary data to the serial port.
static uint8_t getAvailableUARTsCount()
Gets available UARTs instances count.
void abortDMATx()
Aborts Tx DMA Transfer.
size_t read(uint8_t *buffer, size_t size)
Copies the number of data from Rx buffer to your buffer.
Definition: HardwareSerial.h:362
This class is for handling UART periphery.
Definition: HardwareSerial.h:109
size_t readBytes(char *buffer, size_t size) override
@Reads characters from the Rx buffer into your buffer.
@ SERIAL_TX_ONLY
Only transmitting mode
Definition: HardwareSerial.h:73
size_t write(uint8_t c) override
Writes binary data to the serial port.
static HardwareSerial * instances[]
Array of all created UART instances (just from this class, not UART structure).
Definition: HardwareSerial.h:745
Base class for character-based streams.