On-chip buses connect blocks inside one chip. Peripheral interfaces connect the chip to the outside world: sensors, flash memories, radios, debug consoles, other chips on the board. They use only a few pins, so they are serial, and they are simple enough that almost every microcontroller has several of each. This part covers the three you will meet everywhere: UART, SPI and I2C, starting with the oldest and simplest.

A UART (Universal Asynchronous Receiver-Transmitter) sends bytes one bit at a time over a single wire, without a clock wire. Both sides agree on the bit rate in advance, and every byte is wrapped in a start bit and a stop bit so that the receiver can find it. This article explains the UART frame, the baud rate, how two UARTs are wired, and shows a byte crossing the wire in a simulation.

7 min read | Beginner to Intermediate

What You'll Learn

  • Describe the UART frame: start bit, data bits, optional parity, stop bit
  • Calculate bit times and throughput from the baud rate
  • Wire two UARTs together, with or without flow control
  • Explain how a receiver finds bytes without a clock

The Frame

When nothing is being sent, the line is high (idle). A byte is sent as a frame:

  1. a start bit: the line goes low for one bit time;
  2. data bits, usually 8, least significant bit first;
  3. an optional parity bit;
  4. one or two stop bits: the line is high again.

The common setting is written "8N1": 8 data bits, no parity, 1 stop bit. A frame then has 10 bit times for 8 bits of data, so the useful throughput is 80 percent of the bit rate. The byte 0x4B (binary 01001011) goes out as start, then 1, 1, 0, 1, 0, 0, 1, 0, then stop.

Baud Rate

The baud rate is the number of bit times per second. Common values are 9600, 115200 and 921600, and both sides must use the same one. At 115200 baud a bit lasts 8.68 µs and an 8N1 frame 86.8 µs, so a UART can send about 11,520 bytes per second.

Inside a chip, the bit time is counted in clock cycles: CLKS_PER_BIT = clock frequency / baud rate. A 50 MHz clock at 115200 baud gives 434 cycles per bit. The example in this chapter uses 16 cycles per bit on a 100 MHz clock (6.25 Mbaud) so that the waveforms are short.

Quick Check

Q: How long does it take to send 1000 bytes at 9600 baud with 8N1 framing?

Show Answer

Each byte takes 10 bit times, so 10,000 bits at 9600 bits per second: about 1.04 seconds.

Finding Bytes Without a Clock

The receiver has its own clock, unrelated to the transmitter's. It watches the idle line for a falling edge, the beginning of a start bit. It then waits half a bit time and checks that the line is still low, which rejects short glitches. From there it samples once per bit time, so every sample lands near the middle of a bit, where the value is stable. After the data bits, it checks that the stop bit is high; if not, the frame is broken and the receiver reports a framing error.

Every frame restarts this timing at its start bit. The two clocks only have to agree for the length of one frame, which is why UARTs tolerate a few percent of difference between the two baud rates. Designing a UART Receiver shows exactly how much.

Wiring

The transmit pin of each side goes to the receive pin of the other, and both share ground. That is a full-duplex link: both directions work at the same time. Optional RTS (request to send) and CTS (clear to send) wires let a receiver pause the sender when its buffer is full; see Parity, Flow Control and Physical Layers.

A Loopback Simulation

The example connects a UART transmitter to a UART receiver with one wire and sends three bytes:


send(8'h4B);
send(8'h55);
send(8'hA3);
Output
25  tx sends 0x4b
1595  rx got   0x4b
1635  tx sends 0x55
3205  rx got   0x55
3245  tx sends 0xa3
4815  rx got   0xa3

Each frame takes 10 bit times of 160 ns: 1600 ns. The receiver reports each byte in the middle of its stop bit, about 1570 ns after the transmitter started it.

Quick Check

Q: Why is the idle line high rather than low?

Show Answer

So that the start bit is a clear high-to-low edge that the receiver can find, and so that a line held low by a fault, such as a short to ground, shows up as framing errors instead of looking idle.

Where UARTs Are Used

  • debug consoles and boot loaders on almost every embedded system;
  • GPS, Bluetooth and cellular modules, which accept commands over UART;
  • RS-232 and RS-485 links between equipment, with level shifters or transceivers;
  • USB-to-serial adapters that make a UART look like a COM port on a PC.

Running It in ChipVerify Lab

The example contains the UART transmitter, receiver and the loopback testbench. Open the waveform and find the start, data and stop bits of each byte.

Common Mistakes

Mistake #1: Connecting TX to TX

Two transmit pins wired together fight each other and nothing is received. TX of one side always goes to RX of the other.

Mistake #2: Sending the most significant bit first

UART sends the least significant bit first. A transmitter that starts with bit 7 delivers bit-reversed bytes.

Mistake #3: Mismatched frame settings

Both sides must agree on baud rate, data bits, parity and stop bits. A receiver set to 8N1 that receives 8E1 frames sees the parity bit as the stop bit.

Practice Exercise

Challenge: Twice the Baud Rate

Set both TX_CLKS and RX_CLKS to 8, which doubles the baud rate. Predict how long each frame takes, then run it.

Hint

A bit time is now 8 clock cycles of 10 ns.

Solution
Output
25  tx sends 0x4b
835  tx sends 0x55
835  rx got   0x4b
1645  tx sends 0xa3
1645  rx got   0x55
2455  rx got   0xa3

A bit lasts 80 ns and a frame 800 ns, so the three bytes arrive twice as fast. The bytes are still received correctly because both sides changed together.

Open the ready-made example in the lab and run the simulation.

Summary

  • A UART sends bytes over one wire with no clock: idle high, start bit, data bits LSB first, optional parity, stop bit.
  • 8N1 frames take 10 bit times per byte; the baud rate is the number of bit times per second.
  • The receiver synchronizes on each start bit and samples in the middle of each bit.
  • TX connects to RX on the other side; RTS/CTS optionally add flow control.
  • Both sides must agree on baud rate and frame format.
Did you find what you were looking for?

Quiz

No quiz questions available for this article.