Detailed Explanation of STM32 Serial Communication Basics

Communication Methods Between Devices

Generally, the communication methods between devices can be divided into two types: parallel communication and serial communication. The differences between parallel and serial communication are shown in the table below.

Detailed Explanation of STM32 Serial Communication Basics

Classification of Serial Communication

1. According to the data transmission direction, it can be divided into:

  • Simplex: Data transmission only supports data transfer in one direction;

  • Half-duplex: Allows data to be transmitted in both directions. However, at any one time, data can only be transmitted in one direction. It is essentially a switched direction simplex communication; it does not require independent receiving and sending ends, both can use a single port.

  • Full duplex: Allows data to be transmitted simultaneously in both directions. Therefore, full duplex communication is a combination of two simplex communication methods, requiring independent receiving and sending ends.

Detailed Explanation of STM32 Serial Communication Basics

2. According to the communication method, it can be divided into:

  • Synchronous communication: Transmits with a clock synchronization signal. For example: SPI, IIC communication interfaces.

  • Asynchronous communication: Does not carry a clock synchronization signal. For example: UART (Universal Asynchronous Receiver-Transmitter), single bus.

In synchronous communication, a signal line is used to transmit signals above the sending and receiving devices. Under the drive of the clock signal, both sides coordinate to synchronize data. For example, during communication, both sides usually agree to sample the data line at the rising or falling edge of the clock signal.

In asynchronous communication, clock signals are not used for data synchronization; instead, they directly intersperse some signal bits used for synchronization within the data signal, or package the main data to transmit in the format of data frames. Both sides also need to agree on the data transmission rate (i.e., baud rate) to better synchronize. Common baud rates include 4800bps, 9600bps, 115200bps, etc.

In synchronous communication, the content transmitted by the data signal is mostly valid data, while asynchronous communication may contain various identifiers of the data frame. Therefore, synchronous communication is more efficient, but the clock error allowed between both sides of synchronous communication is small; a slight clock error may lead to data confusion, while asynchronous communication allows for larger clock errors.

Common Serial Communication Interfaces

Detailed Explanation of STM32 Serial Communication Basics

Basics of STM32 Serial Communication

STM32 has two types of serial communication interfaces: UART (Universal Asynchronous Receiver-Transmitter) and USART (Universal Synchronous Asynchronous Receiver-Transmitter). For the high-capacity STM32F10x series chips, there are 3 USARTs and 2 UARTs.

UART Pin Connection Method

  • RXD: Data input pin, data reception;

  • TXD: Data output pin, data transmission.

Detailed Explanation of STM32 Serial Communication Basics

For the connection between two chips, the GND of the two chips should be common, and TXD and RXD should be crossed connected. The meaning of crossed connection here is that the RxD of chip 1 is connected to the TXD of chip 2, and the RXD of chip 2 is connected to the TXD of chip 1. In this way, TTL level communication can be conducted between the two chips. For examples of STM32 serial communication with the 51 microcontroller, please visit here: STM32 and 51 Microcontroller Serial Communication Example.

Detailed Explanation of STM32 Serial Communication Basics

If the chip is connected to a PC (or host), in addition to sharing the ground, it cannot be directly crossed connected. Although both the PC and the chip have TXD and RXD pins, the PC (or host) usually uses an RS232 interface (usually in DB9 packaging), so direct crossed connection is not possible. The RS232 interface has 9 pins (or pins), and TxD and RxD are usually obtained through level conversion. Therefore, to allow direct communication between the chip and the RS232 interface of the PC, the chip’s input and output ports must also be level converted to RS232 type before crossing connection.

After level conversion, the voltage standards of the chip’s serial port and RS232 are different:

  • Microcontroller voltage standard (TTL level): +5V represents 1, 0V represents 0;

  • RS232 voltage standard: +15/+13 V represents 0, -15/-13 represents 1.

The communication structure diagram for devices using the RS-232 communication protocol is as follows:

Detailed Explanation of STM32 Serial Communication Basics

Therefore, the connection between the microcontroller’s serial port and the PC’s serial port should follow the connection method below: between the microcontroller’s serial port and the RS232 port provided by the host, a level conversion circuit (such as the Max232 chip shown in the diagram below) is used to achieve conversion between TTL level and RS232 level. For examples of communication between STM32 and PC, please visit here: STM32 Example – Sending Data via Serial Port Controlled by Buttons, with code attached at the end.

Detailed Explanation of STM32 Serial Communication Basics

Introduction to RS232 Serial Port

The 9-pin interface on the back of desktop computers is the COM port (serial port) widely used in industrial control and data acquisition. The rightmost interface in the above image is the serial port interface commonly referred to as the RS232 interface, which is a common DB9 packaging.

Detailed Explanation of STM32 Serial Communication Basics

During the communication process, only two pins participate in communication.

  • Pin 2: Computer’s input RXD

  • Pin 3: Computer’s output TXD. Through pins 2 and 3, full duplex (simultaneous send and receive) asynchronous serial communication can be achieved.

  • Pin 5: Ground

The P3 port of the microcontroller has two multiplexed interfaces RXD and TXD. This is the transceiver port for serial communication of the microcontroller, and the connection should correspondingly connect to the computer’s TDX RDX. Note: The voltage standards between the microcontroller and RS232 are different.

The voltage standard of the microcontroller is TTL level: +5V represents 1, 0V represents 0.

The voltage standard of RS232 is +15/+13 V represents 1, -15/-13 represents 0.

Therefore, the communication between the microcontroller and the computer’s serial port should follow the connection method below:

Between the microcontroller and the RS232 port provided by the host, a level conversion circuit (the Max232 chip shown in the diagram above) is used to achieve conversion between TTL level and RS232 level. The connection method between the PC’s serial port and the microcontroller’s serial port is shown in the diagram:

Detailed Explanation of STM32 Serial Communication Basics

Note these two DB9: DB91 is on the computer, and DB92 is soldered on the microcontroller experimental board.

The meaning of the crossed connection here is that RXD of DB91 is connected to TXD of DB92.

RXD of DB92 is connected to TXD of DB91, thus crossed connected. If the computer does not have an RS232 port but only a USB port, a serial port conversion cable can be used to convert to a serial port, as shown in the diagram below.

Detailed Explanation of STM32 Serial Communication Basics

At this point, the host computer needs to install the serial port driver program.

Note that this driver program drives the PL2303 chip (inside the large head in the above diagram) to convert RS232 information into USB information.

The diagram below shows the internal structure of the above diagram:

Detailed Explanation of STM32 Serial Communication Basics

Using serial communication is simpler than USB because serial communication does not have a protocol and is easy to use.

Features of UART in STM32

  • Full duplex asynchronous communication;

  • Fractional baud rate generator system, providing accurate baud rates. The same programmable baud rate is used for sending and receiving, up to 4.5Mbits/s;

  • Programmable data word length (8 bits or 9 bits);

  • Configurable stop bits (supports 1 or 2 stop bits);

  • Configurable DMA multi-buffer communication;

  • Separate transmitter and receiver enable bits;

  • Detection flags:

  • ① Receive buffer

  • ② Transmit buffer empty

  • ③ Transmission end flag;

  • Multiple interrupt sources with flags, triggering interrupts;

  • Others: Parity control, four error detection flags.

Serial Communication Process

Detailed Explanation of STM32 Serial Communication Basics

UART Parameters in STM32

The data packet for serial communication is transmitted from the sending device through its TXD interface to the RXD interface of the receiving device. The packet format of both parties must be consistent to properly send and receive data.

Parameters that need to be defined for asynchronous serial communication in STM32 include: start bit, data bits (8 bits or 9 bits), parity bit (9th bit), stop bits (1, 1.5, 2 bits), baud rate settings.

The data packet for UART serial communication is structured in frames, commonly structured as: 1 start bit + 8 data bits + 1 parity bit (optional) + 1 stop bit. As shown in the diagram below:

Detailed Explanation of STM32 Serial Communication Basics

The parity bit can be odd parity or even parity, which is a simple method for error checking. Odd parity means that the total number of 1s in the 9 bits (including data bits and parity bits) must be odd; even parity means that the total number of 1s must be even.

Besides odd parity (odd) and even parity (even), there are also: space parity (0), mark parity (1), and no parity (noparity). 0/1 parity: Regardless of the content of the valid data, the parity bit is always 0 or 1.

UART (USART) Block Diagram

Detailed Explanation of STM32 Serial Communication Basics

This block diagram is divided into upper, middle, and lower parts. This article briefly describes the content of each part; for specifics, please refer to the “STM32 Chinese Reference Manual”.

The upper part of the block diagram shows that data enters the receive shift register from RX, then enters the receive data register, and is ultimately read by the CPU or DMA; data from the CPU or DMA is passed in, enters the transmit data register, then the transmit shift register, and is finally sent out through TX.

However, both sending and receiving in UART require baud rate control. How is the baud rate controlled?

This leads us to the lower part of the block diagram, where both the receive shift register and transmit shift register have an incoming arrow, connecting to the receiver control and transmitter control, respectively. This means that although asynchronous communication does not have clock synchronization signals, clock signals are provided internally in the serial port for control. The receiver clock and transmitter clock are controlled by the same control unit, meaning they share the same baud rate generator. We can also see the calculation method of the receiver clock (generator clock) and the calculation method of USRRTDIV.

Detailed Explanation of STM32 Serial Communication BasicsDisclaimer: This article is reproduced from “the internet”, and the copyright belongs to the author. If there is any infringement, please contact us for removal!
「If useful, please share」

👇Click Follow, Technical Content will be delivered on time! 👇

Detailed Explanation of STM32 Serial Communication Basics
  • Very detailed basic knowledge of radio frequency

  • Learn a few tips on inductor saturation, have you learned them?

  • Principle of lithium battery protection circuit

  • Purely handmade a 16-bit RISC architecture CPU

  • Differences between STM32, GD32, and ESP32

  • Surge

  • What is the connection between Fourier transform, Laplace transform, and Z transform?

  • Those once popular microcontrollers~

  • A Bluetooth chip costing less than 2 yuan, can you imagine?

Leave a Comment