2025-05-05 14:52:01 +02:00
|
|
|
|
module top_uart_loopback (
|
|
|
|
|
input wire clk, // 27 MHz
|
|
|
|
|
input wire rx,
|
|
|
|
|
output wire tx,
|
|
|
|
|
output reg [5:0] leds
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
wire rx_received;
|
|
|
|
|
wire [7:0] rx_data;
|
|
|
|
|
reg [7:0] tx_data;
|
|
|
|
|
reg tx_enable;
|
|
|
|
|
|
|
|
|
|
wire tx_ready;
|
|
|
|
|
|
|
|
|
|
initial begin
|
2025-05-06 09:42:26 +02:00
|
|
|
|
leds = 6'b111111;
|
2025-05-05 14:52:01 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
// === UART RX ===
|
2025-05-07 09:46:43 +02:00
|
|
|
|
other_uart_rx uart_rx_inst (
|
2025-05-05 14:52:01 +02:00
|
|
|
|
.clk(clk),
|
2025-05-07 09:46:43 +02:00
|
|
|
|
.rst_n(1'b1),
|
2025-05-05 14:52:01 +02:00
|
|
|
|
.rx_pin(rx),
|
2025-05-07 09:46:43 +02:00
|
|
|
|
.rx_data_valid(rx_received),
|
|
|
|
|
.rx_data_ready(1'b1),
|
2025-05-05 14:52:01 +02:00
|
|
|
|
.rx_data(rx_data)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// === UART TX ===
|
2025-05-07 09:46:43 +02:00
|
|
|
|
other_uart_tx uart_tx_inst (
|
2025-05-05 14:52:01 +02:00
|
|
|
|
.clk(clk),
|
2025-05-07 09:46:43 +02:00
|
|
|
|
.rst_n(1'b1),
|
|
|
|
|
.tx_data(tx_data),
|
|
|
|
|
.tx_data_valid(tx_enable),
|
|
|
|
|
.tx_data_ready(tx_ready),
|
|
|
|
|
.tx_pin(tx)
|
2025-05-05 14:52:01 +02:00
|
|
|
|
);
|
|
|
|
|
|
2025-05-06 09:42:26 +02:00
|
|
|
|
// === FSM avec délai ===
|
|
|
|
|
localparam IDLE = 0, WAIT = 1, SEND = 2;
|
|
|
|
|
reg [1:0] state = IDLE;
|
|
|
|
|
reg [8:0] delay_counter = 0;
|
2025-05-05 14:52:01 +02:00
|
|
|
|
|
|
|
|
|
always @(posedge clk) begin
|
2025-05-05 14:54:40 +02:00
|
|
|
|
leds[5] <= rx;
|
2025-05-06 09:42:26 +02:00
|
|
|
|
leds[4] <= tx;
|
|
|
|
|
|
2025-05-05 14:52:01 +02:00
|
|
|
|
case (state)
|
|
|
|
|
IDLE: begin
|
|
|
|
|
tx_enable <= 0;
|
2025-05-06 09:42:26 +02:00
|
|
|
|
delay_counter <= 0;
|
|
|
|
|
|
2025-05-05 14:52:01 +02:00
|
|
|
|
if (rx_received && tx_ready) begin
|
|
|
|
|
tx_data <= rx_data;
|
2025-05-06 09:42:26 +02:00
|
|
|
|
state <= WAIT;
|
2025-05-07 09:46:43 +02:00
|
|
|
|
|
|
|
|
|
leds[0] <= 0;
|
|
|
|
|
leds[1] <= 1;
|
2025-05-06 09:42:26 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
WAIT: begin
|
2025-05-07 09:46:43 +02:00
|
|
|
|
if (tx_ready) begin
|
|
|
|
|
tx_enable <= 1;
|
2025-05-05 14:52:01 +02:00
|
|
|
|
state <= SEND;
|
2025-05-06 09:42:26 +02:00
|
|
|
|
end else begin
|
2025-05-07 09:46:43 +02:00
|
|
|
|
tx_enable <= 0;
|
2025-05-05 14:52:01 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
SEND: begin
|
2025-05-07 09:46:43 +02:00
|
|
|
|
if (!tx_ready) begin // Attendre que la transmission commence
|
|
|
|
|
tx_enable <= 0;
|
|
|
|
|
end else if (tx_ready && tx_enable == 0) begin
|
|
|
|
|
state <= IDLE; // Transmission terminée, retour à l’attente
|
|
|
|
|
end
|
2025-05-05 14:52:01 +02:00
|
|
|
|
|
2025-05-06 09:42:26 +02:00
|
|
|
|
leds[0] <= 0;
|
2025-05-07 09:46:43 +02:00
|
|
|
|
leds[1] <= 0;
|
2025-05-05 14:52:01 +02:00
|
|
|
|
end
|
|
|
|
|
endcase
|
|
|
|
|
end
|
|
|
|
|
|
2025-05-07 09:46:43 +02:00
|
|
|
|
|
2025-05-06 09:42:26 +02:00
|
|
|
|
endmodule
|