-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuart_tx.v
More file actions
100 lines (79 loc) · 1.83 KB
/
Copy pathuart_tx.v
File metadata and controls
100 lines (79 loc) · 1.83 KB
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
93
94
95
96
97
98
99
`timescale 1ns / 1ns
module uart_tx(
i_clk,
i_reset,
i_dat,
i_fifo_push,
o_fifo_full,
tx
);
input i_clk;
input i_reset;
input [7:0] i_dat;
input i_fifo_push;
output o_fifo_full;
output tx;
parameter SYS_CLK = 'd25_000_000;
parameter BAUDRATE = 'd115200;
localparam TICK = (SYS_CLK/BAUDRATE);
//---------------------------------------------
// fifo
wire [7:0] fifo_dat;
wire fifo_pop;
wire fifo_empty;
fifo8 #(.DEPTH(2)) fifo0(
.i_clk(i_clk),
.i_reset(i_reset),
.i_dat(i_dat),
.o_dat(fifo_dat),
.i_push(i_fifo_push),
.i_pop(fifo_pop),
.o_empty(fifo_empty),
.o_full(o_fifo_full)
);
//---------------------------------------------
// uart tx
// tx baudrate generator
reg [8:0] baud_tx;
wire tick_tx = (baud_tx[8:0] == TICK[8:0]);
wire start_tx = (state_tx == IDLE) && ~fifo_empty;
always @(posedge i_clk) begin
if(tick_tx || start_tx) begin
baud_tx <= 0;
end else begin
baud_tx <= baud_tx + 1;
end
end
localparam
SEND = 4'd0,
STOPBIT1 = 4'd8,
STOPBIT2 = 4'd9,
IDLE = 4'd10,
STARTBIT = 4'd11;
reg [3:0] state_tx = IDLE;
wire [2:0] bit_idx = state_tx[2:0];
assign tx = (state_tx < STOPBIT1) ? fifo_dat[ bit_idx ] :
(state_tx == STARTBIT) ? 1'b0 : // start bit
1'b1; // idle & stop bit
assign fifo_pop = (state_tx == STOPBIT1);
always @(posedge i_clk)
begin
case( state_tx )
IDLE: // idle, wait for data in fifo
if( ~fifo_empty ) begin
state_tx <= STARTBIT;
end
STARTBIT: // start bit
if( tick_tx ) begin
state_tx <= SEND;
end
default:
if( tick_tx ) begin
state_tx <= state_tx + 1;
end
endcase
if( i_reset ) begin
state_tx <= IDLE;
end
end
endmodule