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 48

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

powered by: WebSVN 2.1.0

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