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 35

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

powered by: WebSVN 2.1.0

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