Skip to main content

Subsection 10.3.7 Part 7a: What an I2C Library Has to Provide

We observed the I2C protocol on the AD2 oscilloscope yesterday. Before we open the I2C library, try to work out what functions need to be in it. A program talking to a device on the I2C bus needs a small number of operations, and you can derive the list from what you know at this point.

Activity 10.3.5. What Five Things Does the Library Have to Do?

Work individually first, then compare your list with your table group. Think about what a transaction consists of and what a program has to be able to do with a device.

(a)

Open your own helloDisplay.c. Three of the operations needed of an I2C library can be seen in this program. Name them from the calls you can see, and for each one say what the program had to pass to the function in order to call it.

(b)

Recall your own capture on the AD2 yesterday. Which of those three calls produced the START, the address, the ACK, the data byte and the STOP you captured? Looking only at the trace, could you tell a one-byte write from a multi-byte one?

(c)

Two operations needed of an I2C library are still missing. Your capture from yesterday shows one acknowledge per byte and never a byte coming back to you. What I2C operations cannot be built out of the three calls we have seen in helloDisplay.c?

(d)

One of the five things needed of an I2C library has nothing to do with any particular device. What is it and what does it have to set up?
The five, in the order this course’s library gives them. There is initialization, which happens once and belongs to the bus rather than to any device: claim the pins, set the clock rate, switch the peripheral on. Then write one byte to a device, which is all the loop at the end of helloDisplay.c ever does, and read one byte from it. Then write several bytes and read several bytes in a single transaction, and those two carry one extra argument, because a device with more than one thing inside it needs to be told where the bytes are going. In C:
/* Configure I2C pins, initialize bus speed */
void i2c1_init(void);

/* Write one byte to target */
void i2c1_byteWrite(uint8_t deviceAddr, uint8_t data);

/* Read one byte from a target */
void i2c1_byteRead(uint8_t deviceAddr, uint8_t *data);

/* Write one or more bytes to a memory location within target */
void i2c1_memWrite(uint8_t deviceAddr, uint8_t registerAddr,
                   uint8_t nbytes, uint8_t *data);

/* Read one or more bytes from a memory location within target */
void i2c1_memRead(uint8_t deviceAddr, uint8_t registerAddr,
                  uint8_t nbytes, uint8_t *data);
Why “several bytes in one transaction” (i2c1_memWrite()) is a separate function and not a loop over the single-byte one: between two calls to i2c1_byteWrite() the bus goes back to idle, with a STOP after the first and a fresh START before the second. To send data to a digit of the display we need to first send the display-internal address of the digit and then the data we wish to store at that address, and this needs to happen in a single transaction. If we did this in two separate transactions the display would interpret each as an independent single command as opposed to the second one being the data to be stored at the address transmitted first. In fact, we’ll be able to send the data for all digits and the colon in a single transaction, see Subsection 10.3.5.
Reading a byte from a target can be accomplished in one I2C transaction, and this is what happens in i2c1_byteRead(). Reading the contents of one or more registers from a device takes two steps again: first a write transaction to the device that tells it which of its registers to read from, then the read transaction. The I2C protocol’s answer is to join the two with a repeated START instead of a STOP, so that nothing else can get onto the bus in between, see also Subsection 10.4.11. Our i2c1_memRead() does exactly that: it sends the register address and then joins the read to it with a repeated START instead of a STOP. That is why it exists alongside i2c1_byteRead(): the register address goes first.