-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathi2c_op.vhd
299 lines (276 loc) · 9.96 KB
/
i2c_op.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
-- i2c_op.vhd : I2C device read/write operation controller
-- Controller accesses I2C device and reads/writes data byte
-- Copyright (C) 2019 CESNET
-- Author(s): Stepan Friedl <[email protected]>
--
-- SPDX-License-Identifier: BSD-3-Clause
--
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.numeric_std.all;
entity i2c_op is
generic (
-- I2C clock dividier
CLK_CNT : ieee.numeric_std.unsigned(15 downto 0) := X"007D"
);
port (
-- =============
-- CLK and RST
-- =============
-- sync reset
RESET : in std_logic;
-- clock
CLK : in std_logic;
-- =============
-- I2C interface
-- =============
SCL_I : in std_logic;
SCL_O : out std_logic;
SCL_OEN : out std_logic;
SDA_I : in std_logic;
SDA_O : out std_logic;
SDA_OEN : out std_logic;
--
-- Start the read operation
START : in std_logic;
-- '0' = read, '1' = write
WR : in std_logic;
-- Address Only flag. When set, terminate the bus operation after address phase ()
AO : in std_logic := '0';
-- I2C bus device address
DEV : in std_logic_vector( 6 downto 0);
-- I2C register address
REG : in std_logic_vector( 7 downto 0);
-- Do a 16 bit read
DRD16 : in std_logic := '0';
-- I2C read data (Slave -> master)
DRD : out std_logic_vector(15 downto 0);
-- I2C write data (master -> slave)
DWD : in std_logic_vector( 7 downto 0);
-- Operation acknowledge
ACK : out std_logic;
-- Error during I2C operation (arbitration lost, destination not available)
ERROR : out std_logic;
BUSY : out std_logic;
DONE : out std_logic
);
end i2c_op;
architecture behavioral of i2c_op is
type t_i2c_state is (IDLE, DA, DR,
RB_DA, RB_D, RB_D2, -- (Multiple) read byte states
WB_D, -- Write byte
ERR
);
signal i2c_state : t_i2c_state;
signal reset_n : std_logic;
signal i2c_start : std_logic;
signal i2c_stop : std_logic;
signal i2c_rd : std_logic;
signal i2c_wr : std_logic;
signal i2c_ack : std_logic;
signal i2c_din : std_logic_vector(7 downto 0);
signal i2c_drd : std_logic_vector(7 downto 0);
signal i2c_done : std_logic;
signal i2c_busy : std_logic;
signal i2c_rxack : std_logic;
signal i2c_al : std_logic;
signal i2c_scl_o : std_logic;
signal i2c_scl_i : std_logic;
signal i2c_scl_oen : std_logic;
signal i2c_sda_o : std_logic;
signal i2c_sda_i : std_logic;
signal i2c_sda_oen : std_logic;
begin
-- FSM performs sequence of I2C bus operations to read or write single byte
-- to/from device DEV register REG
fsm: process(CLK)
begin
if rising_edge(CLK) then
i2c_start <= '0';
i2c_stop <= '0';
i2c_rd <= '0';
i2c_wr <= '0';
i2c_ack <= '0';
DONE <= '0';
ACK <= '0';
ERROR <= '0';
case i2c_state is
-- Bus IDLE, waiting for start
when IDLE =>
BUSY <= '0';
if (START = '1') then
i2c_state <= DA;
BUSY <= '1';
ACK <= '1';
end if;
-- Read-byte - device address
when DA =>
i2c_start <= '1';
i2c_wr <= '1';
i2c_din <= DEV & '0';
if (i2c_done = '1') then
i2c_start <= '0';
i2c_wr <= '0';
if (i2c_rxack = '0') then
i2c_state <= DR;
else
i2c_state <= ERR; -- not acknowledged (no such device on bus)
end if;
end if;
if (i2c_al = '1') then
i2c_state <= ERR; -- arbitration lost
end if;
-- Read-byte - write register number
when DR =>
i2c_wr <= '1';
i2c_stop <= (not WR) or AO;
i2c_din <= REG;
if (i2c_done = '1') then
i2c_wr <= '0';
i2c_stop <= '0';
if (i2c_rxack = '0') then
if AO = '1' then -- Requested to execute addressing phase only
i2c_state <= IDLE;
DONE <= '1';
BUSY <= '0';
elsif WR = '1' then
i2c_state <= WB_D;
else
i2c_state <= RB_DA;
end if;
else
i2c_state <= ERR; -- not acknowledged
end if;
end if;
if (i2c_al = '1') then
i2c_state <= ERR; -- arbitration lost
end if;
-- Read byte - write device address & read bit
when RB_DA =>
i2c_start <= '1';
i2c_wr <= '1';
i2c_rd <= '0';
i2c_din <= DEV & '1';
i2c_ack <= '0';
if (i2c_done = '1') then
i2c_start <= '0';
i2c_rd <= '0';
i2c_wr <= '0';
if (i2c_rxack = '0') then
i2c_state <= RB_D;
else
i2c_state <= ERR; -- not acknowledged (no such device on bus)
end if;
end if;
if (i2c_al = '1') then
i2c_state <= ERR; -- arbitration lost
end if;
-- Read byte - read data
when RB_D =>
i2c_rd <= '1';
if DRD16 = '0' then
DRD(7 downto 0) <= i2c_drd;
DRD(15 downto 8) <= X"00";
i2c_ack <= '1'; -- NACK
i2c_stop <= '1';
else
DRD(15 downto 8) <= i2c_drd;
i2c_ack <= '0'; -- ACK
i2c_stop <= '0';
end if;
if (i2c_done = '1') then -- byte is read
i2c_rd <= '0';
i2c_stop <= '0';
if DRD16 = '0' then -- All done
DONE <= '1';
BUSY <= '0';
i2c_state <= IDLE;
else -- Read next byte
i2c_state <= RB_D2;
end if;
end if;
if (i2c_al = '1') then
i2c_state <= ERR; -- arbitration lost
end if;
-- Read second data byte
when RB_D2 =>
i2c_rd <= '1';
i2c_ack <= '1'; -- NACK
i2c_stop <= '1';
DRD(7 downto 0) <= i2c_drd;
if (i2c_done = '1') then -- byte is read
i2c_rd <= '0';
i2c_stop <= '0';
DONE <= '1';
BUSY <= '0';
i2c_state <= IDLE;
end if;
if (i2c_al = '1') then
i2c_state <= ERR; -- arbitration lost
end if;
-- write data byte
when WB_D =>
i2c_wr <= '1';
i2c_stop <= '1';
i2c_din <= DWD;
if (i2c_done = '1') then -- all done, byte is written
i2c_wr <= '0';
i2c_stop <= '0';
DONE <= '1';
BUSY <= '0';
if (i2c_rxack = '0') then
i2c_state <= IDLE;
else
i2c_state <= ERR;
end if;
end if;
if (i2c_al = '1') then
i2c_state <= ERR; -- arbitration lost
end if;
--
when ERR =>
i2c_stop <= '1';
--if (i2c_busy_i = '0') then --
if (i2c_done = '1') or (i2c_al = '1') then --
i2c_state <= IDLE;
BUSY <= '0';
ERROR <= '1';
i2c_stop <= '0';
end if;
end case;
if RESET = '1' then
i2c_state <= IDLE;
end if;
end if;
end process;
reset_n <= not RESET;
i2c_ctrl: entity work.i2c_master_byte_ctrl
port map (
clk => CLK,
rst => RESET,
nReset => reset_n,
ena => '1',
clk_cnt => ieee.std_logic_arith.unsigned(CLK_CNT), -- Clock dividier
-- input signals
start => i2c_start,
stop => i2c_stop,
read => i2c_rd,
write => i2c_wr,
ack_in => i2c_ack,
din => i2c_din,
-- output signals
cmd_ack => i2c_done,
ack_out => i2c_rxack,
i2c_busy => i2c_busy,
i2c_al => i2c_al,
dout => i2c_drd,
-- i2c lines
scl_i => scl_i,
scl_o => scl_o,
scl_oen => scl_oen,
sda_i => SDA_i,
sda_o => sda_o,
sda_oen => sda_oen
);
end behavioral;