L'UART (Universal Asynchronous Receiver-Transmitter) è il cuore della comunicazione seriale. Questa guida spiega come implementare un trasmettitore (TX) UART semplice, parametrizzato e sintetizzabile in Verilog per FPGA.
Principi base UART TX
- Seriale: 1 bit alla volta su 1 filo (TX line)
- Asincrono: niente clock condiviso. Synchronizzazione via start bit + baud rate concordato
- Frame standard: 1 start bit (0) + 8 data bit (LSB first) + 1 parity bit (opzionale, even/odd/none) + 1/2 stop bit (1)
- Linea inattiva: livello alto (1)
Architettura moduli
uart_tx_top
├── baud_rate_generator (contatore: clk_sys / baud_rate)
├── tx_state_machine (IDLE, START, DATA[0-7], PARITY, STOP)
├── shift_register (11-bit: start + 8 data + parity + stop)
└── output_driver (gestisce linea TX, buffer opzionale)
Calcolo divisore baud rate
DIVISOR = FREQ_CLK_HZ / BAUD_RATE
Esempio: 50 MHz clock, 9600 baud → DIVISOR = 50.000.000 / 9600 = 5208 (arrotondato). Errore % = |actual - target| / target < 3% tollerabile.
Consiglio: usa parametro `CLK_FREQ_HZ` e `BAUD_RATE` nel modulo, calcola `localparam DIVISOR = CLK_FREQ_HZ / BAUD_RATE`.
Codice Verilog completo (trasmettitore parametrico)
module uart_tx #(
parameter integer CLK_FREQ_HZ = 50_000_000,
parameter integer BAUD_RATE = 9600,
parameter integer DATA_BITS = 8,
parameter PARITY_EN = 1'b0, // 0=none, 1=even
parameter integer STOP_BITS = 1 // 1 o 2
) (
input logic clk,
input logic rst_n, // active low reset
input logic tx_start, // pulse 1 clk: avvia trasmissione
input logic [DATA_BITS-1:0] tx_data, // byte da trasmettere
output logic tx_serial, // linea TX seriale
output logic tx_busy, // alto durante trasmissione
output logic tx_done // pulse 1 clk: fine trasmissione
);
localparam integer DIVISOR = CLK_FREQ_HZ / BAUD_RATE;
localparam integer DIV_W = $clog2(DIVISOR);
// Baud rate tick generator
logic [DIV_W-1:0] baud_cnt;
logic baud_tick;
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
baud_cnt <= '0;
baud_tick <= 1'b0;
end else if (baud_cnt == DIVISOR-1) begin
baud_cnt <= '0;
baud_tick <= 1'b1;
end else begin
baud_cnt <= baud_cnt + 1'b1;
baud_tick <= 1'b0;
end
end
// Frame construction
localparam integer FRAME_BITS = 1 + DATA_BITS + (PARITY_EN ? 1 : 0) + STOP_BITS;
logic [FRAME_BITS-1:0] tx_shift_reg;
logic [$clog2(FRAME_BITS):0] bit_idx;
typedef enum logic [1:0] {S_IDLE, S_LOAD, S_SHIFT, S_DONE} state_t;
state_t state;
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
state <= S_IDLE;
tx_shift_reg <= {FRAME_BITS{1'b1}};
bit_idx <= '0;
tx_serial <= 1'b1;
tx_busy <= 1'b0;
tx_done <= 1'b0;
end else begin
tx_done <= 1'b0; // default
case (state)
S_IDLE: begin
tx_serial <= 1'b1;
tx_busy <= 1'b0;
if (tx_start) begin
state <= S_LOAD;
end
end
S_LOAD: begin
logic parity_bit;
parity_bit = ^tx_data; // even parity
tx_shift_reg <= { {STOP_BITS{1'b1}}, // stop bit(s) - MSB first in shift reg
(PARITY_EN ? parity_bit : 1'b1), // parity or extra stop if disabled
tx_data, // 8 data bits
1'b0 }; // start bit (LSB first out)
bit_idx <= FRAME_BITS;
tx_busy <= 1'b1;
state <= S_SHIFT;
end
S_SHIFT: begin
if (baud_tick) begin
tx_serial <= tx_shift_reg[0];
tx_shift_reg <= {1'b1, tx_shift_reg[FRAME_BITS-1:1]}; // right shift, line held high after frame
bit_idx <= bit_idx - 1'b1;
if (bit_idx == 1) begin
state <= S_DONE;
end
end
end
S_DONE: begin
tx_busy <= 1'b0;
tx_done <= 1'b1;
state <= S_IDLE;
end
endcase
end
end
endmodule
Testbench essenziale
module uart_tx_tb;
logic clk, rst_n, tx_start;
logic [7:0] tx_data;
logic tx_serial, tx_busy, tx_done;
uart_tx #(.CLK_FREQ_HZ(50_000_000), .BAUD_RATE(9600)) dut (
.clk(clk), .rst_n(rst_n), .tx_start(tx_start), .tx_data(tx_data),
.tx_serial(tx_serial), .tx_busy(tx_busy), .tx_done(tx_done)
);
always #10 clk = ~clk; // 50 MHz
initial begin
clk = 0; rst_n = 0; tx_start = 0; tx_data = 8'h00;
#100 rst_n = 1;
#100 tx_data = 8'h55; tx_start = 1; #20 tx_start = 0; // 'U' = 01010101
wait(tx_done);
#100 tx_data = 8'hAA; tx_start = 1; #20 tx_start = 0; // 10101010
wait(tx_done);
#1000 $finish;
end
endmodule
Simulazione e verifica
- Simula (ModelSim, Vivado Sim, Icarus Verilog + GTKWave)
- Verifica forma d'onda `tx_serial`: start(0) → 8 data → parity → stop(1)
- Misura periodo bit: deve essere ~104.17 µs (9600 baud)
- Testa back-to-back: `tx_start` durante `tx_busy` deve essere ignorato (o bufferizzato se aggiungi FIFO)
Estensioni comuni
- FIFO TX: bufferizza più byte (piccolo RAM/registro array + puntatori)
- Parity odd/none: parametro `PARITY_TYPE`
- 9-bit data: per indirizzamento multi-drop (es. RS-485)
- Flow control RTS/CTS: segnali aggiuntivi
- RX UART: macchina stati speculare con oversampling (16x baud tipico)
Deploy su FPGA
- Aggiungi constraint pin: `tx_serial` → pin PMOD/connettore seriale
- Clock constraint: `create_clock -period 20.000 [get_ports clk]` (50 MHz)
- Sintetizza, implementa, genera bitstream
- Testa con PuTTY / Tera Term / screen (9600 8N1) su USB-UART bridge
- Come recuperare la chiave CD di StarCraft: procedura ufficiale Blizzard
- Come rimuovere virus e adware gratis: Microsoft MRT, Malwarebytes e procedura completa
Windows 2016
- Come salvare un documento in PDF da Office, OpenOffice o LibreOffice
- Come Vedere un PDF come Presentazione a Schermo Intero con Adobe Reader
- Come reinstallare Picasa 3 (gestione foto Google)
- Come convertire video MPEG-2 in MPEG-1 per masterizzare VCD
- Simulare una gravidanza in Photoshop: profilo e fronte
- Rimuovere filigrane (watermark) da file PDF: metodi e strumenti
- Come spostare un file PDF sul desktop per stamparlo o archiviarlo facilmente
- Come eliminare le installazioni incomplete dal sistema Windows