Hardware Serial
Class documentation: HardwareSerial
Main features:
- Supports UART, USART and LPUART periphery handling.
- Almost all configuration can be done programatically.
- You don't have to care about receiving, it will start receiving automatically into buffer after calling begin().
- Supports polling, interrupts or DMA communication modes.
- Formatted output functions can be used. See Print.
- Can be used as Arduino Serial library
- Note
- This library supports only asynchronous serial mode.
What not to do:
- Never use this library in any other mode than Asynchronous mode.
- Do not try to send or receive data before you have called begin() method.
- Do not try to create this instance for UART periphery, that does not exists. All methods will be disabled.
How to use:
- Check if libraries (in newer version only definitions) for your MCU are included in this file.
- In .ioc file settings set UART mode to "Asynchronous".
- In tab called "NVIC settings" enable all interrupts.
- In tab "Parameter Settings" set UART configuration, or set it by calling begin() method with SerialConfig / AdvSerialConfig parameter.
- (OPTIONAL) If you want to use DMA, in. ioc file add DMA channels for Rx Tx. Those channels may not be available for some peripheries or may not be available, because they are used for another periphery.
- Call begin() method in your code to initialize and enable UART periphery.
- Note
- This code is only c++ compatible. If you want to use it, convert your project to c++.
How to add support for your MCU:
- Check if your MCU is not supported yet.
- Add elif macro to include part (#elif defined(YOUR_MCU) ...). It will mean, that you have revisited this file for your MCU.
- Check if some functions or variables does not require specific code for your MCU.
- Check those functions/variables at first: begin, connectDMAtoRx, connectDMAtoTx, HS_UART_AbortReceive_IT, UART_EndRxTransfer, UART_EndTxTransfer, abortDMARx, abortDMATx, getClockInstFrequency, updateBaudRate, HAL_UART_RxCpltCallback, HAL_UART_TxCpltCallback
Communication modes:
1. DMA mode
- Can be used for receiving and transmitting.
- You can do another job, while receiving or transmitting large data.
- Methods, that can use DMA mode: all types of read methods (also >> operator) and writeNonBlocking() method.
- To use this mode, just connect available DMA channels to UART periphery Rx and Tx in .ioc file or using connectDMAtoRx() / connectDMAtoTx() methods.
- PROS:
- MCU can do another job while sending/receiving data without interrupting your code.
- CONS:
- Buffer, which data are transmitted using writeNonBlocking() method have to be available during transmission.
- DMA channels for selected UART periphery may not be available, so you cannot use DMA mode.
- You can call writeNonBlocking() method only when data are not transmitting, else it will return error.
2. IT mode
- Can be used for receiving and transmitting.
- You can do another job, while receiving or transmitting large data, but your program is interrupted when received or transmitted 1 byte.
- Methods, that can use IT mode: all types of read methods (also >> operator) and writeNonBlocking() method.
- To use this mode, make sure, you have disconnected DMA from UART periphery in .ioc file or you can call disconnectDMAfromRx() / disconnectDMAfromTx().
- PROS:
- MCU can do another job while sending/receiving data.
- CONS:
- Your code is interrupted every time, when 1 byte is received or transmitted.
- Buffer, which data are transmitted using writeNonBlocking() method have to be available during transmission.
- You can call writeNonBlocking() method only when data are not transmitting, else it will return error.
3. Polling (blocking) mode
- Can be used only for transmitting.
- Processor is sending your data manually. You cannot do any job while sending data.
- Methods, that can use polling mode: write, print and println methods (also << operator).
- To use this mode just call polling methods, that are mentioned above.
- PROS:
- Buffer, which data are transmitted will be certainly available during transmission.
- You do can send data several times in a row.
- CONS:
- MCU cannot do another job while sending/receiving data in this mode.
- When you are transmitting data using writeNonBlocking method, flush() method (which waits until all data are sent) will be called and then your new data will be sent in blocking mode.
Special hidden functions:
1. Sleep UART periphery
- You can easily sleep UART periphery by calling method end(), that disables periphery clock, DMA and it's interrupts.
- If you want to wake up UART periphery, just call begin() method with(out) parameters, that enables periphery clock, DMA and it's interrupts.
- Note
- When you wake up UART periphery using begin() method, you don't have to set configurations again, because old configurations are still saved in registers or in UART handle structure.
Troubleshooting:
1. I sent a lot of bytes to MCU, but it has received only 128 bytes.
- Check if you have not sent 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.
- If you really want, you can change Rx buffer size in HS_RX_BUF_SIZE macro.
2. I cannot receive or transmit any data.
- Check in .ioc file in your periphery settings, if it has set "Asynchronous" mode.
- Check if you have called begin() in your code before transmitting or receiving data.
- Check in .ioc file in your periphery settings in tab "NVIC Settings" if you have enabled all interrupts.
- Check if you have not swapped Rx an Tx pin or if you have connected those pin correctly.
- Check if your MCU is supported by this library.
Credits
- Author
- Matej Fitoš and Patrik Cepko
- Date
- Jan 29, 2021
- See also
- HardwareSerial
-
HardwareSerial.h
Examples:
HarwareSerialExample