forked from nand2mario/nestang
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuart_tx_V2.v
92 lines (75 loc) · 1.67 KB
/
uart_tx_V2.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module uart_tx_V2(
input wire clk,
input wire [7:0] din,
input wire wr_en,
output wire tx_busy,
output reg tx_p
);
initial begin
tx_p = 1'b1;
end
parameter clk_freq = 27000000;
parameter uart_freq = 115200;
localparam STATE_IDLE = 2'b00;
localparam STATE_START = 2'b01;
localparam STATE_DATA = 2'b10;
localparam STATE_STOP = 2'b11;
reg[7:0] localdin;
reg localwr_en;
//always@(posedge clk)begin
always@(*)begin
localdin<=din;
localwr_en<=wr_en;
end
reg [7:0] data= 8'h00;
reg [2:0] bitpos= 3'h0;
reg [1:0] state= STATE_IDLE;
wire tx_clk;
localparam TX_CLK_MAX = (clk_freq / uart_freq)-1;
reg[$clog2(TX_CLK_MAX+1)+1:0] tx_clkcnt;
assign tx_clk = (tx_clkcnt == 0);
initial tx_clkcnt=0;
always @(posedge clk) begin
if (tx_clkcnt >= TX_CLK_MAX)
tx_clkcnt <= 0;
else
tx_clkcnt <= tx_clkcnt + 1;
end
always @(posedge clk) begin
case (state)
STATE_IDLE: begin
if (localwr_en) begin
state <= STATE_START;
data <= localdin;
bitpos <= 3'h0;
end
end
STATE_START: begin
if (tx_clk) begin
tx_p <= 1'b0;
state <= STATE_DATA;
end
end
STATE_DATA: begin
if (tx_clk) begin
if (bitpos == 3'h7)
state <= STATE_STOP;
else
bitpos <= bitpos + 3'h1;
tx_p <= data[bitpos];
end
end
STATE_STOP: begin
if (tx_clk) begin
tx_p <= 1'b1;
state <= STATE_IDLE;
end
end
default: begin
tx_p <= 1'b1;
state <= STATE_IDLE;
end
endcase
end
assign tx_busy = (state != STATE_IDLE);
endmodule