Skip to content

Commit 28efbe8

Browse files
Update fifo_interrupt.ino
Simplified FIFO interrupt example, as proposed by @cparata Co-Authored-By: Carlo Parata <[email protected]>
1 parent ab133f5 commit 28efbe8

File tree

1 file changed

+70
-63
lines changed

1 file changed

+70
-63
lines changed

examples/fifo_interrupt.ino

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,28 @@
3737

3838
#include "LSM6DSOXSensor.h"
3939

40-
#define SR 417 // Sample rate. Options are: 12.5, 26, 52, 104, 208, 417, 833, 1667, 3333 and 6667 Hz
41-
#define WTM_LV 500 // Watermark threshold level. Max samples in this FIFO configuration is 512 (accel and gyro only).
40+
#define SR 417.0f // Sample rate. Options are: 12.5, 26, 52, 104, 208, 417, 833, 1667, 3333 and 6667 Hz
41+
#define WTM_LV 102 // Watermark threshold level. Max samples in this FIFO configuration is 512 (accel and gyro only).
4242

43-
// Define interrupt pins acording to MCU board and sensor wiring.
43+
// Define interrupt pins according to MCU board and sensor wiring.
4444
#define INT1_pin A0 // MCU input pin connected to sensor INT1 output pin
45-
#define INT2_pin A1 // MCU imput pin connected to sensor INT2 output pin
4645

4746
/** LSM6DSOX i2c address:
4847
* LSM6DSOX_I2C_ADD_L: 0x6A (default)
4948
* LSM6DSOX_I2C_ADD_H: 0x6B
5049
**/
5150
LSM6DSOXSensor lsm6dsoxSensor = LSM6DSOXSensor(&Wire, LSM6DSOX_I2C_ADD_L);
5251

53-
volatile uint8_t wtmFlag = 0; // FIFO watermark flag
5452
volatile uint8_t fullFlag = 0; // FIFO full flag
5553

5654
// ISR callback for INT1
57-
void INT1_wtmEvent_cb() {
58-
59-
wtmFlag = 1;
60-
}
61-
62-
// ISR callback for INT2
63-
void INT2_fullEvent_cb() {
64-
55+
void INT1_fullEvent_cb() {
6556
fullFlag = 1;
6657
}
6758

6859
void setup() {
6960

70-
Serial.begin(115200);
61+
Serial.begin(921600);
7162
// Comment this line to skip waiting for serial:
7263
while(!Serial) delay(10);
7364

@@ -76,10 +67,8 @@ void setup() {
7667
Wire.setClock(400000);
7768

7869
// Interrupt pin settings
79-
pinMode(INT1_pin, INPUT_PULLUP);
80-
pinMode(INT2_pin, INPUT_PULLUP);
81-
attachInterrupt(digitalPinToInterrupt(INT1_pin), INT1_wtmEvent_cb, RISING); // attach watermark event to INT1 input pin
82-
attachInterrupt(digitalPinToInterrupt(INT2_pin), INT2_fullEvent_cb, RISING); // attach full event to INT2 input pin
70+
pinMode(INT1_pin, INPUT);
71+
attachInterrupt(digitalPinToInterrupt(INT1_pin), INT1_fullEvent_cb, RISING); // attach watermark event to INT1 input pin
8372

8473
// Initialize sensors
8574
lsm6dsoxSensor.begin();
@@ -122,16 +111,17 @@ void setup() {
122111
* LSM6DSOX_BYPASS_TO_STREAM_MODE: FIFO buffer starts operating in Bypass mode and switches to Continuous mode when an event condition occurs.
123112
* */
124113
lsm6dsoxSensor.Set_FIFO_Mode(LSM6DSOX_BYPASS_MODE); // flush any previous value in FIFO before start
125-
lsm6dsoxSensor.Set_FIFO_Mode(LSM6DSOX_STREAM_MODE); // start batching in continous mode
126-
114+
115+
lsm6dsoxSensor.Set_FIFO_INT1_FIFO_Full(1); // enable FIFO full interrupt on sensor INT1 pin
116+
127117
// Set FIFO watermark level. Can be used to check when the number of samples in buffer reaches this defined threshold level.
128118
lsm6dsoxSensor.Set_FIFO_Watermark_Level(WTM_LV);
129119

130120
// FIFO size can be limited to the watermark level by setting the STOP_ON_WTM flag to 1
131-
//lsm6dsoxSensor.Set_FIFO_Stop_On_Fth(1);
121+
lsm6dsoxSensor.Set_FIFO_Stop_On_Fth(1);
132122

133-
lsm6dsoxSensor.Set_FIFO_INT1_FIFO_Threshold(1); // enable FIFO threshold interrupt on sensor INT1 pin
134-
lsm6dsoxSensor.Set_FIFO_INT2_FIFO_Full(1); // enable FIFO full interrupt on sensor INT2 pin
123+
// start batching in continous mode
124+
lsm6dsoxSensor.Set_FIFO_Mode(LSM6DSOX_STREAM_MODE);
135125

136126
Serial.println("Starting...");
137127
}
@@ -142,55 +132,72 @@ void loop() {
142132
uint8_t Tag; // FIFO data sensor identifier
143133
int32_t acceleration[3]; // X, Y, Z accelerometer values in mg
144134
int32_t rotation[3]; // X, Y, Z giroscope values in mdps
135+
int16_t dummy[3];
136+
uint8_t fullStatus = 0; // full status
137+
int32_t count_acc_samples = 0;
138+
int32_t count_gyro_samples = 0;
139+
int32_t count_dummy_samples = 0;
145140

146141
// Get number of samples in buffer
147142
lsm6dsoxSensor.Get_FIFO_Num_Samples(&numSamples);
148-
Serial.print("Samples in FIFO: "); Serial.println(numSamples);
143+
Serial.print("Samples in FIFO: ");
144+
Serial.println(numSamples);
149145

150146
// Check if FIFO threshold level was reached.
151-
if (wtmFlag != 0) {
152-
Serial.println("-- FIFO Watermark level reached!, fetching data.");
147+
if (fullFlag != 0) {
148+
fullFlag = 0;
149+
150+
lsm6dsoxSensor.Get_FIFO_Full_Status(&fullStatus);
151+
152+
if(fullStatus) {
153+
Serial.println("-- FIFO Watermark level reached!, fetching data.");
153154

154-
// fetch data from FIFO
155-
for (uint16_t i = 0; i < WTM_LV; i++) {
155+
lsm6dsoxSensor.Get_FIFO_Num_Samples(&numSamples);
156+
157+
Serial.print("numSamples=");
158+
Serial.println(numSamples);
159+
160+
// fetch data from FIFO
161+
for (uint16_t i = 0; i < numSamples; i++) {
156162

157-
lsm6dsoxSensor.Get_FIFO_Tag(&Tag); // get data identifier
163+
lsm6dsoxSensor.Get_FIFO_Tag(&Tag); // get data identifier
158164

159-
// Get gyroscope data
160-
if (Tag == 1) {
161-
lsm6dsoxSensor.Get_FIFO_G_Axes(rotation);
162-
#if 0 // set to 1 for printing values
163-
Serial.print("mdps: "); Serial.print(rotation[0]);
164-
Serial.print(", "); Serial.print(rotation[1]);
165-
Serial.print(", "); Serial.print(rotation[2]);
166-
Serial.println();
167-
#endif
168-
}
165+
// Get gyroscope data
166+
if (Tag == 1) {
167+
lsm6dsoxSensor.Get_FIFO_G_Axes(rotation);
168+
count_gyro_samples++;
169+
#if 1 // set to 1 for printing values
170+
Serial.print("mdps: "); Serial.print(rotation[0]);
171+
Serial.print(", "); Serial.print(rotation[1]);
172+
Serial.print(", "); Serial.print(rotation[2]);
173+
Serial.println();
174+
#endif
175+
}
169176

170-
// Get accelerometer data
171-
else if (Tag == 2) {
172-
lsm6dsoxSensor.Get_FIFO_X_Axes(acceleration);
173-
#if 0 // set to 1 for printing values
174-
Serial.print("mG: "); Serial.print(acceleration[0]);
175-
Serial.print(", "); Serial.print(acceleration[1]);
176-
Serial.print(", "); Serial.print(acceleration[2]);
177-
Serial.println();
178-
#endif
177+
// Get accelerometer data
178+
else if (Tag == 2) {
179+
lsm6dsoxSensor.Get_FIFO_X_Axes(acceleration);
180+
count_acc_samples++;
181+
#if 1 // set to 1 for printing values
182+
Serial.print("mG: "); Serial.print(acceleration[0]);
183+
Serial.print(", "); Serial.print(acceleration[1]);
184+
Serial.print(", "); Serial.print(acceleration[2]);
185+
Serial.println();
186+
#endif
187+
}
188+
189+
// Flush unused tag
190+
else {
191+
lsm6dsoxSensor.Get_FIFO_Data((uint8_t *)dummy);
192+
count_dummy_samples++;
193+
}
179194
}
195+
Serial.print("Acc Samples: ");
196+
Serial.println(count_acc_samples);
197+
Serial.print("Gyro Samples: ");
198+
Serial.println(count_gyro_samples);
199+
Serial.print("Dummy Samples: ");
200+
Serial.println(count_dummy_samples);
180201
}
181-
182-
wtmFlag = 0;
183202
}
184-
185-
// Check if FIFO is full.
186-
if (fullFlag != 0) {
187-
Serial.println("-- FIFO is full!, consider reducing Watermark Level or Buffer Data Rate.\nFlushing data from FIFO.");
188-
lsm6dsoxSensor.Set_FIFO_Mode(LSM6DSOX_BYPASS_MODE); // flush FIFO data
189-
lsm6dsoxSensor.Set_FIFO_Mode(LSM6DSOX_STREAM_MODE); // continue batching
190-
191-
fullFlag = 0;
192-
}
193-
194-
delay(10); // FIFO continues batching while we sleep
195-
196-
}
203+
}

0 commit comments

Comments
 (0)