Documente Academic
Documente Profesional
Documente Cultură
depending on the hardware, 3 or 4 wire) and I2C is half duplex communication....we cannt
send and receive data at a time in I2c..but it possible in SPI ....SPI much faster Than I2C.......
Main topic to discuss the difference is
1) speed is difference
in case of i2c is 100/400kbps in 7 bit mode max 400kbps (But High speed I2C communication
protocols allow speeds upto 3.4Mbps!!)
in case of spi speed is upto 1mbps (the higher speed of SPI is due to fact that unlike I2C, SPI
interfaces to a slave device using a sperate pin called the slave select + no concept of
acknowledgements which means increased Band width..)
2)Connection wise: i2c require less pin then spi (SPI 3wire: 3 IOs, SPI 4 wire: 4 IOs, I2C: 2 IOs)
as spi require slave select for individual device...
3)it is better to use i2c in case of if u want to connect more device to connect. (slave
addressing advantageous over SPI slave select individual pins for individual slaves)
4)bus arbitration is possible in case of i2c... not in case of spi... (SPI does not require it, since
only 1 slave is controlled by the master at any given point)
6)noise sensitivity of i2c is high... there is chance to corrupt the r/w bit... then whole data is
corrputed... but in case of spi.. chance is very less as whole word is trasmitted... However, I
must say that I2C is more reliable, since the protocol supports slave feedback machanism
(ACK) to detect whether was received correctly or corrupt.
8) it is easy to implement the spi...while i2c is little bit complex... (over heads
Sudheer has given good explaination,just to add on it :1)I2C is half duplex as there are only two lines(SCL and
SDA).
2)SPI is Full Duplex as between a Master and a dedicated
slave as selected by slave_select signal; there are 4 lines
(Spi_clk,Spi_datain,Spi_dataout and slave_select).So,at any
I2C Questions
Question: I'd like to extend the I2C bus. Is there something like a repeater for
I2C?
Yes indeed this exists. Philips manufactures a special chip to buffer the bi-directional lines of the I2C
bus. Typically, this is a current amplifier. It forces current into the wiring (a couple of mA). That way
you can overcome the capacitance of long wiring.
However, you will need this component on both sides of the line. The charge pump in this devices can
deliver currents up to 30mA which is way too much for a normal I2C chip to handle. With these buffers
you can handle loads up to 2nF. The charge amplifier 'transforms' this load down to a 200pF load
which is still acceptable by I2C components.
There is another possibility to do this: By using the stand-alone I2C controller PCF8584 from Philips.
This chip has a certain mode in which it does not take part in the real I2C action but only records what
is going on. It listens to all addresses, but does not generate any acknowledge. Using some software
routines and a MCU you could build a universal I2C data logger.
for each slave. I2C also supports slave acknowledgment which means that
you can be absolutely sure that youre actually communicating with
something. With SPI, a master can be sending data to nothing at all and
have no way to know that. In general SPI is better suited for applications
that deal with longer data streams and not just words like address
locations. Mostly longer data streams exist in applications where youre
working with a digital signal processor or analog-to digital converter. For
example, SPI would be perfect for playing back some audio stored in an
eeprom and played through a digital to analog converter DAC. Moreover,
since SPI can support significantly higher data rates comparing to I2C,
mostly due to its duplex capability, it is far more suited for higher speed
applications reaching tens of megahertz. And since there is no device
addressing involved in SPI the protocol is a lot harder to use in multiple
slave systems. This means when dealing with more than one node,
generally I2C is the way to go.
SPI interface:
As you can see in the picture, SPI has 4 lines. The SCLK line is the clock
line, the clock is generated by the master and drives the communication in
both directions, and this line is an input to all slaves. The MOSI line is the
master data output, slave data input, and it caries data from the master to
the slave. The MISO line is the master data input, slave data output, and it
carries data from the slave to the master. Finally the SS or sometimes
known as the CS line is the slave select or chip select line, it is toggled to
select a slave to be communicated with. Usually the transfer sequence
consist of driving the SS line low with a general I/O pin, sending X number
of clock signals with the proper polarity and phase, driving the SS line high
to end the communication. As the clock signals are generated, data is
transferred in both directions, therefore in a transmit only system the
received bytes have to be discarded and in a receive only system a dummy
byte has to be transmitted. Care has to be taken not to toggle the SS during
the communication since this will introduce errors. The clock polarity and
clock phase controls on which edge of the clock signal the data is received
and sent. This has to be set up to match between the master and the slave.
There is also a 3-Wire version of SPI however it only supports half duplex
communication. This set up uses a SISO line, this single bidirectional data
line carries data in and out of the slave. This mode has a tendency of not
being supported by microcontrollers however it is easily implemented with
bit banging in software.
I2C:
I2C consists of two bidirectional lines that are pulled up to Vdd. The SDA
line is the serial data line and SCL is the serial clock line. The standard I2C
modules support a 7-bit slave address and can support up to 112/128 nodes,
some extended I2C modules can support 10-bit slave addressing however
all the modules are limited to 400pF bus capacitance. Standard speeds are
10kbit/s Low-speed mode and 100kbit/s Standard mode. The extended
versions support 400kbit/s Fast mode, 1Mbit/s Fast mode plus, and
3.4Mbit/s High-speed mode. The three types of messages that are defined
by the I2C protocol are a single message where the master writes to a slave,
a single message where the master reads from a slave, and a combined
message where a master issues at least two reads and/or writes to one or
more slaves. The sequence of the communication begins with the master
sending a start bit followed by the 7 or 10-bit slave address and finally a bit
that selects if the operation is a write(0) or a read(1). At this point, if the
slave address exists on the bus the slave will send an acknowledgment bit to
the master. The data is than transferred on the SDA line in the direction
that was specified by the master. An acknowledgment bit is sent at the end
of each transfered byte by the receiving end of the transmission. The only
exception is that when the master is in receive mode and the slave in
transmit mode the master will not send an acknowledge bit after the very
last bit received. Lastly the communication is stopped with the master
sending a stop command. The start and stop commands are simply a
transition from high to low (Start) on the SDA line with SCL high or low to
high (Stop) on the SDA line with SCL high. Transitions for the data bits are
always performed while SCL is low; the high state is only for the start/stop
commands.
Hopefully this gives you a better understanding of these two protocols, and
their differences. It should be a lot simpler to select one over the other for a
given application. But life is not always that easy, you still have to consider
if other types of communications are not more suitable for your application,
you could even be better off not using a serial protocol at all but to go with a
parallel type communication.
protocol
Next diagram shows an example of SPI Protocol taken from an Intersil document.
I2C
I2C can be used to connect up to 127 nodes via a bus that only requires two data wires,
known as SDA and SCL.
Since the devices need a common voltage reference (Ground) and a power line (referred to as
Vcc or Vdd) the full bus has four wires, two for data and two for power. Sometimes
additional lines are added, for example to support interrupts when the state of I2C devices
change.
In theory the I2C bus can support multiple masters, but most micro-controllers cant. A
master is usually a microcontroller, although it doesnt have to be. Slaves can be ICs or
microcontrollers.
In the diagram above a RasPi is a master and there are several slaves: a Digital-to-Analog
converter (DAC), an Analog-to-Digital converter (DC), and an Arduino.
I2C can be used to control a very wide range of devices. Common examples include
When the master wishes to communicate with a slave it sends a series of pulses down the
SDA and SCL lines. The data that is sent includes an address that identifies the slave with
which the master needs to interact. Addresses take 7 bits out of a data byte; the remaining bit
specifies whether the master wishes to read (get data from a slave) or write (send data to a
slave).
Some devices have an address that is entirely fixed by the manufacturer; others can be
configured to take one of a range of possible addresses. When a micro-controller is used as a
slave it is normally possible to configure its address by software, and for that address to take
on any of the 127 possible values.
The address byte may be followed by one or more byes of data, which may go from master to
slave or from slave to master.
When data is being sent on the SDA line, clock pulses are sent on the SCL line to keep
master and slave synchronised. Since the data is sent one bit at a time, the data transfer rate is
one eighth of the clock rate. The original standard specified a standard clock rate of 100KHz,
and most I2C chips and micro-controllers can support this. Later updates to the standard
introduced a fast speed of 400KHz and a high speed of 1.7 or 3.4 MHz. The Arduino can
support standard and fast speeds; the BeagleBoard has three I2C busses, one at each speed;
the RasPi and BeagleBone can both support standard and fast I2C.
The fast rate corresponds to a data transfer rate of 50K bytes/sec which is too slow for some
control applications. One option in that case is to use SPI instead of I2C.
SPI
The SPI interface was originally developed by Motorola (now Freescale). SPI is much
simpler than I2C. Master and slave are linked by three data wires, usually called MISO,
(Master in, Slave out), MOSI (Master out, Slave in) and M-CLK.
As the names suggest, the M_CLK line carries clock pulses which provide synchronisation.
When a transfer is taking place, a bit of data is transferred via MOSI from Master to slave
and at the same time a bit of data is transferred via MISO from Slave to Master. At the end of
eight clock pulses an entire byte of data has been transferred in each direction.
Many SPI-enabled ICs and Microcontrollers can cope with data rates of over 10MHz, so
transfer is much faster than with I2C. The downside is that SPI normally has no addressing
capability; instead, devices are selected by means of a Chip Select signal which the master
can use to enable one slave out of several connected to the SPI bus. If more than one slave
exists, one chip select line is required per device, which can use precious GPIO lines on the
Master.
SPI is less well specified than I2C, but the SPI module on the Broadcom chip at the heart of
the Raspberry Pi is flexible enough to cope with most common SPI devices.