cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103C, I2C_ErrorCode 4 with MPU6050

CSand
Associate III

Hello,

I setup i2c1 as per below. I am trying to get data from a MPU6050 device on I2C but I got error code 4.

On the same hardware I tested using Arduino IDE and I can read data from MPU without problems.

Can one support please guide me where should I look for the problem?

I2C configuration:

 hi2c1.Instance = I2C1;

 hi2c1.Init.ClockSpeed = 100000;

 hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;

 hi2c1.Init.OwnAddress1 = 0;

 hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

 hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

 hi2c1.Init.OwnAddress2 = 0;

 hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

 hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

My test code called after MX_I2C1_Init() call:

uint8_t Check=0x00;

HAL_I2C_Mem_Read(&hi2c1, 0x68, 0x75, 1, &Check, 1, HAL_MAX_DELAY);

2 REPLIES 2
TDK
Guru

HAL_I2C_ERROR_AF means the chip did not ACK the transaction.

The slave address is expected to be shifted left one bit.

  * @param  DevAddress Target device address: The device 7 bits address value
  *         in datasheet must be shifted to the left before calling the interface

So you'd call it with:

HAL_I2C_Mem_Read(&hi2c1, 0x68 << 1, 0x75, 1, &Check, 1, HAL_MAX_DELAY);

If you feel a post has answered your question, please click "Accept as Solution".
CSand
Associate III

I thought I tried before using 0x68 << 1 as address. May be at the time there were other issues as well. It just worked instantly.

Thanks for helping,