Sonsivri

Electronics => AVR, 8051 Family and ARM Area => Topic started by: sam_des on October 15, 2016, 07:31:49 19:31



Title: STM32F1, CubeMx I2C issue
Post by: sam_des on October 15, 2016, 07:31:49 19:31
Hello,

Working on porting a project that was originally done using ST's StdPeriph Library to CubeMX HAL.
Controller is STM32F103ZET6.

One of the code involves a touch screen controller over I2C-1. Original code works fine, but cose generated by CubeMx does not.

Initialization of I2C goes fine with CubeMx code, But after that any read or write locks the controller in infinite loop checking BUSY status flag. Specifying any value for TimeOut doesn't work.

No other peripheral, internal or external is being used at the moment for testing I2C.
Scope shows no activity at all on SCL/SDA lines.
Also I am using latest CubeMx and CubeMxF1 packages.

Any help will be great.


Title: Re: STM32F1, CubeMx I2C issue
Post by: sam_des on October 16, 2016, 09:16:32 09:16
Hello again,

Finally solved the issue...
Here's what needs to be added to code from CubeMx to make it work...

Code:
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->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 = GPIO_PIN_6|GPIO_PIN_7;
    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 */
     __HAL_RCC_I2C1_FORCE_RESET();        // <---------------------------- Reset I2C module, before re-initializing
      HAL_Delay( SYS_TICKS_1S/100 );        // <---------------------------- This delay required otherwise I2C module remains reset
      __HAL_RCC_I2C1_RELEASE_RESET();    // <----------------------------

  /* USER CODE END I2C1_MspInit 1 */
  }
}

Also, if you are also using FSMC, make sure you disable FSMC clock before accessing I2C &/or vice versa...

Code:
// Disable I2C clock & Enable FSMC clock
__HAL_RCC_FSMC_CLK_ENABLE();
__HAL_RCC_I2C1_CLK_DISABLE();

// Can Use FSMC immediately

// Disable FSMC clock & enable I2C clock
__HAL_RCC_FSMC_CLK_DISABLE();
__HAL_RCC_I2C1_CLK_ENABLE();

// at least 10mS delay required before actually accessing I2C module, else again locked in infinite busy loop

Hope this helps others.
Thanks.

sam_des