2024-08-04 06:24:44 +00:00
|
|
|
module uart_rx(
|
|
|
|
input wire clk,
|
|
|
|
input wire rst_n,
|
|
|
|
input wire rx_en,
|
2024-08-08 14:49:44 +00:00
|
|
|
input wire rx_pin,
|
2024-08-04 06:24:44 +00:00
|
|
|
output reg rx_rdy,
|
|
|
|
output reg [7:0] rx_data
|
|
|
|
);
|
|
|
|
|
2024-08-08 14:49:44 +00:00
|
|
|
parameter DIV = 50;
|
|
|
|
parameter COUNT_WITH = 6;
|
|
|
|
reg [COUNT_WITH-1:0] count;
|
|
|
|
|
|
|
|
|
2024-08-04 06:24:44 +00:00
|
|
|
reg [7:0] rx_reg;
|
|
|
|
reg [3:0] rx_cnt;
|
2024-08-08 14:49:44 +00:00
|
|
|
reg[1:0] rx_state;
|
2024-08-04 06:24:44 +00:00
|
|
|
always @(posedge clk or negedge rst_n) begin
|
|
|
|
if(~rst_n) begin
|
2024-08-08 14:49:44 +00:00
|
|
|
count <= 0;
|
|
|
|
rx_state <= 0;
|
|
|
|
rx_cnt <= 0;
|
|
|
|
rx_rdy <= 1'd0;
|
|
|
|
rx_reg <= 8'd0;
|
|
|
|
rx_data <= 8'd0;
|
|
|
|
end else begin
|
|
|
|
if(rx_en == 0) begin
|
|
|
|
count <= 0;
|
|
|
|
rx_state <= 0;
|
|
|
|
rx_cnt <= 0;
|
|
|
|
rx_rdy <= 1'd0;
|
|
|
|
rx_reg <= 8'd0;
|
|
|
|
rx_data <= 8'd0;
|
|
|
|
end else begin
|
|
|
|
case(rx_state)
|
|
|
|
0: begin
|
|
|
|
if(rx_pin == 0) begin
|
|
|
|
rx_state <= 1'd1;
|
|
|
|
count <= 1;
|
2024-08-04 06:24:44 +00:00
|
|
|
end
|
|
|
|
end
|
2024-08-08 14:49:44 +00:00
|
|
|
1: begin
|
|
|
|
if(count == (DIV-1)) begin
|
|
|
|
rx_state <= 2;
|
|
|
|
rx_cnt <= 0;
|
|
|
|
count <= 0;
|
|
|
|
rx_reg <= 8'd0;
|
|
|
|
end else begin
|
|
|
|
count <= count + 1'd1;
|
2024-08-04 06:24:44 +00:00
|
|
|
end
|
2024-08-08 14:49:44 +00:00
|
|
|
end
|
|
|
|
2: begin
|
|
|
|
if(count == (DIV-1)) begin
|
|
|
|
count <= 0;
|
|
|
|
if(rx_cnt == 8) begin
|
|
|
|
rx_state <= 3;
|
|
|
|
end
|
|
|
|
end else if(count == (DIV/2-1))begin
|
|
|
|
rx_reg[rx_cnt] <= rx_pin;
|
|
|
|
rx_cnt <= rx_cnt + 1'd1;
|
|
|
|
count <= count + 1'd1;
|
|
|
|
end else begin
|
|
|
|
count <= count + 1'd1;
|
2024-08-04 06:24:44 +00:00
|
|
|
end
|
|
|
|
end
|
2024-08-08 14:49:44 +00:00
|
|
|
3: begin
|
|
|
|
if(count == (DIV-1)) begin
|
|
|
|
count <= 0;
|
|
|
|
rx_state <= 0;
|
|
|
|
rx_rdy <= 1'd0;
|
|
|
|
end else begin
|
|
|
|
count <= count + 1'd1;
|
|
|
|
rx_rdy <= 1'd1;
|
|
|
|
rx_data <= rx_reg;
|
|
|
|
end
|
2024-08-04 06:24:44 +00:00
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-08-08 14:49:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-04 06:24:44 +00:00
|
|
|
endmodule
|