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

Subversion Repositories i2c

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

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

powered by: WebSVN 2.1.0

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