2023-12-29 23:24:15 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
2023-12-21 17:31:12 +00:00
|
|
|
#include "lis3dh.h"
|
2023-12-21 20:52:17 +00:00
|
|
|
#include "registers.h"
|
2023-12-21 17:31:12 +00:00
|
|
|
|
2023-12-22 16:25:25 +00:00
|
|
|
/* reset user regs and reload trim params */
|
|
|
|
static int lis3dh_reset(lis3dh_t *lis3dh) {
|
2023-12-21 23:29:22 +00:00
|
|
|
int err = 0;
|
|
|
|
|
2023-12-22 16:25:25 +00:00
|
|
|
/* set BOOT bit so device reloads internal trim parameters */
|
2023-12-21 23:29:22 +00:00
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG5, 0x80);
|
2023-12-22 16:25:25 +00:00
|
|
|
|
|
|
|
/* write default values to rw regs */
|
2023-12-23 13:31:59 +00:00
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG0, 0x10);
|
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG1, 0x07);
|
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG2, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG3, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG4, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG5, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG6, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_FIFO_CTRL_REG, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_INT1_CFG, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_INT1_THS, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_INT1_DURATION, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_INT2_CFG, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_INT2_THS, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_INT2_DURATION, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_CLICK_CFG, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_CLICK_THS, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_TIME_LIMIT, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_TIME_LATENCY, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_TIME_WINDOW, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_ACT_THS, 0x00);
|
|
|
|
err |= lis3dh->dev.write(REG_ACT_DUR, 0x00);
|
2023-12-21 23:29:22 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-12-21 17:31:12 +00:00
|
|
|
int lis3dh_init(lis3dh_t *lis3dh) {
|
2023-12-21 20:52:17 +00:00
|
|
|
uint8_t result;
|
|
|
|
int err = 0;
|
|
|
|
|
2023-12-29 23:24:15 +00:00
|
|
|
/* if init has been given, check it */
|
|
|
|
if (lis3dh->dev.init != NULL) {
|
|
|
|
if (lis3dh->dev.init() != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
2023-12-21 23:29:22 +00:00
|
|
|
}
|
2023-12-21 20:52:17 +00:00
|
|
|
|
2023-12-29 23:42:15 +00:00
|
|
|
/* check WHO_AM_I to equal to 0x33 */
|
2023-12-21 20:52:17 +00:00
|
|
|
err |= lis3dh->dev.read(REG_WHO_AM_I, &result, 1);
|
|
|
|
if (result != 0x33) {
|
2023-12-22 10:22:43 +00:00
|
|
|
return 1;
|
2023-12-21 20:52:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-29 23:42:15 +00:00
|
|
|
/* zero device struct */
|
2023-12-22 10:22:43 +00:00
|
|
|
lis3dh->cfg.rate = 0;
|
|
|
|
lis3dh->cfg.range = 0;
|
|
|
|
lis3dh->cfg.mode = 0;
|
2023-12-23 18:38:28 +00:00
|
|
|
|
2023-12-22 16:25:25 +00:00
|
|
|
lis3dh->cfg.fifo.mode = 0xFF; /* in use if neq 0xFF */
|
2023-12-22 10:22:43 +00:00
|
|
|
lis3dh->cfg.fifo.trig = 0;
|
2023-12-28 18:40:09 +00:00
|
|
|
lis3dh->cfg.fifo.fth = 31; /* default watermark level. */
|
2023-12-23 18:38:28 +00:00
|
|
|
|
2023-12-29 23:42:15 +00:00
|
|
|
memset(&lis3dh->acc, 0, sizeof lis3dh->acc);
|
|
|
|
memset(&lis3dh->cfg.int1, 0, sizeof lis3dh->cfg.int1);
|
|
|
|
memset(&lis3dh->cfg.int2, 0, sizeof lis3dh->cfg.int2);
|
|
|
|
memset(&lis3dh->cfg.filter, 0, sizeof lis3dh->cfg.filter);
|
|
|
|
|
2023-12-22 17:00:53 +00:00
|
|
|
lis3dh->cfg.filter.mode = 0xFF; /* in use if neq 0xFF */
|
2023-12-23 15:34:20 +00:00
|
|
|
lis3dh->cfg.filter.fds = 1; /* bypass OFF by default */
|
2023-12-23 18:38:28 +00:00
|
|
|
|
2023-12-22 16:25:25 +00:00
|
|
|
err |= lis3dh_reset(lis3dh);
|
2023-12-21 23:29:22 +00:00
|
|
|
|
2023-12-21 20:52:17 +00:00
|
|
|
return err;
|
2023-12-21 17:31:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 23:29:22 +00:00
|
|
|
int lis3dh_configure(lis3dh_t *lis3dh) {
|
2023-12-23 18:38:28 +00:00
|
|
|
uint8_t ctrl_reg1, ctrl_reg2, ctrl_reg3;
|
|
|
|
uint8_t ctrl_reg4, ctrl_reg5, ctrl_reg6;
|
2023-12-22 10:22:43 +00:00
|
|
|
uint8_t fifo_ctrl_reg;
|
2023-12-23 18:38:28 +00:00
|
|
|
uint8_t ref; /* dummy */
|
2023-12-21 23:29:22 +00:00
|
|
|
int err = 0;
|
|
|
|
|
2023-12-23 18:38:28 +00:00
|
|
|
/* the 0x07 enables Z, Y and X axis in that order */
|
2023-12-29 23:24:15 +00:00
|
|
|
ctrl_reg1 = (lis3dh->cfg.rate << 4) | 0x07;
|
2023-12-22 17:00:53 +00:00
|
|
|
ctrl_reg2 = 0;
|
2023-12-23 18:38:28 +00:00
|
|
|
ctrl_reg3 = 0;
|
2023-12-29 23:24:15 +00:00
|
|
|
ctrl_reg4 = (lis3dh->cfg.range << 4);
|
2023-12-22 10:22:43 +00:00
|
|
|
ctrl_reg5 = 0;
|
2023-12-23 18:38:28 +00:00
|
|
|
ctrl_reg6 = 0;
|
2023-12-22 10:22:43 +00:00
|
|
|
fifo_ctrl_reg = 0;
|
|
|
|
|
2023-12-28 18:15:25 +00:00
|
|
|
/* set interrupt registers */
|
|
|
|
ctrl_reg3 |= (lis3dh->cfg.int1.click & 1) << 7;
|
|
|
|
ctrl_reg3 |= (lis3dh->cfg.int1.ia1 & 1) << 6;
|
|
|
|
ctrl_reg3 |= (lis3dh->cfg.int1.ia2 & 1) << 5;
|
|
|
|
ctrl_reg3 |= (lis3dh->cfg.int1.drdy_zyxda & 1) << 4;
|
|
|
|
ctrl_reg3 |= (lis3dh->cfg.int1.drdy_321 & 1) << 3;
|
|
|
|
ctrl_reg3 |= (lis3dh->cfg.int1.wtm & 1) << 2;
|
|
|
|
ctrl_reg3 |= (lis3dh->cfg.int1.overrun & 1) << 1;
|
|
|
|
|
|
|
|
ctrl_reg6 |= (lis3dh->cfg.int2.click & 1) << 7;
|
|
|
|
ctrl_reg6 |= (lis3dh->cfg.int2.ia1 & 1) << 6;
|
|
|
|
ctrl_reg6 |= (lis3dh->cfg.int2.ia2 & 1) << 5;
|
|
|
|
ctrl_reg6 |= (lis3dh->cfg.int2.boot & 1) << 4;
|
|
|
|
ctrl_reg6 |= (lis3dh->cfg.int2.act & 1) << 3;
|
|
|
|
ctrl_reg6 |= (lis3dh->cfg.int2.polarity & 1) << 1;
|
2023-12-23 18:38:28 +00:00
|
|
|
|
2023-12-29 17:26:54 +00:00
|
|
|
ctrl_reg5 |= (lis3dh->cfg.int1.latch & 1) << 3;
|
|
|
|
ctrl_reg5 |= (lis3dh->cfg.int2.latch & 1) << 1;
|
|
|
|
|
2023-12-22 10:22:43 +00:00
|
|
|
/* set enable FIFO */
|
2023-12-22 16:25:25 +00:00
|
|
|
if (lis3dh->cfg.fifo.mode != 0xFF) {
|
2023-12-23 18:38:28 +00:00
|
|
|
ctrl_reg5 |= 0x40; /* bit FIFO_EN */
|
|
|
|
|
|
|
|
/* restrict maximum fifo size */
|
|
|
|
if (lis3dh->cfg.fifo.fth > 32) {
|
|
|
|
lis3dh->cfg.fifo.fth = 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
fifo_ctrl_reg |= (lis3dh->cfg.fifo.fth);
|
2023-12-22 10:22:43 +00:00
|
|
|
fifo_ctrl_reg |= (lis3dh->cfg.fifo.mode << 6);
|
|
|
|
fifo_ctrl_reg |= ((lis3dh->cfg.fifo.trig & 1) << 5);
|
|
|
|
}
|
2023-12-21 23:29:22 +00:00
|
|
|
|
2023-12-22 17:00:53 +00:00
|
|
|
/* set enable filter */
|
|
|
|
if (lis3dh->cfg.filter.mode != 0xFF) {
|
2023-12-23 13:31:59 +00:00
|
|
|
ctrl_reg2 |= ((lis3dh->cfg.filter.mode & 0x03) << 6);
|
|
|
|
ctrl_reg2 |= ((lis3dh->cfg.filter.cutoff & 0x03) << 4);
|
2023-12-22 17:00:53 +00:00
|
|
|
ctrl_reg2 |= ((lis3dh->cfg.filter.fds & 1) << 3);
|
2023-12-23 18:38:28 +00:00
|
|
|
ctrl_reg2 |= ((lis3dh->cfg.filter.click & 1) << 2);
|
2023-12-22 17:00:53 +00:00
|
|
|
ctrl_reg2 |= ((lis3dh->cfg.filter.ia1 & 1) << 1);
|
|
|
|
ctrl_reg2 |= (lis3dh->cfg.filter.ia2 & 1);
|
|
|
|
}
|
|
|
|
|
2023-12-22 10:22:43 +00:00
|
|
|
/* always set block update */
|
2023-12-21 23:29:22 +00:00
|
|
|
ctrl_reg4 |= 0x80;
|
|
|
|
|
|
|
|
/* set high resolution */
|
|
|
|
if (lis3dh->cfg.mode == LIS3DH_MODE_HR) {
|
|
|
|
ctrl_reg4 |= 0x08;
|
|
|
|
}
|
|
|
|
|
2023-12-22 08:26:10 +00:00
|
|
|
/* set LPen */
|
|
|
|
if (lis3dh->cfg.mode == LIS3DH_MODE_LP) {
|
2023-12-23 13:31:59 +00:00
|
|
|
ctrl_reg1 |= 0x08;
|
2023-12-22 08:26:10 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 23:29:22 +00:00
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG1, ctrl_reg1);
|
2023-12-22 17:00:53 +00:00
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG2, ctrl_reg2);
|
2023-12-23 18:38:28 +00:00
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG3, ctrl_reg3);
|
2023-12-21 23:29:22 +00:00
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG4, ctrl_reg4);
|
2023-12-22 10:22:43 +00:00
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG5, ctrl_reg5);
|
2023-12-23 18:38:28 +00:00
|
|
|
err |= lis3dh->dev.write(REG_CTRL_REG6, ctrl_reg6);
|
2023-12-22 10:22:43 +00:00
|
|
|
err |= lis3dh->dev.write(REG_FIFO_CTRL_REG, fifo_ctrl_reg);
|
2023-12-21 23:29:22 +00:00
|
|
|
|
2023-12-22 08:26:10 +00:00
|
|
|
/* read REFERENCE to clear internal filter struct */
|
|
|
|
err |= lis3dh->dev.read(REG_REFERENCE, &ref, 1);
|
|
|
|
|
|
|
|
/* sleep for a period TBD ~ ODR */
|
2023-12-22 16:25:25 +00:00
|
|
|
lis3dh->dev.sleep(50000); /* 50 ms */
|
2023-12-21 23:29:22 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int lis3dh_poll(lis3dh_t *lis3dh) {
|
|
|
|
uint8_t status;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
do {
|
2023-12-22 10:22:43 +00:00
|
|
|
lis3dh->dev.sleep(1000); /* 1 ms */
|
2023-12-21 23:29:22 +00:00
|
|
|
err |= lis3dh->dev.read(REG_STATUS_REG, &status, 1);
|
|
|
|
} while (!err && !((status >> 3) & 1));
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-12-22 10:22:43 +00:00
|
|
|
int lis3dh_poll_fifo(lis3dh_t *lis3dh) {
|
|
|
|
uint8_t src;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
lis3dh->dev.sleep(1000); /* 1 ms */
|
|
|
|
err |= lis3dh->dev.read(REG_FIFO_SRC_REG, &src, 1);
|
|
|
|
} while (!err && !(src >> 6));
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-12-21 23:29:22 +00:00
|
|
|
/* the real size of the int you get back from reading the acc u16
|
|
|
|
depends on the power mode.
|
|
|
|
shift down the 16 bit word by this amount: */
|
2023-12-29 23:24:15 +00:00
|
|
|
static uint8_t acc_shift(uint8_t mode) {
|
|
|
|
switch (mode) {
|
|
|
|
case LIS3DH_MODE_HR: return 4; /* i12 */
|
|
|
|
case LIS3DH_MODE_NORMAL: return 6; /* i10 */
|
|
|
|
case LIS3DH_MODE_LP: return 8; /* i8 */
|
|
|
|
default: return 0;
|
2023-12-21 23:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns a scalar that when multiplied with axis reading
|
|
|
|
turns it to a multiple of mg. */
|
2023-12-29 23:24:15 +00:00
|
|
|
static uint8_t acc_sensitivity(uint8_t mode, uint8_t range) {
|
|
|
|
switch (range) {
|
|
|
|
case LIS3DH_FS_2G: return (mode == LIS3DH_MODE_LP) ? 16 : (mode == LIS3DH_MODE_NORMAL) ? 4 : 1;
|
|
|
|
case LIS3DH_FS_4G: return (mode == LIS3DH_MODE_LP) ? 32 : (mode == LIS3DH_MODE_NORMAL) ? 8 : 2;
|
|
|
|
case LIS3DH_FS_8G: return (mode == LIS3DH_MODE_LP) ? 64 : (mode == LIS3DH_MODE_NORMAL) ? 16 : 4;
|
|
|
|
case LIS3DH_FS_16G: return (mode == LIS3DH_MODE_LP) ? 192 : (mode == LIS3DH_MODE_NORMAL) ? 48 : 12;
|
|
|
|
default: return 0;
|
2023-12-21 23:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-29 23:24:15 +00:00
|
|
|
|
2023-12-21 23:29:22 +00:00
|
|
|
int lis3dh_read(lis3dh_t *lis3dh) {
|
|
|
|
uint8_t data[6];
|
2023-12-23 15:34:20 +00:00
|
|
|
int32_t x, y, z;
|
2023-12-21 23:29:22 +00:00
|
|
|
uint8_t scale, sens;
|
|
|
|
int err = 0;
|
|
|
|
|
2023-12-29 23:24:15 +00:00
|
|
|
scale = acc_shift(lis3dh->cfg.mode);
|
|
|
|
sens = acc_sensitivity(lis3dh->cfg.mode, lis3dh->cfg.range);
|
2023-12-21 23:29:22 +00:00
|
|
|
|
|
|
|
/* must set MSbit of the address to multi-read and
|
|
|
|
have the device auto-increment the address. */
|
|
|
|
err |= lis3dh->dev.read(REG_OUT_X_L | 0x80, data, 6);
|
|
|
|
|
2023-12-22 08:26:10 +00:00
|
|
|
/* x,y,z are now in mg */
|
2023-12-21 23:29:22 +00:00
|
|
|
x = (((int16_t)((data[0] << 8) | data[1])) >> scale) * sens;
|
|
|
|
y = (((int16_t)((data[2] << 8) | data[3])) >> scale) * sens;
|
|
|
|
z = (((int16_t)((data[4] << 8) | data[5])) >> scale) * sens;
|
|
|
|
|
2023-12-23 10:33:28 +00:00
|
|
|
lis3dh->acc.x = ((float)x) / 1000.0;
|
|
|
|
lis3dh->acc.y = ((float)y) / 1000.0;
|
|
|
|
lis3dh->acc.z = ((float)z) / 1000.0;
|
2023-12-21 23:29:22 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-12-23 10:28:43 +00:00
|
|
|
int lis3dh_read_fifo(lis3dh_t *lis3dh, struct lis3dh_fifo_data *fifo) {
|
2023-12-23 15:34:20 +00:00
|
|
|
int32_t x, y, z;
|
2023-12-22 10:22:43 +00:00
|
|
|
uint8_t scale, sens;
|
2023-12-23 10:28:43 +00:00
|
|
|
uint8_t data[192]; /* max size */
|
2023-12-22 10:22:43 +00:00
|
|
|
int err = 0;
|
2023-12-23 10:28:43 +00:00
|
|
|
int i, idx;
|
2023-12-22 10:22:43 +00:00
|
|
|
|
2023-12-26 07:42:55 +00:00
|
|
|
/* FIFO is always 10-bit */
|
|
|
|
scale = 6;
|
2023-12-29 23:24:15 +00:00
|
|
|
sens = acc_sensitivity(lis3dh->cfg.mode, lis3dh->cfg.range);
|
2023-12-22 10:22:43 +00:00
|
|
|
|
2023-12-23 15:34:20 +00:00
|
|
|
/* fifo buffer is max 32 */
|
|
|
|
fifo->size = lis3dh->cfg.fifo.fth > 32 ? 32 : lis3dh->cfg.fifo.fth;
|
2023-12-23 10:28:43 +00:00
|
|
|
|
2023-12-22 10:22:43 +00:00
|
|
|
/* must set MSbit of the address to multi-read and
|
|
|
|
have the device auto-increment the address.
|
|
|
|
see 5.1.5 in datasheet. */
|
|
|
|
err |= lis3dh->dev.read(REG_OUT_X_L | 0x80, data, 192);
|
|
|
|
|
2023-12-23 15:34:20 +00:00
|
|
|
for (i=0, idx=0; i<fifo->size * 6; i+=6, idx++) {
|
2023-12-22 10:22:43 +00:00
|
|
|
x = (((int16_t)((data[i + 0] << 8) | data[i + 1])) >> scale) * sens;
|
|
|
|
y = (((int16_t)((data[i + 2] << 8) | data[i + 3])) >> scale) * sens;
|
|
|
|
z = (((int16_t)((data[i + 4] << 8) | data[i + 5])) >> scale) * sens;
|
|
|
|
|
2023-12-23 10:28:43 +00:00
|
|
|
fifo->x[idx] = ((float)x) / 1000.0;
|
|
|
|
fifo->y[idx] = ((float)y) / 1000.0;
|
|
|
|
fifo->z[idx] = ((float)z) / 1000.0;
|
2023-12-22 10:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-12-29 23:24:15 +00:00
|
|
|
/* if NULL, this function doesn't have to be called */
|
2023-12-21 23:29:22 +00:00
|
|
|
int lis3dh_deinit(lis3dh_t *lis3dh) {
|
2023-12-29 23:24:15 +00:00
|
|
|
if (lis3dh->dev.deinit != NULL) {
|
|
|
|
return lis3dh->dev.deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2023-12-29 17:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 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);
|
2023-12-29 18:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read REFERENCE reg to reset HP filter in REFERENCE mode
|
|
|
|
it uses the --current-- acceleration as the base in the filter */
|
|
|
|
int lis3dh_reference(lis3dh_t *lis3dh) {
|
|
|
|
uint8_t res;
|
|
|
|
return lis3dh->dev.read(REG_REFERENCE, &res, 1);
|
2023-12-21 17:31:12 +00:00
|
|
|
}
|