FIFO example

This commit is contained in:
William Clark 2024-01-07 07:55:06 +00:00
parent 14a051b84b
commit 5446d28c13
9 changed files with 464 additions and 99 deletions

View File

@ -12,11 +12,13 @@ The watermark level can be adjusted to a value [1-32] (0 disables FIFO) by modif
The FIFO "engine" samples/appends another set of [x y z] values at 1/ODR. The maximum ODR supported by the FIFO "engine" is 200 Hz.
**files in `fifo/` dir**
| FIFO mode | symbol | description |
|------------------|-----------------------|----------------------------|
| Bypass | `LIS3DH_FIFO_MODE_BYPASS` | FIFO is inoperational |
| FIFO | `LIS3DH_FIFO_MODE_FIFO` | FIFO can be read/emptied at any time but once overrun has to be reset. See file: `fifo-mode-fifo.c` |
| Stream | `LIS3DH_FIFO_MODE_STREAM` | FIFO continously writes new data at 1/ODR and will overwrite old data until it is read/emptied. See file: `fifo-mode-stream.c` |
| FIFO | `LIS3DH_FIFO_MODE_FIFO` | FIFO can be read/emptied at any time but once overrun has to be reset. See files: `fifo-int-ovrn.c`, `fifo-int-wtm.c`, `fifo.c` |
| Stream | `LIS3DH_FIFO_MODE_STREAM` | FIFO continously writes new data at 1/ODR and will overwrite old data until it is read/emptied. See files: `stream-int-ovrn.c`, `stream-int-wtm.c`, `stream.c` |
| Stream_to_FIFO | `LIS3DH_FIFO_STREAM_TO_FIFO` | FIFO behaves like Stream mode until a set interrupt is activated, then changes to a mode FIFO. |
Note: FIFO will not trigger a watermark interrupt (`pin1.wtm`) if the FIFO size is default (32; maximum size). To use the watermark interrupt, the FIFO size has to be between [1-31]. An overrun interrupt (`pin1.overrun`) will always trigger when the FIFO is full, regardless of programmed capacity.

View File

@ -1,49 +0,0 @@
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include "lis3dh.h"
#include "i2c.h"
#include "interrupt.h"
#define GPIO_INTERRUPT_PIN_INT1 12
int main() {
lis3dh_t lis;
struct lis3dh_fifo_data fifo;
int i;
lis.dev.init = i2c_init;
lis.dev.read = i2c_read;
lis.dev.write = i2c_write;
lis.dev.sleep = usleep;
lis.dev.deinit = i2c_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);
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]);
printf("fifo.size=%d\n", fifo.size);
lis3dh_fifo_reset(&lis);
}
lis3dh_deinit(&lis);
return 0;
}

View File

@ -1,48 +0,0 @@
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include "lis3dh.h"
#include "i2c.h"
#include "interrupt.h"
#define GPIO_INTERRUPT_PIN_INT1 12
int main() {
lis3dh_t lis;
struct lis3dh_fifo_data fifo;
int i;
lis.dev.init = i2c_init;
lis.dev.read = i2c_read;
lis.dev.write = i2c_write;
lis.dev.sleep = usleep;
lis.dev.deinit = i2c_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_STREAM;
lis.cfg.fifo.trig = LIS3DH_FIFO_TRIG_INT1;
lis3dh_configure(&lis);
for ( ;; ) {
int_poll(GPIO_INTERRUPT_PIN_INT1);
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]);
printf("fifo.size=%d\n", fifo.size);
}
lis3dh_deinit(&lis);
return 0;
}

View File

@ -0,0 +1,82 @@
#define _GNU_SOURCE /* usleep() */
#include <unistd.h> /* usleep() */
#include <stdio.h>
#include "lis3dh.h"
#include "i2c.h"
#include "interrupt.h"
/* Raspberry Pi GPIO pin connected to LIS3DH INT1 pin */
#define GPIO_INTERRUPT_PIN_INT1 12
int main() {
lis3dh_t lis;
struct lis3dh_fifo_data fifo;
int i;
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 struct and check device id */
if (lis3dh_init(&lis)) {
/* error handling */
}
/* reset just in case*/
if (lis3dh_reset(&lis)) {
/* error handling */
}
/* register interrupt */
if (int_register(GPIO_INTERRUPT_PIN_INT1)) {
/* error handling */
}
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;
/* write cfg to device */
if (lis3dh_configure(&lis)) {
/* error handling */
}
for ( ;; ) {
/* wait until FIFO overrun interrupt is active */
if (int_poll(GPIO_INTERRUPT_PIN_INT1)) {
/* error handling */
}
/* read FIFO into `fifo_data' struct `fifo' */
if (lis3dh_read_fifo(&lis, &fifo)) {
/* error handling */
}
/* print data .. */
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]);
}
printf("fifo.size=%d\n", fifo.size);
/* in FIFO mode, the FIFO engine must be restarted after it has been overrun */
/* otherwise, it will not fill up again. */
if (lis3dh_fifo_reset(&lis)) {
/* error handling */
}
}
/* deinitalise device */
if (lis3dh_deinit(&lis)) {
/* error handling */
}
return 0;
}

View File

@ -0,0 +1,84 @@
#define _GNU_SOURCE /* usleep() */
#include <unistd.h> /* usleep() */
#include <stdio.h>
#include "lis3dh.h"
#include "i2c.h"
#include "interrupt.h"
/* Raspberry Pi GPIO pin connected to LIS3DH INT1 pin */
#define GPIO_INTERRUPT_PIN_INT1 12
int main() {
lis3dh_t lis;
struct lis3dh_fifo_data fifo;
int i;
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 struct and check device id */
if (lis3dh_init(&lis)) {
/* error handling */
}
/* reset just in case*/
if (lis3dh_reset(&lis)) {
/* error handling */
}
/* register interrupt */
if (int_register(GPIO_INTERRUPT_PIN_INT1)) {
/* error handling */
}
lis.cfg.mode = LIS3DH_MODE_HR;
lis.cfg.range = LIS3DH_FS_2G;
lis.cfg.rate = LIS3DH_ODR_100_HZ;
lis.cfg.pin1.wtm = 1; /* watermark interrupt */
lis.cfg.fifo.mode = LIS3DH_FIFO_MODE_FIFO;
lis.cfg.fifo.trig = LIS3DH_FIFO_TRIG_INT1;
lis.cfg.fifo.size = 15; /* size must be < 32 to use the watermark interrupt */
/* write cfg to device */
if (lis3dh_configure(&lis)) {
/* error handling */
}
for ( ;; ) {
/* wait until FIFO overrun interrupt is active */
if (int_poll(GPIO_INTERRUPT_PIN_INT1)) {
/* error handling */
}
/* read FIFO into `fifo_data' struct `fifo' */
if (lis3dh_read_fifo(&lis, &fifo)) {
/* error handling */
}
/* print data .. */
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]);
}
printf("fifo.size=%d\n", fifo.size);
/* Since the WTM is less than the full size, there is no need to reset the device after reading */
/* However, if the reading of the device is somehow delayed and the FIFO fills up between */
/* receiving the wtm interrupt, and calling lis3dh_read_fifo(), the FIFO must be reset. */
/* this can easily be simulated by calling a sleep function. */
/* Therefore, it is a good idea to reset the FIFO anyway. */
lis3dh_fifo_reset(&lis);
}
/* deinitalise device */
if (lis3dh_deinit(&lis)) {
/* error handling */
}
return 0;
}

71
example/fifo/fifo.c Normal file
View File

@ -0,0 +1,71 @@
#define _GNU_SOURCE /* usleep() */
#include <unistd.h> /* usleep() */
#include <stdio.h>
#include "lis3dh.h"
#include "i2c.h"
int main() {
lis3dh_t lis;
struct lis3dh_fifo_data fifo;
int i;
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 struct and check device id */
if (lis3dh_init(&lis)) {
/* error handling */
}
/* reset just in case*/
if (lis3dh_reset(&lis)) {
/* error handling */
}
lis.cfg.mode = LIS3DH_MODE_HR;
lis.cfg.range = LIS3DH_FS_2G;
lis.cfg.rate = LIS3DH_ODR_100_HZ;
lis.cfg.fifo.mode = LIS3DH_FIFO_MODE_FIFO;
/* modifying the size of the FIFO buffer is not useful when just polling without interrupts */
/* write cfg to device */
if (lis3dh_configure(&lis)) {
/* error handling */
}
for ( ;; ) {
/* read FIFO into `fifo_data' struct `fifo' */
if (lis3dh_read_fifo(&lis, &fifo)) {
/* error handling */
}
/* print data .. */
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]);
}
/* FIFO size will rarely go above 2 in this example, because lis3dh_read_fifo() */
/* does not wait for the FIFO buffer to be full, just non-empty. */
printf("fifo.size=%d\n", fifo.size);
/* Since the WTM is less than the full size, there is no need to reset the device after reading */
/* However, if the reading of the device is somehow delayed and the FIFO fills up between */
/* calls to lis3dh_read_fifo(), then the device is overrun, and must be reset. */
/* this can easily be simulated by calling a sleep function. */
/* Therefore, it is a good idea to reset the FIFO anyway. */
lis3dh_fifo_reset(&lis);
}
/* deinitalise device */
if (lis3dh_deinit(&lis)) {
/* error handling */
}
return 0;
}

View File

@ -0,0 +1,78 @@
#define _GNU_SOURCE /* usleep() */
#include <unistd.h> /* usleep() */
#include <stdio.h>
#include "lis3dh.h"
#include "i2c.h"
#include "interrupt.h"
/* Raspberry Pi GPIO pin connected to LIS3DH INT1 pin */
#define GPIO_INTERRUPT_PIN_INT1 12
int main() {
lis3dh_t lis;
struct lis3dh_fifo_data fifo;
int i;
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 struct and check device id */
if (lis3dh_init(&lis)) {
/* error handling */
}
/* reset just in case*/
if (lis3dh_reset(&lis)) {
/* error handling */
}
/* register interrupt */
if (int_register(GPIO_INTERRUPT_PIN_INT1)) {
/* error handling */
}
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_STREAM;
lis.cfg.fifo.trig = LIS3DH_FIFO_TRIG_INT1;
/* write cfg to device */
if (lis3dh_configure(&lis)) {
/* error handling */
}
for ( ;; ) {
/* wait until FIFO overrun interrupt is active */
if (int_poll(GPIO_INTERRUPT_PIN_INT1)) {
/* error handling */
}
/* read FIFO into `fifo_data' struct `fifo' */
if (lis3dh_read_fifo(&lis, &fifo)) {
/* error handling */
}
/* print data .. */
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]);
}
printf("fifo.size=%d\n", fifo.size);
/* No need to reset the FIFO engine in stream mode */
}
/* deinitalise device */
if (lis3dh_deinit(&lis)) {
/* error handling */
}
return 0;
}

View File

@ -0,0 +1,79 @@
#define _GNU_SOURCE /* usleep() */
#include <unistd.h> /* usleep() */
#include <stdio.h>
#include "lis3dh.h"
#include "i2c.h"
#include "interrupt.h"
/* Raspberry Pi GPIO pin connected to LIS3DH INT1 pin */
#define GPIO_INTERRUPT_PIN_INT1 12
int main() {
lis3dh_t lis;
struct lis3dh_fifo_data fifo;
int i;
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 struct and check device id */
if (lis3dh_init(&lis)) {
/* error handling */
}
/* reset just in case*/
if (lis3dh_reset(&lis)) {
/* error handling */
}
/* register interrupt */
if (int_register(GPIO_INTERRUPT_PIN_INT1)) {
/* error handling */
}
lis.cfg.mode = LIS3DH_MODE_HR;
lis.cfg.range = LIS3DH_FS_2G;
lis.cfg.rate = LIS3DH_ODR_100_HZ;
lis.cfg.pin1.wtm = 1; /* watermark interrupt */
lis.cfg.fifo.mode = LIS3DH_FIFO_MODE_STREAM;
lis.cfg.fifo.trig = LIS3DH_FIFO_TRIG_INT1;
lis.cfg.fifo.size = 15; /* size must be < 32 to use the watermark interrupt */
/* write cfg to device */
if (lis3dh_configure(&lis)) {
/* error handling */
}
for ( ;; ) {
/* wait until FIFO overrun interrupt is active */
if (int_poll(GPIO_INTERRUPT_PIN_INT1)) {
/* error handling */
}
/* read FIFO into `fifo_data' struct `fifo' */
if (lis3dh_read_fifo(&lis, &fifo)) {
/* error handling */
}
/* print data .. */
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]);
}
printf("fifo.size=%d\n", fifo.size);
/* No need to reset the FIFO engine in stream mode */
}
/* deinitalise device */
if (lis3dh_deinit(&lis)) {
/* error handling */
}
return 0;
}

66
example/fifo/stream.c Normal file
View File

@ -0,0 +1,66 @@
#define _GNU_SOURCE /* usleep() */
#include <unistd.h> /* usleep() */
#include <stdio.h>
#include "lis3dh.h"
#include "i2c.h"
int main() {
lis3dh_t lis;
struct lis3dh_fifo_data fifo;
int i;
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 struct and check device id */
if (lis3dh_init(&lis)) {
/* error handling */
}
/* reset just in case*/
if (lis3dh_reset(&lis)) {
/* error handling */
}
lis.cfg.mode = LIS3DH_MODE_HR;
lis.cfg.range = LIS3DH_FS_2G;
lis.cfg.rate = LIS3DH_ODR_100_HZ;
lis.cfg.fifo.mode = LIS3DH_FIFO_MODE_STREAM;
/* modifying the size of the FIFO buffer is not useful when just polling without interrupts */
/* write cfg to device */
if (lis3dh_configure(&lis)) {
/* error handling */
}
for ( ;; ) {
/* read FIFO into `fifo_data' struct `fifo' */
if (lis3dh_read_fifo(&lis, &fifo)) {
/* error handling */
}
/* print data .. */
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]);
}
/* FIFO size will rarely go above 2 in this example, because lis3dh_read_fifo() */
/* does not wait for the FIFO buffer to be full, just non-empty. */
printf("fifo.size=%d\n", fifo.size);
/* No need to reset the FIFO engine in stream mode */
}
/* deinitalise device */
if (lis3dh_deinit(&lis)) {
/* error handling */
}
return 0;
}