Skip to main content
StudentWei
Associate II
December 15, 2021
Solved

STM32F4 HAL I2C Driver Address Problem

  • December 15, 2021
  • 3 replies
  • 1717 views

I have a problem of the HAL Driver function HAL_I2C_Master_Transmit's parameter 'DevAddress'. It says that the device 7 bits address value in datasheet must be shifted to the left before calling the interface, that is, if I want to transmit to a device addressed 0x60, the parameter should be 0xc0. And that's exactly what happened. But when I use the function HAL_I2C_Mem_Read, which has same require, the parameter was 0x60, the 0xc0 was wrong.

MCU: STM32F407ZGT6

CubeMX Firmware Package: V1.26.2​

I'am confused now. Please help me.:sad_but_relieved_face:

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

Don't understand what exactly caused you confusion, maybe showing a minimal code example would help.

All I2C API functions behave the same.

Let's take the MPU-9250 as example. The 7-bit I2C slave address in the datasheet is 0x68. You define in your code

#define IMU_I2C_SLAVE_ADDR (0x68 << 1)

and use that macro in your code. Debug step into the HAL source code to see what happens. Finally, you find a line

hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);

Moreover, check all HAL return codes to find errors as soon as possible.

hth

KnarfB

3 replies

KnarfB
KnarfBBest answer
Super User
December 15, 2021

Don't understand what exactly caused you confusion, maybe showing a minimal code example would help.

All I2C API functions behave the same.

Let's take the MPU-9250 as example. The 7-bit I2C slave address in the datasheet is 0x68. You define in your code

#define IMU_I2C_SLAVE_ADDR (0x68 << 1)

and use that macro in your code. Debug step into the HAL source code to see what happens. Finally, you find a line

hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);

Moreover, check all HAL return codes to find errors as soon as possible.

hth

KnarfB

StudentWei
Associate II
December 17, 2021

Thank you. I had resolved this question as your reply.

S.Ma
Principal
December 15, 2021

Build a handy sw tool that scan all slave addresses and stops when one is acknowledged, and put a breakpoint. You will get the idea and the answer to your question very fast.