Skip to content

Commit f5c3c36

Browse files
ABOSTMfpistm
authored andcommitted
[F2] I2C HAL fix: generate Start only once Stop is finished
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 393fa63 commit f5c3c36

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

Diff for: system/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_hal_i2c.c

+86-1
Original file line numberDiff line numberDiff line change
@@ -3438,6 +3438,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16
34383438
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
34393439
}
34403440

3441+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3442+
/* Wait until STOP flag is reset */
3443+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3444+
do
3445+
{
3446+
count--;
3447+
if (count == 0U)
3448+
{
3449+
hi2c->PreviousState = I2C_STATE_NONE;
3450+
hi2c->State = HAL_I2C_STATE_READY;
3451+
hi2c->Mode = HAL_I2C_MODE_NONE;
3452+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3453+
3454+
/* Process Unlocked */
3455+
__HAL_UNLOCK(hi2c);
3456+
3457+
return HAL_ERROR;
3458+
}
3459+
}
3460+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3461+
34413462
/* Process Locked */
34423463
__HAL_LOCK(hi2c);
34433464

@@ -3537,6 +3558,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint1
35373558
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
35383559
}
35393560

3561+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3562+
/* Wait until STOP flag is reset */
3563+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3564+
do
3565+
{
3566+
count--;
3567+
if (count == 0U)
3568+
{
3569+
hi2c->PreviousState = I2C_STATE_NONE;
3570+
hi2c->State = HAL_I2C_STATE_READY;
3571+
hi2c->Mode = HAL_I2C_MODE_NONE;
3572+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3573+
3574+
/* Process Unlocked */
3575+
__HAL_UNLOCK(hi2c);
3576+
3577+
return HAL_ERROR;
3578+
}
3579+
}
3580+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3581+
35403582
/* Process Locked */
35413583
__HAL_LOCK(hi2c);
35423584

@@ -3703,6 +3745,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_
37033745
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
37043746
}
37053747

3748+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3749+
/* Wait until STOP flag is reset */
3750+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3751+
do
3752+
{
3753+
count--;
3754+
if (count == 0U)
3755+
{
3756+
hi2c->PreviousState = I2C_STATE_NONE;
3757+
hi2c->State = HAL_I2C_STATE_READY;
3758+
hi2c->Mode = HAL_I2C_MODE_NONE;
3759+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3760+
3761+
/* Process Unlocked */
3762+
__HAL_UNLOCK(hi2c);
3763+
3764+
return HAL_ERROR;
3765+
}
3766+
}
3767+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3768+
37063769
/* Process Locked */
37073770
__HAL_LOCK(hi2c);
37083771

@@ -3828,6 +3891,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16
38283891
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
38293892
}
38303893

3894+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3895+
/* Wait until STOP flag is reset */
3896+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3897+
do
3898+
{
3899+
count--;
3900+
if (count == 0U)
3901+
{
3902+
hi2c->PreviousState = I2C_STATE_NONE;
3903+
hi2c->State = HAL_I2C_STATE_READY;
3904+
hi2c->Mode = HAL_I2C_MODE_NONE;
3905+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3906+
3907+
/* Process Unlocked */
3908+
__HAL_UNLOCK(hi2c);
3909+
3910+
return HAL_ERROR;
3911+
}
3912+
}
3913+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3914+
38313915
/* Process Locked */
38323916
__HAL_LOCK(hi2c);
38333917

@@ -4511,7 +4595,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevA
45114595
UNUSED(DevAddress);
45124596

45134597
/* Abort Master transfer during Receive or Transmit process */
4514-
if (hi2c->Mode == HAL_I2C_MODE_MASTER)
4598+
if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && (hi2c->Mode == HAL_I2C_MODE_MASTER))
45154599
{
45164600
/* Process Locked */
45174601
__HAL_LOCK(hi2c);
@@ -4542,6 +4626,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevA
45424626
{
45434627
/* Wrong usage of abort function */
45444628
/* This function should be used only in case of abort monitored by master device */
4629+
/* Or periphal is not in busy state, mean there is no active sequence to be abort */
45454630
return HAL_ERROR;
45464631
}
45474632
}

0 commit comments

Comments
 (0)