cancel
Showing results for 
Search instead for 
Did you mean: 

Am I right initialize I2C and read bytes?

wsevendays
Associate II
Posted on December 06, 2011 at 18:19

Hello. I have a device connected to stm32 through I2C1. Am I right initialize it?

&sharpdefine 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.

#stm32 #i2c
6 REPLIES 6
rosarium
Associate II
Posted on December 08, 2011 at 10:41

Are you facing some problem with this?

emalund
Associate III
Posted on December 08, 2011 at 14:26

evidentlu <i> can't read a device ID(0xE5)</i>

emalund
Associate III
Posted on December 08, 2011 at 14:26

evidently <i> can't read a device ID(0xE5)</i>

emalund
Associate III
Posted on December 08, 2011 at 14:28

sorry about the many replies, had trouble with the connection

Posted on December 08, 2011 at 16:23

sorry about the many replies, had trouble with the connection

The forum has been very flaky over the last couple of days, save your work people!

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
wsevendays
Associate II
Posted on December 12, 2011 at 23:32

Sorry for the late answer. My problem solved.