3 -- State machine for reading data from Dallas 1621
5 -- Testsystem for i2c controller
9 use ieee.std_logic_1164.all;
10 use ieee.std_logic_arith.all;
14 entity DS1621_interface is
17 nReset : in std_logic;
19 Dout : out std_logic_vector(7 downto 0); -- data read from ds1621
21 error : out std_logic; -- no correct ack received
23 SCL : inout std_logic;
26 end entity DS1621_interface;
28 architecture structural of DS1621_interface is
29 constant SLAVE_ADDR : std_logic_vector(6 downto 0) := "1001000";
30 constant CLK_CNT : unsigned(7 downto 0) := conv_unsigned(20, 8);
32 signal cmd_ack : std_logic;
33 signal D : std_logic_vector(7 downto 0);
34 signal lack, store_dout : std_logic;
36 signal start, read, write, ack, stop : std_logic;
37 signal i2c_dout : std_logic_vector(7 downto 0);
40 -- hookup I2C controller
41 u1: simple_i2c port map (clk => clk, ena => '1', clk_cnt => clk_cnt, nReset => nReset,
42 read => read, write => write, start => start, stop => stop, ack_in => ack, cmd_ack => cmd_ack,
43 Din => D, Dout => i2c_dout, ack_out => lack, SCL => SCL, SDA => SDA);
45 init_statemachine : block
46 type states is (i1, i2, i3, i4, i5, t1, t2, t3, t4, t5);
47 signal state : states;
49 nxt_state_decoder: process(clk, nReset, state)
50 variable nxt_state : states;
51 variable iD : std_logic_vector(7 downto 0);
52 variable ierr : std_logic;
53 variable istart, iread, iwrite, iack, istop : std_logic;
54 variable istore_dout : std_logic;
69 -- 1) send start condition
70 -- 2) send slave address + write
72 -- 4) send "access config" command (0xAC)
74 -- 6) send config register data (0x00)
76 -- 8) send stop condition
77 -- 9) send start condition
78 -- 10) send slave address + write
80 -- 12) send "start conversion" command (0xEE)
82 -- 14) send stop condition
84 when i1 => -- send start condition, sent slave address + write
91 iD := (slave_addr & '0'); -- write to slave (R/W = '0')
93 when i2 => -- send "access config" command
94 if (cmd_ack = '1') then
96 -- check aknowledge bit
98 ierr := '1'; -- no acknowledge received from last command, expected ACK
109 when i3 => -- send config register data, sent stop condition
110 if (cmd_ack = '1') then
112 -- check aknowledge bit
114 ierr := '1'; -- no acknowledge received from last command, expected ACK
125 when i4 => -- send start condition, sent slave address + write
126 if (cmd_ack = '1') then
134 iD := (slave_addr & '0'); -- write to slave (R/W = '0')
137 when i5 => -- send "start conversion" command + stop condition
138 if (cmd_ack = '1') then
140 -- check aknowledge bit
142 ierr := '1'; -- no acknowledge received from last command, expected ACK
153 -- 1) sent start condition
154 -- 2) sent slave address + write
156 -- 4) sent "read temperature" command (0xAA)
158 -- 6) sent start condition
159 -- 7) sent slave address + read
165 -- 13) send stop condition
167 when t1 => -- send start condition, sent slave address + write
168 if (cmd_ack = '1') then
170 -- check aknowledge bit
172 ierr := '1'; -- no acknowledge received from last command, expected ACK
180 iD := (slave_addr & '0'); -- write to slave (R/W = '0')
183 when t2 => -- send read temperature command
184 if (cmd_ack = '1') then
186 -- check aknowledge bit
188 ierr := '1'; -- no acknowledge received from last command, expected ACK
199 when t3 => -- send (repeated) start condition, send slave address + read
200 if (cmd_ack = '1') then
202 -- check aknowledge bit
204 ierr := '1'; -- no acknowledge received, expected ACK
212 iD := (slave_addr & '1'); -- read from slave (R/W = '1')
215 when t4 => -- read MSB (hi-byte), send acknowledge
216 if (cmd_ack = '1') then
218 -- check aknowledge bit
220 ierr := '1'; -- no acknowledge received from last command, expected ACK
230 when t5 => -- read LSB (lo-byte), send acknowledge, sent stop
231 if (cmd_ack = '1') then
245 if (nReset = '0') then
255 D <= (others => '0');
256 elsif (clk'event and clk = '1') then
259 store_dout <= istore_dout;
268 end process nxt_state_decoder;
269 end block init_statemachine;
272 gen_dout : process(clk)
274 if (clk'event and clk = '1') then
275 if (store_dout = '1') then
279 end process gen_dout;
281 end architecture structural;