-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuart_io.v
180 lines (168 loc) · 4.21 KB
/
uart_io.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
// SPDX-License-Identifier: BSD-2-Clause
// Copyright (c) 2019 miya All rights reserved.
module uart_io
#(
parameter CLK_HZ = 50000000,
parameter SCLK_HZ = 115200
)
(
input clk,
input reset,
input uart_rxd,
input [7:0] tx_data,
input tx_we,
output uart_txd,
output uart_busy,
output reg [31:0] rx_addr,
output reg [31:0] rx_data,
output reg rx_we
);
localparam TRUE = 1'b1;
localparam FALSE = 1'b0;
localparam ONE = 1'd1;
localparam ZERO = 1'd0;
localparam UART_WIDTH = 8;
reg uart_start;
reg [UART_WIDTH-1:0] uart_data_tx;
wire uart_re;
reg io_we_p1;
localparam IO_UART_IF_START_BYTE = 8'b10101010;
localparam IO_UART_IF_END_BYTE = 8'b01010101;
wire [UART_WIDTH-1:0] uart_data_rx;
reg [7:0] rx_buf [0:9];
reg [3:0] rx_buf_state;
reg uart_re_d1;
wire uart_re_posedge;
/* uart_io spec
start byte [0]: 10101010
addr [4][3][2][1]
data [8][7][6][5]
end byte [9]: 01010101
*/
// uart read packet
always @(posedge clk)
begin
uart_re_d1 <= uart_re;
end
assign uart_re_posedge = ((uart_re == TRUE) && (uart_re_d1 == FALSE)) ? TRUE : FALSE;
always @(posedge clk)
begin
if (reset == TRUE)
begin
rx_buf_state <= 4'd0;
io_we_p1 <= FALSE;
end
else
begin
if (uart_re_posedge == TRUE)
begin
rx_buf[rx_buf_state] <= uart_data_rx;
case (rx_buf_state)
4'd0:
begin
// check start byte
if (uart_data_rx == IO_UART_IF_START_BYTE)
begin
rx_buf_state <= rx_buf_state + ONE;
io_we_p1 <= FALSE;
end
else
begin
rx_buf_state <= 4'd0;
io_we_p1 <= FALSE;
end
end
4'd9:
begin
// check end byte
rx_buf_state <= 4'd0;
if (uart_data_rx == IO_UART_IF_END_BYTE)
begin
io_we_p1 <= TRUE;
end
else
begin
io_we_p1 <= FALSE;
end
end
default:
begin
rx_buf_state <= rx_buf_state + ONE;
io_we_p1 <= FALSE;
end
endcase
end
else
begin
io_we_p1 <= FALSE;
end
end
end
always @(posedge clk)
begin
if (reset == TRUE)
begin
rx_we <= FALSE;
end
else
begin
rx_we <= io_we_p1;
end
end
always @(posedge clk)
begin
if (reset == TRUE)
begin
rx_addr <= ZERO;
rx_data <= ZERO;
end
else
begin
if (io_we_p1 == TRUE)
begin
rx_addr <= {rx_buf[4], rx_buf[3], rx_buf[2], rx_buf[1]};
rx_data <= {rx_buf[8], rx_buf[7], rx_buf[6], rx_buf[5]};
end
end
end
// uart send byte
always @(posedge clk)
begin
if (reset == TRUE)
begin
uart_data_tx <= ZERO;
uart_start <= FALSE;
end
else
begin
if ((tx_we == TRUE) && (uart_busy == FALSE))
begin
uart_data_tx <= tx_data;
uart_start <= TRUE;
end
else
begin
uart_data_tx <= ZERO;
uart_start <= FALSE;
end
end
end
uart
#(
.CLK_HZ (CLK_HZ),
.SCLK_HZ (SCLK_HZ),
.WIDTH (UART_WIDTH)
)
uart_0
(
.clk (clk),
.reset (reset),
.rxd (uart_rxd),
.start (uart_start),
.data_tx (uart_data_tx),
.txd (uart_txd),
.busy (uart_busy),
.re (uart_re),
.data_rx (uart_data_rx)
);
endmodule