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

Subversion Repositories i2c

[/] [i2c/] [trunk/] [rtl/] [vhdl/] [i2c_master_top.vhd] - Blame information for rev 38

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; top level           ----
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 38 rherveille
--  $Id: i2c_master_top.vhd,v 1.6 2003-08-09 07:01:13 rherveille Exp $
41 15 rherveille
--
42 38 rherveille
--  $Date: 2003-08-09 07:01:13 $
43
--  $Revision: 1.6 $
44 15 rherveille
--  $Author: rherveille $
45
--  $Locker:  $
46
--  $State: Exp $
47
--
48
-- Change History:
49
--               $Log: not supported by cvs2svn $
50 38 rherveille
--               Revision 1.5  2003/02/01 02:03:06  rherveille
51
--               Fixed a few 'arbitration lost' bugs. VHDL version only.
52
--
53 34 rherveille
--               Revision 1.4  2002/12/26 16:05:47  rherveille
54
--               Core is now a Multimaster I2C controller.
55
--
56 31 rherveille
--               Revision 1.3  2002/11/30 22:24:37  rherveille
57
--               Cleaned up code
58
--
59 27 rherveille
--               Revision 1.2  2001/11/10 10:52:44  rherveille
60
--               Changed PRER reset value from 0x0000 to 0xffff, conform specs.
61
--
62 15 rherveille
 
63
 
64
library ieee;
65
use ieee.std_logic_1164.all;
66
use ieee.std_logic_arith.all;
67
 
68
entity i2c_master_top is
69
        generic(
70 27 rherveille
                ARST_LVL : std_logic := '0'                   -- asynchronous reset level
71 15 rherveille
        );
72
        port (
73
                -- wishbone signals
74 27 rherveille
                wb_clk_i  : in  std_logic;                    -- master clock input
75
                wb_rst_i  : in  std_logic := '0';             -- synchronous active high reset
76
                arst_i    : in  std_logic := not ARST_LVL;    -- asynchronous reset
77
                wb_adr_i  : in  unsigned(2 downto 0);         -- lower address bits
78
                wb_dat_i  : in  std_logic_vector(7 downto 0); -- Databus input
79
                wb_dat_o  : out std_logic_vector(7 downto 0); -- Databus output
80
                wb_we_i   : in  std_logic;                    -- Write enable input
81
                wb_stb_i  : in  std_logic;                    -- Strobe signals / core select signal
82
                wb_cyc_i  : in  std_logic;                    -- Valid bus cycle input
83
                wb_ack_o  : out std_logic;                    -- Bus cycle acknowledge output
84
                wb_inta_o : out std_logic;                    -- interrupt request output signal
85 15 rherveille
 
86
                -- i2c lines
87
                scl_pad_i     : in  std_logic;                -- i2c clock line input
88
                scl_pad_o     : out std_logic;                -- i2c clock line output
89
                scl_padoen_o  : out std_logic;                -- i2c clock line output enable, active low
90
                sda_pad_i     : in  std_logic;                -- i2c data line input
91
                sda_pad_o     : out std_logic;                -- i2c data line output
92
                sda_padoen_o  : out std_logic                 -- i2c data line output enable, active low
93
        );
94
end entity i2c_master_top;
95
 
96
architecture structural of i2c_master_top is
97
        component i2c_master_byte_ctrl is
98 27 rherveille
        port (
99
                clk    : in std_logic;
100
                rst    : in std_logic; -- synchronous active high reset (WISHBONE compatible)
101
                nReset : in std_logic;  -- asynchornous active low reset (FPGA compatible)
102
                ena    : in std_logic; -- core enable signal
103 15 rherveille
 
104 27 rherveille
                clk_cnt : in unsigned(15 downto 0);      -- 4x SCL
105 15 rherveille
 
106 27 rherveille
                -- input signals
107
                start,
108
                stop,
109
                read,
110
                write,
111
                ack_in : std_logic;
112
                din    : in std_logic_vector(7 downto 0);
113 15 rherveille
 
114 27 rherveille
                -- output signals
115
                cmd_ack  : out std_logic;
116
                ack_out  : out std_logic;
117
                i2c_busy : out std_logic;
118 31 rherveille
                i2c_al   : out std_logic;
119 27 rherveille
                dout     : out std_logic_vector(7 downto 0);
120 15 rherveille
 
121 27 rherveille
                -- i2c lines
122
                scl_i   : in std_logic;  -- i2c clock line input
123
                scl_o   : out std_logic; -- i2c clock line output
124
                scl_oen : out std_logic; -- i2c clock line output enable, active low
125
                sda_i   : in std_logic;  -- i2c data line input
126
                sda_o   : out std_logic; -- i2c data line output
127
                sda_oen : out std_logic  -- i2c data line output enable, active low
128
        );
129 15 rherveille
        end component i2c_master_byte_ctrl;
130
 
131
        -- registers
132 27 rherveille
        signal prer : unsigned(15 downto 0);             -- clock prescale register
133
        signal ctr  : std_logic_vector(7 downto 0);      -- control register
134
        signal txr  : std_logic_vector(7 downto 0);      -- transmit register
135
        signal rxr  : std_logic_vector(7 downto 0);      -- receive register
136
        signal cr   : std_logic_vector(7 downto 0);      -- command register
137
        signal sr   : std_logic_vector(7 downto 0);      -- status register
138 15 rherveille
 
139
        -- internal reset signal
140
        signal rst_i : std_logic;
141
 
142 31 rherveille
        -- wishbone write access
143
        signal wb_wacc : std_logic;
144
 
145 27 rherveille
        -- internal acknowledge signal
146
        signal iack_o : std_logic;
147
 
148 15 rherveille
        -- done signal: command completed, clear command register
149
        signal done : std_logic;
150
 
151
        -- command register signals
152
        signal sta, sto, rd, wr, ack, iack : std_logic;
153
 
154 31 rherveille
        signal core_en : std_logic;                      -- core enable signal
155
        signal ien     : std_logic;                      -- interrupt enable signal
156 15 rherveille
 
157
        -- status register signals
158 27 rherveille
        signal irxack, rxack : std_logic;                -- received aknowledge from slave
159 31 rherveille
        signal tip           : std_logic;                -- transfer in progress
160
        signal irq_flag      : std_logic;                -- interrupt pending flag
161
        signal i2c_busy      : std_logic;                -- i2c bus busy (start signal detected)
162
        signal i2c_al, al    : std_logic;                -- arbitration lost
163 15 rherveille
 
164
begin
165
        -- generate internal reset signal
166
        rst_i <= arst_i xor ARST_LVL;
167
 
168
        -- generate acknowledge output signal
169 27 rherveille
        gen_ack_o : process(wb_clk_i)
170 15 rherveille
        begin
171 27 rherveille
            if (wb_clk_i'event and wb_clk_i = '1') then
172
              iack_o <= wb_cyc_i and wb_stb_i and not iack_o;         -- because timing is always honored
173
            end if;
174
        end process gen_ack_o;
175
        wb_ack_o <= iack_o;
176 31 rherveille
 
177
 
178
        -- generate wishbone write access signal
179
        wb_wacc <= wb_cyc_i and wb_stb_i and wb_we_i;
180 15 rherveille
 
181 27 rherveille
        -- assign wb_dat_o
182
        assign_dato : process(wb_clk_i)
183
        begin
184
            if (wb_clk_i'event and wb_clk_i = '1') then
185
              case wb_adr_i is
186
                when "000"  => wb_dat_o <= std_logic_vector(prer( 7 downto 0));
187
                when "001"  => wb_dat_o <= std_logic_vector(prer(15 downto 8));
188
                when "010"  => wb_dat_o <= ctr;
189
                when "011"  => wb_dat_o <= rxr; -- write is transmit register TxR
190
                when "100"  => wb_dat_o <= sr;  -- write is command register CR
191 15 rherveille
 
192 27 rherveille
                -- Debugging registers:
193
                -- These registers are not documented.
194
                -- Functionality could change in future releases
195
                when "101"  => wb_dat_o <= txr;
196
                when "110"  => wb_dat_o <= cr;
197
                when "111"  => wb_dat_o <= (others => '0');
198
                when others => wb_dat_o <= (others => 'X');     -- for simulation only
199
              end case;
200
            end if;
201 15 rherveille
        end process assign_dato;
202
 
203
 
204 31 rherveille
        -- generate registers (CR, SR see below)
205
        gen_regs: process(rst_i, wb_clk_i)
206 15 rherveille
        begin
207 27 rherveille
            if (rst_i = '0') then
208
              prer <= (others => '1');
209
              ctr  <= (others => '0');
210
              txr  <= (others => '0');
211
            elsif (wb_clk_i'event and wb_clk_i = '1') then
212
              if (wb_rst_i = '1') then
213
                prer <= (others => '1');
214
                ctr  <= (others => '0');
215
                txr  <= (others => '0');
216 31 rherveille
              elsif (wb_wacc = '1') then
217
                case wb_adr_i is
218
                   when "000" => prer( 7 downto 0) <= unsigned(wb_dat_i);
219
                   when "001" => prer(15 downto 8) <= unsigned(wb_dat_i);
220
                   when "010" => ctr               <= wb_dat_i;
221
                   when "011" => txr               <= wb_dat_i;
222 15 rherveille
 
223 31 rherveille
                   -- illegal cases, for simulation only
224
                   when others =>
225
                      report ("Illegal write address, setting all registers to unknown.");
226
                      prer <= (others => 'X');
227
                      ctr  <= (others => 'X');
228
                      txr  <= (others => 'X');
229
                end case;
230
              end if;
231
            end if;
232
        end process gen_regs;
233 15 rherveille
 
234
 
235 31 rherveille
        -- generate command register
236
        gen_cr: process(rst_i, wb_clk_i)
237
        begin
238
            if (rst_i = '0') then
239 38 rherveille
                cr <= (others => '0');
240 31 rherveille
            elsif (wb_clk_i'event and wb_clk_i = '1') then
241 38 rherveille
                if (wb_rst_i = '1') then
242
                    cr <= (others => '0');
243
                elsif (wb_wacc = '1') then
244
                    if ( (core_en = '1') and (wb_adr_i = 4) ) then
245
                        -- only take new commands when i2c core enabled
246
                        -- pending commands are finished
247
                        cr <= wb_dat_i;
248
                    end if;
249
                else
250
                    if (done = '1' or i2c_al = '1') then
251
                        cr(7 downto 4) <= (others => '0'); -- clear command bits when command done or arbitration lost
252
                    end if;
253 31 rherveille
 
254 38 rherveille
                    cr(2 downto 1) <= (others => '0');   -- reserved bits, always '0'
255
                    cr(0) <= '0';                        -- clear IRQ_ACK bit
256
                end if;
257 27 rherveille
            end if;
258 31 rherveille
        end process gen_cr;
259 27 rherveille
 
260 15 rherveille
        -- decode command register
261
        sta  <= cr(7);
262
        sto  <= cr(6);
263
        rd   <= cr(5);
264
        wr   <= cr(4);
265
        ack  <= cr(3);
266
        iack <= cr(0);
267
 
268
        -- decode control register
269
        core_en <= ctr(7);
270
        ien     <= ctr(6);
271
 
272
        -- hookup byte controller block
273 31 rherveille
        byte_ctrl: i2c_master_byte_ctrl port map (
274 15 rherveille
                clk      => wb_clk_i,
275
                rst      => wb_rst_i,
276
                nReset   => rst_i,
277 27 rherveille
                ena      => core_en,
278 15 rherveille
                clk_cnt  => prer,
279
                start    => sta,
280
                stop     => sto,
281
                read     => rd,
282
                write    => wr,
283
                ack_in   => ack,
284
                i2c_busy => i2c_busy,
285 31 rherveille
                i2c_al   => i2c_al,
286 15 rherveille
                din      => txr,
287
                cmd_ack  => done,
288
                ack_out  => irxack,
289
                dout     => rxr,
290
                scl_i    => scl_pad_i,
291
                scl_o    => scl_pad_o,
292
                scl_oen  => scl_padoen_o,
293
                sda_i    => sda_pad_i,
294
                sda_o    => sda_pad_o,
295
                sda_oen  => sda_padoen_o
296
        );
297
 
298
 
299
        -- status register block + interrupt request signal
300
        st_irq_block : block
301
        begin
302 27 rherveille
            -- generate status register bits
303
            gen_sr_bits: process (wb_clk_i, rst_i)
304
            begin
305
                if (rst_i = '0') then
306 31 rherveille
                  al       <= '0';
307 27 rherveille
                  rxack    <= '0';
308
                  tip      <= '0';
309
                  irq_flag <= '0';
310
                elsif (wb_clk_i'event and wb_clk_i = '1') then
311
                  if (wb_rst_i = '1') then
312 31 rherveille
                    al       <= '0';
313 27 rherveille
                    rxack    <= '0';
314
                    tip      <= '0';
315
                    irq_flag <= '0';
316
                  else
317 31 rherveille
                    al       <= i2c_al or (al and not sta);
318 27 rherveille
                    rxack    <= irxack;
319
                    tip      <= (rd or wr);
320 15 rherveille
 
321 27 rherveille
                    -- interrupt request flag is always generated
322 31 rherveille
                    irq_flag <= (done or i2c_al or irq_flag) and not iack;
323 27 rherveille
                  end if;
324
                end if;
325
            end process gen_sr_bits;
326 15 rherveille
 
327 27 rherveille
            -- generate interrupt request signals
328
            gen_irq: process (wb_clk_i, rst_i)
329
            begin
330
                if (rst_i = '0') then
331
                  wb_inta_o <= '0';
332
                elsif (wb_clk_i'event and wb_clk_i = '1') then
333
                  if (wb_rst_i = '1') then
334
                    wb_inta_o <= '0';
335
                  else
336
                    -- interrupt signal is only generated when IEN (interrupt enable bit) is set
337
                    wb_inta_o <= irq_flag and ien;
338
                  end if;
339
                end if;
340
            end process gen_irq;
341 15 rherveille
 
342 27 rherveille
            -- assign status register bits
343
            sr(7)          <= rxack;
344
            sr(6)          <= i2c_busy;
345 34 rherveille
            sr(5)          <= al;
346
            sr(4 downto 2) <= (others => '0'); -- reserved
347 27 rherveille
            sr(1)          <= tip;
348
            sr(0)          <= irq_flag;
349 15 rherveille
        end block;
350
 
351
end architecture structural;

powered by: WebSVN 2.1.0

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