I2C IRQ handler implementation Part 8
Implementation of CloseReceiveData and CloseSendData (Figure 1):
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.
- 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.
- 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).
- Enable the Acking (Figure 5) because, during a reception, you might have disabled 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.
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.
- Disable the IT event enable control bit (ITEVFEN), as shown in Figure 8.
- Make the TxRxState as ready, the Tx Buffer as null, and make the TxLen equal to 0, as shown in Figure 9.
4. Give the prototypes of CloseReceiveData and CloseSendData APIs in the driver.h file, as shown in Figure 10.
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).
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).
7. Compile the code again. Look at Figure 16. Now the code is error-free.