STM32's USART analysis

Basic knowledge of bus and communication

Before introducing the USART, we need to understand some basic knowledge of information transmission, and most of the information transmission of the microcontroller is carried out by the bus.

bus definition

The bus refers to the public channel through which the microcomputer system and the intelligent instrument have already transmitted information to each other, and is the actual interconnection line between the internal modules of the chip, between devices, and between systems. For example, to enable the bus to exchange information effectively and reliably, a series of regulations must be made on bus signals, transmission rules, and physical media for transmission. These regulations are called bus protocols or bus rules.
The bus can be divided into the following three categories according to its scope of use or connection objects:

Chip bus : the channel connecting each module in the chip, used for information transmission between modules. The on-chip bus of microcontrollers mostly adopts a parallel single-bus structure (time-division multiplexing)
system bus (also called internal bus) : a channel for transmitting information between modules and devices inside a microcontroller system or intelligent instrument. The system bus is divided into parallel system bus (such as PCI bus, VXI bus) and serial system bus (such as I2C, SPI, 1-wire).
Communication Bus (also called External Bus) : A channel for transferring information between two or more systems. The communication bus is divided into parallel communication bus (such as IEEE488 bus) and serial communication bus (such as RS232, RS485 bus).

The external bus can be subdivided into two communication buses:

Parallel communication : every bit of the data byte is transmitted or received at the same time.
Features: The transmission speed is fast. When the transmission distance is long and the number of bits is large, the cost will be increased and the communication success rate will be reduced.
In parallel communication, each bit of data is transmitted at the same time, which means that as many bits of data need as many transmission lines, but multiple bits can be transmitted at the same time to achieve fast data transmission.

Serial communication : each bit of the data byte is transmitted or received bit by bit in sequence
Features: Only 2-3 transmission lines are needed, the line is simple and the cost is low, especially suitable for long-distance communication, but the transmission speed is slower than parallel communication.
Serial communication is to send data bit by bit in byte order. Compared with parallel communication, serial communication requires much fewer transmission lines, but the transmission speed will also be slower. Due to the high cost of parallel communication and the low communication success rate during long-distance transmission, only a small part of the communication method used in practical applications is parallel communication, and most of them use serial communication.

Asynchronous and synchronous communication

The serial communication type can be divided into asynchronous communication and synchronous communication according to the different transmission methods of data.

asynchronous communication

In asynchronous communication, data or characters are sent frame by frame. A data frame is defined as a character-complete communication format, also known as a "format frame", usually expressed in binary.

Data Format

The complete communication format of a character is a data frame, which consists of start bit, data bit, parity check bit and stop bit.
insert image description here
Each part of the data frame:
start bit : a logic "0" signal, occupying one bit, used to notify the receiving device that a new character has started.
Data bit : followed by the start bit is the lowest bit (LSB) of 5-8 bits of data. Asynchronous transmission stipulates that the low bit comes first and the high bit comes after.
Parity bit : Immediately after the highest bit (MSB) of the data, one bit is occupied. When parity is checked, it is set to "1" or "0" according to the protocol (set as required).
Stop bit : It is a logic "1" signal, which can occupy 1-2 bits. When the receiving end receives the stop bit, it indicates the end of a frame of data. At the same time, it is ready to receive the next frame of data, as long as another logic "0" is received, it is the beginning of a new character.

Asynchronous communication features:
1. The transmission is carried out in units of characters (data frames), and the time interval between frames is arbitrary, that is, the frames are asynchronous, and the correct sending and receiving of both parties is controlled by the start bit.
2. Each bit in each data frame is transmitted at a fixed time, so the two sides of the communication must use the same baud rate to realize the synchronization of data frame sending and receiving.
3. Since the data frame of asynchronous communication has a fixed format, the two sides of the communication only need to send and receive data according to the agreed frame format, so its hardware structure is simpler than that of synchronous communication.
4. Since the start bit and stop bit for synchronous transmission and reception are inserted in the data frame, its communication efficiency is lower than that of synchronous communication.
5. A check bit can be added to the data frame of asynchronous communication to detect errors in data transmission, which is widely used.

baud rate

The key reason why asynchronous communication is called asynchronous communication is that the two parties in the communication do not share the same clock signal. When the communication party A wants to transmit a data to the communication party B, if the clock frequency of the party A and the party B is different, then A transmits the data at the clock frequency of A according to the data frame format, and if the receiver B receives the data according to its own clock frequency, data misalignment or bit leakage may occur. This requires A and B to agree on the same frequency to send and receive data, which is the baud rate.
The baud rate is an important indicator of asynchronous communication, reflecting the rate of data transmission.
Baud rate definition: the number of bits of binary data transmitted per unit of time. It is usually expressed by the number of bits transmitted per second, which is b/ps or bps (bit per second). The higher the baud rate, the faster the transfer speed.
Commonly used baud rates: 1200, 1800, 2400, 4800, 9600, 115200

synchronous communication

Synchronous communication is a communication method that transmits data continuously and serially. The sending end and the receiving end need a synchronous clock signal to control the synchronization of the two sides of the communication. The synchronization signal is generated by the host that initiates the communication.
The so-called synchronization means that the two sides of the communication need to transmit information at the same clock signal frequency. Synchronous communication requires at least 3 lines: data line SDA, synchronous clock SCL, and common ground. The data format of synchronous communication is shown in the figure, and its characteristic is to communicate in the unit of "data series". A data series includes the following three parts:

1. Synchronization character: Indicates the start of data transmission to achieve synchronization between the sending end and the receiving end.
2. Data block: the data content to be communicated. After the sync character ends, send and receive continuously and sequentially.
3. Check character: In order to check the correctness of communication data and improve the reliability of data transmission, after the data block is sent, it is usually necessary to send the check code of the data block according to the agreement. The verification method and the length of the verification code are carried out according to the agreed communication protocol.
insert image description here
The serial system bus (internal bus) usually adopts synchronous communication methods, such as IIC, SPI, USB.

Data transmission method of serial communication

Serial communication is a commonly used communication method. According to the direction of data transmission, serial communication can be divided into three transmission methods: simplex, half-duplex and full-duplex. 1. Simplex transmission
is
a kind of data transmission in only one direction In the communication mode, machine A can only be used as a transmitter, and machine B can only be used as a receiver, and data is transmitted from machine A to machine B. Data transmission can be realized with only one transmission line during communication.
insert image description here
2. Half-duplex transmission mode
Data can be transmitted in two directions, but at the same time, only one can send and the other can receive. Only one transmission line is used to alternately transmit data in both directions. Data can be sent in both directions, but not simultaneously.
insert image description here
3. Full-duplex transmission mode
There are 2 data lines between machine A and machine B. Both machine A and machine B have independent receiver and generator hardware modules, so both parties can send and receive at the same time at any time.
insert image description here
Among these three transmission methods, simplex transmission can be understood as listening to the radio, and information can only be heard from the radio, but cannot be transmitted to the radio; half-duplex transmission can be understood as a walkie-talkie conversation, when one party is talking When the speaking party is in the busy state, the other party can only listen but not speak, and the speaking party can only talk but not listen. Only when the speaking party stops and the busy state is released can it speak; full-duplex transmission can It is understood as a phone call. Both parties in the call can speak or listen to information at any time without being restricted by the other party.

USE

USART is a full-duplex communication method. STM32 has 5 serial communication interfaces: 3 universal synchronous/asynchronous transceivers (USART1, USART2, USART3) and two universal asynchronous transceivers (UART4, UART5). The number of chips of different models is different. USART1 is connected to the high-speed APB2 bus with a running frequency of 72MHz (supporting a transmission frequency of up to 4.5Mbps); the other is located on the APB1 bus with a running frequency of 36MHz (2.25Mbps transmission frequency).

USART interface pins

Two-way communication of USART requires at least two pins: RX (Recerive Data, receiving data) input pin and TX (Transmit Data) output pin. When two devices communicate, it is necessary to connect the TX of device 1 to the RX of device 2, and connect the TX to the RX of device 2. insert image description here
In synchronous communication mode, the data synchronization clock needs to be sent out through the clock SCLK output pin. In order to support hardware flow control, it is also necessary to request to send RST and clear the CTS pin.
However, the pins of the UASRT interface are shared with GPIOs on the STM32 microcontroller. In other words, the USART pin uses the AFIO function pin. If the GPIO still wants to use this I/O pin, you can use the pin of the remapping function.

Serial asynchronous communication character format

In serial communication, data, control and status information are all transmitted using the same signal line, so the sending and receiving parties must abide by the common communication protocol in order to solve the problems of transmission rate, information format, bit synchronization, character format, data verification, etc. Serial asynchronous communication is transmitted in units of characters, and its communication protocol is a start-stop asynchronous communication protocol.
insert image description here
After the transmission of a character is completed, the next character can be transmitted, or the next character can be transmitted after being idle for a period of time. Idle bits are at a logic 1 level. The "bit" in the character format means binary, and the duration of each bit is the same, which is the reciprocal of the data transmission rate. The data transfer rate is also known as the bit rate, which is the number of binary bits transferred per second. **Bit rate equals baud rate when binary digits are transmitted and each bit is of equal length. **Baud rate means the data modulation rate, and its unit is baud.

USART structure

The figure below is a block diagram of the USART structure of the STM32 microcontroller:
insert image description here
the most important part is the shaded part in the figure, and the other parts are the interface and control parts.

When sending data, the data bus transfers the data to be sent to the sending data register (TDR), and the sending data register (TDR) then transfers a byte of data to the sending shift register, and the sending shift register transfers the data of this byte It is transmitted to the TX pin bit by bit according to the binary bit. After the data of the previous byte is sent through the data shift register, the content in the send data register will be sent to the send shift register. As can be seen from the above figure, when the send shift register sends the data, it first sends the data to the send shift register. The bit at the right end is sent out, which also shows that the serial port communication is low-order first.
When receiving data, the data is received by the RX pin in the form of a data frame, and the received data is sent to the receiving shift register. When the receiving shift register receives a complete byte of data, the entire byte of data will be transmitted. to the receive data register. When the receiving shift register receives data, the data is input from the left end in binary form. Every time a bit is received, the data moves to the right by one bit, and the original binary bit order will be restored at the end, which is why the serial port communication is low bit first. But the reason why the data doesn't change when receiving.

USART basic functions

There are many peripheral functions such as USART provided by the STM32 firmware library, which can be simply classified and understood once. The commonly used related functions of USART are as follows:

void USART_DeInit(USART_TypeDef* USARTx);//USART初始化:根据USART初始化结构参数USART外设
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);//USART解除初始化//
void USART_StructInit(USART_InitTypeDef* USART_InitStruct);//使用默认值填充USART初始化结构体成员
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);//允许或禁止USART外设
uint16_t USART_ReceiveData(USART_TypeDef* USARTx);//返回USART外设最新接收的数据
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);//通过USART外设发送当个数据
void USART_SendBreak(USART_TypeDef* USARTx);//发送终止字符
void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);//设置USART的某个中断请求是允许或禁止
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);//获取USART中断状态
void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);//清除USART某个挂起中断标志
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);//获取标志状态:检测某个USART事件是否置位
void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);//挂起标志清除:清除某个USART事件挂起标志

Guess you like

Origin blog.csdn.net/Tao_9/article/details/130546602