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_internal.h
 Command definition for the LIS2DH12 accelerometer.
 

Data Structures

struct  lis2dh12_params_t
 LIS2DH12 configuration parameters. More...
 
struct  lis2dh12_t
 LIS2DH12 device descriptor. More...
 
struct  lis2dh12_int_params_t
 Parameter for interrupt configuration. More...
 

Macros

#define LIS2DH12_ADDR_DEFAULT   (0x19)
 Default I2C slave address for LIS2DH12 devices.
 
#define LIS2DH12_INT_SRC_XL   (0x01)
 Status of INT_SRC register. More...
 
#define LIS2DH12_INT_SRC_XH   (0x02)
 X high event has occurred.
 
#define LIS2DH12_INT_SRC_YL   (0x04)
 Y low event has occurred.
 
#define LIS2DH12_INT_SRC_YH   (0x08)
 Y high event has occurred.
 
#define LIS2DH12_INT_SRC_ZL   (0x10)
 Z low event has occurred.
 
#define LIS2DH12_INT_SRC_ZH   (0x20)
 Z high event has occurred.
 
#define LIS2DH12_INT_SRC_IA   (0x40)
 1 if interrupt occurred
 
#define LIS2DH12_STATUS_XDA   (0x01)
 Status of INT_SRC register. More...
 
#define LIS2DH12_STATUS_YDA   (0x02)
 Y-axis new data available.
 
#define LIS2DH12_STATUS_ZDA   (0x04)
 Z-axis new data available.
 
#define LIS2DH12_STATUS_ZYXDA   (0x08)
 on X-, Y-, Z-axis new data available
 
#define LIS2DH12_STATUS_XOR   (0x10)
 X-axis data overrun.
 
#define LIS2DH12_STATUS_YOR   (0x20)
 Y-axis data overrun.
 
#define LIS2DH12_STATUS_ZOR   (0x40)
 Y-axis data overrun.
 
#define LIS2DH12_STATUS_ZYXOR   (0x80)
 on X-, Y-, Z-axis data overrun
 

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 = 0x17, LIS2DH12_RATE_10HZ = 0x27, LIS2DH12_RATE_25HZ = 0x37, LIS2DH12_RATE_50HZ = 0x47,
  LIS2DH12_RATE_100HZ = 0x57, LIS2DH12_RATE_200HZ = 0x67, LIS2DH12_RATE_400HZ = 0x77
}
 Available sampling rates. 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_INT_CFG_XLIE = 0x01, LIS2DH12_INT_CFG_XHIE = 0x02, LIS2DH12_INT_CFG_YLIE = 0x04, LIS2DH12_INT_CFG_YHIE = 0x08,
  LIS2DH12_INT_CFG_ZLIE = 0x10, LIS2DH12_INT_CFG_ZHIE = 0x20, LIS2DH12_INT_CFG_6D = 0x40, LIS2DH12_INT_CFG_AOI = 0x80
}
 Interrupt config register values. More...
 
enum  {
  LIS2DH12_INT_TYPE_I1_OVERRUN = 0x02, LIS2DH12_INT_TYPE_I1_WTM = 0x04, LIS2DH12_INT_TYPE_I1_ZYXDA = 0x10, LIS2DH12_INT_TYPE_I1_IA2 = 0x20,
  LIS2DH12_INT_TYPE_I1_IA1 = 0x40, LIS2DH12_INT_TYPE_I1_CLICK = 0x80, LIS2DH12_INT_TYPE_INT_POLARITY = 0x02, LIS2DH12_INT_TYPE_I2_ACT = 0x08,
  LIS2DH12_INT_TYPE_I2_BOOT = 0x10, LIS2DH12_INT_TYPE_I2_IA2 = 0x20, LIS2DH12_INT_TYPE_I2_IA1 = 0x40, LIS2DH12_INT_TYPE_I2_CLICK = 0x80
}
 Interrupt type values. 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_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_poweron (const lis2dh12_t *dev)
 Power on the given device. 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.
 

Macro Definition Documentation

◆ LIS2DH12_INT_SRC_XL

#define LIS2DH12_INT_SRC_XL   (0x01)

Status of INT_SRC register.

X low event has occurred

Definition at line 184 of file lis2dh12.h.

◆ LIS2DH12_STATUS_XDA

#define LIS2DH12_STATUS_XDA   (0x01)

Status of INT_SRC register.

X-axis new data available

Definition at line 196 of file lis2dh12.h.

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

◆ anonymous enum

anonymous enum
Enumerator
LIS2DH12_INT1 

first interrupt line

LIS2DH12_INT2 

second interrupt line

Definition at line 129 of file lis2dh12.h.

◆ anonymous enum

anonymous enum

Interrupt config register values.

Enumerator
LIS2DH12_INT_CFG_XLIE 

enable X low evnt

LIS2DH12_INT_CFG_XHIE 

enable X high event

LIS2DH12_INT_CFG_YLIE 

enable Y low event

LIS2DH12_INT_CFG_YHIE 

enable Y high event

LIS2DH12_INT_CFG_ZLIE 

enable Z low event

LIS2DH12_INT_CFG_ZHIE 

enable Z high event

LIS2DH12_INT_CFG_6D 

enable 6-direction detection

LIS2DH12_INT_CFG_AOI 

and/or combination interrupt events

Definition at line 137 of file lis2dh12.h.

◆ anonymous enum

anonymous enum

Interrupt type values.

Enumerator
LIS2DH12_INT_TYPE_I1_OVERRUN 

FIFO overrun interrupt on INT1.

LIS2DH12_INT_TYPE_I1_WTM 

FIFO watermark inter.

on INT1

LIS2DH12_INT_TYPE_I1_ZYXDA 

ZYXDA interrupt on INT1.

LIS2DH12_INT_TYPE_I1_IA2 

IA2 interrupt on INT1.

LIS2DH12_INT_TYPE_I1_IA1 

IA1 interrupt on INT1.

LIS2DH12_INT_TYPE_I1_CLICK 

click interrupt on INT1

LIS2DH12_INT_TYPE_INT_POLARITY 

INT1 and INT2 pin polarity.

LIS2DH12_INT_TYPE_I2_ACT 

enable activity interrupt on INT2

LIS2DH12_INT_TYPE_I2_BOOT 

enable boot on INT2

LIS2DH12_INT_TYPE_I2_IA2 

IA2 on INT2.

LIS2DH12_INT_TYPE_I2_IA1 

IA1 on INT2.

LIS2DH12_INT_TYPE_I2_CLICK 

click interrupt on INT2

Definition at line 151 of file lis2dh12.h.

◆ lis2dh12_rate_t

Available sampling rates.

Note
The device does also support some additional rates for specific low- power modes, but those are as of now not supported by this driver
Enumerator
LIS2DH12_RATE_1HZ 

sample with 1Hz

LIS2DH12_RATE_10HZ 

sample with 10Hz

LIS2DH12_RATE_25HZ 

sample with 25Hz

LIS2DH12_RATE_50HZ 

sample with 50Hz

LIS2DH12_RATE_100HZ 

sample with 100Hz

LIS2DH12_RATE_200HZ 

sample with 200Hz

LIS2DH12_RATE_400HZ 

sample with 400Hz

Definition at line 77 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 64 of file lis2dh12.h.

Function Documentation

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

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_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_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