[Bus] One article to understand the UART communication protocol

content

Introduction to UART Communication

How UART works

start bit

Data Frame

parity bit

stop bit

Steps for UART transfer

Advantages and disadvantages of UART

advantage

shortcoming


Introduction to UART Communication

UART is a universal asynchronous transceiver, which is a general serial and asynchronous communication bus. The bus has two data lines, which can realize full-duplex transmission and reception. It is often used between the host and auxiliary equipment in embedded systems. Communication. In UART communication, two UARTs communicate directly with each other. A sending UART converts parallel data from a controlling device (such as a CPU) to serial form, transfers it serially to a receiving UART, which then converts the serial data back to parallel data for the receiving device. Only two wires are required to transfer data between two UARTs. Data flows from the Tx pin of the transmit UART to the Rx pin of the receive UART:

The UART transfers data asynchronously, which means that there is no clock signal to synchronize the bit output of the sending UART to the bit sampling of the receiving UART. Instead of a clock signal, the sending UART adds start and stop bits to the packet being transmitted. These bits define the start and end of the packet, so the receiving UART knows when to start reading bits.

When the receiving UART detects a start bit, it starts reading incoming bits at a specific frequency called baud rate. Baud rate is a measure of the speed of data transfer, expressed in bits per second (bps). Both UARTs must run at approximately the same baud rate. The baud rate between the transmit and receive UARTs can only differ by about 10% before the timing of the bits deviates too far. Both UARTs must also be configured to transmit and receive the same packet structure.

number of lines required

2

maximum rate

Up to 10Mb/s

synchronous or asynchronous

asynchronous

serial or parallel

parallel

Maximum number of hosts

1

Maximum number of slaves

1

How UART works

The UART that will transmit data receives data from the data bus. The data bus is used to send data to the UART via another device such as a CPU, memory or microcontroller. Data is transferred from the data bus to the transmit UART in parallel. After the sending UART gets parallel data from the data bus, it adds a start bit, a parity bit, and a stop bit to create the packet. Next, the packet is serially output bit by bit on the Tx pin. The receiving UART reads packets bit by bit on its Rx pin. The receiving UART then converts the data back to parallel form and removes the start, parity and stop bits. Finally, the receiving UART transfers the packets in parallel to the data bus on the receiving end:

The data transmitted by the UART is organized into packets. Each packet contains 1 start bit, 5 to 9 data bits (depending on the UART), an optional parity bit, and 1 or 2 stop bits:

start bit

The UART data transmission line is normally held high when it is not transmitting data, that is, idle. To start a data transfer, the transmitting UART pulls the transmission line from high to low for one clock cycle. When the receiving UART detects a high-to-low voltage transition, it starts reading the bits in the data frame at the baud rate.

Data Frame

The data frame contains the actual data being transferred. If parity bits are used, the length can be 5 to 8 bits. If the parity bit is not used, the data frame can be 5 to 9 bits long. In most cases, data is sent from the lowest bit in the data frame.

parity bit

The purpose of the parity bit is to check whether there is a data error in the data frame from transmission to reception. Parity describes the even or odd number of numbers. The parity bit is a way for the receiving UART to determine if any data has changed during transmission. After the receiving UART reads the data frame, it counts the number of bits with a value of 1 and checks if the total is even or odd. If the parity bit is 0 (even parity), the 1 bits in the data frame should add up to an even number. If the parity bit is 1 (odd parity), the 1 bits in the data frame should add up to an odd number. When the parity bits match the data, the UART knows the transfer was error-free. However, if the parity bit is 0 and the total is odd; or if the parity bit is 1 and the total is even, the UART knows that the bits in the data frame have changed.

stop bit

To signal the end of a packet, the sending UART transitions the data line from low to high for at least two bits.

Steps for UART transfer

1. The transmit UART receives data in parallel from the data bus:

2. The sending UART adds start, even parity and stop bits to the data frame:

3. The entire packet is sent serially from the transmit UART to the receive UART. The receiving UART samples the data line at a preconfigured baud rate:

4. The receiving UART discards the start, parity and stop bits from the data frame:

5. The receiving UART converts the serial data back to parallel and transfers it to the data bus on the receiving end:

Advantages and disadvantages of UART

No communication protocol is perfect, but UART is very good at what it does. Here are some pros and cons:

advantage

  • Use only two wires
  • No clock signal required
  • Has parity bits to check data for errors
  • The structure of the packet can be changed, just add the necessary bits at both ends

shortcoming

  • The size of the data frame is limited to a maximum of 9 bits
  • Multiple masters or slaves are not supported
  • The baud rate of each UART must be within 10% of each other

Guess you like

Origin blog.csdn.net/m0_61298445/article/details/124123456