FPGA-Based Serial Communication Design: A Practical Guide
Implementing serial communication on an FPGA provides significant advantages over software-based UART implementations, particularly regarding timing precision and high-speed data throughput. By moving the bit-banging logic into hardware, you free up the CPU to handle application-level logic while the FPGA manages the physical layer.
🏗️ System Architecture Overview #
A standard UART (Universal Asynchronous Receiver-Transmitter) implementation in an FPGA consists of three primary blocks: a baud rate generator, a transmitter (TX) module, and a receiver (RX) module.
The TX module converts parallel data from the FPGA’s internal registers into a serial stream. The RX module performs the inverse, sampling the incoming serial line and reconstructing the parallel byte. Because UART is asynchronous, both sides must agree on a baud rate, which is why the baud rate generator is the most critical component for maintaining data integrity.
⏱️ Baud Rate Generation #
FPGA internal clocks typically run in the MHz or GHz range, while standard UART speeds (like 9600 or 115200 baud) are much slower. You need a clock divider to generate a “tick” that corresponds to the duration of a single bit.
To calculate the divider value, use the following logic:
Divider = Clock_Frequency / Baud_Rate
For example, if your FPGA clock is 50 MHz and you need a 9600 baud rate, the divider must produce a pulse every 5,208 clock cycles. Using a counter to track these cycles ensures that the TX and RX modules have a stable timing reference.
📤 Transmitter (TX) Logic #
The TX path is relatively straightforward. It operates as a shift register controlled by a Finite State Machine (FSM).
State Machine Flow #
- IDLE: The TX line is held high. The FSM waits for a “Start” signal from the system.
- START: The TX line is pulled low for one bit period.
- DATA: The FSM shifts out 8 bits (or 7, depending on configuration) from a parallel buffer.
- STOP: The TX line is pulled high for at least one bit period.
The shift register moves one bit per “tick” from the baud rate generator. Once the STOP bit is sent, the FSM returns to IDLE and signals the system that the transmission is complete.
📥 Receiver (RX) Logic #
The RX path is more complex because the FPGA cannot predict exactly when the start bit will arrive. The logic must detect the falling edge of the RX line and synchronize the sampling clock.
Sampling Strategy #
When the RX line drops from high to low, the RX FSM starts a counter. To ensure the data is sampled accurately, the FPGA should wait until the middle of the bit period before reading the line.
If the baud rate is 9600, and the FPGA is sampling at a higher frequency, the FSM waits for 4 or 5 “ticks” after the start bit edge before sampling the first data bit. It then samples every 16 ticks (assuming a 16x oversampling rate) to collect the full byte. This oversampling provides a buffer against slight clock drifts between the sender and receiver.
🔄 FIFO and Data Handling #
In high-speed applications, the FPGA might receive data faster than the main processor can process it. Implementing a First-In-First-Out (FIFO) buffer between the RX module and the rest of the system prevents data loss.
The RX module pushes completed bytes into the FIFO. The system can then use an interrupt or a “Full” flag to notify the processor. For complex systems requiring deterministic handling of this serial data, the FPGA can interface with a real-time operating system like VxWorks to manage the application-layer tasks.
// Simple example of a baud rate counter
reg [15:0] count;
always @(posedge clk) begin
if (count < BAUD_DIVIDER)
count <= count + 1;
else
count <= 0;
endBy offloading the bit-level timing to the FPGA, you ensure that the serial communication remains stable even when the main processor is under heavy load.