Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 37 ยท Projects and Practice

Project: A UART Transmitter

Design a UART transmitter in VHDL: frame format, baud-rate tick generator, shift-register FSM, ready/valid interface and a runnable C model that checks the bit stream.

The UART (universal asynchronous receiver/transmitter) is the simplest serial link: one wire, no clock, each byte sent as a framed sequence of bits at an agreed speed. It is a perfect project: a baud-rate divider, a state machine and a shift register, with an interface that is easy to test. You will design the transmitter and check its output bit by bit.

VHDL is shown read-only (no simulator on this site). The C model is runnable and follows the same state machine, and a decoder in C checks that the bit stream it makes is valid.

The frame (8N1)

A line idles high. To send a byte:

  1. One start bit: the line goes low for one bit time.
  2. Eight data bits, least significant bit first.
  3. One stop bit: the line goes high again for one bit time.

That format is called 8N1 (8 data bits, no parity, 1 stop bit). At 115200 baud, one bit lasts 1/115200 s, about 8.68 microseconds, which is 868 cycles of a 100 MHz clock (100,000,000 / 115,200 = 868.05, so the integer divider 868 gives an error of about 0.006%, fine for UARTs, which tolerate a few percent).

flowchart LR
  A["idle (1)"] --> B["start (0)"]
  B --> C["d0"] --> D["d1"] --> E["..."] --> F["d7"]
  F --> G["stop (1)"]
  G --> A

Bit order for one byte. The receiver finds the falling edge of the start bit and samples each bit in the middle.

Interface

VHDL
entity uart_tx is
  generic (CLKS_PER_BIT : positive := 868);     -- 100 MHz / 115200
  port (clk, rst : in  std_logic;
        data  : in  std_logic_vector(7 downto 0);
        valid : in  std_logic;     -- producer has a byte
        ready : out std_logic;     -- transmitter is idle and can take it
        tx    : out std_logic);    -- the serial line
end entity;

The valid/ready pair is the handshake from FIFOs and Handshakes: the byte is accepted on the edge where both are high.

The FSM

Four states: IDLE, START, DATA, STOP. A counter measures one bit time (CLKS_PER_BIT clock cycles) in each of the last three states; a bit index counts 0 to 7 in DATA.

VHDL
architecture rtl of uart_tx is
  type state_t is (IDLE, START, DATA, STOP);
  signal state   : state_t := IDLE;
  signal clk_cnt : natural range 0 to CLKS_PER_BIT - 1 := 0;
  signal bit_idx : natural range 0 to 7 := 0;
  signal shreg   : std_logic_vector(7 downto 0);
begin
  ready <= '1' when state = IDLE else '0';

  process (clk)
  begin
    if rising_edge(clk) then
      if rst = '1' then
        state <= IDLE; tx <= '1'; clk_cnt <= 0; bit_idx <= 0;
      else
        case state is
          when IDLE =>
            tx <= '1';
            if valid = '1' then          -- ready is '1' in this state
              shreg <= data;
              clk_cnt <= 0;
              state <= START;
            end if;
          when START =>
            tx <= '0';
            if clk_cnt = CLKS_PER_BIT - 1 then
              clk_cnt <= 0; bit_idx <= 0; state <= DATA;
            else
              clk_cnt <= clk_cnt + 1;
            end if;
          when DATA =>
            tx <= shreg(0);              -- LSB first
            if clk_cnt = CLKS_PER_BIT - 1 then
              clk_cnt <= 0;
              shreg <= '0' & shreg(7 downto 1);     -- shift right
              if bit_idx = 7 then state <= STOP;
              else bit_idx <= bit_idx + 1; end if;
            else
              clk_cnt <= clk_cnt + 1;
            end if;
          when STOP =>
            tx <= '1';
            if clk_cnt = CLKS_PER_BIT - 1 then
              clk_cnt <= 0; state <= IDLE;
            else
              clk_cnt <= clk_cnt + 1;
            end if;
        end case;
      end if;
    end if;
  end process;
end architecture;

Points worth noticing:

  • tx is registered: it is assigned in the clocked process, so the output is glitch-free, an important property for a line that another device samples asynchronously.
  • The shift register moves right each bit time, so shreg(0) is always the next bit to send (LSB first).
  • ready is high only in IDLE, so a producer cannot overwrite a byte in flight.
  • CLKS_PER_BIT is a generic, so the testbench can use 4 or 8 for fast simulation.

A C model with a frame decoder

The model steps one clock per iteration, exactly as the VHDL does, and records the tx line. A separate decoder (the receiver's job) looks for the start bit, samples in the middle of each bit and rebuilds the bytes. If the decoded bytes match what was sent, the transmitter works.

C C stand-in: UART transmitter FSM and a frame decoder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The decoder finds three frames, each 10 bit times long (80 clocks), and the decoded text equals the message. The first-frame line (it starts with one idle clock before the start bit) shows the shape: a low start bit, the data bits for H (0x48 = 01001000, LSB first: 0 0 0 1 0 0 1 0) and a high stop bit, each bit lasting 8 characters.

Parity, baud error and timing

Optional extensions of the format:

  • Parity bit: after the data bits, one extra bit makes the count of ones even (even parity) or odd. Computing it is an XOR of all data bits.
  • Two stop bits give the receiver more time.
  • Baud error: the transmitter and receiver clocks differ. The receiver resynchronizes on each start bit, so errors below about 2% over a 10-bit frame are harmless (the last bit sample drifts by 10 bit times x the error).
C C stand-in: baud divider error for common clock/baud pairs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Every error is below 0.5%, well inside a UART's tolerance of a few percent. The 12 MHz row at 921600 baud leaves only 13 clocks per bit, too few for a receiver that oversamples 16 times, so very fast baud rates call for a faster clock (or a PLL).

Testbench plan

  1. Use CLKS_PER_BIT := 4; reset; check that tx is high.
  2. Send 0x55 (alternating bits, easiest to see on a waveform) and verify the frame bit by bit: sample tx in the middle of each bit time.
  3. Send back-to-back bytes; verify that ready falls and rises, and that the second byte is not accepted while the first is in flight.
  4. Send 0x00 and 0xFF (all low, all high data bits).
  5. Reset in the middle of a frame: tx returns to idle high.
  6. Write a UART receiver model in the testbench (like the C decoder) so every test is self-checking, following Self-Checking Testbenches.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In UART 8N1, in which order are the data bits sent?

    UART sends the LSB first, so the transmitter shifts right and sends bit 0 each bit time.
  2. 2. What is the idle level of a UART line?

    Idle is high, so the start bit is a falling edge the receiver can detect.
  3. 3. CLKS_PER_BIT for a 100 MHz clock at 115200 baud is about

    100,000,000 / 115,200 is roughly 868.05 clock cycles per bit.
  4. 4. Why is the tx output registered?

    A glitch on a UART line can be mistaken for a start bit; a registered output changes only on clock edges.

Summary

  • A UART frame is a low start bit, eight LSB-first data bits and a high stop bit (8N1).
  • The bit time comes from dividing the clock: CLKS_PER_BIT = clock / baud.
  • The transmitter is a four-state FSM with a bit-time counter, a bit index and a shift register.
  • Register tx and use ready/valid for the byte interface.
  • Verify with a receiver model that decodes the line and compares each byte.

Next: Practice: An ALU and Exercises