Hello Eason,
Return value -2 indicated communication fail error. Firstly you should check I2C communication works well.
This is I2C read/write function on STM32 platform for your reference.
int8_t SensorAPI_I2Cx_Read(uint8_t slave_address7, uint8_t subaddress, uint8_t *pBuffer, uint16_t ReadNumbr) { uint16_t DevAddress = slave_address7 << 1;
// send register address HAL_I2C_Master_Transmit(&I2C_HANDLE, DevAddress, &subaddress, 1, BUS_TIMEOUT); HAL_I2C_Master_Receive(&I2C_HANDLE, DevAddress, pBuffer, ReadNumbr, BUS_TIMEOUT); return 0; }
int8_t SensorAPI_I2Cx_Write(uint8_t slave_address7, uint8_t subaddress, uint8_t *pBuffer, uint16_t WriteNumbr) { uint16_t DevAddress = slave_address7 << 1;
GTXBuffer[0] = subaddress; memcpy(>XBuffer[1], pBuffer, WriteNumbr);
// send register address HAL_I2C_Master_Transmit(&I2C_HANDLE, DevAddress, GTXBuffer, WriteNumbr+1, BUS_TIMEOUT); return 0; }
... View more