Compare commits

..

2 Commits

Author SHA1 Message Date
1a54ce4e5e single click detection 2024-01-01 11:05:45 +00:00
d1ab5a8d99 more src reg interaction 2024-01-01 10:15:57 +00:00
3 changed files with 106 additions and 69 deletions

View File

@ -25,6 +25,7 @@ int lis3dh_init(lis3dh_t *lis3dh) {
memset(&lis3dh->acc, 0, sizeof lis3dh->acc);
memset(&lis3dh->cfg, 0, sizeof lis3dh->cfg);
memset(&lis3dh->adc, 0, sizeof lis3dh->adc);
memset(&lis3dh->src, 0, sizeof lis3dh->src);
lis3dh->cfg.fifo.mode = 0xFF; /* in use if neq 0xFF */
lis3dh->cfg.fifo.fth = 31; /* default watermark level. */
@ -161,6 +162,7 @@ int lis3dh_configure(lis3dh_t *lis3dh) {
}
/* always set block update (BDU) */
/* guarantees all bytes for one [x y z] set were sampled at the same time */
ctrl_reg4 |= 0x80;
/* set high resolution */
@ -322,16 +324,19 @@ int lis3dh_deinit(lis3dh_t *lis3dh) {
return 0;
}
/* read INT1_SRC to clear latched INT1 irq */
int lis3dh_clear_int1(lis3dh_t *lis3dh) {
uint8_t res;
return lis3dh->dev.read(REG_INT1_SRC, &res, 1);
/* read INT1_SRC to clear interrupt active flag and get relevant data */
int lis3dh_read_int1(lis3dh_t *lis3dh) {
return lis3dh->dev.read(REG_INT1_SRC, &lis3dh->src.int1, 1);
}
/* read INT2_SRC to clear latched INT2 irq */
int lis3dh_clear_int2(lis3dh_t *lis3dh) {
uint8_t res;
return lis3dh->dev.read(REG_INT2_SRC, &res, 1);
/* read INT2_SRC to clear interrupt active flag and get relevant data */
int lis3dh_read_int2(lis3dh_t *lis3dh) {
return lis3dh->dev.read(REG_INT2_SRC, &lis3dh->src.int2, 1);
}
/* read CLICK_SRC to clear interrupt active flag and get relevant data */
int lis3dh_read_click(lis3dh_t *lis3dh) {
return lis3dh->dev.read(REG_CLICK_SRC, &lis3dh->src.click, 1);
}
/* read REFERENCE reg to reset HP filter in REFERENCE mode
@ -375,7 +380,7 @@ int lis3dh_reset(lis3dh_t *lis3dh) {
}
/* read all 3 ADCs and convert readings depending on power mode
st 1 LSb is equal to 1 millivolt */
st 1 float count is equal to 1 millivolt */
int lis3dh_read_adc(lis3dh_t *lis3dh) {
uint8_t data[6];
int err = 0;

View File

@ -3,6 +3,22 @@
#include <stdint.h>
/* macros for checking INT_SRC registers upon interrupt */
#define LIS3DH_INT_SRC_X_LOW(c) ((c & 1) == 1)
#define LIS3DH_INT_SRC_X_HIGH(c) (((c >> 1) & 1) == 1)
#define LIS3DH_INT_SRC_Y_LOW(c) (((c >> 2) & 1) == 1)
#define LIS3DH_INT_SRC_Y_HIGH(c) (((c >> 3) & 1) == 1)
#define LIS3DH_INT_SRC_Z_LOW(c) (((c >> 4) & 1) == 1)
#define LIS3DH_INT_SRC_Z_HIGH(c) (((c >> 5) & 1) == 1)
/* macros for checking CLICK_SRC register upon interrupt */
#define LIS3DH_CLICK_SRC_X(c) ((c & 1) == 1) /* X high event */
#define LIS3DH_CLICK_SRC_Y(c) (((c >> 1) & 1) == 1) /* Y high event */
#define LIS3DH_CLICK_SRC_Z(c) (((c >> 2) & 1) == 1) /* Z high event */
#define LIS3DH_CLICK_SIGN(c) (((c >> 3) & 1) == 1) /* Click sign, 1 = positive */
#define LIS3DH_CLICK_SCLICK(c) (((c >> 4) & 1) == 1) /* Single-click det. enabled */
#define LIS3DH_CLICK_DCLICK(c) (((c >> 5) & 1) == 1) /* Double-click det. enabled */
/* rates */
/* all power modes */
#define LIS3DH_ODR_POWEROFF 0x00
@ -41,11 +57,29 @@
#define LIS3DH_FIFO_TRIG_INT2 0x01
/* filter modes */
/* this one is reset by reading REFERENCE (0x26) */
#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
/* Normal mode
* but reset by reading REFERENCE, instantly removes the DC component
*/
#define LIS3DH_FILTER_MODE_NORMAL_REF 0x00
/* Reference mode
* Output [x y z] data is calculated as the difference between the
* measured acceleration and the value stored in REFERENCE.
* signed 7-bit int, 1 LSb value depends on FS.
* FS_2G: ~ 16mg per 1 LSb
* FS_4G: ~ 31mg per 1 LSb
* FS_8G: ~ 63mg per 1 LSb
* FS_16G: ~127mg per 1 LSb
* */
#define LIS3DH_FILTER_MODE_REFERENCE 0x01
/* Normal mode
* Probably the same as LIS3DH_FILTER_MODE_NORMAL_REF
*/
#define LIS3DH_FILTER_MODE_NORMAL 0x02
/* Autoreset mode
* The filter is automatically reset upon configured interrupt event.
* It can be reset at any time by reading REFERENCE.
*/
#define LIS3DH_FILTER_MODE_AUTORESET 0x03
/* filter cutoff */
/* unfortunately, there is only a table for low-power mode,
@ -207,11 +241,19 @@ struct lis3dh_accel {
float z;
};
/* stores interrupt source registers read from the device */
struct lis3dh_interrupt_src {
uint8_t int1;
uint8_t int2;
uint8_t click;
};
struct lis3dh {
struct lis3dh_device dev;
struct lis3dh_config cfg;
struct lis3dh_accel acc;
struct lis3dh_adc adc;
struct lis3dh_device dev; /* fn ptrs to interface w/ device */
struct lis3dh_config cfg; /* config variables to write to device */
struct lis3dh_accel acc; /* accel data read from device (not FIFO) */
struct lis3dh_adc adc; /* adc and optionally temp read from device */
struct lis3dh_interrupt_src src; /* INT_SRC registers read from device */
};
typedef struct lis3dh lis3dh_t;
@ -231,8 +273,9 @@ int lis3dh_poll(lis3dh_t *lis3dh);
int lis3dh_read(lis3dh_t *lis3dh);
int lis3dh_poll_fifo(lis3dh_t *lis3dh);
int lis3dh_read_fifo(lis3dh_t *lis3dh, struct lis3dh_fifo_data *fifo);
int lis3dh_clear_int1(lis3dh_t *lis3dh);
int lis3dh_clear_int2(lis3dh_t *lis3dh);
int lis3dh_read_int1(lis3dh_t *lis3dh);
int lis3dh_read_int2(lis3dh_t *lis3dh);
int lis3dh_read_click(lis3dh_t *lis3dh);
int lis3dh_reference(lis3dh_t *lis3dh);
int lis3dh_reset(lis3dh_t *lis3dh);
int lis3dh_read_adc(lis3dh_t *lis3dh);

87
main.c
View File

@ -10,11 +10,6 @@
#define GPIO_INTERRUPT_PIN_INT1 12
#define GPIO_INTERRUPT_PIN_INT2 16
/* calc magnitude of accel [x y z] vector */
static float mag(float x, float y, float z) {
return sqrt( powf(x, 2) + powf(y, 2) + powf(z, 2) );
}
/* print message then exit */
static void quit(const char *msg, lis3dh_t *lis) {
lis->dev.deinit();
@ -25,8 +20,6 @@ static void quit(const char *msg, lis3dh_t *lis) {
int main() {
lis3dh_t lis;
struct lis3dh_fifo_data fifo;
int i, k;
/* set fn ptrs to rw on bus (i2c or SPI) */
lis.dev.init = i2c_init;
@ -51,58 +44,54 @@ int main() {
}
/* set up config */
lis.cfg.mode = LIS3DH_MODE_NORMAL;
lis.cfg.mode = LIS3DH_MODE_HR;
lis.cfg.range = LIS3DH_FS_2G;
lis.cfg.rate = LIS3DH_ODR_100_HZ;
lis.cfg.fifo.mode = LIS3DH_FIFO_MODE_STREAM;
lis.cfg.fifo.trig = LIS3DH_FIFO_TRIG_INT2;
lis.cfg.pin1.wtm = 1;
lis.cfg.pin1.latch = 1;
lis.cfg.filter.mode = LIS3DH_FILTER_MODE_AUTORESET;
lis.cfg.rate = LIS3DH_ODR_400_HZ;
lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL;
lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8;
lis.cfg.click.latch = 1;
lis.cfg.click.xs = 1;
lis.cfg.click.ys = 1;
lis.cfg.click.zs = 1;
lis.cfg.pin1.click = 1;
lis.cfg.en_adc = 1;
lis.cfg.en_temp = 1;
/* 1 LSb = 16 mg @ FS_2G
* so a 0.8g 'shock' is 800/16 = 50
*/
lis.cfg.click_ths = 50;
/* the 800 mg shock must be gone after 30 ms let's say .. */
/* Duration time is measured in N/ODR where:
* --- N = The content of the intX_dur integer
* --- ODR = the data rate, eg 100, 400...
* [ODR] [1 LSb in milliseconds]
* 400 2.5
*
* At 400 ODR, 30ms/2.5ms = 16
*/
lis.cfg.time_limit = 16;
/* write device config */
if (lis3dh_configure(&lis)) {
quit("configure()", &lis);
}
for(i=0; i<50; i++) {
/* wait for interrupt from LIS3DH */
if (int_poll(GPIO_INTERRUPT_PIN_INT1)) {
quit("int_poll()", &lis);
}
/* clear latched interrupt on INT1 */
if (lis3dh_clear_int1(&lis)) {
quit("clear_int1()", &lis);
}
/* read stored fifo data into `fifo' struct */
if (lis3dh_read_fifo(&lis, &fifo)) {
quit("read_fifo()", &lis);
}
/* read ADCs */
if (lis3dh_read_adc(&lis)) {
quit("read_adc()", &lis);
}
/* read temp from ADC3 and overwrite local ADC reading for ADC3 */
if (lis3dh_read_temp(&lis)) {
quit("read_temp()", &lis);
}
for(k=0; k<fifo.size; k++) {
printf("x: %04.04f y: %04.04f z: %04.04f mag: %04.04f ADC1:%.1f, ADC2:%.1f, ADC3:%.1f\n",
fifo.x[k], fifo.y[k], fifo.z[k],
mag(fifo.x[k], fifo.y[k], fifo.z[k]),
lis.adc.adc1, lis.adc.adc2, lis.adc.adc3);
}
if (int_poll(GPIO_INTERRUPT_PIN_INT1)) {
quit("int_poll()", &lis);
}
if (lis3dh_read_click(&lis)) {
quit("read_click()", &lis);
}
/* print data gathered from CLICK_SRC */
printf("Click: X=%d, Y=%d, Z=%d, Sign=%d, S_en=%d, D_en=%d\n",
LIS3DH_CLICK_SRC_X(lis.src.click),
LIS3DH_CLICK_SRC_Y(lis.src.click),
LIS3DH_CLICK_SRC_Z(lis.src.click),
LIS3DH_CLICK_SIGN(lis.src.click),
LIS3DH_CLICK_SCLICK(lis.src.click),
LIS3DH_CLICK_DCLICK(lis.src.click));
/* unregister interrupt */
if (int_unregister(GPIO_INTERRUPT_PIN_INT1)) {