-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart_rx.v
More file actions
147 lines (124 loc) · 4.41 KB
/
uart_rx.v
File metadata and controls
147 lines (124 loc) · 4.41 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
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
module uart_rx #(
parameter integer DATA_BIT = 8,
parameter integer OVERSAMPLE = 16
) (
input wire clk,
input wire rst_n,
input wire tick,
input wire rx_ready,
output reg rx_valid,
output reg [DATA_BIT-1:0] rx_data,
input wire rx_serial
);
localparam IDLE=0, START=1, DATA=2, STOP=3;
reg [DATA_BIT-1:0] shift, next_shift;
reg [2:0] bit_cnt, next_bit_cnt; // 0..7
reg [3:0] os_cnt, next_os_cnt; // 0..15
reg [1:0] sync_rx_serial;
reg [1:0] state, next_state;
reg next_rx_valid;
reg [DATA_BIT-1:0] next_rx_data;
wire rx_sync = sync_rx_serial[0];
// Sequential
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
rx_valid <= 1'b0;
rx_data <= {DATA_BIT{1'b0}};
shift <= {DATA_BIT{1'b0}};
bit_cnt <= 3'd0;
os_cnt <= 4'd0;
state <= IDLE;
sync_rx_serial <= 2'b11; // idle high
end else begin
rx_valid <= next_rx_valid;
rx_data <= next_rx_data;
shift <= next_shift;
bit_cnt <= next_bit_cnt;
os_cnt <= next_os_cnt;
state <= next_state;
// 2-flop synchronizer
sync_rx_serial[1] <= rx_serial;
sync_rx_serial[0] <= sync_rx_serial[1];
end
end
// Combinational
always @(*) begin
// defaults: hold
next_state = state;
next_shift = shift;
next_bit_cnt = bit_cnt;
next_os_cnt = os_cnt;
// hold data/valid by default
next_rx_data = rx_data;
next_rx_valid = rx_valid;
if (rx_valid && rx_ready) begin
next_rx_valid = 1'b0;
end
case (state)
IDLE: begin
next_os_cnt = 4'd0;
next_bit_cnt = 3'd0;
if (!rx_valid) begin
if (!rx_sync) begin
next_state = START;
next_os_cnt = 4'd0;
end
end
end
START: begin
if (tick) begin
if (os_cnt == 4'd8) begin
if (rx_sync) begin
next_state = IDLE;
next_os_cnt = 4'd0;
end
end
if (os_cnt == 4'd15) begin
next_state = DATA;
next_os_cnt = 4'd0;
next_bit_cnt = 3'd0;
next_shift = {DATA_BIT{1'b0}};
end else begin
next_os_cnt = os_cnt + 4'd1;
end
end
end
DATA: begin
if (tick) begin
if (os_cnt == 4'd8) begin
next_shift = shift;
next_shift[bit_cnt] = rx_sync; // LSB-first
end
if (os_cnt == 4'd15) begin
next_os_cnt = 4'd0;
if (bit_cnt == 3'd7) begin
next_bit_cnt = 3'd0;
next_state = STOP;
end else begin
next_bit_cnt = bit_cnt + 3'd1;
end
end else begin
next_os_cnt = os_cnt + 4'd1;
end
end
end
STOP: begin
if (tick) begin
if (os_cnt == 4'd15) begin
next_os_cnt = 4'd0;
next_state = IDLE;
if (!rx_valid) begin
next_rx_data = shift;
next_rx_valid = 1'b1;
end
end else begin
next_os_cnt = os_cnt + 4'd1;
end
end
end
default: begin
next_state = IDLE;
end
endcase
end
endmodule