cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407 I2C

Parmin Nayani
Associate III
Posted on January 31, 2017 at 16:30

Hi All,

I m trying to use I2C on STM32F104. Here is my init code. I am not able to read an EEPROM. To test I sent data (I2C_SendData(I2C2, (uint8_t) 0x100);) in a loop (with a small delay), and monitored SCL and SDA pins on scope. There are absolutely no pulses on the pins. What could be wrong with the code? I await your help. Thanks.

void Config_I2C2 (void)

{

    GPIO_InitTypeDef GPIO_InitStructure;

    I2C_InitTypeDef I2C_InitStructure;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2);

    I2C_InitStructure.I2C_ClockSpeed = 10000;

    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

    I2C_InitStructure.I2C_OwnAddress1 = 0xF0;

    I2C_InitStructure.I2C_Ack = I2C_Ack_Disable;

    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

    I2C_Init(I2C2, &I2C_InitStructure);

    

    I2C_Cmd(I2C2, ENABLE);

}

Param

#i2c-problem
1 ACCEPTED SOLUTION

Accepted Solutions
Nesrine M_O
Lead II
Posted on February 01, 2017 at 10:38

Hi

nayani.parmin

,

Please refer toI2C and M24CXX EEPROM communication example under the STM32F4standard peripherals library, it may be very helpful :

STM32F4xx_DSP_StdPeriph_Lib_V1.7.1\Project\STM32F4xx_StdPeriph_Examples\I2C\I2C_EEPROM

This example shows how to use the I2C firmware library and an associate I2C EEPROM driver to communicate with an I2C EEPROM device (here the example is interfacing with M24C64 EEPROM)

-Nesrine-

View solution in original post

7 REPLIES 7
Sirma Siang
ST Employee
Posted on February 01, 2017 at 10:36

Hello Param,

I just compare with the skeleton code generated by CubeMX.

Actually cube set the following:

First enable the clock

static void MX_GPIO_Init(void)

{

  /* GPIO Ports Clock Enable */

  __HAL_RCC_GPIOF_CLK_ENABLE();

}

and then configure the I2C

  hi2c2.Instance = I2C2;

  hi2c2.Init.ClockSpeed = 100000;

  hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;

  hi2c2.Init.OwnAddress1 = 0;

  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

  hi2c2.Init.OwnAddress2 = 0;

  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

  if (HAL_I2C_Init(&hi2c2) != HAL_OK)

  {

    Error_Handler();

  }

Could you please show :

- How have you configured the clock ?

- Which HAL version are you using ?

Kind regards

Sirma

Nesrine M_O
Lead II
Posted on February 01, 2017 at 10:38

Hi

nayani.parmin

,

Please refer toI2C and M24CXX EEPROM communication example under the STM32F4standard peripherals library, it may be very helpful :

STM32F4xx_DSP_StdPeriph_Lib_V1.7.1\Project\STM32F4xx_StdPeriph_Examples\I2C\I2C_EEPROM

This example shows how to use the I2C firmware library and an associate I2C EEPROM driver to communicate with an I2C EEPROM device (here the example is interfacing with M24C64 EEPROM)

-Nesrine-

Parmin Nayani
Associate III
Posted on February 01, 2017 at 18:03

Hi,

Thank you for your time and help.

1. I have enabled the clock much before initializing I2C.  Here is the code

    RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA|

                            RCC_AHB1Periph_GPIOB|

                            RCC_AHB1Periph_GPIOC|

                            RCC_AHB1Periph_GPIOD|

                            RCC_AHB1Periph_GPIOE|

                            RCC_APB1Periph_I2C2|

                            RCC_APB1Periph_I2C1|

                            RCC_AHB1Periph_GPIOF,

                            ENABLE);

2. I will go through the example and revert.

Param

Parmin Nayani
Associate III
Posted on February 02, 2017 at 18:04

Hi Nesrine,

I was not able to locate the library you referred to. Can you kindly post link to the library please? Thank you.

Best regards

Param

Posted on February 03, 2017 at 09:45

Hi

Nayani.Parmin

,

Please refer to this link to download the

STM32F4standard peripherals library

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-libraries/stsw-stm32html

-Nesrine-

Parmin Nayani
Associate III
Posted on February 03, 2017 at 16:50

Thank you very much Nesrine.

Best regards

Param

Parmin Nayani
Associate III
Posted on February 18, 2017 at 15:08

Hi all,

Coming back after a long gap. Tried and tested sample codes to other suggestions. I2C on STM32F407VE is not working. I am wondering if there is any HW issue in the device itself. Checked errata which does mention about issues in START command. Can someone confirm? If so is there a workaround? I await your replies/suggestions. Thank you very much for your time.

Regards

Param