Atmel Software Framework

kxtf9.c File Reference

Kionix KXTF9 3-axis accelerometer.

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

Copyright (c) 2012 Atmel Corporation. All rights reserved.

#include "kxtf9.h"

Data Structures

union  kxtf9_axis_t
 KXTF9 Axis Data Format. More...
 

Enumerations

enum  {
  INDEX_2G,
  INDEX_4G,
  INDEX_8G
}
 
enum  {
  ODR_INDEX_13HZ,
  ODR_INDEX_25HZ,
  ODR_INDEX_50HZ,
  ODR_INDEX_100HZ,
  ODR_INDEX_200HZ,
  ODR_INDEX_400HZ,
  ODR_INDEX_800HZ
}
 

Functions

static int16_t kxtf9_axis_val (const kxtf9_axis_t axis)
 Construct a signed value from a raw axis sample. More...
 
static bool kxtf9_device_id (sensor_hal_t *hal, sensor_data_t *data)
 Read accelerometer device ID and revision numbers. More...
 
static bool kxtf9_event (sensor_t *sensor, sensor_event_t sensor_event, sensor_event_callback_t *callback, bool enable)
 Enable or disable KXTF9 sensor events. More...
 
static bool kxtf9_get_accel (sensor_hal_t *hal, sensor_data_t *data)
 Get accelerometer data. More...
 
static bool kxtf9_get_threshold (sensor_hal_t *hal, sensor_threshold_desc_t *threshold)
 Get event threshold value. More...
 
bool kxtf9_init (sensor_t *sensor, int resvd)
 Kionix KXTF9 accelerometer driver initialization. More...
 
static bool kxtf9_ioctl (sensor_t *sensor, sensor_command_t cmd, void *arg)
 KXTF9 ioctl control entry point. More...
 
static void kxtf9_isr (volatile void *arg)
 Kionix KXTF9 driver interrupt service routine. More...
 
static bool kxtf9_read (sensor_t *sensor, sensor_read_t type, sensor_data_t *data)
 Read sensor data. More...
 
static void kxtf9_resume (sensor_hal_t *hal, uint8_t regval)
 Resume sensor standby/run mode. More...
 
static bool kxtf9_set_bandwidth (sensor_hal_t *hal, int16_t index)
 Set the sample bandwidth for the accelerometer. More...
 
static bool kxtf9_set_range (sensor_hal_t *hal, int16_t range)
 Set the g range for the accelerometer. More...
 
static bool kxtf9_set_sample_rate (sensor_hal_t *hal, int16_t sr)
 Set the sample rate for the accelerometer. More...
 
static bool kxtf9_set_state (sensor_hal_t *hal, sensor_state_t state)
 Set the accelerometer operation state. More...
 
static bool kxtf9_set_tap (sensor_hal_t *hal, sensor_tap_params_t *params)
 Set tap detection parameters. More...
 
static bool kxtf9_set_threshold (sensor_hal_t *hal, sensor_threshold_desc_t *threshold)
 Set event threshold value. More...
 
static uint8_t kxtf9_standby (sensor_hal_t *hal)
 Put sensor in standby mode. More...
 

Variables

static const sensor_map_t band_table []
 Kionix KXTF9 Bandwidth Table (hertz, register value) LPF bandwidth on the KXTF9 is 1/2 ODR. More...
 
static sensor_event_callback_t event_cb [4]
 Sensor Event Callbacks (data=0, motion=1, tap=2, tilt=3) More...
 
static const sensor_map_t range_table []
 Kionix KXTF9 Range Table (milli-g, register value) More...
 

anonymous enum
Enumerator
INDEX_2G 
INDEX_4G 
INDEX_8G 
anonymous enum
Enumerator
ODR_INDEX_13HZ 
ODR_INDEX_25HZ 
ODR_INDEX_50HZ 
ODR_INDEX_100HZ 
ODR_INDEX_200HZ 
ODR_INDEX_400HZ 
ODR_INDEX_800HZ 

static int16_t kxtf9_axis_val ( const kxtf9_axis_t  axis)
inlinestatic

Construct a signed value from a raw axis sample.

The KXTF9 returns 2's-complement 12-bit axis values stored within a 16-bit word.

Parameters
axisA kxtf9_axis_t type storing a raw sensor axis sample.
Returns
The sign extended axis sample value.

References kxtf9_axis_t::acc_lsb, kxtf9_axis_t::acc_msb, kxtf9_axis_t::field, and kxtf9_axis_t::word.

Referenced by kxtf9_get_accel().

static bool kxtf9_device_id ( sensor_hal_t hal,
sensor_data_t data 
)
static

Read accelerometer device ID and revision numbers.

This function reads the accelerometer hardware identification registers and returns these values to the addresses specified in the function parameters.

Parameters
halAddress of an initialized sensor hardware descriptor.
idAn address where the device ID is returned.
verAn address where the device version is returned.
Returns
bool true if the call succeeds, else false is returned.

References sensor_data_t::device, KXTF9_WHO_AM_I, and sensor_bus_get.

Referenced by kxtf9_read().

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

Enable or disable KXTF9 sensor events.

Parameters
sensorAddress of an initialized sensor device descriptor
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 callback, CTRL_REG1_DRDYE, CTRL_REG1_TDTE, CTRL_REG1_TPE, CTRL_REG1_WUFE, sensor_desc::hal, INT_CTRL_REG1_IEA, INT_CTRL_REG1_IEL, INT_CTRL_REG1_IEN, KXTF9_CTRL_REG1, KXTF9_INT_CTRL_REG1, kxtf9_standby(), sensor_bus_put, SENSOR_EVENT_MOTION, SENSOR_EVENT_NEW_DATA, SENSOR_EVENT_TAP, SENSOR_EVENT_TILT, and status.

Referenced by kxtf9_init(), and kxtf9_ioctl().

static bool kxtf9_get_accel ( sensor_hal_t hal,
sensor_data_t data 
)
static

Get accelerometer data.

This function obtains accelerometer data for all three axes of the Kionix 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.

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

References sensor_data_t::axis, sensor_axis_vec_t::axis, sensor_hal::burst_addr, count, kxtf9_axis_val(), sensor_hal::orientation, raw_to_scaled(), sensor_data_t::scaled, sensor_bus_read, sensor_axis_vec_t::sign, sensor_orient_t::x, sensor_orient_t::y, and sensor_orient_t::z.

Referenced by kxtf9_ioctl(), kxtf9_isr(), and kxtf9_read().

static bool kxtf9_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 KXTF9_WUF_THRESH, sensor_bus_get, SENSOR_THRESHOLD_MOTION, sensor_threshold_desc_t::type, and sensor_threshold_desc_t::value.

Referenced by kxtf9_ioctl().

static bool kxtf9_ioctl ( sensor_t sensor,
sensor_command_t  cmd,
void *  arg 
)
static
static void kxtf9_isr ( volatile void *  arg)
static

Kionix KXTF9 driver interrupt service routine.

This is the common interrupt service routine for all enabled KXTF9 interrupt events. Several different types of interrupts can be programmed. All interrupt criteria are combined and drive the interrupt pad with a Boolean OR condition.

This routine handles interrupts generated when motion, tap, double-tap, tilt, and new data criteria are satisfied and the corresponding event notification is enabled in the device.

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

References sensor_hal::bus, sensor_event_data_t::data, sensor_event_data_t::event, sensor_event_callback_t::handler, INT_SRC_REG1_TDO, INT_SRC_REG1_TFD, INT_SRC_REG1_TFU, INT_SRC_REG1_TLE, INT_SRC_REG1_TRI, INT_SRC_REG1_TUP, INT_SRC_REG2_DRDY, INT_SRC_REG2_TD, INT_SRC_REG2_TD_DOUBLE, INT_SRC_REG2_TD_SINGLE, INT_SRC_REG2_TPS, INT_SRC_REG2_WUFS, kxtf9_get_accel(), KXTF9_INT_SRC_REG1, KXTF9_TILT_POS_CUR, bus_desc_t::no_wait, sensor_data_t::scaled, sensor_bus_get, sensor_bus_read, SENSOR_EVENT_MOTION, SENSOR_EVENT_NEW_DATA, SENSOR_EVENT_TAP, SENSOR_EVENT_TILT, SENSOR_EVENT_UNKNOWN, SENSOR_TAP_AXIS_UNKNOWN, SENSOR_TAP_AXIS_X, SENSOR_TAP_AXIS_Y, SENSOR_TAP_AXIS_Z, SENSOR_TAP_DIRECTION_NEG, SENSOR_TAP_DIRECTION_POS, SENSOR_TAP_DIRECTION_UNKNOWN, sensor_timestamp(), bus_desc_t::status, STATUS_INT, STATUS_OK, sensor_data_t::tap, sensor_data_t::timestamp, and sensor_data_t::value.

Referenced by kxtf9_init().

static bool kxtf9_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 sensor_desc::err, sensor_desc::hal, kxtf9_device_id(), kxtf9_get_accel(), SENSOR_ERR_FUNCTION, SENSOR_READ_ACCELERATION, and SENSOR_READ_ID.

Referenced by kxtf9_init().

static void kxtf9_resume ( sensor_hal_t hal,
uint8_t  regval 
)
static

Resume sensor standby/run mode.

This function restores the KXTF9 device to standby or normal mode, based on the register value parameter. This can be used to restore the mode to the state previously saved during a kxtf9_standby() call.

Parameters
halAddress of an initialized sensor hardware descriptor.
Returns
uint8_t Previous contents of CTRL_REG1 register

References KXTF9_CTRL_REG1, and sensor_bus_put.

Referenced by kxtf9_set_bandwidth(), kxtf9_set_tap(), and kxtf9_set_threshold().

static bool kxtf9_set_bandwidth ( sensor_hal_t hal,
int16_t  index 
)
static

Set the sample bandwidth for the accelerometer.

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

References sensor_hal::bandwidth, sensor_map_t::bandwidth_Hz, DATA_CTRL_ODR, index, KXTF9_DATA_CTRL_REG, kxtf9_resume(), kxtf9_standby(), sensor_hal::sample_rate, sensor_bus_get, and sensor_bus_put.

Referenced by kxtf9_ioctl(), and kxtf9_set_sample_rate().

static bool kxtf9_set_range ( sensor_hal_t hal,
int16_t  range 
)
static

Set the g range for the accelerometer.

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 CTRL_REG1_GSEL, KXTF9_CTRL_REG1, kxtf9_standby(), and sensor_bus_put.

Referenced by kxtf9_ioctl().

static bool kxtf9_set_sample_rate ( sensor_hal_t hal,
int16_t  sr 
)
static

Set the sample rate for the accelerometer.

Parameters
halAddress of an initialized sensor hardware descriptor.
srthe sample rate desired in HZ
Returns
bool true if the call succeeds, else false is returned.

References ARRAYSIZE, index, and kxtf9_set_bandwidth().

Referenced by kxtf9_ioctl().

static bool kxtf9_set_state ( sensor_hal_t hal,
sensor_state_t  state 
)
static

Set the accelerometer operation state.

Parameters
halAddress of an initialized sensor hardware descriptor.
@returnbool true if the call succeeds, else false is returned.

References CTRL_REG1_PC1, CTRL_REG3_SRST, delay_ms, KXTF9_CTRL_REG1, KXTF9_CTRL_REG3, kxtf9_standby(), RESET_DELAY_MSEC, sensor_bus_get, sensor_reg_bitset, SENSOR_STATE_HIGH_POWER, SENSOR_STATE_HIGHEST_POWER, SENSOR_STATE_LOWEST_POWER, SENSOR_STATE_NORMAL, SENSOR_STATE_POWER_DOWN, SENSOR_STATE_RESET, SENSOR_STATE_SLEEP, and SENSOR_STATE_SUSPEND.

Referenced by kxtf9_ioctl().

static bool kxtf9_set_threshold ( sensor_hal_t hal,
sensor_threshold_desc_t threshold 
)
static

Set event threshold value.

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

References kxtf9_resume(), kxtf9_standby(), KXTF9_WUF_THRESH, sensor_bus_put, SENSOR_THRESHOLD_MOTION, sensor_threshold_desc_t::type, and sensor_threshold_desc_t::value.

Referenced by kxtf9_ioctl().

static uint8_t kxtf9_standby ( sensor_hal_t hal)
static

Put sensor in standby mode.

This function places the KXTF9 device in standby mode. This can be done to save power, or to allow modification of various control registers. This function will return the value of the CTRL_REG1 register prior to entering standby mode, so that it can be restored.

Parameters
halAddress of an initialized sensor hardware descriptor.
Returns
uint8_t Previous contents of CTRL_REG1 register

References CTRL_REG1_PC1, KXTF9_CTRL_REG1, sensor_bus_get, and sensor_bus_put.

Referenced by kxtf9_event(), kxtf9_set_bandwidth(), kxtf9_set_range(), kxtf9_set_state(), kxtf9_set_tap(), and kxtf9_set_threshold().

const sensor_map_t band_table[]
static
Initial value:
= {
}
#define DATA_CTRL_ODR_200HZ
Definition: kxtf9.h:235
#define DATA_CTRL_ODR_400HZ
Definition: kxtf9.h:236
#define DATA_CTRL_ODR_100HZ
Definition: kxtf9.h:234
#define DATA_CTRL_ODR_50HZ
Definition: kxtf9.h:233
#define DATA_CTRL_ODR_25HZ
Definition: kxtf9.h:232
#define DATA_CTRL_ODR_800HZ
Definition: kxtf9.h:237
#define DATA_CTRL_ODR_12_5HZ
Definition: kxtf9.h:231

Kionix KXTF9 Bandwidth Table (hertz, register value) LPF bandwidth on the KXTF9 is 1/2 ODR.

Referenced by kxtf9_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: common/services/sensors/sensor.c:123

Sensor Event Callbacks (data=0, motion=1, tap=2, tilt=3)

const sensor_map_t range_table[]
static
Initial value:
= {
{{2000}, CTRL_REG1_GSEL_2G},
{{4000}, CTRL_REG1_GSEL_4G},
}
#define CTRL_REG1_GSEL_2G
Definition: kxtf9.h:170
#define CTRL_REG1_GSEL_4G
Definition: kxtf9.h:171
#define CTRL_REG1_GSEL_8G
Definition: kxtf9.h:172

Kionix KXTF9 Range Table (milli-g, register value)

Referenced by kxtf9_init().