cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Configuration on STM32F2xx

stephan239955_st
Associate II
Posted on June 01, 2011 at 22:00

I am using a STM32F217 based board with the CrossStudio tool chain.  I am having problems configuring the I2C bus correctly.  No matter what I do I cannot measure any signals on the data or clock lines.  My configuration code is below.

 #define I2C1_PORT             GPIOB

 #define I2C1_SCL_PIN         GPIO_Pin_8

 #define I2C1_SDA_PIN         GPIO_Pin_9

void I2C_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  I2C_InitTypeDef I2C_InitStructure;

  /* Enable I2C1 and I2C1_PORT & Alternate Function clocks */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  /* Reset I2C1 IP */

  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);

 

  /* Release reset signal of I2C1 IP */

  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); 

  /* I2C1 SCL and SDA pins configuration */

  GPIO_InitStructure.GPIO_Pin = I2C1_SCL_PIN;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(I2C1_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = I2C1_SDA_PIN;

  GPIO_Init(I2C1_PORT, &GPIO_InitStructure);

 

  /* Alternate function remapping */

  GPIO_PinAFConfig(I2C1_PORT, I2C1_SCL_PIN, GPIO_AF_I2C1);

  GPIO_PinAFConfig(I2C1_PORT, I2C1_SDA_PIN, GPIO_AF_I2C1);

  /* I2C De-initialize */

  I2C_DeInit(I2C1);

  /* I2C configuration */

  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

  I2C_InitStructure.I2C_OwnAddress1 = 0x00;

  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  I2C_InitStructure.I2C_ClockSpeed = 400000;

  /*!< I2C Initialize */

  I2C_Init(I2C1, &I2C_InitStructure);

  /* I2C ENABLE */

  I2C_Cmd(I2C1, ENABLE);

}

I cannot get into master mode even when using the ''I2C_GenerateSTART(I2C1, ENABLE);'' command.  It seems like my configuration is incomplete since I don't measure any SDA/SCK signals.  I have similar, working, code for a STM32F107 based board but the '217 confounds me.

Thanks for any help.

Stephan

12 REPLIES 12
stephan239955_st
Associate II
Posted on June 03, 2011 at 21:34

Looks like the problem with my software configuration is related to how I used the alternate function remapping command.

I had...

  /* Alternate function remapping */

  GPIO_PinAFConfig(I2C1_PORT, I2C1_SCL_PIN, GPIO_AF_I2C1);

  GPIO_PinAFConfig(I2C1_PORT, I2C1_SDA_PIN, GPIO_AF_I2C1);

Where...

 #define I2C1_SCL_PIN         GPIO_Pin_8

 #define I2C1_SDA_PIN         GPIO_Pin_9

This is incorrect.  The GPIO_PinAFConfig function requires the GPIO_PinSource as an input parameter NOT GPIO_Pin.  It should have read this...

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);

Now my I2C is limping along.  I still have some more device specific debugging to do but at least I get viable SDA/SCK signals on my scope.

mtaallah
Associate II
Posted on June 06, 2011 at 17:04

Hi,

With STM32F217 board you should use this following Pins:

  I2C1_SCL =>  PB06

  I2C1_SDA =>  PB09

because other pins of I2C are used with other devices.

don't forget to update GPIO_PinAFConfig functions.

tilman
Associate II
Posted on June 10, 2011 at 10:04

Hi Stephan,

thank you very much, your code already helped a lot. Would you mind posting the lines with which you launch the communication? Is it simply:

I2C_Configuration();

I2C_GenerateSTART(I2C1, ENABLE);

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

I2C_SendData(I2C1, 0x0E); // Address

....

?

I am desperate of trying to get the I2C working on my F207, so I was glad to hear you succeded. Unfortunately, your code doesn't seem to work for me, there must be some errors in the way I launch the transmission. Otherwise, I really start to believe the reason is a faulty MCU...

Best regards

DonTseTse

stephan239955_st
Associate II
Posted on June 10, 2011 at 15:51

Hi,

You need to use the ''send address'' function for sending the device address.

 

I2C_Send7bitAddress(2C1, DeviceAddr, I2C_Direction_Receiver);

Look at the I2C examples included with the stm32f2xx_stdperiph_lib.zip file.

Stephan
tilman
Associate II
Posted on June 10, 2011 at 16:20

Hi Stephan,

thanks for your reply. I tried Send7BitAddress aswell and of course it is not the same as SendData, but it doesn't make any difference here - both somehow write a byte to data reg DR and at the end, both don't work for me. My problem is anterior to that, it is that after the call to I2C_GenerateStart() the SCL doesn't start clocking as it should, and when I call SendData (or Send7BitAddress), the port goes to acknowledge failure state AF - I never even managed to send a single bit. That's why I am so interested to know how you initiate transfer.

Best regards

DonTseTse

tilman
Associate II
Posted on June 15, 2011 at 10:10

So, what do you call between I2C_Config and I2C_Send7bitAddress? Just I2C_GenerateSTART ?

acihana
Associate II
Posted on June 02, 2014 at 10:55

I have the same problem for STM32F205. When I use GPIO_Pin_6 and GPIO_Pin_7, device working properly. I can see SCLK and SDA signals via my scope. But, when I configure  GPIO_Pin_8 and GPIO_Pin_9 for I2C device lead I2C read-write error and I can't see SCLK signal on GPIO_Pin_8 while I see SDA signal on GPIO_Pin_9.

Maybe, there is a pheripheral bug in STM32F series?

acihana
Associate II
Posted on June 02, 2014 at 10:59

I have the same problem for STM32F205. When I use GPIO_Pin_6 and GPIO_Pin_7, device working properly. I can see SCLK and SDA signals via my scope. But, when I configure  GPIO_Pin_8 and GPIO_Pin_9 for I2C, the device lead I2C read-write error and I can't see SCLK signal on GPIO_Pin_8 while I see SDA signal on GPIO_Pin_9.

Maybe, there is a pheripheral bug in STM32F series?

chen
Associate II
Posted on June 02, 2014 at 11:24

Hi

Please specify which part and number of pins of the device you are using.

''I have the same problem for STM32F205. When I use GPIO_Pin_6 and GPIO_Pin_7, device working properly. I can see SCLK and SDA signals via my scope. But, when I configure  GPIO_Pin_8 and GPIO_Pin_9 for I2C device lead I2C read-write error and I can't see SCLK signal on GPIO_Pin_8 while I see SDA signal on GPIO_Pin_9.''

You have not said which port the IO pins are.

You cannot just change pins to which ever you like. Each peripheral is linked to certain IO pins and can ONLY be used on those IO pins. Some peripherals are attached to multiple IO pins to give you multiple choices in which pins are used.