cancel
Showing results for 
Search instead for 
Did you mean: 

Issues interfacing BNO086 with STM32 using CubeIDE (I²C + interrupt/reset)

Mathias_R
Visitor

Hello,

I am currently trying to read the angles from a BNO086 IMU using an STM32 microcontroller (using CubeIDE), and I use this library : https://www.grozeaion.com/electronics/stm32/stm32-i2c-library-for-bno08x-9-axis-imu.

The I²C communication seems to work correctly (the device responds, and I can receive data), but I am having several issues that I cannot resolve :

- Interrupt behavior : According to the documentation, the BNO086 should trigger the INT pin whenever new sensor data is available. In my setup, the interrupt seems to be triggered, but I don't receive any dataI added a condition so I can't receive data unless ROTATION_VECTOR is enabled, but it is never enabled because it never receives the right packet.

- Reset Behavior : After sending the initialization commands, the BNO086 seems to reboot and I get the reset response frame repeatedly. Because of this, the sensor never stays in a stable state, and I cannot get continuous valid data. The weird thing about this is that when I manually debug line by line, I receive the right type of packet but the condition ROTATION_VECTOR enabled is never true so I can't get the angles.

I suspect the problem comes from the initialization sequence. With the Arduino SparkFun BNO08x library, everything works fine (the device initializes properly and provides continuous data). But when I try to replicate the process in CubeIDE with the HAL I²C driver, the BNO086 never remains initialized.

My I2C address is correct, I cheked, INT pin is connected and configured as EXTI, Reset as a GPIO_Output. BOOT isn't wired.

Has anyone here experienced the BNO086 continuously resetting like this on STM32, and how did you solve it ?

Thanks in advance !

Here's my main.c, I used the .h and .c of the library : 

/* USER CODE BEGIN Header */
/*******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/

/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "tim.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "BNO_08x_I2C.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
volatile uint8_t BNO_Ready = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void PeriphCommonClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void I2C_Scan_Bus(void)
{
    HAL_StatusTypeDef result;
    uint8_t i;
    printf("Scanning I2C bus...\r\n");
    for (i = 1; i < 128; i++)
    {
        /*
        * HAL_I2C_IsDeviceReady returns HAL_OK if device responds,
        * else HAL_ERROR or HAL_BUSY or HAL_TIMEOUT
        */
        result = HAL_I2C_IsDeviceReady(&hi2c1, (i << 1), 1, 10);
        if (result == HAL_OK)
        {
            printf("Found device at address 0x%02X\r\n", i);
        }
    }
    printf("Scan complete.\r\n\n");
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
    /* USER CODE BEGIN 1 */
    /* USER CODE END 1 */
    /* MCU Configuration--------------------------------------------------------*/
    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();
    /* USER CODE BEGIN Init */
    /* USER CODE END Init */
    /* Configure the system clock */
    SystemClock_Config();
    /* Configure the peripherals common clocks */
    PeriphCommonClock_Config();
    /* USER CODE BEGIN SysInit */
    /* USER CODE END SysInit */
    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_I2C1_Init();
    MX_LPUART1_UART_Init();
    MX_TIM2_Init();
    /* USER CODE BEGIN 2 */
    ///* -----------------------------------------------------------------------------------------------
    BNO_RST_On;
    // BNO_BOOT_On;
    // Initialisation du capteur BNO08x
    if (BNO_Init() == HAL_OK)
    {
        BNO_setHighAccuracyMode();
        printf("BNO init OK\r\n");
    }
    else
    {
        // Si erreur
        printf("Erreur d'initialisation BNO08x !\n");
        while (1);
    }
    /* USER CODE END 2 */
    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        if (BNO_Ready)
        {
            if (isResetOccurred())
            { // Fonction modifiée en bool au lieu de uint8_t, à changer si pas concluant ::::::::::::::::::::::::::::::::::::::::::::::::::
                // Réactive le feature si reset
                // printf("rotation vector = %02X\n", ROTATION_VECTOR);
                if (BNO_setFeature(ROTATION_VECTOR, 100000, 0) == HAL_OK)
                {
                    // printf("%d\n", BNO_getFeature(ROTATION_VECTOR));
                    printf("Reset Occurred. Set feature OK!\r\n");
                    // printf("SensorId = %d\n", sensorData.sensorId);
                }
            }
            // BNO_setFeature(ROTATION_VECTOR, 100000, 0);
            if (BNO_dataAvailable() == HAL_OK)
            {
                BNO_setFeature(ROTATION_VECTOR, 100000, 0);
                printf("ReportID = 0x%02X\r\n", sensorData.sensorId);
            }
            if ((BNO_dataAvailable() == HAL_OK) && (sensorData.sensorId == ROTATION_VECTOR))
            {
                /*if (sensorData.sensorId == ROTATION_VECTOR) {*/
                sensorData.sensorId = 0; // Reset l'ID pour la prochaine frame
                rpy = BNO_getRollPitchYaw();
                printf("Roll: %.2f, Pitch: %.2f, Yaw: %.2f\r\n", rpy.Roll, rpy.Pitch, rpy.Yaw);
                // BNO_RotationVectorWAcc_t rot = getRotationVector();
                // printf("I: %.3f, J: %.3f, K: %.3f\n", rot.I, rot.J, rot.K);
                // BNO_Gyroscope_t gyro = getGyroscope();
                // printf("X: %.3f, Y: %.3f, Z: %.3f\n", gyro.X, gyro.Y, gyro.Z);
                /*} else {
                printf("SensorId reçu = %d\n", sensorData.sensorId);
                }*/
            }
        }
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
    }
    /* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    /** Configure the main internal regulator output voltage
    */
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
    /** Initializes the RCC Oscillators according to the specified parameters
    * in the RCC_OscInitTypeDef structure.
    */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE
                                       | RCC_OSCILLATORTYPE_MSI;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.MSIState = RCC_MSI_ON;
    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
    RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
        Error_Handler();
    }
    /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
    */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK4 | RCC_CLOCKTYPE_HCLK2
                                  | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                  | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1;
    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
    {
        Error_Handler();
    }
}
/**
* @brief Peripherals Common Clock Configuration
* @retval None
*/
void PeriphCommonClock_Config(void)
{
    RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
    /** Initializes the peripherals clock
    */

    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SMPS;
    PeriphClkInitStruct.SmpsClockSelection = RCC_SMPSCLKSOURCE_HSI;
    PeriphClkInitStruct.SmpsDivSelection = RCC_SMPSCLKDIV_RANGE1;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN Smps */
    /* USER CODE END Smps */
}
/* USER CODE BEGIN 4 */
/* Callback interruption GPIO */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    if (GPIO_Pin == GPIO_PIN_0)
    {
        BNO_Ready = 1;
        // printf("IRQ reçu, ready!\n");
    }
    __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while (1)
    {
    }
    /* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
*  file: pointer to the source file name
*  line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

Edit by moderator: Please make sure to use code blocks next time for better readability. Thank you!

0 REPLIES 0