-
Notifications
You must be signed in to change notification settings - Fork 1
/
uart.v
215 lines (187 loc) · 5.59 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
module uart_tx(
input [7:0] data_bus,
input byte_ready,
input ld_tx_datareg,
input t_byte,
input clk,
input reset_,
output serial_out
);
reg [7:0] tx_datareg;
reg [8:0] tx_shiftreg;
reg ld_tx_shiftreg;
reg [2:0] state, next_state;
reg [2:0] bit_count;
reg clr;
reg shift;
reg start;
parameter idle = 3'b001;
parameter waiting = 3'b010;
parameter sending = 3'b100;
parameter all_ones = 9'b111111111;
assign serial_out = tx_shiftreg[0]; //Output is LSB of shift register
always @ (state or bit_count or t_byte or byte_ready)
begin
ld_tx_shiftreg = 0;
clr = 0;
shift = 0;
start = 0;
next_state = state;
case(state)
idle:
if(byte_ready == 1)
begin
ld_tx_shiftreg = 1;
next_state = waiting;
end
waiting:
if(t_byte == 1)
begin
start = 1;
next_state = sending;
end
sending:
if(bit_count != 9)
shift = 1;
else
begin
clr = 1;
next_state = idle;
end
default: next_state = idle;
endcase
end
always @ (posedge clk or negedge reset_)
begin
if(reset_ == 0)
state <= idle;
else
state <= next_state;
end
always @ (posedge clk or negedge reset_)
begin
if(reset_ == 0)
begin
tx_shiftreg <= all_ones;
bit_count <= 0; end
else
begin
if(ld_tx_datareg == 1)
tx_datareg <= data_bus;
if(ld_tx_shiftreg == 1)
tx_shiftreg <= {tx_datareg, 1'b1};
if(start == 1)
tx_shiftreg[0] <= 0;
if(clr == 1)
bit_count <= 0;
else if(shift == 1)
bit_count <= bit_count+1;
if(shift == 1)
tx_shiftreg <= {1'b1, tx_shiftreg[8:1]};
end
end
endmodule
module uart_rx(
output reg [7:0] rx_datareg,
output reg error1, error2,
output rx_handshake, // read_not_ready_out
input serial_in,
input sample_clk,
input reset_,
input host_not_ready // read_not_ready_in
);
parameter idle = 2'b00;
parameter starting = 2'b01;
parameter receiving = 2'b11;
reg [7:0] rx_shiftreg;
reg [3:0] sample_counter;
reg [4:0] bit_counter;
reg [1:0] state, next_state;
reg inc_bit_counter, clr_bit_counter, inc_sample_counter, clr_sample_counter, shift, load, rx_handshake;
always @(state or serial_in or host_not_ready or sample_counter or bit_counter)
begin
rx_handshake = 0;
clr_sample_counter = 0;
clr_bit_counter = 0;
inc_sample_counter = 0;
inc_bit_counter = 0;
shift = 0;
error1 = 0;
error2 = 0;
load = 0;
next_state = state;
case (state)
idle: if(serial_in == 0) next_state = starting;
starting:
begin
if(serial_in == 1)
begin
next_state = idle;
clr_sample_counter = 1;
end
else if(sample_counter == 3) // Check if sample_counter == half_word - 1 (half_word = word_size/2 = 8/2 = 4) = 3
begin
next_state = receiving;
clr_sample_counter = 1;
end
else inc_sample_counter = 1;
end
receiving:
begin
if(sample_counter < 7) // word_size - 1
begin
inc_sample_counter = 1;
end
else
begin
clr_sample_counter = 1;
if(bit_counter != 8)
begin
shift = 1;
inc_bit_counter = 1;
end
else
begin
next_state = idle;
rx_handshake = 1;
clr_bit_counter = 1;
if(host_not_ready == 1)
error1 = 1;
else if(serial_in == 0)
error2 = 1;
else
load = 1;
end
end
end
default: next_state = idle;
endcase
end
// State transitions and register transfers
always @ (posedge sample_clk)
begin
if(reset_ == 0)
begin
state <= idle;
sample_counter <= 0;
bit_counter <= 0;
rx_datareg <= 0;
rx_shiftreg <= 0;
end
else begin
state <= next_state;
if(clr_sample_counter == 1)
sample_counter <= 0;
else if(inc_sample_counter == 1)
sample_counter <= sample_counter + 1;
if(clr_bit_counter == 1)
bit_counter <= 0;
else if(inc_bit_counter == 1)
bit_counter <= bit_counter + 1;
if(shift == 1)
rx_shiftreg <= {serial_in, rx_shiftreg[7:1]};
if(load == 1)
rx_datareg <= rx_shiftreg;
end
end
endmodule