cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 I2C code doesn't work

Nickname925_O
Associate II
Posted on November 22, 2013 at 17:10

Hi,

here my code for I2C setup on STM32F051R8 (STM32 discovery board practically)

void set_I2C(void){

  GPIO_InitTypeDef GPIO_InitStructure;

  I2C_InitTypeDef I2C_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

     RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

     /* Configure the I2C clock source. The clock is derived from the HSI */

     RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);

     GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1);

     GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1);

     //Configure pins: SCL and SDA ------------------

     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

     GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

     GPIO_Init(GPIOB, &GPIO_InitStructure);

     I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;

     I2C_InitStructure.I2C_DigitalFilter = 0x00;

     I2C_InitStructure.I2C_OwnAddress1 = 0x00; // MPU6050 7-bit adress = 0x68, 8-bit adress = 0xD0;

     I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

     I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

     //I2C_InitStructure.I2C_Timing = 0xA0120227;

     I2C_InitStructure.I2C_Timing = 0x20310A0D;

     I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

     I2C_Init(I2C1, &I2C_InitStructure);

     I2C_Cmd(I2C1, ENABLE);

}

but when in the main I try to send one data:

I2C_SendData(I2C1, 0xAA);

I don't see any signal on the Pb6 PB7.

Please, can you tell me what's wrong?

Thanks a lot in advance.

#stm32 #i2c #stm32f0
11 REPLIES 11
Posted on November 22, 2013 at 19:21

Please, can you tell me what's wrong?

Do you have any external pull-ups? Otherwise you're not going to see any pin transitions.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Nickname925_O
Associate II
Posted on November 23, 2013 at 15:49

Hi Clive,

I've tried with and withoud external pullups (22.7 k)

and I tried with this

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

and this

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_PU;

Can you help me to understand what's the problem?

Thanks a lot in advance,

Peppe.

Posted on November 23, 2013 at 16:14

Isn't the bus protocol a little more complex than just using SendData

This uses the same PB6/7 pins

STM32F0xx_StdPeriph_Lib_V1.1.0\Utilities\STM32_EVAL\STM320518_EVAL\stm320518_eval_i2c_tsensor.c

and there are other examples within the library.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Nickname925_O
Associate II
Posted on November 23, 2013 at 17:24

Hi Clive,

well, I've just used SendData because I wanna know if the I2C is correctly set up before writing more code.... but the problem is that I do not see any data on the bus...

Nickname925_O
Associate II
Posted on November 24, 2013 at 10:36

So Clive,

do you have any idea about the error I make in the set up?

If I use the send command I should see some data in the SDA at scope, correct?

Let me know and thanks in advance,

Peppe.

Posted on November 24, 2013 at 14:14

The initialization code looks reasonable enough, not sure on the timing parameter. This is not a part I'm actively using, and a protocol/implementation I'm not a big fan of.

I've cited an example I think I'd start with as a reference point.

I don't think that a SendData absent any surrounding control and sequencing is going to do the job here. I think you are going to need to pay more attention to the status and control of the peripheral, and perhaps that will indicate why it's not placing the data you expect on the wire.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Nickname925_O
Associate II
Posted on November 24, 2013 at 18:58

Hi Clive,

I understand well what you mean but the problem is that I don't see neither the clock on SCL line... well I am fear that I miss something in I2C parameter setting...

I'd like a lot an help.

Thanks,

Peppe.

Posted on November 24, 2013 at 23:55

I'd like a lot of help

I'm not looking to do dev work on the STM32F0. Did you have an hourly rate in mind?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Nickname925_O
Associate II
Posted on November 25, 2013 at 23:08

Ahahah...

I am sorry Clive but it was a type error mistake 🙂

I wanted to tell you: ''I'd like an help in order to get a base I2C set up''.

Thanks in any case for your time 🙂

Ciao,

Peppe.