lis3dh/main.c

141 lines
3.6 KiB
C
Raw Normal View History

2023-12-21 20:52:17 +00:00
#define _GNU_SOURCE
2023-12-21 18:17:20 +00:00
#include <stdio.h>
2023-12-23 13:31:59 +00:00
#include <stdlib.h>
2023-12-21 20:52:17 +00:00
#include <unistd.h>
2023-12-21 23:29:22 +00:00
#include <math.h>
2023-12-21 18:17:20 +00:00
#include "lis3dh.h"
2023-12-28 18:10:34 +00:00
#include "interrupt.h"
2023-12-21 20:52:17 +00:00
#include "i2c.h"
2023-12-21 18:17:20 +00:00
2023-12-30 13:10:40 +00:00
#define GPIO_INTERRUPT_PIN_INT1 12
2023-12-28 18:10:34 +00:00
2023-12-23 13:31:59 +00:00
/* print message then exit */
2023-12-29 23:24:15 +00:00
static void quit(const char *msg, lis3dh_t *lis) {
2023-12-23 13:31:59 +00:00
lis->dev.deinit();
fprintf(stderr, "%s\n", msg);
exit(1);
}
2023-12-21 18:17:20 +00:00
int main() {
lis3dh_t lis;
2023-12-23 13:31:59 +00:00
/* set fn ptrs to rw on bus (i2c or SPI) */
2023-12-21 20:52:17 +00:00
lis.dev.init = i2c_init;
lis.dev.read = i2c_read;
lis.dev.write = i2c_write;
lis.dev.sleep = usleep;
lis.dev.deinit = i2c_deinit;
/* initialise LIS3DH struct */
2023-12-22 08:26:10 +00:00
if (lis3dh_init(&lis)) {
2023-12-23 13:31:59 +00:00
quit("init()", &lis);
2023-12-21 23:29:22 +00:00
}
/* reset device because it sometimes corrupts itself */
if (lis3dh_reset(&lis)) {
quit("reset()", &lis);
}
2023-12-28 18:10:34 +00:00
/* register interrupt */
2023-12-30 13:10:40 +00:00
if (int_register(GPIO_INTERRUPT_PIN_INT1)) {
2023-12-28 18:10:34 +00:00
quit("int_register()", &lis);
}
2023-12-23 13:31:59 +00:00
/* set up config */
2024-01-01 11:05:45 +00:00
lis.cfg.mode = LIS3DH_MODE_HR;
2023-12-23 10:28:43 +00:00
lis.cfg.range = LIS3DH_FS_2G;
2024-01-02 11:00:49 +00:00
lis.cfg.rate = LIS3DH_ODR_400_HZ;
lis.cfg.pin1.ia1 = 1; /* allow INT1 through INT_PIN1 */
2024-01-01 11:05:45 +00:00
/* 1 LSb = 16 mg @ FS_2G
2024-01-02 11:00:49 +00:00
* 0.3g threshold = 300/16 = 18.75
* add read error, +40mg => 240/16 = 21.25 ~= 21
* if you for some reason don't want to use the HP filter,
* just add 1g to the threshold calculation.
2024-01-01 11:05:45 +00:00
*/
2024-01-02 11:00:49 +00:00
lis.cfg.int1_ths = 21;
2024-01-01 11:05:45 +00:00
2024-01-01 13:08:06 +00:00
/* Duration time is measured in N/ODR where:
2024-01-01 11:05:45 +00:00
* --- N = The content of the intX_dur integer
* --- ODR = the data rate, eg 100, 400...
* [ODR] [1 LSb in milliseconds]
* 400 2.5
*
2024-01-01 13:08:06 +00:00
* For ODR=400:
2024-01-02 11:00:49 +00:00
* 10 ms => 10/2.5 = 5
* lis.cfg.int1_dur = 5; <== 10 ms minimum duration to wake up
2024-01-01 11:05:45 +00:00
*/
2024-01-02 11:00:49 +00:00
lis.cfg.int1_dur = 0; /* instantaneous */
/* enable X_high, Y_high and Z_high */
lis.cfg.int1.yh = 1;
lis.cfg.int1.zh = 1;
lis.cfg.int1.xh = 1;
/* OR mode. Think about the axis combinations for AND mode */
lis.cfg.int1.aoi = 0; /* set to 1 for AND mode */
lis.cfg.int1.en_6d = 0;
/* latch interrupt. might not work. */
lis.cfg.int1.latch = 1;
/* set up a HP filter to ignore constant earth acceleration */
lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL_REF;
lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8;
lis.cfg.filter.ia1 = 1; /* enable filter for INT1 generator */
2023-12-31 15:24:40 +00:00
2023-12-23 13:31:59 +00:00
/* write device config */
2023-12-22 08:26:10 +00:00
if (lis3dh_configure(&lis)) {
2023-12-23 13:31:59 +00:00
quit("configure()", &lis);
2023-12-21 18:17:20 +00:00
}
2023-12-29 23:24:15 +00:00
2024-01-02 11:00:49 +00:00
/* read REFERENCE to set filter to current accel field */
if (lis3dh_reference(&lis)) {
quit("reference()", &lis);
}
/* read INT1_SRC to clear old interrupt if any */
if (lis3dh_read_int1(&lis)) {
quit("read_int1()", &lis);
}
for( ;; ) {
2024-01-01 11:05:45 +00:00
2024-01-02 11:00:49 +00:00
/* wait for INT1 to go active */
2024-01-01 13:08:06 +00:00
if (int_poll(GPIO_INTERRUPT_PIN_INT1)) {
quit("int_poll()", &lis);
}
2024-01-02 11:00:49 +00:00
/* read INT1_SRC */
if (lis3dh_read_int1(&lis)) {
quit("read_int1()", &lis);
2024-01-01 13:08:06 +00:00
}
2024-01-02 11:00:49 +00:00
/* print received interrupt .. */
printf("IA=%d ZH=%d ZL=%d YH=%d YL=%d XH=%d XL=%d\n",
2024-01-02 11:04:37 +00:00
!!(lis.src.int1 & 0x40),
2024-01-02 11:00:49 +00:00
LIS3DH_INT_SRC_Z_HIGH(lis.src.int1),
LIS3DH_INT_SRC_Z_LOW(lis.src.int1),
LIS3DH_INT_SRC_Y_HIGH(lis.src.int1),
LIS3DH_INT_SRC_Y_LOW(lis.src.int1),
LIS3DH_INT_SRC_X_HIGH(lis.src.int1),
LIS3DH_INT_SRC_X_LOW(lis.src.int1));
2024-01-01 13:08:06 +00:00
}
2023-12-28 18:10:34 +00:00
/* unregister interrupt */
2023-12-30 13:10:40 +00:00
if (int_unregister(GPIO_INTERRUPT_PIN_INT1)) {
2024-01-02 11:00:49 +00:00
quit("int_unregister()", &lis);
2023-12-23 13:31:59 +00:00
}
/* deinitalise struct */
2023-12-22 08:26:10 +00:00
if (lis3dh_deinit(&lis)) {
2023-12-23 13:31:59 +00:00
quit("deinit()", &lis);
2023-12-21 18:17:20 +00:00
}
return 0;
}