Skip to main content
CSand
Senior
July 19, 2020
Question

STM32F103C, I2C_ErrorCode 4 with MPU6050

  • July 19, 2020
  • 2 replies
  • 2586 views

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);

This topic has been closed for replies.

2 replies

TDK
July 19, 2020

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
CSandAuthor
Senior
July 19, 2020

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,