cancel
Showing results for 
Search instead for 
Did you mean: 

I2C MASTER TRANSMIT IS NOT WORKING

Hito
Associate II

 

STM32 DevloperBord : STM32F030C6T6TR
CubeMX : version 6.7.0
keil : version 5

i2c config at standard mode I2C_Speed_Frequancy-100KHZ

/*************************************************************************************/
static void MX_I2C1_Init(void)
{

/* USER CODE BEGIN I2C1_Init 0 */

/* USER CODE END I2C1_Init 0 */

/* USER CODE BEGIN I2C1_Init 1 */

/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x00201D2C;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}

/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}

/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
Error_Handler();
}

/*************************************************************************************/
if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
{
return HAL_ERROR;
}

/*************************************************************************************/
static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
uint32_t Tickstart)
{
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
{
/* Check if an error is detected */
if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
{
return HAL_ERROR;
}

/* Check for the Timeout */
if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
{
if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET))
{
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
hi2c->State = HAL_I2C_STATE_READY;
hi2c->Mode = HAL_I2C_MODE_NONE;

/* Process Unlocked */
__HAL_UNLOCK(hi2c);

return HAL_ERROR;
}
}
}
return HAL_OK;
}
/*************************************************************************************/


This function return HAL_ERROR every time and stuck in while loop
ErrorCode is --> 0x00000020 (TIMEOUT)
When i checked in oscilloscope, both SDA and SCL pins are at high state. I have checked multiple threads related to I2C error in HAL but haven't found solid root cause, can anyone help to address this issue?

 

4 REPLIES 4
Ghofrane GSOURI
ST Employee

Hello @Hito 

First let me thank you for posting.

The issue you are facing with the I2C communication could be caused by various factors. Here are some suggestions to help you troubleshoot and resolve the problem:

1- Try to use the latest version of CubeMX 6.9.2

2- Verify hardware connections: Ensure that the I2C bus connections (SDA and SCL) are properly connected and there are no loose or faulty connections.

3- Check pull-up resistors: Make sure that the SDA and SCL lines have appropriate pull-up resistors connected to the power supply voltage (e.g., 3.3V or 5V). Insufficient or missing pull-up resistors can cause communication issues.

Thx

Ghofrane

i'm using RTC(MCP79410) as peripheral and communicate with i2c
you suggested three solution i cheked all this threee solutions

1) - Verify hardware connections
2) - Check pull-up resistors
3) - Try to use the latest version of CubeMX 6.9.2

void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hi2c->Instance==I2C1)
{
/* USER CODE BEGIN I2C1_MspInit 0 */

/* USER CODE END I2C1_MspInit 0 */

__HAL_RCC_GPIOB_CLK_ENABLE();
/**I2C1 GPIO Configuration
PB10 ------> I2C1_SCL
PB11 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
/* USER CODE BEGIN I2C1_MspInit 1 */

/* USER CODE END I2C1_MspInit 1 */
}

}

ErrorCode is --> 0x00000020 (TIMEOUT)

But Still HAL_ERROR and stuck in While Loop

 

You dont show func where error ocur and no code what you use... plus place code in code part

static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
uint32_t Tickstart)
{
while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
{
/* Check if an error is detected */
if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
{
return HAL_ERROR;
}

/* Check for the Timeout */
if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
{
if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET))
{
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
hi2c->State = HAL_I2C_STATE_READY;
hi2c->Mode = HAL_I2C_MODE_NONE;

/* Process Unlocked */
__HAL_UNLOCK(hi2c);

return HAL_ERROR;
}
}
}
return HAL_OK;
}
AScha.3
Chief III

--- just what i have different:  ( + working...)

- own address:    hi2c1.Init.OwnAddress1 = 20;   // not zero

- pin speed : GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;   // not high

If you feel a post has answered your question, please click "Accept as Solution".