STM32 I2C Lecture 51: I2C IRQ handler implementation Part 8

  • Post author:
  • Post category:Blog

 

I2C IRQ handler implementation Part 8

 

 


Implementation of CloseReceiveData and CloseSendData (Figure 1):

I2C IRQ handler implementation Part 8
Figure 1. Implementation of CloseReceiveData and CloseSendData.

 

1.Whenever you send or receive the data in interrupt mode, and if you want to close the reception or transmission, make sure that you disable all the interrupts. Closing the communication means you don’t want more interrupts. Therefore, first, you have to clear or disable all the interrupt enable control bits.

2.In the case of CloseReceiveData:

  • The IT buf enable (ITBUFEN) is disabled, as shown in Figure 2, to prevent the generation of more RXNE or TXE interrupts.
I2C IRQ handler implementation Part 8
Figure 2. Code to disable the ITBUFEN.

 

  • The control bit IT event enable (ITEVFEN) is disabled, as shown in Figure 3, when you want to stop the receiving interrupt for different events of the I2C.
I2C IRQ handler implementation Part 8
Figure 3. Code to disable the ITEVFEN.

 

  • The TxRxState should be rested to I2C ready, the buffer should be initialized to NULL, RX length (RxLen) should return to 0, and the RxSize should return to 0 (Figure 4).
I2C IRQ handler implementation Part 8
Figure 4. Initializing the variables of the handle to their initial value.

 

  • Enable the Acking (Figure 5) because, during a reception, you might have disabled the Acking.
I2C IRQ handler implementation Part 8
Figure 5. Code to enable the Acking.

 

  • If the ACK control is set to true, then only you can enable the Acking. Now let’s check the ACK control bit by using an if statement, as shown in Figure 6.
I2C IRQ handler implementation Part 8
Figure 6. Code to enable the Acking when the ACK control is set to true.

 

3.In the case of CloseSendData:

  • The IT buf enable (ITBUFEN) is disabled, as shown in Figure 7, to prevent the generation of more RXNE or TXE interrupts.
I2C IRQ handler implementation Part 8
Figure 7. Code to disable the ITBUFEN.

 

  • Disable the IT event enable control bit (ITEVFEN), as shown in Figure 8.
I2C IRQ handler implementation Part 8
Figure 8. Code to disable the ITEVFEN.

 

  • Make the TxRxState as ready, the Tx Buffer as null, and make the TxLen equal to 0, as shown in Figure 9.
I2C IRQ handler implementation Part 8
Figure 9. Initializing the variables of the handle to their initial value.

 

4. Give the prototypes of CloseReceiveData and CloseSendData APIs in the driver.h file, as shown in Figure 10.

I2C IRQ handler implementation Part 8
Figure 10. Prototypes of CloseReceiveData and CloseSendData APIs.

 

5. In the I2C driver source file, we implemented some helper functions (Figure 11) in order to handle RXNE and TXE interrupts. These functions are private functions, and you can even mention them as static functions (Figure 12). Now let’s give the prototypes for them (Figure 13).

I2C IRQ handler implementation Part 8
Figure 11. Helper functions to handle RXNE and TXE interrupts.

 

I2C IRQ handler implementation Part 8
Figure 12. Static helper functions to handle RXNE and TXE interrupts.

 

I2C IRQ handler implementation Part 8
Figure 13. Prototypes for the helper functions.

 

6. Check whether your code builds or not. The code has one error called too few arguments to function I2C_CloseReceiveData, as shown in Figure 14. In order to solve this error, you need to mention the pointer to the handle while calling the CloseReceiveData API (Figure 15).

I2C IRQ handler implementation Part 8
Figure 14. Result of compilation.

 

I2C IRQ handler implementation Part 8
Figure 15. Passing the pointer to the handle while calling the CloseReceiveData API.

 

7. Compile the code again. Look at Figure 16. Now the code is error-free.

I2C IRQ handler implementation Part 8
Figure 16. Result of compilation.

 

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.