Implementation of I2C master receive data API: Part 2
Procedure to read the data from slave when the length is greater than 1:
1. Clear the ADDR flag (Figure 1): The data reception begins only when you clear the ADDR flag.
2.Read the data until length becomes zero: Using for loop, read the data from the slave until the length becomes zero. To achieve this, you have to start reading data from i=Len to till i>0, as shown in Figure 2.
3. Wait until RXNE becomes 1 (Figure 3).
4.Check if the last 2 bytes are remaining (if (i=2)):
- Let’s assume that i is not equal to 2. Let’s assume i as 6, therefore length=6. Now when RXNE becomes 1, read the data and increment the buffer address. That means read the data into the buffer and increment the buffer address, as shown in Figure 4. You have to repeat this step until i becomes 2.
- When i becomes 2, clear the ACK bit or disable the Acking and then generate the STOP, as shown in Figure 5.
This completes the master receive data. In the end, you have to re-enable the Acking (Figure 6) because when you enter into I2C_MasterReceiveData() API, the ACK was enabled. Therefore, before leaving this API, you have to re-enable the Acking only if ACK control is equal to I2C ACK enable in the config of I2C handle (Figure 7).
In the following article, let’s do the exercise: Read data from the I2C slave.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1