cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 I2C 2nd ReadByte (can't GenerateStart)

farabiahmed
Associate II
Posted on July 08, 2010 at 12:52

STM32 I2C 2nd ReadByte (can't GenerateStart)

2 REPLIES 2
farabiahmed
Associate II
Posted on May 17, 2011 at 13:58

Does not anyone a suggestion?

John F.
Senior
Posted on May 17, 2011 at 13:58

I think you shouldn't have the STOP in the middle of your routine and I'm not sure about the ACK disable.

Your code looks similar to some I use to read another I2C device with device address, register number and byte read(s) - which seems to work. Does it help you?

John F.

#define DeviceAddress 0xCE

void DeviceReadBuffer(uint8_t* pBuffer, uint8_t NumByteToRead)

  /*!< While the bus is busy */

  while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))

  {

  }

 

  /*!< Send START condition */

  I2C_GenerateSTART(I2C1, ENABLE);

 

  /*!< Test on EV5 and clear it */

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))

  {

  }

  

  /*!< Send EEPROM address for write */

  I2C_Send7bitAddress(I2C1, LTC4151Address, I2C_Direction_Transmitter);

  /*!< Test on EV6 and clear it */

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))

  {

  }

  //send the reg select COMMAND byte

  I2C_SendData(I2C1, 0);   

 

  /*!< Test on EV8 and clear it */

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))

  {

  }

 

  //send START condition a second time

  I2C_GenerateSTART(I2C1, ENABLE);

 

  /*!< Test on EV5 and clear it */

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))

  {

  }

 

  /*!< Send EEPROM address for read */

  I2C_Send7bitAddress(I2C1, LTC4151Address, I2C_Direction_Receiver);

 

  /*!< Test on EV6 and clear it */

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))

  {

  }

 

  /*!< While there is data to be read */

  while(NumByteToRead) 

  {

    if(NumByteToRead == 1)

    {

      /*!< Disable Acknowledgement */

      I2C_AcknowledgeConfig(I2C1, DISABLE);

     

      /*!< Send STOP Condition */

      I2C_GenerateSTOP(I2C1, ENABLE);

    }

    /*!< Test on EV7 and clear it */

    if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) 

    {     

      /*!< Read a byte from the EEPROM */

      *pBuffer = I2C_ReceiveData(I2C1);

      /*!< Point to the next location where the byte read will be saved */

      pBuffer++;

     

      /*!< Decrement the read bytes counter */

      NumByteToRead--;       

    }  

  }

  /*!< Enable Acknowledgement to be ready for another reception */

  I2C_AcknowledgeConfig(I2C1, ENABLE);

}