cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Issues in STM32F215

thirukannan
Associate II
Posted on June 28, 2014 at 17:27

Hi,

I have configured I2C2 in stm32f215 like below mentioned code, as well as i tried to read the device id. But while i am running its hanging  in

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); .Please

help how to solve this. Below is my code.

static void I2C_Config(void)

{

 GPIO_InitTypeDef GPIO_InitStructure;

  I2C_InitTypeDef I2C_InitStructure;

    

          

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

 

//  /* Reset I2C1 IP */

//  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);

//

//  /* Release reset signal of I2C1 IP */

//  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);

  /* I2C1 SCL and SDA pins configuration */

  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_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

//  GPIO_Init(GPIOB, &GPIO_InitStructure);

 

  /* Alternate function remapping */

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2);

  /* I2C De-initialize */

  I2C_DeInit(I2C2);

  /* I2C configuration */

  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

  I2C_InitStructure.I2C_OwnAddress1 = 0xA0;

  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  I2C_InitStructure.I2C_ClockSpeed = 1000;

  /*!< I2C Initialize */

  I2C_Init(I2C2, &I2C_InitStructure);

  I2C_Cmd(I2C2, ENABLE);

  /* I2C ENABLE */

}

unsigned char I2C_ Pressure_Read(unsigned char Address)

{

    unsigned char c,Add=0x5D<<1;

     I2C_AcknowledgeConfig(I2C2, ENABLE);

    while (I2C_GetFlagStatus(I2C2,I2C_FLAG_BUSY));

    I2C_GenerateSTART(I2C2, ENABLE);

    while (!I2C_GetFlagStatus(I2C2,I2C_FLAG_SB));

    I2C_Send7bitAddress(I2C2, Add, I2C_Direction_Transmitter);

    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

    //I2C_SendData(I2C2, WRITE_ADD);

    I2C_SendData(I2C2,Address);

    while (!I2C_GetFlagStatus(I2C2,I2C_FLAG_TXE));

    I2C_GenerateSTART(I2C2, ENABLE);

     while (!I2C_GetFlagStatus(I2C2,I2C_FLAG_SB));

    //I2C_SendData(I2C2,READ_ADD);

     I2C_Send7bitAddress(I2C2, Add, I2C_Direction_Receiver);

    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

    c=I2C_ReceiveData(I2C2);

    I2C_NACKPositionConfig(I2C2, I2C_NACKPosition_Current);

    I2C_AcknowledgeConfig(I2C2, DISABLE);

    I2C_GenerateSTOP(I2C2, ENABLE);

    while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF));

    return c;

}

Regards,

Thirukkannan.P

4 REPLIES 4
stm322399
Senior
Posted on June 28, 2014 at 17:45

The GPIO configuration can explain your problem: make sure pins are open-drain and pull-up. Moreover, 50MHZ is not a good idea, configure them for the lowest speed (should be enough for 400KHz I2C)

thirukannan
Associate II
Posted on June 30, 2014 at 06:51

Hi gonzalez.laurent,

I tried with this configuration, because minimum speed is 2MHz only for this controller

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

But i am getting always BUSY in SR2 Register, once calling this function

 

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

From: gonzalez.laurent

Posted: Saturday, June 28, 2014 5:45 PM

Subject: SPI Issues in STM32F215

The GPIO configuration can explain your problem: make sure pins are open-drain and pull-up. Moreover, 50MHZ is not a good idea, configure them for the lowest speed (should be enough for 400KHz I2C)

thirukannan
Associate II
Posted on June 30, 2014 at 07:04

Hi gonzalez.laurent,

I tried with this configuration, because minimum speed is 2MHz only for this controller

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

But i am getting always BUSY in SR2 Register, once calling this function

 

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

stm322399
Senior
Posted on June 30, 2014 at 09:23

You do not need to care about I2C state before its configuration. At the moment you enable I2C clock, pins are still misconfigured, so that the I2C controller might think the bus is locked by another device.

After I2C controller initialization, if it is still BUSY, it is possible that the bus is really locked by another device, some I2C slave are stupid and does not reset on bus stop. In such a case you need to bitbang the bus, simply clocking the SCL until SDA is released (high) and then you generate a bus stop