[HAL][I2C] Update function HAL_I2C_IsDeviceReady() to take into account the number of trials
diff --git a/Src/stm32h7xx_hal_i2c.c b/Src/stm32h7xx_hal_i2c.c
index 39b2d68..74bd220 100644
--- a/Src/stm32h7xx_hal_i2c.c
+++ b/Src/stm32h7xx_hal_i2c.c
@@ -3255,6 +3255,8 @@
__IO uint32_t I2C_Trials = 0UL;
+ HAL_StatusTypeDef status = HAL_OK;
+
FlagStatus tmp1;
FlagStatus tmp2;
@@ -3312,37 +3314,64 @@
/* Wait until STOPF flag is reset */
if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
{
- return HAL_ERROR;
+ /* A non acknowledge appear during STOP Flag waiting process, a new trial must be performed */
+ if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
+ {
+ /* Clear STOP Flag */
+ __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
+
+ /* Reset the error code for next trial */
+ hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
+ }
+ else
+ {
+ status = HAL_ERROR;
+ }
}
+ else
+ {
+ /* A acknowledge appear during STOP Flag waiting process, this mean that device respond to its address */
- /* Clear STOP Flag */
- __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
+ /* Clear STOP Flag */
+ __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
- /* Device is ready */
- hi2c->State = HAL_I2C_STATE_READY;
+ /* Device is ready */
+ hi2c->State = HAL_I2C_STATE_READY;
- /* Process Unlocked */
- __HAL_UNLOCK(hi2c);
+ /* Process Unlocked */
+ __HAL_UNLOCK(hi2c);
- return HAL_OK;
+ return HAL_OK;
+ }
}
else
{
- /* Wait until STOPF flag is reset */
- if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
- {
- return HAL_ERROR;
- }
+ /* A non acknowledge is detected, this mean that device not respond to its address,
+ a new trial must be performed */
/* Clear NACK Flag */
__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
- /* Clear STOP Flag, auto generated with autoend*/
- __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
+ /* Wait until STOPF flag is reset */
+ if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
+ {
+ status = HAL_ERROR;
+ }
+ else
+ {
+ /* Clear STOP Flag, auto generated with autoend*/
+ __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
+ }
}
/* Increment Trials */
I2C_Trials++;
+
+ if ((I2C_Trials < Trials) && (status == HAL_ERROR))
+ {
+ status = HAL_OK;
+ }
+
} while (I2C_Trials < Trials);
/* Update I2C state */