cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F1xx HAL I2C Generate Start Failure

k3nt00
Associate II
Posted on May 01, 2015 at 08:45

Hello,

I have not been able to successfully use the newSTM32F1xx HAL I2C on a STM32F103CB. The issue remains the same regardless of the method used (polling, Interrupt, or DMA). When attempting to write to a slave for the first time, the following always returns HAL_TIMEOUT

static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout)
{
/* Generate Start */
SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
/* Wait until SB flag is set */
if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)
{
return HAL_TIMEOUT;
}

This is because theI2C_FLAG_SB is never set in the SR1 register. The Start bit in CR1 is clearly being set, so something is not happening after this. Any help or suggestions would be greatly appreciated. Thanks
1 REPLY 1
k3nt00
Associate II
Posted on May 01, 2015 at 18:12

I found the problem.

STM32CubeMX Generated faulty I2C Init code. After reviewing example projects I noticed that the GPIO, I2C, and DMA clocks were being enabled before doing the GPIO Config. After switching the GPIO Init Config to follow the clock enables, the i2c peripheral started working just fine.

/* GPIO Ports Clock Enable */
__GPIOB_CLK_ENABLE();
/* Peripheral clock enable */
__I2C1_CLK_ENABLE();
/* DMA1 clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

Hope this helps someone else!