Basics of UART - 115DAB/WS2024 GitHub Wiki

Backgrounds

Universal Asynchronous Receiver-Transmitter (UART) is a device-to-device serial data transmission protocol. As one of the serial communication protocols, UART transmits data bit-by-bit sequentially, which means one bit is transmitted at a time. It is also an asynchronous protocol, meaning that UART is not dependent on a clock signal shared among devices. Instead, each packet sent using UART would be framed by a start bit and a stop bit. UART can support up to full-duplex communications, which allows data transmission in both directions simultaneously. UART is one of the earliest serial communication protocols. It was proposed by Gordon Bell in order to free up the main processor from handling data transmission tasks. The first individual UART transceiver IC WD1402A was developed by Western Digital in 1971 [2]. A UART communication system only requires a maximum of two wires. Even though some other more sophisticated serial protocols such as USB are more commonly used nowadays, UART still has a place in modern communications due to its simplicity.

WD1402A UART chip

Figure 1: Block diagram of WD1402A [2].

Hardware Setup (Circuitry) of UART

UART only requires two wires to communicate in both directions: TX, the transmitter, and RX, the receiver. To send data from device A to device B, we connect TX pin of A to the RX pin of B to achieve a one-way data communication. To communicate legitimately, UART needs the three blocks: timekeeping, transmitting, and receiving.

Block Diagram of a UART IC

Figure 2: Block diagram of a UART IC [5].

Clocks

Timekeeping is an important part of UART communication as the data line switches between logic 1 and logic 0’s to indicate the 1 and 0 bit. To read the input and write to output correctly, we need clocks to decide the time to read and write data. From the fastest system clock given by the processor, UART’s clock generator generates a sample clock and a transmit (baud rate) clock. The sample clock is usually 3,8, or 16 times faster than the transmit clock [5].

Data Transmit

To write data to the TX pin, the processor loads the bytes into a FIFO (first in, first out) transmit buffer register. The transmit register performs a parallel to serial conversion and loads serial data into a shift register. In every transmit clock cycle received, the shift register shifts out one bit stored, creating a serial data stream. The buffer register refills the shift register automatically, and the transmit logic sends a TX empty flag to the processor when the buffer is empty, indicating the data writing is complete [5].

Data Receive

To read data from the RX pin, the shift register on the receiver will start to shift in incoming bits after the “start of data” pattern has been detected. When the shift register is full, it sends an RX flag to the processor, indicating data has been received, and writes the data to a FIFO receive buffer, performing a serial to parallel conversion. The processor will then decide when or whether to read the data stored in the receive buffer. To decide whether an incoming bit is 0 or 1, we sample the bit at around its mid-point. For example, if the sample clock is 16x faster (16x oversampling) than the transmit clock, the receiver samples the incoming bit at the 7th, 8th, and 9th of the sample clock cycle, and the majority rule applies to decide if the incoming bit is 0 or 1. After 16 cycles of the sample clock, the next input bit will begin. Higher oversampling rate and the majority rule protect the signal integrity at the cost of more power consumption [5].

UART Packets

A UART packet consists of four parts: start bit, data frame, parity bit, and stop bit(s). The most used setting of a packet is 8-bit (1-byte) data frame, no parity bit, and 1 stop bit (aka 8N1).

UART Packet Format

Figure 3: UART packet format [1].

Baud Rate

No clock signal is shared between two devices. To allow communication between two devices, the baud rate, which tells the clock frequency of the UART packets, must be configured to be the same on both devices. Only one bit would be read and/or sent by a device within one clock cycle specified by the baud rate. If the baud rates are mismatched, two devices would not understand each other, as the data samples taken may be spread between multiple bits, and some bits may not be sampled at all.

Start Bit

The transmission line keeps in high (1) when no packet is being transmitted. To start transferring data, the first bit of a packet would be pulled down to low (0) for one clock cycle. This provides a way of synchronizing the clocks between two devices – the falling edge of the start bit should match the start of a sampling clock cycle.

Data Frame

The data frame contains the actual message the packet is carrying. The minimum length of the data frame is 5 bits. The maximum length of the data frame and the parity bit together is 9 bits, which means when a parity bit is included in a packet, the maximum length of the data frame is 8 bits. Usually, the data frame is least-significant bit first.

Parity Bit

To improve transmission accuracy, a parity bit can be included in a packet to verify the data frame. The parity bit tells the oddness of the total number of 1’s in the data frame. For example, if the data frame is 01101101, the total number of 1’s is 5 (odd number), thus the parity bit is 1. The parity bit can tell if one bit in the data frame is changed during transmission.

Stop Bit(s)

To end a packet, the data line is pulled up to high (1) for one or two clock cycles, depending on the settings.

Modern Applications

UART’s popularity has been decreasing in recent years, with protocols such as SPI and I2C replacing it. Instead of communicating over a serial port, modern computers and peripherals now use USB and Ethernet. However, UART is still used in communication that requires lower-speed applications, because it is simple, low-cost, and easy to implement.

UART serves as a cornerstone in embedded systems, facilitating seamless communication between microcontrollers and peripheral devices. This protocol finds extensive application in various sensors, including GPS modules, temperature sensors, and speed sensors. Through UART, microcontrollers can effortlessly retrieve data from these sensors, ensuring reliable exchange and control of operations. The communication mode varies depending on the application: from simplex, where the microcontroller merely receives data, to half-duplex, where bidirectional communication is needed for command exchange, and full-duplex, allowing simultaneous data transmission between the microcontroller and peripherals.

Moreover, UART extends its utility into wireless realms, enabling wireless communication through protocols like Bluetooth and Wi-Fi. In wireless setups, microcontrollers utilize UART to transmit and receive data wirelessly, expanding the scope of connectivity. These wireless communications can adopt either half-duplex or full-duplex configurations, depending on the protocol employed. For instance, Bluetooth typically operates in full-duplex mode, while radio frequency (RF) protocols generally function in half-duplex, showcasing the versatility of UART in bridging the gap between embedded systems and wireless networks. Examples of UART applications

Figure 4: UART application [7].

Comparison Between Protocols

Some other serial protocols such as SPI and I2C are also widely used in communications. Each of them has some similarities and differences between each other, providing advantages in different aspects such as difficulty in system design, hardware complexity, and transmission speed.

UART's simplicity and low hardware requirements make it easy to implement and suitable for straightforward microcontroller-peripheral device communication. For instance, UART is commonly used in GPS modules for data exchange between the module and the microcontroller. However, its relatively slower data transfer rates (230 Kbps to 460 Kbps) may limit its application in high-speed scenarios.

SPI, offering high-speed full-duplex communication (10 Mbps to 20 Mbps), excels in scenarios involving multiple devices under the control of a single master, ensuring efficient data transfer. An example of SPI usage is in memory chips, where the microcontroller acts as the master device controlling multiple memory chips for data storage. Yet, its increased hardware complexity, requiring four pins and potentially intricate circuitry, might pose challenges in design and implementation.

On the other hand, I2C's flexibility, supporting multi-master operation and accommodating various device classes, makes it versatile for consumer electronics and peripheral devices. For example, I2C is commonly employed in temperature sensors, allowing multiple sensors to be connected to a single microcontroller for temperature monitoring. However, its maximum data rates (100 Kbps, 400 Kbps, and 3.4 Mbps) might not meet the demands of high-speed applications, limiting its suitability in certain contexts. UART Communication diagram

Figure 5: UART Communication [6].

SPI Communication Diagram

Figure 6: SPI Communication [6].

  • I2C: I2C is also a synchronous serial communication protocol with multiple master devices controlling multiple slave devices.

I2C Communication Diagram

Figure 7: I2C Communication [6].

Conclusion

As one of the oldest but simplest communication protocols, UART is still an important piece in modern communications. The advantages and disadvantages of using UART are very straightforward: it is slow, and it can only connect between two devices, but it is simple enough and cheap enough. These features will help UART maintain a unique position in modern applications.

References

[1] E. Peňa and M. G. Legaspi, (2020, Dec) “UART: A Hardware Communication Protocol Understanding Universal Asynchronous Receiver/Transmitter”. Analog Dialogue Mag. [Online] Available: https://www.analog.com/en/resources/analog-dialogue/articles/uart-a-hardware-communication-protocol.html

[2] IEEE Spectrum, (2017, Jun. 30). “Chip Hall of Fame: Western Digital WD1402A UART”. IEEE Spectrum. [Online]. Available: https://spectrum.ieee.org/chip-hall-of-fame-western-digital-wd1402a-uart

[3] H. Siebeneicher, (2023 Nov. 01) “Universal Asynchronous Receiver-Transmitter (UART)”. Arduino Documentation. [Online]. Available: https://docs.arduino.cc/learn/communication/uart/

[4] Texas Instruments, (2022, Dec. 21). “UART: Microcontroller Overview”. Texas Instruments. [Online]. Available: https://www.ti.com/video/6288225615001

[5] NextPCB, “SPI vs. I2C vs. UART: Differences Between These Communication Interfaces”. [Online]. Available: https://www.nextpcb.com/blog/spi-i2c-uart

[6] Anusha, (2017, Jun. 17) “Basics of UART communication”. Electronics Hub. [Online]. Available: https://www.electronicshub.org/basics-uart-communication/

[7] U. Nanda and S. K. Pattnaik, "Universal Asynchronous Receiver and Transmitter (UART),", in 2016 3rd International Conference on Advanced Computing and Communication Systems (ICACCS), Coimbatore, India, 2016, pp. 1-5, doi: 10.1109/ICACCS.2016.7586376. 8