STM32G474: Byte order with i2cMasterTransmitTimeout.

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: RoccoMarco, barthess

gp7
Posts: 17
Joined: Fri Oct 14, 2022 1:52 pm
Has thanked: 10 times

STM32G474: Byte order with i2cMasterTransmitTimeout.

Postby gp7 » Tue Oct 18, 2022 4:42 pm

Hi all,

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;
}

steved
Posts: 823
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: STM32G474: Byte order with i2cMasterTransmitTimeout.

Postby steved » Tue Oct 18, 2022 6:20 pm

I've seen that when the link gets out of sync; I think you're seeing the last byte of a prior message, then the first 3 bytes of the next message.

Is the I2C mutex actually enabled?

Code: Select all

#define I2C_USE_MUTUAL_EXCLUSION            TRUE

gp7
Posts: 17
Joined: Fri Oct 14, 2022 1:52 pm
Has thanked: 10 times

Re: STM32G474: Byte order with i2cMasterTransmitTimeout.

Postby gp7 » Tue Oct 18, 2022 7:08 pm

Thanks steved,

I2C Mutual exclusion is enabled in halconf.h. I will need to figure out how I might be preserving the last byte of the previous message and why the new code might be overwriting the previous memory location, it should be out of scope by then.

I found this https://community.st.com/s/question/0D50X00009XkepLSAR/stm32f7-hal-i2c-reading-bytes-out-of-sync which looks interesting and I'll check for any error flags from the bus.


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 27 guests