-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuart_rx.v
More file actions
100 lines (79 loc) · 1.85 KB
/
uart_rx.v
File metadata and controls
100 lines (79 loc) · 1.85 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
`timescale 1ns / 1ns
module uart_rx(
i_clk,
i_reset,
o_dat,
rx,
received
);
input i_clk;
input i_reset;
output [7:0] o_dat;
input rx;
output reg received;
//-------------------------------------------------
// Baud generator
//
parameter SYS_CLK = 'd25_000_000;
parameter BAUDRATE = 'd115200;
localparam TICK = (SYS_CLK/BAUDRATE);
reg [8:0] baud_rx;
wire baud_start;
wire baud_reset = (baud_rx[8:0] == TICK[8:0]);
wire tick_rx = (baud_rx[8:0] == TICK[8:0]/2);
always @(posedge i_clk) begin
if( baud_start || baud_reset ) begin
baud_rx <= 0;
end else begin
baud_rx <= baud_rx + 1;
end
end
//-------------------------------------------------
// Receiver
//
reg [7:0] rx_reg;
localparam
IDLE = 10,
STARTBIT = 11,
STOPBIT = 8,
INTERRUPT = 9,
RECEIVE = 0;
reg [3:0] state_rx;
wire [2:0] bit_idx = state_rx[2:0];
assign baud_start = (state_rx == IDLE) && (rx == 1'b0);
reg [7:0] rx_buf; // temp receive buffer
always @(posedge i_clk) begin
received <= 0;
case( state_rx )
IDLE: // waiting for start bit
if( rx == 1'b0 ) begin
state_rx <= STARTBIT;
end
STARTBIT:
if( tick_rx ) begin
state_rx <= rx ? IDLE : RECEIVE;
end
STOPBIT:
if( tick_rx ) begin
state_rx <= rx ? INTERRUPT : IDLE;
end
INTERRUPT:
begin
received <= 1;
rx_reg <= rx_buf;
state_rx <= IDLE;
end
default:
if( tick_rx ) begin
rx_buf[ bit_idx ] <= rx;
state_rx <= state_rx + 1;
end
endcase
if( i_reset ) begin
state_rx <= IDLE;
end
end
//-------------------------------------------------
// bus interface
assign o_dat = rx_reg;
endmodule