A topic about USART transmission flag TXE/TC

Publisher:美人如玉剑如虹Latest update time:2017-02-19 Source: eefocusKeywords:USART  TXE Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

11

 

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.

12

 

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.

13

 

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".

14

 

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.

15

 

Below are two output waveforms measured by querying the TXE and TC flags of the TX pin of the same UART respectively.

16

 

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.

17

 

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.


Keywords:USART  TXE Reference address:A topic about USART transmission flag TXE/TC

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号