AnsweredAssumed Answered

[error report] STM32F1 I2C HAL BUG I2C_MasterTransmit_TXE

Question asked by riabovas.justas on Oct 18, 2015
Latest reply on Apr 19, 2016 by riabovas.justas
Here is original function

static HAL_StatusTypeDef I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c)
{
        /* Write data to DR */
      hi2c->Instance->DR = (*hi2c->pBuffPtr++);
      hi2c->XferCount--;

  if(hi2c->XferCount == 0)
  {
    /* Disable BUF interrupt */
    __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  }
  return HAL_OK;
}

I found that in runtime this function is entered with XferCount already 0.
As there is no protection from this situation unknown data is put to DR register.
Moreover XferCount is decremented to 0xFFFF and the fun begins.

I've workarounded this problem by editing I2C_MasterTransmit_TXE function:

static HAL_StatusTypeDef I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c)
{
  if(hi2c->XferCount == 0)
  {
    /* Disable BUF interrupt */
    __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  } else {
        /* Write data to DR */
      hi2c->Instance->DR = (*hi2c->pBuffPtr++);
      hi2c->XferCount--;
  }
  return HAL_OK;
}

Outcomes