1 | library IEEE;
|
2 | use IEEE.STD_LOGIC_1164.ALL;
|
3 | use IEEE.NUMERIC_STD.ALL;
|
4 |
|
5 | entity I2C_Master_Controller is
|
6 | Port (
|
7 | clock : in STD_LOGIC;
|
8 | rst : in STD_LOGIC; -- reset
|
9 | rw : in STD_LOGIC; -- read or write
|
10 | tbr : in STD_LOGIC; -- two-byte-read
|
11 | reg_addr_in : in STD_LOGIC_VECTOR(7 downto 0); --Registeradresse vom Konfigurationsmodul
|
12 | data_in : in STD_LOGIC_VECTOR(7 downto 0); --Daten vom Konfigurationsmodul, die bei rw <= '0' in den Sensor geschrieben werden
|
13 | busy : out STD_LOGIC;
|
14 | data_out : out STD_LOGIC_VECTOR(15 downto 0);--Registerinhalt
|
15 | scl : out STD_LOGIC;
|
16 | sda : inout STD_LOGIC);
|
17 | end I2C_Master_Controller;
|
18 |
|
19 | architecture Behavioral of I2C_Master_Controller is
|
20 |
|
21 | type state_type is (idle, start_condition, send_dev_addr, loop_send_dev_addr, get_ack, loop_get_ack, send_reg_addr, loop_send_reg_addr, repeat_condition,loop_repeat_condition, loop_again_repeat_condition, one_byte_read, loop_one_byte_read, get_no_ack, loop_get_no_ack, send_stop, loop_send_stop, loop_again_send_stop, write_data, loop_write_data, two_byte_read, loop_two_byte_read);
|
22 | signal state, prev_state: state_type;
|
23 |
|
24 | constant dev_addr_w : std_logic_vector(7 downto 0) := "10010110";-- Sensoradresse mit Write-Bit
|
25 | constant dev_addr_r : std_logic_vector(7 downto 0) := "10010111";--Sensoradresse mit Read-Bit
|
26 |
|
27 | signal tmp_data : STD_LOGIC_VECTOR(15 downto 0);
|
28 | constant prescaler : integer := 100000000/400000;
|
29 | signal data : std_logic_vector(7 downto 0);
|
30 | signal bitcnt : integer range 0 to 7;
|
31 | signal cnt_clk : integer range 0 to prescaler;
|
32 | signal new_clk : STD_LOGIC;
|
33 | signal sda_tmp : STD_LOGIC;
|
34 | signal first_byte_sent : STD_LOGIC; --Bit für 2-Byte-Read: wird '1', sobald das erste Byte vom Sensor empfangen wurde.
|
35 |
|
36 | begin
|
37 |
|
38 | sda <= 'Z' when sda_tmp = '1' else '0';
|
39 | -------------------------------------------------------------------------
|
40 | -------------------------------------------------------------------------
|
41 | process(new_clk, rst)
|
42 | begin
|
43 | if (rst <= '0') then
|
44 | scl <= '1';
|
45 | sda_tmp <= '1';
|
46 | tmp_data <= x"FFFF";
|
47 | data_out <= x"FFFF";
|
48 | first_byte_sent <= '1';
|
49 | data_out <= x"FFFF";
|
50 | bitcnt <= 7;
|
51 | busy <= '0';
|
52 | state <= idle;
|
53 | elsif rising_edge(new_clk) then
|
54 | case state is
|
55 | when idle =>
|
56 | busy <= '0';
|
57 | first_byte_sent <= '0';
|
58 | scl <= '1';
|
59 | sda_tmp <= '1';
|
60 | state <= start_condition;
|
61 | -------------------------------------------------------------------------
|
62 | when start_condition =>
|
63 | busy <= '1';
|
64 | sda_tmp <= '0';
|
65 | bitcnt <= 7;
|
66 | data <= dev_addr_w;
|
67 | prev_state <= start_condition;
|
68 | state <= send_dev_addr;
|
69 | -------------------------------------------------------------------------
|
70 | when send_dev_addr =>
|
71 | scl <= '0';
|
72 | sda_tmp <= data(bitcnt);
|
73 | state <= loop_send_dev_addr;
|
74 |
|
75 | when loop_send_dev_addr =>
|
76 | scl <= '1';
|
77 | if bitcnt /= 0 then
|
78 | bitcnt <= bitcnt - 1;
|
79 | state <= send_dev_addr;
|
80 | else
|
81 | bitcnt <= 7;
|
82 | case prev_state is
|
83 | when start_condition =>
|
84 | prev_state <= send_dev_addr;
|
85 | when repeat_condition =>
|
86 | prev_state <= repeat_condition;
|
87 | when others => null;
|
88 | end case;
|
89 | state <= get_ack;
|
90 | end if;
|
91 | -------------------------------------------------------------------------
|
92 | when get_ack =>
|
93 | scl <= '0';
|
94 | sda_tmp <= '1';
|
95 | state <= loop_get_ack;
|
96 |
|
97 | when loop_get_ack =>
|
98 | scl <= '1';
|
99 | sda_tmp <= '0';
|
100 | case prev_state is
|
101 | when send_dev_addr =>
|
102 | state <= send_reg_addr;
|
103 | when send_reg_addr =>
|
104 | if rw = '0' then
|
105 | bitcnt <= 7;
|
106 | state <= write_data;
|
107 | elsif rw <= '1' then
|
108 | bitcnt <= 7;
|
109 | state <= repeat_condition;
|
110 | end if;
|
111 | when repeat_condition =>
|
112 | if tbr <= '0' then
|
113 | state <= one_byte_read;
|
114 | elsif tbr <= '1' then
|
115 | state <= two_byte_read;
|
116 | end if;
|
117 | when write_data =>
|
118 | state <= send_stop;
|
119 | when two_byte_read =>
|
120 | first_byte_sent <= '1';
|
121 | state <= two_byte_read;
|
122 | when others =>
|
123 | state <= idle;
|
124 | end case;
|
125 | -------------------------------------------------------------------------
|
126 | when send_reg_addr =>
|
127 | scl <= '0';
|
128 | sda_tmp <= reg_addr_in(bitcnt);
|
129 | state <= loop_send_reg_addr;
|
130 |
|
131 | when loop_send_reg_addr =>
|
132 | scl <= '1';
|
133 | if bitcnt /= 0 then
|
134 | bitcnt <= bitcnt - 1;
|
135 | state <= send_reg_addr;
|
136 | else
|
137 | bitcnt <= 7;
|
138 | prev_state <= send_reg_addr;
|
139 | state <= get_ack;
|
140 | end if;
|
141 | -------------------------------------------------------------------------
|
142 | when repeat_condition =>
|
143 | scl <= '0';
|
144 | sda_tmp <= '1';
|
145 | state <= loop_repeat_condition;
|
146 |
|
147 | when loop_repeat_condition =>
|
148 | scl <= '1';
|
149 | sda_tmp <= '1';
|
150 | state <= loop_again_repeat_condition;
|
151 |
|
152 | when loop_again_repeat_condition =>
|
153 | scl <= '1';
|
154 | sda_tmp <= '0';
|
155 | data <= dev_addr_r;
|
156 | prev_state <= repeat_condition;
|
157 | state <= send_dev_addr;
|
158 | -------------------------------------------------------------------------
|
159 | when write_data =>
|
160 | scl <= '0';
|
161 | sda_tmp <= data_in(bitcnt);
|
162 | state <= loop_write_data;
|
163 |
|
164 | when loop_write_data =>
|
165 | scl <= '1';
|
166 | if bitcnt /= 0 then
|
167 | bitcnt <= bitcnt - 1;
|
168 | state <= write_data;
|
169 | else
|
170 | bitcnt <= 7;
|
171 | prev_state <= write_data;
|
172 | state <= get_ack;
|
173 | end if;
|
174 | -------------------------------------------------------------------------
|
175 | when one_byte_read =>
|
176 | scl <= '0';
|
177 | sda_tmp <= '1';
|
178 | state <= loop_one_byte_read;
|
179 |
|
180 | when loop_one_byte_read =>
|
181 | scl <= '1';
|
182 | data(bitcnt) <= sda;
|
183 | if bitcnt /= 0 then
|
184 | bitcnt <= bitcnt - 1;
|
185 | state <= one_byte_read;
|
186 | else
|
187 | bitcnt <= 7;
|
188 | tmp_data(15 downto 8) <= data;
|
189 | tmp_data(7 downto 0) <= (others => '0');
|
190 | state <= get_no_ack;
|
191 | end if;
|
192 | -------------------------------------------------------------------------
|
193 | when two_byte_read =>
|
194 | scl <= '0';
|
195 | sda_tmp <= '1';
|
196 | state <= loop_two_byte_read;
|
197 |
|
198 | when loop_two_byte_read =>
|
199 | scl <= '1';
|
200 | data(bitcnt) <= sda;
|
201 | if bitcnt /= 0 then
|
202 | bitcnt <= bitcnt - 1;
|
203 | state <= two_byte_read;
|
204 | elsif first_byte_sent <= '0' then
|
205 | bitcnt <= 7;
|
206 | tmp_data(15 downto 8) <= data;
|
207 | state <= get_ack;
|
208 | prev_state <= two_byte_read;
|
209 | elsif first_byte_sent <= '1' then
|
210 | bitcnt <= 7;
|
211 | tmp_data(7 downto 0) <= data;
|
212 | state <= get_no_ack;
|
213 | end if;
|
214 | -------------------------------------------------------------------------
|
215 | when get_no_ack =>
|
216 | scl <= '0';
|
217 | sda_tmp <= '1';
|
218 | data_out <= tmp_data;
|
219 | state <= loop_get_no_ack;
|
220 |
|
221 | when loop_get_no_ack =>
|
222 | scl <= '1';
|
223 | state <= send_stop;
|
224 | -------------------------------------------------------------------------
|
225 | when send_stop =>
|
226 | scl <= '0';
|
227 | sda_tmp <= '0';
|
228 | state <= loop_send_stop;
|
229 |
|
230 | when loop_send_stop =>
|
231 | scl <= '1';
|
232 | sda_tmp <= '0';
|
233 | state <= loop_again_send_stop;
|
234 |
|
235 | when loop_again_send_stop =>
|
236 | scl <= '1';
|
237 | sda_tmp <= '1';
|
238 | state <= idle;
|
239 | -------------------------------------------------------------------------
|
240 | when others =>
|
241 | state <= idle;
|
242 | -------------------------------------------------------------------------
|
243 | end case;
|
244 | end if;
|
245 | end process;
|
246 | -------------------------------------------------------------------------
|
247 | -------------------------------------------------------------------------
|
248 | process (clock)
|
249 | begin
|
250 | if rising_edge(clock) then
|
251 | if (cnt_clk < prescaler) then
|
252 | cnt_clk <= cnt_clk + 1;
|
253 | if (cnt_clk < prescaler/2) then
|
254 | new_clk <= '0';
|
255 | else
|
256 | new_clk <= '1';
|
257 | end if;
|
258 | else
|
259 | cnt_clk <= 0;
|
260 | end if;
|
261 | end if;
|
262 |
|
263 | end process;
|
264 | -------------------------------------------------------------------------
|
265 | -------------------------------------------------------------------------
|
266 | end Behavioral;
|