Microchip® Advanced Software Framework

bma220.c File Reference

Bosch BMA220 3-axis accelerometer driver.

This file contains functions for initializing and reading data from a Bosch BMA220 3-axis accelerometer.

Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.

#include "bma220.h"
#include "bma_axis.inc.c"

Macros

#define REG_ADDR(reg)   ((reg) * burst_increment)
 

Functions

static bool bma220_device_id (sensor_hal_t *hal, sensor_data_t *data)
 Read BMA220 device ID and revision numbers. More...
 
static bool bma220_event (sensor_t *sensor, sensor_event_t sensor_event, sensor_event_callback_t *callback, bool enable)
 Enable or disable BMA220 sensor events. More...
 
static bool bma220_get_accel (sensor_hal_t *hal, sensor_data_t *data)
 Read BMA220 acceleration data. More...
 
static bool bma220_get_threshold (sensor_hal_t *hal, sensor_threshold_desc_t *threshold)
 Get a BMA220 event threshold value. More...
 
bool bma220_init (sensor_t *sensor, int resvd)
 Bosch BMA220 accelerometer driver initialization. More...
 
static bool bma220_ioctl (sensor_t *sensor, sensor_command_t cmd, void *arg)
 BMA220 ioctl control entry point. More...
 
static void bma220_isr (volatile void *arg)
 Bosch BMA220 driver interrupt service routine. More...
 
static bool bma220_read (sensor_t *sensor, sensor_read_t type, sensor_data_t *data)
 Read sensor data. More...
 
static bool bma220_selftest (sensor_t *sensor, int *test_code, void *arg)
 Run BMA220 self-tests. More...
 
static bool bma220_set_bandwidth (sensor_hal_t *hal, int16_t band)
 Set the BMA220 digital filter cut-off frequency. More...
 
static bool bma220_set_range (sensor_hal_t *hal, int16_t range)
 Set the BMA220 full scale acceleration range. More...
 
static bool bma220_set_state (sensor_t *sensor, sensor_state_t mode)
 Set the BMA220 execution mode. More...
 
static bool bma220_set_tap (sensor_hal_t *hal, sensor_tap_params_t *params)
 Set up tap detection parameters. More...
 
static bool bma220_set_threshold (sensor_hal_t *hal, sensor_threshold_desc_t *threshold)
 Set a BMA220 event threshold value. More...
 
static void bma220_sleep_en (sensor_hal_t *hal, bool sleep)
 Enable or disable the BMA220 sleep mode. More...
 

Variables

static const sensor_map_t band_table []
 Bosch BMA220 Bandwidth Table (hertz, register value) More...
 
static uint8_t burst_increment
 Burst Address Adjustment. 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 [2]
 Status bytes. More...
 
      struct {
         uint8_t   data_int: 1
 New data interrupt triggered. More...
 
         uint8_t   high_int: 1
 High-g criteria triggered. More...
 
         uint8_t   int_first_x: 1
 x-axis triggered any-motion interrupt More...
 
         uint8_t   int_first_y: 1
 y-axis triggered any-motion interrupt More...
 
         uint8_t   int_first_z: 1
 z-axis triggered any-motion interrupt More...
 
         uint8_t   int_sign: 1
 Axis motion direction. 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
 < Status fields More...
 
         uint8_t   slope_int: 1
 Slope criteria triggered. More...
 
         uint8_t   tt_int: 1
 Tap criteria triggered. More...
 
         uint8_t   unused: 3
 
      }   status_field
 
   } 
 
   bma_axis_t   acc [3]
 Acceleration data. More...
 
   uint8_t   unused [6]
 Unused register space. More...
 
event_regs
 Sensor Event Registers. More...
 
static const sensor_map_t range_table []
 Bosch BMA220 Range Table (milli-g, register value) More...
 

static bool bma220_device_id ( sensor_hal_t hal,
sensor_data_t data 
)
static

Read BMA220 device ID and revision numbers.

This function reads the accelerometer hardware identification registers and returns these values in the specified data structure.

Parameters
halAddress of an initialized sensor hardware descriptor.
dataAddress of sensor_data_t structure to return values.
Returns
bool true if the call succeeds, else false is returned.

References BMA220_CHIP_ID, BMA220_CHIP_VERSION, sensor_data_t::device, REG_ADDR, and sensor_bus_get.

Referenced by bma220_read().

static bool bma220_event ( sensor_t sensor,
sensor_event_t  sensor_event,
sensor_event_callback_t callback,
bool  enable 
)
static

Enable or disable BMA220 sensor events.

Parameters
sensorAddress of an initialized sensor device descriptor
sensor_eventSpecifies the sensor event type
callbackApplication-defined event callback handler descriptor
enableEnable flag: true = enable event, false = disable event
Returns
bool true if the call succeeds, else false is returned

References BMA220_INT_ENABLE1, BMA220_INT_ENABLE2, EN_DATA_FIELD, EN_HIGH_X_FIELD, EN_HIGH_Y_FIELD, EN_HIGH_Z_FIELD, EN_LOW_FIELD, EN_SLOPE_X_FIELD, EN_SLOPE_Y_FIELD, EN_SLOPE_Z_FIELD, EN_TT_X_FIELD, EN_TT_Y_FIELD, EN_TT_Z_FIELD, sensor_desc::hal, LATCH_INT_FIELD, REG_ADDR, RESET_INT, 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 bma220_init(), and bma220_ioctl().

static bool bma220_get_accel ( sensor_hal_t hal,
sensor_data_t data 
)
static

Read BMA220 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.

Parameters
sensorAddress of an initialized sensor device descriptor.
dataThe address of a vector storing sensor axis data.
Returns
bool true if the call succeeds, else false is returned.

References sensor_hal::burst_addr, event_regs, and sensor_bus_read.

Referenced by bma220_ioctl(), and bma220_read().

static bool bma220_get_threshold ( sensor_hal_t hal,
sensor_threshold_desc_t threshold 
)
static

Get a BMA220 event threshold value.

Parameters
halAddress of an initialized sensor hardware descriptor.
thresholdAddress of threshold descriptor.
Returns
bool true if the call succeeds, else false is returned.

References BMA220_HG_LG_THRESHOLD, BMA220_SLOPE_CONFIG, sensor_hal::range, REG_ADDR, sensor_bus_get, SENSOR_THRESHOLD_HIGH_G, SENSOR_THRESHOLD_LOW_G, SENSOR_THRESHOLD_MOTION, SENSOR_THRESHOLD_TAP, SENSOR_THRESHOLD_TILT, sensor_threshold_desc_t::type, and sensor_threshold_desc_t::value.

Referenced by bma220_ioctl().

static bool bma220_ioctl ( sensor_t sensor,
sensor_command_t  cmd,
void *  arg 
)
static
static void bma220_isr ( volatile void *  arg)
static

Bosch BMA220 driver interrupt service routine.

This is the common interrupt service routine for all enabled BMA220 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 BMA220 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 BMA220 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.

Parameters
argThe address of the driver sensor_hal_t descriptor.
Returns
Nothing.

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_HIGH_G, SENSOR_EVENT_LOW_G, SENSOR_EVENT_MOTION, SENSOR_EVENT_NEW_DATA, SENSOR_EVENT_TAP, SENSOR_EVENT_UNKNOWN, sensor_timestamp(), bus_desc_t::status, STATUS_OK, and sensor_data_t::timestamp.

Referenced by bma220_init().

static bool bma220_read ( sensor_t sensor,
sensor_read_t  type,
sensor_data_t data 
)
static

Read sensor data.

This routine calls the appropriate internal data function to obtain the specified type of data from the sensor device.

Parameters
sensorAddress of an initialized sensor device descriptor.
typeType of sensor data to read.
dataThe address where data values are returned.
Returns
bool true if the call succeeds, else false is returned.

References bma220_device_id(), bma220_get_accel(), sensor_desc::err, sensor_desc::hal, SENSOR_ERR_FUNCTION, SENSOR_READ_ACCELERATION, and SENSOR_READ_ID.

Referenced by bma220_init().

static bool bma220_selftest ( sensor_t sensor,
int *  test_code,
void *  arg 
)
static

Run BMA220 self-tests.

Parameters
sensorAddress of an initialized sensor device descriptor.
test_codeAddress of a device-specific numeric test code.
argDevice-specific self-test argument options.
Returns
bool true if the call succeeds, else false is returned.

References SENSOR_TEST_DEFLECTION.

Referenced by bma220_init().

static bool bma220_set_bandwidth ( sensor_hal_t hal,
int16_t  band 
)
static

Set the BMA220 digital filter cut-off frequency.

Parameters
halAddress of an initialized sensor hardware descriptor.
bandThe index of a driver-specific bandwidth table entry.
Returns
bool true if the call succeeds, else false is returned.

References BMA220_BANDWIDTH_CONFIG, FILTER_CONFIG_FIELD, REG_ADDR, and sensor_reg_fieldset.

Referenced by bma220_ioctl().

static bool bma220_set_range ( sensor_hal_t hal,
int16_t  range 
)
static

Set the BMA220 full scale acceleration range.

Parameters
halAddress of an initialized sensor hardware descriptor.
rangeThe index of a driver-specific range table entry.
Returns
bool true if the call succeeds, else false is returned.

References BMA220_RANGE_SELFTEST, RANGE_FIELD, REG_ADDR, and sensor_reg_fieldset.

Referenced by bma220_ioctl().

static bool bma220_set_state ( sensor_t sensor,
sensor_state_t  mode 
)
static

Set the BMA220 execution mode.

This routine sets a specified BMA220 execution state to one of the following:

SENSOR_STATE_SUSPEND or SENSOR_STATE_LOWEST_POWER The BMA220 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 BMA220 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:

  1. Wake-up
  2. Enable analog front-end and convert acceleration data until the low-pass filters have settled.
  3. Enable interrupt controller and evaluate interrupt conditions. Once interrupt conditions are evaluated and no interrupt has occurred, the chip goes back to sleep. If no interrupts are enabled, acceleration for all three axes are converted once before the chip goes back to sleep.
  4. Sleep for the programmed duration. Available sleep durations are 2ms, 10ms, 25ms, 50ms, 100ms, 500ms, 1s, and 2s.

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 BMA220, 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.

Parameters
sensorAddress of a sensor device descriptor.
modeA specified sensor operational mode.
Returns
bool true if the call succeeds, else false is returned.

References bma220_sleep_en(), BMA220_SOFTRESET, BMA220_SUSPEND_MODE, sensor_desc::hal, sensor_desc::mod, REG_ADDR, sensor_bus_get, 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 bma220_init(), and bma220_ioctl().

static bool bma220_set_tap ( sensor_hal_t hal,
sensor_tap_params_t params 
)
static

Set up tap detection parameters.

Parameters
halAddress of an initialized sensor hardware descriptor.
paramsAddress of an initialized tap parameter structure.
Returns
bool true if the call succeeds, else false is returned.

Referenced by bma220_ioctl().

static bool bma220_set_threshold ( sensor_hal_t hal,
sensor_threshold_desc_t threshold 
)
static

Set a BMA220 event threshold value.

Parameters
halAddress of an initialized sensor hardware descriptor.
thresholdAddress of threshold descriptor.
Returns
bool true if the call succeeds, else false is returned.

References BMA220_HG_LG_THRESHOLD, BMA220_SLOPE_CONFIG, BMA220_TAP_CONFIG, HIGH_TH_FIELD, LOW_TH_FIELD, sensor_hal::range, REG_ADDR, sensor_reg_fieldset, SENSOR_THRESHOLD_HIGH_G, SENSOR_THRESHOLD_LOW_G, SENSOR_THRESHOLD_MOTION, SENSOR_THRESHOLD_TAP, SENSOR_THRESHOLD_TILT, SLOPE_TH_FIELD, THRESHOLD_IN_G, TT_TH_FIELD, sensor_threshold_desc_t::type, and sensor_threshold_desc_t::value.

Referenced by bma220_ioctl().

static void bma220_sleep_en ( sensor_hal_t hal,
bool  sleep 
)
inlinestatic

Enable or disable the BMA220 sleep mode.

This routine enables or disables the BMA220 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.

Parameters
halAddress of an initialized sensor hardware descriptor.
sleepSet flag to enable sleep mode.
Returns
Nothing

References BMA220_SLEEP_CONFIG, sensor_reg_bitclear, sensor_reg_bitset, and SLEEP_ENABLE.

Referenced by bma220_set_state().

bma_axis_t acc[3]

Acceleration data.

Referenced by sample_callback().

const sensor_map_t band_table[]
static
Initial value:
= {
{{ 32}, BANDWIDTH_32HZ},
{{ 64}, BANDWIDTH_64HZ},
{{ 125}, BANDWIDTH_125HZ},
{{ 250}, BANDWIDTH_250HZ},
{{ 500}, BANDWIDTH_500HZ},
{{1000}, BANDWIDTH_1000HZ}
}
#define BANDWIDTH_64HZ
Definition: bma220.h:226
#define BANDWIDTH_1000HZ
Definition: bma220.h:222
#define BANDWIDTH_125HZ
Definition: bma220.h:225
#define BANDWIDTH_32HZ
Definition: bma220.h:227
#define BANDWIDTH_250HZ
Definition: bma220.h:224
#define BANDWIDTH_500HZ
Definition: bma220.h:223

Bosch BMA220 Bandwidth Table (hertz, register value)

Referenced by bma220_init().

uint8_t burst_increment
static

Burst Address Adjustment.

From SPI -> TWI use burst address increment in 2h steps.

Referenced by bma220_init().

uint8_t data_int

New data interrupt triggered.

sensor_event_callback_t event_cb[5]
static
Initial value:
= {
{.handler = default_event_handler},
{.handler = default_event_handler},
{.handler = default_event_handler},
{.handler = default_event_handler},
{.handler = default_event_handler}
}
void default_event_handler(volatile sensor_event_data_t *data, volatile void *arg)
Default sensor event callback handler.
Definition: sensor.c:116

Sensor Event Callback Descriptors (data=0, motion=1, low-g=2, high-g=3, tap=4)

struct { ... } event_regs

Sensor Event Registers.

Referenced by bma220_get_accel(), and bma220_isr().

uint8_t high_int

High-g criteria triggered.

uint8_t int_first_x

x-axis triggered any-motion interrupt

uint8_t int_first_y

y-axis triggered any-motion interrupt

uint8_t int_first_z

z-axis triggered any-motion interrupt

uint8_t int_sign

Axis motion direction.

uint8_t low_int

Low-g criteria triggered.

uint8_t orient

Orientation with respect to gravity.

Referenced by hmc5883l_get_data().

uint8_t orient_int

< Status fields

Orientation interrupt triggered

const sensor_map_t range_table[]
static
Initial value:
= {
{{ 2000}, RANGE_2G},
{{ 4000}, RANGE_4G},
{{ 8000}, RANGE_8G},
{{16000}, RANGE_16G}
}
#define RANGE_2G
Definition: bma220.h:234
#define RANGE_4G
Definition: bma220.h:235
#define RANGE_8G
Definition: bma220.h:236
#define RANGE_16G
Definition: bma220.h:237

Bosch BMA220 Range Table (milli-g, register value)

Referenced by bma220_init().

uint8_t slope_int

Slope criteria triggered.

uint8_t status_byte[2]

Status bytes.

Referenced by sha204c_send_and_receive().

struct { ... } status_field
uint8_t tt_int

Tap criteria triggered.

uint8_t unused

Unused register space.