cancel
Showing results for 
Search instead for 
Did you mean: 

I2C error handling.

BMart.2
Associate II

I am using the following code:

  while(HAL_I2C_Master_Transmit(&hi2c1, DEVICE_ADDR, i2cbuffer, 1, 1000) != HAL_OK)

  {

   /* Error_Handler() function is called when Timeout error occurs.

     When Acknowledge failure occurs (Slave doesn't acknowledge its address)

     Master restarts communication */

   if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)

   {

    errorcount = 20;

    Error_Handler();

   }

The routine error handler basically is a while(1) that toggles an led and never exits. I'm trying to trap the errors, mostly I'm getting a NACK error, with the following code:

 /* I2C not acknowlege NACK flag ---------------------------------------------*/

 if (I2C_CHECK_FLAG(itflags, I2C_FLAG_AF) != RESET)

 {

  hi2c1.ErrorCode |= HAL_I2C_ERROR_AF;

  /* Clear NACK flag */

  __HAL_I2C_CLEAR_FLAG(&hi2c1, I2C_FLAG_AF);

 }

  

 /* I2C Bus error interrupt occurred ------------------------------------*/

 if (I2C_CHECK_FLAG(itflags, I2C_FLAG_BERR) != RESET)

 {

  hi2c1.ErrorCode |= HAL_I2C_ERROR_BERR;

  /* Clear BERR flag */

  __HAL_I2C_CLEAR_FLAG(&hi2c1, I2C_FLAG_BERR);

 }

etc. Even though my mainline code sets the AF flag and errorcount to 20 the line

 if (I2C_CHECK_FLAG(itflags, I2C_FLAG_AF) != RESET) checks good.

What registers should I look at to determine the source of the error and how do i reset that errror? I'm running as a master talking to one slave.

Thanks

1 REPLY 1
TDK
Guru

Duplicate of:

https://community.st.com/s/question/0D53W00000Cgz7WSAR/i2c-error-trapping

Please don't post the same problem multiple times.

If you feel a post has answered your question, please click "Accept as Solution".