From d1ab5a8d991cee8899e8f9d3a1de297b17e6ba4b Mon Sep 17 00:00:00 2001 From: William Clark Date: Mon, 1 Jan 2024 10:15:57 +0000 Subject: [PATCH] more src reg interaction --- lis3dh.c | 23 ++++++++++++-------- lis3dh.h | 65 ++++++++++++++++++++++++++++++++++++++++++++++---------- main.c | 4 ++-- 3 files changed, 70 insertions(+), 22 deletions(-) diff --git a/lis3dh.c b/lis3dh.c index 945ea71..edeb370 100644 --- a/lis3dh.c +++ b/lis3dh.c @@ -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; diff --git a/lis3dh.h b/lis3dh.h index 2b8fc5f..f565a2b 100644 --- a/lis3dh.h +++ b/lis3dh.h @@ -3,6 +3,22 @@ #include +/* 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); diff --git a/main.c b/main.c index a2a6219..e696526 100644 --- a/main.c +++ b/main.c @@ -58,7 +58,7 @@ int main() { 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.filter.mode = LIS3DH_FILTER_MODE_NORMAL; lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8; lis.cfg.en_adc = 1; @@ -76,7 +76,7 @@ int main() { } /* clear latched interrupt on INT1 */ - if (lis3dh_clear_int1(&lis)) { + if (lis3dh_read_int1(&lis)) { quit("clear_int1()", &lis); }