Skip to content

Commit d5815c6

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

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

system/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_hal_i2c.c

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

3431+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3432+
/* Wait until STOP flag is reset */
3433+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3434+
do
3435+
{
3436+
count--;
3437+
if (count == 0U)
3438+
{
3439+
hi2c->PreviousState = I2C_STATE_NONE;
3440+
hi2c->State = HAL_I2C_STATE_READY;
3441+
hi2c->Mode = HAL_I2C_MODE_NONE;
3442+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3443+
3444+
/* Process Unlocked */
3445+
__HAL_UNLOCK(hi2c);
3446+
3447+
return HAL_ERROR;
3448+
}
3449+
}
3450+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3451+
34313452
/* Process Locked */
34323453
__HAL_LOCK(hi2c);
34333454

@@ -3527,6 +3548,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint1
35273548
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
35283549
}
35293550

3551+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3552+
/* Wait until STOP flag is reset */
3553+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3554+
do
3555+
{
3556+
count--;
3557+
if (count == 0U)
3558+
{
3559+
hi2c->PreviousState = I2C_STATE_NONE;
3560+
hi2c->State = HAL_I2C_STATE_READY;
3561+
hi2c->Mode = HAL_I2C_MODE_NONE;
3562+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3563+
3564+
/* Process Unlocked */
3565+
__HAL_UNLOCK(hi2c);
3566+
3567+
return HAL_ERROR;
3568+
}
3569+
}
3570+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3571+
35303572
/* Process Locked */
35313573
__HAL_LOCK(hi2c);
35323574

@@ -3693,6 +3735,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_
36933735
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
36943736
}
36953737

3738+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3739+
/* Wait until STOP flag is reset */
3740+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3741+
do
3742+
{
3743+
count--;
3744+
if (count == 0U)
3745+
{
3746+
hi2c->PreviousState = I2C_STATE_NONE;
3747+
hi2c->State = HAL_I2C_STATE_READY;
3748+
hi2c->Mode = HAL_I2C_MODE_NONE;
3749+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3750+
3751+
/* Process Unlocked */
3752+
__HAL_UNLOCK(hi2c);
3753+
3754+
return HAL_ERROR;
3755+
}
3756+
}
3757+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3758+
36963759
/* Process Locked */
36973760
__HAL_LOCK(hi2c);
36983761

@@ -3818,6 +3881,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16
38183881
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
38193882
}
38203883

3884+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3885+
/* Wait until STOP flag is reset */
3886+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3887+
do
3888+
{
3889+
count--;
3890+
if (count == 0U)
3891+
{
3892+
hi2c->PreviousState = I2C_STATE_NONE;
3893+
hi2c->State = HAL_I2C_STATE_READY;
3894+
hi2c->Mode = HAL_I2C_MODE_NONE;
3895+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3896+
3897+
/* Process Unlocked */
3898+
__HAL_UNLOCK(hi2c);
3899+
3900+
return HAL_ERROR;
3901+
}
3902+
}
3903+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3904+
38213905
/* Process Locked */
38223906
__HAL_LOCK(hi2c);
38233907

@@ -4501,7 +4585,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevA
45014585
UNUSED(DevAddress);
45024586

45034587
/* Abort Master transfer during Receive or Transmit process */
4504-
if (hi2c->Mode == HAL_I2C_MODE_MASTER)
4588+
if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && (hi2c->Mode == HAL_I2C_MODE_MASTER))
45054589
{
45064590
/* Process Locked */
45074591
__HAL_LOCK(hi2c);
@@ -4532,6 +4616,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevA
45324616
{
45334617
/* Wrong usage of abort function */
45344618
/* This function should be used only in case of abort monitored by master device */
4619+
/* Or periphal is not in busy state, mean there is no active sequence to be abort */
45354620
return HAL_ERROR;
45364621
}
45374622
}

0 commit comments

Comments
 (0)