Driver for the STM LIS2DH12 accelerometer. More...

Detailed Description

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...
 
struct  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.
 

Enumerations

enum  lis2dh12_scale_t { LIS2DH12_SCALE_2G = 0x00, LIS2DH12_SCALE_4G = 0x10, LIS2DH12_SCALE_8G = 0x20, LIS2DH12_SCALE_16G = 0x30 }
 Available scale values. More...
 
enum  lis2dh12_rate_t {
  LIS2DH12_RATE_1HZ = 0x1, LIS2DH12_RATE_10HZ = 0x2, LIS2DH12_RATE_25HZ = 0x3, LIS2DH12_RATE_50HZ = 0x4,
  LIS2DH12_RATE_100HZ = 0x5, LIS2DH12_RATE_200HZ = 0x6, LIS2DH12_RATE_400HZ = 0x7, LIS2DH12_RATE_1620HZ = 0x8,
  LIS2DH12_RATE_VERYHIGH = 0x9
}
 Available sampling rates. More...
 
enum  lis2dh12_powermode_t { LIS2DH12_POWER_DOWN = 0, LIS2DH12_POWER_LOW = 1, LIS2DH12_POWER_NORMAL = 2, LIS2DH12_POWER_HIGH = 3 }
 Available power modes. More...
 
enum  lis2dh12_hp_mode_t { LIS2DH12_HP_MODE_NORMAL = 0x0, LIS2DH12_HP_MODE_REFERENCE = 0x1, LIS2DH12_HP_MODE_AUTORESET = 0x3 }
 LIS2DH12 high pass modes. More...
 
enum  lis2dh12_hp_freq_t { LIS2DH12_HP_FREQ_DIV50 = 0, LIS2DH12_HP_FREQ_DIV100 = 1, LIS2DH12_HP_FREQ_DIV200 = 2, LIS2DH12_HP_FREQ_DIV400 = 3 }
 LIS2DH12 high pass cutoff frequency. More...
 
enum  {
  LIS2DH12_OK = 0, LIS2DH12_NOBUS = -1, LIS2DH12_NODEV = -2, LIS2DH12_NOINT = -3,
  LIS2DH12_NODATA = -4
}
 Status and error return codes. More...
 
enum  { LIS2DH12_INT1 = 1, LIS2DH12_INT2 = 2 }
 
enum  lis2dh12_fifo_mode_t { LIS2DH12_FIFO_MODE_BYPASS = 0, LIS2DH12_FIFO_MODE_FIFOMODE, LIS2DH12_FIFO_MODE_STREAM, LIS2DH12_FIFO_MODE_STREAMtoFIFO }
 LIS2DH12 FIFO modes. More...
 

Functions

int lis2dh12_set_int (const lis2dh12_t *dev, const lis2dh12_int_params_t *params, uint8_t int_line)
 Set the interrupt values in LIS2DH12 sensor device. More...
 
int lis2dh12_read_int_src (const lis2dh12_t *dev, uint8_t *data, uint8_t int_line)
 Read an interrupt event on LIS2DH12 sensor device. More...
 
int lis2dh12_set_fifo (const lis2dh12_t *dev, const lis2dh12_fifo_t *config)
 Set the FIFO configuration. More...
 
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. More...
 
int lis2dh12_read_fifo_src (const lis2dh12_t *dev, LIS2DH12_FIFO_SRC_REG_t *data)
 Read the FIFO source register. More...
 
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. More...
 
int lis2dh12_init (lis2dh12_t *dev, const lis2dh12_params_t *params)
 Initialize the given LIS2DH12 sensor device. More...
 
int lis2dh12_read (const lis2dh12_t *dev, int16_t *data)
 Read acceleration data from the given device. More...
 
int lis2dh12_clear_data (const lis2dh12_t *dev)
 Clear the LIS2DH12 memory, clears all sampled data. More...
 
int lis2dh12_set_scale (lis2dh12_t *dev, lis2dh12_scale_t scale)
 Change device scale value. More...
 
int lis2dh12_set_datarate (const lis2dh12_t *dev, lis2dh12_rate_t rate)
 Change device sampling rate. More...
 
int lis2dh12_set_powermode (const lis2dh12_t *dev, lis2dh12_powermode_t powermode)
 Change device power mode. More...
 
int lis2dh12_set_highpass (const lis2dh12_t *dev, const lis2dh12_highpass_t *config)
 Configures the high pass filter. More...
 
int lis2dh12_set_reference (const lis2dh12_t *dev, uint8_t reference)
 Set the reference value to control the high-pass reference. More...
 
int lis2dh12_read_reference (const lis2dh12_t *dev, uint8_t *data)
 Read the reference value. More...
 
int lis2dh12_set_click (const lis2dh12_t *dev, const lis2dh12_click_t *config)
 Set click configuration. More...
 
int lis2dh12_read_click_src (const lis2dh12_t *dev, LIS2DH12_CLICK_SRC_t *data)
 Read click source register. More...
 
int lis2dh12_poweron (const lis2dh12_t *dev)
 Power on the given device and resets power mode and sampling rate to default values in the device descriptor parameters. More...
 
int lis2dh12_poweroff (const lis2dh12_t *dev)
 Power off the given device. More...
 

Variables

const saul_driver_t lis2dh12_saul_driver
 Export the SAUL interface for this driver.
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Status and error return codes.

Enumerator
LIS2DH12_OK 

everything was fine

LIS2DH12_NOBUS 

bus interface error

LIS2DH12_NODEV 

unable to talk to device

LIS2DH12_NOINT 

wrong interrupt line (has to be LIS2DH12_INT1 or LIS2DH12_INT2)

LIS2DH12_NODATA 

no data available

Definition at line 179 of file lis2dh12.h.

◆ anonymous enum

anonymous enum
Enumerator
LIS2DH12_INT1 

first interrupt line

LIS2DH12_INT2 

second interrupt line

Definition at line 192 of file lis2dh12.h.

◆ lis2dh12_fifo_mode_t

LIS2DH12 FIFO modes.

Enumerator
LIS2DH12_FIFO_MODE_BYPASS 

default mode, FIFO is bypassed

LIS2DH12_FIFO_MODE_FIFOMODE 

normal FIFO mode, stops if FIFO is full

LIS2DH12_FIFO_MODE_STREAM 

Stream mode, oldest values get overwritten.

LIS2DH12_FIFO_MODE_STREAMtoFIFO 

Stream mode and on interrupt jumps to FIFO mode.

Definition at line 224 of file lis2dh12.h.

◆ lis2dh12_hp_freq_t

LIS2DH12 high pass cutoff frequency.

Enumerator
LIS2DH12_HP_FREQ_DIV50 

cutoff freq is ODR divided by 50

LIS2DH12_HP_FREQ_DIV100 

cutoff freq is ODR divided by 100

LIS2DH12_HP_FREQ_DIV200 

cutoff freq is ODR divided by 200

LIS2DH12_HP_FREQ_DIV400 

cutoff freq is ODR divided by 400

Definition at line 132 of file lis2dh12.h.

◆ lis2dh12_hp_mode_t

LIS2DH12 high pass modes.

Enumerator
LIS2DH12_HP_MODE_NORMAL 

normal mode, reset by reading REG_REFERENCE

LIS2DH12_HP_MODE_REFERENCE 

uses the reference signal for filtering

LIS2DH12_HP_MODE_AUTORESET 

automatically resets on interrupt generation

Definition at line 123 of file lis2dh12.h.

◆ lis2dh12_powermode_t

Available power modes.

Enumerator
LIS2DH12_POWER_DOWN 

power down the device

LIS2DH12_POWER_LOW 

low power mode

LIS2DH12_POWER_NORMAL 

normal mode

LIS2DH12_POWER_HIGH 

high resolution

Definition at line 93 of file lis2dh12.h.

◆ lis2dh12_rate_t

Available sampling rates.

Enumerator
LIS2DH12_RATE_1HZ 

sample with 1Hz @ all power modes

LIS2DH12_RATE_10HZ 

sample with 10Hz @ all power modes

LIS2DH12_RATE_25HZ 

sample with 25Hz @ all power modes

LIS2DH12_RATE_50HZ 

sample with 50Hz @ all power modes

LIS2DH12_RATE_100HZ 

sample with 100Hz @ all power modes

LIS2DH12_RATE_200HZ 

sample with 200Hz @ all power modes

LIS2DH12_RATE_400HZ 

sample with 400Hz @ all power modes

LIS2DH12_RATE_1620HZ 

sample with 1620HZ @ Low Power

LIS2DH12_RATE_VERYHIGH 

sample with 1344Hz @ High resolution or \ 5376Hz @ Low Power

Definition at line 76 of file lis2dh12.h.

◆ 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 65 of file lis2dh12.h.

Function Documentation

◆ lis2dh12_clear_data()

int lis2dh12_clear_data ( const lis2dh12_t dev)

Clear the LIS2DH12 memory, clears all sampled data.

Parameters
[in]devdevice descriptor
Returns
LIS2DH12_OK on success

◆ lis2dh12_init()

int lis2dh12_init ( lis2dh12_t dev,
const lis2dh12_params_t params 
)

Initialize the given LIS2DH12 sensor device.

Parameters
[out]devdevice descriptor
[in]paramsstatic device configuration
Returns
LIS2DH12_OK on success
LIS2DH12_NOBUS on bus errors
LIS2DH12_NODEV if no LIS2DH12 device was found on the bus

◆ lis2dh12_poweroff()

int lis2dh12_poweroff ( const lis2dh12_t dev)

Power off the given device.

Parameters
[in]devdevice descriptor
Returns
LIS2DH12_OK on success
LIS2DH12_NOBUS on bus error

◆ lis2dh12_poweron()

int lis2dh12_poweron ( const lis2dh12_t dev)

Power on the given device and resets power mode and sampling rate to default values in the device descriptor parameters.

Parameters
[in]devdevice descriptor
Returns
LIS2DH12_OK on success
LIS2DH12_NOBUS on bus error

◆ lis2dh12_read()

int lis2dh12_read ( const lis2dh12_t dev,
int16_t *  data 
)

Read acceleration data from the given device.

Parameters
[in]devdevice descriptor
[out]dataacceleration data in mili-g, MUST hold 3 values
Returns
LIS2DH12_OK on success
LIS2DH12_NOBUS on bus error

◆ lis2dh12_read_click_src()

int lis2dh12_read_click_src ( const lis2dh12_t dev,
LIS2DH12_CLICK_SRC_t data 
)

Read click source register.

Parameters
[in]devdevice descriptor
[out]dataLIS2DH12_CLICK_SRC_t content, allocate one byte
Returns
LIS2DH12_OK on success

◆ lis2dh12_read_fifo_data()

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.

Parameters
[in]devdevice descriptor
[out]fifo_dataFIFO data, must have space for number of data
[in]numberamount of FIFO data to be read
Returns
number of valid data read from FIFO

◆ lis2dh12_read_fifo_src()

int lis2dh12_read_fifo_src ( const lis2dh12_t dev,
LIS2DH12_FIFO_SRC_REG_t data 
)

Read the FIFO source register.

Parameters
[in]devdevice descriptor
[out]dataLIS2DH12_FIFO_SRC_REG_t content, allocate one byte
Returns
LIS2DH12_OK on success

◆ lis2dh12_read_int_src()

int lis2dh12_read_int_src ( const lis2dh12_t dev,
uint8_t *  data,
uint8_t  int_line 
)

Read an interrupt event on LIS2DH12 sensor device.

Parameters
[in]devdevice descriptor
[out]datadevice interrupt data
[in]int_linenumber of interrupt line (LIS2DH12_INT1 or LIS2DH12_INT2)
Returns
LIS2DH12_OK on success
LIS2DH12_NOBUS on bus errors

◆ lis2dh12_read_reference()

int lis2dh12_read_reference ( const lis2dh12_t dev,
uint8_t *  data 
)

Read the reference value.

Parameters
[in]devdevice descriptor
[out]datareference value read from device
Returns
LIS2DH12_OK on success

◆ lis2dh12_restart_fifo()

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.

Parameters
[in]devdevice descriptor
Returns
LIS2DH12_OK on success

◆ lis2dh12_set_click()

int lis2dh12_set_click ( const lis2dh12_t dev,
const lis2dh12_click_t config 
)

Set click configuration.

Parameters
[in]devdevice descriptor
[in]configdevice click configuration
Returns
LIS2DH12_OK on success

◆ lis2dh12_set_datarate()

int lis2dh12_set_datarate ( const lis2dh12_t dev,
lis2dh12_rate_t  rate 
)

Change device sampling rate.

Parameters
[in]devdevice descriptor
[in]ratechange to given sampling rate
Returns
LIS2DH12_OK on success

◆ lis2dh12_set_fifo()

int lis2dh12_set_fifo ( const lis2dh12_t dev,
const lis2dh12_fifo_t config 
)

Set the FIFO configuration.

Parameters
[in]devdevice descriptor
[in]configdevice FIFO configuration
Returns
LIS2DH12_OK on success

◆ lis2dh12_set_highpass()

int lis2dh12_set_highpass ( const lis2dh12_t dev,
const lis2dh12_highpass_t config 
)

Configures the high pass filter.

Parameters
[in]devdevice descriptor
[in]configdevice high pass configuration
Returns
LIS2DH12_OK on success

◆ lis2dh12_set_int()

int lis2dh12_set_int ( const lis2dh12_t dev,
const lis2dh12_int_params_t params,
uint8_t  int_line 
)

Set the interrupt values in LIS2DH12 sensor device.

Parameters
[in]devdevice descriptor
[in]paramsdevice interrupt configuration
[in]int_linenumber of interrupt line (LIS2DH12_INT1 or LIS2DH12_INT2)
Returns
LIS2DH12_OK on success
LIS2DH12_NOBUS on bus errors

◆ lis2dh12_set_powermode()

int lis2dh12_set_powermode ( const lis2dh12_t dev,
lis2dh12_powermode_t  powermode 
)

Change device power mode.

Parameters
[in]devdevice descriptor
[in]powermodechange to given power mode
Returns
LIS2DH12_OK on success

◆ lis2dh12_set_reference()

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

Parameters
[in]devdevice descriptor
[in]referencereference value [8 Bit]
Returns
LIS2DH12_OK on success

◆ lis2dh12_set_scale()

int lis2dh12_set_scale ( lis2dh12_t dev,
lis2dh12_scale_t  scale 
)

Change device scale value.

Parameters
[in]devdevice descriptor
[in]scalechange to given scale value
Returns
LIS2DH12_OK on success