OpenCores
URL https://opencores.org/ocsvn/i2c/i2c/trunk

Subversion Repositories i2c

[/] [i2c/] [trunk/] [rtl/] [vhdl/] [i2c_master_bit_ctrl.vhd] - Blame information for rev 39

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 15 rherveille
---------------------------------------------------------------------
2
----                                                             ----
3 27 rherveille
----  WISHBONE revB2 I2C Master Core; bit-controller             ----
4 15 rherveille
----                                                             ----
5
----                                                             ----
6
----  Author: Richard Herveille                                  ----
7
----          richard@asics.ws                                   ----
8
----          www.asics.ws                                       ----
9
----                                                             ----
10
----  Downloaded from: http://www.opencores.org/projects/i2c/    ----
11
----                                                             ----
12
---------------------------------------------------------------------
13
----                                                             ----
14
---- Copyright (C) 2000 Richard Herveille                        ----
15
----                    richard@asics.ws                         ----
16
----                                                             ----
17
---- This source file may be used and distributed without        ----
18
---- restriction provided that this copyright statement is not   ----
19
---- removed from the file and that any derivative work contains ----
20
---- the original copyright notice and the associated disclaimer.----
21
----                                                             ----
22
----     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     ----
23
---- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   ----
24
---- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   ----
25
---- FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      ----
26
---- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         ----
27
---- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    ----
28
---- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   ----
29
---- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        ----
30
---- BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  ----
31
---- LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  ----
32
---- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  ----
33
---- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         ----
34
---- POSSIBILITY OF SUCH DAMAGE.                                 ----
35
----                                                             ----
36
---------------------------------------------------------------------
37
 
38
--  CVS Log
39
--
40 39 rherveille
--  $Id: i2c_master_bit_ctrl.vhd,v 1.9 2003-08-12 14:48:37 rherveille Exp $
41 15 rherveille
--
42 39 rherveille
--  $Date: 2003-08-12 14:48:37 $
43
--  $Revision: 1.9 $
44 15 rherveille
--  $Author: rherveille $
45
--  $Locker:  $
46
--  $State: Exp $
47
--
48
-- Change History:
49
--               $Log: not supported by cvs2svn $
50 39 rherveille
--               Revision 1.8  2003/08/09 07:01:13  rherveille
51
--               Fixed a bug in the Arbitration Lost generation caused by delay on the (external) sda line.
52
--               Fixed a potential bug in the byte controller's host-acknowledge generation.
53
--
54 38 rherveille
--               Revision 1.7  2003/02/05 00:06:02  rherveille
55
--               Fixed a bug where the core would trigger an erroneous 'arbitration lost' interrupt after being reset, when the reset pulse width < 3 clk cycles.
56
--
57 35 rherveille
--               Revision 1.6  2003/02/01 02:03:06  rherveille
58
--               Fixed a few 'arbitration lost' bugs. VHDL version only.
59
--
60 34 rherveille
--               Revision 1.5  2002/12/26 16:05:47  rherveille
61
--               Core is now a Multimaster I2C controller.
62
--
63 31 rherveille
--               Revision 1.4  2002/11/30 22:24:37  rherveille
64
--               Cleaned up code
65
--
66 27 rherveille
--               Revision 1.3  2002/10/30 18:09:53  rherveille
67
--               Fixed some reported minor start/stop generation timing issuess.
68
--
69 24 rherveille
--               Revision 1.2  2002/06/15 07:37:04  rherveille
70
--               Fixed a small timing bug in the bit controller.\nAdded verilog simulation environment.
71
--
72 22 rherveille
--               Revision 1.1  2001/11/05 12:02:33  rherveille
73
--               Split i2c_master_core.vhd into separate files for each entity; same layout as verilog version.
74
--               Code updated, is now up-to-date to doc. rev.0.4.
75
--               Added headers.
76
--
77 15 rherveille
 
78
 
79
--
80
-------------------------------------
81
-- Bit controller section
82
------------------------------------
83
--
84
-- Translate simple commands into SCL/SDA transitions
85
-- Each command has 5 states, A/B/C/D/idle
86
--
87 31 rherveille
-- start:    SCL  ~~~~~~~~~~~~~~\____
88
--           SDA  XX/~~~~~~~\______
89
--                x | A | B | C | D | i
90 15 rherveille
--
91 31 rherveille
-- repstart  SCL  ______/~~~~~~~\___
92
--           SDA  __/~~~~~~~\______
93
--                x | A | B | C | D | i
94 15 rherveille
--
95 31 rherveille
-- stop      SCL  _______/~~~~~~~~~~~
96
--           SDA  ==\___________/~~~~~
97
--                x | A | B | C | D | i
98 15 rherveille
--
99 31 rherveille
--- write    SCL  ______/~~~~~~~\____
100
--           SDA  XXX===============XX
101
--                x | A | B | C | D | i
102 15 rherveille
--
103 31 rherveille
--- read     SCL  ______/~~~~~~~\____
104
--           SDA  XXXXXXX=XXXXXXXXXXX
105
--                x | A | B | C | D | i
106 15 rherveille
--
107
 
108 24 rherveille
-- Timing:      Normal mode     Fast mode
109 15 rherveille
-----------------------------------------------------------------
110 24 rherveille
-- Fscl         100KHz          400KHz
111
-- Th_scl       4.0us           0.6us   High period of SCL
112
-- Tl_scl       4.7us           1.3us   Low period of SCL
113
-- Tsu:sta      4.7us           0.6us   setup time for a repeated start condition
114
-- Tsu:sto      4.0us           0.6us   setup time for a stop conditon
115
-- Tbuf         4.7us           1.3us   Bus free time between a stop and start condition
116 15 rherveille
--
117
 
118
library ieee;
119
use ieee.std_logic_1164.all;
120
use ieee.std_logic_arith.all;
121
 
122
entity i2c_master_bit_ctrl is
123
        port (
124
                clk    : in std_logic;
125
                rst    : in std_logic;
126
                nReset : in std_logic;
127
                ena    : in std_logic;                          -- core enable signal
128
 
129
                clk_cnt : in unsigned(15 downto 0);              -- clock prescale value
130
 
131
                cmd     : in std_logic_vector(3 downto 0);
132 31 rherveille
                cmd_ack : out std_logic; -- command completed
133
                busy    : out std_logic; -- i2c bus busy
134
                al      : out std_logic; -- arbitration lost
135 15 rherveille
 
136
                din  : in std_logic;
137
                dout : out std_logic;
138
 
139
                -- i2c lines
140
                scl_i   : in std_logic;  -- i2c clock line input
141
                scl_o   : out std_logic; -- i2c clock line output
142
                scl_oen : out std_logic; -- i2c clock line output enable, active low
143
                sda_i   : in std_logic;  -- i2c data line input
144
                sda_o   : out std_logic; -- i2c data line output
145
                sda_oen : out std_logic  -- i2c data line output enable, active low
146
        );
147
end entity i2c_master_bit_ctrl;
148
 
149
architecture structural of i2c_master_bit_ctrl is
150 22 rherveille
        constant I2C_CMD_NOP    : std_logic_vector(3 downto 0) := "0000";
151
        constant I2C_CMD_START  : std_logic_vector(3 downto 0) := "0001";
152
        constant I2C_CMD_STOP   : std_logic_vector(3 downto 0) := "0010";
153
        constant I2C_CMD_READ   : std_logic_vector(3 downto 0) := "0100";
154
        constant I2C_CMD_WRITE  : std_logic_vector(3 downto 0) := "1000";
155 15 rherveille
 
156 27 rherveille
        type states is (idle, start_a, start_b, start_c, start_d, start_e,
157 24 rherveille
                        stop_a, stop_b, stop_c, stop_d, rd_a, rd_b, rd_c, rd_d, wr_a, wr_b, wr_c, wr_d);
158 15 rherveille
        signal c_state : states;
159
 
160 22 rherveille
        signal iscl_oen, isda_oen : std_logic;          -- internal I2C lines
161 31 rherveille
        signal sda_chk            : std_logic;          -- check SDA status (multi-master arbitration)
162
        signal dscl_oen           : std_logic;          -- delayed scl_oen signals
163 22 rherveille
        signal sSCL, sSDA         : std_logic;          -- synchronized SCL and SDA inputs
164 34 rherveille
        signal clk_en, slave_wait : std_logic;          -- clock generation signals
165
        signal ial                : std_logic;          -- internal arbitration lost signal
166 22 rherveille
--      signal cnt : unsigned(15 downto 0) := clk_cnt;  -- clock divider counter (simulation)
167 15 rherveille
        signal cnt : unsigned(15 downto 0);             -- clock divider counter (synthesis)
168
 
169
begin
170 31 rherveille
        -- whenever the slave is not ready it can delay the cycle by pulling SCL low
171 22 rherveille
        -- delay scl_oen
172
        process (clk)
173
        begin
174 24 rherveille
            if (clk'event and clk = '1') then
175 27 rherveille
              dscl_oen <= iscl_oen;
176 24 rherveille
            end if;
177 22 rherveille
        end process;
178
        slave_wait <= dscl_oen and not sSCL;
179 15 rherveille
 
180
        -- generate clk enable signal
181
        gen_clken: process(clk, nReset)
182
        begin
183 24 rherveille
            if (nReset = '0') then
184 27 rherveille
              cnt    <= (others => '0');
185
              clk_en <= '1';
186 24 rherveille
            elsif (clk'event and clk = '1') then
187
              if (rst = '1') then
188 27 rherveille
                cnt    <= (others => '0');
189
                clk_en <= '1';
190 24 rherveille
              else
191
                if ( (cnt = 0) or (ena = '0') ) then
192 31 rherveille
                  if (slave_wait = '0') then
193
                    cnt    <= clk_cnt;
194
                    clk_en <= '1';
195
                  else
196
                    cnt    <= cnt;
197
                    clk_en <= '0';
198
                  end if;
199 24 rherveille
                else
200
                  if (slave_wait = '0') then
201 27 rherveille
                    cnt <= cnt -1;
202 24 rherveille
                  end if;
203 27 rherveille
                  clk_en <= '0';
204 24 rherveille
                end if;
205
              end if;
206
            end if;
207 15 rherveille
        end process gen_clken;
208
 
209
 
210
        -- generate bus status controller
211
        bus_status_ctrl: block
212 31 rherveille
          signal dSCL, dSDA          : std_logic;  -- delayes sSCL and sSDA
213
          signal sta_condition       : std_logic;  -- start detected
214
          signal sto_condition       : std_logic;  -- stop detected
215 38 rherveille
          signal cmd_stop            : std_logic;  -- STOP command
216 31 rherveille
          signal ibusy               : std_logic;  -- internal busy signal
217
        begin
218
            -- synchronize SCL and SDA inputs
219 35 rherveille
            synch_scl_sda: process(clk, nReset)
220 31 rherveille
            begin
221 35 rherveille
                if (nReset = '0') then
222
                  sSCL <= '1';
223
                  sSDA <= '1';
224 15 rherveille
 
225 35 rherveille
                  dSCL <= '1';
226
                  dSDA <= '1';
227
                elsif (clk'event and clk = '1') then
228
                  if (rst = '1') then
229
                    sSCL <= '1';
230
                    sSDA <= '1';
231
 
232
                    dSCL <= '1';
233
                    dSDA <= '1';
234
                  else
235
                    sSCL <= scl_i;
236
                    sSDA <= sda_i;
237
 
238
                    dSCL <= sSCL;
239
                    dSDA <= sSDA;
240
                  end if;
241 31 rherveille
                end if;
242
            end process synch_SCL_SDA;
243
 
244 24 rherveille
            -- detect start condition => detect falling edge on SDA while SCL is high
245
            -- detect stop condition  => detect rising edge on SDA while SCL is high
246 35 rherveille
            detect_sta_sto: process(clk, nReset)
247 24 rherveille
            begin
248 35 rherveille
                if (nReset = '0') then
249
                  sta_condition <= '0';
250
                  sto_condition <= '0';
251
                elsif (clk'event and clk = '1') then
252
                  if (rst = '1') then
253
                    sta_condition <= '0';
254
                    sto_condition <= '0';
255
                  else
256
                    sta_condition <= (not sSDA and dSDA) and sSCL;
257
                    sto_condition <= (sSDA and not dSDA) and sSCL;
258
                  end if;
259 24 rherveille
                end if;
260
            end process detect_sta_sto;
261 15 rherveille
 
262 31 rherveille
            -- generate i2c-bus busy signal
263 24 rherveille
            gen_busy: process(clk, nReset)
264
            begin
265
                if (nReset = '0') then
266 27 rherveille
                  ibusy <= '0';
267 24 rherveille
                elsif (clk'event and clk = '1') then
268
                  if (rst = '1') then
269 27 rherveille
                    ibusy <= '0';
270 24 rherveille
                  else
271 27 rherveille
                    ibusy <= (sta_condition or ibusy) and not sto_condition;
272 24 rherveille
                  end if;
273
                end if;
274
            end process gen_busy;
275 31 rherveille
            busy <= ibusy;
276 15 rherveille
 
277 31 rherveille
 
278
            -- generate arbitration lost signal
279 35 rherveille
            gen_al: process(clk, nReset)
280 31 rherveille
            begin
281 35 rherveille
              if (nReset = '0') then
282
                cmd_stop  <= '0';
283
                ial       <= '0';
284
              elsif (clk'event and clk = '1') then
285
                if (rst = '1') then
286
                  cmd_stop  <= '0';
287
                  ial       <= '0';
288
                else
289 39 rherveille
                  if (clk_en = '1' and cmd = I2C_CMD_STOP) then
290
                    cmd_stop <= '1';
291
                  else
292
                    cmd_stop <= '0';
293
                  end if;
294 31 rherveille
 
295 38 rherveille
                  ial <= (sda_chk and not sSDA and isda_oen) or (sto_condition and not cmd_stop);
296 35 rherveille
                end if;
297 31 rherveille
              end if;
298
            end process gen_al;
299 35 rherveille
            al <= ial;
300 34 rherveille
 
301 31 rherveille
            -- generate dout signal, store dout on rising edge of SCL
302
            gen_dout: process(clk)
303
            begin
304
              if (clk'event and clk = '1') then
305
                if (sSCL = '1' and dSCL = '0') then
306
                  dout <= sSDA;
307
                end if;
308
              end if;
309
            end process gen_dout;
310 15 rherveille
        end block bus_status_ctrl;
311
 
312
 
313
        -- generate statemachine
314
        nxt_state_decoder : process (clk, nReset, c_state, cmd)
315
        begin
316 27 rherveille
            if (nReset = '0') then
317
              c_state  <= idle;
318
              cmd_ack  <= '0';
319
              iscl_oen <= '1';
320
              isda_oen <= '1';
321 31 rherveille
              sda_chk  <= '0';
322 27 rherveille
            elsif (clk'event and clk = '1') then
323 34 rherveille
              if (rst = '1' or ial = '1') then
324 27 rherveille
                c_state  <= idle;
325
                cmd_ack  <= '0';
326
                iscl_oen <= '1';
327
                isda_oen <= '1';
328 31 rherveille
                sda_chk  <= '0';
329 27 rherveille
              else
330
                cmd_ack <= '0'; -- default no acknowledge
331 15 rherveille
 
332 27 rherveille
                if (clk_en = '1') then
333
                  case (c_state) is
334
                     -- idle
335
                     when idle =>
336
                        case cmd is
337
                          when I2C_CMD_START => c_state <= start_a;
338
                          when I2C_CMD_STOP  => c_state <= stop_a;
339
                          when I2C_CMD_WRITE => c_state <= wr_a;
340
                          when I2C_CMD_READ  => c_state <= rd_a;
341
                          when others        => c_state <= idle; -- NOP command
342
                        end case;
343 15 rherveille
 
344 27 rherveille
                        iscl_oen <= iscl_oen; -- keep SCL in same state
345
                        isda_oen <= isda_oen; -- keep SDA in same state
346 31 rherveille
                        sda_chk  <= '0';      -- don't check SDA
347 15 rherveille
 
348 27 rherveille
                     -- start
349
                     when start_a =>
350
                        c_state  <= start_b;
351
                        iscl_oen <= iscl_oen; -- keep SCL in same state (for repeated start)
352
                        isda_oen <= '1';      -- set SDA high
353 31 rherveille
                        sda_chk  <= '0';      -- don't check SDA
354 15 rherveille
 
355 27 rherveille
                     when start_b =>
356
                        c_state  <= start_c;
357
                        iscl_oen <= '1'; -- set SCL high
358
                        isda_oen <= '1'; -- keep SDA high
359 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA
360 15 rherveille
 
361 27 rherveille
                     when start_c =>
362
                        c_state  <= start_d;
363
                        iscl_oen <= '1'; -- keep SCL high
364
                        isda_oen <= '0'; -- set SDA low
365 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA
366 15 rherveille
 
367 27 rherveille
                     when start_d =>
368
                        c_state  <= start_e;
369
                        iscl_oen <= '1'; -- keep SCL high
370
                        isda_oen <= '0'; -- keep SDA low
371 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA
372 15 rherveille
 
373 27 rherveille
                     when start_e =>
374
                        c_state  <= idle;
375
                        cmd_ack  <= '1'; -- command completed
376
                        iscl_oen <= '0'; -- set SCL low
377
                        isda_oen <= '0'; -- keep SDA low
378 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA
379 15 rherveille
 
380 27 rherveille
                     -- stop
381
                     when stop_a =>
382
                        c_state  <= stop_b;
383 31 rherveille
                        iscl_oen <= '0'; -- keep SCL low
384 27 rherveille
                        isda_oen <= '0'; -- set SDA low
385 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA
386 15 rherveille
 
387 27 rherveille
                     when stop_b =>
388
                        c_state  <= stop_c;
389
                        iscl_oen <= '1'; -- set SCL high
390
                        isda_oen <= '0'; -- keep SDA low
391 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA
392 15 rherveille
 
393 27 rherveille
                     when stop_c =>
394
                        c_state  <= stop_d;
395
                        iscl_oen <= '1'; -- keep SCL high
396
                        isda_oen <= '0'; -- keep SDA low
397 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA
398 15 rherveille
 
399 27 rherveille
                     when stop_d =>
400
                        c_state  <= idle;
401
                        cmd_ack  <= '1'; -- command completed
402
                        iscl_oen <= '1'; -- keep SCL high
403
                        isda_oen <= '1'; -- set SDA high
404 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA
405 15 rherveille
 
406 27 rherveille
                     -- read
407
                     when rd_a =>
408
                        c_state  <= rd_b;
409
                        iscl_oen <= '0'; -- keep SCL low
410
                        isda_oen <= '1'; -- tri-state SDA
411 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA
412 15 rherveille
 
413 27 rherveille
                     when rd_b =>
414
                        c_state  <= rd_c;
415
                        iscl_oen <= '1'; -- set SCL high
416
                        isda_oen <= '1'; -- tri-state SDA
417 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA
418 15 rherveille
 
419 27 rherveille
                     when rd_c =>
420
                        c_state  <= rd_d;
421
                        iscl_oen <= '1'; -- keep SCL high
422
                        isda_oen <= '1'; -- tri-state SDA
423 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA
424 15 rherveille
 
425 27 rherveille
                     when rd_d =>
426
                        c_state  <= idle;
427
                        cmd_ack  <= '1'; -- command completed
428
                        iscl_oen <= '0'; -- set SCL low
429
                        isda_oen <= '1'; -- tri-state SDA
430 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA
431 15 rherveille
 
432 27 rherveille
                     -- write
433
                     when wr_a =>
434
                        c_state  <= wr_b;
435
                        iscl_oen <= '0'; -- keep SCL low
436
                        isda_oen <= din; -- set SDA
437 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA (SCL low)
438 15 rherveille
 
439 27 rherveille
                     when wr_b =>
440
                        c_state  <= wr_c;
441
                        iscl_oen <= '1'; -- set SCL high
442
                        isda_oen <= din; -- keep SDA
443 31 rherveille
                        sda_chk  <= '1'; -- check SDA
444 15 rherveille
 
445 27 rherveille
                     when wr_c =>
446
                        c_state  <= wr_d;
447
                        iscl_oen <= '1'; -- keep SCL high
448
                        isda_oen <= din; -- keep SDA
449 31 rherveille
                        sda_chk  <= '1'; -- check SDA
450 15 rherveille
 
451 27 rherveille
                     when wr_d =>
452
                        c_state  <= idle;
453
                        cmd_ack  <= '1'; -- command completed
454
                        iscl_oen <= '0'; -- set SCL low
455
                        isda_oen <= din; -- keep SDA
456 31 rherveille
                        sda_chk  <= '0'; -- don't check SDA (SCL low)
457 15 rherveille
 
458 27 rherveille
                     when others =>
459 15 rherveille
 
460 27 rherveille
                  end case;
461 24 rherveille
                end if;
462
              end if;
463
            end if;
464 15 rherveille
        end process nxt_state_decoder;
465
 
466
 
467
        -- assign outputs
468
        scl_o   <= '0';
469
        scl_oen <= iscl_oen;
470
        sda_o   <= '0';
471
        sda_oen <= isda_oen;
472
end architecture structural;
473 34 rherveille
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.