cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F37 I2C read data

yarreg
Associate
Posted on October 24, 2013 at 13:38

Hello,

I'm using the STM32F373 microcontroller and wrote a simple function to read data from a MPU6050 gyro/accel on bus i2c.

Problem is that when i try to read data - flag I2C_ISR_TXIS always equal RESET (not set).

Why this might happen?

Thanks

1 REPLY 1
yarreg
Associate
Posted on October 24, 2013 at 13:43

Here is source code with the problem.

void i2c1_init(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  I2C_InitTypeDef  I2C_InitStructure;

    

  RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);  

  

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);  

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

  

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_4);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_4);

  

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

    

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

    

  I2C_DeInit(I2C1);

  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;  

  I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;

  I2C_InitStructure.I2C_DigitalFilter = 0x00;

  I2C_InitStructure.I2C_OwnAddress1 = 0x33;

  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  I2C_InitStructure.I2C_Timing = 0xC062121F; 

  

  I2C_Init(I2C1, &I2C_InitStructure);  

  I2C_Cmd(I2C1, ENABLE);

}

uint8_t i2c_read(I2C_TypeDef* I2Cx, uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t len)

{    

  /* Test on BUSY Flag */

  uint32_t timeout = I2C_TIMEOUT;

  while(I2C_GetFlagStatus(I2Cx, I2C_ISR_BUSY) != RESET)

  {

    if((timeout--) == 0) return 0;

  }    

  I2C_TransferHandling(I2Cx, DeviceAddr, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);

  

  

  /* !!! Wait until TXIS flag is set !!! */

  timeout = I2C_TIMEOUT;

  while(I2C_GetFlagStatus(I2Cx, I2C_ISR_TXIS) == RESET) // PROBLEM HERE!!!!!!!!!!!!!!!

  {

    if((timeout--) == 0) return 0;

  }

  ....

  ....

}