-
Notifications
You must be signed in to change notification settings - Fork 1
/
uartRx.sv
90 lines (89 loc) · 2.36 KB
/
uartRx.sv
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
module uartRx(
input var logic clock,
input var logic reset,
input var logic uartRxPin,
output var logic [7:0]buffer,
output var logic fin
);
logic busy;
logic clk;
logic receptionEnable;
logic [10:0]divCounter = 11'b0;
always_ff @(posedge clock) begin
if (uartRxPin==1'b0) begin
if(receptionEnable==1'b0) begin
receptionEnable <= 1'b1;
end
end else if (busy==1'b0) begin
if(receptionEnable==1'b1) begin
receptionEnable <= 1'b0;
end
end
end
always_ff @(posedge clock) begin
if (receptionEnable==1'b1) begin
if (divCounter==11'd104-1) begin //これで57600bps
divCounter <= 11'd0;
clk <= ~clk;
end else begin
divCounter <= divCounter+1;
end
end else begin
divCounter <= 0;
clk <= 0;
end
end
logic [1:0] state, nextState;
logic [3:0] receptionCounter, nextReceptionCounter;
logic [1:0] timingCounter, nextTimingCounter;
logic [9:0] register, nextRegister;
logic [7:0] nextBuffer;
always_comb begin
busy = 1'b0;
fin = 1'b0;
nextRegister = register;
nextState = state;
nextBuffer = buffer;
nextReceptionCounter = receptionCounter;
case (state)
2'd0: begin //idle
if(uartRxPin==1'b0) begin //start bit
busy = 1'b1;
nextReceptionCounter = 4'd0;
nextRegister = {uartRxPin,register[9:1]};
nextState = 2'd2;
end
end
2'd2: begin
busy = 1'b1;
if (receptionCounter == 4'd9) begin
nextState = 2'd3; //fin
nextReceptionCounter = 4'd0;
nextBuffer = register[8:1];
end else begin
nextRegister = {uartRxPin,register[9:1]};
nextReceptionCounter = receptionCounter+1;
end
end
2'd3: begin
fin = 1'b1;
busy = 1'b1;
nextState = 2'd0;
nextReceptionCounter = 0;
end
default: begin
end
endcase
end
always_ff @(posedge clk or negedge reset) begin
if(!reset) begin
state <= 2'b0;
receptionCounter <= 4'b0;
end else begin
state <= nextState;
register <= nextRegister;
buffer <= nextBuffer;
receptionCounter <= nextReceptionCounter;
end
end
endmodule