Subsection 10.4.9 The Library, and Where It Cuts Corners
Reading one byte is the same shape as writing one, with
RD_WRN set, a wait on RXNE instead of TXIS, and the data coming out of I2C_RXDR instead of going into I2C_TXDR:
void i2c1_byteRead(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 read 1 byte
| I2C_CR2_RD_WRN // ping for reading
| I2C_CR2_AUTOEND // Send Stop condition after transfer
| I2C_CR2_START; // Send Start condition
while (!(I2C1->ISR & (I2C_ISR_RXNE | I2C_ISR_NACKF))); // byte arrived, or nobody answered
// Read the data
if (!(I2C1->ISR & I2C_ISR_NACKF)) {
*data = I2C1->RXDR; // Read the data
}
while (!(I2C1->ISR & I2C_ISR_STOPF)); // STOP follows AUTOEND or a NACK
I2C1->ICR = I2C_ICR_STOPCF | I2C_ICR_NACKCF;
}
Every function in the library handles a NACK the same way, following the transmitter flow in RM0490’s Figure 228 and applying the same test on the read side: each wait for the hardware to be ready also ends when
NACKF comes up. The reason is in RM0490 §23.4.9, which says plainly that on a NACK “the TXIS flag is not set”. A wait on TXIS alone would never end when nobody answers, and the very first call would never return. The same paragraph continues “a STOP condition is automatically sent after the NACK reception”, which is the peripheral’s own NACK handling rather than AUTOEND completing a transfer, so a refused transfer still ends in a STOP, and one wait on STOPF at the end of each function covers both endings.
NACKF and STOPF are sticky flags: they stay set until software clears them through I2C_ICR. Each function clears both before it starts, so that a flag left over from an earlier transaction cannot end a wait early, and again after the STOP, so that it leaves nothing behind. And the multi-byte functions never leave their loops early: after a NACK, NACKF stays set until the end, so every remaining wait ends at once and every remaining byte is skipped.
What the functions still cannot do is tell their caller what happened, since they return
void. A write to a device that is not there puts the address on the wire and then no data, and a read from one leaves the caller’s variable as it was. A starting value can stand in for a report only when the device could never send it: the accelerometer’s WHO_AM_I register reads 0x33, so a read that returns 0 did not reach it, but most registers have no such value. Giving the functions a return value means changing every call site, which is a decision about the whole library rather than about one function.
The narrower lesson is worth stating in general, because a NACK is not the only thing that can go wrong: none of the waits in this library has a timeout. Every
while( !(I2C1->ISR & ...) ); in it assumes the hardware will eventually do what it was asked. The very first one, after setting START, assumes that SDA and SCL can rise at all. A pull-up on each line, the internal one if there is nothing else, lets them rise, but a line held LOW, by a short to ground or by a device stuck partway through a transfer, cannot rise. A bus stuck LOW hangs the program on its first I2C call, before any of the NACK handling applies. Production code puts a bound on every one of these waits and reports the timeout to its caller; this library is a teaching library and does not.
i2c1_memWrite() and i2c1_memRead() follow the same pattern with the register address sent first, which is why i2c1_memWrite() programs NBYTES as nbytes + 1, counting the register address as one of the bytes it is about to send. i2c1_memRead() joins its two halves with the repeated START the protocol provides for (Subsection 10.4.11). Its first I2C_CR2 write leaves AUTOEND clear, so after the register address the peripheral holds SCL LOW and sets TC, transfer complete, instead of sending a STOP. The second I2C_CR2 write, with RD_WRN set, is what puts the repeated START on the wire. It makes that second write only if the device address and the register address were both acknowledged: after a NACK the hardware is ending the transfer with a STOP of its own, so there is nothing for a repeated START to continue. You are given both and only ever call them.
