Bosch BMA222 3-axis accelerometer driver.
This file contains functions for initializing and reading data from a Bosch BMA222 3-axis accelerometer.
Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
Functions | |
static bool | bma222_device_id (sensor_hal_t *hal, sensor_data_t *data) |
Read BMA222 device ID and revision numbers. More... | |
static bool | bma222_event (sensor_t *sensor, sensor_event_t sensor_event, sensor_event_callback_t *callback, bool enable) |
Enable or disable BMA222 sensor events. More... | |
static bool | bma222_get_accel (sensor_hal_t *hal, sensor_data_t *data) |
Read BMA222 acceleration data. More... | |
static bool | bma222_get_temperature (sensor_hal_t *hal, sensor_data_t *data) |
Read BMA222 temperature data. More... | |
static bool | bma222_get_threshold (sensor_hal_t *hal, sensor_threshold_desc_t *threshold) |
Get event threshold value. More... | |
bool | bma222_init (sensor_t *sensor, int resvd) |
Bosch BMA222 accelerometer driver initialization. More... | |
static bool | bma222_ioctl (sensor_t *sensor, sensor_command_t cmd, void *arg) |
BMA222 ioctl control entry point. More... | |
static void | bma222_isr (volatile void *arg) |
Bosch BMA222 driver interrupt service routine. More... | |
static bool | bma222_read (sensor_t *sensor, sensor_read_t type, sensor_data_t *data) |
Read sensor data. More... | |
static bool | bma222_selftest (sensor_t *sensor, int *test_code, void *arg) |
Run BMA222 self-tests. More... | |
static bool | bma222_set_bandwidth (sensor_hal_t *hal, int16_t band) |
Set the BMA222 digital filter cut-off frequency. More... | |
static bool | bma222_set_range (sensor_hal_t *hal, int16_t range) |
Set the BMA222 full scale acceleration range. More... | |
static bool | bma222_set_state (sensor_t *sensor, sensor_state_t mode) |
Set the BMA222 execution mode. More... | |
static bool | bma222_set_tap (sensor_hal_t *hal, sensor_tap_params_t *params) |
BMA222 tap detection configuration. More... | |
static bool | bma222_set_threshold (sensor_hal_t *hal, sensor_threshold_desc_t *threshold) |
Set event threshold value. More... | |
static void | bma222_sleep_en (sensor_hal_t *hal, bool sleep) |
Enable or disable the BMA222 sleep mode. More... | |
Variables | |
static const sensor_map_t | band_table [] |
Bosch BMA222 Bandwidth Table (hertz, register value) More... | |
static sensor_event_callback_t | event_cb [5] |
Sensor Event Callback Descriptors (data=0, motion=1, low-g=2, high-g=3, tap=4) More... | |
struct { | |
union { | |
uint8_t status_byte [4] | |
Status bytes. More... | |
struct { | |
uint8_t d_tap_int: 1 | |
double-tap interrupt triggered More... | |
uint8_t data_int: 1 | |
New data interrupt triggered. More... | |
uint8_t flat: 1 | |
Orientation with respect to gravity. More... | |
uint8_t flat_int: 1 | |
< Status fields More... | |
uint8_t high_first_x: 1 | |
x-axis high-g interrupt More... | |
uint8_t high_first_y: 1 | |
y-axis high-g interrupt More... | |
uint8_t high_first_z: 1 | |
z-axis high-g interrupt More... | |
uint8_t high_int: 1 | |
High-g criteria triggered. More... | |
uint8_t high_sign: 1 | |
High-g interrupt sign. More... | |
uint8_t low_int: 1 | |
Low-g criteria triggered. More... | |
uint8_t orient: 3 | |
orientation with respect to gravity More... | |
uint8_t orient_int: 1 | |
Orientation interrupt triggered. More... | |
uint8_t reserved_09: 1 | |
uint8_t reserved_0a: 7 | |
uint8_t s_tap_int: 1 | |
Single-tap interrupt triggered. More... | |
uint8_t slope_first_x: 1 | |
x-axis any-motion interrupt More... | |
uint8_t slope_first_y: 1 | |
y-axis any-motion interrupt More... | |
uint8_t slope_first_z: 1 | |
z-axis any-motion interrupt More... | |
uint8_t slope_int: 1 | |
Slope criteria triggered. More... | |
uint8_t slope_sign: 1 | |
Axis motion direction. More... | |
uint8_t tap_first_x: 1 | |
x-axis tap interrupt More... | |
uint8_t tap_first_y: 1 | |
y-axis tap interrupt More... | |
uint8_t tap_first_z: 1 | |
z-axis tap interrupt More... | |
uint8_t tap_sign: 1 | |
Tap axis motion direction. More... | |
} status_field | |
} | |
bma_axis_t acc [3] | |
Acceleration data. More... | |
int8_t temp | |
Temperature data. More... | |
} | event_regs |
Sensor Event Registers. More... | |
static const sensor_map_t | range_table [] |
Bosch BMA222 Range Table (milli-g, register value) More... | |
|
static |
Read BMA222 device ID and revision numbers.
This function reads the accelerometer hardware identification registers and returns these values in the specified data structure.
hal | Address of an initialized sensor hardware descriptor. |
data | Address of sensor_data_t structure to return values. |
References BMA222_CHIP_ID, sensor_hal::bus, sensor_data_t::device, sensor_bus_get, bus_desc_t::status, and STATUS_OK.
Referenced by bma222_read().
|
static |
Enable or disable BMA222 sensor events.
sensor | Address of an initialized sensor device descriptor |
sensor_event | Specifies the sensor event type |
callback | Application-defined event callback handler descriptor |
enable | Enable flag: true = enable event, false = disable event |
References BMA222_16_INTR_EN, BMA222_17_INTR_EN, BMA222_D_TAP_EN, BMA222_DATA_EN, BMA222_HIGH_EN_X, BMA222_HIGH_EN_Y, BMA222_HIGH_EN_Z, BMA222_LOW_EN, BMA222_S_TAP_EN, BMA222_SLOPE_EN_X, BMA222_SLOPE_EN_Y, BMA222_SLOPE_EN_Z, sensor_desc::hal, sensor_bus_get, sensor_bus_put, SENSOR_EVENT_HIGH_G, SENSOR_EVENT_LOW_G, SENSOR_EVENT_MOTION, SENSOR_EVENT_NEW_DATA, SENSOR_EVENT_TAP, and status.
Referenced by bma222_init(), and bma222_ioctl().
|
static |
Read BMA222 acceleration data.
This function obtains accelerometer data for all three axes of the Bosch device. The data is read from three device registers using a multi-byte bus transfer.
Along with the actual sensor data, the LSB byte contains a "new" flag indicating if the data for this axis has been updated since the last time the axis data was read. Reading either LSB or MSB data will clear this flag.
sensor | Address of an initialized sensor device descriptor. |
data | The address of a vector storing sensor axis data. |
References sensor_hal::burst_addr, event_regs, and sensor_bus_read.
Referenced by bma222_ioctl(), and bma222_read().
|
static |
Read BMA222 temperature data.
This function reads temperature data, where center temperature 24 C corresponds to a value 0x00 read from the temperature register with temperature slope 0.5 C/LSB.
hal | Address of an initialized sensor hardware descriptor. |
data | The address where temperature samples are returned. |
References BMA222_TEMP, BMA222_TEMP_OFFSET, sensor_hal::bus, sensor_data_t::scaled, sensor_bus_get, bus_desc_t::status, STATUS_OK, and sensor_data_t::temperature.
Referenced by bma222_read().
|
static |
Get event threshold value.
hal | Address of an initialized sensor hardware descriptor. |
threshold | Address of threshold descriptor. |
References BMA222_HIGH_G_THRESHOLD, BMA222_LOW_G_THRESHOLD, BMA222_SLOPE_THRESHOLD, BMA222_TAP_CONFIG, BMA222_TAP_TH_FIELD, sensor_hal::bus, raw_to_scaled(), sensor_bus_get, sensor_reg_fieldget, SENSOR_THRESHOLD_HIGH_G, SENSOR_THRESHOLD_LOW_G, SENSOR_THRESHOLD_MOTION, SENSOR_THRESHOLD_TAP, bus_desc_t::status, STATUS_OK, sensor_threshold_desc_t::type, and sensor_threshold_desc_t::value.
Referenced by bma222_ioctl().
Bosch BMA222 accelerometer driver initialization.
This is the main initialization function for the BMA222 device.
sensor | Address of a sensor device descriptor. |
resvd | Reserved value. |
References ARRAYSIZE, band_table, sensor_hal::bandwidth, BMA222_CHIP_ID, BMA222_DATA_RESOLUTION, bma222_event(), BMA222_ID_VAL, bma222_ioctl(), bma222_isr(), BMA222_NEW_DATA_X, bma222_read(), bma222_selftest(), bma222_set_state(), sensor_hal::burst_addr, sensor_hal::bus, sensor_desc::drv, sensor_device_t::func, sensor_desc::hal, sensor_hal::mcu_sigint, sensor_hal::range, range_table, sensor_funcs_t::read, sensor_hal::resolution, sensor_bus_get, SENSOR_CAPS_3_AXIS, SENSOR_CAPS_AUX_TEMP, SENSOR_CAPS_HI_G_EVENT, SENSOR_CAPS_LO_G_EVENT, SENSOR_CAPS_SELFTEST, SENSOR_CAPS_TAP_EVENT, SENSOR_CAPS_TILT_EVENT, sensor_irq_connect(), SENSOR_SCALE_milli, SENSOR_STATE_RESET, SENSOR_UNITS_g0, SENSOR_VENDOR_BOSCH, status, bus_desc_t::status, and STATUS_OK.
|
static |
BMA222 ioctl control entry point.
hal | Address of an initialized sensor hardware descriptor. |
cmd | Command to execute |
arg | Argument for command (varies) |
References sensor_data_t::axis, bma222_event(), bma222_get_accel(), bma222_get_threshold(), bma222_set_bandwidth(), bma222_set_range(), bma222_set_state(), bma222_set_tap(), bma222_set_threshold(), sensor_desc::err, sensor_desc::hal, sensor_desc::mod, sensor_data_t::scaled, SENSOR_DISABLE_EVENT, SENSOR_ENABLE_EVENT, SENSOR_ERR_UNSUPPORTED, SENSOR_GET_THRESHOLD, SENSOR_READ_VECTOR, SENSOR_SET_BANDWIDTH, SENSOR_SET_RANGE, SENSOR_SET_STATE, SENSOR_SET_TAP, SENSOR_SET_THRESHOLD, SENSOR_STATE_NORMAL, SENSOR_STATE_RESET, vector3_t::x, vector3_t::y, and vector3_t::z.
Referenced by bma222_init().
|
static |
Bosch BMA222 driver interrupt service routine.
This is the common interrupt service routine for all enabled BMA222 interrupt events. Five different types of interrupts can be programmed. All interrupt criteria are combined and drive the interrupt pad with a Boolean OR
condition.
Interrupt criteria are tested against values from the BMA222 digital filter output. All thresholds are scaled using the current device range. Timings for high and low acceleration are absolute values (1 LSB of HG_dur and LG_dur registers corresponds to 1 millisecond, +/- 10%). Timing for the any-motion interrupt and alert detection are proportional to the bandwidth setting.
This routine handles interrupts generated when low-g, high-g, any-motion, alert, and new data criteria are satisfied and the corresponding event notification is enabled in the device.
The BMA222 device does not provide any way to definitively identify an any-motion interrupt once it has occurred. So, if a handler has been installed for that event, it will always be called by this routine, and the SENSOR_EVENT_MOTION indicator will be set in the event type field.
arg | The address of the driver sensor_hal_t descriptor. |
References sensor_hal::burst_addr, sensor_hal::bus, sensor_event_data_t::data, sensor_event_data_t::event, event_regs, sensor_event_callback_t::handler, bus_desc_t::no_wait, sensor_data_t::scaled, sensor_bus_read, SENSOR_EVENT_D_TAP, SENSOR_EVENT_HIGH_G, SENSOR_EVENT_LOW_G, SENSOR_EVENT_MOTION, SENSOR_EVENT_NEW_DATA, SENSOR_EVENT_S_TAP, SENSOR_EVENT_UNKNOWN, sensor_timestamp(), bus_desc_t::status, STATUS_OK, and sensor_data_t::timestamp.
Referenced by bma222_init().
|
static |
Read sensor data.
This routine calls the appropriate internal data function to obtain the specified type of data from the sensor device.
sensor | Address of an initialized sensor device descriptor. |
type | Type of sensor data to read. |
data | The address where data values are returned. |
References bma222_device_id(), bma222_get_accel(), bma222_get_temperature(), sensor_desc::err, sensor_desc::hal, SENSOR_ERR_FUNCTION, SENSOR_READ_ACCELERATION, SENSOR_READ_ID, and SENSOR_READ_TEMPERATURE.
Referenced by bma222_init().
Run BMA222 self-tests.
sensor | Address of an initialized sensor device descriptor. |
test_code | Address of a device-specific numeric test code. |
arg | Device-specific self-test argument options. |
References SENSOR_TEST_DEFLECTION.
Referenced by bma222_init().
|
static |
Set the BMA222 digital filter cut-off frequency.
hal | Address of an initialized sensor hardware descriptor. |
band | The index of a driver-specific bandwidth table entry. |
References BMA222_BANDWIDTH, sensor_hal::bus, sensor_bus_put, bus_desc_t::status, and STATUS_OK.
Referenced by bma222_ioctl().
|
static |
Set the BMA222 full scale acceleration range.
hal | Address of an initialized sensor hardware descriptor. |
range | The index of a driver-specific range table entry. |
References BMA222_G_RANGE, sensor_hal::bus, sensor_bus_put, bus_desc_t::status, and STATUS_OK.
Referenced by bma222_ioctl().
|
static |
Set the BMA222 execution mode.
This routine sets a specified BMA222 execution state to one of the following:
SENSOR_STATE_SUSPEND or SENSOR_STATE_LOWEST_POWER The BMA222 can be put into a suspend mode to easily achieve a power consumption below 1uA. In this mode all analog modules except for power-on reset will be disabled. Only reads through the serial interface are supported during suspend.
SENSOR_STATE_SLEEP or SENSOR_STATE_LOW_POWER This option sets the BMA222 to a low-power mode. In this mode, the device periodically wakes up, evaluates acceleration data with respect to interrupt criteria defined by the user and goes back to sleep if no interrupt has occurred using the following procedure:
SENSOR_STATE_NORMAL or SENSOR_STATE_HIGHEST_POWER In normal mode the sensor IC data and status registers can be accessed without restriction. The device current consumption is typically 250 microamps in this state.
SENSOR_STATE_X_AXIS_STANDBY SENSOR_STATE_Y_AXIS_STANDBY SENSOR_STATE_Z_AXIS_STANDBY In order to optimize further power consumption of the BMA222, data evaluation of individual axes can be deactivated.
SENSOR_STATE_RESET This function resets the device and internal registers to the power-up default settings.
In the wake-up mode, the BMA222 automatically switches from sleep mode to normal mode after a delay defined by a programmable wake_up_pause
value. After transitioning from sleep to normal mode, acceleration data acquisition and interrupt verification are performed. The sensor automatically returns to sleep mode again if no interrupt criteria are satisfied.
sensor | Address of a sensor device descriptor. |
mode | A specified sensor operational mode. |
References BMA222_POWER_MODES, BMA222_RESET, bma222_sleep_en(), BMA222_SOFTRESET, BMA222_SUSPEND, sensor_desc::hal, sensor_desc::mod, sensor_bus_put, SENSOR_STATE_HIGHEST_POWER, SENSOR_STATE_LOW_POWER, SENSOR_STATE_LOWEST_POWER, SENSOR_STATE_NORMAL, SENSOR_STATE_RESET, SENSOR_STATE_SLEEP, and SENSOR_STATE_SUSPEND.
Referenced by bma222_init(), and bma222_ioctl().
|
static |
BMA222 tap detection configuration.
hal | Address of an initialized sensor hardware descriptor. |
params | Address of an initialized tap parameter structure. |
Referenced by bma222_ioctl().
|
static |
Set event threshold value.
hal | Address of an initialized sensor hardware descriptor. |
threshold | Address of threshold descriptor. |
References BMA222_HIGH_G_THRESHOLD, BMA222_LOW_G_THRESHOLD, BMA222_SLOPE_THRESHOLD, BMA222_TAP_CONFIG, BMA222_TAP_TH_FIELD, sensor_hal::bus, scaled_to_raw(), sensor_bus_put, sensor_reg_fieldset, SENSOR_THRESHOLD_HIGH_G, SENSOR_THRESHOLD_LOW_G, SENSOR_THRESHOLD_MOTION, SENSOR_THRESHOLD_TAP, bus_desc_t::status, STATUS_OK, sensor_threshold_desc_t::type, and sensor_threshold_desc_t::value.
Referenced by bma222_ioctl().
|
inlinestatic |
Enable or disable the BMA222 sleep mode.
This routine enables or disables the BMA222 sleep mode depending upon the value of the parameter; a value enables sleep mode and a value disables sleep mode by setting or clearing the 'sleep_en' bit, respectively.
hal | Address of an initialized sensor hardware descriptor. |
sleep | Set flag to enable sleep mode. |
References BMA222_LOWPOWER_EN, BMA222_POWER_MODES, BMA222_SLEEP_DUR_1ms, and sensor_bus_put.
Referenced by bma222_set_state().
bma_axis_t acc[3] |
Acceleration data.
|
static |
Bosch BMA222 Bandwidth Table (hertz, register value)
Referenced by bma222_init().
uint8_t d_tap_int |
double-tap interrupt triggered
uint8_t data_int |
New data interrupt triggered.
|
static |
Sensor Event Callback Descriptors (data=0, motion=1, low-g=2, high-g=3, tap=4)
struct { ... } event_regs |
Sensor Event Registers.
Referenced by bma222_get_accel(), and bma222_isr().
uint8_t flat |
Orientation with respect to gravity.
uint8_t flat_int |
< Status fields
Flat interrupt triggered
uint8_t high_first_x |
x-axis high-g interrupt
uint8_t high_first_y |
y-axis high-g interrupt
uint8_t high_first_z |
z-axis high-g interrupt
uint8_t high_int |
High-g criteria triggered.
uint8_t high_sign |
High-g interrupt sign.
uint8_t low_int |
Low-g criteria triggered.
uint8_t orient |
orientation with respect to gravity
uint8_t orient_int |
Orientation interrupt triggered.
|
static |
Bosch BMA222 Range Table (milli-g, register value)
Referenced by bma222_init().
uint8_t reserved_09 |
uint8_t reserved_0a |
uint8_t s_tap_int |
Single-tap interrupt triggered.
uint8_t slope_first_x |
x-axis any-motion interrupt
uint8_t slope_first_y |
y-axis any-motion interrupt
uint8_t slope_first_z |
z-axis any-motion interrupt
uint8_t slope_int |
Slope criteria triggered.
uint8_t slope_sign |
Axis motion direction.
uint8_t status_byte[4] |
Status bytes.
struct { ... } status_field |
uint8_t tap_first_x |
x-axis tap interrupt
uint8_t tap_first_y |
y-axis tap interrupt
uint8_t tap_first_z |
z-axis tap interrupt
uint8_t tap_sign |
Tap axis motion direction.
int8_t temp |
Temperature data.