2024-12-16 10:38 PM
Hi there,
I have an IIC sensor connected to the MCU. And I read the sensor data at 50Hz.
However, there are sometimes some IIC communication errors present with HAL_I2C_ERROR_TIMEOUT.
My project is based on FreeRTOS with 4 Tasks, the sensor data collection task is one of the tasks, and the priority of this one isn't the highest.
I try to read the sensor data by the blocking API "HAL_I2C_Master_Transmit()" and "HAL_I2C_Master_Receive()".
I want to know if is there any possibility that the HAL_I2C_ERROR_TIMEOUT is caused by the blocking API in the sensor data collection task being interrupted by higher-priority tasks.
If yes, how to modify the code? Change to "HAL_I2C_Master_Transmit_IT()" and "HAL_I2C_Master_Receive_IT()" is OK?
(Because the frequency of the error is very low, it's not easy to position the root reason by test.)
(And can't include "HAL_I2C_Master_Transmit()" and "HAL_I2C_Master_Receive()" in "taskENTER_CRITICAL()" and "taskExit_CRITICAL()" to keep others task's realtime)
Thanks for your help!
2024-12-17 07:31 PM
if you want use "HAL_I2C_Master_Transmit_IT()" and "HAL_I2C_Master_Receive_IT()", you need set the macro "USE_HAL_I2C_REGISTER_CALLBACKS"(in stm32g4xx_hal_conf.h) to 1.
And rewrite the callback function that you need. (the callback function lists can refer to HAL_I2C_Init() function)
Of course, you need to enable the IIC interrupt.