I2C IRQ handler implementation Part 4
Now let’s code for the STOP flag shown in Figure 1.
The master will not execute the if block in Figure 1 since the STOP flag will not be set in the master mode. That means this if block will only get executed in the slave mode.
Why do the stop flag sets?
In the reference manual (Figure 2), it is given that the stop detection is only applicable to slave mode. If the STOP flag is 0, then no stop condition is detected. This bit is set by the hardware when a stop condition is detected on the bus by the slave after an acknowledgement, and the software clears it after reading the SR1 register followed by writing into the CR1 register (Figure 3).
Observe Figure 1. In the code, the STOP flag is set, and you have to clear it.
Procedure to clear the STOP flag:
- Read the SR1 register (Figure 4).
- Write to the CR1 register (Figure 5): While writing data to the CR1, make sure that you don’t write any invalid data; otherwise, the peripheral will not work properly. Just do bitwise OR with 0X0000 so that the contents of the CR1 will not get affected.
- Call I2C application event callback to notify the application about the stop generation (Figure 7). Let’s call the application callback function, as shown in Figure 8, and for this function, you have to mention the handle and the app event.