BMI270 Sensor API
repository·master·Indexed 40 days ago
https://github.com/boschsensortec/bmi270_sensorapiSoftware interface for the BMI270 Inertial Measurement Unit (IMU), providing features for motion detection, activity recognition, and gesture sensing. The API includes a Host Interface for I2C/SPI configuration and a dedicated OIS (Optical Image Stabilization) Interface for SPI-based data reading. Supported modes include BMI270 base, Context, Legacy, and Maximum FIFO with a 6kB buffer.
What's inside BMI270 Sensor API
- The BMI270 Sensor API provides software interfaces for the BMI270, a small, low-power, low-noise Inertial Measurement Unit (IMU). It is designed for mobile applications requiring highly accurate, real-time sensor data, such as augmented reality (AR) or indoor navigation.
Understand the difference between Host and OIS interfaces
masterThe BMI2 sensor API distinguishes between two communication paths:
- Host Interface: Used for initializing and configuring the BMI2 sensor. It supports I2C (default) and SPI. APIs for this interface are defined in
bmi2.c. - OIS Interface: Used specifically for OIS configurations and reading OIS data. This interface uses SPI. APIs for this interface are defined in
bmi2_ois.c.
Important: You must use
bmi2.cfunctions for sensor setup andbmi2_ois.cfunctions for OIS-specific data and configuration.- Host Interface: Used for initializing and configuring the BMI2 sensor. It supports I2C (default) and SPI. APIs for this interface are defined in
Available BMI270 features and application modes
masterThe BMI270 API supports several functional modes depending on the specific implementation or hardware configuration used:
BMI270 (base)
Standard features include:
- Motion detection: Any motion, No motion, Significant motion.
- Pedometer: Wrist-worn Step counter and Step detector.
- Activity recognition: Still, Walking, Running.
- Wrist gestures: Push arm down, Pivot up, Wrist shake jiggle, Flick in, Flick out.
- Wrist wear wake up.
BMI270 Context
Focused features include:
- Pedometer: Step counter and Step detector.
- Activity recognition: Still, Walking, Running.
BMI270 Legacy
Includes older or specific detection capabilities:
- Motion detection: Any motion, No motion, Significant motion.
- Orientation detection: Advanced Portrait-Landscape.
- Fall/Impact detection: High-G, Low-G (Freefall), Flat detector.
- Tap detection: Single, Double, Triple taps.
- Smartphone Pedometer: Step counter and Step detector.
- Activity recognition: Still, Walking, Running.
BMI270 Maximum FIFO
- Supports a 6kB FIFO buffer.
Integrate the BMI2 OIS Sensor API
masterTo use the OIS (Optical Image Stabilization) interface, you must include the following files in your project:
bmi2.c/bmi2.h(for BMI2 host interface APIs)bmi2_ois.c/bmi2_ois.h(for OIS interface APIs)bmi2_defs.h- Required variant files
In your source code, you must include both the variant header (e.g.,
bmi261.h) andbmi2_ois.hto access both the standard BMI2 APIs and the OIS-specific APIs.#include "bmi261.h" #include "bmi2_ois.h"Get accelerometer and gyroscope data through the OIS interface
masterTo read OIS data, follow this workflow:
- Initialize BMI2 via the host interface using
bmi2_init(). - Enable sensors (Accel/Gyro) via the host interface using
bmi2_sensor_enable(). - Configure OIS Range via the host interface using
bmi2_get_sensor_config()andbmi2_set_sensor_config(). - Enable OIS interface via the host interface using
bmi2_set_ois_interface(BMI2_ENABLE, &dev). - Configure OIS via the OIS interface using
bmi2_ois_get_config()andbmi2_ois_set_config(&ois_dev). - Read Data via the OIS interface using
bmi2_ois_read_data().
Data is accessed through the
ois_dev.acc_dataandois_dev.gyr_datastructures.int8_t rslt; /* Array to enable sensor through host interface */ uint8_t sens_list[2] = {BMI2_ACCEL, BMI2_GYRO}; /* Array to enable sensor through OIS interface */ uint8_t sens_sel[2] = {BMI2_OIS_ACCEL, BMI2_OIS_GYRO}; /* Initialize the configuration structure */ struct bmi2_sens_config sens_cfg = {0}; /* Initialize BMI2 */ rslt = bmi2_init(&dev); if (rslt != BMI2_OK) { printf("Error: %d\n", rslt); return; } /* Enable accelerometer and gyroscope through host interface */ rslt = bmi2_sensor_enable(sens_list, 2, &dev); if (rslt != BMI2_OK) { printf("Error: %d\n", rslt); return; } /* Setting of OIS Range is done through host interface */ sens_cfg.type = BMI2_GYRO; rslt = bmi2_get_sensor_config(&sens_cfg, 1, &dev); if (rslt != BMI2_OK) { printf("Error: %d\n", rslt); return; } sens_cfg.cfg.gyr.ois_range = BMI2_GYR_OIS_2000; rslt = bmi2_set_sensor_config(&sens_cfg, 1, &dev); if (rslt != BMI2_OK) { printf("Error: %d\n", rslt); return; } /* Enable OIS through host interface */ rslt = bmi2_set_ois_interface(BMI2_ENABLE, &dev); if (rslt != BMI2_OK) { printf("Error: %d\n", rslt); return; } /* Disable Advance Power Save Mode through host interface */ rslt = bmi2_set_adv_power_save(BMI2_DISABLE, &dev); if (rslt != BMI2_OK) { printf("Error: %d\n", rslt); return; } /* Get configurations for OIS through OIS interface */ rslt = bmi2_ois_get_config(&ois_dev); if (rslt != BMI2_OK) { printf("Error: %d\n", rslt); return; } /* Enable accelerometer and gyroscope for reading OIS data */ ois_dev.acc_en = BMI2_ENABLE; ois_dev.gyr_en = BMI2_ENABLE; /* Set configurations for OIS through OIS interface */ rslt = bmi2_ois_set_config(&ois_dev); if (rslt == BMI2_OK) { /* Get OIS accelerometer and gyroscope data through OIS interface */ rslt = bmi2_ois_read_data(sens_sel, 2, &ois_dev); if (rslt == BMI2_OK) { /* Print accelerometer data */ printf("OIS Accel x-axis = %d\t", ois_dev.acc_data.x); printf("OIS Accel y-axis= %d\t", ois_dev.acc_data.y); printf("OIS Accel z-axis = %d\r\n", ois_dev.acc_data.z); /* Print gyroscope data */ printf("OIS Gyro x-axis = %d\t", ois_dev.gyr_data.x); printf("OIS Gyro y-axis= %d\t", ois_dev.gyr_data.y); printf("OIS Gyro z-axis = %d\r\n", ois_dev.gyr_data.z); } } if (rslt != BMI2_OK) { printf("Error code: %d\n", rslt); return; } /* Enable Advance Power Save Mode through host interface */ rslt = bmi2_set_adv_power_save(BMI2_ENABLE, &dev);- Initialize BMI2 via the host interface using
Configure the OIS interface using `bmi2_ois_dev`
masterTo use the OIS interface, you must create and populate an instance of the
struct bmi2_ois_dev. This structure requires the following parameters to be provided by the user:Parameter Details intf_ptrDevice address reference of the SPI interface (contains chip selection info for SPI CS pin) ois_readUser-provided function for reading through the SPI interface ois_writeUser-provided function for writing through the SPI interface ois_delay_usDelay in microseconds acc_enFlag for enabling the accelerometer gyr_enFlag for enabling the gyroscope Note:
acc_enandgyr_enare used to enable the sensors for OIS data reading.int8_t rslt = 0; struct bmi2_ois_dev ois_dev = { .intf_ptr = intf_ptr, // contains SPI CS pin info .ois_read = user_spi_reg_read, .ois_write = user_spi_reg_write, .ois_delay_us = user_delay_us };