Skip to content

Commit 2e36778

Browse files
authored
Merge pull request #13 from alvaro-oliver/FIFO_STATUS2-FIFO_WTM_IA
Get_FIFO_Watermark_Status
2 parents 261c03d + 28efbe8 commit 2e36778

File tree

4 files changed

+537
-0
lines changed

4 files changed

+537
-0
lines changed

examples/fifo_interrupt.ino

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/**
2+
******************************************************************************
3+
* @file fifo_interrupt.ino
4+
* @author alvaro-oliver
5+
* @version V1.0.0
6+
* @date May 2021
7+
* @brief Example for LSM6DSOX library with FIFO status interrupts.
8+
******************************************************************************
9+
* @attention
10+
*
11+
* <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
12+
*
13+
* Redistribution and use in source and binary forms, with or without modification,
14+
* are permitted provided that the following conditions are met:
15+
* 1. Redistributions of source code must retain the above copyright notice,
16+
* this list of conditions and the following disclaimer.
17+
* 2. Redistributions in binary form must reproduce the above copyright notice,
18+
* this list of conditions and the following disclaimer in the documentation
19+
* and/or other materials provided with the distribution.
20+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
21+
* may be used to endorse or promote products derived from this software
22+
* without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*
35+
******************************************************************************
36+
*/
37+
38+
#include "LSM6DSOXSensor.h"
39+
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).
42+
43+
// Define interrupt pins according to MCU board and sensor wiring.
44+
#define INT1_pin A0 // MCU input pin connected to sensor INT1 output pin
45+
46+
/** LSM6DSOX i2c address:
47+
* LSM6DSOX_I2C_ADD_L: 0x6A (default)
48+
* LSM6DSOX_I2C_ADD_H: 0x6B
49+
**/
50+
LSM6DSOXSensor lsm6dsoxSensor = LSM6DSOXSensor(&Wire, LSM6DSOX_I2C_ADD_L);
51+
52+
volatile uint8_t fullFlag = 0; // FIFO full flag
53+
54+
// ISR callback for INT1
55+
void INT1_fullEvent_cb() {
56+
fullFlag = 1;
57+
}
58+
59+
void setup() {
60+
61+
Serial.begin(921600);
62+
// Comment this line to skip waiting for serial:
63+
while(!Serial) delay(10);
64+
65+
// i2c, fast mode
66+
Wire.begin();
67+
Wire.setClock(400000);
68+
69+
// Interrupt pin settings
70+
pinMode(INT1_pin, INPUT);
71+
attachInterrupt(digitalPinToInterrupt(INT1_pin), INT1_fullEvent_cb, RISING); // attach watermark event to INT1 input pin
72+
73+
// Initialize sensors
74+
lsm6dsoxSensor.begin();
75+
if (lsm6dsoxSensor.Enable_G() == LSM6DSOX_OK && lsm6dsoxSensor.Enable_X() == LSM6DSOX_OK) {
76+
Serial.println("Success enabling accelero and gyro");
77+
} else {
78+
Serial.println("Error enabling accelero and gyro");
79+
abort();
80+
}
81+
82+
// Check device id
83+
uint8_t id;
84+
lsm6dsoxSensor.ReadID(&id);
85+
if (id != LSM6DSOX_ID) {
86+
Serial.println("Wrong id for LSM6DSOX sensor. Check that device is plugged");
87+
abort();
88+
} else {
89+
Serial.println("Success checking id for LSM6DSOX sensor");
90+
}
91+
92+
// Set accelerometer scale. Available values are: 2, 4, 8, 16 G
93+
lsm6dsoxSensor.Set_X_FS(2);
94+
// Set gyroscope scale. Available values are: 125, 250, 500, 1000, 2000 dps
95+
lsm6dsoxSensor.Set_G_FS(250);
96+
97+
// Set accelerometer Output Data Rate. Available values are: 1.6, 12.5, 26, 52, 104, 208, 417, 833, 1667, 3333, 6667 Hz
98+
lsm6dsoxSensor.Set_X_ODR(SR);
99+
// Set gyroscope Output Data Rate. Available values are 12.5, 26, 52, 104, 208, 417, 833, 1667, 3333, 6667 Hz
100+
lsm6dsoxSensor.Set_G_ODR(SR);
101+
102+
// Set FIFO Batch Data Rate for accelerometer and gyroscope. Available values are: 0, 12.5, 26, 52, 104, 208, 417, 833, 1667, 3333, 6667 Hz
103+
lsm6dsoxSensor.Set_FIFO_X_BDR(SR);
104+
lsm6dsoxSensor.Set_FIFO_G_BDR(SR);
105+
106+
/** Set FIFO operation mode. Available values are:
107+
* LSM6DSOX_BYPASS_MODE: FIFO is not used, the buffer content is cleared
108+
* LSM6DSOX_FIFO_MODE: bufer continues filling until it becomes full. Then it stops collecting data.
109+
* LSM6DSOX_STREAM_MODE: continuous mode. Older data are replaced by the new data.
110+
* LSM6DSOX_STREAM_TO_FIFO_MODE: FIFO buffer starts operating in Continuous mode and switches to FIFO mode when an event condition occurs.
111+
* LSM6DSOX_BYPASS_TO_STREAM_MODE: FIFO buffer starts operating in Bypass mode and switches to Continuous mode when an event condition occurs.
112+
* */
113+
lsm6dsoxSensor.Set_FIFO_Mode(LSM6DSOX_BYPASS_MODE); // flush any previous value in FIFO before start
114+
115+
lsm6dsoxSensor.Set_FIFO_INT1_FIFO_Full(1); // enable FIFO full interrupt on sensor INT1 pin
116+
117+
// Set FIFO watermark level. Can be used to check when the number of samples in buffer reaches this defined threshold level.
118+
lsm6dsoxSensor.Set_FIFO_Watermark_Level(WTM_LV);
119+
120+
// FIFO size can be limited to the watermark level by setting the STOP_ON_WTM flag to 1
121+
lsm6dsoxSensor.Set_FIFO_Stop_On_Fth(1);
122+
123+
// start batching in continous mode
124+
lsm6dsoxSensor.Set_FIFO_Mode(LSM6DSOX_STREAM_MODE);
125+
126+
Serial.println("Starting...");
127+
}
128+
129+
void loop() {
130+
131+
uint16_t numSamples = 0; // number of samples in FIFO
132+
uint8_t Tag; // FIFO data sensor identifier
133+
int32_t acceleration[3]; // X, Y, Z accelerometer values in mg
134+
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;
140+
141+
// Get number of samples in buffer
142+
lsm6dsoxSensor.Get_FIFO_Num_Samples(&numSamples);
143+
Serial.print("Samples in FIFO: ");
144+
Serial.println(numSamples);
145+
146+
// Check if FIFO threshold level was reached.
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.");
154+
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++) {
162+
163+
lsm6dsoxSensor.Get_FIFO_Tag(&Tag); // get data identifier
164+
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+
}
176+
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+
}
194+
}
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);
201+
}
202+
}
203+
}

examples/fifo_polling.ino

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/**
2+
******************************************************************************
3+
* @file fifo_polling.ino
4+
* @author alvaro-oliver
5+
* @version V1.0.1
6+
* @date May 2021
7+
* @brief Example for LSM6DSOX library with FIFO status polling.
8+
******************************************************************************
9+
* @attention
10+
*
11+
* <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
12+
*
13+
* Redistribution and use in source and binary forms, with or without modification,
14+
* are permitted provided that the following conditions are met:
15+
* 1. Redistributions of source code must retain the above copyright notice,
16+
* this list of conditions and the following disclaimer.
17+
* 2. Redistributions in binary form must reproduce the above copyright notice,
18+
* this list of conditions and the following disclaimer in the documentation
19+
* and/or other materials provided with the distribution.
20+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
21+
* may be used to endorse or promote products derived from this software
22+
* without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*
35+
******************************************************************************
36+
*/
37+
38+
#include "LSM6DSOXSensor.h"
39+
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).
42+
43+
/** LSM6DSOX i2c address:
44+
* LSM6DSOX_I2C_ADD_L: 0x6A (default)
45+
* LSM6DSOX_I2C_ADD_H: 0x6B
46+
**/
47+
LSM6DSOXSensor lsm6dsoxSensor = LSM6DSOXSensor(&Wire, LSM6DSOX_I2C_ADD_L);
48+
49+
void setup() {
50+
51+
Serial.begin(115200);
52+
// Comment this line to skip waiting for serial:
53+
while(!Serial) delay(10);
54+
55+
// i2c, fast mode
56+
Wire.begin();
57+
Wire.setClock(400000);
58+
59+
// Initialize sensors
60+
lsm6dsoxSensor.begin();
61+
if (lsm6dsoxSensor.Enable_G() == LSM6DSOX_OK && lsm6dsoxSensor.Enable_X() == LSM6DSOX_OK) {
62+
Serial.println("Success enabling accelero and gyro");
63+
} else {
64+
Serial.println("Error enabling accelero and gyro");
65+
abort();
66+
}
67+
68+
// Check device id
69+
uint8_t id;
70+
lsm6dsoxSensor.ReadID(&id);
71+
if (id != LSM6DSOX_ID) {
72+
Serial.println("Wrong id for LSM6DSOX sensor. Check that device is plugged");
73+
abort();
74+
} else {
75+
Serial.println("Success checking id for LSM6DSOX sensor");
76+
}
77+
78+
// Set accelerometer scale. Available values are: 2, 4, 8, 16 G
79+
lsm6dsoxSensor.Set_X_FS(2);
80+
// Set gyroscope scale. Available values are: 125, 250, 500, 1000, 2000 dps
81+
lsm6dsoxSensor.Set_G_FS(250);
82+
83+
// Set accelerometer Output Data Rate. Available values are: 1.6, 12.5, 26, 52, 104, 208, 417, 833, 1667, 3333, 6667 Hz
84+
lsm6dsoxSensor.Set_X_ODR(SR);
85+
// Set gyroscope Output Data Rate. Available values are 12.5, 26, 52, 104, 208, 417, 833, 1667, 3333, 6667 Hz
86+
lsm6dsoxSensor.Set_G_ODR(SR);
87+
88+
// Set FIFO Batch Data Rate for accelerometer and gyroscope. Available values are: 0, 12.5, 26, 52, 104, 208, 417, 833, 1667, 3333, 6667 Hz
89+
lsm6dsoxSensor.Set_FIFO_X_BDR(SR);
90+
lsm6dsoxSensor.Set_FIFO_G_BDR(SR);
91+
92+
/** Set FIFO operation mode. Available values are:
93+
* LSM6DSOX_BYPASS_MODE: FIFO is not used, the buffer content is cleared
94+
* LSM6DSOX_FIFO_MODE: bufer continues filling until it becomes full. Then it stops collecting data.
95+
* LSM6DSOX_STREAM_MODE: continuous mode. Older data are replaced by the new data.
96+
* LSM6DSOX_STREAM_TO_FIFO_MODE: FIFO buffer starts operating in Continuous mode and switches to FIFO mode when an event condition occurs.
97+
* LSM6DSOX_BYPASS_TO_STREAM_MODE: FIFO buffer starts operating in Bypass mode and switches to Continuous mode when an event condition occurs.
98+
* */
99+
lsm6dsoxSensor.Set_FIFO_Mode(LSM6DSOX_BYPASS_MODE); // flush any previous value in FIFO before start
100+
lsm6dsoxSensor.Set_FIFO_Mode(LSM6DSOX_STREAM_MODE); // start batching in continous mode
101+
102+
// Set FIFO watermark level. Can be used to check when the number of samples in buffer reaches this defined threshold level.
103+
lsm6dsoxSensor.Set_FIFO_Watermark_Level(WTM_LV);
104+
105+
// FIFO size can be limited to the watermark level by setting the STOP_ON_WTM flag to 1
106+
//lsm6dsoxSensor.Set_FIFO_Stop_On_Fth(1);
107+
108+
Serial.println("Starting...");
109+
}
110+
111+
void loop() {
112+
113+
static uint8_t wtmStatus = 0; // FIFO watermark status
114+
uint8_t fullStatus = 0; // FIFO full status
115+
uint16_t numSamples = 0; // number of samples in FIFO
116+
uint8_t Tag; // FIFO data sensor identifier
117+
int32_t acceleration[3]; // X, Y, Z accelerometer values in mg
118+
int32_t rotation[3]; // X, Y, Z giroscope values in mdps
119+
120+
// Get number of samples in buffer
121+
lsm6dsoxSensor.Get_FIFO_Num_Samples(&numSamples);
122+
Serial.print("Samples in FIFO: "); Serial.println(numSamples);
123+
124+
// Check if FIFO threshold level was reached.
125+
lsm6dsoxSensor.Get_FIFO_Watermark_Status(&wtmStatus);
126+
127+
if (wtmStatus != 0) {
128+
Serial.println("-- FIFO Watermark level reached!, fetching data.");
129+
130+
// fetch data from FIFO
131+
for (uint16_t i = 0; i < WTM_LV; i++) {
132+
133+
lsm6dsoxSensor.Get_FIFO_Tag(&Tag); // get data identifier
134+
135+
// Get gyroscope data
136+
if (Tag == 1) {
137+
lsm6dsoxSensor.Get_FIFO_G_Axes(rotation);
138+
#if 0 // set to 1 for printing values
139+
Serial.print("mdps: "); Serial.print(rotation[0]);
140+
Serial.print(", "); Serial.print(rotation[1]);
141+
Serial.print(", "); Serial.print(rotation[2]);
142+
Serial.println();
143+
#endif
144+
}
145+
146+
// Get accelerometer data
147+
else if (Tag == 2) {
148+
lsm6dsoxSensor.Get_FIFO_X_Axes(acceleration);
149+
#if 0 // set to 1 for printing values
150+
Serial.print("mG: "); Serial.print(acceleration[0]);
151+
Serial.print(", "); Serial.print(acceleration[1]);
152+
Serial.print(", "); Serial.print(acceleration[2]);
153+
Serial.println();
154+
#endif
155+
}
156+
}
157+
}
158+
159+
// Check if FIFO is full.
160+
lsm6dsoxSensor.Get_FIFO_Full_Status(&fullStatus);
161+
162+
if (fullStatus != 0) {
163+
Serial.println("-- FIFO is full!, consider reducing Watermark Level or Buffer Data Rate.\nFlushing data from FIFO.");
164+
lsm6dsoxSensor.Set_FIFO_Mode(LSM6DSOX_BYPASS_MODE); // flush FIFO data
165+
lsm6dsoxSensor.Set_FIFO_Mode(LSM6DSOX_STREAM_MODE); // continue batching
166+
}
167+
168+
delay(10); // FIFO continues batching while we sleep
169+
}

0 commit comments

Comments
 (0)