@@ -3277,6 +3277,8 @@ HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAdd
32773277
32783278 __IO uint32_t I2C_Trials = 0UL ;
32793279
3280+ HAL_StatusTypeDef status = HAL_OK ;
3281+
32803282 FlagStatus tmp1 ;
32813283 FlagStatus tmp2 ;
32823284
@@ -3334,37 +3336,64 @@ HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAdd
33343336 /* Wait until STOPF flag is reset */
33353337 if (I2C_WaitOnFlagUntilTimeout (hi2c , I2C_FLAG_STOPF , RESET , Timeout , tickstart ) != HAL_OK )
33363338 {
3337- return HAL_ERROR ;
3339+ /* A non acknowledge appear during STOP Flag waiting process, a new trial must be performed */
3340+ if (hi2c -> ErrorCode == HAL_I2C_ERROR_AF )
3341+ {
3342+ /* Clear STOP Flag */
3343+ __HAL_I2C_CLEAR_FLAG (hi2c , I2C_FLAG_STOPF );
3344+
3345+ /* Reset the error code for next trial */
3346+ hi2c -> ErrorCode = HAL_I2C_ERROR_NONE ;
3347+ }
3348+ else
3349+ {
3350+ status = HAL_ERROR ;
3351+ }
33383352 }
3353+ else
3354+ {
3355+ /* A acknowledge appear during STOP Flag waiting process, this mean that device respond to its address */
33393356
3340- /* Clear STOP Flag */
3341- __HAL_I2C_CLEAR_FLAG (hi2c , I2C_FLAG_STOPF );
3357+ /* Clear STOP Flag */
3358+ __HAL_I2C_CLEAR_FLAG (hi2c , I2C_FLAG_STOPF );
33423359
3343- /* Device is ready */
3344- hi2c -> State = HAL_I2C_STATE_READY ;
3360+ /* Device is ready */
3361+ hi2c -> State = HAL_I2C_STATE_READY ;
33453362
3346- /* Process Unlocked */
3347- __HAL_UNLOCK (hi2c );
3363+ /* Process Unlocked */
3364+ __HAL_UNLOCK (hi2c );
33483365
3349- return HAL_OK ;
3366+ return HAL_OK ;
3367+ }
33503368 }
33513369 else
33523370 {
3353- /* Wait until STOPF flag is reset */
3354- if (I2C_WaitOnFlagUntilTimeout (hi2c , I2C_FLAG_STOPF , RESET , Timeout , tickstart ) != HAL_OK )
3355- {
3356- return HAL_ERROR ;
3357- }
3371+ /* A non acknowledge is detected, this mean that device not respond to its address,
3372+ a new trial must be performed */
33583373
33593374 /* Clear NACK Flag */
33603375 __HAL_I2C_CLEAR_FLAG (hi2c , I2C_FLAG_AF );
33613376
3362- /* Clear STOP Flag, auto generated with autoend*/
3363- __HAL_I2C_CLEAR_FLAG (hi2c , I2C_FLAG_STOPF );
3377+ /* Wait until STOPF flag is reset */
3378+ if (I2C_WaitOnFlagUntilTimeout (hi2c , I2C_FLAG_STOPF , RESET , Timeout , tickstart ) != HAL_OK )
3379+ {
3380+ status = HAL_ERROR ;
3381+ }
3382+ else
3383+ {
3384+ /* Clear STOP Flag, auto generated with autoend*/
3385+ __HAL_I2C_CLEAR_FLAG (hi2c , I2C_FLAG_STOPF );
3386+ }
33643387 }
33653388
33663389 /* Increment Trials */
33673390 I2C_Trials ++ ;
3391+
3392+ if ((I2C_Trials < Trials ) && (status == HAL_ERROR ))
3393+ {
3394+ status = HAL_OK ;
3395+ }
3396+
33683397 } while (I2C_Trials < Trials );
33693398
33703399 /* Update I2C state */
@@ -6377,7 +6406,7 @@ static void I2C_ITSlaveCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
63776406 /* Increment Buffer pointer */
63786407 hi2c -> pBuffPtr ++ ;
63796408
6380- if (( hi2c -> XferSize > 0U ) )
6409+ if (hi2c -> XferSize > 0U )
63816410 {
63826411 hi2c -> XferSize -- ;
63836412 hi2c -> XferCount -- ;
@@ -6533,7 +6562,7 @@ static void I2C_ITListenCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
65336562 /* Increment Buffer pointer */
65346563 hi2c -> pBuffPtr ++ ;
65356564
6536- if (( hi2c -> XferSize > 0U ) )
6565+ if (hi2c -> XferSize > 0U )
65376566 {
65386567 hi2c -> XferSize -- ;
65396568 hi2c -> XferCount -- ;
@@ -6987,7 +7016,7 @@ static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uin
69877016 {
69887017 if (((HAL_GetTick () - Tickstart ) > Timeout ) || (Timeout == 0U ))
69897018 {
6990- if (( __HAL_I2C_GET_FLAG (hi2c , Flag ) == Status ) )
7019+ if (__HAL_I2C_GET_FLAG (hi2c , Flag ) == Status )
69917020 {
69927021 hi2c -> ErrorCode |= HAL_I2C_ERROR_TIMEOUT ;
69937022 hi2c -> State = HAL_I2C_STATE_READY ;
@@ -7027,7 +7056,7 @@ static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c,
70277056 {
70287057 if (((HAL_GetTick () - Tickstart ) > Timeout ) || (Timeout == 0U ))
70297058 {
7030- if (( __HAL_I2C_GET_FLAG (hi2c , I2C_FLAG_TXIS ) == RESET ) )
7059+ if (__HAL_I2C_GET_FLAG (hi2c , I2C_FLAG_TXIS ) == RESET )
70317060 {
70327061 hi2c -> ErrorCode |= HAL_I2C_ERROR_TIMEOUT ;
70337062 hi2c -> State = HAL_I2C_STATE_READY ;
@@ -7066,7 +7095,7 @@ static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c,
70667095 /* Check for the Timeout */
70677096 if (((HAL_GetTick () - Tickstart ) > Timeout ) || (Timeout == 0U ))
70687097 {
7069- if (( __HAL_I2C_GET_FLAG (hi2c , I2C_FLAG_STOPF ) == RESET ) )
7098+ if (__HAL_I2C_GET_FLAG (hi2c , I2C_FLAG_STOPF ) == RESET )
70707099 {
70717100 hi2c -> ErrorCode |= HAL_I2C_ERROR_TIMEOUT ;
70727101 hi2c -> State = HAL_I2C_STATE_READY ;
@@ -7144,7 +7173,7 @@ static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c,
71447173 /* Check for the Timeout */
71457174 if ((((HAL_GetTick () - Tickstart ) > Timeout ) || (Timeout == 0U )) && (status == HAL_OK ))
71467175 {
7147- if (( __HAL_I2C_GET_FLAG (hi2c , I2C_FLAG_RXNE ) == RESET ) )
7176+ if (__HAL_I2C_GET_FLAG (hi2c , I2C_FLAG_RXNE ) == RESET )
71487177 {
71497178 hi2c -> ErrorCode |= HAL_I2C_ERROR_TIMEOUT ;
71507179 hi2c -> State = HAL_I2C_STATE_READY ;
@@ -7311,15 +7340,17 @@ static HAL_StatusTypeDef I2C_IsErrorOccurred(I2C_HandleTypeDef *hi2c, uint32_t T
73117340static void I2C_TransferConfig (I2C_HandleTypeDef * hi2c , uint16_t DevAddress , uint8_t Size , uint32_t Mode ,
73127341 uint32_t Request )
73137342{
7343+ uint32_t tmp ;
7344+
73147345 /* Check the parameters */
73157346 assert_param (IS_I2C_ALL_INSTANCE (hi2c -> Instance ));
73167347 assert_param (IS_TRANSFER_MODE (Mode ));
73177348 assert_param (IS_TRANSFER_REQUEST (Request ));
73187349
73197350 /* Declaration of tmp to prevent undefined behavior of volatile usage */
7320- uint32_t tmp = ((uint32_t )(((uint32_t )DevAddress & I2C_CR2_SADD ) | \
7321- (((uint32_t )Size << I2C_CR2_NBYTES_Pos ) & I2C_CR2_NBYTES ) | \
7322- (uint32_t )Mode | (uint32_t )Request ) & (~0x80000000U ));
7351+ tmp = ((uint32_t )(((uint32_t )DevAddress & I2C_CR2_SADD ) | \
7352+ (((uint32_t )Size << I2C_CR2_NBYTES_Pos ) & I2C_CR2_NBYTES ) | \
7353+ (uint32_t )Mode | (uint32_t )Request ) & (~0x80000000U ));
73237354
73247355 /* update CR2 register */
73257356 MODIFY_REG (hi2c -> Instance -> CR2 , \
0 commit comments