Why am I getting this error whilst trying to perform an i2c read

Hi, I am trying to perform a read of 4 bytes from my Honeywell pressure sensor ~ to do that I am performing this operation -

I2C pressureread(PB_8,PB_9);

uint32_t readpressure(){
uint8_t parray[4];
uint8_t status;
pressureread.start(); // to start the i2c communication with the sensor we send the start bit
pressureread.read(0x31, &parray[0], 4, false);
status = parray[0];
if((status & 0b00100000) != 0){
return -1;
}
else
{
return(parray[1]<<16|parray[2]<<8|parray[3]);
}
}

However running this I get this error can someone help ~

*no instance of overloaded function “mbed::I2C::read” matches the argument list – argument types are: (int, uint8_t , int, bool) – object type is: mbed::I2C

Hello Utkarsh,

The I2C constructor has the following signature:

    /** Create an I2C Master interface, connected to the specified pins
     *
     *  @param sda I2C data line pin
     *  @param scl I2C clock line pin
     */
    I2C(PinName sda, PinName scl);

Since PB_8 is the I2C1_SCL pin and PB_9 is the I2C1_SDA pin the pressureread object shall be created as:

I2C pressureread(PB_9, PB_8);  // rather than pressureread(PB_8, PB_9);

An address like &parray[0] is of uint32_t type so try to cast it to uint8_t pointer when calling the read function as follows:

pressureread.read(0x31, (uint8_t*)&parray[0], 4, false);
1 Like

Hello,

and also do not use I2C::start() method the I2C::read ( *int, uint8_t *, int, bool**) method already manage it.

BR, Jan

1 Like

Hello Zoltan,

Thank you for your quick reply. I have switched the ports in my code as you said.
However, I am still getting the same error regarding the read function.
I checked the I2C.cpp file and in that the i2c read function has been defined as -

int I2C::read(int address, char *data, int length, bool repeated)

So my understanding is that the i2c read function expects a pointer to a character whereas the variable in my code(&parray[0]) is a pointer to an uint8_t(unsigned char) type.

So maybe I could convert the whole thing into a char array instead of uint_8t. As in instead of uint_8t parray[4] I could declare it as a char parray[4]

Just visit Mbed docks (It also contains an example)- I2C - API references and tutorials | Mbed OS 6 Documentation

char parray[4];
i2c.read(0x31, parray, 4);

BR, Jan

I’m sorry. Of course you are right (and Jan as well)! The second parameter should be of char* type. So you have two options:

  • Either change the array type to char
char parray[4];
  • or keep the array as it is and cast to char* when calling the read function:
pressureread.read(0x31, (char*)parray, 4, false);