Hi,
We are using a Bosch BHI360 shuttle board 3.0 with an STM32 Nucleo board. We did an I2C scan and found the device at address 0x28. But now we are stuck at the firmware loading step. In our main.c file, we call bosch_imu_setup()
and bosch_imu_run()
from bosch_imu.c.
Can anyone help us with this issue? We can't figure out what’s going wrong. I have attached our main.c code below for reference.
/* 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"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <stdarg.h>
#include "common.h"
#include "bosch_imu.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 ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
TIM_HandleTypeDef htim3;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
uint64_t last_result = 0;
uint8_t sensor_reports = 0;
extern float Heading;
extern float Pitch;
extern float Roll;
extern float acc_x;
extern float acc_y;
extern float acc_z;
extern float gyr_x;
extern float gyr_y;
extern float gyr_z;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_I2C1_Init(void);
static void MX_TIM3_Init(void);
/* USER CODE BEGIN PFP */
uint64_t TIM7_ITs=0;
void micros_delay(uint64_t delay);
void I2C_Scan_Bus(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* Function to route printf to UART1 */
int __io_putchar(int ch) {
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
/* 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();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_I2C1_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
uint32_t last_i2c_scan_time=HAL_GetTick();
I2C_Scan_Bus();
printf("bosch setup\r\n");
bosch_imu_setup();
printf("bosch setup done\r\n");
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
bosch_imu_run();
uint32_t now = HAL_GetTick();
if(now-last_i2c_scan_time>=1000)
{
I2C_Scan_Bus();
last_i2c_scan_time = now;
}
printf("Hello from STM32 friday");
printf("Heading: %.2f°, Pitch: %.2f°, Roll: %.2f°\r\n", Heading, Pitch, Roll);
printf("Accel [g]: X=%.2f, Y=%.2f, Z=%.2f\r\n", acc_x, acc_y, acc_z);
printf("Gyro [dps]: X=%.2f, Y=%.2f, Z=%.2f\r\n\n", gyr_x, gyr_y, gyr_z);
HAL_Delay(500); // 500 ms delay
/* 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_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
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;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**
* @brief TIM3 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM3_Init(void)
{
/* USER CODE BEGIN TIM3_Init 0 */
/* USER CODE END TIM3_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* USER CODE BEGIN TIM3_Init 1 */
/* USER CODE END TIM3_Init 1 */
htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 65535;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM3_Init 2 */
/* USER CODE END TIM3_Init 2 */
HAL_TIM_MspPostInit(&htim3);
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : B1_Pin */
GPIO_InitStruct.Pin = B1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : LD2_Pin */
GPIO_InitStruct.Pin = LD2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* I2c scanner*/
void I2C_Scan_Bus(void)
{
printf("Scanning I2C bus...\r\n");
for (uint8_t addr = 1; addr < 128; addr++)
{
if (HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(addr << 1), 1, 10) == HAL_OK)
{
printf("I2C device found at address: 0x%02X\r\n", addr);
}
}
printf("I2C scan complete.\r\n\n");
}
/* I2C communication functions */
int8_t bhy2_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr)
{
(void)intf_ptr;
return HAL_I2C_Mem_Read(&hi2c1, 0x28 << 1, reg_addr, I2C_MEMADD_SIZE_8BIT, reg_data, length, 100) == HAL_OK ? 0 : 1;
}
int8_t bhy2_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, void *intf_ptr)
{
(void)intf_ptr;
return HAL_I2C_Mem_Write(&hi2c1, 0x28 << 1, reg_addr, I2C_MEMADD_SIZE_8BIT, (uint8_t *)reg_data, length, 100) == HAL_OK ? 0 : 1;
}
/* Delay in microseconds */
void bhy2_delay_us(uint32_t us, void *private_data)
{
(void)private_data;
micros_delay(us);
}
/* Microsecond timer helpers */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM3)
TIM7_ITs++;
}
uint64_t micros(void)
{
return (uint64_t)(__HAL_TIM_GET_COUNTER(&htim3) + 50000u * TIM7_ITs);
}
void micros_delay(uint64_t delay)
{
uint64_t timestamp = micros();
while (micros() < timestamp + delay);
}
/* 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 */
printf("FATAL ERROR: Error_Handler() called. System halted.\r\n");
HAL_Delay(100); /* Delay to allow UART to transmit the message */
__disable_irq();
while (1)
{
/* Blink the LED to give a visual indication of the error state */
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
HAL_Delay(100);
}
/* 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.
* @param file: pointer to the source file name
* @param 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 */
Below code is the bosch_imu.c file for the reference.
/*
MIT License
Copyright (c) 2024 VoltBro
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "bosch_imu.h"
#include "common.h"
#include "main.h"
#include "bhy2_parse.h"
#include <stdio.h>
enum bhy2_intf intf;
#define my_printf printf
#define WORK_BUFFER_SIZE 2048
// pointer to log output function
// extern custom_my_printf_prt my_printf;
void parse_euler(const struct bhy2_fifo_parse_data_info *callback_info, void *callback_ref);
void parse_accell(const struct bhy2_fifo_parse_data_info *callback_info, void *callback_ref);
void parse_gyro(const struct bhy2_fifo_parse_data_info *callback_info, void *callback_ref);
// Add prototypes if not included from a header:
int8_t bhy2_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr);
int8_t bhy2_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, void *intf_ptr);
void bhy2_delay_us(uint32_t us, void *private_data);
uint8_t product_id = 0;
uint16_t version = 0;
int8_t rslt;
struct bhy2_dev bhy2;
uint8_t work_buffer[WORK_BUFFER_SIZE];
uint8_t hintr_ctrl, hif_ctrl, boot_status;
uint8_t accuracy; // Accuracy is reported as a meta event. It is being printed alongside the data
void bosch_imu_setup(void)
{
intf = BHY2_I2C_INTERFACE;
rslt = bhy2_init(BHY2_I2C_INTERFACE, bhy2_i2c_read, bhy2_i2c_write, bhy2_delay_us, BHY2_RD_WR_LEN, NULL, &bhy2);
print_api_error(rslt, &bhy2);
rslt = bhy2_soft_reset(&bhy2);
print_api_error(rslt, &bhy2);
rslt = bhy2_get_product_id(&product_id, &bhy2);
print_api_error(rslt, &bhy2);
// Check for a valid product ID
if (product_id != BHY2_PRODUCT_ID)
{
my_printf("Product ID read %X. Expected %X\r\n", product_id, BHY2_PRODUCT_ID);
}
else
{
my_printf("BHIx60/BHAx60 found. Product ID read %X\r\n", product_id);
}
// Check the interrupt pin and FIFO configurations. Disable status and debug
hintr_ctrl = BHY2_ICTL_DISABLE_STATUS_FIFO | BHY2_ICTL_DISABLE_DEBUG;
rslt = bhy2_set_host_interrupt_ctrl(hintr_ctrl, &bhy2);
print_api_error(rslt, &bhy2);
rslt = bhy2_get_host_interrupt_ctrl(&hintr_ctrl, &bhy2);
print_api_error(rslt, &bhy2);
// Configure the host interface
hif_ctrl = 0;
rslt = bhy2_set_host_intf_ctrl(hif_ctrl, &bhy2);
print_api_error(rslt, &bhy2);
// Check if the sensor is ready to load firmware
rslt = bhy2_get_boot_status(&boot_status, &bhy2);
print_api_error(rslt, &bhy2);
if (boot_status & BHY2_BST_HOST_INTERFACE_READY)
{
upload_firmware(boot_status, &bhy2);
rslt = bhy2_get_kernel_version(&version, &bhy2);
print_api_error(rslt, &bhy2);
if ((rslt == BHY2_OK) && (version != 0))
{
my_printf("Boot successful. Kernel version %u.\r\n", version);
}
rslt = bhy2_register_fifo_parse_callback(BHY2_SYS_ID_META_EVENT, parse_meta_event, (void*)&accuracy, &bhy2);
print_api_error(rslt, &bhy2);
rslt = bhy2_register_fifo_parse_callback(BHY2_SYS_ID_META_EVENT_WU, parse_meta_event, (void*)&accuracy, &bhy2);
print_api_error(rslt, &bhy2);
rslt = bhy2_register_fifo_parse_callback(BHY2_SENSOR_ID_ORI, parse_euler, (void*)&accuracy, &bhy2);
print_api_error(rslt, &bhy2);
rslt = bhy2_register_fifo_parse_callback(BHY2_SENSOR_ID_ACC, parse_accell, (void*)&accuracy, &bhy2);
print_api_error(rslt, &bhy2);
rslt = bhy2_register_fifo_parse_callback(BHY2_SENSOR_ID_GYRO, parse_gyro, (void*)&accuracy, &bhy2);
print_api_error(rslt, &bhy2);
rslt = bhy2_get_and_process_fifo(work_buffer, WORK_BUFFER_SIZE, &bhy2);
print_api_error(rslt, &bhy2);
}
else
{
my_printf("Host interface not ready. Exiting\r\n");
Error_Handler();
}
rslt = bhy2_update_virtual_sensor_list(&bhy2);
print_api_error(rslt, &bhy2);
printInfo(&bhy2);
float sample_rate = 100.0; // Read out data measured at 100Hz
uint32_t report_latency_ms = 0; // Report immediately
rslt = bhy2_set_virt_sensor_cfg(BHY2_SENSOR_ID_ORI, sample_rate, report_latency_ms, &bhy2);
print_api_error(rslt, &bhy2);
my_printf("Enable %s at %.2fHz.\r\n", get_sensor_name(BHY2_SENSOR_ID_ORI), sample_rate);
rslt = bhy2_set_virt_sensor_cfg(BHY2_SENSOR_ID_ACC, sample_rate, report_latency_ms, &bhy2);
print_api_error(rslt, &bhy2);
my_printf("Enable %s at %.2fHz.\r\n", get_sensor_name(BHY2_SENSOR_ID_ACC), sample_rate);
rslt = bhy2_set_virt_sensor_cfg(BHY2_SENSOR_ID_GYRO, sample_rate, report_latency_ms, &bhy2);
print_api_error(rslt, &bhy2);
my_printf("Enable %s at %.2fHz.\r\n", get_sensor_name(BHY2_SENSOR_ID_GYRO), sample_rate);
}
void bosch_imu_run(void)
{
rslt = bhy2_get_and_process_fifo(work_buffer, WORK_BUFFER_SIZE, &bhy2);
print_api_error(rslt, &bhy2);
}
extern uint64_t last_result;
extern uint8_t sensor_reports;
float Heading = 0.0f;
float Pitch = 0.0f;
float Roll = 0.0f;
void parse_euler(const struct bhy2_fifo_parse_data_info *callback_info, void *callback_ref)
{
(void)callback_ref;
struct bhy2_data_orientation data;
uint32_t s, ns;
uint8_t *accuracy = (uint8_t*)callback_ref;
if (callback_info->data_size != 7) // Check for a valid payload size. Includes sensor ID
{
return;
}
bhy2_parse_orientation(callback_info->data_ptr, &data);
uint64_t timestamp = *callback_info->time_stamp; // Store the last timestamp
timestamp = timestamp * 15625; // Timestamp is now in nanoseconds
s = (uint32_t)(timestamp / UINT64_C(1000000000));
ns = (uint32_t)(timestamp - (s * UINT64_C(1000000000)));
if (accuracy)
{
Heading = data.heading * 360.0f / 32768.0f;
Pitch = data.pitch * 360.0f / 32768.0f;
Roll = data.roll * 360.0f / 32768.0f;
}
else
{
Heading = data.heading * 360.0f / 32768.0f;
Pitch = data.pitch * 360.0f / 32768.0f;
Roll = data.roll * 360.0f / 32768.0f;
}
sensor_reports |= 1UL << 0; // register euler sensor callback
last_result = micros();
}
float acc_x = 0.0f;
float acc_y = 0.0f;
float acc_z = 0.0f;
void parse_accell(const struct bhy2_fifo_parse_data_info *callback_info, void *callback_ref)
{
(void)callback_ref;
struct bhy2_data_xyz data;
uint32_t s, ns;
uint8_t *accuracy = (uint8_t*)callback_ref;
if (callback_info->data_size != 7) // Check for a valid payload size. Includes sensor ID
{
return;
}
bhy2_parse_xyz(callback_info->data_ptr, &data);
uint64_t timestamp = *callback_info->time_stamp; // Store the last timestamp
timestamp = timestamp * 15625; // Timestamp is now in nanoseconds
s = (uint32_t)(timestamp / UINT64_C(1000000000));
ns = (uint32_t)(timestamp - (s * UINT64_C(1000000000)));
if (accuracy)
{
acc_x = data.x * 1.0f / 4096.0f;
acc_y = data.y * 1.0f / 4096.0f;
acc_z = data.z * 1.0f / 4096.0f;
}
else
{
acc_x = data.x * 1.0f / 4096.0f;
acc_y = data.y * 1.0f / 4096.0f;
acc_z = data.z * 1.0f / 4096.0f;
}
sensor_reports |= 1UL << 1; // register accelerometer sensor callback
last_result = micros();
}
float gyr_x = 0.0f;
float gyr_y = 0.0f;
float gyr_z = 0.0f;
void parse_gyro(const struct bhy2_fifo_parse_data_info *callback_info, void *callback_ref)
{
(void)callback_ref;
struct bhy2_data_xyz data;
uint32_t s, ns;
uint8_t *accuracy = (uint8_t*)callback_ref;
if (callback_info->data_size != 7) // Check for a valid payload size. Includes sensor ID
{
return;
}
bhy2_parse_xyz(callback_info->data_ptr, &data);
uint64_t timestamp = *callback_info->time_stamp; // Store the last timestamp
timestamp = timestamp * 15625; // Timestamp is now in nanoseconds
s = (uint32_t)(timestamp / UINT64_C(1000000000));
ns = (uint32_t)(timestamp - (s * UINT64_C(1000000000)));
if (accuracy)
{
gyr_x = data.x * 2000.0f / 32768.0f;
gyr_y = data.y * 2000.0f / 32768.0f;
gyr_z = data.z * 2000.0f / 32768.0f;
}
else
{
gyr_x = data.x * 2000.0f / 32768.0f;
gyr_y = data.y * 2000.0f / 32768.0f;
gyr_z = data.z * 2000.0f / 32768.0f;
}
sensor_reports |= 1UL << 2; // register gyro sensor callback
last_result = micros();
}
Regards,
Aditya P