cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_I2C_IsDeviceReady() returning HAL_BUSY

AllanLiu
Associate II

Hi, I'm using an STM32F401RE board for a class(programming using MacOS). Right now, I'm using VSCode and make to edit and compile my code, then using stlink to flash it. Now, I'm trying to communicate with an MPU6050 using I2C; however, when I run HAL_I2C_IsDeviceReady() all I'm getting is a return of HAL_BUSY. This happens even when everything is unplugged. Everything should be wired correctly, I've double checked and the green light on the MPU6050 lights up. 

Here is my code:

ret = HAL_I2C_IsDeviceReady(&hi2c1, MPU6050_ADDR, 10, 1000);
if (ret == HAL_OK) {
strcpy((char*)buf, "READY READY!\r\n");
HAL_UART_Transmit(&huart2, buf, strlen((char*)buf), HAL_MAX_DELAY);
}
else if (ret == HAL_ERROR) {
strcpy((char*)buf, "ERROR ERROR!\r\n");
HAL_UART_Transmit(&huart2, buf, strlen((char*)buf), HAL_MAX_DELAY);
}
else if (ret == HAL_BUSY) {
strcpy((char*)buf, "BUSY BUSY!\r\n");
HAL_UART_Transmit(&huart2, buf, strlen((char*)buf), HAL_MAX_DELAY);
}
else if (ret == HAL_TIMEOUT) {
strcpy((char*)buf, "TIMEOUT!\r\n");
HAL_UART_Transmit(&huart2, buf, strlen((char*)buf), HAL_MAX_DELAY);
}
HAL_GPIO_TogglePin(GPIOA, LD2_Pin);
HAL_Delay(500)
 
all of the if and else if statements were done to check what HAL_I2C_IsDeviceReady() is actually returning.
Any help would be very appreciated!
Thanks,
Allan
8 REPLIES 8
Foued_KH
ST Employee

Hello @AllanLiu , 

Could you please check the initialization and the configuration of the I2C bus and try to check the HW connection.

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

David Littell
Senior III

Are the required pull-up resistors installed for both the SCL and SDA signals?

What's the value of "MPU6050_ADDR"?

AllanLiu
Associate II

I figured it out. Turns out when I was soldering the board I missed the SDA pin, so I was getting that error. 

Now however, I'm getting funny values from the sensor, even though I'm dividing by 131.0 like the datasheet says. I've attached my code below if anyone would like to help me out 🙂

In readGyroGx() maybe gxh and gxl should be 8-bit variables?

I started with trying uint8_t datatype for gxh and gxl but it has the same outputs as using int.

Well, then, it's back to the Data Sheet!  😀

Well, to be more clear it Gx rests at roughly 46 but as soon as I rotate the mpu6050 Gx changes to a more reasonable value close to 0 because I was rotating it slowly. 

Pavel A.
Evangelist II

 

  uint8_t gyroData[] = {0};
  uint8_t gyroRegAddrStart = 0x43;
  HAL_StatusTypeDef pass;
  for (int i = 0; i < 6; i++) {
    pass = HAL_I2C_Mem_Read(&hi2c1, MPU6050_ADDR, gyroRegAddrStart + i, 1, &gyroData[i], 1, 100);

 

Here is your bug. There are no gyroData[i] elements besides of i==0. Recommendation as usual: get a good static analyzer. Consider changing the code:

 

  uint8_t gyroData[6] = {0};
  uint8_t gyroRegAddrStart = 0x43;
  HAL_StatusTypeDef pass;
  pass = HAL_I2C_Mem_Read(&hi2c1, MPU6050_ADDR, gyroRegAddrStart, 1, gyroData, 6, 100);