FPGA_module/uart/uart_rx.v

93 lines
2.2 KiB
Verilog

module uart_rx(
input wire clk,
input wire rst_n,
input wire rx_en,
input wire rx,
output reg rx_rdy,
output reg [7:0] rx_data
);
reg [7:0] rx_reg;
reg [3:0] rx_cnt;
reg [1:0] state;
parameter STOP = 2'd0, IDLE = 2'd1, DATA = 2'd2;
always @(posedge clk or negedge rst_n) begin
if(~rst_n) begin
state <= IDLE;
end
else begin
if(rx_en==1'b0) begin
state <= STOP;
end
else begin
case(state)
STOP: begin
state <= IDLE;
end
IDLE: begin
if(rx==1'b0) begin
state <= DATA;
end
else begin
state <= IDLE;
end
end
DATA: begin
if(rx_cnt==4'd8) begin
state <= IDLE;
end
else begin
state <= DATA;
end
end
default: begin
state <= state;
end
endcase
end
end
end
always @(posedge clk or negedge rst_n) begin
if(~rst_n) begin
rx_cnt <= 4'd0;
rx_reg <= 8'd0;
rx_data <= 8'd0;
rx_rdy <= 1'b0;
end
else begin
case(state)
IDLE: begin
rx_cnt <= 4'd0;
rx_reg <= 8'd0;
rx_data <= rx_data;
rx_rdy <= 1'b0;
end
DATA: begin
if(rx_cnt<4'd8) begin
rx_reg[rx_cnt] <= rx;
rx_cnt <= rx_cnt + 1'b1;
end
else begin
rx_reg <= rx_reg;
rx_cnt <= rx_cnt;
end
if(rx_cnt==4'd8) begin
rx_data <= rx_reg;
rx_rdy <= 1'b1;
end
else begin
rx_data <= rx_data;
rx_rdy <= 1'b0;
end
end
default : begin
rx_reg <= rx_reg;
rx_cnt <= rx_cnt;
rx_data <= rx_data;
rx_rdy <= rx_rdy;
end
endcase
end
end
endmodule