Follow+Star Public Account, don’t miss wonderful content
Source | AI Electronics
Today I will share with you some features of UART in STM32, mainly targeting newer series of STM32 (such as: STM32H7, G0, G4, etc.), which allows changes to the Rx and Tx pins, level inversion, high-low order, and introduces timeout, etc.
Support for RXD and TXD Pin Swapping
Sometimes, when we connect an RS232 chip, we might accidentally reverse the RXD and TXD lines. Such low-level mistakes are usually made only by experienced users.
.
If you know that the TXD and RXD pins of USART can be swapped, then when connecting external RS232 chips, if an error occurs, you do not need to modify the hardware; you can simply reverse the RXD and TXD pins directly in the software to correct the error.


Refer to the above image, set the SWAP bit to swap the RXD and TXD pins.
Support for Inversion of Level Polarity for Receiving and Sending
The second new feature:Tx and Rx level polarity inversion.
Typically, the default serial port level is high level for logic 1 and low level for logic 0. However, in the new features of STM32’s USART, it is possible to set high level to logic 0 and low level to logic 1.
This feature allows us to use it flexibly in some special scenarios, such as when USART is connected to an inverter.
In the data receive and transmit register, the data can also be inverted, where the original 0 becomes 1, and the original 1 becomes 0, which is similar to level polarity inversion. It is important to note that during polarity inversion, both the start and stop bits are also inverted, and all signal levels are inverted; whereas in the data register, only the data bits are inverted, which also includes the parity bit, without inverting the polarity of the start and stop bits of the signal.
Refer to the above image, in the USART_CR2 register, use TXINV, RXINV, and DATAINV bits for configuration.
Support for Sending or Receiving Data in High and Low Bit Order
The third new feature:When sending and receiving data, it can be configured in the register to send low bits first or high bits first.
The default configuration is to send or receive the low bit signal (bit 0) first. In practical applications, by setting MSBFIRST in USART_CR2, it is also possible to have USART send or receive the high bit signal (bit 7/8) first, which is often used when reversing data.
We can consider this situation: if we need to reverse the data received from the serial port before performing calculations, then this hardware function can help us save a lot of software workload.
USART Supports DMA Transmission
Finally,STM32’s USART supports DMA, which is beneficial for continuous data transmission and buffered reception.In certain STM32 product series, such as STM32F4 and H7 series, there are dedicated serial port FIFOs, which can be used for serial port wake-up, allowing the MCU to enter Sleep mode. When a complete data packet is received, which may be 100 bytes or 200 bytes, the microcontroller can be awakened, thus preventing data loss while reducing the MCU’s wake-up frequency.
Additionally, USART reception also has a timeout function, which allows a time to be set manually in the USART RTOR register. When the time between received data frames exceeds this set value, a timeout interrupt will be triggered, and there will be a flag called RTOF in the serial port to indicate the occurrence of a timeout event. Through the code in this interrupt, we can know that the serial port data has been interrupted, which can be considered that the other party’s data has been fully sent. This method has many application scenarios in the development of upper-layer protocols for serial ports.

Summary of USART New Features

-
Support RXD and TXD pin swapping
-
Support for level logic inversion on pins and logic inversion in data registers
-
Support for sending or receiving data in high and low bit order, allowing either low or high bits to be sent or received first
-
Support for timeout function in receiving data, and each USART can support DMA transmission
Reply in the background with ‘STM32’ or ‘Basic Knowledge of Embedded Software and Hardware’ to read more related articles.
Welcome to follow my public account, reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Welcome to follow my video account:

Click “Read the Original” for more shares, welcome to share, collect, like, and view.