Skip to main content
Ade J.3
Associate II
April 17, 2023
Solved

STM32CubeIDE HAL_I2C_ErrorCallback function does not work.

I'm using i2c interface to communicate with a eeprom memory, by dma.

It's working, however, when I put a wrong address to write in the memory (to test the errors), I get as return HAL_ERROR, but the function HAL_I2C_ErrorCallback (that i implemented) does not called.

How can I get a callback from this function in a error situation?

Part of my code is available below:

status = HAL_I2C_Mem_Write(&hi2c3, 0xB4, (0<<8), 2, dataWrite, 270, 1000); //Wrong Address
 
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){
		HAL_UART_Transmit(&huart1, errorMsg, sizeof(errorMsg), 1000);
}

This topic has been closed for replies.
Foued_KH
Foued_KHBest answer
ST Employee
April 17, 2023

Hello @Ade J.3​ ,

You can try to set this function :

/* USER CODE BEGIN 2 */
if (HAL_I2C_Mem_Write(&hi2c3, 0xB4, (0<<8), 2, dataWrite, 270, 1000) != HAL_OK)
 {
 Error_Handler();
 }
/* USER CODE END 2 */

and :

/* USER CODE BEGIN 4 */
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{
HAL_UART_Transmit(&huart1, errorMsg, sizeof(errorMsg), 1000);
}
/* USER CODE END 4 */

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

6 replies

Foued_KH
Foued_KHBest answer
ST Employee
April 17, 2023

Hello @Ade J.3​ ,

You can try to set this function :

/* USER CODE BEGIN 2 */
if (HAL_I2C_Mem_Write(&hi2c3, 0xB4, (0<<8), 2, dataWrite, 270, 1000) != HAL_OK)
 {
 Error_Handler();
 }
/* USER CODE END 2 */

and :

/* USER CODE BEGIN 4 */
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{
HAL_UART_Transmit(&huart1, errorMsg, sizeof(errorMsg), 1000);
}
/* USER CODE END 4 */

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Ade J.3
Ade J.3Author
Associate II
April 18, 2023

Thank you for the answer. That worked for me!

However, it isn't using DMA. Is there any way to get the callback with transmission by DMA?

Foued_KH
ST Employee
April 18, 2023

If this solves your problem, please mark my answer as "Best Answer" by clicking on the "Select as Best" button, this can be helpful for Community users to find this solution faster. 

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.