Compare commits

..

No commits in common. "1a54ce4e5e29bee723f33a066345ab50585cd019" and "6352a65fec237829587370f31225d0d00f5b7ab6" have entirely different histories.

3 changed files with 68 additions and 105 deletions

View File

@ -25,7 +25,6 @@ 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. */
@ -162,7 +161,6 @@ 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 */
@ -324,19 +322,16 @@ int lis3dh_deinit(lis3dh_t *lis3dh) {
return 0; return 0;
} }
/* read INT1_SRC to clear interrupt active flag and get relevant data */ /* read INT1_SRC to clear latched INT1 irq */
int lis3dh_read_int1(lis3dh_t *lis3dh) { int lis3dh_clear_int1(lis3dh_t *lis3dh) {
return lis3dh->dev.read(REG_INT1_SRC, &lis3dh->src.int1, 1); uint8_t res;
return lis3dh->dev.read(REG_INT1_SRC, &res, 1);
} }
/* read INT2_SRC to clear interrupt active flag and get relevant data */ /* read INT2_SRC to clear latched INT2 irq */
int lis3dh_read_int2(lis3dh_t *lis3dh) { int lis3dh_clear_int2(lis3dh_t *lis3dh) {
return lis3dh->dev.read(REG_INT2_SRC, &lis3dh->src.int2, 1); uint8_t res;
} 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
@ -380,7 +375,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 float count is equal to 1 millivolt */ st 1 LSb 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,22 +3,6 @@
#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
@ -57,29 +41,11 @@
#define LIS3DH_FIFO_TRIG_INT2 0x01 #define LIS3DH_FIFO_TRIG_INT2 0x01
/* filter modes */ /* filter modes */
/* Normal mode /* this one is reset by reading REFERENCE (0x26) */
* but reset by reading REFERENCE, instantly removes the DC component #define LIS3DH_FILTER_MODE_NORMAL 0x00
*/ #define LIS3DH_FILTER_MODE_REFERENCE 0x01
#define LIS3DH_FILTER_MODE_NORMAL_REF 0x00 #define LIS3DH_FILTER_MODE_NORMAL2 0x02 /* same as 00? */
/* Reference mode #define LIS3DH_FILTER_MODE_AUTORESET 0x03
* 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,
@ -241,19 +207,11 @@ 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; /* fn ptrs to interface w/ device */ struct lis3dh_device dev;
struct lis3dh_config cfg; /* config variables to write to device */ struct lis3dh_config cfg;
struct lis3dh_accel acc; /* accel data read from device (not FIFO) */ struct lis3dh_accel acc;
struct lis3dh_adc adc; /* adc and optionally temp read from device */ struct lis3dh_adc adc;
struct lis3dh_interrupt_src src; /* INT_SRC registers read from device */
}; };
typedef struct lis3dh lis3dh_t; typedef struct lis3dh lis3dh_t;
@ -273,9 +231,8 @@ 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_read_int1(lis3dh_t *lis3dh); int lis3dh_clear_int1(lis3dh_t *lis3dh);
int lis3dh_read_int2(lis3dh_t *lis3dh); int lis3dh_clear_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);

85
main.c
View File

@ -10,6 +10,11 @@
#define GPIO_INTERRUPT_PIN_INT1 12 #define GPIO_INTERRUPT_PIN_INT1 12
#define GPIO_INTERRUPT_PIN_INT2 16 #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 */ /* print message then exit */
static void quit(const char *msg, lis3dh_t *lis) { static void quit(const char *msg, lis3dh_t *lis) {
lis->dev.deinit(); lis->dev.deinit();
@ -20,6 +25,8 @@ static void quit(const char *msg, lis3dh_t *lis) {
int main() { int main() {
lis3dh_t lis; lis3dh_t lis;
struct lis3dh_fifo_data fifo;
int i, k;
/* set fn ptrs to rw on bus (i2c or SPI) */ /* set fn ptrs to rw on bus (i2c or SPI) */
lis.dev.init = i2c_init; lis.dev.init = i2c_init;
@ -44,54 +51,58 @@ int main() {
} }
/* set up config */ /* set up config */
lis.cfg.mode = LIS3DH_MODE_HR; lis.cfg.mode = LIS3DH_MODE_NORMAL;
lis.cfg.range = LIS3DH_FS_2G; lis.cfg.range = LIS3DH_FS_2G;
lis.cfg.rate = LIS3DH_ODR_400_HZ; lis.cfg.rate = LIS3DH_ODR_100_HZ;
lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL; 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.filter.cutoff = LIS3DH_FILTER_CUTOFF_8; 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;
/* 1 LSb = 16 mg @ FS_2G lis.cfg.en_adc = 1;
* so a 0.8g 'shock' is 800/16 = 50 lis.cfg.en_temp = 1;
*/
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 */ /* write device config */
if (lis3dh_configure(&lis)) { if (lis3dh_configure(&lis)) {
quit("configure()", &lis); quit("configure()", &lis);
} }
if (int_poll(GPIO_INTERRUPT_PIN_INT1)) { for(i=0; i<50; i++) {
quit("int_poll()", &lis); /* wait for interrupt from LIS3DH */
} if (int_poll(GPIO_INTERRUPT_PIN_INT1)) {
quit("int_poll()", &lis);
}
if (lis3dh_read_click(&lis)) { /* clear latched interrupt on INT1 */
quit("read_click()", &lis); if (lis3dh_clear_int1(&lis)) {
} quit("clear_int1()", &lis);
}
/* print data gathered from CLICK_SRC */ /* read stored fifo data into `fifo' struct */
printf("Click: X=%d, Y=%d, Z=%d, Sign=%d, S_en=%d, D_en=%d\n", if (lis3dh_read_fifo(&lis, &fifo)) {
LIS3DH_CLICK_SRC_X(lis.src.click), quit("read_fifo()", &lis);
LIS3DH_CLICK_SRC_Y(lis.src.click), }
LIS3DH_CLICK_SRC_Z(lis.src.click),
LIS3DH_CLICK_SIGN(lis.src.click), /* read ADCs */
LIS3DH_CLICK_SCLICK(lis.src.click), if (lis3dh_read_adc(&lis)) {
LIS3DH_CLICK_DCLICK(lis.src.click)); 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);
}
}
/* unregister interrupt */ /* unregister interrupt */
if (int_unregister(GPIO_INTERRUPT_PIN_INT1)) { if (int_unregister(GPIO_INTERRUPT_PIN_INT1)) {