Skip to content

Commit e7cab5c

Browse files
committed
[STM32] HAL F1: I2C fix btf / rxne cases
Applying the same fix as in L1 and F4. This is an alignement to F4 HAL as the same IP is used.
1 parent f88803b commit e7cab5c

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_i2c.c

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3980,6 +3980,7 @@ static HAL_StatusTypeDef I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c)
39803980
return HAL_OK;
39813981
}
39823982

3983+
39833984
/**
39843985
* @brief Handle RXNE flag for Master
39853986
* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
@@ -3988,6 +3989,7 @@ static HAL_StatusTypeDef I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c)
39883989
*/
39893990
static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c)
39903991
{
3992+
39913993
if(hi2c->State == HAL_I2C_STATE_BUSY_RX)
39923994
{
39933995
uint32_t tmp = 0U;
@@ -4001,34 +4003,24 @@ static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c)
40014003
}
40024004
else if((tmp == 2U) || (tmp == 3U))
40034005
{
4004-
if(hi2c->XferOptions != I2C_NEXT_FRAME)
4005-
{
4006-
/* Disable Acknowledge */
4007-
hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4008-
4009-
/* Enable Pos */
4010-
hi2c->Instance->CR1 |= I2C_CR1_POS;
4011-
}
4012-
else
4013-
{
4014-
/* Enable Acknowledge */
4015-
hi2c->Instance->CR1 |= I2C_CR1_ACK;
4016-
}
4006+
/* Disable Acknowledge */
4007+
hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4008+
4009+
/* Enable Pos */
4010+
hi2c->Instance->CR1 |= I2C_CR1_POS;
40174011

40184012
/* Disable BUF interrupt */
40194013
__HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
40204014
}
40214015
else
40224016
{
4023-
if(hi2c->XferOptions != I2C_NEXT_FRAME)
4024-
{
4025-
/* Disable Acknowledge */
4026-
hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4027-
}
4028-
else
4017+
/* Disable Acknowledge */
4018+
hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4019+
4020+
if(hi2c->XferOptions == I2C_NEXT_FRAME)
40294021
{
4030-
/* Enable Acknowledge */
4031-
hi2c->Instance->CR1 |= I2C_CR1_ACK;
4022+
/* Enable Pos */
4023+
hi2c->Instance->CR1 |= I2C_CR1_POS;
40324024
}
40334025

40344026
/* Disable EVT, BUF and ERR interrupt */
@@ -4038,17 +4030,17 @@ static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c)
40384030
(*hi2c->pBuffPtr++) = hi2c->Instance->DR;
40394031
hi2c->XferCount--;
40404032

4033+
tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
4034+
hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
40414035
hi2c->State = HAL_I2C_STATE_READY;
40424036

40434037
if(hi2c->Mode == HAL_I2C_MODE_MEM)
40444038
{
4045-
hi2c->PreviousState = I2C_STATE_NONE;
40464039
hi2c->Mode = HAL_I2C_MODE_NONE;
40474040
HAL_I2C_MemRxCpltCallback(hi2c);
40484041
}
40494042
else
40504043
{
4051-
hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
40524044
hi2c->Mode = HAL_I2C_MODE_NONE;
40534045
HAL_I2C_MasterRxCpltCallback(hi2c);
40544046
}
@@ -4066,6 +4058,7 @@ static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c)
40664058
static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
40674059
{
40684060
/* Declaration of temporary variables to prevent undefined behavior of volatile usage */
4061+
uint32_t tmp;
40694062
uint32_t CurrentXferOptions = hi2c->XferOptions;
40704063

40714064
if(hi2c->XferCount == 3U)
@@ -4085,25 +4078,21 @@ static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
40854078
/* Prepare next transfer or stop current transfer */
40864079
if((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME))
40874080
{
4088-
if(CurrentXferOptions != I2C_NEXT_FRAME)
4089-
{
4090-
/* Disable Acknowledge */
4091-
hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4092-
}
4093-
else
4081+
/* Disable Acknowledge */
4082+
hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
4083+
4084+
if((CurrentXferOptions == I2C_NEXT_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME))
40944085
{
4095-
/* Enable Acknowledge */
4096-
hi2c->Instance->CR1 |= I2C_CR1_ACK;
4086+
/* Generate Start */
4087+
hi2c->Instance->CR1 |= I2C_CR1_START;
40974088
}
4098-
4099-
/* Disable EVT and ERR interrupt */
4100-
__HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
4089+
tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
4090+
hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
41014091
}
41024092
else
41034093
{
4104-
/* Disable EVT and ERR interrupt */
4105-
__HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
4106-
4094+
hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
4095+
41074096
/* Generate Stop */
41084097
hi2c->Instance->CR1 |= I2C_CR1_STOP;
41094098
}
@@ -4116,18 +4105,20 @@ static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
41164105
(*hi2c->pBuffPtr++) = hi2c->Instance->DR;
41174106
hi2c->XferCount--;
41184107

4108+
/* Disable EVT and ERR interrupt */
4109+
__HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
4110+
41194111
hi2c->State = HAL_I2C_STATE_READY;
4120-
4112+
hi2c->PreviousState = I2C_STATE_NONE;
4113+
41214114
if(hi2c->Mode == HAL_I2C_MODE_MEM)
41224115
{
4123-
hi2c->PreviousState = I2C_STATE_NONE;
41244116
hi2c->Mode = HAL_I2C_MODE_NONE;
41254117

41264118
HAL_I2C_MemRxCpltCallback(hi2c);
41274119
}
41284120
else
41294121
{
4130-
hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
41314122
hi2c->Mode = HAL_I2C_MODE_NONE;
41324123

41334124
HAL_I2C_MasterRxCpltCallback(hi2c);
@@ -4142,6 +4133,7 @@ static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
41424133
return HAL_OK;
41434134
}
41444135

4136+
41454137
/**
41464138
* @brief Handle SB flag for Master
41474139
* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains

0 commit comments

Comments
 (0)