-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathi2c_slave.v
126 lines (106 loc) · 2.28 KB
/
i2c_slave.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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 22.12.2020 18:37:09
// Design Name:
// Module Name: i2c_slave
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module i2c_slave( inout sda, inout scl );
localparam address = 7'b0101010;
localparam read_addr = 0;
localparam ack_tx = 1;
localparam read_data = 2;
localparam write_data = 3;
localparam ack_tx2 = 4;
reg [7:0] addr;
reg [7:0] counter;
reg [7:0] state = 0;
reg [7:0] data_in = 0;
reg [7:0] data_out = 8'b11001100;
reg sda_out = 0;
reg sda_in = 0;
reg start = 0;
reg write_enable = 0;
assign sda = (write_enable == 1) ? sda_out : 'bz;
always @(negedge sda) begin
if ((start == 0) && (scl == 1)) begin
start <= 1;
counter <= 7;
end
end
always @(posedge sda) begin
if ((start == 1) && (scl == 1)) begin
state <= read_addr;
start <= 0;
write_enable <= 0;
end
end
always @(posedge scl) begin
if (start == 1) begin
case(state)
read_addr: begin
addr[counter] <= sda;
if(counter == 0) state <= ack_tx;
else counter <= counter - 1;
end
ack_tx: begin
if(addr[7:1] == address) begin
counter <= 7;
if(addr[0] == 0) begin
state <= read_data;
end
else state <= write_data;
end
end
read_data: begin
data_in[counter] <= sda;
if(counter == 0) begin
state <= ack_tx2;
end else counter <= counter - 1;
end
ack_tx2: begin
state <= read_addr;
end
write_data: begin
if(counter == 0) state <= read_addr;
else counter <= counter - 1;
end
endcase
end
end
always @(negedge scl) begin
case(state)
read_addr: begin
write_enable <= 0;
end
ack_tx: begin
sda_out <= 0;
write_enable <= 1;
end
read_data: begin
write_enable <= 0;
end
write_data: begin
sda_out <= data_out[counter];
write_enable <= 1;
end
ack_tx2: begin
sda_out <= 0;
write_enable <= 1;
end
endcase
end
endmodule