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 31

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

powered by: WebSVN 2.1.0

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