AnsweredAssumed Answered

stm32cube I2c busy stm32F107

Question asked by giso on Nov 24, 2016
Latest reply on Mar 5, 2018 by Ioan Domuta
Hi,

I found an issue in the smt32cubeMX (ver 4.17.0 stm32F1 lib ver 1.4.0) when generating the I2C init code.
I have an EEPROM connected to a smt32F107VC micro through  I2C.
Stm32cube does generate the following code to initialize the I2C.
void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  if(i2cHandle->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */


  /* USER CODE END I2C1_MspInit 0 */
  
    /**I2C1 GPIO Configuration    
    PB6     ------> I2C1_SCL
    PB7     ------> I2C1_SDA 
    */
    GPIO_InitStruct.Pin = I2C_SCL_EEPROM_Pin|I2C_SDA_EEPROM_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    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 */
  }
}

When I start this code my I2C interface is always busy, I found out that starting the I2C clock before to initialize the GPIO does fix it. See code below
void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{


  GPIO_InitTypeDef GPIO_InitStruct;
  if(i2cHandle->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */
    // must be done otherwise I2c is busy
    //?????
    __HAL_RCC_I2C1_CLK_ENABLE();


  /* USER CODE END I2C1_MspInit 0 */
  
    /**I2C1 GPIO Configuration    
    PB6     ------> I2C1_SCL
    PB7     ------> I2C1_SDA 
    */
    GPIO_InitStruct.Pin = I2C_SCL_EEPROM_Pin|I2C_SDA_EEPROM_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    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 */
  }
}

i checked in other driver and it looks like for SPI or UART drivers the clock is always started before the GPIO is configured.

Outcomes