cancel
Showing results for 
Search instead for 
Did you mean: 

I2C HAL lock state not properly updated

Noirim DIH
Associate II

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

1 ACCEPTED SOLUTION

Accepted Solutions
Foued_KH
ST Employee

Hello @Noirim DIH​ ,

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

Thx

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

3 REPLIES 3
Foued_KH
ST Employee

Hello @Noirim DIH​ ,

Which FW version do you use ?

Please make sure that you are using the latest version : STM32CubeL4 - STM32Cube MCU Package for STM32L4 series and STM32L4 Plus series (HAL, Low-Layer APIs and CMSIS, USB, TouchSensing, File system, RTOS, Graphic - and examples running on ST boards) - STMicroelectronics

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.

Noirim DIH
Associate II

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_KH
ST Employee

Hello @Noirim DIH​ ,

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

Thx

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.