Hardware Reference
In-Depth Information
The diagram shows each message starting with the S (start) bit and ending with
a P (stop) bit. After the start bit, each message begins with a byte containing the 7-bit
peripheral address and a read/write bit. Every peripheral must read this byte in order to
determine whether the message is addressed to it.
The addressed peripheral is expected to return an ACK/NAK bit after the address
is sent. If the peripheral fails to respond for any reason, the line will go high due to the
pull-up resistor, indicating a NAK. The master, upon seeing a NAK, will send a stop bit and
terminate the transmission.
When the peripheral ACKs the address byte, the master then continues to write when
the request is a write. The first example shows the MCP23017 8-bit register number being
written next. This indicates which of the peripheral's registers is to be written to. The
peripheral will then ACK the register number, allowing the master to follow with the data
byte to be written into the selected register. This too must be ACKed. If the master has no
more data to send, the P (stop) bit is sent to end the transmission. Otherwise, more data
bytes could follow with the sequence ending with the stop bit.
The second example in Figure 12-4 shows how a message may be composed of both
write and read messages. The initial sequence looks like the write, but this only writes
a register number into the peripheral. Once the register number is ACKed, the master
then sends an SR (start, repeated) bit. This tells the peripheral that no more write data is
coming and to expect a peripheral address next. Since the address transmitted specifies
the same peripheral, the same peripheral responds with an ACK. This request is a read,
so the peripheral continues to respond with 8 bits of the requested read data, with the
master ACKing the data received. The master terminates the message with a P (stop) to
indicate that no more data is to be read.
Many peripherals will support an auto-increment register mode. This is a feature
of the peripheral, however, and not all devices support this. Once a peripheral's register
has been established by a write, successive reads or writes can occur in auto-increment
mode, with the register being incremented with each byte transferred. This results in
more-efficient transfers.
Which I2C Bus?
Before we look at the I2C software API provided by Raspbian Linux, you should first
determine which I2C bus you'll be working with. Early Raspberry Pi revisions provided
I2C bus 0 on header strip P1, while later units changed this to bus 1. This will matter to
both commands and programs communicating with I2C peripherals.
The “Identification” section of Chapter 5 discusses how to identify your Pi by
displaying the firmware code from /proc/cpuinfo . What is displayed as a Revision is
actually more of a firmware code. The following is a quick check example:
$ grep Revision /proc/cpuinfo
Revision : 000f
 
Search WWH ::




Custom Search