Skip to main content
PLee.3
Associate II
May 27, 2022
Question

I2C - Adding a registry & value.

  • May 27, 2022
  • 3 replies
  • 997 views

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!

This topic has been closed for replies.

3 replies

Andrew Neil
Super User
May 27, 2022

"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")

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Tesla DeLorean
Guru
May 27, 2022

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

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
PLee.3
PLee.3Author
Associate II
May 30, 2022

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!