2023-12-21 17:31:12 +00:00
|
|
|
#ifndef LIS3DH_H
|
|
|
|
#define LIS3DH_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2023-12-21 18:17:20 +00:00
|
|
|
/* rates */
|
2023-12-23 13:31:59 +00:00
|
|
|
#define LIS3DH_ODR_POWEROFF 0x00
|
|
|
|
#define LIS3DH_ODR_1_HZ 0x01
|
|
|
|
#define LIS3DH_ODR_10_HZ 0x02
|
|
|
|
#define LIS3DH_ODR_25_HZ 0x03
|
|
|
|
#define LIS3DH_ODR_50_HZ 0x04
|
|
|
|
#define LIS3DH_ODR_100_HZ 0x05
|
|
|
|
#define LIS3DH_ODR_200_HZ 0x06
|
|
|
|
#define LIS3DH_ODR_400_HZ 0x07
|
|
|
|
#define LIS3DH_ODR_NORM_1344_HZ 0x09
|
|
|
|
#define LIS3DH_ODR_LP_1600_HZ 0x08
|
|
|
|
#define LIS3DH_ODR_LP_5376_HZ 0x09
|
2023-12-21 17:31:12 +00:00
|
|
|
|
|
|
|
/* range/sens */
|
2023-12-23 13:31:59 +00:00
|
|
|
#define LIS3DH_FS_2G 0x00
|
|
|
|
#define LIS3DH_FS_4G 0x01
|
|
|
|
#define LIS3DH_FS_8G 0x02
|
|
|
|
#define LIS3DH_FS_16G 0x03
|
2023-12-21 23:29:22 +00:00
|
|
|
|
2023-12-22 10:22:43 +00:00
|
|
|
/* operating modes */
|
2023-12-23 13:31:59 +00:00
|
|
|
#define LIS3DH_MODE_HR 0x00
|
|
|
|
#define LIS3DH_MODE_LP 0x01
|
|
|
|
#define LIS3DH_MODE_NORMAL 0x02
|
2023-12-21 17:31:12 +00:00
|
|
|
|
2023-12-22 10:22:43 +00:00
|
|
|
/* FIFO modes */
|
2023-12-23 13:31:59 +00:00
|
|
|
#define LIS3DH_FIFO_MODE_BYPASS 0x00
|
|
|
|
#define LIS3DH_FIFO_MODE_NORMAL 0x01 /* "FIFO" */
|
|
|
|
#define LIS3DH_FIFO_MODE_STREAM 0x02
|
|
|
|
#define LIS3DH_FIFO_MODE_STREAM_TO_FIFO 0x03
|
2023-12-22 10:22:43 +00:00
|
|
|
|
|
|
|
/* FIFO trigger pin selection */
|
2023-12-23 13:31:59 +00:00
|
|
|
#define LIS3DH_FIFO_TRIG_INT1 0x00
|
|
|
|
#define LIS3DH_FIFO_TRIG_INT2 0x01
|
2023-12-21 17:31:12 +00:00
|
|
|
|
2023-12-22 17:00:53 +00:00
|
|
|
/* filter modes */
|
|
|
|
/* this one is reset by reading REFERENCE (0x26) */
|
2023-12-23 13:31:59 +00:00
|
|
|
#define LIS3DH_FILTER_MODE_NORMAL 0x00
|
|
|
|
#define LIS3DH_FILTER_MODE_REFERENCE 0x01
|
|
|
|
#define LIS3DH_FILTER_MODE_NORMAL2 0x02 /* same as 00? */
|
|
|
|
#define LIS3DH_FILTER_MODE_AUTORESET 0x03
|
2023-12-22 17:00:53 +00:00
|
|
|
|
|
|
|
/* filter cutoff */
|
|
|
|
/* unfortunately, there is only a table for low-power mode,
|
|
|
|
and the actual cutoff-frequency depends on the ODR.
|
|
|
|
Naming scheme after ODR@400Hz
|
|
|
|
AN3308 > section 4.3.1.1 */
|
2023-12-23 13:31:59 +00:00
|
|
|
#define LIS3DH_FILTER_CUTOFF_8 0x00 /* highest freq */
|
|
|
|
#define LIS3DH_FILTER_CUTOFF_4 0x01
|
|
|
|
#define LIS3DH_FILTER_CUTOFF_2 0x02
|
|
|
|
#define LIS3DH_FILTER_CUTOFF_1 0x03 /* lowest freq */
|
2023-12-22 17:00:53 +00:00
|
|
|
|
|
|
|
|
2023-12-29 23:42:15 +00:00
|
|
|
/* user provided functions, init and deinit can be set to NULL and won't be used */
|
2023-12-21 17:31:12 +00:00
|
|
|
struct lis3dh_device {
|
|
|
|
int (*init)(void);
|
|
|
|
int (*read)(uint8_t reg, uint8_t *dst, uint32_t size);
|
|
|
|
int (*write)(uint8_t reg, uint8_t value);
|
|
|
|
int (*sleep)(uint32_t dur_us);
|
|
|
|
int (*deinit)(void);
|
|
|
|
};
|
|
|
|
|
2023-12-29 23:42:15 +00:00
|
|
|
/* config for INT2 trigger output */
|
2023-12-30 15:36:35 +00:00
|
|
|
struct lis3dh_int_pin2_config {
|
2023-12-23 18:38:28 +00:00
|
|
|
uint8_t click; /* CLICK interrupt */
|
|
|
|
uint8_t ia1; /* IA1 interrupt */
|
|
|
|
uint8_t ia2; /* IA2 interrupt */
|
|
|
|
uint8_t boot; /* enable BOOT on pin 2 */
|
|
|
|
uint8_t act; /* interrupt on activity */
|
|
|
|
uint8_t polarity; /* INT1 & INT2 polarity. 0 active high, 1 active low */
|
2023-12-29 18:19:11 +00:00
|
|
|
uint8_t latch; /* latch interrupt until cleared */
|
2023-12-23 18:38:28 +00:00
|
|
|
};
|
|
|
|
|
2023-12-29 23:42:15 +00:00
|
|
|
/* config for INT1 trigger output */
|
2023-12-30 15:36:35 +00:00
|
|
|
struct lis3dh_int_pin1_config {
|
2023-12-23 18:38:28 +00:00
|
|
|
uint8_t click; /* CLICK interrupt */
|
|
|
|
uint8_t ia1; /* IA1 interrupt */
|
|
|
|
uint8_t ia2; /* IA2 interrupt */
|
|
|
|
uint8_t drdy_zyxda; /* new [xyz] data ready (not via FIFO) */
|
|
|
|
uint8_t drdy_321; /* not sure */
|
|
|
|
uint8_t wtm; /* FIFO reached watermark level */
|
|
|
|
uint8_t overrun; /* FIFO has overrun */
|
2023-12-29 18:19:11 +00:00
|
|
|
uint8_t latch; /* latch interrupt until cleared */
|
2023-12-23 18:38:28 +00:00
|
|
|
};
|
|
|
|
|
2023-12-29 23:42:15 +00:00
|
|
|
/* config for high-pass filter */
|
2023-12-22 17:00:53 +00:00
|
|
|
struct lis3dh_filter_config {
|
2023-12-23 18:38:28 +00:00
|
|
|
uint8_t mode; /* filter mode, reset behaviour */
|
|
|
|
uint8_t cutoff; /* high-pass filter cutoff freq (~ ODR) */
|
|
|
|
uint8_t fds; /* ¬(bypass filter) */
|
|
|
|
uint8_t click; /* enable filter for CLICK function */
|
|
|
|
uint8_t ia2; /* enable filter for AOI func on INT 2 */
|
|
|
|
uint8_t ia1; /* enable filter for AOI func on INT 1 */
|
2023-12-22 17:00:53 +00:00
|
|
|
};
|
|
|
|
|
2023-12-29 23:42:15 +00:00
|
|
|
/* config for FIFO */
|
2023-12-22 10:22:43 +00:00
|
|
|
struct lis3dh_fifo_config {
|
2023-12-23 13:31:59 +00:00
|
|
|
uint8_t fth; /* user-specified watermark level 0-32 */
|
2023-12-23 18:38:28 +00:00
|
|
|
uint8_t trig; /* pin to trigger when watermark/overrun occurs */
|
|
|
|
uint8_t mode; /* FIFO mode */
|
2023-12-22 10:22:43 +00:00
|
|
|
};
|
|
|
|
|
2023-12-21 17:31:12 +00:00
|
|
|
struct lis3dh_config {
|
|
|
|
uint8_t rate; /* ODR */
|
|
|
|
uint8_t range; /* FS */
|
2023-12-21 23:29:22 +00:00
|
|
|
uint8_t mode; /* LPen and HR */
|
2023-12-22 10:22:43 +00:00
|
|
|
struct lis3dh_fifo_config fifo;
|
2023-12-22 17:00:53 +00:00
|
|
|
struct lis3dh_filter_config filter;
|
2023-12-30 15:36:35 +00:00
|
|
|
struct lis3dh_int_pin1_config int_pin1;
|
|
|
|
struct lis3dh_int_pin2_config int_pin2;
|
2023-12-21 23:29:22 +00:00
|
|
|
};
|
|
|
|
|
2023-12-29 23:42:15 +00:00
|
|
|
/* data read not from FIFO is put here */
|
2023-12-21 23:29:22 +00:00
|
|
|
struct lis3dh_acceleration {
|
2023-12-23 10:33:28 +00:00
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float z;
|
2023-12-21 17:31:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct lis3dh {
|
|
|
|
struct lis3dh_device dev;
|
|
|
|
struct lis3dh_config cfg;
|
2023-12-21 23:29:22 +00:00
|
|
|
struct lis3dh_acceleration acc;
|
2023-12-21 17:31:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct lis3dh lis3dh_t;
|
|
|
|
|
2023-12-23 10:28:43 +00:00
|
|
|
/* struct for containing the FIFO data */
|
|
|
|
struct lis3dh_fifo_data {
|
|
|
|
uint8_t size; /* up to 32 */
|
|
|
|
float x[32];
|
|
|
|
float y[32];
|
|
|
|
float z[32];
|
|
|
|
};
|
|
|
|
|
2023-12-21 17:31:12 +00:00
|
|
|
int lis3dh_init(lis3dh_t *lis3dh);
|
|
|
|
int lis3dh_deinit(lis3dh_t *lis3dh);
|
2023-12-21 23:29:22 +00:00
|
|
|
int lis3dh_configure(lis3dh_t *lis3dh);
|
|
|
|
int lis3dh_poll(lis3dh_t *lis3dh);
|
|
|
|
int lis3dh_read(lis3dh_t *lis3dh);
|
2023-12-22 10:22:43 +00:00
|
|
|
int lis3dh_poll_fifo(lis3dh_t *lis3dh);
|
2023-12-23 10:28:43 +00:00
|
|
|
int lis3dh_read_fifo(lis3dh_t *lis3dh, struct lis3dh_fifo_data *fifo);
|
2023-12-29 17:26:54 +00:00
|
|
|
int lis3dh_clear_int1(lis3dh_t *lis3dh);
|
|
|
|
int lis3dh_clear_int2(lis3dh_t *lis3dh);
|
2023-12-29 18:19:11 +00:00
|
|
|
int lis3dh_reference(lis3dh_t *lis3dh);
|
2023-12-23 10:28:43 +00:00
|
|
|
|
2023-12-21 17:31:12 +00:00
|
|
|
#endif
|