Designing a UART Controller Using CPLD Logic
Moving UART logic from software drivers into a CPLD provides deterministic timing and offloads the main CPU. In high-speed embedded systems, software-based bit-banging often fails due to interrupt latency or jitter. Implementing the UART protocol directly in hardware ensures consistent baud rates and reliable data framing.
🏗️ Core Architecture Overview #
A standard UART controller in a CPLD consists of three primary functional blocks: the Baud Rate Generator, the Transmitter (TX), and the Receiver (RX).
Baud Rate Generation #
The CPLD operates on a high-frequency system clock. To achieve a specific baud rate (e.g., 115200 bps), the logic must divide the system clock.
The formula is straightforward: $Divisor = \frac{Clock Frequency}{Baud Rate}$
For example, with a 50MHz clock and a 115200 baud rate, the divisor is approximately 434. The CPLD uses a counter to track these cycles, triggering a “tick” every time a bit period elapses.
Transmitter (TX) Logic #
The TX block is a state machine that manages the serial output. It takes a parallel byte from a register and shifts it out bit by bit.
- IDLE State: The TX line remains high.
- START Bit: The line pulls low for one bit period.
- Data Bits: The logic shifts out 8 bits (or 7, depending on configuration).
- STOP Bit: The line returns to high for at least one bit period.
📡 Receiver (RX) Logic and Sampling #
The RX logic is more complex because it must handle asynchronous signals. Since the sender and receiver clocks are never perfectly synchronized, the receiver must sample the incoming line at the center of each bit period to minimize the impact of clock drift.
Start Bit Detection #
The RX logic constantly monitors the input line. When a falling edge is detected (transition from high to low), the baud rate counter resets. This marks the beginning of the start bit.
Mid-bit Sampling Strategy #
To ensure reliability, the controller waits for 1.5 bit periods after the start bit before taking the first sample. It then samples every 1 bit period thereafter. By sampling in the middle of the bit, the system avoids the transition edges where the signal might be unstable.
If the sampled value is low, the bit is recorded as a ‘0’; if high, it is a ‘1’.
⚙️ State Machine Implementation #
Implementing these behaviors requires a Finite State Machine (FSM). Below is a simplified Verilog representation of a UART TX state machine.
module uart_tx (
input clk,
input reset,
input start,
input [7:0] data_in,
output reg tx_out
);
// State definitions
parameter IDLE = 2'b00, START = 2'b01, DATA = 2'b10, STOP = 2'b11;
reg [1:0] state;
reg [3:0] bit_idx;
reg [7:0] shift_reg;
always @(posedge clk or posedge reset) begin
if (reset) begin
state <= IDLE;
tx_out <= 1'b1;
end else begin
case (state)
IDLE: begin
tx_out <= 1'b1;
if (start) begin
shift_reg <= data_in;
state <= START;
end
end
START: begin
tx_out <= 1'b0;
state <= DATA;
bit_idx <= 0;
end
DATA: begin
tx_out <= shift_reg[0];
if (bit_idx == 7) begin
state <= STOP;
end else begin
shift_reg <= {shift_reg[6:0], 1'b0};
bit_idx <= bit_idx + 1;
end
end
STOP: begin
tx_out <= 1'b1;
state <= IDLE;
end
endcase
end
end
endmoduleIn complex systems, these CPLD-based UART blocks often interface with a Real-Time Operating System (RTOS). For instance, when integrating with VxWorks, the CPLD handles the low-level bit manipulation while the OS manages the high-level data processing and task scheduling. This separation of concerns improves overall system stability and responsiveness.