SURF  1.0
I2cPkg.vhd
Go to the documentation of this file.
1 ------------------------------------------------------------------------------
2 -- This file is a part of the GRLIB VHDL IP LIBRARY
3 -- Copyright (C) 2003 - 2008, Gaisler Research
4 -- Copyright (C) 2008 - 2012, Aeroflex Gaisler
5 --
6 -- This program is free software; you can redistribute it and/or modify
7 -- it under the terms of the GNU General Public License as published by
8 -- the Free Software Foundation; either version 2 of the License, or
9 -- (at your option) any later version.
10 --
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU General Public License for more details.
15 --
16 -- You should have received a copy of the GNU General Public License
17 -- along with this program; if not, write to the Free Software
18 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 -----------------------------------------------------------------------------
20 -- Package: i2c
21 -- File: i2c.vhd
22 -- Author: Jiri Gaisler - Gaisler Research
23 -- Description: I2C interface package
24 --! @file
25  --! @ingroup protocols_i2c
26 ------------------------------------------------------------------------------
27 
28 library ieee;
29 use ieee.std_logic_1164.all;
30 use work.StdRtlPkg.all;
31 
32 package I2cPkg is
33 
34  type i2c_in_type is record
35  scl : std_ulogic;
36  sda : std_ulogic;
37  end record;
38 
39  type i2c_in_array is array (natural range <>) of i2c_in_type;
40 
41  type i2c_out_type is record
42  scl : std_ulogic;
43  scloen : std_ulogic;
44  sda : std_ulogic;
45  sdaoen : std_ulogic;
46  enable : std_ulogic;
47  end record;
48 
49  type i2c_out_array is array (natural range <>) of i2c_out_type;
50 
51  --------------------------------------------------------------------------------------------------
52  constant I2C_INVALID_ADDR_ERROR_C : slv(7 downto 0) := X"01";
53  constant I2C_WRITE_ACK_ERROR_C : slv(7 downto 0) := X"02";
54  constant I2C_ARBITRATION_LOST_ERROR_C : slv(7 downto 0) := X"03";
55 
56  type I2cMasterInType is record
57  enable : sl; -- Enable the master
58  prescale : slv(15 downto 0); -- Determines i2c clock speed
59  filter : slv(15 downto 0); -- Dynamic filter value
60  txnReq : sl; -- Execute a transaction
61  stop : sl; -- Set STOP when done
62  op : sl; -- 1 for write, 0 for read
63  busReq : sl; -- 1 for bus request, 0 for read/write
64  addr : slv(9 downto 0); -- i2c device address
65  tenbit : sl; -- use 10 bit addressing
67  wrData : slv(7 downto 0); -- Data sent during write txn
68  rdAck : sl;
69  end record;
70 
71  type I2cMasterOutType is record
73  txnError : sl; -- An error occured during the txn
74  wrAck : sl;
76  rdData : slv(7 downto 0); -- Data received during read txn
77  end record;
78  --------------------------------------------------------------------------------------------------
79  type I2cRegMasterInType is record
80  i2cAddr : slv(9 downto 0);
81  tenbit : sl;
82  regAddr : slv(31 downto 0);
83  regWrData : slv(31 downto 0);
84  regOp : sl;
85  regAddrSkip : sl;
86  regAddrSize : slv(1 downto 0);
87  regDataSize : slv(1 downto 0);
88  regReq : sl;
89  busReq : sl;
92  end record;
93 
95  i2cAddr => (others => '0'),
96  tenbit => '0',
97  regAddr => (others => '0'),
98  regWrData => (others => '0'),
99  regOp => '0', -- 1 for write, 0 for read
100  regAddrSkip => '0',
101  regAddrSize => (others => '0'),
102  regDataSize => (others => '0'),
103  regReq => '0',
104  busReq => '0',
105  endianness => '0',
106  repeatStart => '0');
107 
108  type I2cRegMasterInArray is array (natural range <>) of I2cRegMasterInType;
109 
110  type I2cRegMasterOutType is record
111  regAck : sl;
112  regFail : sl;
113  regFailCode : slv(7 downto 0);
114  regRdData : slv(31 downto 0);
115  end record;
116 
118  regAck => '0',
119  regFail => '0',
120  regFailCode => (others => '0'),
121  regRdData => (others => '0'));
122 
123  type I2cRegMasterOutArray is array (natural range <>) of I2cRegMasterOutType;
124 
125  --------------------------------------------------------------------------------------------------
126  type I2cSlaveInType is record
127  enable : sl;
129  txData : slv(7 downto 0);
131  end record I2cSlaveInType;
132 
134  enable => '0',
135  txValid => '0',
136  txData => (others => '0'),
137  rxAck => '0');
138 
139  type I2cSlaveOutType is record
142  rxData : slv(7 downto 0);
145  nack : sl;
146  end record I2cSlaveOutType;
147 
149  rxActive => '0',
150  rxValid => '0',
151  rxData => (others => '0'),
152  txActive => '0',
153  txAck => '0',
154  nack => '0');
155 
156  -------------------------------------------------------------------------------------------------
157  -- AXI Bridge Generic Type, stick here for now
158  -------------------------------------------------------------------------------------------------
159  type I2cAxiLiteDevType is record
160  i2cAddress : slv(9 downto 0);
162  dataSize : integer;
163  addrSize : integer;
164  endianness : sl;
165  repeatStart : sl;
166  end record I2cAxiLiteDevType;
167 
169  i2cAddress : slv;
170  dataSize : integer;
171  addrSize : integer;
172  endianness : sl;
173  repeatStart : sl := '0')
174  return I2cAxiLiteDevType;
175 
176  type I2cAxiLiteDevArray is array (natural range <>) of I2cAxiLiteDevType;
177 
179  0 => (MakeI2cAxiLiteDevType("0000000", 8, 8, '0')),
180  1 => (MakeI2cAxiLiteDevType("0000010", 16, 16, '0')),
181  2 => (MakeI2cAxiLiteDevType("0000100", 32, 8, '0')),
182  3 => (MakeI2cAxiLiteDevType("0001000", 32, 32, '0')));
183 
184  function maxAddrSize (constant devMap : I2cAxiLiteDevArray) return natural;
185 
186  --------------------------------------------------------------------------------------------------
187  -- Opencores i2c
188  component i2c_master_byte_ctrl is
189  generic (filter : integer; dynfilt : integer);
190  port (
191  clk : in std_logic;
192  rst : in std_logic; -- active high reset
193  nReset : in std_logic; -- asynchornous active low reset
194  -- (not used in GRLIB)
195  ena : in std_logic; -- core enable signal
196 
197  clk_cnt : in std_logic_vector(15 downto 0); -- 4x SCL
198 
199  -- input signals
200  start,
201  stop,
202  read,
203  write,
204  ack_in : std_logic;
205  din : in std_logic_vector(7 downto 0);
206  filt : in std_logic_vector((filter-1)*dynfilt downto 0);
207 
208  -- output signals
209  cmd_ack : out std_logic;
210  ack_out : out std_logic;
211  i2c_busy : out std_logic;
212  i2c_al : out std_logic;
213  dout : out std_logic_vector(7 downto 0);
214 
215  -- i2c lines
216  scl_i : in std_logic; -- i2c clock line input
217  scl_o : out std_logic; -- i2c clock line output
218  scl_oen : out std_logic; -- i2c clock line output enable, active low
219  sda_i : in std_logic; -- i2c data line input
220  sda_o : out std_logic; -- i2c data line output
221  sda_oen : out std_logic -- i2c data line output enable, active low
222  );
223  end component i2c_master_byte_ctrl;
224 
225 
226 
227 end;
228 
229 package body I2cPkg is
230 
232  i2cAddress : slv;
233  dataSize : integer;
234  addrSize : integer;
235  endianness : sl;
236  repeatStart : sl := '0')
237  return I2cAxiLiteDevType
238  is
239  variable ret : I2cAxiLiteDevType;
240  begin
241  if (i2cAddress'length = 7) then
242  ret.i2cAddress := "000" & i2cAddress;
243  ret.i2cTenbit := '0';
244  elsif (i2cAddress'length = 10) then
245  ret.i2cAddress := i2cAddress;
246  ret.i2cTenbit := '1';
247  else
248  report "i2cAddress param must have length of 7 or 10" severity error;
249  end if;
250 
251  ret.dataSize := dataSize;
252  ret.addrSize := addrSize;
253  ret.endianness := endianness;
254  ret.repeatStart := repeatStart;
255  return ret;
256  end function MakeI2cAxiLiteDevType;
257 
258  function maxAddrSize (constant devMap : I2cAxiLiteDevArray) return natural is
259  variable ret : natural := 0;
260  begin
261  for i in devMap'range loop
262  if (devMap(i).addrSize > ret) then
263  ret := devMap(i).addrSize;
264  end if;
265  end loop;
266  return ret;
267  end function maxAddrSize;
268 
269 end package body I2cPkg;
array(natural range <> ) of I2cRegMasterInType I2cRegMasterInArray
Definition: I2cPkg.vhd:108
sl op
Definition: I2cPkg.vhd:62
I2cMasterInType
Definition: I2cPkg.vhd:56
in filtstd_logic_vector(( filter- 1)* dynfilt downto 0)
I2cRegMasterOutType :=(regAck => '0',regFail => '0',regFailCode =>( others => '0'),regRdData =>( others => '0')) I2C_REG_MASTER_OUT_INIT_C
Definition: I2cPkg.vhd:117
integer addrSize
Definition: I2cPkg.vhd:163
sl rdValid
Definition: I2cPkg.vhd:75
in clk_cntstd_logic_vector( 15 downto 0)
slv( 7 downto 0) rdData
Definition: I2cPkg.vhd:76
i2c_in_type
Definition: I2cPkg.vhd:34
std_logic sl
Definition: StdRtlPkg.vhd:28
sl txnReq
Definition: I2cPkg.vhd:60
sl stop
Definition: I2cPkg.vhd:61
integer dataSize
Definition: I2cPkg.vhd:162
slv( 15 downto 0) prescale
Definition: I2cPkg.vhd:58
I2cSlaveOutType :=(rxActive => '0',rxValid => '0',rxData =>( others => '0'),txActive => '0',txAck => '0',nack => '0') I2C_SLAVE_OUT_INIT_C
Definition: I2cPkg.vhd:148
sl endianness
Definition: I2cPkg.vhd:90
sl rxValid
Definition: I2cPkg.vhd:141
I2cSlaveInType
Definition: I2cPkg.vhd:126
slv( 7 downto 0) := X"01" I2C_INVALID_ADDR_ERROR_C
Definition: I2cPkg.vhd:52
I2cMasterOutType
Definition: I2cPkg.vhd:71
array(natural range <> ) of I2cAxiLiteDevType I2cAxiLiteDevArray
Definition: I2cPkg.vhd:176
sl txValid
Definition: I2cPkg.vhd:128
i2c_master_byte_ctrl
Definition: I2cPkg.vhd:188
std_ulogic scl
Definition: I2cPkg.vhd:35
slv( 7 downto 0) wrData
Definition: I2cPkg.vhd:67
sl rxActive
Definition: I2cPkg.vhd:140
slv( 7 downto 0) txData
Definition: I2cPkg.vhd:129
I2cRegMasterOutType
Definition: I2cPkg.vhd:110
array(natural range <> ) of I2cRegMasterOutType I2cRegMasterOutArray
Definition: I2cPkg.vhd:123
sl txActive
Definition: I2cPkg.vhd:143
i2c_out_type
Definition: I2cPkg.vhd:41
I2cRegMasterInType :=(i2cAddr =>( others => '0'),tenbit => '0',regAddr =>( others => '0'),regWrData =>( others => '0'),regOp => '0',regAddrSkip => '0',regAddrSize =>( others => '0'),regDataSize =>( others => '0'),regReq => '0',busReq => '0',endianness => '0',repeatStart => '0') I2C_REG_MASTER_IN_INIT_C
Definition: I2cPkg.vhd:94
sl rdAck
Definition: I2cPkg.vhd:68
slv( 15 downto 0) filter
Definition: I2cPkg.vhd:59
sl txnError
Definition: I2cPkg.vhd:73
sl tenbit
Definition: I2cPkg.vhd:65
slv( 7 downto 0) rxData
Definition: I2cPkg.vhd:142
slv( 7 downto 0) := X"02" I2C_WRITE_ACK_ERROR_C
Definition: I2cPkg.vhd:53
slv( 9 downto 0) i2cAddress
Definition: I2cPkg.vhd:160
I2cAxiLiteDevType MakeI2cAxiLiteDevTypei2cAddress,dataSize,addrSize,endianness,repeatStart,
Definition: I2cPkg.vhd:168
I2cSlaveInType :=(enable => '0',txValid => '0',txData =>( others => '0'),rxAck => '0') I2C_SLAVE_IN_INIT_C
Definition: I2cPkg.vhd:133
slv( 9 downto 0) i2cAddr
Definition: I2cPkg.vhd:80
array(natural range <> ) of i2c_out_type i2c_out_array
Definition: I2cPkg.vhd:49
I2cSlaveOutType
Definition: I2cPkg.vhd:139
sl busAck
Definition: I2cPkg.vhd:72
sl nack
Definition: I2cPkg.vhd:145
I2cRegMasterInType
Definition: I2cPkg.vhd:79
slv( 9 downto 0) addr
Definition: I2cPkg.vhd:64
array(natural range <> ) of i2c_in_type i2c_in_array
Definition: I2cPkg.vhd:39
sl rxAck
Definition: I2cPkg.vhd:130
natural maxAddrSizedevMap,
Definition: I2cPkg.vhd:184
I2cAxiLiteDevType
Definition: I2cPkg.vhd:159
I2cAxiLiteDevArray( 0 to 3) :=( 0=>( MakeI2cAxiLiteDevType( "0000000", 8, 8, '0')), 1=>( MakeI2cAxiLiteDevType( "0000010", 16, 16, '0')), 2=>( MakeI2cAxiLiteDevType( "0000100", 32, 8, '0')), 3=>( MakeI2cAxiLiteDevType( "0001000", 32, 32, '0'))) I2C_AXIL_DEV_ARRAY_DEFAULT_C
Definition: I2cPkg.vhd:178
out doutstd_logic_vector( 7 downto 0)
_library_ ieeeieee
Definition: I2cMaster.vhd:40
in dinstd_logic_vector( 7 downto 0)
slv( 7 downto 0) := X"03" I2C_ARBITRATION_LOST_ERROR_C
Definition: I2cPkg.vhd:54
sl busReq
Definition: I2cPkg.vhd:63
std_ulogic scloen
Definition: I2cPkg.vhd:43
sl txAck
Definition: I2cPkg.vhd:144
std_ulogic sda
Definition: I2cPkg.vhd:36
sl wrValid
Definition: I2cPkg.vhd:66
sl repeatStart
Definition: I2cPkg.vhd:91
sl wrAck
Definition: I2cPkg.vhd:74
sl i2cTenbit
Definition: I2cPkg.vhd:161
std_ulogic sdaoen
Definition: I2cPkg.vhd:45
std_logic_vector slv
Definition: StdRtlPkg.vhd:29