The physical I2C bus
This is just two wires, called SCL and SDA. SCL is the clock line. It
is used to
synchronize all data transfers over the I2C bus. SDA is the data line.
The SCL
& SDA lines are connected to all devices on the I2C bus. There
needs to be a
third wire which is just the ground or 0 volts. There may also be a
5volt wire
is power is being distributed to the devices. Both SCL and SDA lines
are
"open drain" drivers. What this means is that the chip can drive its
output low, but it cannot drive it high. For the line to be able to go
high you must provide
pull-up resistors to the 5v supply. There should be a resistor from the
SCL line
to the 5v line and another from the SDA line to the 5v line. You only
need one
set of pull-up resistors for the whole I2C bus, not for each device, as
illustrated below:
The value of the resistors should be from 1.8K (1800 ohms) to 4.7k (4700 ohms). It depends on the length of the I2C bus, the longer the bus, the smaller value should be used. If the value is too large, the rise time of the signals will be too slow and the bus may not work properly. If the resistors are missing, the SCL and SDA lines will always be low - nearly 0 volts - and the I2C bus will not work.
Masters and Slaves
The devices on the I2C bus are either masters or slaves. The ARMexpress
as a master is always
the device that drives the SCL clock line. The slaves are the devices
that
respond to the master. A slave cannot initiate a transfer over the I2C
bus, only
a master can do that. There can be, and usually are, multiple slaves on
the I2C
bus, however there is normally only one master. ARMexpress does not
support multiple
masters. Slaves will never initiate a transfer. Both master and slave
can
transfer data over the I2C bus, but that transfer is always controlled
by the
master.
The I2C Physical Protocol
When the ARMexpress wishes to talk to a slave it begins by issuing a
start sequence on the I2C bus. A start sequence
is one of two special sequences defined for the I2C bus, the other
being the
stop sequence. The start sequence and stop sequence are special in that
these
are the only places where the SDA (data line) is allowed to change
while the SCL
(clock line) is high. When data is being transferred, SDA must remain
stable and
not change whilst SCL is high. The start and stop sequences mark the
beginning
and end of a transaction with the slave device.
Data is transferred in sequences of 8 bits. The bits are placed on the SDA line starting with the MSB (Most Significant Bit). The SCL line is then pulsed high, then low. Remember that the chip cannot really drive the line high, it simply "lets go" of it and the resistor actually pulls it high. For every 8 bits transferred, the device receiving the data sends back an acknowledge bit, so there are actually 9 SCL clock pulses to transfer each 8 bit byte of data. If the receiving device sends back a low ACK bit, then it has received the data and is ready to accept another byte. If it sends back a high then it is indicating it cannot accept any further data and the master should terminate the transfer by sending a stop sequence.
How fast?
ARMexpress runs in Fast mode at approximately 380 KHz.
I2C Device Addressing
By convention the I2C address is reffered to as the address byte
and is
the first byte shifted out in the sequence. However the LSB (Least Significant Bit) of
that byte represents
Read/not Write. This means that you can have up to 128 devices on the I2C bus,
since only
the upper 7 bits of the number are significant. Addresses on I2C will be used specified as even
numbers, and
it is implied that the LSB will be set for reads..
The I2C Software Protocol
The first thing that will happen is that the master will send out a
start
sequence. This will alert all the slave devices on the bus that a
transaction is
starting and they should listen in incase it is for them. Next the
master will
send out the device address. The slave that matches this address will
continue
with the transaction, any others will ignore the rest of this
transaction and
wait for the next. Having addressed the slave device the master must
now send
out the internal location or register number inside the slave that it
wishes to
write to or read from. This number is obviously dependant on what the
slave
actually is and how many internal registers it has. Some very simple
devices do
not have any, but most do. Having sent the I2C address and the
internal register address the master can now send the data byte
(or bytes,
it doesn't have to be just one). The
master can continue to send data bytes to the slave and these will
normally be
placed in the following registers because the slave will automatically
increment
the internal register address after each byte. When the master has
finished
writing all data to the slave, it sends a stop sequence which completes
the
transaction. So to write to a slave device:
1. Send a start sequence
2. Send the I2C address of the slave with the R/W bit low (even address)
3. Send the internal register number you want to write to
4. Send the data byte
5. [Optionally, send any further data bytes]
6. Send the stop sequence.
Reading from the Slave
This is a little more complicated - but not too much more. Before
reading data
from the slave device, you must tell it which of its internal addresses
you want
to read. So a read of the slave actually starts off by writing to it.
This is
the same as when you want to write to it: You send the start sequence,
the I2C
address of the slave with the R/W bit low (even address) and the
internal
register number you want to write to. Now you send another start
sequence
(sometimes called a restart) and the I2C address again - this time with
the read
bit set. You then read as many data bytes as you wish and terminate the
transaction with a stop sequence. So to read the compass bearing as a
byte from
the CMPS03 module:
1. Send a start sequence
2. Send the I2C address of the slave with the R/W bit low (even address)
3. Send the internal register number you want to read from.
4. Send a start sequence again (repeated start)
2. Send the I2C address of the slave with the R/W bit high (odd address)
6. Read data byte from the slave device. (may be repeated depending on
the slave capabilities)
7. Send the stop sequence.
The bit sequence will look like this:
Wait a moment
The ARMexpress does not support slaves that use clock stretching. The
result is that erroneous
data is read from the slave. Beware! Luckily this function is
relatively rare these days.
Example Master Code
Easy isn't it?
The definitive specs on the I2C bus can be found on the Philips website. Its currently here but if its moved you'll find it easily be googleing on "i2c bus specification".