Skip to content

Commit cc05df9

Browse files
authored
Merge pull request #883 from ABOSTM/I2C_WAIT_END_OF_STOP_BEFORE_START
I2C wait end of stop before start
2 parents e013b67 + 709dbc2 commit cc05df9

File tree

4 files changed

+344
-4
lines changed

4 files changed

+344
-4
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
}

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
}

system/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c

Lines changed: 86 additions & 1 deletion
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)