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 15

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

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