Module 10: Capstone Projects · Project 2 — UART Controller

Project 2, Part 2: The Baud Rate Generator

The previous article established UART bit timing. This article implements the hardware counter that generates periodic timing strobe pulses. A parameterized modulo-N counter divides the system clock into precise serial baud rate ticks.

Transmitter Baud Rate Generator: 1x Pulse

The transmitter requires exactly one enable pulse per bit duration. A parameterized counter divides the system clock frequency:

module baud_tick_gen #(
    parameter CLK_FREQ  = 50_000_000,
    parameter BAUD_RATE = 115200
) (
    input  logic clk, rst_n,
    output logic tick   // Generates a 1-cycle enable strobe once per bit period
);
    localparam int DIVISOR = CLK_FREQ / BAUD_RATE;
    logic [$clog2(DIVISOR)-1:0] count;

    always_ff @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            count <= 0;
            tick  <= 1'b0;
        end else if (count == DIVISOR - 1) begin
            count <= 0;
            tick  <= 1'b1;
        end else begin
            count <= count + 1'b1;
            tick  <= 1'b0;
        end
    end
endmodule

The module computes the integer counter limit using the $clog2 function. Changing parameters adapts the counter to any system frequency and baud rate without manual recoding.

Receiver Oversampling Generator: 16x Pulse

The receiver must detect asynchronous start edges and sample incoming data bits at their centers. To achieve high timing resolution, the receiver operates from a clock tick 16 times faster than the baud rate. The receiver counts 16 sub-ticks per bit and samples incoming serial data on the eighth sub-tick.

One bit period with 16 sub-ticks and the sample at the centre
Figure 1: The 16x oversampling tick and the centre sample

module baud_tick_gen_16x #(
    parameter CLK_FREQ  = 50_000_000,
    parameter BAUD_RATE = 115200
) (
    input  logic clk, rst_n,
    output logic tick_16x
);
    localparam int DIVISOR_16X = CLK_FREQ / (BAUD_RATE * 16);
    logic [$clog2(DIVISOR_16X)-1:0] count;

    always_ff @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            count <= 0;
            tick_16x <= 1'b0;
        end else if (count == DIVISOR_16X - 1) begin
            count <= 0;
            tick_16x <= 1'b1;
        end else begin
            count <= count + 1'b1;
            tick_16x <= 1'b0;
        end
    end
endmodule

The 16x oversampling architecture provides reliable noise immunity. It allows the receiver logic to reject transient glitches before validating the start bit.

Integer Division Timing Errors

Integer division truncates fractional clock remainder cycles:

$$\text{Actual Baud Rate} = \frac{\text{CLK_FREQ}}{\text{DIVISOR}}$$

$$\text{Error Percentage} = \left| \frac{\text{Actual Rate} - \text{Target Rate}}{\text{Target Rate}} \right| \times 100%$$

For a 50 MHz clock at 115200 baud:

$$\text{DIVISOR} = \lfloor 50{,}000{,}000 / 115200 \rfloor = 434$$

$$\text{Actual Rate} = 50{,}000{,}000 / 434 \approx 115{,}207 \text{ baud (0.006% error)}$$

A timing error under 2 percent is acceptable for asynchronous serial communication. Errors exceeding 3 percent cause cumulative phase drift and trigger framing errors.

Practice Exercise

Calculate the integer counter divisor and percentage error for a 16 MHz clock targeting 9600 baud. Confirm whether this error remains within acceptable UART tolerances.

Solution

Calculate the counter divisor:

$$\text{DIVISOR} = \lfloor 16{,}000{,}000 / 9600 \rfloor = 1666$$

Compute the actual baud rate:

$$\text{Actual Rate} = \frac{16{,}000{,}000}{1666} \approx 9603.84 \text{ baud}$$

Calculate the percentage error:

$$\text{Error} = \left| \frac{9603.84 - 9600}{9600} \right| \times 100% \approx 0.04%$$

An error of 0.04 percent is well below the 2 percent operational limit. The configuration will communicate reliably without framing errors.

Modulo-N counters generate periodic timing ticks for asynchronous serial controllers. Transmitters utilize single bit-rate ticks, while receivers use 16x oversampled ticks for midpoint sampling. The next article constructs the transmitter and receiver finite state machines.

← Project 2, Part 1: The UART Protocol Project 2, Part 3: The Transmitter and Receiver FSMs →