BMI160 Motion Sensor (sensor-motion-bmi160)
Motion Sensor

General Description

Basic set of APIs for interacting with the BMI160 motion sensor.

This provides basic initialization and access to to the basic accelerometer & gyroscope data. It also provides access to the base BMI160 driver for full control. For more information about the motion sensor, see: https://github.com/BoschSensortec/BMI160_driver

Note
Currently, this library only supports being used for a single instance of this device.
BMI160 support requires delays. If the RTOS_AWARE component is set or CY_RTOS_AWARE is defined, the HAL driver will defer to the RTOS for delays. Because of this, it is not safe to call any functions other than mtb_bmi160_init_i2c until after the RTOS scheduler has started.
There is a known issue with the BMI160 endianness detection. Any code referencing the structures defined in the BMI160 driver should have this header file, mtb_bmi160.h, first in any includes.

Code snippets

Snippet 1: Simple initialization with I2C.

The following snippet initializes an I2C instance and the BMI160, then reads from the BMI160.

/* Initialize I2C and BMI160 */
result = cyhal_i2c_init(&i2c_instance, PIN_IMU_I2C_SDA, PIN_IMU_I2C_SCL, NULL);
result = mtb_bmi160_init_i2c(&imu, &i2c_instance, MTB_BMI160_DEFAULT_ADDRESS);
/* Read data from BMI160 */
result = mtb_bmi160_read(&imu, &imu_data);
cy_rslt_t mtb_bmi160_read(mtb_bmi160_t *obj, mtb_bmi160_data_t *sensor_data)
Reads the current accelerometer & gyroscope data from the motion sensor.
Definition: mtb_bmi160.c:313
cy_rslt_t mtb_bmi160_init_i2c(mtb_bmi160_t *obj, cyhal_i2c_t *inst, mtb_bmi160_address_t address)
Initialize the IMU for I2C communication.
Definition: mtb_bmi160.c:228

Snippet 2: BMI160 interrupt configuration.

The following snippet demonstrates how to configure a BMI160 interrupt.

/* Populate BMI160 interrupt configuration structure and configure interrupt */
struct bmi160_int_settg intsettings;
cy_rslt_t result;
intsettings.int_channel = BMI160_INT_CHANNEL_1;
intsettings.int_type = BMI160_ACC_ANY_MOTION_INT;
intsettings.int_pin_settg.output_en = BMI160_ENABLE;
intsettings.int_pin_settg.output_mode = 0;
intsettings.int_pin_settg.output_type = 0;
intsettings.int_pin_settg.edge_ctrl = 1;
intsettings.int_pin_settg.input_en = 0;
intsettings.int_pin_settg.latch_dur = 0;
intsettings.int_type_cfg.acc_any_motion_int.anymotion_en = 1;
intsettings.int_type_cfg.acc_any_motion_int.anymotion_x = 1;
intsettings.int_type_cfg.acc_any_motion_int.anymotion_y = 1;
intsettings.int_type_cfg.acc_any_motion_int.anymotion_z = 1;
intsettings.int_type_cfg.acc_any_motion_int.anymotion_dur = 0;
intsettings.int_type_cfg.acc_any_motion_int.anymotion_thr = 20;
intsettings.fifo_full_int_en = 0;
intsettings.fifo_wtm_int_en = 0;
result = mtb_bmi160_config_int(obj, &intsettings, PIN_IMU_INT_1, CYHAL_ISR_PRIORITY_DEFAULT,
CYHAL_GPIO_IRQ_FALL, (cyhal_gpio_event_callback_t)&imucallback,
NULL);
cy_rslt_t mtb_bmi160_config_int(mtb_bmi160_t *obj, struct bmi160_int_settg *intsettings, cyhal_gpio_t pin, uint8_t intr_priority, cyhal_gpio_event_t event, cyhal_gpio_event_callback_t callback, void *callback_arg)
Configure a GPIO pin as an interrupt for the BMI160.
Definition: mtb_bmi160.c:358

Data Structures

struct  mtb_bmi160_t
 Structure holding the IMU instance specific information. More...
 
struct  mtb_bmi160_data_t
 Structure holding the accelerometer and gyroscope data read from the device. More...
 

Macros

#define MTB_BMI160_RSLT_ERR_INSUFFICIENT_INT_PINS    (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_BOARD_HARDWARE_BMI160, 0x100))
 An attempt was made to configure too many gpio pins as interrupts.
 

Enumerations

enum  mtb_bmi160_address_t {
  MTB_BMI160_DEFAULT_ADDRESS = BMI160_I2C_ADDR ,
  MTB_BMI160_SECONDARY_ADDRESS = BMI160_AUX_BMM150_I2C_ADDR
}
 Enumeration used for selecting I2C address.
 

Functions

cy_rslt_t mtb_bmi160_init_i2c (mtb_bmi160_t *obj, cyhal_i2c_t *inst, mtb_bmi160_address_t address)
 Initialize the IMU for I2C communication. More...
 
cy_rslt_t mtb_bmi160_init_spi (mtb_bmi160_t *obj, cyhal_spi_t *inst, cyhal_gpio_t spi_ss)
 Initialize the sensor for SPI communication. More...
 
cy_rslt_t mtb_bmi160_config_default (mtb_bmi160_t *obj)
 Configure the motion sensor to a default mode with both accelerometer & gyroscope enabled with a nominal output data rate. More...
 
cy_rslt_t mtb_bmi160_read (mtb_bmi160_t *obj, mtb_bmi160_data_t *sensor_data)
 Reads the current accelerometer & gyroscope data from the motion sensor. More...
 
struct bmi160_dev * mtb_bmi160_get (mtb_bmi160_t *obj)
 Gets access to the base motion sensor data. More...
 
cy_rslt_t mtb_bmi160_selftest (mtb_bmi160_t *obj)
 Performs both accelerometer and gyro self tests. More...
 
cy_rslt_t mtb_bmi160_config_int (mtb_bmi160_t *obj, struct bmi160_int_settg *intsettings, cyhal_gpio_t pin, uint8_t intr_priority, cyhal_gpio_event_t event, cyhal_gpio_event_callback_t callback, void *callback_arg)
 Configure a GPIO pin as an interrupt for the BMI160. More...
 
void mtb_bmi160_free (mtb_bmi160_t *obj)
 Frees up any resources allocated by the motion_sensor as part of mtb_bmi160_init_i2c(). More...
 

Data Structure Documentation

◆ mtb_bmi160_t

struct mtb_bmi160_t
Data Fields
struct bmi160_dev sensor
_mtb_bmi160_interrupt_pin_t intpin1
_mtb_bmi160_interrupt_pin_t intpin2

◆ mtb_bmi160_data_t

struct mtb_bmi160_data_t
Data Fields
struct bmi160_sensor_data accel Accelerometer data.
struct bmi160_sensor_data gyro Gyroscope data.

Function Documentation

◆ mtb_bmi160_init_i2c()

cy_rslt_t mtb_bmi160_init_i2c ( mtb_bmi160_t obj,
cyhal_i2c_t *  inst,
mtb_bmi160_address_t  address 
)

Initialize the IMU for I2C communication.

Then applies the default configuration settings for both the accelerometer & gyroscope. Known maximum I2C frequency of 1MHz; refer to manufacturer's datasheet for confirmation. See: mtb_bmi160_config_default()

Parameters
[in]objPointer to a BMI160 object. The caller must allocate the memory for this object but the init function will initialize its contents.
[in]instI2C instance to use for communicating with the BMI160 sensor.
[in]addressBMI160 I2C address, set by hardware implementation.
Returns
CY_RSLT_SUCCESS if properly initialized, else an error indicating what went wrong.

◆ mtb_bmi160_init_spi()

cy_rslt_t mtb_bmi160_init_spi ( mtb_bmi160_t obj,
cyhal_spi_t *  inst,
cyhal_gpio_t  spi_ss 
)

Initialize the sensor for SPI communication.

Then applies the default configuration settings for the accelerometer & gyroscope. NOTE: The SPI slave select pin (spi_ss) is expected to be controlled by this driver, not the SPI block itself. This allows the driver to issue multiple read/write requests back to back. See: mtb_bmi160_config_default()

Parameters
[in]objPointer to a BMI160 object. The caller must allocate the memory for this object but the init function will initialize its contents.
[in]instSPI instance to use for communicating with the BMI160 sensor.
[in]spi_ssSPI slave select pin to use for communicating with the BMI160 sensor.
Returns
CY_RSLT_SUCCESS if properly initialized, else an error indicating what went wrong.

◆ mtb_bmi160_config_default()

cy_rslt_t mtb_bmi160_config_default ( mtb_bmi160_t obj)

Configure the motion sensor to a default mode with both accelerometer & gyroscope enabled with a nominal output data rate.

The default values used are from the example in the BMI160 driver repository, see https://github.com/BoschSensortec/BMI160_driver

Parameters
[in]objPointer to a BMI160 object.
Returns
CY_RSLT_SUCCESS if properly initialized, else an error indicating what went wrong.

◆ mtb_bmi160_read()

cy_rslt_t mtb_bmi160_read ( mtb_bmi160_t obj,
mtb_bmi160_data_t sensor_data 
)

Reads the current accelerometer & gyroscope data from the motion sensor.

Parameters
[in]objPointer to a BMI160 object.
[out]sensor_dataThe accelerometer & gyroscope data read from the motion sensor
Returns
CY_RSLT_SUCCESS if properly initialized, else an error indicating what went wrong.

◆ mtb_bmi160_get()

struct bmi160_dev * mtb_bmi160_get ( mtb_bmi160_t obj)

Gets access to the base motion sensor data.

This allows for direct manipulation of the sensor for any desired behavior. See https://github.com/BoschSensortec/BMI160_driver for more details on the sensor.

Parameters
[in]objPointer to a BMI160 object.
Returns
pointer to the BMI160 configuration structure.

◆ mtb_bmi160_selftest()

cy_rslt_t mtb_bmi160_selftest ( mtb_bmi160_t obj)

Performs both accelerometer and gyro self tests.

Note these tests cause a soft reset of the device and device should be reconfigured after a test. See https://github.com/BoschSensortec/BMI160_driver for more details.

Parameters
[in]objPointer to a BMI160 object.
Returns
CY_RSLT_SUCCESS if self tests pass, else an error indicating what went wrong.

◆ mtb_bmi160_config_int()

cy_rslt_t mtb_bmi160_config_int ( mtb_bmi160_t obj,
struct bmi160_int_settg *  intsettings,
cyhal_gpio_t  pin,
uint8_t  intr_priority,
cyhal_gpio_event_t  event,
cyhal_gpio_event_callback_t  callback,
void *  callback_arg 
)

Configure a GPIO pin as an interrupt for the BMI160.

This configures the pin as an interrupt, and calls the BMI160 interrupt configuration API with the application supplied settings structure. See https://github.com/BoschSensortec/BMI160_driver for more details.

Parameters
[in]objPointer to a BMI160 object.
[in]intsettingsPointer to a BMI160 interrupt settings structure.
[in]pinWhich pin to configure as interrupt
[in]intr_priorityThe priority for NVIC interrupt events
[in]eventThe type of interrupt event
[in]callbackThe function to call when the specified event happens. Pass NULL to unregister the handler.
[in]callback_argGeneric argument that will be provided to the callback when called, can be NULL
Returns
CY_RSLT_SUCCESS if interrupt was successfully enabled.

◆ mtb_bmi160_free()

void mtb_bmi160_free ( mtb_bmi160_t obj)

Frees up any resources allocated by the motion_sensor as part of mtb_bmi160_init_i2c().

Parameters
[in]objPointer to a BMI160 object.