Driver for the STM LIS2DH12 accelerometer. More...
Driver for the STM LIS2DH12 accelerometer.
This device driver provides a minimal interface to LIS2DH12 devices. As of now, it only provides very basic access to the device. The driver configures the device to continuously read the acceleration data with statically defined scale and rate, and with a fixed 10-bit resolution. The LIS2DH12's FIFO is bypassed, so the driver might not be sufficient for use cases where the complete history of readings is of interest.
Also, the current version of the driver supports only interfacing the sensor via SPI. The driver is however written in a way, that adding I2C interface support is quite simple, as all bus related functions (acquire, release, read, write) are cleanly separated in the code.
This driver provides [S]ensor [A]ctuator [U]ber [L]ayer capabilities.
Files | |
file | lis2dh12.h |
Interface definition for the STM LIS2DH12 accelerometer. | |
file | lis2dh12_params.h |
Default configuration for LIS2DH12 devices. | |
file | lis2dh12_registers.h |
LIS2DH12 register definitions. | |
file | lis2dh12_internal.h |
Command definition for the LIS2DH12 accelerometer. | |
Data Structures | |
struct | lis2dh12_params_t |
LIS2DH12 configuration parameters. More... | |
struct | lis2dh12_highpass_t |
LIS2DH12 high pass config values. More... | |
struct | lis2dh12_click_t |
LIS2DH12 click config values. More... | |
struct | lis2dh12_t |
LIS2DH12 device descriptor. More... | |
struct | lis2dh12_int_params_t |
Parameter for interrupt configuration. More... | |
union | lis2dh12_fifo_data_t |
LIS2DH12 FIFO data struct. More... | |
struct | lis2dh12_fifo_t |
LIS2DH12 FIFO config values. More... | |
Macros | |
#define | LIS2DH12_ADDR_DEFAULT (0x19) |
Default I2C slave address for LIS2DH12 devices. | |
#define | LIS2DH12_CLICK_X_SINGLE (1 << 0) |
single click on X axis | |
#define | LIS2DH12_CLICK_X_DOUBLE (1 << 1) |
double click on X axis | |
#define | LIS2DH12_CLICK_Y_SINGLE (1 << 2) |
single click on Y axis | |
#define | LIS2DH12_CLICK_Y_DOUBLE (1 << 3) |
double click on Y axis | |
#define | LIS2DH12_CLICK_Z_SINGLE (1 << 4) |
single click on Z axis | |
#define | LIS2DH12_CLICK_Z_DOUBLE (1 << 5) |
double click on Z axis | |
Functions | |
void | lis2dh12_cfg_threshold_event (const lis2dh12_t *dev, uint32_t mg, uint32_t us, uint8_t axis, uint8_t event, uint8_t pin) |
Configure a threshold event An Interrupt will be generated if acceleration exceeds the set threshold around the current reference value. | |
void | lis2dh12_cfg_click_event (const lis2dh12_t *dev, uint32_t mg, uint32_t us_limit, uint32_t us_latency, uint32_t us_window, uint8_t click, uint8_t pin) |
Configure a click event A click event is generated when the acceleration exceeds the set threshold for less than us_limit µs. | |
void | lis2dh12_cfg_disable_event (const lis2dh12_t *dev, uint8_t event, uint8_t pin) |
Disable interrupt generation for an event This disables an interrupt on pin if a previously configured event occurs. | |
int | lis2dh12_wait_event (const lis2dh12_t *dev, uint8_t pin, bool stale_events) |
Wait for an interrupt event This function will block until an interrupt is received. | |
int | lis2dh12_set_fifo (const lis2dh12_t *dev, const lis2dh12_fifo_t *config) |
Set the FIFO configuration. | |
int | lis2dh12_restart_fifo (const lis2dh12_t *dev) |
Restart the FIFO mode this sets the FIFO mode in BYPASS mode and then back to previous mode Note: The LIS module disables the FIFO after interrupt automatically, it is recommended to set the FIFO in BYPASS mode and then back to old FIFO mode to enable the FIFO again. | |
uint8_t | lis2dh12_read_fifo_data (const lis2dh12_t *dev, lis2dh12_fifo_data_t *fifo_data, uint8_t number) |
This function will read a given number of data from FIFO reads amount of data that is available in FIFO. | |
int | lis2dh12_init (lis2dh12_t *dev, const lis2dh12_params_t *params) |
Initialize the given LIS2DH12 sensor device. | |
int | lis2dh12_read (const lis2dh12_t *dev, lis2dh12_fifo_data_t *data) |
Read acceleration data from the given device. | |
int | lis2dh12_read_temperature (const lis2dh12_t *dev, int16_t *temp) |
Read temperature data from the given device. | |
int | lis2dh12_clear_data (const lis2dh12_t *dev) |
Clear the LIS2DH12 memory, clears all sampled data. | |
int | lis2dh12_set_scale (lis2dh12_t *dev, lis2dh12_scale_t scale) |
Change device measuring range. | |
lis2dh12_scale_t | lis2dh12_get_scale (lis2dh12_t *dev) |
Get device measuring range. | |
int | lis2dh12_set_datarate (const lis2dh12_t *dev, lis2dh12_rate_t rate) |
Change device sampling rate. | |
uint16_t | lis2dh12_get_datarate (const lis2dh12_t *dev) |
Get device sampling rate in Hz. | |
int | lis2dh12_set_resolution (const lis2dh12_t *dev, lis2dh12_resolution_t resolution) |
Change device resolution. | |
lis2dh12_resolution_t | lis2dh12_get_resolution (const lis2dh12_t *dev) |
Get device resolution. | |
int | lis2dh12_set_highpass (const lis2dh12_t *dev, const lis2dh12_highpass_t *config) |
Configures the high pass filter. | |
int | lis2dh12_set_reference (const lis2dh12_t *dev, uint8_t reference) |
Set the reference value to control the high-pass reference. | |
int | lis2dh12_poweron (const lis2dh12_t *dev) |
Power on the given device and resets resolution and sampling rate to default values in the device descriptor parameters. | |
int | lis2dh12_poweroff (const lis2dh12_t *dev) |
Power off the given device. | |
const saul_driver_t | lis2dh12_saul_driver |
Export the SAUL interface for this driver. | |
const saul_driver_t | lis2dh12_saul_temp_driver |
#define LIS2DH12_ADDR_DEFAULT (0x19) |
Default I2C slave address for LIS2DH12 devices.
Definition at line 60 of file lis2dh12.h.
#define LIS2DH12_CLICK_X_DOUBLE (1 << 1) |
double click on X axis
Definition at line 102 of file lis2dh12.h.
#define LIS2DH12_CLICK_X_SINGLE (1 << 0) |
single click on X axis
Definition at line 101 of file lis2dh12.h.
#define LIS2DH12_CLICK_Y_DOUBLE (1 << 3) |
double click on Y axis
Definition at line 104 of file lis2dh12.h.
#define LIS2DH12_CLICK_Y_SINGLE (1 << 2) |
single click on Y axis
Definition at line 103 of file lis2dh12.h.
#define LIS2DH12_CLICK_Z_DOUBLE (1 << 5) |
double click on Z axis
Definition at line 106 of file lis2dh12.h.
#define LIS2DH12_CLICK_Z_SINGLE (1 << 4) |
single click on Z axis
Definition at line 105 of file lis2dh12.h.
anonymous enum |
Status and error return codes.
Definition at line 186 of file lis2dh12.h.
anonymous enum |
Enumerator | |
---|---|
LIS2DH12_INT1 | first interrupt line |
LIS2DH12_INT2 | second interrupt line |
Definition at line 198 of file lis2dh12.h.
enum lis2dh12_fifo_mode_t |
LIS2DH12 FIFO modes.
Definition at line 230 of file lis2dh12.h.
enum lis2dh12_hp_freq_t |
LIS2DH12 high pass cutoff frequency.
Definition at line 140 of file lis2dh12.h.
enum lis2dh12_hp_mode_t |
LIS2DH12 high pass modes.
Definition at line 131 of file lis2dh12.h.
enum lis2dh12_rate_t |
Available sampling rates.
Definition at line 77 of file lis2dh12.h.
Available resolutions.
Enumerator | |
---|---|
LIS2DH12_POWER_DOWN | power down the device |
LIS2DH12_POWER_LOW | 8-bit mode |
LIS2DH12_POWER_NORMAL | 10-bit mode |
LIS2DH12_POWER_HIGH | 12-bit mode |
Definition at line 94 of file lis2dh12.h.
enum lis2dh12_scale_t |
Available scale values.
Enumerator | |
---|---|
LIS2DH12_SCALE_2G | +- 2g |
LIS2DH12_SCALE_4G | +- 4g |
LIS2DH12_SCALE_8G | +- 8g |
LIS2DH12_SCALE_16G | +- 16g |
Definition at line 66 of file lis2dh12.h.
void lis2dh12_cfg_click_event | ( | const lis2dh12_t * | dev, |
uint32_t | mg, | ||
uint32_t | us_limit, | ||
uint32_t | us_latency, | ||
uint32_t | us_window, | ||
uint8_t | click, | ||
uint8_t | pin | ||
) |
Configure a click event A click event is generated when the acceleration exceeds the set threshold for less than us_limit
µs.
A double click event is generated if a second click event occurs within us_window
µs after the first one.
[in] | dev | device descriptor |
[in] | mg | acceleration in mg |
[in] | us_limit | upper limit for click duration in µs |
[in] | us_latency | dead time after click event in µs |
[in] | us_window | time after us_latency in which the second click event must occur to register as double click |
[in] | click | bit map of click axis / types |
[in] | pin | Interrupt pin to use (LIS2DH12_INT1/LIS2DH12_INT2) |
void lis2dh12_cfg_disable_event | ( | const lis2dh12_t * | dev, |
uint8_t | event, | ||
uint8_t | pin | ||
) |
Disable interrupt generation for an event This disables an interrupt on pin
if a previously configured event occurs.
[in] | dev | device descriptor |
[in] | event | Event to disable (LIS2DH12_EVENT_1, LIS2DH12_EVENT_2 or LIS2DH12_EVENT_CLICK) |
[in] | pin | Interrupt pin to use (LIS2DH12_INT1/LIS2DH12_INT2) |
void lis2dh12_cfg_threshold_event | ( | const lis2dh12_t * | dev, |
uint32_t | mg, | ||
uint32_t | us, | ||
uint8_t | axis, | ||
uint8_t | event, | ||
uint8_t | pin | ||
) |
Configure a threshold event An Interrupt will be generated if acceleration exceeds the set threshold around the current reference value.
[in] | dev | device descriptor |
[in] | mg | acceleration in mg |
[in] | us | time in µs for which the threshold must be exceeded |
[in] | axis | bitmap of axis / events to be monitored |
[in] | event | Event slot (1 or 2) |
[in] | pin | Interrupt pin to use (LIS2DH12_INT1/LIS2DH12_INT2) |
int lis2dh12_clear_data | ( | const lis2dh12_t * | dev | ) |
Clear the LIS2DH12 memory, clears all sampled data.
[in] | dev | device descriptor |
uint16_t lis2dh12_get_datarate | ( | const lis2dh12_t * | dev | ) |
Get device sampling rate in Hz.
[in] | dev | device descriptor |
lis2dh12_resolution_t lis2dh12_get_resolution | ( | const lis2dh12_t * | dev | ) |
Get device resolution.
[in] | dev | device descriptor |
lis2dh12_scale_t lis2dh12_get_scale | ( | lis2dh12_t * | dev | ) |
Get device measuring range.
[in] | dev | device descriptor |
int lis2dh12_init | ( | lis2dh12_t * | dev, |
const lis2dh12_params_t * | params | ||
) |
Initialize the given LIS2DH12 sensor device.
[out] | dev | device descriptor |
[in] | params | static device configuration |
int lis2dh12_poweroff | ( | const lis2dh12_t * | dev | ) |
Power off the given device.
[in] | dev | device descriptor |
int lis2dh12_poweron | ( | const lis2dh12_t * | dev | ) |
Power on the given device and resets resolution and sampling rate to default values in the device descriptor parameters.
[in] | dev | device descriptor |
int lis2dh12_read | ( | const lis2dh12_t * | dev, |
lis2dh12_fifo_data_t * | data | ||
) |
Read acceleration data from the given device.
[in] | dev | device descriptor |
[out] | data | acceleration data in mili-g |
uint8_t lis2dh12_read_fifo_data | ( | const lis2dh12_t * | dev, |
lis2dh12_fifo_data_t * | fifo_data, | ||
uint8_t | number | ||
) |
This function will read a given number of data from FIFO reads amount of data that is available in FIFO.
[in] | dev | device descriptor |
[out] | fifo_data | FIFO data, must have space for number of data |
[in] | number | amount of FIFO data to be read |
int lis2dh12_read_temperature | ( | const lis2dh12_t * | dev, |
int16_t * | temp | ||
) |
Read temperature data from the given device.
[in] | dev | device descriptor |
[out] | temp | temperature data in centi-°C |
int lis2dh12_restart_fifo | ( | const lis2dh12_t * | dev | ) |
Restart the FIFO mode this sets the FIFO mode in BYPASS mode and then back to previous mode Note: The LIS module disables the FIFO after interrupt automatically, it is recommended to set the FIFO in BYPASS mode and then back to old FIFO mode to enable the FIFO again.
[in] | dev | device descriptor |
int lis2dh12_set_datarate | ( | const lis2dh12_t * | dev, |
lis2dh12_rate_t | rate | ||
) |
Change device sampling rate.
[in] | dev | device descriptor |
[in] | rate | change to given sampling rate |
int lis2dh12_set_fifo | ( | const lis2dh12_t * | dev, |
const lis2dh12_fifo_t * | config | ||
) |
Set the FIFO configuration.
[in] | dev | device descriptor |
[in] | config | device FIFO configuration |
int lis2dh12_set_highpass | ( | const lis2dh12_t * | dev, |
const lis2dh12_highpass_t * | config | ||
) |
Configures the high pass filter.
[in] | dev | device descriptor |
[in] | config | device high pass configuration |
int lis2dh12_set_reference | ( | const lis2dh12_t * | dev, |
uint8_t | reference | ||
) |
Set the reference value to control the high-pass reference.
In LIS2DH12_HP_MODE_REFERENCE the reference value is used to filter data on all axis. Subtracts reference value from acceleration. Note: LSB changes according to LIS2DH12_SCALE
[in] | dev | device descriptor |
[in] | reference | reference value [8 Bit] |
int lis2dh12_set_resolution | ( | const lis2dh12_t * | dev, |
lis2dh12_resolution_t | resolution | ||
) |
Change device resolution.
[in] | dev | device descriptor |
[in] | resolution | change to given resolution |
int lis2dh12_set_scale | ( | lis2dh12_t * | dev, |
lis2dh12_scale_t | scale | ||
) |
Change device measuring range.
[in] | dev | device descriptor |
[in] | scale | change to given scale value |
int lis2dh12_wait_event | ( | const lis2dh12_t * | dev, |
uint8_t | pin, | ||
bool | stale_events | ||
) |
Wait for an interrupt event This function will block until an interrupt is received.
[in] | dev | device descriptor |
[in] | pin | Interrupt pin to monitor (LIS2DH12_INT1 or LIS2DH12_INT2) |
[in] | stale_events | If true, this also reports events that were generated before this function was called and which are still in the fifo buffer. |