AnsweredAssumed Answered

I2C Bug in STM32CubeF2 Firmware Package V1.4.0

Question asked by backeberg.christian on Dec 5, 2016
Latest reply on Feb 1, 2018 by backeberg.christian

Hi folks,

 

my application crashes occasionally with the above mentioned firmware package when data is transmitted to I2C devices.

The crash is caused by following code in HAL_I2C_Master_Transmit:

    while(hi2c->XferSize > 0U)
    {
      /* Wait until TXE flag is set */
      if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
      {
           ...

      }

 

      /* Write data to DR */
      hi2c->Instance->DR = (*hi2c->pBuffPtr++);
      hi2c->XferCount--;
      hi2c->XferSize--;

 

      if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0U))
      {

You can get here when I2C_FLAG_BTF is set due to delay (interrupt) and XferSize == 0 because the wrong var (Size) is checked. This results in XferSize wrapping around to 0xffff. The transmit loops continues and usually the pBuffPtr runs beyond bounds causing a bus error.
        /* Write data to DR */
        hi2c->Instance->DR = (*hi2c->pBuffPtr++);
        hi2c->XferCount--;
        hi2c->XferSize--;
      }

 

This code fragment can be found several times in stm32f2xx_hal_i2c.c.

 

I temporarily went back to Rev 1.3.0.

 

Christian

Outcomes