1 year ago

#107520

test-img

Andrea

Issue With I2C Library

I'm not sure someone can help me, but I try. I'm working with an I2C library created by a ex collegue and I found some problems with that library. Sometimes, during the communication with a CCS811 sensor, the communication stuks inside a while loop;

unsigned char I2C_Receive_Byte(void) {
    //    while (!(I2C1->ISR & I2C_ISR_TXE));
    uint32_t empty = 0;
    
    do {

        empty = I2C1->ISR & I2C_ISR_RXNE;
               
    } while (!(empty));

    return I2C1->RXDR;
}

I tried to understand the problem and I discovered something strange within the receiver function;

void I2C_ReceiveMultiBytesWithAddress(unsigned char address, unsigned char pbytes[], int nbytes)
{
    // Set the peripheral (slave) I2C address and left shift by 1
    address = (address<<1);

    I2C1->CR2 &= ~I2C_CR2_SADD; //Clear old address
    I2C1->CR2 |= address; //New address

    if (I2C_StartRead(nbytes) != I2C_OK)
      // reset PE !!!!
      I2C_Disable_And_ReEnable();

    for (int index = 0; index<nbytes; index++)
        pbytes[index] = I2C_Receive_Byte();

    if (I2C_Stop() != I2C_OK)
      // reset PE !!!
      I2C_Disable_And_ReEnable();

    uint32_t address_reread = I2C1->CR2 & I2C_CR2_SADD;

}

That reset PE, I don't understand its goal.

I know I give you little information, but I am really stuck. Can anyone help me understand?

I'm working with an stm32F070.

stm32

stm32f0

0 Answers

Your Answer

Accepted video resources