cancel
Showing results for 
Search instead for 
Did you mean: 

Need help understanding HAL I2C operation

TomC1
Associate III

I am attempting to transmit four bytes via I2C to a device with address 0x27, using the code below.

data_t[0] = 0x01;
data_t[1] = 0x02;
data_t[2] = 0x04;
data_t[3] = 0x08;
HAL_I2C_Master_Transmit (&hi2c4, 0x27U,(uint8_t *) data_t, 4, 100);

My understanding is that the structure of an I2C message is

7 bit addr | R/W bit = 0 (for write) | Data

The address is 0x27 = 0b0100111

The RW bit is 0 for W

So the header byte is then 0b01001110 = 0x4E

So I would expect one of two possible things to happen when I use this command. Either

  • Each byte is separate and preceded by the address byte. Then 8 bytes will be clocked out to the I2C bus

[0x4E],[0x01],[0x4E],[0x02],[0x4E],[0x04],[0x4E],[0x08]

  • Or there is one address byte, followed by 4 bytes of data. Then 5 bytes will be clocked out

[0x4E],[0x01],[0x02],[0x04],[0x08]

What I actually see on the logic analyser below. So there is 5 bytes, which is something I would have expected, but it seems to be just repeating the address each time, and never actually writing the data.

TomC1_0-1752159941446.png

If anyone could offer any insight on what is going wrong here, it would be appreciated.

2 REPLIES 2
MM..1
Chief III

Simply HAL use shifted addr:

HAL_I2C_Master_Transmit (&hi2c4, 0x27U<<1,(uint8_t *) data_t, 4, 100);
Saket_Om
ST Employee

Hello @TomC1 

Please refer to the RM0433 to check the transfer bus diagrams. 

Saket_Om_0-1752223355299.png

As @MM..1 mentioned the HAL functions expect the address to be left-shifted by 1 bit.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om