cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_I2C_Master_Transmit() may overrun 1byte.

Tadao Tomiyama
Associate III
Posted on October 19, 2016 at 06:03

Hi!

Attached stm32f4xx_hal_i2c.c is from stm32cube_fw_f4_v1131.zip.

line 657,

      if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (

Size

!= 0U))

It looks better to be hi2c->XferCount or hi2c->XferSize, than

Size

.

regards,

---------------------------

      /* Write data to DR */

      hi2c->Instance->DR = (*hi2c->pBuffPtr++);

      hi2c->XferCount--;

      hi2c->XferSize--;

      if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (

Size

!= 0U))

      {

        /* Write data to DR */

        hi2c->Instance->DR = (*hi2c->pBuffPtr++);

        hi2c->XferCount--;

        hi2c->XferSize--;

      }

---------------------------

#i2c #stm32cube
1 ACCEPTED SOLUTION

Accepted Solutions
tobias2
Associate II
Posted on October 31, 2016 at 13:08

Thanks for your reply,

I have changed the part at 5 locations so far:

@@ -654,7 +654,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevA
hi2c->XferCount--;
hi2c->XferSize--;
- if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
+ if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0U))
{
/* Write data to DR */
hi2c->Instance->DR = (*hi2c->pBuffPtr++);
@@ -1022,7 +1022,7 @@ HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData
hi2c->XferCount--;
hi2c->XferSize--;
- if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
+ if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0U))
{
/* Write data to DR */
hi2c->Instance->DR = (*hi2c->pBuffPtr++);
@@ -1138,7 +1138,7 @@ HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData,
hi2c->XferSize--;
hi2c->XferCount--;
- if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
+ if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0U))
{
/* Read data from DR */
(*hi2c->pBuffPtr++) = hi2c->Instance->DR;
@@ -2652,7 +2652,7 @@ HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress,
hi2c->XferCount--;
}
/* Two bytes */
- else if(hi2c->XferSize == 2U)
+ else if(Size == 2U)
{
/* Wait until BTF flag is set */
if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
@@ -3150,7 +3150,7 @@ HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddr
}
}
- if(hi2c->XferSize == 1U)
+ if(Size == 1U)
{
/* Disable Acknowledge */
hi2c->Instance->CR1 &= ~I2C_CR1_ACK;

So far, it works. Maybe it helps somebody (but no guarantee that it is absolutely correct) Greets, Tobi EDIT: Image not uploaded :\ Change to patch text...

View solution in original post

4 REPLIES 4
Walid FTITI_O
Senior II
Posted on October 19, 2016 at 11:15

Hi tomiyama.tadao, 

Thanks for the feedback. I submit a request internally to our development team.

-Hannibal-

tobias2
Associate II
Posted on October 25, 2016 at 11:53

Hello,

thanks for this post. I'm facing also the same problems with I2C since upgrading to the same HAL (1.5.2) which is from the FW package v1131.

With the previous FW lib (1.5.1) it worked.

The problem is, that I couldn't get it running so far. I exchanged XferSize and Size very often in the stm32f4xx_hal_i2c.c, but without success.

Do you have a version which is fixed?

Greets,

Tobias

Tadao Tomiyama
Associate III
Posted on October 29, 2016 at 11:34

Hi Tobias

No,I don't have.

I tried to modify sample project in X-CUBE-NFC1 STM32F401RE nucleo.

It did not work as expected, only a portion of data is set. Short message was transfered correctly. I forgot the threshold byte number.

 My workaround is to use old revision stm32f4xx_hal_i2c.c that is included in

the X-CUBE-NFC1. Only  the stm32f4xx_hal_i2c.c is overwritten, the other *.c *.h are from f4_v1131.

I am sorry that I was no help to you.

tobias2
Associate II
Posted on October 31, 2016 at 13:08

Thanks for your reply,

I have changed the part at 5 locations so far:

@@ -654,7 +654,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevA
hi2c->XferCount--;
hi2c->XferSize--;
- if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
+ if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0U))
{
/* Write data to DR */
hi2c->Instance->DR = (*hi2c->pBuffPtr++);
@@ -1022,7 +1022,7 @@ HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData
hi2c->XferCount--;
hi2c->XferSize--;
- if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
+ if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0U))
{
/* Write data to DR */
hi2c->Instance->DR = (*hi2c->pBuffPtr++);
@@ -1138,7 +1138,7 @@ HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData,
hi2c->XferSize--;
hi2c->XferCount--;
- if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
+ if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0U))
{
/* Read data from DR */
(*hi2c->pBuffPtr++) = hi2c->Instance->DR;
@@ -2652,7 +2652,7 @@ HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress,
hi2c->XferCount--;
}
/* Two bytes */
- else if(hi2c->XferSize == 2U)
+ else if(Size == 2U)
{
/* Wait until BTF flag is set */
if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
@@ -3150,7 +3150,7 @@ HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddr
}
}
- if(hi2c->XferSize == 1U)
+ if(Size == 1U)
{
/* Disable Acknowledge */
hi2c->Instance->CR1 &= ~I2C_CR1_ACK;

So far, it works. Maybe it helps somebody (but no guarantee that it is absolutely correct) Greets, Tobi EDIT: Image not uploaded :\ Change to patch text...