Skip to content

Commit 709dbc2

Browse files
committed
[L1] I2C HAL fix: generate Start only once Stop is finished
1 parent 71d1bf0 commit 709dbc2

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

system/Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_i2c.c

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

3419+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3420+
/* Wait until STOP flag is reset */
3421+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3422+
do
3423+
{
3424+
count--;
3425+
if (count == 0U)
3426+
{
3427+
hi2c->PreviousState = I2C_STATE_NONE;
3428+
hi2c->State = HAL_I2C_STATE_READY;
3429+
hi2c->Mode = HAL_I2C_MODE_NONE;
3430+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3431+
3432+
/* Process Unlocked */
3433+
__HAL_UNLOCK(hi2c);
3434+
3435+
return HAL_ERROR;
3436+
}
3437+
}
3438+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3439+
34193440
/* Process Locked */
34203441
__HAL_LOCK(hi2c);
34213442

@@ -3515,6 +3536,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint1
35153536
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
35163537
}
35173538

3539+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3540+
/* Wait until STOP flag is reset */
3541+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3542+
do
3543+
{
3544+
count--;
3545+
if (count == 0U)
3546+
{
3547+
hi2c->PreviousState = I2C_STATE_NONE;
3548+
hi2c->State = HAL_I2C_STATE_READY;
3549+
hi2c->Mode = HAL_I2C_MODE_NONE;
3550+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3551+
3552+
/* Process Unlocked */
3553+
__HAL_UNLOCK(hi2c);
3554+
3555+
return HAL_ERROR;
3556+
}
3557+
}
3558+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3559+
35183560
/* Process Locked */
35193561
__HAL_LOCK(hi2c);
35203562

@@ -3681,6 +3723,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_
36813723
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
36823724
}
36833725

3726+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3727+
/* Wait until STOP flag is reset */
3728+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3729+
do
3730+
{
3731+
count--;
3732+
if (count == 0U)
3733+
{
3734+
hi2c->PreviousState = I2C_STATE_NONE;
3735+
hi2c->State = HAL_I2C_STATE_READY;
3736+
hi2c->Mode = HAL_I2C_MODE_NONE;
3737+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3738+
3739+
/* Process Unlocked */
3740+
__HAL_UNLOCK(hi2c);
3741+
3742+
return HAL_ERROR;
3743+
}
3744+
}
3745+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3746+
36843747
/* Process Locked */
36853748
__HAL_LOCK(hi2c);
36863749

@@ -3798,6 +3861,27 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16
37983861
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
37993862
}
38003863

3864+
/* Before any new treatment like start or restart, check that there is no pending STOP request */
3865+
/* Wait until STOP flag is reset */
3866+
count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3867+
do
3868+
{
3869+
count--;
3870+
if (count == 0U)
3871+
{
3872+
hi2c->PreviousState = I2C_STATE_NONE;
3873+
hi2c->State = HAL_I2C_STATE_READY;
3874+
hi2c->Mode = HAL_I2C_MODE_NONE;
3875+
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
3876+
3877+
/* Process Unlocked */
3878+
__HAL_UNLOCK(hi2c);
3879+
3880+
return HAL_ERROR;
3881+
}
3882+
}
3883+
while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
3884+
38013885
/* Process Locked */
38023886
__HAL_LOCK(hi2c);
38033887

@@ -4465,7 +4549,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevA
44654549
UNUSED(DevAddress);
44664550

44674551
/* Abort Master transfer during Receive or Transmit process */
4468-
if (hi2c->Mode == HAL_I2C_MODE_MASTER)
4552+
if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && (hi2c->Mode == HAL_I2C_MODE_MASTER))
44694553
{
44704554
/* Process Locked */
44714555
__HAL_LOCK(hi2c);
@@ -4496,6 +4580,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevA
44964580
{
44974581
/* Wrong usage of abort function */
44984582
/* This function should be used only in case of abort monitored by master device */
4583+
/* Or periphal is not in busy state, mean there is no active sequence to be abort */
44994584
return HAL_ERROR;
45004585
}
45014586
}

0 commit comments

Comments
 (0)