The Godfather talking
You may crack software. How about me?
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 23, 2024, 06:29:42 06:29


Login with username, password and session length


Pages: [1]
Print
Author Topic: STM32F1, CubeMx I2C issue  (Read 5612 times)
0 Members and 1 Guest are viewing this topic.
sam_des
Senior Member
****
Offline Offline

Posts: 253

Thank You
-Given: 124
-Receive: 146


« 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.
Logged

Never be afraid to do something new. Remember Amateurs built the Ark, Professionals built the Titanic !
sam_des
Senior Member
****
Offline Offline

Posts: 253

Thank You
-Given: 124
-Receive: 146


« Reply #1 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
Logged

Never be afraid to do something new. Remember Amateurs built the Ark, Professionals built the Titanic !
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC