2022-05-27 03:03 AM
Hi, I want to try sending data to a slave consisting of several registries and have seen code example:-
uint8_t buf[2];
buf[0] = register_addr;
buf[1] = data;
HAL_I2C_Mem_Write(ftHandle, slaveAddr, buf, 2, timeout);
Would this work in practice? As it is merging 2x8 bit integers together in 1 packet? I like the idea of creating a data array to send via serial makes things so much simpler!
2022-05-27 06:34 AM
"Would this work in practice?"
That would depend entirely on the Slave in question.
I2C itself just transports bytes - the interpretation of those bytes is entirely down to the Slave.
I2C itself has no concept of "registers".
https://www.nxp.com/docs/en/user-guide/UM10204.pdf
(Note that its "registers" - not "registries")
2022-05-27 08:01 AM
>>Would this work in practice?
Yes, as Andrew notes its usage would depend on the slave device, and presumably be described in its documentation. Most I2C devices auto-increment their internal register/address, so each subsequent byte goes to a different register.
uint8_t buf[4];
buf[0] = register_addr;
buf[1] = data;
buf[2] = data_for_reg_plus_one;
buf[3] = data_for_reg_plus_two;
HAL_I2C_Mem_Write(ftHandle, slaveAddr, buf, sizeof(buf), timeout);
2022-05-30 06:30 AM
Hi, Yes valid point the "HAL_I2C_Slave_Receive" receive command is a lot more limited compared to the Master_Send but I still think you could create a data array at the receive end that matched the sent data array length like so:-
uint8_t data[7];
uint8_t data1;
uint8_t data2;
uint8_t data3;
uint8_t data4;
uint8_t data5;
uint8_t data6;
uint8_t data7;
uint8_t data8;
HAL_I2C_Slave_Receive(&hi2c1, 0x00, data, 10);
data[0] = data1;
data[1] = data2;
data[2] = data3;
data[3] = data4;
data[4] = data5;
data[5] = data6;
data[6] = data7;
data[7] = data8;
Would this not work? It would surely make sending arrays of data a lot simpler!