forked from tanchou/Verilog
- Implemented the uart_tx module for UART transmission with configurable clock frequency and baud rate. - Added a testbench (uart_tx_tb) to verify the functionality of the uart_tx module, including signal generation for start, data, and clock. - Created a backup of the previous testbench (uart_tx_tb_old) for reference.
57 lines
1.9 KiB
Verilog
57 lines
1.9 KiB
Verilog
module uart_tx(
|
|
input wire clk,
|
|
input wire start, // Signal de démarrage de la transmission
|
|
input wire [7:0] data, // Données à transmettre
|
|
output reg tx, // Sortie de transmission
|
|
output reg busy // Indicateur de transmission en cours
|
|
);
|
|
|
|
parameter CLK_FREQ = 27_000_000;
|
|
parameter BAUD_RATE = 115_200;
|
|
|
|
localparam BIT_PERIOD = CLK_FREQ / BAUD_RATE;
|
|
|
|
reg [3:0] bit_index;
|
|
reg [15:0] clk_count;
|
|
reg [7:0] tx_data = 0;
|
|
|
|
initial begin
|
|
tx = 1; // État idle (1)
|
|
busy = 0; // Pas de transmission en cours
|
|
end
|
|
|
|
always @(posedge clk) begin
|
|
if (start && !busy) begin
|
|
|
|
busy <= 1; // Démarrer la transmission
|
|
bit_index <= 0; // Réinitialiser l'index du bit
|
|
clk_count <= 0; // Réinitialiser le compteur d'horloge
|
|
tx_data <= data;
|
|
tx <= 1; // État idle (1)
|
|
|
|
end else if (busy) begin
|
|
|
|
if (clk_count < BIT_PERIOD - 1) begin
|
|
clk_count <= clk_count + 1;
|
|
|
|
end else begin
|
|
clk_count <= 0;
|
|
|
|
if (bit_index == 0) begin
|
|
tx <= 0; // Start bit (0)
|
|
end else if (bit_index < 9) begin
|
|
tx <= tx_data[bit_index - 1]; // Transmettre les données (8 bits)
|
|
end else if (bit_index == 9) begin
|
|
tx <= 1; // Stop bit (1)
|
|
end else begin
|
|
busy <= 0; // Fin de la transmission
|
|
end
|
|
|
|
bit_index <= bit_index + 1; // Passer au bit suivant
|
|
end
|
|
|
|
end else begin
|
|
tx <= 1; // État idle (1)
|
|
end
|
|
end
|
|
endmodule |