cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 I2C issues

totally
Associate II
Posted on September 01, 2013 at 18:26

Hello,

I am trying to get the I2C bus to run with the MMA8452 accelerometer, but it just wont.

I can generate a start condition but nothing else. I'm watching the bus lines with a scope/logic analyzer and I can see that it doesn't generate anything after the start condition on SCL/SDA.

In debug mode I also noticed that after writing to the I2C data register the ''AF - acknowledge failure'' bit is set in SR1

The code:

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOA, ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

    I2C_InitTypeDef I2C_InitStruct;

    I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;

    I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

    I2C_InitStruct.I2C_ClockSpeed = 100000;

    I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;

    I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;

    I2C_InitStruct.I2C_OwnAddress1 = 0;

    I2C_Init(I2C2, &I2C_InitStruct);

    I2C_Cmd(I2C2, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    while(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0));

        

   

        GPIO_SetBits(GPIOA, GPIO_Pin_5); //Trigger for analyzer

        I2C_GenerateSTART(I2C2, ENABLE);

        I2C_Send7bitAddress(I2C2, 0x3A, I2C_Direction_Transmitter);

        while(1);

Any suggestions?
3 REPLIES 3
Posted on September 01, 2013 at 19:00

You don't fill the pin initialization structure completely/properly. Make sure to configure the output in Open Drain mode, and use pull-up resistors. 

If address 0x3A doesn't work, try 0x38
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
totally
Associate II
Posted on September 01, 2013 at 19:20

Ok, thanks, I changed it but it doesn't seem to have any affect, still no transmission after start condition!

totally
Associate II
Posted on September 02, 2013 at 11:27

Ok, problem solved.

Adding

      while(!I2C_CkeckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

after start condition generation helps a lot :)