Skip to content

Commit 4aba75e

Browse files
committed
[F1] I2C HAL fix: generate Start only once Stop is finished
1 parent a6f7a98 commit 4aba75e

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

system/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_i2c.c

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3492,6 +3492,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16
34923492
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
34933493
}
34943494

3495+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3496+
/* Wait until STOP flag is reset */
3497+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3498+
do
3499+
{
3500+
count--;
3501+
if (count == 0U)
3502+
{
3503+
hi2c->PreviousState = I2C_STATE_NONE;
3504+
hi2c->State = HAL_I2C_STATE_READY;
3505+
hi2c->Mode = HAL_I2C_MODE_NONE;
3506+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3507+
3508+
/* Process Unlocked */
3509+
__HAL_UNLOCK(hi2c);
3510+
3511+
return HAL_ERROR;
3512+
}
3513+
}
3514+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3515+
34953516
/* Process Locked */
34963517
__HAL_LOCK(hi2c);
34973518

@@ -3591,6 +3612,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint1
35913612
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
35923613
}
35933614

3615+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3616+
/* Wait until STOP flag is reset */
3617+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3618+
do
3619+
{
3620+
count--;
3621+
if (count == 0U)
3622+
{
3623+
hi2c->PreviousState = I2C_STATE_NONE;
3624+
hi2c->State = HAL_I2C_STATE_READY;
3625+
hi2c->Mode = HAL_I2C_MODE_NONE;
3626+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3627+
3628+
/* Process Unlocked */
3629+
__HAL_UNLOCK(hi2c);
3630+
3631+
return HAL_ERROR;
3632+
}
3633+
}
3634+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3635+
35943636
/* Process Locked */
35953637
__HAL_LOCK(hi2c);
35963638

@@ -3757,6 +3799,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_
37573799
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
37583800
}
37593801

3802+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3803+
/* Wait until STOP flag is reset */
3804+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3805+
do
3806+
{
3807+
count--;
3808+
if (count == 0U)
3809+
{
3810+
hi2c->PreviousState = I2C_STATE_NONE;
3811+
hi2c->State = HAL_I2C_STATE_READY;
3812+
hi2c->Mode = HAL_I2C_MODE_NONE;
3813+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3814+
3815+
/* Process Unlocked */
3816+
__HAL_UNLOCK(hi2c);
3817+
3818+
return HAL_ERROR;
3819+
}
3820+
}
3821+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3822+
37603823
/* Process Locked */
37613824
__HAL_LOCK(hi2c);
37623825

@@ -3882,6 +3945,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16
38823945
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
38833946
}
38843947

3948+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3949+
/* Wait until STOP flag is reset */
3950+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3951+
do
3952+
{
3953+
count--;
3954+
if (count == 0U)
3955+
{
3956+
hi2c->PreviousState = I2C_STATE_NONE;
3957+
hi2c->State = HAL_I2C_STATE_READY;
3958+
hi2c->Mode = HAL_I2C_MODE_NONE;
3959+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3960+
3961+
/* Process Unlocked */
3962+
__HAL_UNLOCK(hi2c);
3963+
3964+
return HAL_ERROR;
3965+
}
3966+
}
3967+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3968+
38853969
/* Process Locked */
38863970
__HAL_LOCK(hi2c);
38873971

@@ -4565,7 +4649,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevA
45654649
UNUSED(DevAddress);
45664650

45674651
/* Abort Master transfer during Receive or Transmit process */
4568-
if (hi2c->Mode == HAL_I2C_MODE_MASTER)
4652+
if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && (hi2c->Mode == HAL_I2C_MODE_MASTER))
45694653
{
45704654
/* Process Locked */
45714655
__HAL_LOCK(hi2c);
@@ -4596,6 +4680,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevA
45964680
{
45974681
/* Wrong usage of abort function */
45984682
/* This function should be used only in case of abort monitored by master device */
4683+
/* Or periphal is not in busy state, mean there is no active sequence to be abort */
45994684
return HAL_ERROR;
46004685
}
46014686
}

0 commit comments

Comments
 (0)