Microchip® Advanced Software Framework

bma150.c File Reference

Bosch BMA150 3-axis accelerometer driver.

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

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

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

Functions

static bool bma150_device_id (sensor_hal_t *hal, sensor_data_t *data)
 Read BMA150 device ID and revision numbers. More...
 
static bool bma150_event (sensor_t *sensor, sensor_event_t sensor_event, sensor_event_callback_t *callback, bool enable)
 Enable or disable BMA150 sensor events. More...
 
static bool bma150_get_accel (sensor_hal_t *hal, sensor_data_t *data)
 Read BMA150 acceleration data. More...
 
static bool bma150_get_temperature (sensor_hal_t *hal, sensor_data_t *data)
 Read BMA150 integrated temperature sensor data. More...
 
static bool bma150_get_threshold (sensor_hal_t *hal, sensor_threshold_desc_t *threshold)
 Get event threshold value. More...
 
bool bma150_init (sensor_t *sensor, int resvd)
 Bosch BMA150 accelerometer driver initialization. More...
 
static bool bma150_ioctl (sensor_t *sensor, sensor_command_t cmd, void *arg)
 BMA150 ioctl control entry point. More...
 
static void bma150_isr (volatile void *arg)
 Bosch BMA150 driver interrupt service routine. More...
 
static bool bma150_read (sensor_t *sensor, sensor_read_t type, sensor_data_t *data)
 Read sensor data. More...
 
static bool bma150_selftest (sensor_t *sensor, int *test_code, void *arg)
 Run BMA150 self-tests. More...
 
static bool bma150_set_bandwidth (sensor_hal_t *hal, int16_t band)
 Set the BMA150 ADC output data bandwidth. More...
 
static bool bma150_set_range (sensor_hal_t *hal, int16_t range)
 Set the BMA150 full scale acceleration range. More...
 
static bool bma150_set_state (sensor_hal_t *hal, sensor_state_t state)
 Set the BMA150 execution mode. More...
 
static bool bma150_set_threshold (sensor_hal_t *hal, sensor_threshold_desc_t *threshold)
 Set event threshold value. More...
 

Variables

static const sensor_map_t band_table []
 Bosch BMA150 Bandwidth Table (hertz, register value) More...
 
static sensor_event_callback_t event_cb [4]
 Sensor Event Callback Descriptors (data=0, motion=1, low-g=2, high-g=3) More...
 
struct {
   union {
      uint8_t   status_byte
 Status registers Byte. More...
 
      struct {
         uint8_t   alert_phase: 1
 Device in alert mode. More...
 
         uint8_t   HG_issued: 1
 High-g criteria issued. More...
 
         uint8_t   HG_latched: 1
 High-g criteria issued (latch) More...
 
         uint8_t   LG_issued: 1
 Low-g criteria issued. More...
 
         uint8_t   LG_latched: 1
 Low-g criteria issued (latch) More...
 
         uint8_t   st_result: 1
 < Status registers fields More...
 
         uint8_t   unused: 2
 
      }   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 BMA150 Range Table (milli-g, register value) More...
 

static bool bma150_device_id ( sensor_hal_t hal,
sensor_data_t data 
)
static

Read BMA150 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 BMA150_CHIP_ID, BMA150_CHIP_VERSION, sensor_data_t::device, and sensor_bus_get.

Referenced by bma150_read().

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

Enable or disable BMA150 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 BMA150_CTRL2, BMA150_CTRL5, CTRL2_ALERT, CTRL2_ANY_MOTION, CTRL2_ENABLE_HG, CTRL2_ENABLE_LG, CTRL5_ENABLE_ADV_INT, CTRL5_NEW_DATA_INT, sensor_desc::hal, sensor_bus_get, sensor_bus_put, SENSOR_EVENT_HIGH_G, SENSOR_EVENT_LOW_G, SENSOR_EVENT_MOTION, SENSOR_EVENT_NEW_DATA, and status.

Referenced by bma150_init(), and bma150_ioctl().

static bool bma150_get_accel ( sensor_hal_t hal,
sensor_data_t data 
)
static

Read BMA150 acceleration data.

This function obtains accelerometer data for all three axes of the Bosch device. The data is read from six device registers using a multi-byte bus transfer. The 10-bit raw results are then assembled from the two register values for each axis, including extending the sign bit, to form a signed 32-bit value.

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 bma150_ioctl(), and bma150_read().

static bool bma150_get_temperature ( sensor_hal_t hal,
sensor_data_t data 
)
static

Read BMA150 integrated temperature sensor data.

Parameters
sensorAddress of an initialized sensor device descriptor.
dataThe address where temperature samples are returned.
Returns
bool true if the call succeeds, else false is returned.

References BMA150_TEMP, BMA150_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 bma150_read().

static bool bma150_get_threshold ( sensor_hal_t hal,
sensor_threshold_desc_t threshold 
)
static

Get 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 BMA150_ANY_MOTION_THRES, BMA150_HG_THRES, BMA150_LG_THRES, sensor_hal::range, sensor_bus_get, SENSOR_THRESHOLD_HIGH_G, SENSOR_THRESHOLD_LOW_G, SENSOR_THRESHOLD_MOTION, sensor_threshold_desc_t::type, and sensor_threshold_desc_t::value.

Referenced by bma150_ioctl().

static bool bma150_ioctl ( sensor_t sensor,
sensor_command_t  cmd,
void *  arg 
)
static

BMA150 ioctl control entry point.

Parameters
halAddress of an initialized sensor hardware descriptor.
cmdCommand to execute
argArgument for command (varies)
Returns
bool true if the call succeeds, else false is returned.

References sensor_data_t::axis, bma150_event(), bma150_get_accel(), bma150_get_threshold(), bma150_set_bandwidth(), bma150_set_range(), bma150_set_state(), bma150_set_threshold(), sensor_desc::err, sensor_desc::hal, 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_THRESHOLD, vector3_t::x, vector3_t::y, and vector3_t::z.

Referenced by bma150_init().

static void bma150_isr ( volatile void *  arg)
static

Bosch BMA150 driver interrupt service routine.

This is the common interrupt service routine for all enabled BMA150 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 events may be set to an inconsistent state by device EEPROM changes. BMA150 interrupts should be disabled at the host microcontroller when device EEPROM writes are performed.

Interrupt criteria are tested against values from the BMA150 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 BMA150 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_timestamp(), bus_desc_t::status, STATUS_OK, and sensor_data_t::timestamp.

Referenced by bma150_init().

static bool bma150_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 bma150_device_id(), bma150_get_accel(), bma150_get_temperature(), sensor_desc::err, sensor_desc::hal, SENSOR_ERR_FUNCTION, SENSOR_READ_ACCELERATION, SENSOR_READ_ID, and SENSOR_READ_TEMPERATURE.

Referenced by bma150_init().

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

Run BMA150 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 BMA150_CTRL1, BMA150_STATUS1, CTRL1_SELF_TEST_0, CTRL1_SELF_TEST_1, sensor_desc::hal, sensor_bus_get, sensor_reg_bitclear, sensor_reg_bitset, SENSOR_TEST_DEFLECTION, SENSOR_TEST_INTERRUPT, and STATUS1_ST_RESULT.

Referenced by bma150_init().

static bool bma150_set_bandwidth ( sensor_hal_t hal,
int16_t  band 
)
static

Set the BMA150 ADC output data bandwidth.

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 BMA150_CTRL4, CTRL4_BANDWIDTH, sensor_bus_get, and sensor_bus_put.

Referenced by bma150_ioctl().

static bool bma150_set_range ( sensor_hal_t hal,
int16_t  range 
)
static

Set the BMA150 full scale acceleration range.

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

References BMA150_CTRL4, CTRL4_RANGE, sensor_bus_get, and sensor_bus_put.

Referenced by bma150_ioctl().

static bool bma150_set_state ( sensor_hal_t hal,
sensor_state_t  state 
)
static

Set the BMA150 execution mode.

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

SENSOR_STATE_SLEEP or SENSOR_STATE_LOWEST_POWER Setting the sleep mode puts the BMA150 into a very low power state in which no communication with the sensor IC is possible. The current consumption in sleep mode is about 1 microamp.

In case of a soft-reset, it is recommended to do the reset after switching from sleep to operational mode. In case a soft-reset is activated during sleep mode, it can take up to 30ms until normal operation has resumed.

SENSOR_STATE_LOW_POWER This option sets the BMA150 to a low power "wake-up" mode that triggers a system wake-up (interrupt output to master) when motion is detected. In this mode, the device periodically evaluates acceleration data with respect to interrupt criteria defined by the user.

Typical current consumption in wake-up mode is estimated as follows:

* i_DD * t_active + i_DDsm * wake_up_pause
* i_self_wake_up = ------------------------------------------
* t_active + wake_up_pause
*

with approximation:

* t_active = 1ms + 0.333ms * [(4 * 750/bandwidth) + (1500/bandwidth) * n]
*

with parameters:

* i_DD Normal mode current (200 microamp)
* i_DDsm Sleep mode current (1 microamp)
* wake_up_pause Wake-up pause setting
* n Number of data points in any-motion logic (n=0 for
* high-g and low-g threshold, n=3 for any-motion)
* bandwidth bandwidth @ settings 1500 through 25 Hz.
*

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 200 microamps in this state.

SENSOR_STATE_RESET This function resets the device and internal registers to the power-up default settings.

In the wake-up mode, the BMA150 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.

Parameters
halAddress of an initialized sensor hardware descriptor.
stateA specified sensor operational state.
Returns
bool true if the call succeeds, else false is returned.

References BMA150_CTRL1, BMA150_CTRL5, CTRL1_SLEEP, CTRL1_SOFT_RESET, CTRL5_WAKE_UP, sensor_bus_put, sensor_reg_bitclear, sensor_reg_bitset, SENSOR_STATE_HIGHEST_POWER, SENSOR_STATE_LOW_POWER, SENSOR_STATE_LOWEST_POWER, SENSOR_STATE_NORMAL, SENSOR_STATE_RESET, and SENSOR_STATE_SLEEP.

Referenced by bma150_ioctl().

static bool bma150_set_threshold ( sensor_hal_t hal,
sensor_threshold_desc_t threshold 
)
static

Set 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 BMA150_ANY_MOTION_THRES, BMA150_HG_THRES, BMA150_LG_THRES, sensor_hal::bus, sensor_hal::range, sensor_bus_put, SENSOR_THRESHOLD_HIGH_G, SENSOR_THRESHOLD_LOW_G, SENSOR_THRESHOLD_MOTION, bus_desc_t::status, STATUS_OK, sensor_threshold_desc_t::type, and sensor_threshold_desc_t::value.

Referenced by bma150_ioctl().

bma_axis_t acc[3]

Acceleration data.

uint8_t alert_phase

Device in alert mode.

const sensor_map_t band_table[]
static
Initial value:
= {
{{ 25}, BANDWIDTH_25HZ },
{{ 50}, BANDWIDTH_50HZ },
{{ 100}, BANDWIDTH_100HZ },
{{ 190}, BANDWIDTH_190HZ },
{{ 375}, BANDWIDTH_375HZ },
{{ 750}, BANDWIDTH_750HZ },
{{1500}, BANDWIDTH_1500HZ}
}
#define BANDWIDTH_25HZ
Definition: bma020.h:128
#define BANDWIDTH_375HZ
Definition: bma020.h:132
#define BANDWIDTH_1500HZ
Definition: bma020.h:134
#define BANDWIDTH_190HZ
Definition: bma020.h:131
#define BANDWIDTH_50HZ
Definition: bma020.h:129
#define BANDWIDTH_750HZ
Definition: bma020.h:133
#define BANDWIDTH_100HZ
Definition: bma020.h:130

Bosch BMA150 Bandwidth Table (hertz, register value)

Referenced by bma150_init().

sensor_event_callback_t event_cb[4]
static
Initial value:
= {
{.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)

struct { ... } event_regs

Sensor Event Registers.

Referenced by bma150_get_accel(), and bma150_isr().

uint8_t HG_issued

High-g criteria issued.

uint8_t HG_latched

High-g criteria issued (latch)

uint8_t LG_issued

Low-g criteria issued.

uint8_t LG_latched

Low-g criteria issued (latch)

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

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

Referenced by bma150_init().

uint8_t st_result

< Status registers fields

Self-test 0 result

uint8_t status_byte

Status registers Byte.

struct { ... } status_field
uint8_t unused