SURF  1.0
I2cSlave.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 -- Entity: i2cslv
21 -- File: i2cslv.vhd
22 -- Author: Jan Andersson - Gaisler Research
23 -- jan@gaisler.com
24 --
25 -- Documentation of generics:
26 --
27 -- [TENBIT_G]
28 -- Support for ten bit addresses.
29 --
30 -- [I2C_ADDR_G]
31 -- The slave's i2c address.
32 --
33 -- [OUTPUT_EN_POLARITY_G]
34 -- Output enable polarity
35 --
36 -- [FILTER_G]
37 -- Length of filters used on SCL and SDA.
38 -- This generic should specify, in number of system clock cycles plus one,
39 -- the time of the shortest pulse on the I2C bus to be registered as a valid
40 -- value. For instance, to disregard any pulse that is 50 ns or shorter in
41 -- a system with a system frequency of 54 MHz this generic should be set to:
42 -- ((pulse time) / (clock period)) + 1 = (50 ns) / ((1/(54 MHz)) + 1 = 3.7
43 -- The value from this calculation should always be rounded up.
44 -- In other words an appropriate filter length for a 54 MHz system is 4.
45 --
46 -- The slave has four different modes operation. The mode is defined by the
47 -- value of the bits RMODE and TMODE.
48 -- RMODE TMODE I2CSLAVE Mode
49 -- 0 0 0
50 -- 0 1 1
51 -- 1 0 2
52 -- 1 1 3
53 --
54 -- RMODE_G 0:
55 -- The slave accepts one byte and NAKs all other transfers until software has
56 -- acknowledged the received byte.
57 -- RMODE_G 1:
58 -- The slave accepts one byte and keeps SCL low until software has acknowledged
59 -- the received byte
60 -- TMODE_G 0:
61 -- The slave transmits the same byte to all if the master requests more than
62 -- one byte in the transfer. The slave then NAKs all read requests unless the
63 -- Transmit Always Valid (TAV) bit in the control register is set.
64 -- TMODE_G 1:
65 -- The slave transmits one byte and then keeps SCL low until software has
66 -- acknowledged that the byte has been transmitted.
67 ----------------------------------------------------------------------------------------------------
68 -- Modified by Benjamin Reese <bareese@slac.stanford.edu>
69 -- Removed APB interface and replaced with generic IO.
70 
71 
72 library ieee;
73 use ieee.std_logic_1164.all;
74 use ieee.numeric_std.all;
75 use work.StdRtlPkg.all;
76 use work.I2cPkg.all;
77 use work.stdlib.all;
78 
79 
80 --! @see entity
81  --! @ingroup protocols_i2c
82 entity I2cSlave is
83  generic (
84  TPD_G : time := 1 ns;
85  -- I2C configuration
86  TENBIT_G : integer range 0 to 1 := 0;
87  I2C_ADDR_G : integer range 0 to 1023 := 0;
88  OUTPUT_EN_POLARITY_G : integer range 0 to 1 := 0;
89  FILTER_G : integer range 2 to 512 := 4;
90  RMODE_G : integer range 0 to 1 := 0;
91  TMODE_G : integer range 0 to 1 := 0
92  );
93  port (
94  sRst : in std_ulogic := '0'; -- Synchronous Reset - active high
95  aRst : in std_ulogic := '0'; -- Asynchronous Reset - active high
96  clk : in std_ulogic;
97  -- Front End
100  -- I2C signals
102  i2co : out i2c_out_type
103  );
104 end entity I2cSlave;
105 
106 architecture rtl of I2cSlave is
107  -----------------------------------------------------------------------------
108  -- Constants
109  -----------------------------------------------------------------------------
110  -- Core configuration
111  constant I2C_ADDR_LEN_C : integer := 7 + TENBIT_G*3;
112  constant I2C_SLAVE_ADDR_C : std_logic_vector((I2C_ADDR_LEN_C-1) downto 0) :=
113  conv_std_logic_vector(I2C_ADDR_G, I2C_ADDR_LEN_C);
114 
115  -- Misc constants
116  constant I2C_READ_C : std_ulogic := '1'; -- R/Wn bit
117  constant I2C_WRITE_C : std_ulogic := '0';
118 
119  constant OEPOL_LEVEL_C : std_ulogic := conv_std_logic(OUTPUT_EN_POLARITY_G = 1);
120 
121  constant I2C_LOW_C : std_ulogic := OEPOL_LEVEL_C; -- OE
122  constant I2C_HIZ_C : std_ulogic := not OEPOL_LEVEL_C;
123 
124  constant I2C_ACK_C : std_ulogic := '0';
125 
126  constant TENBIT_ADDR_START_C : std_logic_vector(4 downto 0) := "11110";
127 
128  -----------------------------------------------------------------------------
129  -- Types
130  -----------------------------------------------------------------------------
131  type i2c_in_array is array (FILTER_G downto 0) of i2c_in_type;
132 
133  type slv_state_type is (idle, checkaddr, check10bitaddr, sclhold,
134  movebyte, handshake);
135 
136  type i2cslv_reg_type is record
137  slvstate : slv_state_type;
138  -- Transfer phase
139  active : boolean;
140  addr : boolean;
141 -- transmit : boolean;
142 -- receive : boolean;
143  -- Shift register
144  sreg : std_logic_vector(7 downto 0);
145  cnt : std_logic_vector(2 downto 0);
146  -- Synchronizers for inputs SCL and SDA
147  scl : std_ulogic;
148  sda : std_ulogic;
149  i2ci : i2c_in_array;
150  -- Output enables
151  scloen : std_ulogic;
152  sdaoen : std_ulogic;
153  -- Registered Outputs
154  o : I2cSlaveOutType;
155  end record;
156 
157  constant REG_INIT_C : i2cslv_reg_type := (
158  slvstate => idle,
159  active => false,
160  addr => false,
161  sreg => (others => '0'),
162  cnt => (others => '0'),
163  scl => '0',
164  sda => '0',
165  i2ci => (others => (scl => '0', sda => '0')),
166  scloen => I2C_HIZ_C,
167  sdaoen => I2C_HIZ_C,
168  o => I2C_SLAVE_OUT_INIT_C);
169 
170  -----------------------------------------------------------------------------
171  -- Subprograms
172  -----------------------------------------------------------------------------
173  -- purpose: Compares the first byte of a received address with the slave's
174  -- address. The tba input determines if the slave is using a ten bit address.
175  function compaddr1stb (
176  ibyte : std_logic_vector(7 downto 0)) -- I2C byte
177  return boolean
178  is
179  variable correct : std_logic_vector(7 downto 1);
180  begin -- compaddr1stb
181  if TENBIT_G = 1 then
182  correct(7 downto 3) := TENBIT_ADDR_START_C;
183  correct(2 downto 1) := I2C_SLAVE_ADDR_C((I2C_ADDR_LEN_C-1) downto (I2C_ADDR_LEN_C-2));
184  else
185  correct(7 downto 1) := I2C_SLAVE_ADDR_C(6 downto 0);
186  end if;
187  return ibyte(7 downto 1) = correct(7 downto 1);
188  end compaddr1stb;
189 
190  -- purpose: Compares the 2nd byte of a ten bit address with the slave address
191  function compaddr2ndb (
192  ibyte : std_logic_vector(7 downto 0)) -- I2C byte
193  return boolean is
194  begin -- compaddr2ndb
195  return ibyte((I2C_ADDR_LEN_C-3) downto 0) = I2C_SLAVE_ADDR_C((I2C_ADDR_LEN_C-3) downto 0);
196  end compaddr2ndb;
197 
198  -----------------------------------------------------------------------------
199  -- Signals
200  -----------------------------------------------------------------------------
201 
202  -- Register interface
203  signal r : i2cslv_reg_type := REG_INIT_C;
204  signal rin : i2cslv_reg_type;
205 
206 begin
207 
208  comb : process (r, sRst, i2ci, i2cSlaveIn)
209  variable v : i2cslv_reg_type;
210  variable sclfilt : std_logic_vector(FILTER_G-1 downto 0);
211  variable sdafilt : std_logic_vector(FILTER_G-1 downto 0);
212  begin -- process comb
213  v := r;
214 
215  v.i2ci(0) := i2ci; v.i2ci(FILTER_G downto 1) := r.i2ci(FILTER_G-1 downto 0);
216 
217  ----------------------------------------------------------------------------
218  -- Bus filtering
219  ----------------------------------------------------------------------------
220  for i in 0 to FILTER_G-1 loop
221  sclfilt(i) := r.i2ci(i+1).scl; sdafilt(i) := r.i2ci(i+1).sda;
222  end loop; -- i
223  if andv(sclfilt) = '1' then v.scl := '1'; end if;
224  if orv(sclfilt) = '0' then v.scl := '0'; end if;
225  if andv(sdafilt) = '1' then v.sda := '1'; end if;
226  if orv(sdafilt) = '0' then v.sda := '0'; end if;
227 
228  -- txAck pulsed for 1 clock only when set by state machine below.
229  v.o.txAck := '0';
230 
231  -- Reset rxValid when ack'd from IO
232  if (r.o.rxValid = '1' and i2cSlaveIn.rxAck = '1') then
233  v.o.rxValid := '0';
234  end if;
235 
236  ---------------------------------------------------------------------------
237  -- I2C slave control FSM
238  ---------------------------------------------------------------------------
239  case r.slvstate is
240  when idle =>
241  -- Release bus
242  if (r.scl and not v.scl) = '1' then
243  v.sdaoen := I2C_HIZ_C;
244  end if;
245 
246  when checkaddr =>
247  if compaddr1stb(r.sreg) then
248  if r.sreg(0) = I2C_READ_C then
249  if (TENBIT_G = 0 or (TENBIT_G = 1 and r.active)) then
250  if i2cSlaveIn.txValid = '1' then
251  -- Transmit data
252  v.o.txActive := '1';
253  v.slvstate := handshake;
254  else
255  -- No data to transmit, NAK
256  v.o.nack := '1';
257  v.slvstate := idle;
258  end if;
259  else
260  -- Ten bit address with R/Wn = 1 and slave not previously
261  -- addressed.
262  v.slvstate := idle;
263  end if;
264  else
265  v.o.rxActive := toSl(TENBIT_G = 0);
266  v.slvstate := handshake;
267  end if;
268  else
269  -- Slave address did not match
270  v.active := false;
271  v.slvstate := idle;
272  end if;
273  v.sreg := i2cSlaveIn.txData;
274 
275  when check10bitaddr =>
276  if compaddr2ndb(r.sreg) then
277  -- Slave has been addressed with a matching 10 bit address
278  -- If we receive a repeated start condition, matching address
279  -- and R/Wn = 1 we will transmit data. Without start condition we
280  -- will receive data.
281  v.addr := true;
282  v.active := true;
283  v.o.rxActive := '1';
284  v.slvstate := handshake;
285  else
286  v.slvstate := idle;
287  end if;
288 
289  when sclhold =>
290  -- This state is used when the device has been addressed to see if SCL
291  -- should be kept low until the receive register is free or the
292  -- transmit register is filled. It is also used when a data byte has
293  -- been transmitted or received to SCL low until software acknowledges
294  -- the transfer.
295  if (r.scl and not v.scl) = '1' then
296  v.scloen := I2C_LOW_C;
297  v.sdaoen := I2C_HIZ_C;
298  end if;
299  -- Ack has happened and rxValid set back to '0'
300  if ((r.o.rxActive = '1' and (r.o.rxValid = '0' or RMODE_G = 0)) or
301  (r.o.txActive = '1' and (i2cSlaveIn.txValid = '1' or TMODE_G = 0))) then
302  v.slvstate := movebyte;
303  v.scloen := I2C_HIZ_C;
304  -- Falling edge that should be detected in movebyte may have passed
305  if r.o.txActive = '1' and v.scl = '0' then
306  v.sdaoen := r.sreg(7) xor OEPOL_LEVEL_C;
307  end if;
308  end if;
309  v.sreg := i2cSlaveIn.txData;
310 
311  when movebyte =>
312  if (r.scl and not v.scl) = '1' then
313  if r.o.txActive = '1' then
314  v.sdaoen := r.sreg(7) xor OEPOL_LEVEL_C;
315  else
316  v.sdaoen := I2C_HIZ_C;
317  end if;
318  end if;
319  if (not r.scl and v.scl) = '1' then
320  v.sreg := r.sreg(6 downto 0) & r.sda;
321  if r.cnt = "111" then
322  if r.addr then
323  v.slvstate := checkaddr;
324  elsif r.o.rxActive = '1' nor r.o.txActive = '1' then
325  v.slvstate := check10bitaddr;
326  else
327  v.slvstate := handshake;
328  end if;
329  v.cnt := (others => '0');
330  else
331  v.cnt := r.cnt + 1;
332  end if;
333  end if;
334 
335  when handshake =>
336  -- Falling edge
337  if (r.scl and not v.scl) = '1' then
338  if r.addr then
339  v.sdaoen := I2C_LOW_C;
340  elsif r.o.rxActive = '1' then
341  -- Receive, send ACK/NAK
342  -- Acknowledge byte if core has room in receive register
343  -- This code assumes that the core's receive register is free if we are
344  -- in RMODE 1. This should always be the case unless software has
345  -- reconfigured the core during operation.
346  if r.o.rxValid = '0' then
347  v.sdaoen := I2C_LOW_C;
348  v.o.rxData := r.sreg;
349  v.o.rxValid := '1';
350  else
351  -- NAK the byte, the master must abort the transfer
352  v.sdaoen := I2C_HIZ_C;
353  v.slvstate := idle;
354  end if;
355  else
356  -- Transmit, release bus
357  v.sdaoen := I2C_HIZ_C;
358  -- Byte transmitted, ack it.
359  v.o.txAck := '1';
360  end if;
361  if not r.addr and r.o.rxActive = '1' and v.sdaoen = I2C_HIZ_C then
362  v.o.nack := '1';
363  end if;
364  end if;
365  -- Risinge edge
366  if (not r.scl and v.scl) = '1' then
367  if r.addr then
368  v.slvstate := movebyte;
369  else
370  if r.o.rxActive = '1' then
371  -- RMODE 0: Be ready to accept one more byte which will be NAK'd if
372  -- software has not read the receive register
373  -- RMODE 1: Keep SCL low until software has acknowledged received byte
374  if RMODE_G = 0 then
375  v.slvstate := movebyte;
376  else
377  v.slvstate := sclhold;
378  end if;
379  else
380  -- Transmit, check ACK/NAK from master
381  -- If the master NAKs the transmitted byte the transfer has ended and
382  -- we should wait for the master's next action. If the master ACKs the
383  -- byte the core will act depending on tmode:
384  -- TMODE 0:
385  -- If the master ACKs the byte we must continue to transmit and will
386  -- transmit the same byte on all requests.
387  -- TMODE 1:
388  -- IF the master ACKs the byte we will keep SCL low until software has
389  -- put new transmit data into the transmit register.
390  if r.sda = I2C_ACK_C then
391  if TMODE_G = 0 then
392  v.slvstate := movebyte;
393  else
394  v.slvstate := sclhold;
395  end if;
396  else
397  v.slvstate := idle;
398  end if;
399  end if;
400  end if;
401  v.addr := false;
402  v.sreg := i2cSlaveIn.txData;
403  end if;
404  end case;
405 
406  if i2cSlaveIn.enable = '1' then
407  -- STOP condition
408  if (r.scl and v.scl and not r.sda and v.sda) = '1' then
409  v.active := false;
410  v.slvstate := idle;
411  v.o.txActive := '0';
412  v.o.rxActive := '0';
413  end if;
414 
415  -- START or repeated START condition
416  if (r.scl and v.scl and r.sda and not v.sda) = '1' then
417  v.slvstate := movebyte;
418  v.cnt := (others => '0');
419  v.addr := true;
420  v.o.txActive := '0';
421  v.o.rxActive := '0';
422  end if;
423  end if;
424 
425  ----------------------------------------------------------------------------
426  -- Reset and idle operation
427  ----------------------------------------------------------------------------
428 
429  if (sRst = '1') then
430  v.slvstate := idle;
431  v.scl := '0';
432  v.active := false;
433  v.scloen := I2C_HIZ_C;
434  v.sdaoen := I2C_HIZ_C;
435  v.sreg := (others => '0');
436  v.cnt := (others => '0');
437  v.o.rxActive := '0';
438  v.o.rxValid := '0';
439  v.o.rxData := (others => '0');
440  v.o.txActive := '0';
441  v.o.txAck := '0';
442  v.o.nack := '0';
443 
444  end if;
445 
446  ----------------------------------------------------------------------------
447  -- Signal assignments
448  ----------------------------------------------------------------------------
449 
450  -- Update registers
451  rin <= v;
452 
453  -- Update outputs
454  i2cSlaveOut <= r.o;
455  i2co.scl <= '0';
456  i2co.scloen <= r.scloen;
457  i2co.sda <= '0';
458  i2co.sdaoen <= r.sdaoen;
460  end process comb;
461 
462  reg : process (clk, aRst)
463  begin -- process reg
464  if (aRst = '1') then
465  r.slvstate <= idle after TPD_G;
466  r.scl <= '0' after TPD_G;
467  r.active <= false after TPD_G;
468  r.scloen <= I2C_HIZ_C after TPD_G;
469  r.sdaoen <= I2C_HIZ_C after TPD_G;
470  r.sreg <= (others => '0') after TPD_G;
471  r.cnt <= (others => '0') after TPD_G;
472  r.o.rxActive <= '0' after TPD_G;
473  r.o.rxValid <= '0' after TPD_G;
474  r.o.rxData <= (others => '0') after TPD_G;
475  r.o.txActive <= '0' after TPD_G;
476  r.o.txAck <= '0' after TPD_G;
477  r.o.nack <= '0' after TPD_G;
478  elsif rising_edge(clk) then
479  r <= rin after TPD_G;
480  end if;
481  end process reg;
482 
483 
484 end architecture rtl;
I2C_ADDR_Ginteger range 0 to 1023:= 0
Definition: I2cSlave.vhd:87
TPD_Gtime := 1 ns
Definition: I2cSlave.vhd:84
RMODE_Ginteger range 0 to 1:= 0
Definition: I2cSlave.vhd:90
in clkstd_ulogic
Definition: I2cSlave.vhd:96
TMODE_Ginteger range 0 to 1:= 0
Definition: I2cSlave.vhd:92
FILTER_Ginteger range 2 to 512:= 4
Definition: I2cSlave.vhd:89
i2c_in_type
Definition: I2cPkg.vhd:34
I2cSlaveOutType :=(rxActive => '0',rxValid => '0',rxData =>( others => '0'),txActive => '0',txAck => '0',nack => '0') I2C_SLAVE_OUT_INIT_C
Definition: I2cPkg.vhd:148
in sRststd_ulogic := '0'
Definition: I2cSlave.vhd:94
sl rxValid
Definition: I2cPkg.vhd:141
I2cSlaveInType
Definition: I2cPkg.vhd:126
OUTPUT_EN_POLARITY_Ginteger range 0 to 1:= 0
Definition: I2cSlave.vhd:88
sl txValid
Definition: I2cPkg.vhd:128
std_ulogic scl
Definition: I2cPkg.vhd:35
sl rxActive
Definition: I2cPkg.vhd:140
slv( 7 downto 0) txData
Definition: I2cPkg.vhd:129
in aRststd_ulogic := '0'
Definition: I2cSlave.vhd:95
sl txActive
Definition: I2cPkg.vhd:143
i2c_out_type
Definition: I2cPkg.vhd:41
slv( 7 downto 0) rxData
Definition: I2cPkg.vhd:142
I2cSlaveOutType
Definition: I2cPkg.vhd:139
sl nack
Definition: I2cPkg.vhd:145
slv( 9 downto 0) addr
Definition: I2cPkg.vhd:64
in i2cSlaveInI2cSlaveInType
Definition: I2cSlave.vhd:98
array(natural range <> ) of i2c_in_type i2c_in_array
Definition: I2cPkg.vhd:39
sl rxAck
Definition: I2cPkg.vhd:130
in i2cii2c_in_type
Definition: I2cSlave.vhd:101
std_ulogic scloen
Definition: I2cPkg.vhd:43
sl txAck
Definition: I2cPkg.vhd:144
out i2coi2c_out_type
Definition: I2cSlave.vhd:103
std_ulogic sda
Definition: I2cPkg.vhd:36
TENBIT_Ginteger range 0 to 1:= 0
Definition: I2cSlave.vhd:86
std_ulogic enable
Definition: I2cPkg.vhd:46
out i2cSlaveOutI2cSlaveOutType
Definition: I2cSlave.vhd:99
_library_ ieeeieee
Definition: I2cRegSlave.vhd:20
std_ulogic sdaoen
Definition: I2cPkg.vhd:45