Skip to content

Commit 0fb14b8

Browse files
committed
Add FIFO and Qvar examples
1 parent 274e752 commit 0fb14b8

5 files changed

Lines changed: 334 additions & 1 deletion

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ to find out the 6D orientation and display data on a hyperterminal.
4949

5050
The `STEVAL_MKBOXPRO_MEMS_Double_Tap_Detection` This application shows how to detect the double tap event using the LSM6DSV16X accelerometer.
5151

52+
## STEVAL_MKBOXPRO_MEMS_FIFO_Polling
53+
54+
The `STEVAL_MKBOXPRO_MEMS_FIFO_Polling` This application shows how to get accelerometer and gyroscope data from FIFO in pooling mode and print them on terminal.
55+
56+
## STEVAL_MKBOXPRO_MEMS_FIFO_Interrupt
57+
58+
The `STEVAL_MKBOXPRO_MEMS_FIFO_Interrupt` This application shows how to get accelerometer and gyroscope data from FIFO using interrupt and print them on terminal.
59+
5260
## STEVAL_MKBOXPRO_MEMS_Free_Fall_Detection
5361

5462
The `STEVAL_MKBOXPRO_MEMS_Free_Fall_Detection` This application shows how to detect the free fall event using the LSM6DSV16X accelerometer.
@@ -57,6 +65,10 @@ The `STEVAL_MKBOXPRO_MEMS_Free_Fall_Detection` This application shows how to de
5765

5866
The `STEVAL_MKBOXPRO_MEMS_Pedometer` This application shows how to use LSM6DSV16X accelerometer to count steps.
5967

68+
## STEVAL_MKBOXPRO_MEMS_Qvar_Polling
69+
70+
The `STEVAL_MKBOXPRO_MEMS_Qvar_Polling` This application shows how to use LSM6DSV16X Qvar features in polling mode.
71+
6072
## STEVAL_MKBOXPRO_MEMS_Single_Tap_Detection
6173

6274
The `STEVAL_MKBOXPRO_MEMS_Single_Tap_Detection` This application shows how to detect the single tap event using the LSM6DSV16X accelerometer.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
@file STEVAL_MKBOXPRO_MEMS_FIFO_Interrupt.ino
3+
@author STMicroelectronics
4+
@brief Example to use the LSM6DSV16X library with FIFO status interrupts.
5+
*******************************************************************************
6+
Copyright (c) 2022, STMicroelectronics
7+
All rights reserved.
8+
9+
This software component is licensed by ST under BSD 3-Clause license,
10+
the "License"; You may not use this file except in compliance with the
11+
License. You may obtain a copy of the License at:
12+
opensource.org/licenses/BSD-3-Clause
13+
14+
*******************************************************************************
15+
*/
16+
#include <LSM6DSV16XSensor.h>
17+
18+
#define SENSOR_ODR 104.0f // In Hertz
19+
#define ACC_FS 2 // In g
20+
#define GYR_FS 2000 // In dps
21+
#define MEASUREMENT_TIME_INTERVAL (1000.0f/SENSOR_ODR) // In ms
22+
#define FIFO_SAMPLE_THRESHOLD 199
23+
#define FLASH_BUFF_LEN 8192
24+
#define INT1_pin PA4
25+
LSM6DSV16XSensor AccGyr(&Wire);
26+
27+
volatile uint8_t fullFlag = 0; // FIFO full flag
28+
29+
// ISR callback for INT1
30+
void INT1_fullEvent_cb() {
31+
fullFlag = 1;
32+
}
33+
34+
unsigned long timestamp_count = 0;
35+
bool acc_available = false;
36+
bool gyr_available = false;
37+
int32_t acc_value[3];
38+
int32_t gyr_value[3];
39+
char buff[FLASH_BUFF_LEN];
40+
uint32_t pos = 0;
41+
42+
void Read_FIFO_Data();
43+
44+
void setup() {
45+
46+
Serial.begin(115200);
47+
Wire.begin();
48+
49+
// Enable INT1 pin.
50+
attachInterrupt(INT1_pin, INT1_fullEvent_cb, RISING);
51+
52+
// Initialize LSM6DSV16X.
53+
AccGyr.begin();
54+
AccGyr.Enable_X();
55+
AccGyr.Enable_G();
56+
57+
// Configure ODR and FS of the acc and gyro
58+
AccGyr.Set_X_ODR(SENSOR_ODR);
59+
AccGyr.Set_X_FS(ACC_FS);
60+
AccGyr.Set_G_ODR(SENSOR_ODR);
61+
AccGyr.Set_G_FS(GYR_FS);
62+
63+
// Configure FIFO BDR for acc and gyro
64+
AccGyr.FIFO_Set_X_BDR(SENSOR_ODR);
65+
AccGyr.FIFO_Set_G_BDR(SENSOR_ODR);
66+
67+
// Set Set FIFO watermark level
68+
AccGyr.FIFO_Set_Watermark_Level(FIFO_SAMPLE_THRESHOLD);
69+
// Set FIFO stop on watermark level
70+
AccGyr.FIFO_Set_Stop_On_Fth(1);
71+
// Enable FIFO full interrupt on sensor INT1 pin
72+
AccGyr.FIFO_Set_INT1_FIFO_Full(1);
73+
// Set FIFO in Continuous mode
74+
AccGyr.FIFO_Set_Mode(LSM6DSV16X_STREAM_MODE);
75+
76+
Serial.println("LSM6DSV16X FIFO Demo");
77+
}
78+
79+
void loop() {
80+
uint8_t fullStatus = 0;
81+
82+
// If we reach the threshold we can empty the FIFO
83+
if (fullFlag != 0) {
84+
fullFlag = 0;
85+
86+
AccGyr.FIFO_Get_Full_Status(&fullStatus);
87+
88+
if(fullStatus) {
89+
fullStatus = 0;
90+
91+
// Empty the FIFO
92+
Read_FIFO_Data();
93+
94+
// Print FIFO data
95+
Serial.print(buff);
96+
}
97+
}
98+
}
99+
100+
void Read_FIFO_Data()
101+
{
102+
uint16_t i;
103+
uint16_t samples_to_read;
104+
105+
// Check the number of samples inside FIFO
106+
AccGyr.FIFO_Get_Num_Samples(&samples_to_read);
107+
108+
for (i = 0; i < samples_to_read; i++) {
109+
uint8_t tag;
110+
111+
// Check the FIFO tag
112+
AccGyr.FIFO_Get_Tag(&tag);
113+
switch (tag) {
114+
// If we have a gyro tag, read the gyro data
115+
case 1: {
116+
AccGyr.FIFO_Get_G_Axes(gyr_value);
117+
gyr_available = true;
118+
break;
119+
}
120+
121+
// If we have an acc tag, read the acc data
122+
case 2: {
123+
AccGyr.FIFO_Get_X_Axes(acc_value);
124+
acc_available = true;
125+
break;
126+
}
127+
128+
// We can discard other tags
129+
default: {
130+
break;
131+
}
132+
}
133+
// If we have the measurements of both acc and gyro, we can store them with timestamp
134+
if (acc_available && gyr_available) {
135+
int num_bytes;
136+
num_bytes = snprintf(&buff[pos], (FLASH_BUFF_LEN - pos), "%lu %d %d %d %d %d %d\r\n", (unsigned long)((float)timestamp_count * MEASUREMENT_TIME_INTERVAL), (int)acc_value[0], (int)acc_value[1], (int)acc_value[2], (int)gyr_value[0], (int)gyr_value[1], (int)gyr_value[2]);
137+
pos += num_bytes;
138+
timestamp_count++;
139+
acc_available = false;
140+
gyr_available = false;
141+
}
142+
}
143+
// We can add the termination character to the string, so we are ready to print it on hyper-terminal
144+
buff[pos] = '\0';
145+
pos = 0;
146+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
@file STEVAL_MKBOXPRO_MEMS_FIFO_Interrupt.ino
3+
@author STMicroelectronics
4+
@brief Example to use the LSM6DSV16X 3D accelerometer and 3D gyroscope FIFO
5+
*******************************************************************************
6+
Copyright (c) 2022, STMicroelectronics
7+
All rights reserved.
8+
9+
This software component is licensed by ST under BSD 3-Clause license,
10+
the "License"; You may not use this file except in compliance with the
11+
License. You may obtain a copy of the License at:
12+
opensource.org/licenses/BSD-3-Clause
13+
14+
*******************************************************************************
15+
*/
16+
17+
18+
#include <LSM6DSV16XSensor.h>
19+
20+
#define SENSOR_ODR 104.0f // In Hertz
21+
#define ACC_FS 2 // In g
22+
#define GYR_FS 2000 // In dps
23+
#define MEASUREMENT_TIME_INTERVAL (1000.0f/SENSOR_ODR) // In ms
24+
#define FIFO_SAMPLE_THRESHOLD 199
25+
#define FLASH_BUFF_LEN 8192
26+
27+
LSM6DSV16XSensor AccGyr(&Wire);
28+
29+
unsigned long timestamp_count = 0;
30+
bool acc_available = false;
31+
bool gyr_available = false;
32+
int32_t acc_value[3];
33+
int32_t gyr_value[3];
34+
char buff[FLASH_BUFF_LEN];
35+
uint32_t pos = 0;
36+
37+
void Read_FIFO_Data(uint16_t samples_to_read);
38+
39+
void setup() {
40+
41+
Serial.begin(115200);
42+
Wire.begin();
43+
44+
// Initialize LSM6DSV16X.
45+
AccGyr.begin();
46+
AccGyr.Enable_X();
47+
AccGyr.Enable_G();
48+
49+
// Configure ODR and FS of the acc and gyro
50+
AccGyr.Set_X_ODR(SENSOR_ODR);
51+
AccGyr.Set_X_FS(ACC_FS);
52+
AccGyr.Set_G_ODR(SENSOR_ODR);
53+
AccGyr.Set_G_FS(GYR_FS);
54+
55+
// Configure FIFO BDR for acc and gyro
56+
AccGyr.FIFO_Set_X_BDR(SENSOR_ODR);
57+
AccGyr.FIFO_Set_G_BDR(SENSOR_ODR);
58+
59+
// Set FIFO in Continuous mode
60+
AccGyr.FIFO_Set_Mode(LSM6DSV16X_STREAM_MODE);
61+
62+
Serial.println("LSM6DSV16X FIFO Demo");
63+
}
64+
65+
void loop() {
66+
uint16_t fifo_samples;
67+
68+
// Check the number of samples inside FIFO
69+
AccGyr.FIFO_Get_Num_Samples(&fifo_samples);
70+
71+
// If we reach the threshold we can empty the FIFO
72+
if (fifo_samples > FIFO_SAMPLE_THRESHOLD) {
73+
74+
// Empty the FIFO
75+
Read_FIFO_Data(fifo_samples);
76+
77+
// Print FIFO data
78+
Serial.print(buff);
79+
}
80+
}
81+
82+
void Read_FIFO_Data(uint16_t samples_to_read)
83+
{
84+
uint16_t i;
85+
86+
for (i = 0; i < samples_to_read; i++) {
87+
uint8_t tag;
88+
89+
// Check the FIFO tag
90+
AccGyr.FIFO_Get_Tag(&tag);
91+
switch (tag) {
92+
// If we have a gyro tag, read the gyro data
93+
case 1: {
94+
AccGyr.FIFO_Get_G_Axes(gyr_value);
95+
gyr_available = true;
96+
break;
97+
}
98+
99+
// If we have an acc tag, read the acc data
100+
case 2: {
101+
AccGyr.FIFO_Get_X_Axes(acc_value);
102+
acc_available = true;
103+
break;
104+
}
105+
106+
// We can discard other tags
107+
default: {
108+
break;
109+
}
110+
}
111+
// If we have the measurements of both acc and gyro, we can store them with timestamp
112+
if (acc_available && gyr_available) {
113+
int num_bytes;
114+
num_bytes = snprintf(&buff[pos], (FLASH_BUFF_LEN - pos), "%lu %d %d %d %d %d %d\r\n", (unsigned long)((float)timestamp_count * MEASUREMENT_TIME_INTERVAL), (int)acc_value[0], (int)acc_value[1], (int)acc_value[2], (int)gyr_value[0], (int)gyr_value[1], (int)gyr_value[2]);
115+
pos += num_bytes;
116+
timestamp_count++;
117+
acc_available = false;
118+
gyr_available = false;
119+
}
120+
}
121+
// We can add the termination character to the string, so we are ready to print it on hyper-terminal
122+
buff[pos] = '\0';
123+
pos = 0;
124+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
@file STEVAL_MKBOXPRO_MEMS_Qvar_Polling.ino
3+
@author STMicroelectronics
4+
@brief Example to use LSM6DSV16X Qvar features
5+
*******************************************************************************
6+
Copyright (c) 2022, STMicroelectronics
7+
All rights reserved.
8+
9+
This software component is licensed by ST under BSD 3-Clause license,
10+
the "License"; You may not use this file except in compliance with the
11+
License. You may obtain a copy of the License at:
12+
opensource.org/licenses/BSD-3-Clause
13+
14+
*******************************************************************************
15+
*/
16+
#include <LSM6DSV16XSensor.h>
17+
LSM6DSV16XSensor AccGyr(&Wire);
18+
19+
void setup() {
20+
21+
Serial.begin(115200);
22+
Wire.begin();
23+
24+
// Initialize LSM6DSV16X.
25+
AccGyr.begin();
26+
27+
// Enable accelerometer and gyroscope.
28+
AccGyr.Enable_X();
29+
AccGyr.Enable_G();
30+
31+
// Enable QVAR
32+
if(AccGyr.QVAR_Enable()!= 0)
33+
{
34+
Serial.println("Error during initialization of QVAR");
35+
while(1);
36+
}
37+
Serial.println("LSM6DSV16X QVAR Demo");
38+
}
39+
40+
void loop() {
41+
uint8_t qvar_status;
42+
float qvar_data;
43+
// Check if QVAR data is ready
44+
AccGyr.QVAR_GetStatus(&qvar_status);
45+
if(qvar_status)
46+
{
47+
// Get QVAR data
48+
AccGyr.QVAR_GetData(&qvar_data);
49+
Serial.println(qvar_data);
50+
}
51+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=STM32duino STEVAL-MKBOXPRO-Examples
2-
version=1.0.0
2+
version=1.1.0
33
author=SRA
44
maintainer=stm32duino
55
sentence=Examples to use MEMS sensors and NFC tag on STEVAL board

0 commit comments

Comments
 (0)