Kionix KXTF9 3-axis accelerometer.
This file contains functions for initializing and reading data from a Kionix KXTF9 3-axis accelerometer.
Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
#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 |
|
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.
axis | A kxtf9_axis_t type storing a raw sensor axis sample. |
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 |
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.
hal | Address of an initialized sensor hardware descriptor. |
id | An address where the device ID is returned. |
ver | An address where the device version is returned. |
References sensor_data_t::device, KXTF9_WHO_AM_I, and sensor_bus_get.
Referenced by kxtf9_read().
|
static |
Enable or disable KXTF9 sensor events.
sensor | Address of an initialized sensor device descriptor |
callback | Application-defined event callback handler descriptor |
enable | Enable flag: true = enable event, false = disable event |
References 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 |
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.
hal | Address of an initialized sensor hardware descriptor. |
data | The address of a vector storing sensor axis data. |
References sensor_data_t::axis, sensor_axis_vec_t::axis, sensor_hal::burst_addr, 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 |
Get event threshold value.
hal | Address of an initialized sensor hardware descriptor. |
threshold | Address of threshold descriptor. |
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().
Kionix KXTF9 accelerometer driver initialization.
This is the main initialization function for the KXTF9 device.
sensor | Address of a sensor device descriptor. |
resvd | Reserved value. |
References ARRAYSIZE, band_table, sensor_hal::bandwidth, sensor_map_t::bandwidth_Hz, sensor_hal::burst_addr, sensor_hal::bus, CTRL_REG1_PC1, CTRL_REG1_RES, CTRL_REG3_OTDT_400HZ, CTRL_REG3_OTP_6_3HZ, CTRL_REG3_OWUF_50HZ, CTRL_REG3_SRST, DATA_CTRL_ODR_200HZ, delay_ms, sensor_desc::drv, sensor_device_t::func, sensor_desc::hal, INDEX_2G, INT_CTRL_REG1_IEA, INT_CTRL_REG1_IEL, INT_CTRL_REG1_IEN, KXTF9_CTRL_REG1, KXTF9_CTRL_REG3, KXTF9_DATA_CTRL_REG, KXTF9_DATA_RESOLUTION, kxtf9_event(), KXTF9_ID_VAL, KXTF9_INT_CTRL_REG1, KXTF9_INT_REL, kxtf9_ioctl(), kxtf9_isr(), kxtf9_read(), KXTF9_TILT_TIMER, KXTF9_WHO_AM_I, KXTF9_WUF_TIMER, KXTF9_XOUT_L, KXTI9_ID_VAL, sensor_hal::mcu_sigint, ODR_INDEX_50HZ, sensor_hal::range, range_table, sensor_map_t::range_units, sensor_funcs_t::read, RESET_DELAY_MSEC, sensor_hal::resolution, sensor_hal::sample_rate, sensor_bus_get, sensor_bus_put, SENSOR_CAPS_3_AXIS, 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_UNITS_g0, SENSOR_VENDOR_KIONIX, START_DELAY_MSEC, status, bus_desc_t::status, STATUS_OK, TILT_TIMER_INIT, and WUF_TIMER_INIT.
|
static |
KXTF9 ioctl control entry point.
hal | Address of an initialized sensor device descriptor. |
cmd | Command to execute |
arg | Argument for command (varies) |
References sensor_data_t::axis, sensor_desc::err, sensor_desc::hal, kxtf9_event(), kxtf9_get_accel(), kxtf9_get_threshold(), kxtf9_set_bandwidth(), kxtf9_set_range(), kxtf9_set_sample_rate(), kxtf9_set_state(), kxtf9_set_tap(), kxtf9_set_threshold(), 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_SAMPLE_RATE, SENSOR_SET_STATE, SENSOR_SET_TAP, SENSOR_SET_THRESHOLD, vector3_t::x, vector3_t::y, and vector3_t::z.
Referenced by kxtf9_init().
|
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.
arg | The address of the driver sensor_hal_t hardware descriptor. |
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 |
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 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 |
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.
hal | Address of an initialized sensor hardware descriptor. |
References KXTF9_CTRL_REG1, and sensor_bus_put.
Referenced by kxtf9_set_bandwidth(), kxtf9_set_tap(), and kxtf9_set_threshold().
|
static |
Set the sample bandwidth for the accelerometer.
hal | Address of an initialized sensor hardware descriptor. |
index | The index of a driver-specific bandwidth table entry. |
References sensor_hal::bandwidth, sensor_map_t::bandwidth_Hz, DATA_CTRL_ODR, 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 |
Set the g range for the accelerometer.
hal | Address of an initialized sensor hardware descriptor. |
range | The index of a driver-specific range table entry. |
References CTRL_REG1_GSEL, KXTF9_CTRL_REG1, kxtf9_standby(), and sensor_bus_put.
Referenced by kxtf9_ioctl().
|
static |
Set the sample rate for the accelerometer.
hal | Address of an initialized sensor hardware descriptor. |
sr | the sample rate desired in HZ |
References ARRAYSIZE, and kxtf9_set_bandwidth().
Referenced by kxtf9_ioctl().
|
static |
Set the accelerometer operation state.
hal | Address of an initialized sensor hardware descriptor. |
@return | bool 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 |
Set tap detection parameters.
hal | Address of an initialized sensor hardware descriptor. |
params | Address of an initialized tap parameter structure |
References sensor_tap_params_t::axes, sensor_tap_params_t::between_time, sensor_tap_params_t::ignore_time, INT_CTRL_REG3_TDOM, INT_CTRL_REG3_TFDM, INT_CTRL_REG3_TFUM, INT_CTRL_REG3_TLEM, INT_CTRL_REG3_TRIM, INT_CTRL_REG3_TUPM, KXTF9_INT_CTRL_REG3, kxtf9_resume(), kxtf9_standby(), KXTF9_TDT_TIMER, scaled_to_raw(), sensor_bus_put, sensor_bus_write, SENSOR_TAP_AXIS_X, SENSOR_TAP_AXIS_Y, SENSOR_TAP_AXIS_Z, TAP_TDT_LATENCY_TIMER_DEFAULT, TAP_TDT_TAP_TIMER_MAX_DEFAULT, TAP_TDT_TAP_TIMER_MIN_DEFAULT, TAP_TDT_TIMER_DEFAULT, TAP_TDT_TOTAL_TIMER_DEFAULT, TAP_TDT_WINDOW_TIMER_DEFAULT, TAP_THRESH_MAX_DEFAULT, TAP_THRESH_MIN_DEFAULT, sensor_tap_params_t::tap_time_max, sensor_tap_params_t::tap_time_min, TAP_TIMER_HZ, TDT_L_THRESH, sensor_tap_params_t::threshold_max, sensor_tap_params_t::threshold_min, and sensor_tap_params_t::total_time.
Referenced by kxtf9_ioctl().
|
static |
Set event threshold value.
hal | Address of an initialized sensor hardware descriptor. |
threshold | Type of threshold to be set |
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 |
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.
hal | Address of an initialized sensor hardware descriptor. |
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().
|
static |
Kionix KXTF9 Bandwidth Table (hertz, register value) LPF bandwidth on the KXTF9 is 1/2 ODR.
Referenced by kxtf9_init().
|
static |
Sensor Event Callbacks (data=0, motion=1, tap=2, tilt=3)
|
static |
Kionix KXTF9 Range Table (milli-g, register value)
Referenced by kxtf9_init().