STM32 I2C Lecture 46: I2C IRQ handler implementation Part 3

  • Post author:
  • Post category:Blog

 

I2C IRQ handler implementation Part 3

 

 


If control comes to the if block, shown in Figure 1, that means the interrupt is occurred because of the setting of the ADDR flag.

Figure 1. Handle for interrupt generated by ADDR event.

 

When the ADDR flag is set, the clock will be stretched, and the master or slave will be in a wait state. Whenever the address flag is set, it is crucial to clear it.

 

Steps for the handling of an interrupt generated by ADDR event:

  • Clear the ADDR flag according to your logic. There is an API called I2C_ClearADDRFlag() to clear the ADDR flag. 
  • Call I2C_ClearADDRFlag() API and send the peripheral’s base address, as shown in Figure 2.
Figure 2. Call for I2C_ClearADDRFlag() API.

 

Steps for the handling of an interrupt generated by the BTF or Byte Transfer Finished event:

  • Whenever the BTF flag is set, the picture in Figure 3 should come into your mind. 
Figure 3. When the BTF flag is set.
  • During transmission, if both BTF and TXE are set, then both SR and DR will be empty, and the clock will be stretched, as shown in Figure 3.
  • During transmission, if both BTF and RXNE are set, then the SR and DR registers will be full, and the clock will be stretched, as shown in Figure 3.

 

Ways to make a decision in order to handle the BTF flag:

  • Look at the non-blocking API of master send data shown in Figure 4. Here you can observe that in the master send data, the communication is closed by using a while statement. After transmitting all the bytes, we wait until the TXE and BTF are set, and once they become 1, the stop condition is generated. Therefore, the setting of the BTF flag can be used to make sure the successful transmission of the last data byte.
Figure 4. Handle for interrupt generated by BTF event.

 

  • Check the application’s state, as shown in Figure 5.
Figure 5. Code to check the application’s state.

 

  • If the application state or TxRx state is busy in TX, then by using an if condition, confirm that TXE is also set (Figure 6).
Figure 6. Code to confirm that the TXE=1.

 

If the control enters the inner if block, shown in Figure 6, it confirms that both BTF and TXE are set.

  • When the if block shown in Figure 6 becomes TRUE, it is an indication for us to close the transmission. To close the transmission, perform the following steps:
    • Generate the stop condition to close the transmission (Figure 7).
    • Reset all the member elements of the handle structure (Figure 7). That means you can reset the application’s state, the pointer contents, length contents, etc.
    • Call I2C application event callback to notify the application about transmission complete (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.
Figure 7. Steps to be followed when both BTF and TXE became 1.

 

Figure 8. Call for I2C_ApplicationEventCallback() function.

 

The BTF flag is used to end the I2C transmission. I2C transmission is terminated by confirming whether TXE is set or not. If both TXE and BTF are set, the registers SR and DR will be empty, and you must close or terminate the data transmission. Now let’s write the code according to the steps (comments) mentioned in Figure 7. 

  • Generate the stop condition, as shown in Figure 9.
Figure 9. Code to generate the stop condition.

 

The stop condition should be generated only if the repeated start is disabled. Therefore, you need to check the repeated start value stored in the handle variable (Figure 10).

Figure 10. Code to check the Sr value stored in the handle variable.

 

Before closing the transmission, it is crucial to make sure that the length has reached 0. Now you need to make slight changes to the code, as shown in Figure 11.

Figure 11. Modified code of Figure 10 to check the length value.

 

  • Reset all the member elements of the handle structure using I2C_CloseSendData() API, as shown in Figure 12.
Figure 12. Code to reset the member elements of the handle structure.

 

If the application’s state is busy in RX, you have nothing to do. Therefore, the else if block in Figure 13 will be empty.

Figure 13. Steps to be followed when the application’s state is busy in RX.

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.