API Reference

group group_board_libs

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.

note

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.

note

There is a known issue with the BMI160 endianness detection. Any code referencing the structures defined in the BMI160 driver should have this file 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, CY8CKIT_028_TFT_PIN_IMU_I2C_SDA, CY8CKIT_028_TFT_PIN_IMU_I2C_SCL, NULL);
    result = mtb_bmi160_init_i2c(&imu, &i2c_instance, MTB_BMI160_DEFAULT_ADRESS);
    /* Read data from BMI160 */
    result = mtb_bmi160_read(&imu, &imu_data);

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(&imu, &intsettings, CY8CKIT_028_TFT_PIN_IMU_INT_1, CYHAL_ISR_PRIORITY_DEFAULT, CYHAL_GPIO_IRQ_FALL, (cyhal_gpio_event_callback_t) &imucallback, NULL);

Defines

CYHAL_BMI160_RSLT_ERR_ADDITIONAL_INT_PIN

An attempt was made to configure too many gpio pins as interrupts.

Enums

enum mtb_bmi160_address_t

mtb_bmi160_address_t: Enumeration used for selecting I2C address.

Values:

enumerator MTB_BMI160_DEFAULT_ADRESS
enumerator MTB_BMI160_SECONDARY_ADRESS

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.

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()

Return

CY_RSLT_SUCCESS if properly initialized, else an error indicating what went wrong.

Parameters
  • [in] obj: Pointer to a BMI160 object. The caller must allocate the memory for this object but the init function will initialize its contents.

  • [in] inst: I2C instance to use for communicating with the BMI160 sensor.

  • [in] address: BMI160 I2C address, set by hardware implemntation.

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

Return

CY_RSLT_SUCCESS if properly initialized, else an error indicating what went wrong.

Parameters
  • [in] obj: Pointer to a BMI160 object.

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.

Return

CY_RSLT_SUCCESS if properly initialized, else an error indicating what went wrong.

Parameters
  • [in] obj: Pointer to a BMI160 object.

  • [out] sensor_data: The accelerometer & gyroscope data read from the motion sensor

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.

Return

pointer to the BMI160 configuration structure.

Parameters
  • [in] obj: Pointer to a BMI160 object.

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.

Return

CY_RSLT_SUCCESS if self tests pass, else an error indicating what went wrong.

Parameters
  • [in] obj: Pointer to a BMI160 object.

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.

Return

CY_RSLT_SUCCESS if interrupt was succesffuly enabled.

Parameters
  • [in] obj: Pointer to a BMI160 object.

  • [in] intsettings: Pointer to a BMI160 interrupt settings structure.

  • [in] pin: Which pin to configure as interrupt

  • [in] intr_priority: The priority for NVIC interrupt events

  • [in] event: The type of interrupt event

  • [in] callback: The function to call when the specified event happens. Pass NULL to unregister the handler.

  • [in] callback_arg: Generic argument that will be provided to the callback when called, can be NULL

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] obj: Pointer to a BMI160 object.

struct mtb_bmi160_t
#include <mtb_bmi160.h>

Structure holding the IMU instance specific information.

struct mtb_bmi160_data_t
#include <mtb_bmi160.h>

Structure holding the accelerometer and gyroscope data read from the device.