Skip to main content
WSher.1
Associate III
October 22, 2021
Solved

How to use HAL_I2C with repeated start conditions

  • October 22, 2021
  • 2 replies
  • 1814 views

Hello! I'm trying to interface with a sensor that requires a repeated start condition.

Datasheet: https://www.mouser.com/datasheet/2/348/ROHM_S_A0005047448_1-2562246.pdf

I have other sensors on my I2C bus that I can communicate with.

I also get '1' on HAL_I2C_IsDeviceReady(&hi2c1, 0x5B, 3, 5);

I tried using HAL_I2C_Mem_Read, as it's my understanding that this function does a repeated start, but it doesn't seem to work with the sensor (this function works with 3 other sensors on my bus).

I'm assuming the implementation of HAL_I2C_Mem_Read doesn't meet my requirements.

The sensor I'm interfacing requires: [ST][SLAVE ADDR][W/0][ACK][READ REG][ACK][ST][SLAVE ADDR][R/1][ACK][DATA][NACK][SP]

What am I getting wrong here?

Using STM32WB, with CubeIDE.

Thank you!!! :)

This topic has been closed for replies.
Best answer by TDK

> I also get '1' on HAL_I2C_IsDeviceReady(&hi2c1, 0x5B, 3, 5);

Getting 1 (HAL_ERROR) means the slave is not responding.

Devices usually specify their slave address as 7 bits, in which case it needs to be left shifted one bit when you send it to HAL_I2C_* functions. The remaining R/W bit gets populated automatically depending on the function type. Try:

HAL_I2C_IsDeviceReady(&hi2c1, 0x5B << 1, 3, 5);

2 replies

WSher.1
WSher.1Author
Associate III
October 22, 2021

To add to this, I don't believe anything is wrong with the HW in this case. I'm using the standard breakout board.

 uint8_t count;
 count= HAL_I2C_IsDeviceReady(&hi2c1, 0x5B, 3, 5);
 HAL_I2C_Mem_Read(&hi2c1, 0x5B, 0x0F, 1, &count, 1, 1500);

And this is all I'm doing to try and read the device ID, which should return E0.

TDK
TDKBest answer
Super User
October 22, 2021

> I also get '1' on HAL_I2C_IsDeviceReady(&hi2c1, 0x5B, 3, 5);

Getting 1 (HAL_ERROR) means the slave is not responding.

Devices usually specify their slave address as 7 bits, in which case it needs to be left shifted one bit when you send it to HAL_I2C_* functions. The remaining R/W bit gets populated automatically depending on the function type. Try:

HAL_I2C_IsDeviceReady(&hi2c1, 0x5B << 1, 3, 5);

"If you feel a post has answered your question, please click ""Accept as Solution""."
WSher.1
WSher.1Author
Associate III
October 22, 2021

Huzzah! This worked.

Apparently I don't know how to properly address I2C... lol.

I must've missed in the datasheet that the address was only 7 bits.

TDK
Super User
October 22, 2021
Yep, it's a mess. Some suppliers specify a 7-bit address, others an 8-bit address. Just something you need to be mindful of with every new part. Glad it's working.
"If you feel a post has answered your question, please click ""Accept as Solution""."