AnsweredAssumed Answered

Am I right initialize I2C and read bytes?

Question asked by sokolc.anton on Dec 6, 2011
Latest reply on Dec 12, 2011 by sokolc.anton
Hello. I have a device connected to stm32 through I2C1. Am I right initialize it?
#define DEV_ADDR 0x53
uint8_t I2C_ByteRead(uint8_t Addr)
{
     uint8_t tmp;


    while(I2C_GetFlagStatus(I2CG, I2C_FLAG_BUSY));     // While the bus is busy
    I2C_GenerateSTART(I2CG, ENABLE);     // Send START condition
    while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_MODE_SELECT));     // Test on EV5 and clear it
    I2C_Send7bitAddress(I2CG, DEV_ADDR<< 1, I2C_Direction_Transmitter);     // Send address for write (SAD+W)
    while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));     // Test on EV6 and clear it
    I2C_SendData(I2CG, Addr);     // Send the internal address to read from: MSB of the address first
    while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_BYTE_TRANSMITTED));     // Test on EV8 and clear it
    I2C_GenerateSTART(I2CG, ENABLE);     // Send STRAT condition a second time
    while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_MODE_SELECT));     // Test on EV5 and clear it
    I2C_Send7bitAddress(I2CG, DEV_ADDR<< 1, I2C_Direction_Receiver);     // Send address for read
    while(!I2C_CheckEvent(I2CG,I2C_EVENT_MASTER_BYTE_RECEIVED));     // Test on EV6 and clear it
    tmp=I2C_ReceiveData(I2CG);
    I2C_AcknowledgeConfig(I2CG, DISABLE);
    I2C_GenerateSTOP(I2CG, ENABLE);     // Send STOP Condition
    return tmp;
}
void I2CG_Init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    I2C_InitTypeDef  I2C_InitStructure;


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
        // I2CG clock enable
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2CG, ENABLE);
    // GPIOB clock enable
    // I2CG SCL and SDA configuration
    GPIO_InitStructure.GPIO_Pin = SCL|SDA;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
//     GPIO_PinAFConfig(GPIOC,SCLSource,GPIO_AF_I2CG);
 //      GPIO_PinAFConfig(GPIOC,SDASource,GPIO_AF_I2CG);
    // Enable I2CG reset state
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2CG, ENABLE);
       // Release I2CG from reset state
       RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2CG, DISABLE);
   // I2C_DeInit(I2C1);
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
    I2C_InitStructure.I2C_OwnAddress1 =  0x00;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
    I2C_AcknowledgeConfig(I2CG, ENABLE);
    I2C_Init(I2CG, &I2C_InitStructure);
    I2C_Cmd(I2CG, ENABLE);


}
I can't read a device ID(0xE5)
if(I2C_ByteRead(0x00)!=0xE5)
I always stuck at while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); in fuction "I2C_ByteRead"
Please help me understand what I'm doing wrong.

Outcomes