AnsweredAssumed Answered

I²C read operation freezing

Question asked by hofmeister.wolfram on Jun 15, 2012
Latest reply on Jul 8, 2012 by hofmeister.wolfram
Hello guys,

I've got the following problem: I'm trying to read a register of a gyroscope with I²C. Everything works fine as long as I'm using several delays during the I²C communication. This wasn't a problem until yet, where I noticed that exact those delays lock the CPU completely down. So now I need to figure out why this code keeps freezing without delays:

uint8_t I2C_Read_Byte(uint8_t __slave_addr, uint8_t __reg_addr)
{
    uint8_t __retval;

    while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
    I2C_GenerateSTART(I2C1, ENABLE);
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(I2C1, __slave_addr, I2C_Direction_Transmitter);
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    I2C_SendData(I2C1, __reg_addr);
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) ;
    I2C_GenerateSTART(I2C1, ENABLE);
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(I2C1, __slave_addr, I2C_Direction_Receiver);
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

    Delay_us(57); /* freezing without delay */

    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) ;
    I2C_AcknowledgeConfig(I2C1, DISABLE);
    __retval = I2C_ReceiveData(I2C1);

    Delay_us(101); /* same here */

    I2C_GenerateSTOP(I2C1, ENABLE);

    Delay_us(111); /* and here */

    I2C_AcknowledgeConfig(I2C1, ENABLE);

    return __retval;
}

I'd be very greatful for any help!

Best regards and thanks in advance,
Wolfram

Outcomes