cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_I2C_IsDeviceReady and private function problem

samimi.electronics
Associate II

Hi,

I'm trying to interface with an MPU6050 with STM32F103C8T6 Blue pill board or the so called stm32duino board. I have code written as such:

/* USER CODE BEGIN 2 */

if(HAL_I2C_IsDeviceReady(&hi2c1, MPU6050_ADDRESS, 2, 10) == HAL_OK){

HAL_GPIO_TogglePin(SENSOR_OK_LED_GPIO_Port,SENSOR_OK_LED_Pin);

}

 /* USER CODE END 2 */

It works no problem. The problem is when I put this code into a function like so:

/* USER CODE BEGIN PFP */

void MPU6050_Init(void);

/* USER CODE END PFP */

/* USER CODE BEGIN Init */

MPU6050_Init();

 /* USER CODE END Init */

/* USER CODE BEGIN 4 */

void MPU6050_Init(void){

if(HAL_I2C_IsDeviceReady(&hi2c1, MPU6050_ADDRESS, 2, 10) == HAL_OK){

HAL_GPIO_TogglePin(SENSOR_OK_LED_GPIO_Port,SENSOR_OK_LED_Pin);

}

}

/* USER CODE END 4 */

Why? Am I doing something wrong? Does this have to do anything with pointers and functions that I don't know about? Because as far as I know, this should work just fine.

I'm using STM32CubeMX & Keil MDK v5.

2 REPLIES 2
asbjorn.heid
Associate III

The "USER CODE BEGIN Init" block is before the other peripherials are initialized, like the GPIO pin you're using for the sensor ok LED.

So move the MPU6050_Init() back to where you had the other code, ie in "USER CODE BEGIN 2" block.

samimi.electronics
Associate II

thanks men. it fixed.