Skip to main content
Visitor II
January 25, 2023
Solved

I2C HAL lock state not properly updated

  • January 25, 2023
  • 3 replies
  • 1759 views

Hello,

While I was tracking an issue on I2C bus, I found something that seems to be unconsistent in HAL file stm32l4_hal_i2c.c

1 - routines I2C_WaitOn_____UntilTimeout() are called after locking the bus with __HAL_LOCK(hi2c) macro

2- these routines update the lock state in case of error, return and this error to the caller finaly the HAL ends. The caller in that case (error) do not update lock state which is updated by I2C_WaitOn_____UntilTimeout() (=unlock before returning error)

but I found that all path do not update the lock state. for example below the first error do not unlock the bus :

static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,

                            uint32_t Tickstart)

{

 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)

 {

  /* Check if an error is detected */

  if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)

  {

   return HAL_ERROR;

  }

  /* Check for the Timeout */

  if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))

  {

   hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;

   hi2c->State = HAL_I2C_STATE_READY;

   hi2c->Mode = HAL_I2C_MODE_NONE;

   /* Process Unlocked */

   __HAL_UNLOCK(hi2c);

   return HAL_ERROR;

  }

 }

 return HAL_OK;

}

My understanding is that   

/* Process Unlocked */

   __HAL_UNLOCK(hi2c);

is missing before returning the error to the caller. Is it correct?

Regards

    This topic has been closed for replies.
    Best answer by Foued_KH

    Hello @Noirim DIH​ ,

    This issue is already fixed : STM32CubeL4/stm32l4xx_hal_i2c.c at c5e83f31696c3da4fb374224471afd08d9d457b3 · STMicroelectronics/STM32CubeL4 · GitHub

    Thx

    Foued

    3 replies

    ST Employee
    January 25, 2023
    Visitor II
    January 25, 2023

    Hello,

    thank you for your message. My question is about the code generated by CubeMX which is up to date.

    For your information I'm waiting for a fix to this reported issue

    https://community.st.com/s/feed/0D53W00001wIeOsSAK

    as well.

    So I'm using the last available version of CubeMX and generated HAL.

    Regards

    Foued_KHAnswer
    ST Employee
    May 16, 2023