Subsection 10.3.8 Part 7b: Opening i2c1_byteWrite()
Those five functions are the interface with the I2C hardware; we’ll now investigate one of them in more detail.
i2c1_byteWrite() is the one that produced the trace you captured yesterday, and it is short enough to read in full:
void i2c1_byteWrite(uint8_t deviceAddr, uint8_t data) {
I2C1->ICR = I2C_ICR_NACKCF | I2C_ICR_STOPCF; // clear leftovers from the last transfer
I2C1->CR2 = (deviceAddr<<1)
| (1U << I2C_CR2_NBYTES_Pos) // will send 1 byte
| I2C_CR2_AUTOEND // Send Stop condition after transfer
| I2C_CR2_START; // Send Start condition
while (!(I2C1->ISR & (I2C_ISR_TXIS | I2C_ISR_NACKF))); // ready for data, or nobody answered
// Send the data
if (!(I2C1->ISR & I2C_ISR_NACKF)) {
I2C1->TXDR = data; // Write the data
}
while (!(I2C1->ISR & I2C_ISR_STOPF)); // STOP follows AUTOEND or a NACK
I2C1->ICR = I2C_ICR_STOPCF | I2C_ICR_NACKCF;
}
Five fields go into
I2C_CR2 in a single assignment, and between them they describe the entire transaction before any of it happens: who (the address, shifted left by one into the SADD field, which holds the address in bits 7 down to 1), which direction (RD_WRN, left at 0 here, which means write), how many bytes (NBYTES), what to do at the end (AUTOEND, which makes the hardware send the STOP on its own), and the start condition (START). Figure 10.3.25 shows where those five fields sit in the register.
SADD[9:0] holds the address; RD_WRN is the transfer direction, 0 for a write and 1 for a read; and START is the bit whose being written is what puts a START condition on the wire. In the upper row, NBYTES[7:0] is how many data bytes this transaction will carry, and AUTOEND makes the hardware issue the STOP by itself once they have gone. The remaining fields (NACK, HEAD10R, ADD10, RELOAD and PECBYTE) belong to target mode, 10-bit addressing, transfers longer than 255 bytes, and SMBus, none of which this course uses. STOP is the exception: it is a controller bit, and it is how software ends a transfer itself when AUTOEND is clear.Notice the
= in the assignment to I2C1->CR2. The habit from the timer chapter, assign a whole mask rather than compound-assigning one bit (Subsection 8.2.5), applies here for a different reason than it did there. On a status register the danger was what the read-back would clear. I2C_CR2 is a control register and reads back fine; the danger is what is left over. The address, the direction and the byte count from the previous transaction are all still sitting in it, and |= cannot clear a bit, so an |= here would leave the old address ORed with the new one. Assigning the whole mask overwrites every bit and therefore fills in the desired transaction completely.
The two waits (blocking while loops) in
i2c1_byteWrite() are there to ensure the peripheral is ready for the next step. The first loop waits for whichever of two flags comes up first. The I2C_ISR_TXIS flag, transmit interrupt status, comes up when the transmit register is empty and the hardware is ready for the byte, which happens after the address has been sent and acknowledged. The I2C_ISR_NACKF flag (NACK received) comes up instead if nobody acknowledged the address, and in that case the data byte is never written. The second wait is for I2C_ISR_STOPF (STOP detected): the STOP condition signals the end of the transaction and is put on the data line by the hardware because of the AUTOEND. It is also automatically sent after a NACK. Both flags (NACKF and STOPF) stay set until software clears them through I2C1->ICR, the interrupt clear register. This is why at the start of the function we clear both. Figure 10.3.26 is the reference manual’s own picture of the successful sequence, albeit for sending two bytes rather than one. Reading a byte has a similar shape with three changes: I2C_CR2_RD_WRN needs to be set, we need to wait on I2C_ISR_RXNE instead of TXIS, and we take the data out of the receive data register via *data = I2C1->RXDR; instead of the write.
i2c1_byteWrite() waits on before writing I2C1->TXDR. NBYTES holds the programmed count, 2 for the whole of this transfer, and once that many bytes have gone out, AUTOEND puts the STOP on the wire without any further help from the program.Everything below
I2C1->CR2 is done by the I2C hardware. Nothing in this library counts clock edges or times a pulse; it sets up a transaction, and the peripheral shifts the bits out, generates the START and the STOP, releases SDA for the ACK slot and reports what came back. What is left for software is describing the transaction and reading the flags. Subsection 10.4.7 has the peripheral’s own block diagram, if you want to see what the peripheral’s state machine is made of.
