@@ -3277,6 +3277,8 @@ HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAdd
3277
3277
3278
3278
__IO uint32_t I2C_Trials = 0UL ;
3279
3279
3280
+ HAL_StatusTypeDef status = HAL_OK ;
3281
+
3280
3282
FlagStatus tmp1 ;
3281
3283
FlagStatus tmp2 ;
3282
3284
@@ -3334,37 +3336,64 @@ HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAdd
3334
3336
/* Wait until STOPF flag is reset */
3335
3337
if (I2C_WaitOnFlagUntilTimeout (hi2c , I2C_FLAG_STOPF , RESET , Timeout , tickstart ) != HAL_OK )
3336
3338
{
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
+ }
3338
3352
}
3353
+ else
3354
+ {
3355
+ /* A acknowledge appear during STOP Flag waiting process, this mean that device respond to its address */
3339
3356
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 );
3342
3359
3343
- /* Device is ready */
3344
- hi2c -> State = HAL_I2C_STATE_READY ;
3360
+ /* Device is ready */
3361
+ hi2c -> State = HAL_I2C_STATE_READY ;
3345
3362
3346
- /* Process Unlocked */
3347
- __HAL_UNLOCK (hi2c );
3363
+ /* Process Unlocked */
3364
+ __HAL_UNLOCK (hi2c );
3348
3365
3349
- return HAL_OK ;
3366
+ return HAL_OK ;
3367
+ }
3350
3368
}
3351
3369
else
3352
3370
{
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 */
3358
3373
3359
3374
/* Clear NACK Flag */
3360
3375
__HAL_I2C_CLEAR_FLAG (hi2c , I2C_FLAG_AF );
3361
3376
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
+ }
3364
3387
}
3365
3388
3366
3389
/* Increment Trials */
3367
3390
I2C_Trials ++ ;
3391
+
3392
+ if ((I2C_Trials < Trials ) && (status == HAL_ERROR ))
3393
+ {
3394
+ status = HAL_OK ;
3395
+ }
3396
+
3368
3397
} while (I2C_Trials < Trials );
3369
3398
3370
3399
/* Update I2C state */
@@ -6377,7 +6406,7 @@ static void I2C_ITSlaveCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
6377
6406
/* Increment Buffer pointer */
6378
6407
hi2c -> pBuffPtr ++ ;
6379
6408
6380
- if (( hi2c -> XferSize > 0U ) )
6409
+ if (hi2c -> XferSize > 0U )
6381
6410
{
6382
6411
hi2c -> XferSize -- ;
6383
6412
hi2c -> XferCount -- ;
@@ -6533,7 +6562,7 @@ static void I2C_ITListenCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
6533
6562
/* Increment Buffer pointer */
6534
6563
hi2c -> pBuffPtr ++ ;
6535
6564
6536
- if (( hi2c -> XferSize > 0U ) )
6565
+ if (hi2c -> XferSize > 0U )
6537
6566
{
6538
6567
hi2c -> XferSize -- ;
6539
6568
hi2c -> XferCount -- ;
@@ -6987,7 +7016,7 @@ static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uin
6987
7016
{
6988
7017
if (((HAL_GetTick () - Tickstart ) > Timeout ) || (Timeout == 0U ))
6989
7018
{
6990
- if (( __HAL_I2C_GET_FLAG (hi2c , Flag ) == Status ) )
7019
+ if (__HAL_I2C_GET_FLAG (hi2c , Flag ) == Status )
6991
7020
{
6992
7021
hi2c -> ErrorCode |= HAL_I2C_ERROR_TIMEOUT ;
6993
7022
hi2c -> State = HAL_I2C_STATE_READY ;
@@ -7027,7 +7056,7 @@ static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c,
7027
7056
{
7028
7057
if (((HAL_GetTick () - Tickstart ) > Timeout ) || (Timeout == 0U ))
7029
7058
{
7030
- if (( __HAL_I2C_GET_FLAG (hi2c , I2C_FLAG_TXIS ) == RESET ) )
7059
+ if (__HAL_I2C_GET_FLAG (hi2c , I2C_FLAG_TXIS ) == RESET )
7031
7060
{
7032
7061
hi2c -> ErrorCode |= HAL_I2C_ERROR_TIMEOUT ;
7033
7062
hi2c -> State = HAL_I2C_STATE_READY ;
@@ -7066,7 +7095,7 @@ static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c,
7066
7095
/* Check for the Timeout */
7067
7096
if (((HAL_GetTick () - Tickstart ) > Timeout ) || (Timeout == 0U ))
7068
7097
{
7069
- if (( __HAL_I2C_GET_FLAG (hi2c , I2C_FLAG_STOPF ) == RESET ) )
7098
+ if (__HAL_I2C_GET_FLAG (hi2c , I2C_FLAG_STOPF ) == RESET )
7070
7099
{
7071
7100
hi2c -> ErrorCode |= HAL_I2C_ERROR_TIMEOUT ;
7072
7101
hi2c -> State = HAL_I2C_STATE_READY ;
@@ -7144,7 +7173,7 @@ static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c,
7144
7173
/* Check for the Timeout */
7145
7174
if ((((HAL_GetTick () - Tickstart ) > Timeout ) || (Timeout == 0U )) && (status == HAL_OK ))
7146
7175
{
7147
- if (( __HAL_I2C_GET_FLAG (hi2c , I2C_FLAG_RXNE ) == RESET ) )
7176
+ if (__HAL_I2C_GET_FLAG (hi2c , I2C_FLAG_RXNE ) == RESET )
7148
7177
{
7149
7178
hi2c -> ErrorCode |= HAL_I2C_ERROR_TIMEOUT ;
7150
7179
hi2c -> State = HAL_I2C_STATE_READY ;
@@ -7311,15 +7340,17 @@ static HAL_StatusTypeDef I2C_IsErrorOccurred(I2C_HandleTypeDef *hi2c, uint32_t T
7311
7340
static void I2C_TransferConfig (I2C_HandleTypeDef * hi2c , uint16_t DevAddress , uint8_t Size , uint32_t Mode ,
7312
7341
uint32_t Request )
7313
7342
{
7343
+ uint32_t tmp ;
7344
+
7314
7345
/* Check the parameters */
7315
7346
assert_param (IS_I2C_ALL_INSTANCE (hi2c -> Instance ));
7316
7347
assert_param (IS_TRANSFER_MODE (Mode ));
7317
7348
assert_param (IS_TRANSFER_REQUEST (Request ));
7318
7349
7319
7350
/* 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 ));
7323
7354
7324
7355
/* update CR2 register */
7325
7356
MODIFY_REG (hi2c -> Instance -> CR2 , \
0 commit comments