esp-idf-lib/bmi160
The BMI160 is a 16-bit inertial measurement unit from Bosch with a 3-axis accelerometer and a 3-axis gyroscope in one package.
Hardware
The chip answers on 0x68 when SDO is tied to ground and on 0x69 when
it is tied to VDDIO (BMI160_I2C_ADDRESS_GND and BMI160_I2C_ADDRESS_VDD).
The driver sets the port to 1 MHz, the fastest rate ESP-IDF supports; the chip
itself would go up to 2.44 MHz.
The interrupt pins INT1 and INT2 are configured by the driver as outputs
of the chip, but nothing is attached to them on the ESP side: which GPIO to use
and whether to poll it or wire it to an interrupt is the application’s decision.
The default example does the second with gpio_isr_handler_add().
Usage
#include <bmi160.h>
static bmi160_t dev;
ESP_ERROR_CHECK(bmi160_init(&dev, BMI160_I2C_ADDRESS_GND, 0,
CONFIG_EXAMPLE_SDA_GPIO, CONFIG_EXAMPLE_SCL_GPIO));
ESP_ERROR_CHECK(i2cdev_init());
bmi160_conf_t conf = {
.accRange = BMI160_ACC_RANGE_2G,
.accOdr = BMI160_ACC_ODR_200HZ,
.accAvg = BMI160_ACC_LP_AVG_2,
.accUs = BMI160_ACC_US_OFF,
.accMode = BMI160_PMU_ACC_NORMAL,
.gyrRange = BMI160_GYR_RANGE_125DPS,
.gyrOdr = BMI160_GYR_ODR_200HZ,
.gyrMode = BMI160_PMU_GYR_NORMAL,
};
ESP_ERROR_CHECK(bmi160_start(&dev, &conf));
bmi160_result_t result;
ESP_ERROR_CHECK(bmi160_read_data(&dev, &result));
bmi160_start() reads the chip ID, checks it against
BMI160_CHIP_ID_DEFAULT_VALUE, soft-resets the chip and only then writes the
configuration; a wrong ID returns ESP_FAIL. bmi160_init() itself never
talks to the bus, so i2cdev_init() must run before the first transfer, which
happens inside bmi160_self_test() or bmi160_start().
Configuration
bmi160_conf_t is validated before anything is written, an impossible
combination returns ESP_FAIL and the reason goes to the log:
accelerometer ranges are ±2, ±4, ±8 and ±16 g, gyroscope ranges ±125, ±250, ±500, ±1000 and ±2000 dps;
in normal mode the accelerometer ODR must be at least 12.5 Hz;
in low-power mode the averaging window bounds the ODR: 400 Hz allows 1 or 2 samples, 200 Hz up to 4, 100 Hz up to 8, 50 Hz up to 16, 25 Hz up to 32, 12.5 Hz up to 64, and 6.25 Hz and slower up to 128;
the gyroscope has no low-power mode, only suspend, normal and fast start-up, and its ODR starts at 25 Hz;
a sensor set to
BMI160_PMU_*_SUSPENDis not started at all.
The ranges and ODRs can also be changed later with bmi160_set_acc_range(),
bmi160_set_gyr_range(), bmi160_set_acc_conf() and
bmi160_set_gyr_odr(); they return ESP_FAIL for a value the chip cannot
take.
Reading
bmi160_read_data() reads 12 bytes in one transaction, starting at gyroscope
X, so both sensors always belong to the same sample. Each axis is a signed
16-bit value scaled by the resolution of the configured range:
acceleration = raw * range_g / 32768 gyroscope = raw * range_dps / 32768
At ±2 g one count is 61 µg, at ±125 dps one count is 0.0038 dps. The bias stored in the descriptor is subtracted from both.
Bias calibration
bmi160_calibrate() takes 64 readings about 20 ms apart (the delay is quantised
to the FreeRTOS tick), averages them and keeps the average as the bias of both
sensors. Every later reading has it subtracted. Calling it again is safe: the
existing bias is dropped before the averaging, so a second run measures the
sensor, not its own leftovers.
The accelerometer average includes gravity: a sensor lying still on a table has roughly 1 g on one axis, and after the calibration the driver reports zero on all three axes. Calibrate this way when you want “no movement and no gravity”, not when you need the gravity vector. The gyroscope part is a plain zero-rate offset.
Self test
bmi160_self_test() resets the chip, runs the accelerometer self test in both
directions and compares the results; the datasheet requires more than 2 g of
difference on every axis (2 g counts as a pass). Then it resets the chip again and
checks the gyroscope self-test status bit. It returns ESP_OK only when both
sensors pass, and it leaves the chip reset, so bmi160_start() must follow it.
Interrupts
bmi160_enable_int_new_data(), bmi160_enable_int_step() and
bmi160_enable_int_tap() take a bmi160_int_out_conf_t: which pin
(INT1 or INT2), push-pull or open drain, active high or low. They
configure the chip side only; the pin still has to be read or attached to a GPIO
interrupt by the application.
Step counter
bmi160_enable_step_counter() switches the step counter on in normal,
sensitive or robust mode, bmi160_read_step_counter() returns the count and
bmi160_reset_step_counter() sends the reset command.
bmi160_enable_int_step() adds an interrupt on every step. The counter works in
low-power mode too, but the datasheet recommends switching the accelerometer to
normal mode before reading to get the current value; the example does exactly
that.
Tap detection
bmi160_enable_tap_detection() takes a bmi160_tap_conf_t: single or double
tap, the threshold in g (0.032 g … 4 g), the duration of the tap pulse, the quiet
time after it and the shock time. bmi160_read_tap_orient() returns the raw
orientation byte the chip latches for the detected tap; interpreting the bit
field is up to the application.
Limitations
Only I2C is implemented; the chip’s SPI mode and the magnetometer connected to its auxiliary interface are not supported.
There is no FIFO support, every reading is a separate transaction.
The bias is a single constant per axis and does not depend on temperature or on the range, so changing the range invalidates a calibration done at another one.
The mutex protects single transactions, not the sequences:
bmi160_start()andbmi160_self_test()take it once for the whole sequence andbmi160_calibrate()holds it for all 64 readings, but concurrent calls to those three from two tasks should still be avoided.bmi160_free()releases the mutex and the descriptor only; the I2C bus stays untili2cdev_done().
Defines
- BMI160_I2C_ADDRESS_GND
I2C address of BMI160 if SDO is pulled to GND.
- BMI160_I2C_ADDRESS_VDD
I2C address of BMI160 if SDO is pulled to VDDIO.
Enums
- enum bmi160_acc_range_t
enum for the accelerometer range
Values:
- enumerator BMI160_ACC_RANGE_2G
+-2g
- enumerator BMI160_ACC_RANGE_4G
+-4g
- enumerator BMI160_ACC_RANGE_8G
+-8g
- enumerator BMI160_ACC_RANGE_16G
+-16g
- enum bmi160_gyr_range_t
enum for the gyroscope range
Values:
- enumerator BMI160_GYR_RANGE_2000DPS
+-2000dps
- enumerator BMI160_GYR_RANGE_1000DPS
+-1000dps
- enumerator BMI160_GYR_RANGE_500DPS
+-500dps
- enumerator BMI160_GYR_RANGE_250DPS
+-250dps
- enumerator BMI160_GYR_RANGE_125DPS
+-125dps
- enum bmi160_acc_odr_t
enum for the accelerometer ODR
Values:
- enumerator BMI160_ACC_ODR_0_78HZ
0.78Hz
- enumerator BMI160_ACC_ODR_1_56HZ
1.56Hz
- enumerator BMI160_ACC_ODR_3_12HZ
3.12Hz
- enumerator BMI160_ACC_ODR_6_25HZ
6.25Hz
- enumerator BMI160_ACC_ODR_12_5HZ
12.5Hz
- enumerator BMI160_ACC_ODR_25HZ
25Hz
- enumerator BMI160_ACC_ODR_50HZ
50Hz
- enumerator BMI160_ACC_ODR_100HZ
100Hz
- enumerator BMI160_ACC_ODR_200HZ
200Hz
- enumerator BMI160_ACC_ODR_400HZ
400Hz
- enumerator BMI160_ACC_ODR_800HZ
800Hz
- enumerator BMI160_ACC_ODR_1600HZ
1600Hz
- enum bmi160_gyr_odr_t
enum for the gyroscope ODR
Values:
- enumerator BMI160_GYR_ODR_25HZ
25Hz
- enumerator BMI160_GYR_ODR_50HZ
50Hz
- enumerator BMI160_GYR_ODR_100HZ
100Hz
- enumerator BMI160_GYR_ODR_200HZ
200Hz
- enumerator BMI160_GYR_ODR_400HZ
400Hz
- enumerator BMI160_GYR_ODR_800HZ
800Hz
- enumerator BMI160_GYR_ODR_1600HZ
1600Hz
- enum bmi160_int_pin_t
enum for int pin
Values:
- enumerator BMI160_PIN_INT1
INT1.
- enumerator BMI160_PIN_INT2
INT2.
- enum bmi160_int_enable_t
enum for int enable
Values:
- enumerator BMI160_INT_DISABLE
Disable interrupt.
- enumerator BMI160_INT_ENABLE
Enable interrupt.
- enum bmi160_int_od_t
enum for int od
Values:
- enumerator BMI160_INT_PUSH_PULL
Push-pull.
- enumerator BMI160_INT_OPEN_DRAIN
Open-drain.
- enum bmi160_int_level_t
enum for int level
Values:
- enumerator BMI160_INT_ACTIVE_LOW
Active low.
- enumerator BMI160_INT_ACTIVE_HIGH
Active high.
- enum bmi160_step_counter_mode_t
step counter mode
Values:
- enumerator BMI160_STEP_COUNTER_NORMAL
Normal mode.
- enumerator BMI160_STEP_COUNTER_SENSITIVE
Sensitive mode.
- enumerator BMI160_STEP_COUNTER_ROBUST
Robust mode.
- enum bmi160_pmu_acc_mode_t
Accelerometer PMU mode.
Values:
- enumerator BMI160_PMU_ACC_SUSPEND
Suspend mode.
- enumerator BMI160_PMU_ACC_NORMAL
Normal mode.
- enumerator BMI160_PMU_ACC_LOW_POWER
Low power mode.
- enum bmi160_pmu_gyr_mode_t
Gyroscope PMU mode.
Values:
- enumerator BMI160_PMU_GYR_SUSPEND
Suspend mode.
- enumerator BMI160_PMU_GYR_NORMAL
Normal mode.
- enumerator BMI160_PMU_GYR_FAST_STARTUP
Fast startup mode.
- enum bmi160_acc_lp_avg_t
averaging configuration in low power mode for accelerometer
Note
For low power mode defines averaging, for normal mode defines filter bandwidth
Values:
- enumerator BMI160_ACC_LP_AVG_1
1 sample
- enumerator BMI160_ACC_LP_AVG_2
2 samples
- enumerator BMI160_ACC_LP_AVG_4
4 samples
- enumerator BMI160_ACC_LP_AVG_8
8 samples
- enumerator BMI160_ACC_LP_AVG_16
16 samples
- enumerator BMI160_ACC_LP_AVG_32
32 samples
- enumerator BMI160_ACC_LP_AVG_64
64 samples
- enumerator BMI160_ACC_LP_AVG_128
128 samples
- enum bmi160_acc_us_t
bmi160 accelerometer undersampling configuration
Note
This is only applicable for low power mode
Values:
- enumerator BMI160_ACC_US_ON
Enable undersampling.
- enumerator BMI160_ACC_US_OFF
Disable undersampling.
- enum bmi160_tap_quiet_t
tap configuration quiet
Values:
- enumerator BMI160_TAP_QUIET_30MS
30ms
- enumerator BMI160_TAP_QUIET_20MS
20ms
- enum bmi160_tap_mode_t
tap mode
Values:
- enumerator BMI160_TAP_MODE_SINGLE
Single tap.
- enumerator BMI160_TAP_MODE_DOUBLE
Double tap.
- enum bmi160_tap_shock_t
tap configuration shock
Values:
- enumerator BMI160_TAP_SHOCK_50MS
50ms
- enumerator BMI160_TAP_SHOCK_75MS
75ms
- enum bmi160_tap_dur_t
tap configuration duration
Values:
- enumerator BMI160_TAP_DUR_50MS
50ms
- enumerator BMI160_TAP_DUR_100MS
100ms
- enumerator BMI160_TAP_DUR_150MS
- enumerator BMI160_TAP_DUR_200MS
- enumerator BMI160_TAP_DUR_250MS
- enumerator BMI160_TAP_DUR_375MS
- enumerator BMI160_TAP_DUR_500MS
- enumerator BMI160_TAP_DUR_700MS
- enum bmi160_tap_th_t
tap threshold
Values:
- enumerator BMI160_TAP_TH_0_032G
0.032g
- enumerator BMI160_TAP_TH_0_0625G
0.0625g
- enumerator BMI160_TAP_TH_0_125G
0.125g
- enumerator BMI160_TAP_TH_0_25G
0.25g
- enumerator BMI160_TAP_TH_0_5G
0.5g
- enumerator BMI160_TAP_TH_1G
1g
- enumerator BMI160_TAP_TH_2G
2g
- enumerator BMI160_TAP_TH_4G
4g
Functions
- esp_err_t bmi160_init(bmi160_t *dev, uint8_t addr, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)
Initialize the BMI160.
- Parameters:
dev – Pointer to the device descriptor
addr – I2C address of the device
port – I2C port number
sda_gpio – SDA GPIO number
scl_gpio – SCL GPIO number
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_read_reg(bmi160_t *dev, uint8_t reg, uint8_t *val)
Read a register from the BMI160.
- Parameters:
dev – Pointer to the device descriptor
reg – Register address
val – Pointer to store the value
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_read_reg_array(bmi160_t *dev, uint8_t reg, uint8_t *val, uint8_t num)
Read an array of registers from the BMI160.
- Parameters:
dev – Pointer to the device descriptor
reg – Register address
val – Pointer to store the values
num – Number of registers to read
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_free(bmi160_t *dev)
free descriptor
- Parameters:
dev – Pointer to the device descriptor
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_write_reg(bmi160_t *dev, uint8_t reg, uint8_t val)
Write a register to the BMI160.
- Parameters:
dev – Pointer to the device descriptor
reg – Register address
val – Value to write
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_calibrate(bmi160_t *dev)
Calibrate the BMI160 to calculate bias.
Note
bmi160_start must be called before calling this function. After calling this function output data will be offset by the bias values.
- Parameters:
dev – Pointer to the device descriptor
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_start(bmi160_t *dev, const bmi160_conf_t *const conf)
start the BMI160
- Parameters:
dev – Pointer to the device descriptor
conf – Pointer to the configuration structure
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_read_data(bmi160_t *dev, bmi160_result_t *result)
Read data from the BMI160.
- Parameters:
dev – Pointer to the device descriptor
result – Pointer to store the result
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_set_acc_range(bmi160_t *dev, bmi160_acc_range_t range)
set acc range
- Parameters:
dev – Pointer to the device descriptor
range – Accelerometer range
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_set_gyr_range(bmi160_t *dev, bmi160_gyr_range_t range)
set gyro range
- Parameters:
dev – Pointer to the device descriptor
range – Gyroscope range
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_set_acc_conf(bmi160_t *dev, bmi160_acc_odr_t odr, bmi160_acc_lp_avg_t avg, bmi160_acc_us_t acc_us)
set acc configuration
- Parameters:
dev – Pointer to the device descriptor
odr – Accelerometer ODR
avg – Averaging configuration
acc_us – Accelerometer undersampling configuration
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_set_gyr_odr(bmi160_t *dev, bmi160_gyr_odr_t odr)
set gyro odr
- Parameters:
dev – Pointer to the device descriptor
odr – Gyroscope ODR
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_self_test(bmi160_t *dev)
self test for the BMI160
- Parameters:
dev – Pointer to the device descriptor
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_enable_int_new_data(bmi160_t *dev, const bmi160_int_out_conf_t *const intOutConf)
Enable interrupt for the BMI160.
- Parameters:
dev – Pointer to the device descriptor
intOutConf – Interrupt out configuration
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_enable_step_counter(bmi160_t *dev, bmi160_step_counter_mode_t mode)
configure step counter
- Parameters:
dev – Pointer to the device descriptor
mode – Step counter mode
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_read_step_counter(bmi160_t *dev, uint16_t *stepCounter)
read step counter
- Parameters:
dev – Pointer to the device descriptor
stepCounter – Pointer to store the step counter value
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_reset_step_counter(bmi160_t *dev)
reset step counter
- Parameters:
dev – Pointer to the device descriptor
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_enable_int_step(bmi160_t *dev, const bmi160_int_out_conf_t *const intOutConf)
Enable interrupt for the BMI160 for new step.
- Parameters:
dev – Pointer to the device descriptor
intOutConf – Interrupt out configuration
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_switch_mode(bmi160_t *dev, bmi160_pmu_acc_mode_t accMode, bmi160_pmu_gyr_mode_t gyrMode)
switch mode for the BMI160
- esp_err_t bmi160_enable_tap_detection(bmi160_t *dev, const bmi160_tap_conf_t *const tapConf)
configure tap detection
- Parameters:
dev – Pointer to the device descriptor
tapConf – Pointer to the configuration structure
- Returns:
esp_err_t ESP_OK if success
- esp_err_t bmi160_enable_int_tap(bmi160_t *dev, const bmi160_int_out_conf_t *const intOutConf)
Enable interrupt for the BMI160 for tap detection.
- Parameters:
dev – Pointer to the device descriptor
intOutConf – Interrupt out configuration
- Returns:
esp_err_t ESP_OK if success
- struct bmi160_conf_t
#include <bmi160.h>bmi160 configuration structure
Public Members
- bmi160_acc_range_t accRange
Accelerometer range.
- bmi160_acc_odr_t accOdr
Accelerometer ODR.
- bmi160_pmu_acc_mode_t accMode
Accelerometer PMU mode.
- bmi160_acc_lp_avg_t accAvg
Accelerometer averaging configuration for low power mode.
- bmi160_acc_us_t accUs
Accelerometer undersampling configuration.
- bmi160_gyr_range_t gyrRange
Gyroscope range.
- bmi160_gyr_odr_t gyrOdr
Gyroscope ODR.
- bmi160_pmu_gyr_mode_t gyrMode
Gyroscope PMU mode.
- struct bmi160_result_t
#include <bmi160.h>result structure for the BMI160
- struct bmi160_int_out_conf_t
#include <bmi160.h>int out configuration
Public Members
- bmi160_int_pin_t intPin
Interrupt pin.
- bmi160_int_enable_t intEnable
Enable interrupt.
- bmi160_int_od_t intOd
Open-drain.
- bmi160_int_level_t intLevel
Active high.
- struct bmi160_tap_conf_t
#include <bmi160.h>tap configuration
Public Members
- bmi160_tap_quiet_t tapQuiet
Tap quiet.
- bmi160_tap_shock_t tapShock
Tap shock.
- bmi160_tap_dur_t tapDur
Tap duration.
- bmi160_tap_th_t tapTh
Tap threshold.
- bmi160_tap_mode_t tapMode
Tap mode.
- struct bmi160_t
#include <bmi160.h>Device descriptor.
Public Members
- i2c_dev_t i2c_dev
I2C device descriptor.
- float aBias[3]
Accelerometer bias.
- float gBias[3]
Gyroscope bias.
- float aRes
Accelerometer resolution.
- float gRes
Gyroscope resolution.
- bmi160_acc_range_t accRange
Accelerometer range.
- bmi160_gyr_range_t gyrRange
Gyroscope range.
- bmi160_acc_odr_t accOdr
Accelerometer ODR.
- uint8_t accConf
Accelerometer configuration.
- bmi160_gyr_odr_t gyrOdr
Gyroscope ODR.
- gpio_num_t intPin
Interrupt pin.
- bmi160_tap_mode_t tapMode
Tap mode.
- bmi160_pmu_acc_mode_t accMode
Accelerometer mode.
- bmi160_pmu_gyr_mode_t gyrMode
Gyroscope mode.
- void (*callback)()
Interrupt callback.