0

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.

Andrea
  • 89
  • 9
  • 1
    The I2C peripheral on many STM32 is rubbish. Read the errata sheet. You sometimes have to reset and restart to get out of a bug, however, you can't usually pick up where you left off when this happens so I would question that this code is correct. – Tom V Jan 28 '22 at 09:12
  • Thank you @TomV, ok so it could be a sort of workaround? Maybe I can terminate the communication after PE reset and return an error output. Could this be a good solution? – Andrea Jan 28 '22 at 09:37
  • I haven't used the I2C on the F0 so I don't know exactly, but the code reminds me of something similar I did on an F4. I expect you will have to work through this with the manual and a scope on the lines to see what it is doing. – Tom V Jan 28 '22 at 18:04
  • It is not a solution but it is better to always use a timeout counter so that your code can continue to work. – hpirlo Jan 29 '22 at 14:50
  • And try to use the CubeMX HAL library. There are many scenarios and corner cases that the HAL library can handle. Also, it is updated regularly. So, bugs will be resolved as reported. – hpirlo Jan 29 '22 at 14:54

0 Answers0