upm
1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
|
Go to the source code of this file.
Data Structures | |
struct | _kx122_context |
struct | _odr_item |
Functions | |
kx122_context | kx122_init (int bus, int addr, int chip_select_pin, int spi_bus_frequency) |
void | kx122_close (kx122_context dev) |
upm_result_t | kx122_device_init (const kx122_context dev, KX122_ODR_T odr, KX122_RES_T res, KX122_RANGE_T grange) |
float | kx122_get_sample_period (const kx122_context dev) |
upm_result_t | kx122_get_who_am_i (const kx122_context dev, uint8_t *data) |
upm_result_t | kx122_get_acceleration_data_raw (const kx122_context dev, float *x, float *y, float *z) |
upm_result_t | kx122_get_acceleration_data (const kx122_context dev, float *x, float *y, float *z) |
upm_result_t | kx122_sensor_software_reset (const kx122_context dev) |
upm_result_t | kx122_enable_iir (const kx122_context dev) |
upm_result_t | kx122_disable_iir (const kx122_context dev) |
upm_result_t | kx122_self_test (const kx122_context dev) |
upm_result_t | kx122_set_sensor_standby (const kx122_context dev) |
upm_result_t | kx122_set_sensor_active (const kx122_context dev) |
upm_result_t | kx122_set_odr (const kx122_context dev, KX122_ODR_T odr) |
upm_result_t | kx122_set_grange (const kx122_context dev, KX122_RANGE_T grange) |
upm_result_t | kx122_set_resolution (const kx122_context dev, KX122_RES_T res) |
upm_result_t | kx122_set_bw (const kx122_context dev, LPRO_STATE_T lpro) |
upm_result_t | kx122_set_average (const kx122_context dev, KX122_AVG_T avg) |
upm_result_t | kx122_install_isr (const kx122_context dev, mraa_gpio_edge_t edge, KX122_INTERRUPT_PIN_T intp, int pin, void(*isr)(void *), void *arg) |
void | kx122_uninstall_isr (const kx122_context dev, KX122_INTERRUPT_PIN_T intp) |
upm_result_t | kx122_enable_interrupt1 (const kx122_context dev, KX122_INTERRUPT_POLARITY_T polarity) |
upm_result_t | kx122_enable_interrupt2 (const kx122_context dev, KX122_INTERRUPT_POLARITY_T polarity) |
upm_result_t | kx122_disable_interrupt1 (const kx122_context dev) |
upm_result_t | kx122_disable_interrupt2 (const kx122_context dev) |
upm_result_t | kx122_route_interrupt1 (const kx122_context dev, uint8_t bits) |
upm_result_t | kx122_route_interrupt2 (const kx122_context dev, uint8_t bits) |
bool | kx122_get_interrupt_status (const kx122_context dev) |
upm_result_t | kx122_get_interrupt_source (const kx122_context dev, uint8_t *data) |
upm_result_t | kx122_clear_interrupt (const kx122_context dev) |
upm_result_t | kx122_enable_data_ready_interrupt (const kx122_context dev) |
upm_result_t | kx122_disable_data_ready_interrupt (const kx122_context dev) |
upm_result_t | kx122_enable_buffer_full_interrupt (const kx122_context dev) |
upm_result_t | kx122_disable_buffer_full_interrupt (const kx122_context dev) |
upm_result_t | kx122_enable_buffer (const kx122_context dev) |
upm_result_t | kx122_disable_buffer (const kx122_context dev) |
upm_result_t | kx122_buffer_init (const kx122_context dev, uint samples, KX122_RES_T res, KX122_BUFFER_MODE_T mode) |
upm_result_t | kx122_set_buffer_resolution (const kx122_context dev, KX122_RES_T res) |
upm_result_t | kx122_set_buffer_threshold (const kx122_context dev, uint samples) |
upm_result_t | kx122_set_buffer_mode (const kx122_context dev, KX122_BUFFER_MODE_T mode) |
upm_result_t | kx122_get_buffer_status (const kx122_context dev, uint *samples) |
upm_result_t | kx122_read_buffer_samples_raw (const kx122_context dev, uint len, float *x_array, float *y_array, float *z_array) |
upm_result_t | kx122_read_buffer_samples (const kx122_context dev, uint len, float *x_array, float *y_array, float *z_array) |
upm_result_t | kx122_clear_buffer (const kx122_context dev) |
Macros | |
#define | DEFAULT_SPI_FREQUENCY 10000 |
#define | KX122_DEFAULT_SLAVE_ADDR_1 0x1F |
#define | KX122_DEFAULT_SLAVE_ADDR_2 0x1E |
#define | MICRO_S 1000000 |
Typedefs | |
typedef struct _kx122_context * | kx122_context |
typedef struct _odr_item | odr_item |
kx122_context kx122_init | ( | int | bus, |
int | addr, | ||
int | chip_select_pin, | ||
int | spi_bus_frequency | ||
) |
KX122 initialization
Set addr to -1 if using SPI. When using I2C, set chip_select_pin to -1;
If no errors occur, the device gets initialized with default values and gets set to active state.
bus | I2C or SPI bus to use. |
addr | I2C address of the sensor. |
chip_select | Chip select pin for SPI. |
spi_bus_frequency | Speed of the SPI communication bus in Hz (ignored when using I2C). |
void kx122_close | ( | kx122_context | dev | ) |
KX122 destructor Closes the I2C or SPI context, and removes interrupts. Frees memory of the kx122_context.
dev | The device context. |
upm_result_t kx122_device_init | ( | const kx122_context | dev, |
KX122_ODR_T | odr, | ||
KX122_RES_T | res, | ||
KX122_RANGE_T | grange | ||
) |
Intilializes the sensor with the given sampling rate, resolution and acceleration range.
This gets called during the kx122_init(), so it will not need to be called unless the device is reset.
Sensor is automatically set into standby mode during the initialization. Sensor is set to active mode after initialization.
dev | The device context. |
odr | One of the KX122_ODR_T values. |
res | One of the KX122_RES_T values. |
grange | One of the KX122_RANGE_T values. |
float kx122_get_sample_period | ( | const kx122_context | dev | ) |
Gets the length of one sample period depending on the sampling rate of the sensor.
dev | The device context. |
upm_result_t kx122_get_who_am_i | ( | const kx122_context | dev, |
uint8_t * | data | ||
) |
Gets who am i value of the sensor.
dev | The device context. |
data | Pointer to a uint8_t variable to store the value. |
upm_result_t kx122_get_acceleration_data_raw | ( | const kx122_context | dev, |
float * | x, | ||
float * | y, | ||
float * | z | ||
) |
Gets raw accelerometer data from the sensor.
dev | The device context. |
x | Pointer to a floating point variable to store the x-axis value. Can be set to NULL if not wanted. |
y | Pointer to a floating point variable to store the y-axis value. Can be set to NULL if not wanted. |
z | Pointer to a floating point variable to store the z-axis value. Can be set to NULL if not wanted. |
upm_result_t kx122_get_acceleration_data | ( | const kx122_context | dev, |
float * | x, | ||
float * | y, | ||
float * | z | ||
) |
Gets converted (m/s^2) accelerometer data from the sensor.
dev | The device context. |
x | Pointer to a floating point variable to store the x-axis value. Can be set to NULL if not wanted. |
y | Pointer to a floating point variable to store the y-axis value. Can be set to NULL if not wanted. |
z | Pointer to a floating point variable to store the z-axis value. Can be set to NULL if not wanted. |
upm_result_t kx122_sensor_software_reset | ( | const kx122_context | dev | ) |
Performs a sensor software reset. The software reset clears the RAM of the sensor and resets all registers to pre-defined values.
You should call kx122_device_init() after the software reset.
See the datasheet for more details.
dev | The device context. |
upm_result_t kx122_enable_iir | ( | const kx122_context | dev | ) |
Enables sensor filter bypass.
Sensor needs to be in standby mode when enabling the filter bypass.
dev | The device context. |
upm_result_t kx122_disable_iir | ( | const kx122_context | dev | ) |
Disables sensor filter bypass.
Sensor needs to be in standby mode when disabling the filter bypass.
dev | The device context. |
upm_result_t kx122_self_test | ( | const kx122_context | dev | ) |
Performs a self-test of the sensor. The test applies an electrostatic force to the sensor, simulating input acceleration. The test compares samples from all axis before and after applying the electrostatic force to the sensor. If the amount of acceleration increases according to the values defined in TABLE 1 of the datasheet, the test passes.
The function prints out the minimum, maximum and values calculated during the test for each axis, and the result of the test for each axis.
See the datasheet for more information.
dev | The device context. |
upm_result_t kx122_set_sensor_standby | ( | const kx122_context | dev | ) |
Sets the sensor to the standby mode.
dev | The device context. |
upm_result_t kx122_set_sensor_active | ( | const kx122_context | dev | ) |
Sets the sensor to active mode.
dev | The device context. |
upm_result_t kx122_set_odr | ( | const kx122_context | dev, |
KX122_ODR_T | odr | ||
) |
Sets the ODR of the sensor.
Sensor needs to be in standby mode when setting the ODR.
dev | The device context. |
odr | One of the KX122_ODR_T values. |
upm_result_t kx122_set_grange | ( | const kx122_context | dev, |
KX122_RANGE_T | grange | ||
) |
Sets the acceleration range of the sensor.
Sensor needs to be in standby mode when setting the acceleration range value.
dev | The device context. |
grange | One of the KX122_RANGE_T values. |
upm_result_t kx122_set_resolution | ( | const kx122_context | dev, |
KX122_RES_T | res | ||
) |
Sets the resolution of the sensor. High resolution (16 bits) or low resolution (8 bits).
Sensor needs to be in standby mode when setting the sensor resolution.
When sensor is set to low resolution mode, the sensor runs in low power mode, which in turn enables features such as averaging.(kx122_set_average()).
dev | The device context. |
res | One of the KX122_RES_T values. |
upm_result_t kx122_set_bw | ( | const kx122_context | dev, |
LPRO_STATE_T | lpro | ||
) |
Sets the low pass filter roll off
Sensor needs to be in standby mode when setting the filter roll off value.
dev | The device context. |
lpro | One of the LPRO_STATE_T values. |
upm_result_t kx122_set_average | ( | const kx122_context | dev, |
KX122_AVG_T | avg | ||
) |
Set the amount of samples to be averaged in low power mode.
Sensor needs to be in standby mode when setting the average value.
dev | The device context. |
avg | One of the KX122_AVG_T values. |
upm_result_t kx122_install_isr | ( | const kx122_context | dev, |
mraa_gpio_edge_t | edge, | ||
KX122_INTERRUPT_PIN_T | intp, | ||
int | pin, | ||
void(*)(void *) | isr, | ||
void * | arg | ||
) |
Installs an interrupt handler to be executed when an interrupt is detected on the interrupt pin.
dev | The device context. |
edge | One of the mraa_gpio_edge_t values. Interrupt trigger edge. |
intp | One of the KX122_INTERRUPT_PIN_T values. Specifies which interrupt pin you are setting. |
pin | The GPIO pin to use as the interrupt pin. |
isr | Pointer to the function to be called, when the interrupt occurs. |
arg | The arguments to be passed to the function. |
void kx122_uninstall_isr | ( | const kx122_context | dev, |
KX122_INTERRUPT_PIN_T | intp | ||
) |
Uninstalls a previously installed interrupt handler.
dev | The device context. |
intp | One of the KX122_INTERRUPT_PIN_T values. Specifies which interrupt pin handler is uninstalled. |
upm_result_t kx122_enable_interrupt1 | ( | const kx122_context | dev, |
KX122_INTERRUPT_POLARITY_T | polarity | ||
) |
Enables interrupts on the interrupt pin 1 of the sensor.
Pulse width = 50us (10us if data sampling rate > 1600Hz). Using pulse mode.
Sensor needs to be in standby mode when enabling the interrupt.
See datasheet for more details.
dev | The device context. |
polarity | One of the KX122_INTERRUPT_POLARITY_T values. Select the polarity of the interrupt. |
upm_result_t kx122_enable_interrupt2 | ( | const kx122_context | dev, |
KX122_INTERRUPT_POLARITY_T | polarity | ||
) |
Enables interrupts on the interrupt pin 2 of the sensor.
Pulse width = 50us (10us if data sampling rate > 1600Hz). Using pulse mode.
Sensor needs to be in standby mode when enabling the interrupt.
See datasheet for more details.
dev | The device context. |
polarity | One of the KX122_INTERRUPT_POLARITY_T values. Select the polarity of the interrupt. |
upm_result_t kx122_disable_interrupt1 | ( | const kx122_context | dev | ) |
Disables interrupts on the interrupt pin 1 of the sensor.
Sensor needs to be in standby mode when disabling the interrupt pin 1.
dev | The device context. |
upm_result_t kx122_disable_interrupt2 | ( | const kx122_context | dev | ) |
Disables interrupts on the interrupt pin 2 of the sensor.
Sensor needs to be in standby mode when disabling the interrupt pin 2.
dev | The device context. |
upm_result_t kx122_route_interrupt1 | ( | const kx122_context | dev, |
uint8_t | bits | ||
) |
Routes the interrupts to the interrupt pin 1 of the sensor.
Sensor needs to be in standby mode when routing the interrupts.
See datasheet for more details.
dev | The device context. |
bits | One or more of the KX122_INTERRUPT_T values. Combine with bitwise OR (|) |
upm_result_t kx122_route_interrupt2 | ( | const kx122_context | dev, |
uint8_t | bits | ||
) |
Routes the interrupts to the interrupt pin 2 of the sensor.
Sensor needs to be in standby mode when routing the interrupts.
See datasheet for more details.
dev | The device context. |
bits | One or more of the KX122_INTERRUPT_T values. Combine with bitwise OR (|) |
bool kx122_get_interrupt_status | ( | const kx122_context | dev | ) |
Gets the status of the interrupts. (Has an interrupt occured)
See datasheet for more details.
dev | The device context. |
upm_result_t kx122_get_interrupt_source | ( | const kx122_context | dev, |
uint8_t * | data | ||
) |
Gets the source of the interrupt. The value stored is one or more of the KX122_INTERRUPT_T values, depending on the interrupts that have occured.
If multiple interrupts have occured, and you need to determine a specific interrupt, you can use masking to get the state of the specific interrupt: (int_source & KX122_DATA_READY_INT) == KX122_DATA_READY_INT)
See datasheet for more details.
dev | The device context. |
data | Pointer to a uint8_t variable to store the value. |
upm_result_t kx122_clear_interrupt | ( | const kx122_context | dev | ) |
Clears latching interrupts (Wakeup, Data Ready).
See datasheet for more details.
dev | The device context. |
upm_result_t kx122_enable_data_ready_interrupt | ( | const kx122_context | dev | ) |
Enables the data ready interrupt. Availability of new acceleration data is reflected as an interrupt.
Sensor needs to be in standby mode when enabling the interrupt.
dev | The device context. |
upm_result_t kx122_disable_data_ready_interrupt | ( | const kx122_context | dev | ) |
Disables the data ready interrupt.
Sensor needs to be in standby mode when disabling the interrupt.
dev | The device context. |
upm_result_t kx122_enable_buffer_full_interrupt | ( | const kx122_context | dev | ) |
Enables the buffer full interrupt. Buffer can hold 681 sets of values (8 bit, low resolution mode) or 340 sets of values (16 bit, high resolution mode).
Sensor needs to be in standby mode when enabling the interrupt.
dev | The device context. |
upm_result_t kx122_disable_buffer_full_interrupt | ( | const kx122_context | dev | ) |
Disables the buffer full interrupt.
Sensor needs to be in standby mode when disabling the interrupt.
dev | The device context. |
upm_result_t kx122_enable_buffer | ( | const kx122_context | dev | ) |
Enables the buffer.
Sensor needs to be in standby mode when enabling the buffer.
dev | The device context. |
upm_result_t kx122_disable_buffer | ( | const kx122_context | dev | ) |
Disables the buffer.
Sensor needs to be in standby mode when disabling the buffer.
dev | The device context. |
upm_result_t kx122_buffer_init | ( | const kx122_context | dev, |
uint | samples, | ||
KX122_RES_T | res, | ||
KX122_BUFFER_MODE_T | mode | ||
) |
Initializes the buffer with the given sample watermark level, buffer resolution and buffer operating mode. Buffer is enabled after the initialization.
Sensor is automatically set into standby mode during the buffer initialization. Sensor is set to active mode after initialization.
See the other buffer functions for details about the different parameter values.
dev | The device context. |
samples | Amount of samples to trigger the watermark interrupt. |
res | One of the KX122_RES_T values. |
mode | One of the KX122_BUFFER_MODE_T values. |
upm_result_t kx122_set_buffer_resolution | ( | const kx122_context | dev, |
KX122_RES_T | res | ||
) |
Sets the buffer resolution. Buffer resolution is indepedent of the sensor resolution.
Sensor needs to be in standby mode when setting the buffer resolution.
dev | The device context. |
upm_result_t kx122_set_buffer_threshold | ( | const kx122_context | dev, |
uint | samples | ||
) |
Sets the buffer watermark interrupt threshold. When the buffer sample count reaches the watermark, watermark interrupt will be given.
In low resolution mode, maxiumum number of samples is 681. In high resolution, the maximum number is 340.
See datasheet for more details.
Sensor needs to be in standby mode when setting the buffer threshold.
dev | The device context. |
samples | Amount of samples to trigger the watermark interrupt. |
upm_result_t kx122_set_buffer_mode | ( | const kx122_context | dev, |
KX122_BUFFER_MODE_T | mode | ||
) |
Sets the buffer operating mode. The buffer can operate in FIFO,FILO or Stream mode. The buffer gathers data, reports data and interracts with the status indicators in a slightly different way depending on the operating mode.
See datasheet for more details.
Sensor needs to be in standby mode when setting the buffer mode.
dev | The device context. |
mode | One of the KX122_BUFFER_MODE_T values. |
upm_result_t kx122_get_buffer_status | ( | const kx122_context | dev, |
uint * | samples | ||
) |
Gets the current amount of samples in the buffer.
dev | The device context. |
samples | Pointer to an uint variable to store the data. |
upm_result_t kx122_read_buffer_samples_raw | ( | const kx122_context | dev, |
uint | len, | ||
float * | x_array, | ||
float * | y_array, | ||
float * | z_array | ||
) |
Gets the specified amount of raw acceleration samples from the buffer.
Make sure the array size is atleast the amount of samples to be read.
dev | The device context. |
len | The amount of samples to read from the buffer. |
x_array | Pointer to an floating point array to store the x-axis data. Can be set to NULL if not wanted. |
y_array | Pointer to an floating point array to store the y-axis data. Can be set to NULL if not wanted. |
z_array | Pointer to an floating point array to store the z-axis data. Can be set to NULL if not wanted. |
upm_result_t kx122_read_buffer_samples | ( | const kx122_context | dev, |
uint | len, | ||
float * | x_array, | ||
float * | y_array, | ||
float * | z_array | ||
) |
Gets the specified amount of converted (m/s^2) acceleration samples from the buffer.
Make sure the array size is atleast the amount of samples to be read.
dev | The device context. |
len | The amount of samples to read from the buffer. |
x_array | Pointer to an floating point array to store the x-axis data. Can be set to NULL if not wanted. |
y_array | Pointer to an floating point array to store the y-axis data. Can be set to NULL if not wanted. |
z_array | Pointer to an floating point array to store the z-axis data. Can be set to NULL if not wanted. |
upm_result_t kx122_clear_buffer | ( | const kx122_context | dev | ) |
Clears the buffer, removing all existing samples from the buffer.
dev | The device context. |