Compare commits

..

2 Commits

Author SHA1 Message Date
b86e97662a revised examples 2024-01-02 14:57:30 +00:00
1a29e35097 rewrite macros and user configurable REFERENCE 2024-01-02 14:42:49 +00:00
5 changed files with 58 additions and 36 deletions

View File

@ -1,14 +1,14 @@
/* /*
* SCLICK SCLICK * SCLICK SCLICK
* _________ __________ * __________ ___________
* | | | | | * | | | | |
* | | | | | * | | | | |
* ----- ------------------ | ------ * ----- -------------------- | ------
* | * |
* TIME_LIMIT TIME_LIMIT | * TIME_LIMIT TIME_LIMIT |
* >---------< >----------<| * >---------< >--------< | ==> DCLICK_INT
* LATENCY WINDOW | * LATENCY WINDOW >----|---<
* >----------<>----------< | => DCLICK INT * >----------<>----------< LATENCY
* */ * */
#define _GNU_SOURCE #define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
@ -54,18 +54,20 @@ int main() {
lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL; 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.filter.click = 1; /* enable filtering for CLICK function */ lis.cfg.filter.click = 1; /* enable filtering for CLICK function */
lis.cfg.click.xd = 1; /* enable X axis double click */ lis.cfg.click.xd = 1; /* enable X axis double click */
lis.cfg.click.yd = 1; /* enable Y axis double click */ lis.cfg.click.yd = 1; /* enable Y axis double click */
lis.cfg.click.zd = 1; /* enable Z axis double click */ lis.cfg.click.zd = 1; /* enable Z axis double click */
lis.cfg.pin1.click = 1; /* enable click int src through pin1 */ lis.cfg.pin1.click = 1; /* enable click int src through pin1 */
lis.cfg.int1.latch = 1; /* latch interrupt until INT1_SRC is read */ lis.cfg.int1.latch = 1; /* latch interrupt until INT1_SRC is read */
/* 1 LSb = 16 mg @ FS_2G /* 1 LSb = 16 mg @ FS_2G
* so a 0.072g 'shock' is 72/16 = 4.5 * so a 0.3g 'shock' is 300/16 = 18.75
* However, the device can have up to +- 40mg read error * However, the device can have up to +- 40mg read error, so add 40mg
* 0.112g => 112/16 = 15 * 0.34g => 340/16 ~= 21
*/ */
lis.cfg.click_ths = 15; /* pretty sensitive */ lis.cfg.click_ths = 21; /* pretty sensitive */
/* Duration time is measured in N/ODR where: /* Duration time is measured in N/ODR where:
* --- N = The content of the intX_dur integer * --- N = The content of the intX_dur integer
@ -88,6 +90,11 @@ int main() {
/* error handling */ /* error handling */
} }
/* read CLICK_SRC to clear previous interrupts, if any */
if (lis3dh_read_click(&lis)) {
/* error handling */
}
for(;;) { for(;;) {
/* poll interrupt on INT1 pin */ /* poll interrupt on INT1 pin */
@ -100,10 +107,10 @@ int main() {
/* error handling */ /* error handling */
} }
/* only print data if DCLICK=1 in CLICK_SRC */ /* only print if DCLICK=1 */
if (LIS3DH_CLICK_DCLICK(lis.src.click)) { if (LIS3DH_CLICK_DCLICK(lis.src.click)) {
/* print data gathered from CLICK_SRC */ /* print data gathered from CLICK_SRC */
printf("Click: X=%d, Y=%d, Z=%d, Sign=%d, S_en=%d, D_en=%d\n", printf("Click: X=%d, Y=%d, Z=%d, Sign=%d, S_CLICK=%d, D_CLICK=%d\n",
LIS3DH_CLICK_SRC_X(lis.src.click), LIS3DH_CLICK_SRC_X(lis.src.click),
LIS3DH_CLICK_SRC_Y(lis.src.click), LIS3DH_CLICK_SRC_Y(lis.src.click),
LIS3DH_CLICK_SRC_Z(lis.src.click), LIS3DH_CLICK_SRC_Z(lis.src.click),
@ -111,6 +118,10 @@ int main() {
LIS3DH_CLICK_SCLICK(lis.src.click), LIS3DH_CLICK_SCLICK(lis.src.click),
LIS3DH_CLICK_DCLICK(lis.src.click)); LIS3DH_CLICK_DCLICK(lis.src.click));
} }
/* sleep for 5 ms because gpio sysfs is slow at clearing interrupts */
/* not necessary with "real" IRQ */
usleep(5000);
} }
/* unregister interrupt */ /* unregister interrupt */

View File

@ -45,7 +45,7 @@ int main() {
/* 1 LSb = 16 mg @ FS_2G /* 1 LSb = 16 mg @ FS_2G
* 0.3g threshold = 300/16 = 18.75 * 0.3g threshold = 300/16 = 18.75
* add read error, +40mg => 240/16 = 21.25 ~= 21 * add read error, +40mg => 340/16 = 21.25 ~= 21
* if you for some reason don't want to use the HP filter, * if you for some reason don't want to use the HP filter,
* just add 1g to the threshold calculation. * just add 1g to the threshold calculation.
*/ */
@ -72,8 +72,7 @@ int main() {
lis.cfg.int1.aoi = 0; /* set to 1 for AND mode */ lis.cfg.int1.aoi = 0; /* set to 1 for AND mode */
lis.cfg.int1.en_6d = 0; lis.cfg.int1.en_6d = 0;
/* latch interrupt */
/* latch interrupt. might not work. */
lis.cfg.int1.latch = 1; lis.cfg.int1.latch = 1;
/* set up a HP filter to ignore constant earth acceleration */ /* set up a HP filter to ignore constant earth acceleration */
@ -81,7 +80,6 @@ int main() {
lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8; lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8;
lis.cfg.filter.ia1 = 1; /* enable filter for INT1 generator */ lis.cfg.filter.ia1 = 1; /* enable filter for INT1 generator */
/* write device config */ /* write device config */
if (lis3dh_configure(&lis)) { if (lis3dh_configure(&lis)) {
/* error handling */ /* error handling */
@ -111,13 +109,17 @@ int main() {
/* print received interrupt .. */ /* print received interrupt .. */
printf("IA=%d ZH=%d ZL=%d YH=%d YL=%d XH=%d XL=%d\n", printf("IA=%d ZH=%d ZL=%d YH=%d YL=%d XH=%d XL=%d\n",
!!(lis.src.int1 & 0x40), LIS3DH_INT_SRC_IA(lis.src.int1),
LIS3DH_INT_SRC_Z_HIGH(lis.src.int1), LIS3DH_INT_SRC_Z_HIGH(lis.src.int1),
LIS3DH_INT_SRC_Z_LOW(lis.src.int1), LIS3DH_INT_SRC_Z_LOW(lis.src.int1),
LIS3DH_INT_SRC_Y_HIGH(lis.src.int1), LIS3DH_INT_SRC_Y_HIGH(lis.src.int1),
LIS3DH_INT_SRC_Y_LOW(lis.src.int1), LIS3DH_INT_SRC_Y_LOW(lis.src.int1),
LIS3DH_INT_SRC_X_HIGH(lis.src.int1), LIS3DH_INT_SRC_X_HIGH(lis.src.int1),
LIS3DH_INT_SRC_X_LOW(lis.src.int1)); LIS3DH_INT_SRC_X_LOW(lis.src.int1));
/* sleep for 5 ms because gpio sysfs is slow at clearing interrupts */
/* not necessary with "real" IRQ */
usleep(5000);
} }
/* unregister interrupt */ /* unregister interrupt */

View File

@ -199,6 +199,7 @@ int lis3dh_configure(lis3dh_t *lis3dh) {
err |= lis3dh->dev.write(REG_ACT_THS, act_ths); err |= lis3dh->dev.write(REG_ACT_THS, act_ths);
err |= lis3dh->dev.write(REG_ACT_DUR, lis3dh->cfg.act_dur); err |= lis3dh->dev.write(REG_ACT_DUR, lis3dh->cfg.act_dur);
err |= lis3dh->dev.write(REG_TEMP_CFG_REG, temp_cfg_reg); err |= lis3dh->dev.write(REG_TEMP_CFG_REG, temp_cfg_reg);
err |= lis3dh->dev.write(REG_REFERENCE, lis3dh->cfg.reference);
err |= lis3dh->dev.write(REG_CTRL_REG0, ctrl_reg0); err |= lis3dh->dev.write(REG_CTRL_REG0, ctrl_reg0);
err |= lis3dh->dev.write(REG_CTRL_REG1, ctrl_reg1); err |= lis3dh->dev.write(REG_CTRL_REG1, ctrl_reg1);
@ -287,6 +288,7 @@ int lis3dh_read(lis3dh_t *lis3dh) {
return err; return err;
} }
/* assume fifo has been configured and poll'd */
int lis3dh_read_fifo(lis3dh_t *lis3dh, struct lis3dh_fifo_data *fifo) { int lis3dh_read_fifo(lis3dh_t *lis3dh, struct lis3dh_fifo_data *fifo) {
int32_t x, y, z; int32_t x, y, z;
uint8_t scale, sens; uint8_t scale, sens;

View File

@ -4,20 +4,21 @@
#include <stdint.h> #include <stdint.h>
/* macros for checking INT_SRC registers upon interrupt */ /* macros for checking INT_SRC registers upon interrupt */
#define LIS3DH_INT_SRC_X_LOW(c) ((c & 1) == 1) #define LIS3DH_INT_SRC_X_LOW(c) (!!(c & 0x01)) /* X axis low */
#define LIS3DH_INT_SRC_X_HIGH(c) (((c >> 1) & 1) == 1) #define LIS3DH_INT_SRC_X_HIGH(c) (!!(c & 0x02)) /* X axis high */
#define LIS3DH_INT_SRC_Y_LOW(c) (((c >> 2) & 1) == 1) #define LIS3DH_INT_SRC_Y_LOW(c) (!!(c & 0x04)) /* Y axis low */
#define LIS3DH_INT_SRC_Y_HIGH(c) (((c >> 3) & 1) == 1) #define LIS3DH_INT_SRC_Y_HIGH(c) (!!(c & 0x08)) /* Y axis high */
#define LIS3DH_INT_SRC_Z_LOW(c) (((c >> 4) & 1) == 1) #define LIS3DH_INT_SRC_Z_LOW(c) (!!(c & 0x10)) /* Z axis low */
#define LIS3DH_INT_SRC_Z_HIGH(c) (((c >> 5) & 1) == 1) #define LIS3DH_INT_SRC_Z_HIGH(c) (!!(c & 0x20)) /* Z axis high */
#define LIS3DH_INT_SRC_IA(c) (!!(c & 0x40)) /* Interrupt active */
/* macros for checking CLICK_SRC register upon interrupt */ /* macros for checking CLICK_SRC register upon interrupt */
#define LIS3DH_CLICK_SRC_X(c) ((c & 1) == 1) /* X high event */ #define LIS3DH_CLICK_SRC_X(c) (!!(c & 0x01)) /* X high event */
#define LIS3DH_CLICK_SRC_Y(c) (((c >> 1) & 1) == 1) /* Y high event */ #define LIS3DH_CLICK_SRC_Y(c) (!!(c & 0x02)) /* Y high event */
#define LIS3DH_CLICK_SRC_Z(c) (((c >> 2) & 1) == 1) /* Z high event */ #define LIS3DH_CLICK_SRC_Z(c) (!!(c & 0x04)) /* Z high event */
#define LIS3DH_CLICK_SIGN(c) (((c >> 3) & 1) == 1) /* Click sign, 1 = positive */ #define LIS3DH_CLICK_SIGN(c) (!!(c & 0x08)) /* Click sign, 1 = positive */
#define LIS3DH_CLICK_SCLICK(c) (((c >> 4) & 1) == 1) /* Single-click det. enabled */ #define LIS3DH_CLICK_SCLICK(c) (!!(c & 0x10)) /* Single-click det. enabled */
#define LIS3DH_CLICK_DCLICK(c) (((c >> 5) & 1) == 1) /* Double-click det. enabled */ #define LIS3DH_CLICK_DCLICK(c) (!!(c & 0x20)) /* Double-click det. enabled */
/* rates */ /* rates */
/* all power modes */ /* all power modes */
@ -126,7 +127,7 @@ struct lis3dh_int_config {
uint8_t latch; /* active until INT1_SRC/INT2_SRC is read */ uint8_t latch; /* active until INT1_SRC/INT2_SRC is read */
}; };
/* config for INT2 trigger output */ /* config for interrupt output pin #2 */
struct lis3dh_int_pin2_config { struct lis3dh_int_pin2_config {
uint8_t click; /* CLICK interrupt */ uint8_t click; /* CLICK interrupt */
uint8_t ia1; /* IA1 interrupt */ uint8_t ia1; /* IA1 interrupt */
@ -136,7 +137,7 @@ struct lis3dh_int_pin2_config {
uint8_t polarity; /* INT1 & INT2 polarity. 0 active high, 1 active low */ uint8_t polarity; /* INT1 & INT2 polarity. 0 active high, 1 active low */
}; };
/* config for INT1 trigger output */ /* config for interrupt output pin #1 */
struct lis3dh_int_pin1_config { struct lis3dh_int_pin1_config {
uint8_t click; /* CLICK interrupt */ uint8_t click; /* CLICK interrupt */
uint8_t ia1; /* IA1 interrupt */ uint8_t ia1; /* IA1 interrupt */
@ -221,6 +222,8 @@ struct lis3dh_config {
uint8_t sdo_pullup; /* Use pull-up on SDO. default 0 use */ uint8_t sdo_pullup; /* Use pull-up on SDO. default 0 use */
uint8_t en_adc; /* enable ADC */ uint8_t en_adc; /* enable ADC */
uint8_t en_temp; /* enable temp sensor on ADC 3 */ uint8_t en_temp; /* enable temp sensor on ADC 3 */
uint8_t reference; /* HP filter reference */
}; };
/* reads from internal ADCs. /* reads from internal ADCs.

6
main.c
View File

@ -118,13 +118,17 @@ int main() {
/* print received interrupt .. */ /* print received interrupt .. */
printf("IA=%d ZH=%d ZL=%d YH=%d YL=%d XH=%d XL=%d\n", printf("IA=%d ZH=%d ZL=%d YH=%d YL=%d XH=%d XL=%d\n",
!!(lis.src.int1 & 0x40), LIS3DH_INT_SRC_IA(lis.src.int1),
LIS3DH_INT_SRC_Z_HIGH(lis.src.int1), LIS3DH_INT_SRC_Z_HIGH(lis.src.int1),
LIS3DH_INT_SRC_Z_LOW(lis.src.int1), LIS3DH_INT_SRC_Z_LOW(lis.src.int1),
LIS3DH_INT_SRC_Y_HIGH(lis.src.int1), LIS3DH_INT_SRC_Y_HIGH(lis.src.int1),
LIS3DH_INT_SRC_Y_LOW(lis.src.int1), LIS3DH_INT_SRC_Y_LOW(lis.src.int1),
LIS3DH_INT_SRC_X_HIGH(lis.src.int1), LIS3DH_INT_SRC_X_HIGH(lis.src.int1),
LIS3DH_INT_SRC_X_LOW(lis.src.int1)); LIS3DH_INT_SRC_X_LOW(lis.src.int1));
/* sleep for 5 ms because gpio sysfs is slow at clearing interrupts */
/* not necessary with "real" IRQ */
usleep(5000);
} }
/* unregister interrupt */ /* unregister interrupt */