Beruflich Dokumente
Kultur Dokumente
ON
SUBMITTED FOR
Submitted By:
This is to certify that the project entitled GPIO Pin Expansion Using I2C
Signature Signature
i
ACKNOWLEDGMENT
It is matter of great pride and privilege for us to have the esteemed supervision
of our Mr. V. K. SHARMA (Program Coordinator, CDAC Noida) for
giving us this opportunity of making this project.
Were also very grateful to Mr. RAVI PAYAL (Senior IT Consultant, CDAC
Noida) for providing various facilities at college which helped in successful
completion of the project.
And finally, a special thanks to all the helping staff for their invaluable support.
Input/Output) pins from a relatively long PCB trace path within the system.
Since the I2C interface is a two-wire system, the design aims to provide multiple
inputs and outputs pins at the remote end with just a common two-wire trace.
This will provide increase design flexibility and also adds to physical
compactness of entire system. It also enables smaller packaging and reduced pin
count.
The design aims to achieve the capability to provide general purpose I/O
To reduce package size and pin count, the number of general purpose I/Os are
I2C interface, additional GPIO pins via the I2C bus can be easily added.
iii
TABLE OF CONTENTS
Certificate i
Acknowledgement ii
Abstract iii
1. Architecture
1.1 I2C Interface for GPIO Pin Expansion 1
1.2 I2C Signal Format 2
3. Verilog Code
3.1 Design Block 9
3.2 Stimulus Block 14
4. Simulation Result
4.1 Simulated Waveform 17
5. Synthesis Report
5.1 RTL Schematic 19
5.2 Technology Schematic 20
5.3 Synthesis Report 21
6. Conclusion 23
7. Bibliography 24
1. ARCHITECTURE
The SPARTAN 2 FPGA acts as a slave on the I2C bus and has two pins on its I2C interface:
the I2C clock SCL and the I2C data line SDA. The host system, which acts as an I2C master,
communicates with the SPARTAN 2 FPGA device (which acts as an I2C slave). The FPGA
present eight general purpose input ports and eight general purpose output ports to the host.
Data, which is transmitted serially over the I2C bus, is received in parallel at the GPIO pins.
This way, all eight general purpose I/Os can be read or written at the same time.
Whenever the master issues a write condition (R/W = 0), the data received on the I2C bus is
used to update the general purpose output pins until a stop or repeat start condition is
encountered. Similarly, when the I2C master issues a read condition (R/W = 1), the values at
the general purpose input pins are sampled at the ACK bit and transmitted serially over the
I2C bus. This process continues until the master issues a stop or repeat start.
Term Description
START and STOP condition are generated by keeping the level of the SCL line high and then
changing the level of the SDA line. The START condition is generated by high-to-low
change in the SDA line when SCL line is high. The STOP condition is generated by a low-to-
high change in the SDA line SCL is low.
In I2C, each packet may contain either address or data. START condition + address packet +
one or more data packet + STOP condition together form a complete data transfer.
Address bits are used to address a specific slave device on the bus. The 7-bit address lets the
master address a maximum of 128 slaves on the bus, although the address 0000 000 is
reserved for general call and all address of the format 1111 xxx are reserved. That means 119
devices can share I2C bus. In I2C bus the MSB of the address is transmitted first. The 8th bit in
the packet is the READ/WRITE control bit. If this bit is set the master will read the next
frame (data) from the slave, otherwise, the master will write the next frame (data) on the bus
to the slave. When a slave detects its address on the bus, it knows that its being addressed
GPIO Pin Expansion Using I2C Interface 4
and it should acknowledge in the 9th SCL (ACK) cycle by changing SDA to zero. If the
addressed slave isnt ready or for any reason doesnt want to service the master, it should
leave the SDA line high in 9th clock cycle. This is considered to be NACK. In case of NACK,
the master can transmit a STOP condition to terminate the transmission, or a REPEATED
START condition to initiate a new transmission.
When a master transmits address 0000 000, all slaves respond by changing the SDA line to
zero and wait to receive the data byte. This is useful when a master wants to transmit the
same data byte to all slaves in the system. The general call address cant be used to read data
from slaves because no more than one slave is able to write to the bus at a given time.
One of the features of the I2C protocol is clock stretching. It is a kind of flow control. If an
addressed slave device isnt ready to process more data it will stretch the clock by holding the
clock line SCL low after receiving (or sending) a bit of data. Thus the master will not be able
to raise the clock line (because devices are wired-ANDed) and will wait until the slave
releases the SCL line to show its ready to transfer the next bit.
The Acknowledge signal is defined as follows: the transmitter releases the SDA line during
the acknowledge clock pulse so the receiver can pull the SDA line LOW and it remains stable
LOW during the HIGH period of this clock pulse. Set-up and hold times must also be taken
into account. When SDA remains HIGH during this 9th clock pulse, this is defined as the Not
Acknowledge signal. The master can then generate either a STOP condition to abort the
transfer, or a repeated START condition to start a new transfer.
Two masters can begin transmitting on an idle bus at the same time and there needs to be a
method for deciding which will take control of the bus and complete its transmission. This is
done by clock synchronization and arbitration. In single master systems, clock
synchronization and arbitration are not needed.
Clock synchronization is performed using the wired-AND connection of I2C interfaces to the
SCL line. This means that a HIGH to LOW transition on the SCL line will cause the masters
concerned to start counting off their LOW period and, once a master clock has gone LOW, it
will hold the SCL line in that state until the clock HIGH state is reached. However, the LOW
to HIGH transition of this clock may not change the state of the SCL line if another clock is
still within its LOW period. The SCL line will therefore be held LOW by the master with the
longest LOW period. Masters with shorter LOW periods enter a HIGH wait-state during this
time.
When all masters concerned have counted off their LOW period, the clock line will be
released and go HIGH. There will then be no difference between the master clocks and the
state of the SCL line, and all the masters will start counting their HIGH periods. The first
master to complete its HIGH period will again pull the SCL line LOW.
In this way, a synchronized SCL clock is generated with its LOW period determined by the
master with the longest clock LOW period, and its HIGH period determined by the one with
the shortest clock HIGH period.
Arbitration, like synchronization, refers to a portion of the protocol required only if more than
one master will be used in the system. Slaves are not involved in the arbitration procedure. A
master may start a transfer only if the bus is free. Two masters may generate a START
condition within the minimum hold time (t HD : STA ) of the START condition which results in a
valid START condition on the bus. Arbitration is then required to determine which master
will complete its transmission.
Arbitration proceeds bit by bit. During every bit, while SCL is HIGH, each master checks to
see if the SDA level matches what it has sent. This process may take many bits. Two masters
can actually complete an entire transaction without error, as long as the transmissions are
identical. The first time a master tries to send a HIGH, but detects that the SDA level is
LOW, the master knows that it has lost the arbitration and will turn off its SDA output driver.
The other master goes on to complete its transaction.
No information is lost during the arbitration process. A master that loses the arbitration can
generate clock pulses until the end of the byte in which it loses the arbitration and must
restart its transaction when the bus is idle.
If a master also incorporates a slave function and it loses arbitration during the addressing
stage, it is possible that the winning master is trying to address it. The losing master must
therefore switch over immediately to its slave mode.
Figure shows the arbitration procedure for two masters. Of course, more may be involved
depending on how many masters are connected to the bus. The moment there is a difference
between the internal data level of the master generating DATA1 and the actual level on the
SDA line, the DATA1 output is switched off. This will not affect the data transfer initiated by
the winning master.
Since control of the I2C-bus is decided solely on the address and data sent by competing
masters, there is no central master, nor any order of priority on the bus.
There is an undefined condition if the arbitration procedure is still in progress at the moment
when one master sends a repeated START or a STOP condition while the other master is still
sending data.
GPIO Pin Expansion Using I2C Interface 8
3. VERILOG CODE
reg start_stop; // Indicated if the device has detected a start and is busy in some operation
reg start, stop; // Goes high temporarily when a start or stop is detected
reg some_other_device; // to reset start_stop, when bus address is different from the device
address
reg repeat_start; // to reset everything if repeat start is detected.
reg write_flag; // to set when write operation begins
reg reset; // Used to reset the start , stop registers
reg sda_out; // To force the SDA output Low. remains tristated for sending High Logic.
reg done; // To Indicate data transfer done
reg [3:0]count; // Down Counter ( 7 to 0 + ACK bit )
/********************************************************************************/
// Detecting STOP Condition on the SDA Bus
always @ (posedge sda or posedge reset)
if (reset)
stop <= 0;
else if (sclk)
stop <= 1;
/********************************************************************************/
// Counting of bits. First eight bits are data and the last one is ack.
wire sda_is_ack = count[3] & count [2]; // High for ACK , count = 1111
/********************************************************************************/
/********************************************************************************/
always@(negedge sclk)
if (start_stop)
begin
if (~ack_flag)
begin
if ( add_is_matching & ~write_flag)
sda_out <= 1'b0;
else
sda_out <= 1'bz;
wire sda;
assign sda = sda_out;
endmodule
`timescale 1us/10ns
module test_I2C_to_GPIO;
reg sclk;
reg [7:0]GPIO_input;
reg start_t;
reg sda_in;
reg ack_check;
reg GPIO_input_check;
reg read_oper_check;
reg write_oper_check;
reg slave_add_check;
wand sda;
wire [7:0] GPIO_input_reg, GPIO_output;
wire [3:0] count;
reg[7:0] GPIO_input_store; //Stores the data from GPIO input line which master wants to read
reg[7:0] GPIO_output_send; //Stores the data which master wants to write at GPIO output line
reg[6:0] slave_add_compare; //For random slave address
integer i = 0;
integer r_seed; // Seed ensures that the same random sequence is generated during every
simulation
parameter tdelay = 3.5 ;
parameter testcycle = 100.0;
initial
r_seed = 2; // arbitrarily define the seed as 2
initial
begin
@(negedge sclk)
$display("Testing for random values");
#tdelay sda_in = 1;
#tdelay sda_in = 0; // For start
# testcycle
@ (negedge sclk) //For repeat start
$display ("Starting a write operation", $time);
#tdelay sda_in = 1;
#tdelay sda_in = 0;
#tdelay sda_in = 0; // Sending slave address=0000000
# testcycle
$display("starting a read operation", $time);
@ (negedge sclk) //For repeat start
#tdelay sda_in= 1;
#tdelay sda_in= 0; //Sending slave address=0000000
#75 sda_in <=1; //For read operation
GPIO_input = $random(r_seed);
#5 ack_check <= 1;
#10 ack_check <= 0;
//For Storing the data from sda line which master is reading
#10 GPIO_input_store[7] <= sda;
#10 GPIO_input_store[6] <= sda;
#10 GPIO_input_store[5] <= sda;
#10 GPIO_input_store[4] <= sda;
#10 GPIO_input_store[3] <= sda;
#10 GPIO_input_store[2] <= sda;
#10 GPIO_input_store[1] <= sda;
#10 GPIO_input_store[0] <= sda;
#10 read_oper_check <= 1;
#10 read_oper_check <= 0;
# testcycle
@ (negedge sclk) //For stop
#tdelay sda_in = 0;
#tdelay sda_in = 1;
$display (" End of testcycle, for another random check, run again ");
#10 $stop;
end
endmodule
As a result of which design flexibility as well as physical compactness of entire system can
be achieved. It also enables smaller packaging and reduced pin count.
1. Samir Palnitkar (1996), Verilog HDL A Guide to Digital Design and Synthesis.
2. Muhammad Ali Mazidi, Sarmad Naimi and Sepher Naimi, The AVR Micro-controller and
Websites:
1. Altera Corporation http://www.altera.com.
2. Xilinx - http://www.xilinx.com/