-
Notifications
You must be signed in to change notification settings - Fork 2
/
uart.v
248 lines (231 loc) · 5.91 KB
/
uart.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// SPDX-License-Identifier: BSD-2-Clause
// Copyright (c) 2017 miya All rights reserved.
/*
Usage:
Tx: Wait for 'busy' == 0, set 'data_tx' and set 1 to the 'start' signal.
Rx: Read 'data_rx' at the rising edge of the 're' signal
ver. 2024/06/01
*/
module uart
#(
parameter CLK_HZ = 50000000,
parameter SCLK_HZ = 115200,
parameter WIDTH = 8
)
(
input wire clk,
input wire reset,
input wire rxd,
input wire start,
input wire [WIDTH-1:0] data_tx,
output wire txd,
output reg busy,
output reg re,
output reg [WIDTH-1:0] data_rx
);
localparam TRUE = 1'b1;
localparam FALSE = 1'b0;
localparam ONE = 1'd1;
localparam ZERO = 1'd0;
localparam SHIFT_REG_DEPTH = 3;
localparam WAIT = (CLK_HZ / SCLK_HZ - 1);
localparam WAIT_HALF = ((CLK_HZ / SCLK_HZ) / 2 - 1);
localparam COUNTER_WIDTH = $clog2(WAIT + 2);
localparam START_BIT = 1'b0;
localparam STOP_BIT = 1'b1;
reg [WIDTH+1:0] data_tx_buf;
reg [WIDTH+1:0] data_rx_buf;
reg [SHIFT_REG_DEPTH:0] rxd_shift_reg;
wire rxd_sync;
reg tx_state;
reg rx_state;
reg [COUNTER_WIDTH-1:0] tx_counter;
reg [COUNTER_WIDTH-1:0] rx_counter;
reg [WIDTH:0] txd_counter;
reg [WIDTH:0] rxd_counter;
// tx
assign txd = data_tx_buf[txd_counter];
always @(posedge clk)
begin
if (busy == FALSE)
begin
data_tx_buf <= {STOP_BIT, data_tx, START_BIT};
end
else
begin
data_tx_buf <= data_tx_buf;
end
end
always @(posedge clk)
begin
if ((busy == TRUE) && (tx_counter != ZERO))
begin
tx_counter <= tx_counter - ONE;
end
else
begin
tx_counter <= WAIT;
end
end
always @(posedge clk)
begin
if (reset == TRUE)
begin
txd_counter <= (WIDTH + 1);
busy <= FALSE;
tx_state <= ZERO;
end
else
begin
case (tx_state)
ZERO:
begin
if ((start == TRUE) && (busy == FALSE))
begin
txd_counter <= ZERO;
busy <= TRUE;
tx_state <= ONE;
end
else
begin
txd_counter <= (WIDTH + 1);
busy <= FALSE;
tx_state <= ZERO;
end
end
ONE:
begin
if (tx_counter == ZERO)
begin
if (txd_counter == (WIDTH + 1))
begin
txd_counter <= (WIDTH + 1);
busy <= FALSE;
tx_state <= ZERO;
end
else
begin
txd_counter <= txd_counter + ONE;
busy <= TRUE;
tx_state <= ONE;
end
end
else
begin
txd_counter <= txd_counter;
busy <= TRUE;
tx_state <= ONE;
end
end
endcase
end
end
// sync rxd
always @(posedge clk)
begin
if (reset == TRUE)
begin
rxd_shift_reg <= ZERO;
end
else
begin
rxd_shift_reg <= {rxd_shift_reg, ~rxd};
end
end
assign rxd_sync = ~rxd_shift_reg[SHIFT_REG_DEPTH];
// rx
always @(posedge clk)
begin
if (reset == TRUE)
begin
rx_counter <= ZERO;
end
else
begin
if ((rx_state != ZERO) && (rx_counter != ZERO))
begin
rx_counter <= rx_counter - ONE;
end
else
begin
if (rx_state == ZERO)
begin
rx_counter <= WAIT_HALF;
end
else
begin
rx_counter <= WAIT;
end
end
end
end
always @(posedge clk)
begin
if (rx_state == ZERO)
begin
rxd_counter <= ZERO;
end
else
begin
if ((rx_counter == ZERO) && (rxd_counter != (WIDTH + 1)))
begin
rxd_counter <= rxd_counter + ONE;
end
else
begin
rxd_counter <= rxd_counter;
end
end
end
always @(posedge clk)
begin
if (reset == TRUE)
begin
rx_state <= ZERO;
data_rx_buf <= ZERO;
re <= FALSE;
data_rx <= ZERO;
end
else
begin
case (rx_state)
ZERO:
begin
if (rxd_sync == START_BIT)
begin
re <= FALSE;
rx_state <= ONE;
end
else
begin
re <= re;
rx_state <= ZERO;
end
end
ONE:
begin
if (rx_counter == ZERO)
begin
if (rxd_counter == (WIDTH + 1))
begin
re <= TRUE;
rx_state <= ZERO;
data_rx <= data_rx_buf[WIDTH:1];
end
else
begin
data_rx_buf[rxd_counter] <= rxd_sync;
re <= FALSE;
rx_state <= ONE;
end
end
else
begin
re <= FALSE;
rx_state <= ONE;
end
end
endcase
end
end
endmodule