cancel
Showing results for 
Search instead for 
Did you mean: 

how to checkSTM32F30x I2C NACK flag.

cbhar
Associate II
Posted on July 01, 2014 at 18:09

Since there is no function for the stm32f30 series like send7bitslaveaddress which is there for other series when do I check the NACK flag in case the slave is not present? The sending of the slave address seems to happen automatically but if I try to check the NACK flag after right after GenerateSTART command it is always set cause the address has not been sent yet. If however I wait till TXE is RESET after generate it gets stuck in this while loop after 3 calls. any idea how to deal with it? I want to make i2c non blocking using just flags. My code is:-

uint8_t I2C_write_1byte(uint8_t SLAVE_ADDR,uint8_t reg,uint8_t data)

{

  I2C_SlaveAddressConfig ( I2C_NUM,SLAVE_ADDR);

  I2C_AcknowledgeConfig(I2C_NUM, DISABLE); // MOST IMPORTANT!!! OR MAYBE NOT;

  I2C_TransferHandling ( I2C_NUM,SLAVE_ADDR,3,I2C_SoftEnd_Mode,I2C_Generate_Start_Write);

  I2C_GenerateSTART(I2C_NUM,ENABLE);

  mYellowTOGGLE;

  while(I2C_GetFlagStatus(I2C_NUM,I2C_FLAG_TXE)==RESET);

  mGreenTOGGLE;

  if (I2C_GetFlagStatus(I2C_NUM,I2C_FLAG_NACKF)==RESET)

  {

    I2C_ClearFlag(I2C_NUM, I2C_FLAG_TXE);

    I2C_ClearFlag(I2C_NUM, I2C_FLAG_NACKF);

    I2C_GenerateSTOP ( I2C_NUM,ENABLE);

  //   I2C_Cmd(I2C_NUM,DISABLE);

  //   I2C_Cmd(I2C_NUM,ENABLE);

    mBlueTOGGLE;

    return 0;

  }

  // mGreenTOGGLE;

  I2C_NUM->TXDR = reg;

  while(I2C_GetFlagStatus(I2C_NUM,I2C_FLAG_TXE)==RESET);

  I2C_NUM->TXDR = data;

  while(I2C_GetFlagStatus(I2C_NUM,I2C_FLAG_TXE)==RESET);

  I2C_GenerateSTOP ( I2C_NUM,ENABLE);

  return 1;

}

#stm32f303 #i2c
0 REPLIES 0