This post introduces the UART communication protocol and shows a basic implementation of a transmitter in SystemVerilog using a Tang Nano 20k FPGA.
The transmitter created for this post sends an incrementing 16-bit counter, which is received by a small C program that just prints the data to the console.
This post is accompanied by a repo, which contains the source code for this project.
What is UART?
UART is one of the simplest communication protocols, if not the simplest. It requires only a tx/rx connection and ground but no clock line. This however requires the speed to be pre-agreed so that both ends have the correct bit timing. The protocol transmits data frame by frame with one bit per baud tick. An example frame is displayed in the following figure.
Example 8-bit frame
A communication line idles in a high state and is pulled low for the duration of one bit to indicate a start.
This is followed by (in this case) 8 data bits, starting with the least significant bit (LSB).
A high data bit encodes a 1 and a low data bit encodes a 0.
The number of data bits can vary, however. Just like the baud rate, the number of data bits has to be agreed upon by sender and receiver.
A sequence is terminated by pulling the line high for the duration of one bit (2 is also possible) either returning it to the idle state or allowing it to be pulled low for a start bit for the next tick.
Optionally a parity bit can be inserted between the last data bit and the stop bit, however this demo does not use a parity bit for simplicity.
The figure below shows what transmission of one counter value would look like.
Example transmission of one counter value
To transmit the full 16 bits, two frames are transmitted back to back, again starting with the LSB and the first byte forming the least significant byte.
Building the Transmitter
The transmitter is based on the following simple state machine.
Transmitter state machine. Diagram created by generative AI.
When the board is powered on (or the reset button is pressed), the FPGA is in its initial state.
In the initial state, the state variable is set to START, which indicates the next state.
If the reset button is pressed, the LED closest to the USB-C port is turned on.
The corresponding bit for the LED is the LSB, and the LEDs are low-active, so it has to be set to 0 to light up and indicate a reset (and set to 1 after the button has been released).
The data line is pulled high, as the system is currently not transmitting.
1baud_counter <= 8'd0;
2counter <= 16'd0;
3bit_index <= 4'd0;
4bit_counter <= 3'd0;
5state <= START;
6uart_tx <= 1'b1;
7led <= 6'b111110;
8idle_counter <= 16'd0;
The frequency of the FPGA is 27 MHz1. At the chosen baud rate of $115\,200 \text{ baud}$, that is $\frac{27\,000\,000 \text{ Hz}}{115\,200 \text{ baud}} = 234.375$ cycles per bit. In the implementation, the baud counter counts up to 234, which is 235 cycles, resulting in an effective baud rate of $\frac{27\,000\,000 \text{ Hz}}{235 \text{ cycles/bit}} = 114\,893.617 \text{ baud}$, which is $1-\frac{114\,893.617 \text{ baud}}{115\,200 \text{ baud}} = 0.002659 = 0.266\%$ too slow. Well within tolerance2.
After transitioning into the START state, the data line is pulled low and the state transitions to the DATA state:
1START: begin
2 uart_tx <= 1'b0;
3 state <= DATA;
4end
In the data state the counter data is put onto the data line bit by bit until one frame has been sent:
1DATA: begin
2 uart_tx <= counter[bit_index];
3 bit_index <= bit_index + 1'b1;
4
5 // has one window been sent
6 if (bit_counter == 3'd7) begin
7 bit_counter <= 3'd0;
8 state <= STOP;
9 end else begin
10 bit_counter <= bit_counter + 1'b1;
11 end
12end
After a frame has been transmitted, the state transitions to the STOP state.
In the STOP state, the data line is pulled high to send the stop bit and the next state is either set to IDLE or back to START depending on whether another frame follows immediately after.
This is decided by checking whether the bit_index is 0. As a 4 bit value, it wraps around after 16 bits to 0, so a non-zero value means that the whole 16 bits have not been transmitted yet.
1STOP: begin
2 uart_tx <= 1'b1;
3
4 // continue transmitting
5 if (bit_index != 4'b0) begin
6 state <= START;
7 end else begin
8 state <= IDLE;
9 end
10end
The transition to START repeats the same sequence until both frames have been sent.
Then the state transitions into IDLE.
In the IDLE state a 16 bit counter is used to pace the transmission and to blink an LED to show that the transmitter is running:
1IDLE: begin
2 idle_counter <= idle_counter + 1'b1;
3 if (idle_counter == 16'hffff) begin
4 led[5] <= ~led[5];
5 counter <= counter + 1'b1;
6 state <= START;
7 end
8end
After a total of $235 \text{ cycles/tick} \cdot 2^{16} \text{ ticks} = 15\,400\,960 \text{ cycles}$, the counter is increased by one and the state machine transitions back to START to start sending the next counter value.
Reading the Output
Terminal
One of the easiest ways to read data from a UART bus is using a program called screen.
Screen interprets each byte as a character, but in this case the bytes are a binary counter, which results in output that looks like garbage.
To address this, a small receiver has been implemented in C. The receiver assumes that the first byte it reads is the low byte. When compiling and running the program the counter increments correctly and pressing the reset button (S1) resets the counter to 0.
Logic Analyser
A logic analyser can also be used to read the output and can be very useful for debugging. For this however, the data line(s) need to be on probeable pins.
One counter transmission from the logic analyser is displayed below. The trace clearly shows the frame boundaries as well as the individual bits.

Trace of one counter transmission
For this analysis, I reduced the idle counter down to 8 bits to speed up the transmission. With 8 bits one idle period is 256 ticks.
One baud tick lasts $\frac{235 \text{ cycles}}{27 \text{ MHz}} = 8.704 \ \mu\text{s}$.
So $8.704 \ \mu\text{s/tick} \cdot 256 \text{ ticks} = 2\,228.224 \ \mu\text{s}$.
The following figure displays two counter transmissions along with the idle period. The idle period is measured here to check against the calculations that were just made, and indeed, the idle period lasted about the calculated amount. The data is also correct; first byte is the least significant byte and its value has increased by one. So no data has been missed/the counter increments correctly.

Two transmissions with an idle period of ~$2\,229 \ \mu\text{s}$
Conclusion
This post explains how the UART protocol works and demonstrates a simplex implementation (communication only works in one direction) using an FPGA transmitter and PC receiver. The protocol however can also work in a half- or full-duplex configuration.
Different methods of testing the correctness of the implementation have been presented and it is confirmed that the transmitter works as expected.
Tang Nano 20k schematic: https://dl.sipeed.com/fileList/TANG/Nano_20K/2_Schematic/Tang_Nano_20K_3923_Schematics.pdf ↩︎
“Determining Clock Accuracy Requirements for UART Communications”: https://www.analog.com/en/resources/technical-articles/determining-clock-accuracy-requirements-for-uart-communications.html ↩︎