-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart_comm.v
194 lines (173 loc) · 4.07 KB
/
uart_comm.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
`timescale 1ps / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2017/06/15 07:07:54
// Design Name:
// Module Name: uart_comm
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module uart_comm
#(
parameter BAUDRATE = 9600,
parameter CLOCKRATE = 100000000
)(
input CLK,
input RST,
input send_flag,
input [7:0] send_data,
input recv_flag,
output [7:0] recv_data,
output sendable,
output receivable,
output reg Tx,
input Rx
);
reg recv_write_flag;
reg [7:0] recv_write_data;
wire recv_empty, recv_full;
fifo #(.WIDTH(8)) recv_buffer(CLK, RST, recv_flag, recv_data, recv_write_flag, recv_write_data, recv_empty, recv_full);
reg send_read_flag;
wire [7:0] send_read_data;
reg [7:0] send_read_data_buf;
wire send_empty, send_full;
fifo #(.WIDTH(8)) send_buffer(CLK, RST, send_read_flag, send_read_data, send_flag, send_data, send_empty, send_full);
assign receivable = !recv_empty;
assign sendable = !send_full;
localparam SAMPLE_INTERVAL = CLOCKRATE / BAUDRATE;
localparam STATUS_IDLE = 0;
localparam STATUS_BEGIN = 1;
localparam STATUS_DATA = 2;
localparam STATUS_VALID = 4;
localparam STATUS_END = 8;
reg [3:0] recv_status;
reg [3:0] recv_bit;
reg recv_parity;
integer recv_counter;
reg recv_clock;
wire sample = recv_counter == SAMPLE_INTERVAL / 2;
always @(posedge CLK or posedge RST) begin
if(RST) begin
recv_write_flag <= 0;
recv_write_data <= 0;
recv_status <= STATUS_IDLE;
recv_bit <= 0;
recv_parity <= 0;
recv_counter <= 0;
recv_clock <= 0;
end else begin
recv_write_flag <= 0;
if(recv_clock) begin
if(recv_counter == SAMPLE_INTERVAL - 1)
recv_counter <= 0;
else
recv_counter <= recv_counter + 1;
end
if(recv_status == STATUS_IDLE) begin
if(!Rx) begin
recv_status <= STATUS_BEGIN;
recv_counter <= 0;
recv_clock <= 1;
end
end else if(sample) begin
case(recv_status)
STATUS_BEGIN:begin
if(!Rx) begin
recv_status <= STATUS_DATA;
recv_bit <= 0;
recv_parity <= 0;
end else begin
recv_status <= STATUS_IDLE;
recv_clock <= 0;
end
end
STATUS_DATA:begin
recv_parity <= recv_parity ^ Rx;
recv_write_data[recv_bit] <= Rx;
recv_bit <= recv_bit + 1;
if(recv_bit == 8)
recv_status <= STATUS_VALID;
end
STATUS_VALID:begin
if(recv_parity == Rx && !recv_full)
recv_write_flag <= 1;
recv_status <= STATUS_END;
end
STATUS_END: begin
recv_status <= STATUS_IDLE;
recv_clock <= 0;
end
endcase
end
end
end
integer counter;
always @(posedge CLK or posedge RST) begin
if(RST) begin
counter <= 0;
end else begin
counter <= counter + 1;
if(counter == SAMPLE_INTERVAL - 1)
counter <= 0;
end
end
reg [3:0] send_status;
reg [3:0] send_bit;
reg send_parity;
reg tosend;
always @(posedge CLK or posedge RST) begin
if(RST) begin
send_read_flag <= 0;
send_read_data_buf <= 0;
send_status <= STATUS_IDLE;
send_bit <= 0;
send_parity <= 0;
tosend <= 0;
Tx <= 1;
end else begin
send_read_flag <= 0;
#1;
if(counter == 0) begin
case(send_status)
STATUS_IDLE:begin
if(!send_empty) begin
send_read_data_buf <= send_read_data;
send_read_flag <= 1;
Tx <= 0;
send_status <= STATUS_DATA;
send_bit <= 0;
send_parity <= 0;
end
end
STATUS_DATA:begin
Tx <= send_read_data_buf[send_bit];
send_parity <= send_parity ^ send_read_data_buf[send_bit];
send_bit <= send_bit + 1;
if(send_bit == 8)
send_status <= STATUS_VALID;
end
STATUS_VALID:begin
Tx <= send_parity;
send_status <= STATUS_END;
end
STATUS_END:begin
Tx <= 1;
send_status <= STATUS_IDLE;
tosend = 0;
end
endcase
end
end
end
endmodule