Embedded - UART Introduction

overview

Embedded systems often require communication between integrated circuits. As an example, a digital temperature sensor reports the ambient temperature of a room to the main control chip. Typically, this data is transmitted via a serial interface.

So, what is a serial interface? At its most basic, a serial interface is a shift register that shifts data in and out one bit at a time. The illustration below shows that the data of the shift register is first loaded by a parallel means, such as a memory copy. After the parallel load is complete, the data is shifted out bit by bit, starting with the least significant bit.

So why do we use a serial interface instead of a parallel one? If we want to transfer a 32-bit value using the parallel interface, we need at least 32 pins on the microprocessor. Parallel buses are very fast, but they require a lot of external pins, only the pins used for this single interface. Adding extra pins to the device increases the bill of materials and physical size of the microprocessor.

The motivation for using a serial interface is that a serial interface consists of only a few pins. Using a small number of pins, we can transfer data of any size. We only need to increase the clock period if the data we need is 32 bits instead of 8 bits. Reducing the number of pins also makes PCB (printed circuit board) development easier, as far fewer pins need to be routed between devices.

The serial interface we'll be working on here is called a UART. The term UART refers to Universal Asynchronous Receiver/Transmitter (Universal Asynchronous Receiver/Transmitter). UARTs are typically used to interface with devices that do not have graphics display capabilities. The UART can provide input/output functions to a terminal program, allowing the user to monitor status and provide input functions.

UART basic structure (Infrastructure)

  • Needed pin (Pin)

The UART interface consists of two pins: Rx and Tx pins. The Rx pin is used to receive data. Tx pin is used to transmit data. When two devices are connected using UART, the Rx pin of one device is connected to the Tx pin of the second device.

  • Common Registers

The Rx and Tx pins are usually connected to separate shift registers (one for shifting data out and one for shifting data in). Two independent shift registers allow the UART to transmit and receive data at the same time.

In addition to the shift register, there are one or more status registers. Software checks the status register to determine when the transmit shift register is empty. When the transmit register is empty, the next byte of data can be loaded into the transmit shift register. The status register will also have a status bit indicating when a new data byte has been received. On the other hand, software can read the receive register and trigger a delete operation and allow the next byte of data to be shifted in.

UARTs can have additional registers that allow software to configure the behavior of the UART. The rate of receiving and sending data can be set by software, as well as the format of the transmitted data. For example, set the baud rate of the transmission speed, whether each byte of data contains a parity bit.

Packet Structure

One of the key characteristics of a UART is the "A" in its name. A" stands for asynchronous. UART is asynchronous in nature because there is no shared clock between the devices. Instead, the two devices must agree on the structure of the data being sent and the speed at which the data is to be sent. This protocol allows UART The interface oversamples the data line and reconstructs the raw data into a packet.We will examine three properties that make up a UART packet.

  • Data Rate

In order to properly send data between two UARTs, both UARTs must be configured to receive and transmit data at the same data rate. This is often referred to as the UART's baud rate. By setting the same baud rate, the UART's internal state machine can set the appropriate rate for the shift register to work. Common data rates include 9600 and 115200 baud, but some UARTs support higher data rates, up to several Mbit/s. For example, using the CC2564c Bluetooth chip, the UART on it has hardware flow control, and the set baud rate can reach 3Mbit/s.

  • Parity

In some cases, packets may contain a parity bit. The parity bit is used by the receiving device to determine if any data corruption has occurred during transmission. If a packet is configured with even parity, the total number of 1s including the parity bit should be even. If a packet is configured with odd parity, then the total number of 1s including the parity bit should be an odd number.

  • Data Framing¶

In order for the microprocessor to detect when data is being sent, we need to define the behavior of the Rx/Tx lines when no data is being sent. In most cases, both Rx/Tx lines will be driven high when no data is being sent. When a device sends data, the sending process begins with a start bit. The UART will pull down the Tx line so that it is at a low level, and the duration is one clock cycle. The clock cycle depends on the data transmission rate, indicating that the data transmission is about to start.

After the start bit, the data is shifted out bit by bit according to the defined data transmission rate until all the data transmission is completed. UARTs transmit data least significant bit first. Once all the data has been transferred, a parity bit is sent (this bit is optional, as long as the configuration on both devices is the same), followed by a stop bit. The UART generates a stop bit by pulling the Tx line high and keeping it high for 1 cycle. This cycle depends on the data transmission rate. Many UARTs can be configured to generate 1 or 2 stop bits.

The diagram below shows how the value 0x71 is transmitted using even and odd parity.

Framing errors occur when data is sent without start or stop bits. The UART may simply discard this data, or more likely, signal an error condition by setting a status bit in the status register. In most cases, framing errors occur when there is a large amount of electromagnetic interference that invalidates the data in transit.

8N1

One of the most common configurations of UARTs is called 8N1. This represents 8 data bits, no parity bit, and one stop bit. 8N1 transmits 8 bits of data for every 10 bits transmitted. Adding a parity bit and an extra stop bit increases the overhead per packet and reduces the overall throughput of actual data.

reference:

1,UART basics

UART Basics – ECE353: Introduction to Microprocessor Systems – UW–Madison

Guess you like

Origin blog.csdn.net/guoqx/article/details/131066084