cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeIDE HAL_I2C_ErrorCallback function does not work.

Ade J.3
Associate II

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);
}

1 ACCEPTED SOLUTION

Accepted Solutions
Foued_KH
ST Employee

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.

View solution in original post

6 REPLIES 6
Foued_KH
ST Employee

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.

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?

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.

Yes you can get callback with transmission by DMA and by IT also.

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.

But the problem is I've have not been getting the callback with transmission by DMA, just by IT. Should I configure something different to get a correct functioning?

I recommend you this wiki : Getting started with I2C - stm32mcu

check the 2.3.3 NVIC or DMA settings section.

You can check the available example in the STM32 cube FW .

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.