cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 HAL I2C Driver Address Problem

StudentWei
Associate II

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:

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

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

View solution in original post

3 REPLIES 3
KnarfB
Principal III

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

S.Ma
Principal

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.

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