-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUART_Rx.v
84 lines (74 loc) · 1.71 KB
/
UART_Rx.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
module UART_Rx(CLK,RST,SI,Rx_Byte, NINTI);
input CLK,RST, SI;
output reg [7:0] Rx_Byte;
output reg NINTI;
parameter IDLE = 3'b000;
parameter START_BIT = 3'b001;
parameter RECV_BIT = 3'b010;
parameter STOP_BIT = 3'b011;
parameter CLK_FREQ = 200000000;
parameter BAUD_RATE = 115200;
localparam CLKS_FOR_SEND = CLK_FREQ / BAUD_RATE;
localparam CLKS_FOR_RECV = CLKS_FOR_SEND / 2;
reg [2:0]state = 0;
//reg [7:0]One_Byte = 0; // register for save data from input
reg [2:0]Bit_Index = 0;
reg [$clog2(CLKS_FOR_SEND)-1:0] tx_clk_count=0;
always @(posedge CLK)
begin
if (RST)
begin
state <= IDLE;
NINTI <= 1'b0;
end
else
begin
case (state)
IDLE: // IDLE
begin
if (SI==1'b0)
begin
NINTI <= 1'b1;
state <= START_BIT;
end
else
begin
NINTI <= 1'b1;
state <= IDLE;
end
end
START_BIT: // REcieving the start bit ("0"), NINTI becomes low.
begin
NINTI <= 1'b0;
state <= RECV_BIT;
Rx_Byte[Bit_Index] <= SI;
Bit_Index = Bit_Index + 1;
end
RECV_BIT: // Reception: Recieving 8 bits data
begin
NINTI <= 1'b0;
Rx_Byte[Bit_Index] <= SI; // Bit_Index = (0 - 7) 000 to 111
//sending 8 bits
if (Bit_Index < 7)
begin
Bit_Index = Bit_Index + 1;
state <= RECV_BIT;
end
else
begin
Bit_Index = 0;
state <= STOP_BIT;
end
end
STOP_BIT: // sending stop bit ("1")
begin
NINTI <= 1'b1;
state <= IDLE;
end
endcase
end
end
endmodule
// SIDLE: sending IDLE, SO = 1; send = 1, move to S0
// S0, SO = 0, S1 -> S2 .....Sstop -> SIDLE (option 1)
// S0 (start bit) -> Strns (sending 8 bits) -> Sstop -> SIDLE (option 2)