more src reg interaction

This commit is contained in:
William Clark 2024-01-01 10:15:57 +00:00
parent 6352a65fec
commit d1ab5a8d99
3 changed files with 70 additions and 22 deletions

View File

@ -25,6 +25,7 @@ int lis3dh_init(lis3dh_t *lis3dh) {
memset(&lis3dh->acc, 0, sizeof lis3dh->acc); memset(&lis3dh->acc, 0, sizeof lis3dh->acc);
memset(&lis3dh->cfg, 0, sizeof lis3dh->cfg); memset(&lis3dh->cfg, 0, sizeof lis3dh->cfg);
memset(&lis3dh->adc, 0, sizeof lis3dh->adc); 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.mode = 0xFF; /* in use if neq 0xFF */
lis3dh->cfg.fifo.fth = 31; /* default watermark level. */ lis3dh->cfg.fifo.fth = 31; /* default watermark level. */
@ -161,6 +162,7 @@ int lis3dh_configure(lis3dh_t *lis3dh) {
} }
/* always set block update (BDU) */ /* always set block update (BDU) */
/* guarantees all bytes for one [x y z] set were sampled at the same time */
ctrl_reg4 |= 0x80; ctrl_reg4 |= 0x80;
/* set high resolution */ /* set high resolution */
@ -322,16 +324,19 @@ int lis3dh_deinit(lis3dh_t *lis3dh) {
return 0; return 0;
} }
/* read INT1_SRC to clear latched INT1 irq */ /* read INT1_SRC to clear interrupt active flag and get relevant data */
int lis3dh_clear_int1(lis3dh_t *lis3dh) { int lis3dh_read_int1(lis3dh_t *lis3dh) {
uint8_t res; return lis3dh->dev.read(REG_INT1_SRC, &lis3dh->src.int1, 1);
return lis3dh->dev.read(REG_INT1_SRC, &res, 1);
} }
/* read INT2_SRC to clear latched INT2 irq */ /* read INT2_SRC to clear interrupt active flag and get relevant data */
int lis3dh_clear_int2(lis3dh_t *lis3dh) { int lis3dh_read_int2(lis3dh_t *lis3dh) {
uint8_t res; return lis3dh->dev.read(REG_INT2_SRC, &lis3dh->src.int2, 1);
return lis3dh->dev.read(REG_INT2_SRC, &res, 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 /* 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 /* 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) { int lis3dh_read_adc(lis3dh_t *lis3dh) {
uint8_t data[6]; uint8_t data[6];
int err = 0; int err = 0;

View File

@ -3,6 +3,22 @@
#include <stdint.h> #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 */ /* rates */
/* all power modes */ /* all power modes */
#define LIS3DH_ODR_POWEROFF 0x00 #define LIS3DH_ODR_POWEROFF 0x00
@ -41,11 +57,29 @@
#define LIS3DH_FIFO_TRIG_INT2 0x01 #define LIS3DH_FIFO_TRIG_INT2 0x01
/* filter modes */ /* filter modes */
/* this one is reset by reading REFERENCE (0x26) */ /* Normal mode
#define LIS3DH_FILTER_MODE_NORMAL 0x00 * but reset by reading REFERENCE, instantly removes the DC component
#define LIS3DH_FILTER_MODE_REFERENCE 0x01 */
#define LIS3DH_FILTER_MODE_NORMAL2 0x02 /* same as 00? */ #define LIS3DH_FILTER_MODE_NORMAL_REF 0x00
#define LIS3DH_FILTER_MODE_AUTORESET 0x03 /* 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 */ /* filter cutoff */
/* unfortunately, there is only a table for low-power mode, /* unfortunately, there is only a table for low-power mode,
@ -207,11 +241,19 @@ struct lis3dh_accel {
float z; 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 {
struct lis3dh_device dev; struct lis3dh_device dev; /* fn ptrs to interface w/ device */
struct lis3dh_config cfg; struct lis3dh_config cfg; /* config variables to write to device */
struct lis3dh_accel acc; struct lis3dh_accel acc; /* accel data read from device (not FIFO) */
struct lis3dh_adc adc; 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; typedef struct lis3dh lis3dh_t;
@ -231,8 +273,9 @@ int lis3dh_poll(lis3dh_t *lis3dh);
int lis3dh_read(lis3dh_t *lis3dh); int lis3dh_read(lis3dh_t *lis3dh);
int lis3dh_poll_fifo(lis3dh_t *lis3dh); int lis3dh_poll_fifo(lis3dh_t *lis3dh);
int lis3dh_read_fifo(lis3dh_t *lis3dh, struct lis3dh_fifo_data *fifo); int lis3dh_read_fifo(lis3dh_t *lis3dh, struct lis3dh_fifo_data *fifo);
int lis3dh_clear_int1(lis3dh_t *lis3dh); int lis3dh_read_int1(lis3dh_t *lis3dh);
int lis3dh_clear_int2(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_reference(lis3dh_t *lis3dh);
int lis3dh_reset(lis3dh_t *lis3dh); int lis3dh_reset(lis3dh_t *lis3dh);
int lis3dh_read_adc(lis3dh_t *lis3dh); int lis3dh_read_adc(lis3dh_t *lis3dh);

4
main.c
View File

@ -58,7 +58,7 @@ int main() {
lis.cfg.fifo.trig = LIS3DH_FIFO_TRIG_INT2; lis.cfg.fifo.trig = LIS3DH_FIFO_TRIG_INT2;
lis.cfg.pin1.wtm = 1; lis.cfg.pin1.wtm = 1;
lis.cfg.pin1.latch = 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.filter.cutoff = LIS3DH_FILTER_CUTOFF_8;
lis.cfg.en_adc = 1; lis.cfg.en_adc = 1;
@ -76,7 +76,7 @@ int main() {
} }
/* clear latched interrupt on INT1 */ /* clear latched interrupt on INT1 */
if (lis3dh_clear_int1(&lis)) { if (lis3dh_read_int1(&lis)) {
quit("clear_int1()", &lis); quit("clear_int1()", &lis);
} }