This commit is contained in:
William Clark 2024-08-19 01:27:16 +01:00
parent 9a486ad037
commit 2c4b1dbbe5
5 changed files with 104 additions and 47 deletions

View File

@ -1,17 +1,29 @@
CC=gcc
OPT=-O2 -std=c89 -Wall -Wextra -W -pedantic
CFLAGS=-I. $(OPT)
CFILES=$(wildcard ./*.c)
OBJECTS=$(patsubst %.c,%.o, $(CFILES))
BINARY=lis3dh
CFLAGS = -g -O0 -Wall -Wextra -Wpedantic -Wshadow -Wformat=2 -Wfloat-equal \
-Wconversion -Wcast-align -Wpointer-arith -Wstrict-overflow=5 \
-Wwrite-strings -Wcast-qual -Wswitch-default -Wswitch-enum \
-Wunreachable-code -Wstrict-prototypes -Wmissing-prototypes \
-Wmissing-declarations -Wredundant-decls -Wold-style-definition \
-Winline -Wlogical-op -Wjump-misses-init -Wdouble-promotion \
-Wformat-overflow=2 -Wformat-signedness -Wmissing-include-dirs \
-Wnull-dereference -Wstack-usage=1024 -Wpacked -Woverlength-strings \
-Wvla -Walloc-zero -Wduplicated-cond -Wduplicated-branches -Wrestrict \
-fanalyzer \
-I. -std=c89
all: $(BINARY)
CFILES = $(wildcard *.c)
OBJECTS = $(CFILES:.c=.o)
BIN = lis3dh
$(BINARY): $(OBJECTS)
$(CC) $^ -o $@
all: $(BIN)
$(BIN): $(OBJECTS)
@echo ">>> $@"
@$(CC) $(CFLAGS) $^ -o $@
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
@echo "cc $<"
@$(CC) $(CFLAGS) -c $< -o $@
clean:
@rm -rf $(OBJECTS) $(BINARY)
@rm -rf $(OBJECTS)

View File

@ -60,6 +60,19 @@ int lis3dh_fifo_reset(lis3dh_t *lis3dh);
```
All functions return `0` on success, and any non-zero value on error.
## Raspberry Pi
First run `$ sudo raspi-config` and enable SPI and/or I2C.
Edit `main.c` to use I2C or SPI.
See `i2c.c` and `spi.c` for which pins/device to use.
```sh
$ git clone https://github.com/wrclark/lis3dh.git
$ cd lis3dh
$ make
$ ./lis3dh
```
## STM32
### I2C
```c

View File

@ -40,7 +40,7 @@ static int write_file(const char *path, const char *str) {
*/
int int_register(int pin) {
char path[1024];
char path[256];
char buffer[64];
/* begin by unregistering supplied pin, just in case */
@ -55,13 +55,13 @@ int int_register(int pin) {
/* sleep 500 ms to let linux set up the dir .. */
usleep(500000);
memset(path, 0, 1024);
memset(path, 0, 256);
sprintf(path, "%s%d/%s", "/sys/class/gpio/gpio", pin, "edge");
if (write_file(path, "both") != 0) {
return 1;
}
memset(path, 0, 1024);
memset(path, 0, 256);
sprintf(path, "%s%d/%s", "/sys/class/gpio/gpio", pin, "direction");
if (write_file(path, "in") != 0) {
return 1;
@ -95,10 +95,10 @@ int int_unregister(int pin) {
int int_poll(int pin) {
int fd;
struct pollfd pfd;
char path[1024];
char path[256];
char buf[16];
memset(path, 0, 1024);
memset(path, 0, 256);
sprintf(path, "%s%d/%s", "/sys/class/gpio/gpio", pin, "value");
if ((fd = open(path, O_RDONLY)) == 0) {

View File

@ -1,6 +1,5 @@
#include <stddef.h> /* NULL */
#include <string.h> /* memset() */
#include <assert.h>
#include "lis3dh.h"
#include "registers.h"
@ -9,7 +8,9 @@ int lis3dh_init(lis3dh_t *lis3dh) {
uint8_t result;
int err = 0;
assert(lis3dh);
if (!lis3dh) {
return 1;
}
/* if init has been given, check it */
if (lis3dh->dev.init != NULL) {
@ -51,7 +52,9 @@ int lis3dh_configure(lis3dh_t *lis3dh) {
uint8_t time_limit, act_ths;
int err = 0;
assert(lis3dh);
if (!lis3dh) {
return 1;
}
/* the 0x07 enables Z, Y and X axis in that order */
ctrl_reg1 = (lis3dh->cfg.rate << 4) | 0x07;
@ -245,7 +248,9 @@ int lis3dh_read(lis3dh_t *lis3dh) {
uint8_t shift, sens, status;
int err = 0;
assert(lis3dh);
if (!lis3dh) {
return 1;
}
shift = acc_shift(lis3dh->cfg.mode);
sens = acc_sensitivity(lis3dh->cfg.mode, lis3dh->cfg.range);
@ -258,9 +263,9 @@ int lis3dh_read(lis3dh_t *lis3dh) {
} while ((!LIS3DH_STATUS_ZYXDA(status) || !LIS3DH_STATUS_ZYXOR(status)) && !err);
err |= lis3dh->dev.read(REG_OUT_X_L, data, 6);
lis3dh->acc.x = ((int16_t)(data[1] | data[0] << 8) >> shift) * sens;
lis3dh->acc.y = ((int16_t)(data[3] | data[2] << 8) >> shift) * sens;
lis3dh->acc.z = ((int16_t)(data[5] | data[4] << 8) >> shift) * sens;
lis3dh->acc.x = (int16_t)((int16_t)(data[1] | data[0] << 8) >> shift) * sens;
lis3dh->acc.y = (int16_t)((int16_t)(data[3] | data[2] << 8) >> shift) * sens;
lis3dh->acc.z = (int16_t)((int16_t)(data[5] | data[4] << 8) >> shift) * sens;
return err;
}
@ -270,12 +275,12 @@ int lis3dh_read(lis3dh_t *lis3dh) {
/* read groups of the 6 OUT bytes until EMPTY flag in FIFO_SRC is set */
int lis3dh_read_fifo(lis3dh_t *lis3dh, struct lis3dh_fifo_data *fifo) {
uint8_t data[6];
uint8_t sens, fifo_src;
uint8_t sens, fifo_src, idx = 0;
int err = 0;
int idx = 0;
assert(lis3dh);
assert(fifo);
if (!lis3dh || !fifo) {
return 1;
}
/* wait until there is at least 1 unread sample in the FIFO */
@ -305,7 +310,9 @@ int lis3dh_read_fifo(lis3dh_t *lis3dh, struct lis3dh_fifo_data *fifo) {
/* if NULL, this function doesn't have to be called */
int lis3dh_deinit(lis3dh_t *lis3dh) {
assert(lis3dh);
if (!lis3dh) {
return 1;
}
if (lis3dh->dev.deinit != NULL) {
return lis3dh->dev.deinit();
@ -316,19 +323,28 @@ int lis3dh_deinit(lis3dh_t *lis3dh) {
/* read INT1_SRC to clear interrupt active flag and get relevant data */
int lis3dh_read_int1(lis3dh_t *lis3dh) {
assert(lis3dh);
if (!lis3dh) {
return 1;
}
return lis3dh->dev.read(REG_INT1_SRC, &lis3dh->src.int1, 1);
}
/* read INT2_SRC to clear interrupt active flag and get relevant data */
int lis3dh_read_int2(lis3dh_t *lis3dh) {
assert(lis3dh);
if (!lis3dh) {
return 1;
}
return lis3dh->dev.read(REG_INT2_SRC, &lis3dh->src.int2, 1);
}
/* read CLICK_SRC to clear interrupt active flag and get relevant data */
int lis3dh_read_click(lis3dh_t *lis3dh) {
assert(lis3dh);
if (!lis3dh) {
return 1;
}
return lis3dh->dev.read(REG_CLICK_SRC, &lis3dh->src.click, 1);
}
@ -336,7 +352,11 @@ int lis3dh_read_click(lis3dh_t *lis3dh) {
/* it then uses the --current-- acceleration as the base in the filter */
int lis3dh_reference(lis3dh_t *lis3dh) {
uint8_t res;
assert(lis3dh);
if (!lis3dh) {
return 1;
}
return lis3dh->dev.read(REG_REFERENCE, &res, 1);
}
@ -344,7 +364,9 @@ int lis3dh_reference(lis3dh_t *lis3dh) {
int lis3dh_reset(lis3dh_t *lis3dh) {
int err = 0;
assert(lis3dh);
if (!lis3dh) {
return 1;
}
/* set BOOT bit so device reloads internal trim parameters */
err |= lis3dh->dev.write(REG_CTRL_REG5, 0x80);
@ -391,15 +413,17 @@ int lis3dh_read_adc(lis3dh_t *lis3dh) {
uint8_t shift;
int err = 0;
assert(lis3dh);
if (!lis3dh) {
return 1;
}
err |= lis3dh->dev.read(REG_OUT_ADC1_L, data, 6);
shift = (lis3dh->cfg.mode == LIS3DH_MODE_LP) ? 8 : 6;
lis3dh->adc.adc1 = 1200 + ((800 * ((int16_t)(data[1] | data[0] << 8) >> shift)) >> (16 - shift));
lis3dh->adc.adc2 = 1200 + ((800 * ((int16_t)(data[3] | data[2] << 8) >> shift)) >> (16 - shift));
lis3dh->adc.adc3 = 1200 + ((800 * ((int16_t)(data[5] | data[4] << 8) >> shift)) >> (16 - shift));
lis3dh->adc.adc1 = (int16_t)(1200 + ((800 * ((int16_t)(data[1] | data[0] << 8) >> shift)) >> (16 - shift)));
lis3dh->adc.adc2 = (int16_t)(1200 + ((800 * ((int16_t)(data[3] | data[2] << 8) >> shift)) >> (16 - shift)));
lis3dh->adc.adc3 = (int16_t)(1200 + ((800 * ((int16_t)(data[5] | data[4] << 8) >> shift)) >> (16 - shift)));
return err;
}
@ -415,7 +439,9 @@ int lis3dh_read_temp(lis3dh_t *lis3dh) {
uint8_t data;
int err = 0;
assert(lis3dh);
if (!lis3dh) {
return 1;
}
err |= lis3dh->dev.read(REG_OUT_ADC3_H, &data, 1);
lis3dh->adc.adc3 = (int8_t)data + 25;
@ -427,7 +453,9 @@ int lis3dh_fifo_reset(lis3dh_t *lis3dh) {
int err = 0;
uint8_t fifo_ctrl_reg = 0;
assert(lis3dh);
if (!lis3dh) {
return 1;
}
/* create a FIFO_MODE_BYPASS config */
fifo_ctrl_reg |= ((lis3dh->cfg.fifo.size - 1) & 0x1F);

22
main.c
View File

@ -1,13 +1,12 @@
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "lis3dh.h"
#include "i2c.h"
#include "interrupt.h"
/* #include "spi.h" */
#define GPIO_INTERRUPT_PIN_INT1 12
int main() {
int main(void) {
lis3dh_t lis;
struct lis3dh_fifo_data fifo;
@ -19,22 +18,27 @@ int main() {
lis.dev.sleep = usleep;
lis.dev.deinit = i2c_deinit;
/*
lis.dev.init = spi_init;
lis.dev.read = spi_read;
lis.dev.write = spi_write;
lis.dev.sleep = usleep;
lis.dev.deinit = spi_deinit;
*/
lis3dh_init(&lis);
lis3dh_reset(&lis);
int_register(GPIO_INTERRUPT_PIN_INT1);
lis.cfg.mode = LIS3DH_MODE_HR;
lis.cfg.range = LIS3DH_FS_2G;
lis.cfg.rate = LIS3DH_ODR_100_HZ;
lis.cfg.pin1.overrun = 1;
lis.cfg.fifo.mode = LIS3DH_FIFO_MODE_FIFO;
lis.cfg.fifo.trig = LIS3DH_FIFO_TRIG_INT1;
lis3dh_configure(&lis);
for ( ;; ) {
int_poll(GPIO_INTERRUPT_PIN_INT1);
usleep(100000); /* 100 ms */
lis3dh_read_fifo(&lis, &fifo);
for(i=0; i<fifo.size; i++)
printf("x: %4.4d mg, y: %4.4d mg, z: %4.4d mg\n", fifo.x[i], fifo.y[i], fifo.z[i]);
@ -46,4 +50,4 @@ int main() {
lis3dh_deinit(&lis);
return 0;
}
}