2023-05-06 13:47:13 +08:00
|
|
|
module UART_TX (
|
|
|
|
input clk ,
|
|
|
|
input clk_uart,
|
|
|
|
input RSTn ,
|
|
|
|
input [7:0] data ,
|
|
|
|
input tx_en ,
|
|
|
|
output reg TXD ,
|
|
|
|
output wire state ,
|
|
|
|
output wire bps_en
|
2022-04-01 22:20:50 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
//FIFO 8bit-16depth
|
2023-05-06 13:47:13 +08:00
|
|
|
wire FIFOrd_en;
|
|
|
|
wire FIFOwr_en;
|
|
|
|
wire [7:0] FIFOdata ;
|
|
|
|
wire FIFOempty;
|
|
|
|
wire FIFOfull ;
|
|
|
|
FIFO FIFO (
|
|
|
|
.clock(clk ),
|
|
|
|
.sclr (RSTn ),
|
|
|
|
.rdreq(FIFOrd_en),
|
|
|
|
.wrreq(FIFOwr_en),
|
|
|
|
.full (FIFOfull ),
|
|
|
|
.empty(FIFOempty),
|
|
|
|
.data (data ),
|
|
|
|
.q (FIFOdata )
|
|
|
|
);
|
2022-04-01 22:20:50 +08:00
|
|
|
|
|
|
|
//FIFO write control
|
2023-05-06 13:47:13 +08:00
|
|
|
assign FIFOwr_en = (~FIFOfull) & tx_en;
|
2022-04-01 22:20:50 +08:00
|
|
|
|
2023-05-06 13:47:13 +08:00
|
|
|
assign state = FIFOfull;
|
2022-04-01 22:20:50 +08:00
|
|
|
|
2023-05-06 13:47:13 +08:00
|
|
|
//UART TX
|
|
|
|
reg counter_en;
|
|
|
|
reg [3:0] counter ;
|
2022-04-01 22:20:50 +08:00
|
|
|
|
2023-05-06 13:47:13 +08:00
|
|
|
wire trans_finish;
|
|
|
|
assign trans_finish = (counter == 4'hb);
|
2022-04-01 22:20:50 +08:00
|
|
|
|
2023-05-06 13:47:13 +08:00
|
|
|
wire trans_start;
|
|
|
|
assign trans_start = (~FIFOempty) & (~counter_en);
|
2022-04-01 22:20:50 +08:00
|
|
|
|
2023-05-06 13:47:13 +08:00
|
|
|
always@(posedge clk or negedge RSTn) begin
|
|
|
|
if(~RSTn) counter_en <= 1'b0;
|
|
|
|
else if(trans_start) counter_en <= 1'b1;
|
|
|
|
else if(trans_finish) counter_en <= 1'b0;
|
|
|
|
end
|
2022-04-01 22:20:50 +08:00
|
|
|
|
2023-05-06 13:47:13 +08:00
|
|
|
always@(posedge clk or negedge RSTn) begin
|
|
|
|
if(~RSTn) counter <= 4'h0;
|
|
|
|
else if(counter_en) begin
|
|
|
|
if(clk_uart) counter <= counter + 1'b1;
|
|
|
|
else if(trans_finish) counter <= 4'h0;
|
|
|
|
end
|
2022-04-01 22:20:50 +08:00
|
|
|
end
|
|
|
|
|
2023-05-06 13:47:13 +08:00
|
|
|
assign bps_en = counter_en;
|
2022-04-01 22:20:50 +08:00
|
|
|
|
2023-05-06 13:47:13 +08:00
|
|
|
wire [9:0] data_formed;
|
2022-04-01 22:20:50 +08:00
|
|
|
|
2023-05-06 13:47:13 +08:00
|
|
|
assign data_formed = {1'b1,FIFOdata,1'b0};
|
2022-04-01 22:20:50 +08:00
|
|
|
|
2023-05-06 13:47:13 +08:00
|
|
|
always@(posedge clk or negedge RSTn) begin
|
|
|
|
if(~RSTn) TXD <= 1'b1;
|
|
|
|
else if(counter_en) begin
|
|
|
|
if(clk_uart && (counter <= 4'h9)) TXD <= data_formed[counter];
|
|
|
|
end else TXD <= 1'b1;
|
|
|
|
end
|
2022-04-01 22:20:50 +08:00
|
|
|
|
|
|
|
//FIFO read control
|
2023-05-06 13:47:13 +08:00
|
|
|
assign FIFOrd_en = (~FIFOempty) & trans_finish;
|
2022-04-01 22:20:50 +08:00
|
|
|
|
|
|
|
endmodule
|