AnsweredAssumed Answered

STM32F4 I2C TransferHandling

Question asked by jong_hwa.jeon on Jul 6, 2015
I change stm32f0 source code to stm32f4.
but, I have problem on TransferHandling function in stm32f0.
stm32f4 has no that function. 
so i search stm32f4 example source. but it doesn't work.

I2C2 is Gyro I2C, (PB10,PB11).
what is the problem???

 

void MyAHRS_receive(uint8_t address, uint8_t n, uint8_t* msg)

{

  if(n == 0)

  {

    ServerProtocol_errMsg("MyAHRS Message Length Error\n");

  } else

  {

    while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));

   

    // write register address

   

    //I2C_TransferHandling(I2C2, (MYAHRS_I2C_ADDRESS<<1), 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);

    I2C_GenerateSTART(I2C2, ENABLE);

    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

    I2C_Send7bitAddress(I2C2, (MYAHRS_I2C_ADDRESS<<1), I2C_Direction_Transmitter);

    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

   

    I2C_SendData(I2C2, address);

    ///while(!I2C_GetFlagStatus(I2C2, I2C_FLAG_BTF));

    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED));

   

    // read data

    //I2C_TransferHandling(I2C2, (MYAHRS_I2C_ADDRESS<<1)+1, n, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);

    I2C_GenerateSTART(I2C2, ENABLE);

    I2C_Send7bitAddress(I2C2, (MYAHRS_I2C_ADDRESS<<1)+1, I2C_Direction_Receiver);

    //I2C_GenerateSTOP(I2C2, ENABLE);

   

    for(int i = 0; i<n ; i++)

    {

      while(I2C_GetFlagStatus(I2C2,I2C_FLAG_RXNE) == RESET);

      msg[i] = I2C_ReceiveData(I2C2);

    }

  }

}

void I2C2_init()

{

  GPIO_InitTypeDef GPIO_InitStructure;

  I2C_InitTypeDef I2C_InitStruct;

 

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE );

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

 

//  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2);

//  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2);

  //PB10 : SCL //PB11 : SDA

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

 

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2);

 

//  I2C_DeInit(I2C2);

 

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

 

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //Open Drain

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_Init(GPIOC, &GPIO_InitStructure);

  GPIO_PinLockConfig(GPIOC, GPIO_Pin_0);

  GPIO_SetBits(GPIOC, GPIO_Pin_0);

 

  I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;

  I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;

  I2C_InitStruct.I2C_OwnAddress1 = 0x22;

  I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;

  I2C_InitStruct.I2C_ClockSpeed = 100000;//0x10420f13; //I2C_Timing

 

  I2C_Init(I2C2, &I2C_InitStruct);

  I2C_Cmd(I2C2, ENABLE ); 

}



Outcomes