Regarding the USART transmission of ST MCU, people often have some doubts about the use of TXE/TC, or some problems arise due to the application of the two. Here I will take some time to organize it and share it with you.
1. Basic concepts and understanding of TXE and TC marks
Regarding USART transmission, you may want to take a look at a portion of the block diagram. The transmission process is as follows:
The transmission part consists of two parts, one is the data buffer, namely the transmission data register [TDR], and the other is the data shift register, namely the red box at the bottom of the figure below. First, the data to be sent is put into the TDR, and then the data in the TDR is copied into the shift register [transmit shift register] at the right time. The data is sent from the shift register to the TX line one by one until all the data in the shift register is sent out. After the whole process is completed, the data to be sent is considered to be sent.
In this process, two flag bits are involved, one is the TXE bit and the other is the TC bit, which are in the USART_SR register.
After the chip is reset, the default value of the register [USART_SR] is 0x00C0, that is, the default values of [TXE, TC] are both 1. I will mention this here first, and I will mention this default value later. [It is often necessary to pay attention to the default value of the register]
TXE indicates whether the transmit buffer [TDR] is empty. If there is temporary data in TDR, that is, it is not empty, TXE=0. When the data in TDR is copied to the shift register and no new data is put into TDR, TXE=1.
TC is a flag indicating whether all the data from TDR has been moved to the TX line. If all the data from TDR has been moved to the TX line and there is no new data in TDR, TC=1. On the contrary, if all the data from TDR has not been moved to the outside, or if the data in TDR has been moved, but new data has come to TDR, TC=0. Usually, people call TC the transmission end flag, which is correct, but sometimes it may cause some misunderstandings, and the misunderstanding lies in the word "end".
If you find the above description not intuitive enough, you may want to take a look at the above data transmission flow chart. Regarding the TXE/TC flag, we can make a relatively lifelike analogy here, which may not be very appropriate.
Suppose a group of people [all data to be sent] are going to a certain place to do business, and they are planned to be sent there in batches and continuously according to the number of people in each wave [data to be sent each time]. Each group takes a car together [enters TDR] to a certain place halfway, then gets off the car and walks on a single-plank bridge [shift register], and then lines up and walks across the bridge one by one to reach the destination [TX foot].
TXE=0 corresponds to when there are one person in the car; [data is stored in TDR]
TXE=1 corresponds to when each wave of people just got off the bus and walked onto the single-plank bridge; [no data in TDR]
TC=1 Here, it corresponds to the time when all people who want to go out have crossed the single-plank bridge; [all the data to be sent has been sent]
TC=0 corresponds to someone still on the bridge, or some people have crossed the bridge but are still in the car. [For example, when 1024 bytes of data are to be sent, only part of it has been sent. ]
Regarding the TC flag being set to 1, TC is set to 1 as long as all the data coming from TDR is transferred to the TX pin and there is no new data entering TDR at this time.
For the 1024 bytes of data to be transferred mentioned above, without considering the DMA method, you can have three implementation methods:
1. Query 1024 TC flags to send data; [Obviously, each word is sent after the next word is sent. ]
2. Query 1023 TXE flags and 1 TC flag;
3. Query 1024 TXE flags.
In short, it doesn't matter which method you use, as long as it meets the application requirements. [The above mentioned method of crossing the bridge by car is the default method 2]
Here we might as well see if there is any difference in the case of continuously sending data by querying the TXE and TC flags under the same conditions. Assuming 8-bit word length, 1 STOP bit, 1 start bit, the baud rate is the same, and the data is always 0x55.
Below are two output waveforms measured by querying the TXE and TC flags of the TX pin of the same UART respectively.
Obviously, the output waveforms are different in the two cases. When polling the TC flag, it is found that there is an additional gap of approximately 1 bit between words; in contrast, when polling TXE, the data is very coherent. Because when querying TC, each time, it has to wait until the data of each word is completely shifted out of the shift register before adding the next data, which will cause a small pause.
Querying TXE can replenish data in time to ensure transmission efficiency; querying TC can know the exact time when the data is sent out. The two are often used together to ensure transmission efficiency and to know the exact time when data transmission ends.
2. Problems caused by improper use of TXE/TC marks.
2.1 The problem of losing the first word due to improper use of the TC flag when sending data.
The relevant sending code for this situation has a basic feature, first fill in the data into the TDR, and then query the TC flag to decide whether to update the TDR data. The general code is as follows:
for(i=0; i { USART_SendData(USARTx, TxBuffer[i]); while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET); } According to the above code, there may be problems sending the first data. As mentioned above, the default value of the TC flag after chip reset is 1. For single data buffer transmission, if you want to clear TC, you must follow the following two steps in sequence [note the word "sequentially"], that is, read SR first, then write DR. The first data here is written to DR first and then read to SR, which cannot clear TC, so the initial value of TC is 1. According to the code instruction, the second data is immediately filled into TDR, and the first data may be overwritten before it can be sent. However, since the second data update, the order of reading SR first and then writing DR is met each time, so TC can be reliably cleared each time, and errors similar to the first word will naturally not occur later. If you change the polling TC flag in the above code to the TXE flag, there will be no such problem, because each time you write DR, TXE will be cleared. Or you can also avoid the problem by clearing TC once before starting to transmit the code. 2.2 Improper use of TXE causes the last word to be lost. Here, improper use of TXE mainly refers to using the TXE flag when TC should be used. In fact, the main function of the TC flag is to ensure that all data sent to the data buffer TDR is transferred to the TX line each time. When sending a group of multiple data, after the last data is placed in TDR, it is recommended to check and wait for TC=1. Especially in the following situations: A: After UART has sent data, you need to disable UART; B: UART needs to enter sleep mode after sending data; In the above case, when the last data is put into TDR, if you just wait for TXE=1 before taking action, the last data may not be moved out of the shift register in time and will be hung up due to the peripheral disable instruction or sleep instruction. If we apply the above analogy, we often hear "demolishing the bridge after crossing the river", but here people are eager to demolish the bridge before crossing it, so tragedy is easy to happen.
Previous article:The problem and solution of PCROP area function cannot be called
Next article:The influence of signal source characteristics on conversion results in STM32 ADC applications
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 135 classic design examples of Verilog
- [Perf-V Evaluation] On-chip Temperature Collection
- 100 Practical Tips for FPGA Design Experts (English Original).zip
- Huawei_Analog Circuit Design Volume 1
- Recruiting ADC/DAC application engineers, working location: Hangzhou
- EEWORLD University Hall----New Raspberry Pi 4: Can it replace your PC?
- SHT31 Review + Preliminary Understanding of Evaluation Board
- [Red Envelope Ask] Linux kernel outputs garbled code after entering the serial console
- How to calculate the temperature rise of MOS tube and LDO in the most reasonable way
- Review summary: Experience the fresh goods of live broadcast ~ ST60 short-distance, non-contact connector