I am trying to debug an issue regards byte order of bytes returned by i2cMasterTransmitTimeout.
When I trace the bus the returned byte order for a float[0][1][2][3] is [3][0][1][2]. I have checked the order in the i2c trace by probe scope.
The re-arranged byte order is only seen when running on some threads. Normally bytes are returned in the order to match the order that they arrive in. There don't appear to be any related errors appearing when I step through the code in either thread. I need both threads to work identically..
My code is as follows :
uint32_t I2CMux::recv_via_mux_lock(uint8_t channel,uint8_t addr, uint8_t* tx_buf, uint8_t* rx_buf,
size_t tx_qty, size_t rx_qty)
{
//tx_buf,rx_buf must be arrays
i2cAcquireBus(&m_driver);
auto ret = set_channel(channel); //sets mux channel
if (ret != I2C_NO_ERROR)
return ret;
auto status = i2cMasterTransmitTimeout(&m_driver,
addr,
tx_buf,
tx_qty,
rx_buf,
rx_qty,
TIME_MS2I(5));
i2cReleaseBus(&m_driver);
if (status != MSG_OK) {
return i2cGetErrors(&m_driver);
}
return I2C_NO_ERROR;
}
uint32_t SPM041::read_float(SPM041_registers command, float &data) {
uint8_t tx_buf[1] = {static_cast<uint8_t>(command)}; //Set first bit to 1 to read
tx_buf[0] |= 0x80;
uint8_t rbuf[4] = {0,0,0,0};
union {
float a;
unsigned char bytes[4];
} float_arr;
auto status = g_i2cMux.recv_via_mux_lock(m_mux_channel, m_SPM_address, &tx_buf[0], &rbuf[0],1,4);
//float_arr.bytes[0]=rbuf[3];
//float_arr.bytes[1]=rbuf[0];
//float_arr.bytes[2]=rbuf[1];
//float_arr.bytes[3]=rbuf[2];
//
float_arr.bytes[0]=rbuf[0];
float_arr.bytes[1]=rbuf[1];
float_arr.bytes[2]=rbuf[2];
float_arr.bytes[3]=rbuf[3];
//
data = float_arr.a;
return status;
}