-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.vhd
239 lines (216 loc) · 4.96 KB
/
i2c.vhd
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
library IEEE;
use IEEE.NUMERIC_STD.ALL;
use IEEE.STD_LOGIC_1164.ALL;
ENTITY I2C_MASTER IS
GENERIC(bus_clk : INTEGER := 1_000_000;
sys_clk : INTEGER := 1000000000 );
PORT(
CLK : IN STD_LOGIC;
RESET : IN STD_LOGIC;
P : IN STD_LOGIC_VECTOR( 15 DOWNTO 0 );
SDA : INOUT STD_LOGIC;
SCL : INOUT STD_LOGIC
);
END ENTITY;
ARCHITECTURE BEV OF I2C_MASTER IS
CONSTANT delay : INTEGER := 5*bus_clk;
CONSTANT divider : INTEGER := ((sys_clk/8)/bus_clk);
CONSTANT slave_addr : std_logic_vector( 6 DOWNTO 0) := "0101010";
CONSTANT dev_addr_write: STD_LOGIC_VECTOR( 7 DOWNTO 0) := "10100000";
CONSTANT dev_addr_reg: STD_LOGIC_VECTOR( 7 DOWNTO 0) := "10100001";
SIGNAL p_num : STD_LOGIC_VECTOR( 15 DOWNTO 0 ) := X"0000";
COMPONENT shiftReg
PORT(
trigger : in STD_LOGIC;
input : in STD_LOGIC;
output : out STD_LOGIC
);
END COMPONENT;
COMPONENT validate is
PORT ( sdaIn : in STD_LOGIC;
sclIn : in STD_LOGIC;
sdaOut : out STD_LOGIC;
start : out STD_LOGIC;
stop : out STD_LOGIC
);
END COMPONENT;
SIGNAL timer : NATURAL RANGE 0 TO delay;
SIGNAL i : NATURAL RANGE 0 TO delay;
type state_t is ( idle, ack0, ack1, ack2, dev_addr, p0, p1, rw, start, stop, error);
SIGNAL st, st_n : state_t;
SIGNAL scl_ena : STD_LOGIC := '0'; --enables internal scl to output
SIGNAL data_in : STD_LOGIC_VECTOR( 7 DOWNTO 0 );
SIGNAL data_clk, scl_clk : STD_LOGIC; --data clock for sda
SIGNAL data_clk_prev : STD_LOGIC; --data clock during previous system clock
SIGNAL rw_signal : STD_LOGIC := '1';
SIGNAL sda_n : STD_LOGIC;
SIGNAL aux_clk : STD_LOGIC := '0';
SIGNAL wr_flag, rd_flag : STD_LOGIC;
SIGNAL ack_signal :STD_LOGIC := '0';
SIGNAL stop_sent : STD_LOGIC := '0';
BEGIN
PROCESS( clk )
VARIABLE count : INTEGER RANGE 0 TO divider;
BEGIN
IF( rising_edge( clk )) THEN
count := count + 1;
IF( count = divider ) THEN
aux_clk <= not aux_clk;
count := 0;
END IF;
END IF;
END PROCESS;
PROCESS(aux_clk)
VARIABLE count : INTEGER RANGE 0 TO 3;
BEGIN
IF( rising_edge( aux_clk ) ) THEN
count := (count + 1) mod 4;
IF ( count = 0 ) THEN
scl_clk <= '0';
ELSIF( count = 1 ) THEN
data_clk <= '1';
ELSIF( count = 2 ) THEN
scl_clk <= 'Z';
ELSIF( count = 3 ) THEN
data_clk <= '0';
END IF;
END IF;
END PROCESS;
PROCESS ( data_clk, RESET )
BEGIN
IF( RESET = '1' ) THEN
stop_sent <= '0';
st <= idle;
i <= 0;
ELSIF( rising_edge( data_clk )) THEN
IF( i = timer - 1) THEN
st <= st_n;
i <= 0;
ELSE
i <= 1 + i;
END IF;
ELSIF( falling_edge(data_clk)) THEN
IF(st = idle) THEN
wr_flag <= '0';
rd_flag <= '1';
END IF;
IF ( st = p0 AND st = p1 ) THEN
IF( SDA = 'H') THEN
data_in( 7 - i ) <= '1';
ELSE
data_in( 7 - i ) <= '0';
END IF; END IF;
IF( st = ack0 OR st = ack1 OR st = ack2) THEN
IF( SDA = 'H') THEN
ack_signal <= '1';
ELSE
ack_signal <= '0';
END IF;
END IF;
END IF;
END PROCESS;
PROCESS ( st, scl_clk, data_clk )
BEGIN
CASE st IS
WHEN idle =>
SCL <= 'Z';
SDA <= 'Z';
p_num <= P;
IF( wr_flag = '1' OR rd_flag = '1') THEN
timer <= 1;
st_n <= start;
ELSE
st_n <= idle;
timer <= delay;
END IF;
WHEN start =>
SCL <= 'Z';
IF( data_clk = '1' ) THEN
SDA <= 'Z';
ELSE
SDA <= '0';
END IF;
timer <= 1;
st_n <= dev_addr;
WHEN dev_addr =>
IF( slave_addr(6-i) = '1' ) THEN
SDA <= 'Z';
ELSE
SDA <= '0';
END IF;
SCL <= scl_clk;
timer <= 7;
st_n <= rw;
WHEN rw =>
IF( rw_signal = '1' ) THEN
SDA <= 'Z';
ELSE
SDA <= '0';
END IF;
SCL <= scl_clk;
timer <= 1;
st_n <= ack0;
WHEN ack0 =>
IF( scl_clk = '1' ) THEN
SCL <= 'Z';
ELSE
SCL <= '0';
END IF;
timer <= 1;
IF( ack_signal = '1' ) THEN
st_n <= p0;
ELSE
st_n <= ack0;
END IF;
SDA <= 'Z';
WHEN p0 =>
IF( p_num(15-i) = '1' ) THEN
SDA <= 'Z';
ELSE
SDA <= '0';
END IF;
SCL <= scl_clk;
timer <= 8;
st_n <= ack1;
WHEN ack1 =>
SCL <= scl_clk;
timer <= 1;
IF( ack_signal = '1' ) THEN
st_n <= p1;
ELSE
st_n <= ack1;
END IF;
SDA <= 'Z';
WHEN p1 =>
SCL <= scl_clk;
timer <= 8;
st_n <= ack2;
IF( p_num(7-i) = '1' ) THEN
SDA <= 'Z';
ELSE
SDA <= '0';
END IF;
WHEN ack2 =>
timer <= 1;
IF( stop_sent = '0') THEN
SCL <= scl_clk;
IF( not scl_clk'EVENT AND scl_clk = 'Z') THEN
SDA <= 'Z';
stop_sent <= '1';
ELSE
stop_sent <= '0';
SDA <= '0';
END IF;
END IF;
st_n <= stop;
WHEN stop =>
SCL <= 'Z';
SDA <= 'Z';
st_n <= stop;
timer <= delay;
WHEN error =>
END CASE;
END PROCESS;
--SDA <= '0' WHEN
--SCL <= '0' WHEN (scl_ena = '1' AND scl_clk = '0') ELSE 'Z';
END BEV;