├── README.md
├── examples
├── LSM6DSO_6DOrientation
│ └── LSM6DSO_6DOrientation.ino
├── LSM6DSO_DoubleTap
│ └── LSM6DSO_DoubleTap.ino
├── LSM6DSO_FreeFallDetection
│ └── LSM6DSO_FreeFallDetection.ino
├── LSM6DSO_HelloWorld
│ └── LSM6DSO_HelloWorld.ino
├── LSM6DSO_Pedometer
│ └── LSM6DSO_Pedometer.ino
├── LSM6DSO_SingleTap
│ └── LSM6DSO_SingleTap.ino
├── LSM6DSO_TiltDetection
│ └── LSM6DSO_TiltDetection.ino
└── LSM6DSO_WakeUpDetection
│ └── LSM6DSO_WakeUpDetection.ino
├── keywords.txt
├── library.properties
└── src
├── LSM6DSOSensor.cpp
├── LSM6DSOSensor.h
├── lsm6dso_reg.c
└── lsm6dso_reg.h
/README.md:
--------------------------------------------------------------------------------
1 | # LSM6DSO
2 | Arduino library to support the LSM6DSO 3D accelerometer and 3D gyroscope
3 |
4 | ## API
5 |
6 | This sensor uses I2C or SPI to communicate.
7 | For I2C it is then required to create a TwoWire interface before accessing to the sensors:
8 |
9 | TwoWire dev_i2c(I2C_SDA, I2C_SCL);
10 | dev_i2c.begin();
11 |
12 | For SPI it is then required to create a SPI interface before accessing to the sensors:
13 |
14 | SPIClass dev_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
15 | dev_spi.begin();
16 |
17 | An instance can be created and enabled when the I2C bus is used following the procedure below:
18 |
19 | LSM6DSOSensor AccGyr(&dev_i2c);
20 | AccGyr.begin();
21 | AccGyr.Enable_X();
22 | AccGyr.Enable_G();
23 |
24 | An instance can be created and enabled when the SPI bus is used following the procedure below:
25 |
26 | LSM6DSOSensor AccGyr(&dev_spi, CS_PIN);
27 | AccGyr.begin();
28 | AccGyr.Enable_X();
29 | AccGyr.Enable_G();
30 |
31 | The access to the sensor values is done as explained below:
32 |
33 | Read accelerometer and gyroscope.
34 |
35 | int32_t accelerometer[3];
36 | int32_t gyroscope[3];
37 | AccGyr.Get_X_Axes(accelerometer);
38 | AccGyr.Get_G_Axes(gyroscope);
39 |
40 | # Examples
41 |
42 | There are several examples with the LSM6DSO library.
43 | * LSM6DSO_HelloWorld: This application provides a simple example of usage of the LSM6DSO
44 | IMU 6-axis. It shows how to display on a hyperterminal the values of the sensor.
45 | * LSM6DSO_6DOrientation: This application shows how to use the LSM6DSO accelerometer
46 | to find out the 6D orientation and display data on a hyperterminal.
47 | * LSM6DSO_FreeFallDetection: This application shows how to detect the free fall event using the
48 | LSM6DSO accelerometer.
49 | * LSM6DSO_Pedometer: This application shows how to use the LSM6DSO accelerometer
50 | to count steps.
51 | * LSM6DSO_SingleTap: This application shows how to detect the single tap event using the
52 | LSM6DSO accelerometer.
53 | * LSM6DSO_DoubleTap: This application shows how to detect the double tap event using the
54 | LSM6DSO accelerometer.
55 | * LSM6DSO_TiltDetection: This application shows how to detect the tilt event using the
56 | LSM6DSO accelerometer.
57 | * LSM6DSO_WakeUpDetection: This application shows how to detect the wake-up event using the
58 | LSM6DSO accelerometer.
59 |
60 | ## Documentation
61 |
62 | You can find the source files at
63 | https://github.com/stm32duino/LSM6DSO
64 |
65 | The LSM6DSO datasheet is available at
66 | https://www.st.com/content/st_com/en/products/mems-and-sensors/inemo-inertial-modules/lsm6dso.html
67 |
--------------------------------------------------------------------------------
/examples/LSM6DSO_6DOrientation/LSM6DSO_6DOrientation.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file LSM6DSO_6DOrientation.ino
4 | * @author SRA
5 | * @version V1.0.0
6 | * @date February 2024
7 | * @brief Arduino test application for the STMicrolectronics
8 | * LSM6DSO MEMS IMU 6-axis sensor.
9 | * This application makes use of C++ classes obtained from the C
10 | * components' drivers.
11 | ******************************************************************************
12 | * @attention
13 | *
14 | *
© COPYRIGHT(c) 2024 STMicroelectronics
15 | *
16 | * Redistribution and use in source and binary forms, with or without modification,
17 | * are permitted provided that the following conditions are met:
18 | * 1. Redistributions of source code must retain the above copyright notice,
19 | * this list of conditions and the following disclaimer.
20 | * 2. Redistributions in binary form must reproduce the above copyright notice,
21 | * this list of conditions and the following disclaimer in the documentation
22 | * and/or other materials provided with the distribution.
23 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
24 | * may be used to endorse or promote products derived from this software
25 | * without specific prior written permission.
26 | *
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 | *
38 | ******************************************************************************
39 | */
40 | //NOTE: this example isn't compatible with Arduino Uno
41 |
42 |
43 | #include
44 |
45 | #ifdef ARDUINO_SAM_DUE
46 | #define DEV_I2C Wire1
47 | #elif defined(ARDUINO_ARCH_STM32)
48 | #define DEV_I2C Wire
49 | #elif defined(ARDUINO_ARCH_AVR)
50 | #define DEV_I2C Wire
51 | #else
52 | #define DEV_I2C Wire
53 | #endif
54 | #define SerialPort Serial
55 |
56 | #define INT_1 4
57 |
58 | LSM6DSOSensor accGyr(&DEV_I2C);
59 |
60 | //Interrupts.
61 | volatile int mems_event = 0;
62 |
63 | char report[256];
64 |
65 | void INT1Event_cb();
66 | void sendOrientation();
67 |
68 | void setup() {
69 | // Led.
70 | pinMode(LED_BUILTIN, OUTPUT);
71 | // Initialize serial for output.
72 | SerialPort.begin(115200);
73 |
74 | // Initialize I2C bus.
75 | DEV_I2C.begin();
76 |
77 | //Interrupts.
78 | attachInterrupt(INT_1, INT1Event_cb, RISING);
79 |
80 | accGyr.begin();
81 | accGyr.Enable_X();
82 | accGyr.Enable_6D_Orientation(LSM6DSO_INT1_PIN);
83 | }
84 |
85 | void loop() {
86 | if (mems_event)
87 | {
88 | mems_event=0;
89 | LSM6DSO_Event_Status_t status;
90 | accGyr.Get_X_Event_Status(&status);
91 | if (status.D6DOrientationStatus)
92 | {
93 | sendOrientation();
94 | // Led blinking.
95 | digitalWrite(LED_BUILTIN, HIGH);
96 | delay(100);
97 | digitalWrite(LED_BUILTIN, LOW);
98 | }
99 | }
100 | }
101 |
102 | void INT1Event_cb()
103 | {
104 | mems_event = 1;
105 | }
106 |
107 | void sendOrientation()
108 | {
109 | uint8_t xl = 0;
110 | uint8_t xh = 0;
111 | uint8_t yl = 0;
112 | uint8_t yh = 0;
113 | uint8_t zl = 0;
114 | uint8_t zh = 0;
115 |
116 | accGyr.Get_6D_Orientation_XL(&xl);
117 | accGyr.Get_6D_Orientation_XH(&xh);
118 | accGyr.Get_6D_Orientation_YL(&yl);
119 | accGyr.Get_6D_Orientation_YH(&yh);
120 | accGyr.Get_6D_Orientation_ZL(&zl);
121 | accGyr.Get_6D_Orientation_ZH(&zh);
122 |
123 | if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 )
124 | {
125 | sprintf( report, "\r\n ________________ " \
126 | "\r\n | | " \
127 | "\r\n | * | " \
128 | "\r\n | | " \
129 | "\r\n | | " \
130 | "\r\n | | " \
131 | "\r\n | | " \
132 | "\r\n |________________| \r\n" );
133 | }
134 |
135 | else if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
136 | {
137 | sprintf( report, "\r\n ________________ " \
138 | "\r\n | | " \
139 | "\r\n | * | " \
140 | "\r\n | | " \
141 | "\r\n | | " \
142 | "\r\n | | " \
143 | "\r\n | | " \
144 | "\r\n |________________| \r\n" );
145 | }
146 |
147 | else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 )
148 | {
149 | sprintf( report, "\r\n ________________ " \
150 | "\r\n | | " \
151 | "\r\n | | " \
152 | "\r\n | | " \
153 | "\r\n | | " \
154 | "\r\n | | " \
155 | "\r\n | * | " \
156 | "\r\n |________________| \r\n" );
157 | }
158 |
159 | else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
160 | {
161 | sprintf( report, "\r\n ________________ " \
162 | "\r\n | | " \
163 | "\r\n | | " \
164 | "\r\n | | " \
165 | "\r\n | | " \
166 | "\r\n | | " \
167 | "\r\n | * | " \
168 | "\r\n |________________| \r\n" );
169 | }
170 |
171 | else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 )
172 | {
173 | sprintf( report, "\r\n __*_____________ " \
174 | "\r\n |________________| \r\n" );
175 | }
176 |
177 | else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 )
178 | {
179 | sprintf( report, "\r\n ________________ " \
180 | "\r\n |________________| " \
181 | "\r\n * \r\n" );
182 | }
183 |
184 | else
185 | {
186 | sprintf( report, "None of the 6D orientation axes is set in LSM6DSO - accelerometer.\r\n" );
187 | }
188 |
189 | SerialPort.print(report);
190 | }
191 |
--------------------------------------------------------------------------------
/examples/LSM6DSO_DoubleTap/LSM6DSO_DoubleTap.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file LSM6DSO_DoubleTap.ino
4 | * @author SRA
5 | * @version V1.0.0
6 | * @date February 2024
7 | * @brief Arduino test application for the STMicrolectronics
8 | * LSM6DSO MEMS IMU 6-axis sensor.
9 | * This application makes use of C++ classes obtained from the C
10 | * components' drivers.
11 | ******************************************************************************
12 | * @attention
13 | *
14 | * © COPYRIGHT(c) 2024 STMicroelectronics
15 | *
16 | * Redistribution and use in source and binary forms, with or without modification,
17 | * are permitted provided that the following conditions are met:
18 | * 1. Redistributions of source code must retain the above copyright notice,
19 | * this list of conditions and the following disclaimer.
20 | * 2. Redistributions in binary form must reproduce the above copyright notice,
21 | * this list of conditions and the following disclaimer in the documentation
22 | * and/or other materials provided with the distribution.
23 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
24 | * may be used to endorse or promote products derived from this software
25 | * without specific prior written permission.
26 | *
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 | *
38 | ******************************************************************************
39 | */
40 | //NOTE: this example isn't compatible with Arduino Uno
41 |
42 |
43 | #include
44 |
45 | #ifdef ARDUINO_SAM_DUE
46 | #define DEV_I2C Wire1
47 | #elif defined(ARDUINO_ARCH_STM32)
48 | #define DEV_I2C Wire
49 | #elif defined(ARDUINO_ARCH_AVR)
50 | #define DEV_I2C Wire
51 | #else
52 | #define DEV_I2C Wire
53 | #endif
54 | #define SerialPort Serial
55 |
56 | #define INT_1 4
57 |
58 | LSM6DSOSensor accGyr(&DEV_I2C);
59 | int32_t accelerometer[3];
60 | int32_t gyroscope[3];
61 |
62 | //Interrupts.
63 | volatile int mems_event = 0;
64 |
65 | char report[256];
66 |
67 | void INT1Event_cb();
68 |
69 | void setup() {
70 | // Led.
71 | pinMode(LED_BUILTIN, OUTPUT);
72 | // Initialize serial for output.
73 | SerialPort.begin(115200);
74 |
75 | // Initialize I2C bus.
76 | DEV_I2C.begin();
77 |
78 | //Interrupts.
79 | attachInterrupt(INT_1, INT1Event_cb, RISING);
80 |
81 | accGyr.begin();
82 | accGyr.Enable_X();
83 | accGyr.Enable_Double_Tap_Detection(LSM6DSO_INT1_PIN);
84 | }
85 |
86 | void loop() {
87 | if (mems_event)
88 | {
89 | mems_event=0;
90 | LSM6DSO_Event_Status_t status;
91 | accGyr.Get_X_Event_Status(&status);
92 | if (status.DoubleTapStatus)
93 | {
94 | // Output data.
95 | SerialPort.println("Double Tap Detected!");
96 |
97 | // Led blinking.
98 | digitalWrite(LED_BUILTIN, HIGH);
99 | delay(100);
100 | digitalWrite(LED_BUILTIN, LOW);
101 | }
102 | }
103 | }
104 |
105 | void INT1Event_cb()
106 | {
107 | mems_event = 1;
108 | }
109 |
--------------------------------------------------------------------------------
/examples/LSM6DSO_FreeFallDetection/LSM6DSO_FreeFallDetection.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file LSM6DSO_FreeFallDetection.ino
4 | * @author SRA
5 | * @version V1.0.0
6 | * @date February 2024
7 | * @brief Arduino test application for the STMicrolectronics
8 | * LSM6DSO MEMS IMU 6-axis sensor.
9 | * This application makes use of C++ classes obtained from the C
10 | * components' drivers.
11 | ******************************************************************************
12 | * @attention
13 | *
14 | * © COPYRIGHT(c) 2024 STMicroelectronics
15 | *
16 | * Redistribution and use in source and binary forms, with or without modification,
17 | * are permitted provided that the following conditions are met:
18 | * 1. Redistributions of source code must retain the above copyright notice,
19 | * this list of conditions and the following disclaimer.
20 | * 2. Redistributions in binary form must reproduce the above copyright notice,
21 | * this list of conditions and the following disclaimer in the documentation
22 | * and/or other materials provided with the distribution.
23 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
24 | * may be used to endorse or promote products derived from this software
25 | * without specific prior written permission.
26 | *
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 | *
38 | ******************************************************************************
39 | */
40 | //NOTE: this example isn't compatible with Arduino Uno
41 |
42 |
43 | #include
44 |
45 | #ifdef ARDUINO_SAM_DUE
46 | #define DEV_I2C Wire1
47 | #elif defined(ARDUINO_ARCH_STM32)
48 | #define DEV_I2C Wire
49 | #elif defined(ARDUINO_ARCH_AVR)
50 | #define DEV_I2C Wire
51 | #else
52 | #define DEV_I2C Wire
53 | #endif
54 | #define SerialPort Serial
55 |
56 | #define INT_1 4
57 |
58 | LSM6DSOSensor accGyr(&DEV_I2C);
59 |
60 | //Interrupts.
61 | volatile int mems_event = 0;
62 |
63 | void INT1Event_cb();
64 |
65 | void setup() {
66 | // Led.
67 | pinMode(LED_BUILTIN, OUTPUT);
68 | // Initialize serial for output.
69 | SerialPort.begin(115200);
70 |
71 | // Initialize I2C bus.
72 | DEV_I2C.begin();
73 |
74 | //Interrupts.
75 | attachInterrupt(INT_1, INT1Event_cb, RISING);
76 |
77 | accGyr.begin();
78 | accGyr.Enable_X();
79 | accGyr.Enable_Free_Fall_Detection(LSM6DSO_INT1_PIN);
80 | }
81 |
82 | void loop() {
83 | if (mems_event)
84 | {
85 | mems_event=0;
86 | LSM6DSO_Event_Status_t status;
87 | accGyr.Get_X_Event_Status(&status);
88 | if (status.FreeFallStatus)
89 | {
90 | // Led blinking.
91 | digitalWrite(LED_BUILTIN, HIGH);
92 | delay(200);
93 | digitalWrite(LED_BUILTIN, LOW);
94 | // Output data.
95 | SerialPort.println("Free Fall Detected!");
96 | }
97 | }
98 | }
99 |
100 | void INT1Event_cb()
101 | {
102 | mems_event = 1;
103 | }
104 |
--------------------------------------------------------------------------------
/examples/LSM6DSO_HelloWorld/LSM6DSO_HelloWorld.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file LSM6DSO_HelloWorld.ino
4 | * @author SRA
5 | * @version V1.0.0
6 | * @date February 2024
7 | * @brief Arduino test application for the STMicrolectronics LSM6DSO
8 | * MEMS IMU 6-axis sensor.
9 | * This application makes use of C++ classes obtained from the C
10 | * components' drivers.
11 | ******************************************************************************
12 | * @attention
13 | *
14 | * © COPYRIGHT(c) 2024 STMicroelectronics
15 | *
16 | * Redistribution and use in source and binary forms, with or without modification,
17 | * are permitted provided that the following conditions are met:
18 | * 1. Redistributions of source code must retain the above copyright notice,
19 | * this list of conditions and the following disclaimer.
20 | * 2. Redistributions in binary form must reproduce the above copyright notice,
21 | * this list of conditions and the following disclaimer in the documentation
22 | * and/or other materials provided with the distribution.
23 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
24 | * may be used to endorse or promote products derived from this software
25 | * without specific prior written permission.
26 | *
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 | *
38 | ******************************************************************************
39 | */
40 |
41 |
42 | // Includes
43 | #include
44 |
45 | #ifdef ARDUINO_SAM_DUE
46 | #define DEV_I2C Wire1
47 | #elif defined(ARDUINO_ARCH_STM32)
48 | #define DEV_I2C Wire
49 | #elif defined(ARDUINO_ARCH_AVR)
50 | #define DEV_I2C Wire
51 | #else
52 | #define DEV_I2C Wire
53 | #endif
54 | #define SerialPort Serial
55 |
56 | // Components
57 | LSM6DSOSensor AccGyr(&DEV_I2C);
58 |
59 | void setup() {
60 | // Led.
61 | pinMode(LED_BUILTIN, OUTPUT);
62 |
63 | // Initialize serial for output.
64 | SerialPort.begin(115200);
65 |
66 | // Initialize I2C bus.
67 | DEV_I2C.begin();
68 |
69 | AccGyr.begin();
70 | AccGyr.Enable_X();
71 | AccGyr.Enable_G();
72 | }
73 |
74 | void loop() {
75 | // Led blinking.
76 | digitalWrite(LED_BUILTIN, HIGH);
77 | delay(250);
78 | digitalWrite(LED_BUILTIN, LOW);
79 | delay(250);
80 |
81 | // Read accelerometer and gyroscope.
82 | int32_t accelerometer[3];
83 | int32_t gyroscope[3];
84 | AccGyr.Get_X_Axes(accelerometer);
85 | AccGyr.Get_G_Axes(gyroscope);
86 |
87 | // Output data.
88 | SerialPort.print("| Acc[mg]: ");
89 | SerialPort.print(accelerometer[0]);
90 | SerialPort.print(" ");
91 | SerialPort.print(accelerometer[1]);
92 | SerialPort.print(" ");
93 | SerialPort.print(accelerometer[2]);
94 | SerialPort.print(" | Gyr[mdps]: ");
95 | SerialPort.print(gyroscope[0]);
96 | SerialPort.print(" ");
97 | SerialPort.print(gyroscope[1]);
98 | SerialPort.print(" ");
99 | SerialPort.print(gyroscope[2]);
100 | SerialPort.println(" |");
101 | }
102 |
--------------------------------------------------------------------------------
/examples/LSM6DSO_Pedometer/LSM6DSO_Pedometer.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file LSM6DSO_Pedometer.ino
4 | * @author SRA
5 | * @version V1.0.0
6 | * @date February 2024
7 | * @brief Arduino test application for the STMicrolectronics
8 | * LSM6DSO MEMS IMU 6-axis sensor.
9 | * This application makes use of C++ classes obtained from the C
10 | * components' drivers.
11 | ******************************************************************************
12 | * @attention
13 | *
14 | * © COPYRIGHT(c) 2024 STMicroelectronics
15 | *
16 | * Redistribution and use in source and binary forms, with or without modification,
17 | * are permitted provided that the following conditions are met:
18 | * 1. Redistributions of source code must retain the above copyright notice,
19 | * this list of conditions and the following disclaimer.
20 | * 2. Redistributions in binary form must reproduce the above copyright notice,
21 | * this list of conditions and the following disclaimer in the documentation
22 | * and/or other materials provided with the distribution.
23 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
24 | * may be used to endorse or promote products derived from this software
25 | * without specific prior written permission.
26 | *
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 | *
38 | ******************************************************************************
39 | */
40 | //NOTE: this example isn't compatible with Arduino Uno
41 |
42 |
43 | #include
44 |
45 | #ifdef ARDUINO_SAM_DUE
46 | #define DEV_I2C Wire1
47 | #elif defined(ARDUINO_ARCH_STM32)
48 | #define DEV_I2C Wire
49 | #elif defined(ARDUINO_ARCH_AVR)
50 | #define DEV_I2C Wire
51 | #else
52 | #define DEV_I2C Wire
53 | #endif
54 | #define SerialPort Serial
55 |
56 | #define INT_1 4
57 |
58 | LSM6DSOSensor accGyr(&DEV_I2C);
59 |
60 | //Interrupts.
61 | volatile int mems_event = 0;
62 |
63 | uint16_t step_count = 0;
64 | char report[256];
65 | uint32_t previous_tick;
66 |
67 | void INT1Event_cb();
68 |
69 | void setup() {
70 | // Led.
71 | pinMode(LED_BUILTIN, OUTPUT);
72 | // Initialize serial for output.
73 | SerialPort.begin(115200);
74 |
75 | // Initialize I2C bus.
76 | DEV_I2C.begin();
77 |
78 | //Interrupts.
79 | attachInterrupt(INT_1, INT1Event_cb, RISING);
80 |
81 | accGyr.begin();
82 | accGyr.Enable_X();
83 | accGyr.Enable_Pedometer();
84 |
85 | previous_tick = millis();
86 | }
87 |
88 | void loop() {
89 | if (mems_event)
90 | {
91 | mems_event=0;
92 | LSM6DSO_Event_Status_t status;
93 | accGyr.Get_X_Event_Status(&status);
94 | if (status.StepStatus)
95 | {
96 | // New step detected, so print the step counter
97 | accGyr.Get_Step_Count(&step_count);
98 | snprintf(report, sizeof(report), "Step counter: %d", step_count);
99 | SerialPort.println(report);
100 |
101 | // Led blinking.
102 | digitalWrite(LED_BUILTIN, HIGH);
103 | delay(100);
104 | digitalWrite(LED_BUILTIN, LOW);
105 | }
106 | }
107 | // Print the step counter in any case every 3000 ms
108 | uint32_t current_tick = millis();
109 | if((current_tick - previous_tick) >= 3000)
110 | {
111 | accGyr.Get_Step_Count(&step_count);
112 | snprintf(report, sizeof(report), "Step counter: %d", step_count);
113 | SerialPort.println(report);
114 | previous_tick = millis();
115 | }
116 |
117 | }
118 |
119 | void INT1Event_cb()
120 | {
121 | mems_event = 1;
122 | }
123 |
--------------------------------------------------------------------------------
/examples/LSM6DSO_SingleTap/LSM6DSO_SingleTap.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file LSM6DSO_SingleTap.ino
4 | * @author SRA
5 | * @version V1.0.0
6 | * @date February 2024
7 | * @brief Arduino test application for the STMicrolectronics
8 | * LSM6DSO MEMS IMU 6-axis sensor.
9 | * This application makes use of C++ classes obtained from the C
10 | * components' drivers.
11 | ******************************************************************************
12 | * @attention
13 | *
14 | * © COPYRIGHT(c) 2024 STMicroelectronics
15 | *
16 | * Redistribution and use in source and binary forms, with or without modification,
17 | * are permitted provided that the following conditions are met:
18 | * 1. Redistributions of source code must retain the above copyright notice,
19 | * this list of conditions and the following disclaimer.
20 | * 2. Redistributions in binary form must reproduce the above copyright notice,
21 | * this list of conditions and the following disclaimer in the documentation
22 | * and/or other materials provided with the distribution.
23 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
24 | * may be used to endorse or promote products derived from this software
25 | * without specific prior written permission.
26 | *
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 | *
38 | ******************************************************************************
39 | */
40 | //NOTE: this example isn't compatible with Arduino Uno
41 |
42 |
43 | #include
44 |
45 | #ifdef ARDUINO_SAM_DUE
46 | #define DEV_I2C Wire1
47 | #elif defined(ARDUINO_ARCH_STM32)
48 | #define DEV_I2C Wire
49 | #elif defined(ARDUINO_ARCH_AVR)
50 | #define DEV_I2C Wire
51 | #else
52 | #define DEV_I2C Wire
53 | #endif
54 | #define SerialPort Serial
55 |
56 | #define INT_1 4
57 |
58 | LSM6DSOSensor accGyr(&DEV_I2C);
59 | int32_t accelerometer[3];
60 | int32_t gyroscope[3];
61 |
62 | //Interrupts.
63 | volatile int mems_event = 0;
64 |
65 | char report[256];
66 |
67 | void INT1Event_cb();
68 |
69 | void setup() {
70 | // Led.
71 | pinMode(LED_BUILTIN, OUTPUT);
72 | // Initialize serial for output.
73 | SerialPort.begin(115200);
74 |
75 | // Initialize I2C bus.
76 | DEV_I2C.begin();
77 |
78 | //Interrupts.
79 | attachInterrupt(INT_1, INT1Event_cb, RISING);
80 |
81 | accGyr.begin();
82 | accGyr.Enable_X();
83 | accGyr.Enable_Single_Tap_Detection(LSM6DSO_INT1_PIN);
84 | }
85 |
86 | void loop() {
87 | if (mems_event)
88 | {
89 | mems_event=0;
90 | LSM6DSO_Event_Status_t status;
91 | accGyr.Get_X_Event_Status(&status);
92 | if (status.TapStatus)
93 | {
94 | // Output data.
95 | SerialPort.println("Single Tap Detected!");
96 |
97 | // Led blinking.
98 | digitalWrite(LED_BUILTIN, HIGH);
99 | delay(100);
100 | digitalWrite(LED_BUILTIN, LOW);
101 | }
102 | }
103 | }
104 |
105 | void INT1Event_cb()
106 | {
107 | mems_event = 1;
108 | }
109 |
--------------------------------------------------------------------------------
/examples/LSM6DSO_TiltDetection/LSM6DSO_TiltDetection.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file LSM6DSO_TiltDetection.ino
4 | * @author SRA
5 | * @version V1.0.0
6 | * @date February 2024
7 | * @brief Arduino test application for the STMicrolectronics
8 | * LSM6DSO MEMS IMU 6-axis sensor.
9 | * This application makes use of C++ classes obtained from the C
10 | * components' drivers.
11 | ******************************************************************************
12 | * @attention
13 | *
14 | * © COPYRIGHT(c) 2024 STMicroelectronics
15 | *
16 | * Redistribution and use in source and binary forms, with or without modification,
17 | * are permitted provided that the following conditions are met:
18 | * 1. Redistributions of source code must retain the above copyright notice,
19 | * this list of conditions and the following disclaimer.
20 | * 2. Redistributions in binary form must reproduce the above copyright notice,
21 | * this list of conditions and the following disclaimer in the documentation
22 | * and/or other materials provided with the distribution.
23 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
24 | * may be used to endorse or promote products derived from this software
25 | * without specific prior written permission.
26 | *
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 | *
38 | ******************************************************************************
39 | */
40 | //NOTE: this example isn't compatible with Arduino Uno
41 |
42 |
43 | #include
44 |
45 | #ifdef ARDUINO_SAM_DUE
46 | #define DEV_I2C Wire1
47 | #elif defined(ARDUINO_ARCH_STM32)
48 | #define DEV_I2C Wire
49 | #elif defined(ARDUINO_ARCH_AVR)
50 | #define DEV_I2C Wire
51 | #else
52 | #define DEV_I2C Wire
53 | #endif
54 | #define SerialPort Serial
55 |
56 | #define INT_1 4
57 |
58 | LSM6DSOSensor accGyr(&DEV_I2C);
59 |
60 | //Interrupts.
61 | volatile int mems_event = 0;
62 |
63 | char report[256];
64 |
65 | void INT1Event_cb();
66 |
67 | void setup() {
68 | // Led.
69 | pinMode(LED_BUILTIN, OUTPUT);
70 | // Initialize serial for output.
71 | SerialPort.begin(115200);
72 |
73 | // Initialize I2C bus.
74 | DEV_I2C.begin();
75 |
76 | //Interrupts.
77 | attachInterrupt(INT_1, INT1Event_cb, RISING);
78 |
79 | accGyr.begin();
80 | accGyr.Enable_X();
81 | accGyr.Enable_Tilt_Detection(LSM6DSO_INT1_PIN);
82 | }
83 |
84 | void loop() {
85 | if (mems_event)
86 | {
87 | mems_event=0;
88 | LSM6DSO_Event_Status_t status;
89 | accGyr.Get_X_Event_Status(&status);
90 | if (status.TiltStatus)
91 | {
92 | // Led blinking.
93 | digitalWrite(LED_BUILTIN, HIGH);
94 | delay(100);
95 | digitalWrite(LED_BUILTIN, LOW);
96 |
97 | // Output data.
98 | SerialPort.println("Tilt Detected!");
99 | }
100 | }
101 | }
102 |
103 | void INT1Event_cb()
104 | {
105 | mems_event = 1;
106 | }
107 |
--------------------------------------------------------------------------------
/examples/LSM6DSO_WakeUpDetection/LSM6DSO_WakeUpDetection.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file LSM6DSO_WakeUpDetection.ino
4 | * @author SRA
5 | * @version V1.0.0
6 | * @date February 2024
7 | * @brief Arduino test application for the STMicrolectronics
8 | * LSM6DSO MEMS IMU 6-axis sensor.
9 | * This application makes use of C++ classes obtained from the C
10 | * components' drivers.
11 | ******************************************************************************
12 | * @attention
13 | *
14 | * © COPYRIGHT(c) 2024 STMicroelectronics
15 | *
16 | * Redistribution and use in source and binary forms, with or without modification,
17 | * are permitted provided that the following conditions are met:
18 | * 1. Redistributions of source code must retain the above copyright notice,
19 | * this list of conditions and the following disclaimer.
20 | * 2. Redistributions in binary form must reproduce the above copyright notice,
21 | * this list of conditions and the following disclaimer in the documentation
22 | * and/or other materials provided with the distribution.
23 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
24 | * may be used to endorse or promote products derived from this software
25 | * without specific prior written permission.
26 | *
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 | *
38 | ******************************************************************************
39 | */
40 | //NOTE: this example isn't compatible with Arduino Uno
41 |
42 |
43 | #include
44 |
45 | #ifdef ARDUINO_SAM_DUE
46 | #define DEV_I2C Wire1
47 | #elif defined(ARDUINO_ARCH_STM32)
48 | #define DEV_I2C Wire
49 | #elif defined(ARDUINO_ARCH_AVR)
50 | #define DEV_I2C Wire
51 | #else
52 | #define DEV_I2C Wire
53 | #endif
54 | #define SerialPort Serial
55 |
56 | #define INT_1 4
57 |
58 | LSM6DSOSensor accGyr(&DEV_I2C);
59 |
60 | //Interrupts.
61 | volatile int mems_event = 0;
62 |
63 | char report[256];
64 |
65 | void INT1Event_cb();
66 |
67 | void setup() {
68 | // Led.
69 | pinMode(LED_BUILTIN, OUTPUT);
70 | // Initialize serial for output.
71 | SerialPort.begin(115200);
72 |
73 | // Initialize I2C bus.
74 | DEV_I2C.begin();
75 |
76 | //Interrupts.
77 | attachInterrupt(INT_1, INT1Event_cb, RISING);
78 |
79 | accGyr.begin();
80 | accGyr.Enable_X();
81 | accGyr.Enable_Wake_Up_Detection(LSM6DSO_INT1_PIN);
82 | }
83 |
84 | void loop() {
85 | if (mems_event)
86 | {
87 | mems_event=0;
88 | LSM6DSO_Event_Status_t status;
89 | accGyr.Get_X_Event_Status(&status);
90 | if (status.WakeUpStatus)
91 | {
92 | // Led blinking.
93 | digitalWrite(LED_BUILTIN, HIGH);
94 | delay(100);
95 | digitalWrite(LED_BUILTIN, LOW);
96 |
97 | // Output data.
98 | SerialPort.println("Wake up Detected!");
99 | }
100 | }
101 | }
102 |
103 | void INT1Event_cb()
104 | {
105 | mems_event = 1;
106 | }
107 |
--------------------------------------------------------------------------------
/keywords.txt:
--------------------------------------------------------------------------------
1 | #######################################
2 | # Syntax Coloring Map For LSM6DSO
3 | #######################################
4 |
5 | #######################################
6 | # Datatypes (KEYWORD1)
7 | #######################################
8 |
9 | LSM6DSOSensor KEYWORD1
10 |
11 | #######################################
12 | # Methods and Functions (KEYWORD2)
13 | #######################################
14 |
15 | begin KEYWORD2
16 | end KEYWORD2
17 | ReadID KEYWORD2
18 | Enable_X KEYWORD2
19 | Disable_X KEYWORD2
20 | Get_X_Sensitivity KEYWORD2
21 | Get_X_ODR KEYWORD2
22 | Set_X_ODR KEYWORD2
23 | Set_X_ODR_With_Mode KEYWORD2
24 | Get_X_FS KEYWORD2
25 | Set_X_FS KEYWORD2
26 | Get_X_AxesRaw KEYWORD2
27 | Get_X_Axes KEYWORD2
28 | Enable_G KEYWORD2
29 | Disable_G KEYWORD2
30 | Get_G_Sensitivity KEYWORD2
31 | Get_G_ODR KEYWORD2
32 | Set_G_ODR KEYWORD2
33 | Set_G_ODR_With_Mode KEYWORD2
34 | Get_G_FS KEYWORD2
35 | Set_G_FS KEYWORD2
36 | Get_G_AxesRaw KEYWORD2
37 | Get_G_Axes KEYWORD2
38 | Read_Reg KEYWORD2
39 | Write_Reg KEYWORD2
40 | Set_Interrupt_Latch KEYWORD2
41 | Set_Interrupt_Polarity KEYWORD2
42 | Set_Interrupt_PinMode KEYWORD2
43 | Enable_Free_Fall_Detection KEYWORD2
44 | Disable_Free_Fall_Detection KEYWORD2
45 | Set_Free_Fall_Threshold KEYWORD2
46 | Set_Free_Fall_Duration KEYWORD2
47 | Enable_Pedometer KEYWORD2
48 | Disable_Pedometer KEYWORD2
49 | Get_Step_Count KEYWORD2
50 | Step_Counter_Reset KEYWORD2
51 | Enable_Tilt_Detection KEYWORD2
52 | Disable_Tilt_Detection KEYWORD2
53 | Enable_Wake_Up_Detection KEYWORD2
54 | Disable_Wake_Up_Detection KEYWORD2
55 | Set_Wake_Up_Threshold KEYWORD2
56 | Set_Wake_Up_Duration KEYWORD2
57 | Enable_Single_Tap_Detection KEYWORD2
58 | Disable_Single_Tap_Detection KEYWORD2
59 | Enable_Double_Tap_Detection KEYWORD2
60 | Disable_Double_Tap_Detection KEYWORD2
61 | Set_Tap_Threshold KEYWORD2
62 | Set_Tap_Shock_Time KEYWORD2
63 | Set_Tap_Quiet_Time KEYWORD2
64 | Set_Tap_Duration_Time KEYWORD2
65 | Enable_6D_Orientation KEYWORD2
66 | Disable_6D_Orientation KEYWORD2
67 | Set_6D_Orientation_Threshold KEYWORD2
68 | Get_6D_Orientation_XL KEYWORD2
69 | Get_6D_Orientation_XH KEYWORD2
70 | Get_6D_Orientation_YL KEYWORD2
71 | Get_6D_Orientation_YH KEYWORD2
72 | Get_6D_Orientation_ZL KEYWORD2
73 | Get_6D_Orientation_ZH KEYWORD2
74 | Get_X_DRDY_Status KEYWORD2
75 | Get_X_Event_Status KEYWORD2
76 | Set_X_SelfTest KEYWORD2
77 | Get_G_DRDY_Status KEYWORD2
78 | Set_G_SelfTest KEYWORD2
79 | Get_FIFO_Num_Samples KEYWORD2
80 | Get_FIFO_Full_Status KEYWORD2
81 | Set_FIFO_INT1_FIFO_Full KEYWORD2
82 | Set_FIFO_Watermark_Level KEYWORD2
83 | Set_FIFO_Stop_On_Fth KEYWORD2
84 | Set_FIFO_Mode KEYWORD2
85 | Get_FIFO_Tag KEYWORD2
86 | Get_FIFO_Data KEYWORD2
87 | Get_FIFO_X_Axes KEYWORD2
88 | Set_FIFO_X_BDR KEYWORD2
89 | Get_FIFO_G_Axes KEYWORD2
90 | Set_FIFO_G_BDR KEYWORD2
91 |
92 | #######################################
93 | # Constants (LITERAL1)
94 | #######################################
95 |
96 | LSM6DSO_OK LITERAL1
97 | LSM6DSO_ERROR LITERAL1
98 | LSM6DSO_INT1_PIN LITERAL1
99 | LSM6DSO_INT2_PIN LITERAL1
100 | LSM6DSO_ACC_SENSITIVITY_FS_2G LITERAL1
101 | LSM6DSO_ACC_SENSITIVITY_FS_4G LITERAL1
102 | LSM6DSO_ACC_SENSITIVITY_FS_8G LITERAL1
103 | LSM6DSO_ACC_SENSITIVITY_FS_16G LITERAL1
104 | LSM6DSO_GYRO_SENSITIVITY_FS_125DPS LITERAL1
105 | LSM6DSO_GYRO_SENSITIVITY_FS_250DPS LITERAL1
106 | LSM6DSO_GYRO_SENSITIVITY_FS_500DPS LITERAL1
107 | LSM6DSO_GYRO_SENSITIVITY_FS_1000DPS LITERAL1
108 | LSM6DSO_GYRO_SENSITIVITY_FS_2000DPS LITERAL1
109 |
110 |
--------------------------------------------------------------------------------
/library.properties:
--------------------------------------------------------------------------------
1 | name=STM32duino LSM6DSO
2 | version=2.2.0
3 | author=SRA
4 | maintainer=stm32duino
5 | sentence=Ultra Low Power inertial measurement unit.
6 | paragraph=This library provides Arduino support for the Ultra Low Power LSM6DSO for STM32 boards.
7 | category=Sensors
8 | url=https://github.com/stm32duino/LSM6DSO
9 | architectures=*
10 |
--------------------------------------------------------------------------------
/src/LSM6DSOSensor.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file LSM6DSOSensor.cpp
4 | * @author SRA
5 | * @version V1.0.0
6 | * @date February 2019
7 | * @brief Implementation of an LSM6DSO Inertial Measurement Unit (IMU) 3 axes
8 | * sensor.
9 | ******************************************************************************
10 | * @attention
11 | *
12 | * © COPYRIGHT(c) 2019 STMicroelectronics
13 | *
14 | * Redistribution and use in source and binary forms, with or without modification,
15 | * are permitted provided that the following conditions are met:
16 | * 1. Redistributions of source code must retain the above copyright notice,
17 | * this list of conditions and the following disclaimer.
18 | * 2. Redistributions in binary form must reproduce the above copyright notice,
19 | * this list of conditions and the following disclaimer in the documentation
20 | * and/or other materials provided with the distribution.
21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
22 | * may be used to endorse or promote products derived from this software
23 | * without specific prior written permission.
24 | *
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 | *
36 | ******************************************************************************
37 | */
38 |
39 |
40 | /* Includes ------------------------------------------------------------------*/
41 |
42 | #include "LSM6DSOSensor.h"
43 |
44 |
45 | /* Class Implementation ------------------------------------------------------*/
46 |
47 | /** Constructor
48 | * @param i2c object of an helper class which handles the I2C peripheral
49 | * @param address the address of the component's instance
50 | */
51 | LSM6DSOSensor::LSM6DSOSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address)
52 | {
53 | dev_spi = NULL;
54 | reg_ctx.write_reg = LSM6DSO_io_write;
55 | reg_ctx.read_reg = LSM6DSO_io_read;
56 | reg_ctx.handle = (void *)this;
57 | acc_is_enabled = 0;
58 | gyro_is_enabled = 0;
59 | }
60 |
61 | /** Constructor
62 | * @param spi object of an helper class which handles the SPI peripheral
63 | * @param cs_pin the chip select pin
64 | * @param spi_speed the SPI speed
65 | */
66 | LSM6DSOSensor::LSM6DSOSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : dev_spi(spi), cs_pin(cs_pin), spi_speed(spi_speed)
67 | {
68 | reg_ctx.write_reg = LSM6DSO_io_write;
69 | reg_ctx.read_reg = LSM6DSO_io_read;
70 | reg_ctx.handle = (void *)this;
71 | dev_i2c = NULL;
72 | address = 0;
73 | acc_is_enabled = 0U;
74 | gyro_is_enabled = 0U;
75 | }
76 |
77 | /**
78 | * @brief Configure the sensor in order to be used
79 | * @retval 0 in case of success, an error code otherwise
80 | */
81 | LSM6DSOStatusTypeDef LSM6DSOSensor::begin()
82 | {
83 | if(dev_spi)
84 | {
85 | // Configure CS pin
86 | pinMode(cs_pin, OUTPUT);
87 | digitalWrite(cs_pin, HIGH);
88 | }
89 |
90 | /* Disable I3C */
91 | if (lsm6dso_i3c_disable_set(®_ctx, LSM6DSO_I3C_DISABLE) != LSM6DSO_OK)
92 | {
93 | return LSM6DSO_ERROR;
94 | }
95 |
96 | /* Enable register address automatically incremented during a multiple byte
97 | access with a serial interface. */
98 | if (lsm6dso_auto_increment_set(®_ctx, PROPERTY_ENABLE) != LSM6DSO_OK)
99 | {
100 | return LSM6DSO_ERROR;
101 | }
102 |
103 | /* Enable BDU */
104 | if (lsm6dso_block_data_update_set(®_ctx, PROPERTY_ENABLE) != LSM6DSO_OK)
105 | {
106 | return LSM6DSO_ERROR;
107 | }
108 |
109 | /* FIFO mode selection */
110 | if (lsm6dso_fifo_mode_set(®_ctx, LSM6DSO_BYPASS_MODE) != LSM6DSO_OK)
111 | {
112 | return LSM6DSO_ERROR;
113 | }
114 |
115 | /* Select default output data rate. */
116 | acc_odr = LSM6DSO_XL_ODR_104Hz;
117 |
118 | /* Output data rate selection - power down. */
119 | if (lsm6dso_xl_data_rate_set(®_ctx, LSM6DSO_XL_ODR_OFF) != LSM6DSO_OK)
120 | {
121 | return LSM6DSO_ERROR;
122 | }
123 |
124 | /* Full scale selection. */
125 | if (lsm6dso_xl_full_scale_set(®_ctx, LSM6DSO_2g) != LSM6DSO_OK)
126 | {
127 | return LSM6DSO_ERROR;
128 | }
129 |
130 | /* Select default output data rate. */
131 | gyro_odr = LSM6DSO_GY_ODR_104Hz;
132 |
133 | /* Output data rate selection - power down. */
134 | if (lsm6dso_gy_data_rate_set(®_ctx, LSM6DSO_GY_ODR_OFF) != LSM6DSO_OK)
135 | {
136 | return LSM6DSO_ERROR;
137 | }
138 |
139 | /* Full scale selection. */
140 | if (lsm6dso_gy_full_scale_set(®_ctx, LSM6DSO_2000dps) != LSM6DSO_OK)
141 | {
142 | return LSM6DSO_ERROR;
143 | }
144 |
145 | acc_is_enabled = 0;
146 | gyro_is_enabled = 0;
147 |
148 | return LSM6DSO_OK;
149 | }
150 |
151 | /**
152 | * @brief Disable the sensor and relative resources
153 | * @retval 0 in case of success, an error code otherwise
154 | */
155 | LSM6DSOStatusTypeDef LSM6DSOSensor::end()
156 | {
157 | /* Disable both acc and gyro */
158 | if (Disable_X() != LSM6DSO_OK)
159 | {
160 | return LSM6DSO_ERROR;
161 | }
162 |
163 | if (Disable_G() != LSM6DSO_OK)
164 | {
165 | return LSM6DSO_ERROR;
166 | }
167 |
168 | /* Reset CS configuration */
169 | if(dev_spi)
170 | {
171 | // Configure CS pin
172 | pinMode(cs_pin, INPUT);
173 | }
174 |
175 | return LSM6DSO_OK;
176 | }
177 |
178 | /**
179 | * @brief Read component ID
180 | * @param Id the WHO_AM_I value
181 | * @retval 0 in case of success, an error code otherwise
182 | */
183 | LSM6DSOStatusTypeDef LSM6DSOSensor::ReadID(uint8_t *Id)
184 | {
185 | if (lsm6dso_device_id_get(®_ctx, Id) != LSM6DSO_OK)
186 | {
187 | return LSM6DSO_ERROR;
188 | }
189 |
190 | return LSM6DSO_OK;
191 | }
192 |
193 | /**
194 | * @brief Enable the LSM6DSO accelerometer sensor
195 | * @retval 0 in case of success, an error code otherwise
196 | */
197 | LSM6DSOStatusTypeDef LSM6DSOSensor::Enable_X()
198 | {
199 | /* Check if the component is already enabled */
200 | if (acc_is_enabled == 1U)
201 | {
202 | return LSM6DSO_OK;
203 | }
204 |
205 | /* Output data rate selection. */
206 | if (lsm6dso_xl_data_rate_set(®_ctx, acc_odr) != LSM6DSO_OK)
207 | {
208 | return LSM6DSO_ERROR;
209 | }
210 |
211 | acc_is_enabled = 1;
212 |
213 | return LSM6DSO_OK;
214 | }
215 |
216 | /**
217 | * @brief Disable the LSM6DSO accelerometer sensor
218 | * @retval 0 in case of success, an error code otherwise
219 | */
220 | LSM6DSOStatusTypeDef LSM6DSOSensor::Disable_X()
221 | {
222 | /* Check if the component is already disabled */
223 | if (acc_is_enabled == 0U)
224 | {
225 | return LSM6DSO_OK;
226 | }
227 |
228 | /* Get current output data rate. */
229 | if (lsm6dso_xl_data_rate_get(®_ctx, &acc_odr) != LSM6DSO_OK)
230 | {
231 | return LSM6DSO_ERROR;
232 | }
233 |
234 | /* Output data rate selection - power down. */
235 | if (lsm6dso_xl_data_rate_set(®_ctx, LSM6DSO_XL_ODR_OFF) != LSM6DSO_OK)
236 | {
237 | return LSM6DSO_ERROR;
238 | }
239 |
240 | acc_is_enabled = 0;
241 |
242 | return LSM6DSO_OK;
243 | }
244 |
245 | /**
246 | * @brief Get the LSM6DSO accelerometer sensor sensitivity
247 | * @param Sensitivity pointer
248 | * @retval 0 in case of success, an error code otherwise
249 | */
250 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_X_Sensitivity(float *Sensitivity)
251 | {
252 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
253 | lsm6dso_fs_xl_t full_scale;
254 |
255 | /* Read actual full scale selection from sensor. */
256 | if (lsm6dso_xl_full_scale_get(®_ctx, &full_scale) != LSM6DSO_OK)
257 | {
258 | return LSM6DSO_ERROR;
259 | }
260 |
261 | /* Store the Sensitivity based on actual full scale. */
262 | switch (full_scale)
263 | {
264 | case LSM6DSO_2g:
265 | *Sensitivity = LSM6DSO_ACC_SENSITIVITY_FS_2G;
266 | break;
267 |
268 | case LSM6DSO_4g:
269 | *Sensitivity = LSM6DSO_ACC_SENSITIVITY_FS_4G;
270 | break;
271 |
272 | case LSM6DSO_8g:
273 | *Sensitivity = LSM6DSO_ACC_SENSITIVITY_FS_8G;
274 | break;
275 |
276 | case LSM6DSO_16g:
277 | *Sensitivity = LSM6DSO_ACC_SENSITIVITY_FS_16G;
278 | break;
279 |
280 | default:
281 | ret = LSM6DSO_ERROR;
282 | break;
283 | }
284 |
285 | return ret;
286 | }
287 |
288 | /**
289 | * @brief Get the LSM6DSO accelerometer sensor output data rate
290 | * @param Odr pointer where the output data rate is written
291 | * @retval 0 in case of success, an error code otherwise
292 | */
293 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_X_ODR(float *Odr)
294 | {
295 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
296 | lsm6dso_odr_xl_t odr_low_level;
297 |
298 | /* Get current output data rate. */
299 | if (lsm6dso_xl_data_rate_get(®_ctx, &odr_low_level) != LSM6DSO_OK)
300 | {
301 | return LSM6DSO_ERROR;
302 | }
303 |
304 | switch (odr_low_level)
305 | {
306 | case LSM6DSO_XL_ODR_OFF:
307 | *Odr = 0.0f;
308 | break;
309 |
310 | case LSM6DSO_XL_ODR_1Hz6:
311 | *Odr = 1.6f;
312 | break;
313 |
314 | case LSM6DSO_XL_ODR_12Hz5:
315 | *Odr = 12.5f;
316 | break;
317 |
318 | case LSM6DSO_XL_ODR_26Hz:
319 | *Odr = 26.0f;
320 | break;
321 |
322 | case LSM6DSO_XL_ODR_52Hz:
323 | *Odr = 52.0f;
324 | break;
325 |
326 | case LSM6DSO_XL_ODR_104Hz:
327 | *Odr = 104.0f;
328 | break;
329 |
330 | case LSM6DSO_XL_ODR_208Hz:
331 | *Odr = 208.0f;
332 | break;
333 |
334 | case LSM6DSO_XL_ODR_417Hz:
335 | *Odr = 417.0f;
336 | break;
337 |
338 | case LSM6DSO_XL_ODR_833Hz:
339 | *Odr = 833.0f;
340 | break;
341 |
342 | case LSM6DSO_XL_ODR_1667Hz:
343 | *Odr = 1667.0f;
344 | break;
345 |
346 | case LSM6DSO_XL_ODR_3333Hz:
347 | *Odr = 3333.0f;
348 | break;
349 |
350 | case LSM6DSO_XL_ODR_6667Hz:
351 | *Odr = 6667.0f;
352 | break;
353 |
354 | default:
355 | ret = LSM6DSO_ERROR;
356 | break;
357 | }
358 |
359 | return ret;
360 | }
361 |
362 | /**
363 | * @brief Set the LSM6DSO accelerometer sensor output data rate
364 | * @param Odr the output data rate value to be set
365 | * @retval 0 in case of success, an error code otherwise
366 | */
367 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_X_ODR(float Odr)
368 | {
369 | return Set_X_ODR_With_Mode(Odr, LSM6DSO_ACC_HIGH_PERFORMANCE_MODE);
370 | }
371 |
372 | /**
373 | * @brief Set the LSM6DSO accelerometer sensor output data rate with operating mode
374 | * @param Odr the output data rate value to be set
375 | * @param Mode the accelerometer operating mode
376 | * @note This function switches off the gyroscope if Ultra Low Power Mode is set
377 | * @retval 0 in case of success, an error code otherwise
378 | */
379 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_X_ODR_With_Mode(float Odr, LSM6DSO_ACC_Operating_Mode_t Mode)
380 | {
381 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
382 |
383 | switch (Mode)
384 | {
385 | case LSM6DSO_ACC_HIGH_PERFORMANCE_MODE:
386 | {
387 | /* We must uncheck Low Power and Ultra Low Power bits if they are enabled */
388 | lsm6dso_ctrl5_c_t val1;
389 | lsm6dso_ctrl6_c_t val2;
390 |
391 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_CTRL5_C, (uint8_t *)&val1, 1) != LSM6DSO_OK)
392 | {
393 | return LSM6DSO_ERROR;
394 | }
395 |
396 | if (val1.xl_ulp_en)
397 | {
398 | /* Power off the accelerometer */
399 | if (acc_is_enabled == 1U)
400 | {
401 | if (lsm6dso_xl_data_rate_set(®_ctx, LSM6DSO_XL_ODR_OFF) != LSM6DSO_OK)
402 | {
403 | return LSM6DSO_ERROR;
404 | }
405 | }
406 |
407 | val1.xl_ulp_en = 0;
408 | if (lsm6dso_write_reg(®_ctx, LSM6DSO_CTRL5_C, (uint8_t *)&val1, 1) != LSM6DSO_OK)
409 | {
410 | return LSM6DSO_ERROR;
411 | }
412 | }
413 |
414 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_CTRL6_C, (uint8_t *)&val2, 1) != LSM6DSO_OK)
415 | {
416 | return LSM6DSO_ERROR;
417 | }
418 |
419 | if (val2.xl_hm_mode)
420 | {
421 | val2.xl_hm_mode = 0;
422 | if (lsm6dso_write_reg(®_ctx, LSM6DSO_CTRL6_C, (uint8_t *)&val2, 1) != LSM6DSO_OK)
423 | {
424 | return LSM6DSO_ERROR;
425 | }
426 | }
427 |
428 | /* ODR should be at least 12.5Hz */
429 | if (Odr < 12.5f)
430 | {
431 | Odr = 12.5f;
432 | }
433 | break;
434 | }
435 | case LSM6DSO_ACC_LOW_POWER_NORMAL_MODE:
436 | {
437 | /* We must uncheck Ultra Low Power bit if it is enabled */
438 | /* and check the Low Power bit if it is unchecked */
439 | lsm6dso_ctrl5_c_t val1;
440 | lsm6dso_ctrl6_c_t val2;
441 |
442 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_CTRL5_C, (uint8_t *)&val1, 1) != LSM6DSO_OK)
443 | {
444 | return LSM6DSO_ERROR;
445 | }
446 |
447 | if (val1.xl_ulp_en)
448 | {
449 | /* Power off the accelerometer */
450 | if (acc_is_enabled == 1U)
451 | {
452 | if (lsm6dso_xl_data_rate_set(®_ctx, LSM6DSO_XL_ODR_OFF) != LSM6DSO_OK)
453 | {
454 | return LSM6DSO_ERROR;
455 | }
456 | }
457 |
458 | val1.xl_ulp_en = 0;
459 | if (lsm6dso_write_reg(®_ctx, LSM6DSO_CTRL5_C, (uint8_t *)&val1, 1) != LSM6DSO_OK)
460 | {
461 | return LSM6DSO_ERROR;
462 | }
463 | }
464 |
465 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_CTRL6_C, (uint8_t *)&val2, 1) != LSM6DSO_OK)
466 | {
467 | return LSM6DSO_ERROR;
468 | }
469 |
470 | if (!val2.xl_hm_mode)
471 | {
472 | val2.xl_hm_mode = 1U;
473 | if (lsm6dso_write_reg(®_ctx, LSM6DSO_CTRL6_C, (uint8_t *)&val2, 1) != LSM6DSO_OK)
474 | {
475 | return LSM6DSO_ERROR;
476 | }
477 | }
478 |
479 | /* Now we need to limit the ODR to 208 Hz if it is higher */
480 | if (Odr > 208.0f)
481 | {
482 | Odr = 208.0f;
483 | }
484 | break;
485 | }
486 | case LSM6DSO_ACC_ULTRA_LOW_POWER_MODE:
487 | {
488 | /* We must uncheck Low Power bit if it is enabled */
489 | /* and check the Ultra Low Power bit if it is unchecked */
490 | /* We must switch off gyro otherwise Ultra Low Power does not work */
491 | lsm6dso_ctrl5_c_t val1;
492 | lsm6dso_ctrl6_c_t val2;
493 |
494 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_CTRL6_C, (uint8_t *)&val2, 1) != LSM6DSO_OK)
495 | {
496 | return LSM6DSO_ERROR;
497 | }
498 |
499 | if (val2.xl_hm_mode)
500 | {
501 | val2.xl_hm_mode = 0;
502 | if (lsm6dso_write_reg(®_ctx, LSM6DSO_CTRL6_C, (uint8_t *)&val2, 1) != LSM6DSO_OK)
503 | {
504 | return LSM6DSO_ERROR;
505 | }
506 | }
507 |
508 | /* Disable Gyro */
509 | if (gyro_is_enabled == 1U)
510 | {
511 | if (Disable_G() != LSM6DSO_OK)
512 | {
513 | return LSM6DSO_ERROR;
514 | }
515 | }
516 |
517 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_CTRL5_C, (uint8_t *)&val1, 1) != LSM6DSO_OK)
518 | {
519 | return LSM6DSO_ERROR;
520 | }
521 |
522 | if (!val1.xl_ulp_en)
523 | {
524 | /* Power off the accelerometer */
525 | if (acc_is_enabled == 1U)
526 | {
527 | if (lsm6dso_xl_data_rate_set(®_ctx, LSM6DSO_XL_ODR_OFF) != LSM6DSO_OK)
528 | {
529 | return LSM6DSO_ERROR;
530 | }
531 | }
532 |
533 | val1.xl_ulp_en = 1U;
534 | if (lsm6dso_write_reg(®_ctx, LSM6DSO_CTRL5_C, (uint8_t *)&val1, 1) != LSM6DSO_OK)
535 | {
536 | return LSM6DSO_ERROR;
537 | }
538 | }
539 |
540 | /* Now we need to limit the ODR to 208 Hz if it is higher */
541 | if (Odr > 208.0f)
542 | {
543 | Odr = 208.0f;
544 | }
545 | break;
546 | }
547 | default:
548 | ret = LSM6DSO_ERROR;
549 | break;
550 | }
551 |
552 | /* Check if the component is enabled */
553 | if (acc_is_enabled == 1U)
554 | {
555 | ret = Set_X_ODR_When_Enabled(Odr);
556 | }
557 | else
558 | {
559 | ret = Set_X_ODR_When_Disabled(Odr);
560 | }
561 |
562 | return ret;
563 | }
564 |
565 | /**
566 | * @brief Set the LSM6DSO accelerometer sensor output data rate when enabled
567 | * @param Odr the functional output data rate to be set
568 | * @retval 0 in case of success, an error code otherwise
569 | */
570 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_X_ODR_When_Enabled(float Odr)
571 | {
572 | lsm6dso_odr_xl_t new_odr;
573 |
574 | new_odr = (Odr <= 1.6f) ? LSM6DSO_XL_ODR_1Hz6
575 | : (Odr <= 12.5f) ? LSM6DSO_XL_ODR_12Hz5
576 | : (Odr <= 26.0f) ? LSM6DSO_XL_ODR_26Hz
577 | : (Odr <= 52.0f) ? LSM6DSO_XL_ODR_52Hz
578 | : (Odr <= 104.0f) ? LSM6DSO_XL_ODR_104Hz
579 | : (Odr <= 208.0f) ? LSM6DSO_XL_ODR_208Hz
580 | : (Odr <= 417.0f) ? LSM6DSO_XL_ODR_417Hz
581 | : (Odr <= 833.0f) ? LSM6DSO_XL_ODR_833Hz
582 | : (Odr <= 1667.0f) ? LSM6DSO_XL_ODR_1667Hz
583 | : (Odr <= 3333.0f) ? LSM6DSO_XL_ODR_3333Hz
584 | : LSM6DSO_XL_ODR_6667Hz;
585 |
586 | /* Output data rate selection. */
587 | if (lsm6dso_xl_data_rate_set(®_ctx, new_odr) != LSM6DSO_OK)
588 | {
589 | return LSM6DSO_ERROR;
590 | }
591 |
592 | return LSM6DSO_OK;
593 | }
594 |
595 | /**
596 | * @brief Set the LSM6DSO accelerometer sensor output data rate when disabled
597 | * @param Odr the functional output data rate to be set
598 | * @retval 0 in case of success, an error code otherwise
599 | */
600 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_X_ODR_When_Disabled(float Odr)
601 | {
602 | acc_odr = (Odr <= 1.6f) ? LSM6DSO_XL_ODR_1Hz6
603 | : (Odr <= 12.5f) ? LSM6DSO_XL_ODR_12Hz5
604 | : (Odr <= 26.0f) ? LSM6DSO_XL_ODR_26Hz
605 | : (Odr <= 52.0f) ? LSM6DSO_XL_ODR_52Hz
606 | : (Odr <= 104.0f) ? LSM6DSO_XL_ODR_104Hz
607 | : (Odr <= 208.0f) ? LSM6DSO_XL_ODR_208Hz
608 | : (Odr <= 417.0f) ? LSM6DSO_XL_ODR_417Hz
609 | : (Odr <= 833.0f) ? LSM6DSO_XL_ODR_833Hz
610 | : (Odr <= 1667.0f) ? LSM6DSO_XL_ODR_1667Hz
611 | : (Odr <= 3333.0f) ? LSM6DSO_XL_ODR_3333Hz
612 | : LSM6DSO_XL_ODR_6667Hz;
613 |
614 | return LSM6DSO_OK;
615 | }
616 |
617 |
618 | /**
619 | * @brief Get the LSM6DSO accelerometer sensor full scale
620 | * @param FullScale pointer where the full scale is written
621 | * @retval 0 in case of success, an error code otherwise
622 | */
623 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_X_FS(int32_t *FullScale)
624 | {
625 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
626 | lsm6dso_fs_xl_t fs_low_level;
627 |
628 | /* Read actual full scale selection from sensor. */
629 | if (lsm6dso_xl_full_scale_get(®_ctx, &fs_low_level) != LSM6DSO_OK)
630 | {
631 | return LSM6DSO_ERROR;
632 | }
633 |
634 | switch (fs_low_level)
635 | {
636 | case LSM6DSO_2g:
637 | *FullScale = 2;
638 | break;
639 |
640 | case LSM6DSO_4g:
641 | *FullScale = 4;
642 | break;
643 |
644 | case LSM6DSO_8g:
645 | *FullScale = 8;
646 | break;
647 |
648 | case LSM6DSO_16g:
649 | *FullScale = 16;
650 | break;
651 |
652 | default:
653 | ret = LSM6DSO_ERROR;
654 | break;
655 | }
656 |
657 | return ret;
658 | }
659 |
660 | /**
661 | * @brief Set the LSM6DSO accelerometer sensor full scale
662 | * @param FullScale the functional full scale to be set
663 | * @retval 0 in case of success, an error code otherwise
664 | */
665 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_X_FS(int32_t FullScale)
666 | {
667 | lsm6dso_fs_xl_t new_fs;
668 |
669 | /* Seems like MISRA C-2012 rule 14.3a violation but only from single file statical analysis point of view because
670 | the parameter passed to the function is not known at the moment of analysis */
671 | new_fs = (FullScale <= 2) ? LSM6DSO_2g
672 | : (FullScale <= 4) ? LSM6DSO_4g
673 | : (FullScale <= 8) ? LSM6DSO_8g
674 | : LSM6DSO_16g;
675 |
676 | if (lsm6dso_xl_full_scale_set(®_ctx, new_fs) != LSM6DSO_OK)
677 | {
678 | return LSM6DSO_ERROR;
679 | }
680 |
681 | return LSM6DSO_OK;
682 | }
683 |
684 | /**
685 | * @brief Get the LSM6DSO accelerometer sensor raw axes
686 | * @param Value pointer where the raw values of the axes are written
687 | * @retval 0 in case of success, an error code otherwise
688 | */
689 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_X_AxesRaw(int16_t *Value)
690 | {
691 | axis3bit16_t data_raw;
692 |
693 | /* Read raw data values. */
694 | if (lsm6dso_acceleration_raw_get(®_ctx, data_raw.u8bit) != LSM6DSO_OK)
695 | {
696 | return LSM6DSO_ERROR;
697 | }
698 |
699 | /* Format the data. */
700 | Value[0] = data_raw.i16bit[0];
701 | Value[1] = data_raw.i16bit[1];
702 | Value[2] = data_raw.i16bit[2];
703 |
704 | return LSM6DSO_OK;
705 | }
706 |
707 |
708 | /**
709 | * @brief Get the LSM6DSO accelerometer sensor axes
710 | * @param Acceleration pointer where the values of the axes are written
711 | * @retval 0 in case of success, an error code otherwise
712 | */
713 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_X_Axes(int32_t *Acceleration)
714 | {
715 | axis3bit16_t data_raw;
716 | float sensitivity = 0.0f;
717 |
718 | /* Read raw data values. */
719 | if (lsm6dso_acceleration_raw_get(®_ctx, data_raw.u8bit) != LSM6DSO_OK)
720 | {
721 | return LSM6DSO_ERROR;
722 | }
723 |
724 | /* Get LSM6DSO actual sensitivity. */
725 | if (Get_X_Sensitivity(&sensitivity) != LSM6DSO_OK)
726 | {
727 | return LSM6DSO_ERROR;
728 | }
729 |
730 | /* Calculate the data. */
731 | Acceleration[0] = (int32_t)((float)((float)data_raw.i16bit[0] * sensitivity));
732 | Acceleration[1] = (int32_t)((float)((float)data_raw.i16bit[1] * sensitivity));
733 | Acceleration[2] = (int32_t)((float)((float)data_raw.i16bit[2] * sensitivity));
734 |
735 | return LSM6DSO_OK;
736 | }
737 |
738 |
739 | /**
740 | * @brief Enable the LSM6DSO gyroscope sensor
741 | * @retval 0 in case of success, an error code otherwise
742 | */
743 | LSM6DSOStatusTypeDef LSM6DSOSensor::Enable_G()
744 | {
745 | /* Check if the component is already enabled */
746 | if (gyro_is_enabled == 1U)
747 | {
748 | return LSM6DSO_OK;
749 | }
750 |
751 | /* Output data rate selection. */
752 | if (lsm6dso_gy_data_rate_set(®_ctx, gyro_odr) != LSM6DSO_OK)
753 | {
754 | return LSM6DSO_ERROR;
755 | }
756 |
757 | gyro_is_enabled = 1;
758 |
759 | return LSM6DSO_OK;
760 | }
761 |
762 |
763 | /**
764 | * @brief Disable the LSM6DSO gyroscope sensor
765 | * @retval 0 in case of success, an error code otherwise
766 | */
767 | LSM6DSOStatusTypeDef LSM6DSOSensor::Disable_G()
768 | {
769 | /* Check if the component is already disabled */
770 | if (gyro_is_enabled == 0U)
771 | {
772 | return LSM6DSO_OK;
773 | }
774 |
775 | /* Get current output data rate. */
776 | if (lsm6dso_gy_data_rate_get(®_ctx, &gyro_odr) != LSM6DSO_OK)
777 | {
778 | return LSM6DSO_ERROR;
779 | }
780 |
781 | /* Output data rate selection - power down. */
782 | if (lsm6dso_gy_data_rate_set(®_ctx, LSM6DSO_GY_ODR_OFF) != LSM6DSO_OK)
783 | {
784 | return LSM6DSO_ERROR;
785 | }
786 |
787 | gyro_is_enabled = 0;
788 |
789 | return LSM6DSO_OK;
790 | }
791 |
792 | /**
793 | * @brief Get the LSM6DSO gyroscope sensor sensitivity
794 | * @param Sensitivity pointer
795 | * @retval 0 in case of success, an error code otherwise
796 | */
797 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_G_Sensitivity(float *Sensitivity)
798 | {
799 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
800 | lsm6dso_fs_g_t full_scale;
801 |
802 | /* Read actual full scale selection from sensor. */
803 | if (lsm6dso_gy_full_scale_get(®_ctx, &full_scale) != LSM6DSO_OK)
804 | {
805 | return LSM6DSO_ERROR;
806 | }
807 |
808 | /* Store the sensitivity based on actual full scale. */
809 | switch (full_scale)
810 | {
811 | case LSM6DSO_125dps:
812 | *Sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_125DPS;
813 | break;
814 |
815 | case LSM6DSO_250dps:
816 | *Sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_250DPS;
817 | break;
818 |
819 | case LSM6DSO_500dps:
820 | *Sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_500DPS;
821 | break;
822 |
823 | case LSM6DSO_1000dps:
824 | *Sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_1000DPS;
825 | break;
826 |
827 | case LSM6DSO_2000dps:
828 | *Sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_2000DPS;
829 | break;
830 |
831 | default:
832 | ret = LSM6DSO_ERROR;
833 | break;
834 | }
835 |
836 | return ret;
837 | }
838 |
839 | /**
840 | * @brief Get the LSM6DSO gyroscope sensor output data rate
841 | * @param Odr pointer where the output data rate is written
842 | * @retval 0 in case of success, an error code otherwise
843 | */
844 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_G_ODR(float *Odr)
845 | {
846 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
847 | lsm6dso_odr_g_t odr_low_level;
848 |
849 | /* Get current output data rate. */
850 | if (lsm6dso_gy_data_rate_get(®_ctx, &odr_low_level) != LSM6DSO_OK)
851 | {
852 | return LSM6DSO_ERROR;
853 | }
854 |
855 | switch (odr_low_level)
856 | {
857 | case LSM6DSO_GY_ODR_OFF:
858 | *Odr = 0.0f;
859 | break;
860 |
861 | case LSM6DSO_GY_ODR_12Hz5:
862 | *Odr = 12.5f;
863 | break;
864 |
865 | case LSM6DSO_GY_ODR_26Hz:
866 | *Odr = 26.0f;
867 | break;
868 |
869 | case LSM6DSO_GY_ODR_52Hz:
870 | *Odr = 52.0f;
871 | break;
872 |
873 | case LSM6DSO_GY_ODR_104Hz:
874 | *Odr = 104.0f;
875 | break;
876 |
877 | case LSM6DSO_GY_ODR_208Hz:
878 | *Odr = 208.0f;
879 | break;
880 |
881 | case LSM6DSO_GY_ODR_417Hz:
882 | *Odr = 417.0f;
883 | break;
884 |
885 | case LSM6DSO_GY_ODR_833Hz:
886 | *Odr = 833.0f;
887 | break;
888 |
889 | case LSM6DSO_GY_ODR_1667Hz:
890 | *Odr = 1667.0f;
891 | break;
892 |
893 | case LSM6DSO_GY_ODR_3333Hz:
894 | *Odr = 3333.0f;
895 | break;
896 |
897 | case LSM6DSO_GY_ODR_6667Hz:
898 | *Odr = 6667.0f;
899 | break;
900 |
901 | default:
902 | ret = LSM6DSO_ERROR;
903 | break;
904 | }
905 |
906 | return ret;
907 | }
908 |
909 | /**
910 | * @brief Set the LSM6DSO gyroscope sensor output data rate
911 | * @param Odr the output data rate value to be set
912 | * @retval 0 in case of success, an error code otherwise
913 | */
914 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_G_ODR(float Odr)
915 | {
916 | return Set_G_ODR_With_Mode(Odr, LSM6DSO_GYRO_HIGH_PERFORMANCE_MODE);
917 | }
918 |
919 | /**
920 | * @brief Set the LSM6DSO gyroscope sensor output data rate with operating mode
921 | * @param Odr the output data rate value to be set
922 | * @param Mode the gyroscope operating mode
923 | * @retval 0 in case of success, an error code otherwise
924 | */
925 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_G_ODR_With_Mode(float Odr, LSM6DSO_GYRO_Operating_Mode_t Mode)
926 | {
927 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
928 |
929 | switch (Mode)
930 | {
931 | case LSM6DSO_GYRO_HIGH_PERFORMANCE_MODE:
932 | {
933 | /* We must uncheck Low Power bit if it is enabled */
934 | lsm6dso_ctrl7_g_t val1;
935 |
936 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_CTRL7_G, (uint8_t *)&val1, 1) != LSM6DSO_OK)
937 | {
938 | return LSM6DSO_ERROR;
939 | }
940 |
941 | if (val1.g_hm_mode)
942 | {
943 | val1.g_hm_mode = 0;
944 | if (lsm6dso_write_reg(®_ctx, LSM6DSO_CTRL7_G, (uint8_t *)&val1, 1) != LSM6DSO_OK)
945 | {
946 | return LSM6DSO_ERROR;
947 | }
948 | }
949 | break;
950 | }
951 | case LSM6DSO_GYRO_LOW_POWER_NORMAL_MODE:
952 | {
953 | /* We must check the Low Power bit if it is unchecked */
954 | lsm6dso_ctrl7_g_t val1;
955 |
956 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_CTRL7_G, (uint8_t *)&val1, 1) != LSM6DSO_OK)
957 | {
958 | return LSM6DSO_ERROR;
959 | }
960 |
961 | if (!val1.g_hm_mode)
962 | {
963 | val1.g_hm_mode = 1U;
964 | if (lsm6dso_write_reg(®_ctx, LSM6DSO_CTRL7_G, (uint8_t *)&val1, 1) != LSM6DSO_OK)
965 | {
966 | return LSM6DSO_ERROR;
967 | }
968 | }
969 |
970 | /* Now we need to limit the ODR to 208 Hz if it is higher */
971 | if (Odr > 208.0f)
972 | {
973 | Odr = 208.0f;
974 | }
975 | break;
976 | }
977 | default:
978 | ret = LSM6DSO_ERROR;
979 | break;
980 | }
981 |
982 | /* Check if the component is enabled */
983 | if (gyro_is_enabled == 1U)
984 | {
985 | ret = Set_G_ODR_When_Enabled(Odr);
986 | }
987 | else
988 | {
989 | ret = Set_G_ODR_When_Disabled(Odr);
990 | }
991 |
992 | return ret;
993 | }
994 |
995 | /**
996 | * @brief Set the LSM6DSO gyroscope sensor output data rate when enabled
997 | * @param Odr the functional output data rate to be set
998 | * @retval 0 in case of success, an error code otherwise
999 | */
1000 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_G_ODR_When_Enabled(float Odr)
1001 | {
1002 | lsm6dso_odr_g_t new_odr;
1003 |
1004 | new_odr = (Odr <= 12.5f) ? LSM6DSO_GY_ODR_12Hz5
1005 | : (Odr <= 26.0f) ? LSM6DSO_GY_ODR_26Hz
1006 | : (Odr <= 52.0f) ? LSM6DSO_GY_ODR_52Hz
1007 | : (Odr <= 104.0f) ? LSM6DSO_GY_ODR_104Hz
1008 | : (Odr <= 208.0f) ? LSM6DSO_GY_ODR_208Hz
1009 | : (Odr <= 417.0f) ? LSM6DSO_GY_ODR_417Hz
1010 | : (Odr <= 833.0f) ? LSM6DSO_GY_ODR_833Hz
1011 | : (Odr <= 1667.0f) ? LSM6DSO_GY_ODR_1667Hz
1012 | : (Odr <= 3333.0f) ? LSM6DSO_GY_ODR_3333Hz
1013 | : LSM6DSO_GY_ODR_6667Hz;
1014 |
1015 | /* Output data rate selection. */
1016 | if (lsm6dso_gy_data_rate_set(®_ctx, new_odr) != LSM6DSO_OK)
1017 | {
1018 | return LSM6DSO_ERROR;
1019 | }
1020 |
1021 | return LSM6DSO_OK;
1022 | }
1023 |
1024 | /**
1025 | * @brief Set the LSM6DSO gyroscope sensor output data rate when disabled
1026 | * @param Odr the functional output data rate to be set
1027 | * @retval 0 in case of success, an error code otherwise
1028 | */
1029 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_G_ODR_When_Disabled(float Odr)
1030 | {
1031 | gyro_odr = (Odr <= 12.5f) ? LSM6DSO_GY_ODR_12Hz5
1032 | : (Odr <= 26.0f) ? LSM6DSO_GY_ODR_26Hz
1033 | : (Odr <= 52.0f) ? LSM6DSO_GY_ODR_52Hz
1034 | : (Odr <= 104.0f) ? LSM6DSO_GY_ODR_104Hz
1035 | : (Odr <= 208.0f) ? LSM6DSO_GY_ODR_208Hz
1036 | : (Odr <= 417.0f) ? LSM6DSO_GY_ODR_417Hz
1037 | : (Odr <= 833.0f) ? LSM6DSO_GY_ODR_833Hz
1038 | : (Odr <= 1667.0f) ? LSM6DSO_GY_ODR_1667Hz
1039 | : (Odr <= 3333.0f) ? LSM6DSO_GY_ODR_3333Hz
1040 | : LSM6DSO_GY_ODR_6667Hz;
1041 |
1042 | return LSM6DSO_OK;
1043 | }
1044 |
1045 |
1046 | /**
1047 | * @brief Get the LSM6DSO gyroscope sensor full scale
1048 | * @param FullScale pointer where the full scale is written
1049 | * @retval 0 in case of success, an error code otherwise
1050 | */
1051 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_G_FS(int32_t *FullScale)
1052 | {
1053 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
1054 | lsm6dso_fs_g_t fs_low_level;
1055 |
1056 | /* Read actual full scale selection from sensor. */
1057 | if (lsm6dso_gy_full_scale_get(®_ctx, &fs_low_level) != LSM6DSO_OK)
1058 | {
1059 | return LSM6DSO_ERROR;
1060 | }
1061 |
1062 | switch (fs_low_level)
1063 | {
1064 | case LSM6DSO_125dps:
1065 | *FullScale = 125;
1066 | break;
1067 |
1068 | case LSM6DSO_250dps:
1069 | *FullScale = 250;
1070 | break;
1071 |
1072 | case LSM6DSO_500dps:
1073 | *FullScale = 500;
1074 | break;
1075 |
1076 | case LSM6DSO_1000dps:
1077 | *FullScale = 1000;
1078 | break;
1079 |
1080 | case LSM6DSO_2000dps:
1081 | *FullScale = 2000;
1082 | break;
1083 |
1084 | default:
1085 | ret = LSM6DSO_ERROR;
1086 | break;
1087 | }
1088 |
1089 | return ret;
1090 | }
1091 |
1092 | /**
1093 | * @brief Set the LSM6DSO gyroscope sensor full scale
1094 | * @param FullScale the functional full scale to be set
1095 | * @retval 0 in case of success, an error code otherwise
1096 | */
1097 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_G_FS(int32_t FullScale)
1098 | {
1099 | lsm6dso_fs_g_t new_fs;
1100 |
1101 | new_fs = (FullScale <= 125) ? LSM6DSO_125dps
1102 | : (FullScale <= 250) ? LSM6DSO_250dps
1103 | : (FullScale <= 500) ? LSM6DSO_500dps
1104 | : (FullScale <= 1000) ? LSM6DSO_1000dps
1105 | : LSM6DSO_2000dps;
1106 |
1107 | if (lsm6dso_gy_full_scale_set(®_ctx, new_fs) != LSM6DSO_OK)
1108 | {
1109 | return LSM6DSO_ERROR;
1110 | }
1111 |
1112 | return LSM6DSO_OK;
1113 | }
1114 |
1115 | /**
1116 | * @brief Get the LSM6DSO gyroscope sensor raw axes
1117 | * @param Value pointer where the raw values of the axes are written
1118 | * @retval 0 in case of success, an error code otherwise
1119 | */
1120 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_G_AxesRaw(int16_t *Value)
1121 | {
1122 | axis3bit16_t data_raw;
1123 |
1124 | /* Read raw data values. */
1125 | if (lsm6dso_angular_rate_raw_get(®_ctx, data_raw.u8bit) != LSM6DSO_OK)
1126 | {
1127 | return LSM6DSO_ERROR;
1128 | }
1129 |
1130 | /* Format the data. */
1131 | Value[0] = data_raw.i16bit[0];
1132 | Value[1] = data_raw.i16bit[1];
1133 | Value[2] = data_raw.i16bit[2];
1134 |
1135 | return LSM6DSO_OK;
1136 | }
1137 |
1138 |
1139 | /**
1140 | * @brief Get the LSM6DSO gyroscope sensor axes
1141 | * @param AngularRate pointer where the values of the axes are written
1142 | * @retval 0 in case of success, an error code otherwise
1143 | */
1144 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_G_Axes(int32_t *AngularRate)
1145 | {
1146 | axis3bit16_t data_raw;
1147 | float sensitivity;
1148 |
1149 | /* Read raw data values. */
1150 | if (lsm6dso_angular_rate_raw_get(®_ctx, data_raw.u8bit) != LSM6DSO_OK)
1151 | {
1152 | return LSM6DSO_ERROR;
1153 | }
1154 |
1155 | /* Get LSM6DSO actual sensitivity. */
1156 | if (Get_G_Sensitivity(&sensitivity) != LSM6DSO_OK)
1157 | {
1158 | return LSM6DSO_ERROR;
1159 | }
1160 |
1161 | /* Calculate the data. */
1162 | AngularRate[0] = (int32_t)((float)((float)data_raw.i16bit[0] * sensitivity));
1163 | AngularRate[1] = (int32_t)((float)((float)data_raw.i16bit[1] * sensitivity));
1164 | AngularRate[2] = (int32_t)((float)((float)data_raw.i16bit[2] * sensitivity));
1165 |
1166 | return LSM6DSO_OK;
1167 | }
1168 |
1169 |
1170 | /**
1171 | * @brief Get the LSM6DSO register value
1172 | * @param Reg address to be read
1173 | * @param Data pointer where the value is written
1174 | * @retval 0 in case of success, an error code otherwise
1175 | */
1176 | LSM6DSOStatusTypeDef LSM6DSOSensor::Read_Reg(uint8_t Reg, uint8_t *Data)
1177 | {
1178 | if (lsm6dso_read_reg(®_ctx, Reg, Data, 1) != LSM6DSO_OK)
1179 | {
1180 | return LSM6DSO_ERROR;
1181 | }
1182 |
1183 | return LSM6DSO_OK;
1184 | }
1185 |
1186 |
1187 | /**
1188 | * @brief Set the LSM6DSO register value
1189 | * @param Reg address to be written
1190 | * @param Data value to be written
1191 | * @retval 0 in case of success, an error code otherwise
1192 | */
1193 | LSM6DSOStatusTypeDef LSM6DSOSensor::Write_Reg(uint8_t Reg, uint8_t Data)
1194 | {
1195 | if (lsm6dso_write_reg(®_ctx, Reg, &Data, 1) != LSM6DSO_OK)
1196 | {
1197 | return LSM6DSO_ERROR;
1198 | }
1199 |
1200 | return LSM6DSO_OK;
1201 | }
1202 |
1203 | /**
1204 | * @brief Set the interrupt latch
1205 | * @param Status value to be written
1206 | * @retval 0 in case of success, an error code otherwise
1207 | */
1208 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_Interrupt_Latch(uint8_t Status)
1209 | {
1210 | if (Status > 1U)
1211 | {
1212 | return LSM6DSO_ERROR;
1213 | }
1214 |
1215 | if (lsm6dso_int_notification_set(®_ctx, (lsm6dso_lir_t)Status) != LSM6DSO_OK)
1216 | {
1217 | return LSM6DSO_ERROR;
1218 | }
1219 |
1220 | return LSM6DSO_OK;
1221 | }
1222 |
1223 | /**
1224 | * @brief Set the interrupt latch
1225 | * @param Status value to be written
1226 | * @retval 0 in case of success, an error code otherwise
1227 | */
1228 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_Interrupt_Polarity(uint8_t Status)
1229 | {
1230 | if (Status > 1U)
1231 | {
1232 | return LSM6DSO_ERROR;
1233 | }
1234 |
1235 | if (lsm6dso_pin_polarity_set(®_ctx, (lsm6dso_h_lactive_t)Status) != LSM6DSO_OK)
1236 | {
1237 | return LSM6DSO_ERROR;
1238 | }
1239 |
1240 | return LSM6DSO_OK;
1241 | }
1242 |
1243 | /**
1244 | * @brief Set the interrupt latch
1245 | * @param Status value to be written
1246 | * @retval 0 in case of success, an error code otherwise
1247 | */
1248 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_Interrupt_PinMode(uint8_t Status)
1249 | {
1250 | if (Status > 1U)
1251 | {
1252 | return LSM6DSO_ERROR;
1253 | }
1254 |
1255 | if (lsm6dso_pin_mode_set(®_ctx, (lsm6dso_pp_od_t)Status) != LSM6DSO_OK)
1256 | {
1257 | return LSM6DSO_ERROR;
1258 | }
1259 |
1260 | return LSM6DSO_OK;
1261 | }
1262 |
1263 | /**
1264 | * @brief Enable free fall detection
1265 | * @param IntPin interrupt pin line to be used
1266 | * @retval 0 in case of success, an error code otherwise
1267 | */
1268 | LSM6DSOStatusTypeDef LSM6DSOSensor::Enable_Free_Fall_Detection(LSM6DSO_SensorIntPin_t IntPin)
1269 | {
1270 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
1271 | lsm6dso_pin_int1_route_t val1;
1272 | lsm6dso_pin_int2_route_t val2;
1273 |
1274 | /* Output Data Rate selection */
1275 | if (Set_X_ODR(416.0f) != LSM6DSO_OK)
1276 | {
1277 | return LSM6DSO_ERROR;
1278 | }
1279 |
1280 | /* Full scale selection */
1281 | if (Set_X_FS(2) != LSM6DSO_OK)
1282 | {
1283 | return LSM6DSO_ERROR;
1284 | }
1285 |
1286 | /* FF_DUR setting */
1287 | if (lsm6dso_ff_dur_set(®_ctx, 0x06) != LSM6DSO_OK)
1288 | {
1289 | return LSM6DSO_ERROR;
1290 | }
1291 |
1292 | /* WAKE_DUR setting */
1293 | if (lsm6dso_wkup_dur_set(®_ctx, 0x00) != LSM6DSO_OK)
1294 | {
1295 | return LSM6DSO_ERROR;
1296 | }
1297 |
1298 | /* SLEEP_DUR setting */
1299 | if (lsm6dso_act_sleep_dur_set(®_ctx, 0x00) != LSM6DSO_OK)
1300 | {
1301 | return LSM6DSO_ERROR;
1302 | }
1303 |
1304 | /* FF_THS setting */
1305 | if (lsm6dso_ff_threshold_set(®_ctx, LSM6DSO_FF_TSH_312mg) != LSM6DSO_OK)
1306 | {
1307 | return LSM6DSO_ERROR;
1308 | }
1309 |
1310 | /* Enable free fall event on either INT1 or INT2 pin */
1311 | switch (IntPin)
1312 | {
1313 | case LSM6DSO_INT1_PIN:
1314 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
1315 | {
1316 | return LSM6DSO_ERROR;
1317 | }
1318 |
1319 | val1.free_fall = PROPERTY_ENABLE;
1320 |
1321 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
1322 | {
1323 | return LSM6DSO_ERROR;
1324 | }
1325 | break;
1326 |
1327 | case LSM6DSO_INT2_PIN:
1328 | if (lsm6dso_pin_int2_route_get(®_ctx, NULL, &val2) != LSM6DSO_OK)
1329 | {
1330 | return LSM6DSO_ERROR;
1331 | }
1332 |
1333 | val2.free_fall = PROPERTY_ENABLE;
1334 |
1335 | if (lsm6dso_pin_int2_route_set(®_ctx, NULL, val2) != LSM6DSO_OK)
1336 | {
1337 | return LSM6DSO_ERROR;
1338 | }
1339 | break;
1340 |
1341 | default:
1342 | ret = LSM6DSO_ERROR;
1343 | break;
1344 | }
1345 |
1346 | return ret;
1347 | }
1348 |
1349 | /**
1350 | * @brief Disable free fall detection
1351 | * @retval 0 in case of success, an error code otherwise
1352 | */
1353 | LSM6DSOStatusTypeDef LSM6DSOSensor::Disable_Free_Fall_Detection()
1354 | {
1355 | lsm6dso_pin_int1_route_t val1;
1356 | lsm6dso_pin_int2_route_t val2;
1357 |
1358 | /* Disable free fall event on both INT1 and INT2 pins */
1359 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
1360 | {
1361 | return LSM6DSO_ERROR;
1362 | }
1363 |
1364 | val1.free_fall = PROPERTY_DISABLE;
1365 |
1366 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
1367 | {
1368 | return LSM6DSO_ERROR;
1369 | }
1370 |
1371 | if (lsm6dso_pin_int2_route_get(®_ctx, NULL, &val2) != LSM6DSO_OK)
1372 | {
1373 | return LSM6DSO_ERROR;
1374 | }
1375 |
1376 | val2.free_fall = PROPERTY_DISABLE;
1377 |
1378 | if (lsm6dso_pin_int2_route_set(®_ctx, NULL, val2) != LSM6DSO_OK)
1379 | {
1380 | return LSM6DSO_ERROR;
1381 | }
1382 |
1383 | /* FF_DUR setting */
1384 | if (lsm6dso_ff_dur_set(®_ctx, 0x00) != LSM6DSO_OK)
1385 | {
1386 | return LSM6DSO_ERROR;
1387 | }
1388 |
1389 | /* FF_THS setting */
1390 | if (lsm6dso_ff_threshold_set(®_ctx, LSM6DSO_FF_TSH_156mg) != LSM6DSO_OK)
1391 | {
1392 | return LSM6DSO_ERROR;
1393 | }
1394 |
1395 | return LSM6DSO_OK;
1396 | }
1397 |
1398 | /**
1399 | * @brief Set free fall threshold
1400 | * @param Threshold free fall detection threshold
1401 | * @retval 0 in case of success, an error code otherwise
1402 | */
1403 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_Free_Fall_Threshold(uint8_t Threshold)
1404 | {
1405 | if (lsm6dso_ff_threshold_set(®_ctx, (lsm6dso_ff_ths_t)Threshold) != LSM6DSO_OK)
1406 | {
1407 | return LSM6DSO_ERROR;
1408 | }
1409 |
1410 | return LSM6DSO_OK;
1411 | }
1412 |
1413 |
1414 | /**
1415 | * @brief Set free fall duration
1416 | * @param Duration free fall detection duration
1417 | * @retval 0 in case of success, an error code otherwise
1418 | */
1419 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_Free_Fall_Duration(uint8_t Duration)
1420 | {
1421 | if (lsm6dso_ff_dur_set(®_ctx, Duration) != LSM6DSO_OK)
1422 | {
1423 | return LSM6DSO_ERROR;
1424 | }
1425 |
1426 | return LSM6DSO_OK;
1427 | }
1428 |
1429 |
1430 | /**
1431 | * @brief Enable pedometer
1432 | * @retval 0 in case of success, an error code otherwise
1433 | */
1434 | LSM6DSOStatusTypeDef LSM6DSOSensor::Enable_Pedometer()
1435 | {
1436 | lsm6dso_pin_int1_route_t val;
1437 | lsm6dso_emb_sens_t emb_sens;
1438 |
1439 | /* Output Data Rate selection */
1440 | if (Set_X_ODR(26.0f) != LSM6DSO_OK)
1441 | {
1442 | return LSM6DSO_ERROR;
1443 | }
1444 |
1445 | /* Full scale selection */
1446 | if (Set_X_FS(2) != LSM6DSO_OK)
1447 | {
1448 | return LSM6DSO_ERROR;
1449 | }
1450 |
1451 | /* Save current embedded features */
1452 | if (lsm6dso_embedded_sens_get(®_ctx, &emb_sens) != LSM6DSO_OK)
1453 | {
1454 | return LSM6DSO_ERROR;
1455 | }
1456 |
1457 | /* Turn off embedded features */
1458 | if (lsm6dso_embedded_sens_off(®_ctx) != LSM6DSO_OK)
1459 | {
1460 | return LSM6DSO_ERROR;
1461 | }
1462 |
1463 | /* Wait for 10 ms */
1464 | delay(10);
1465 |
1466 | /* Enable pedometer algorithm. */
1467 | emb_sens.step = PROPERTY_ENABLE;
1468 |
1469 | if (lsm6dso_pedo_sens_set(®_ctx, LSM6DSO_PEDO_BASE_MODE) != LSM6DSO_OK)
1470 | {
1471 | return LSM6DSO_ERROR;
1472 | }
1473 |
1474 | /* Turn on embedded features */
1475 | if (lsm6dso_embedded_sens_set(®_ctx, &emb_sens) != LSM6DSO_OK)
1476 | {
1477 | return LSM6DSO_ERROR;
1478 | }
1479 |
1480 | /* Enable step detector on INT1 pin */
1481 | if (lsm6dso_pin_int1_route_get(®_ctx, &val) != LSM6DSO_OK)
1482 | {
1483 | return LSM6DSO_ERROR;
1484 | }
1485 |
1486 | val.step_detector = PROPERTY_ENABLE;
1487 |
1488 | if (lsm6dso_pin_int1_route_set(®_ctx, val) != LSM6DSO_OK)
1489 | {
1490 | return LSM6DSO_ERROR;
1491 | }
1492 |
1493 | return LSM6DSO_OK;
1494 | }
1495 |
1496 | /**
1497 | * @brief Disable pedometer
1498 | * @retval 0 in case of success, an error code otherwise
1499 | */
1500 | LSM6DSOStatusTypeDef LSM6DSOSensor::Disable_Pedometer()
1501 | {
1502 | lsm6dso_pin_int1_route_t val1;
1503 | lsm6dso_emb_sens_t emb_sens;
1504 |
1505 | /* Disable step detector on INT1 pin */
1506 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
1507 | {
1508 | return LSM6DSO_ERROR;
1509 | }
1510 |
1511 | val1.step_detector = PROPERTY_DISABLE;
1512 |
1513 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
1514 | {
1515 | return LSM6DSO_ERROR;
1516 | }
1517 |
1518 | /* Save current embedded features */
1519 | if (lsm6dso_embedded_sens_get(®_ctx, &emb_sens) != LSM6DSO_OK)
1520 | {
1521 | return LSM6DSO_ERROR;
1522 | }
1523 |
1524 | /* Disable pedometer algorithm. */
1525 | emb_sens.step = PROPERTY_DISABLE;
1526 |
1527 | if (lsm6dso_embedded_sens_set(®_ctx, &emb_sens) != LSM6DSO_OK)
1528 | {
1529 | return LSM6DSO_ERROR;
1530 | }
1531 |
1532 | return LSM6DSO_OK;
1533 | }
1534 |
1535 | /**
1536 | * @brief Get step count
1537 | * @param StepCount step counter
1538 | * @retval 0 in case of success, an error code otherwise
1539 | */
1540 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_Step_Count(uint16_t *StepCount)
1541 | {
1542 | if (lsm6dso_number_of_steps_get(®_ctx, (uint8_t *)StepCount) != LSM6DSO_OK)
1543 | {
1544 | return LSM6DSO_ERROR;
1545 | }
1546 |
1547 | return LSM6DSO_OK;
1548 | }
1549 |
1550 | /**
1551 | * @brief Enable step counter reset
1552 | * @retval 0 in case of success, an error code otherwise
1553 | */
1554 | LSM6DSOStatusTypeDef LSM6DSOSensor::Step_Counter_Reset()
1555 | {
1556 | if (lsm6dso_steps_reset(®_ctx) != LSM6DSO_OK)
1557 | {
1558 | return LSM6DSO_ERROR;
1559 | }
1560 |
1561 | return LSM6DSO_OK;
1562 | }
1563 |
1564 | /**
1565 | * @brief Enable tilt detection
1566 | * @param IntPin interrupt pin line to be used
1567 | * @retval 0 in case of success, an error code otherwise
1568 | */
1569 | LSM6DSOStatusTypeDef LSM6DSOSensor::Enable_Tilt_Detection(LSM6DSO_SensorIntPin_t IntPin)
1570 | {
1571 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
1572 | lsm6dso_pin_int1_route_t val1;
1573 | lsm6dso_pin_int2_route_t val2;
1574 | lsm6dso_emb_sens_t emb_sens;
1575 |
1576 | /* Output Data Rate selection */
1577 | if (Set_X_ODR(26.0f) != LSM6DSO_OK)
1578 | {
1579 | return LSM6DSO_ERROR;
1580 | }
1581 |
1582 | /* Full scale selection */
1583 | if (Set_X_FS(2) != LSM6DSO_OK)
1584 | {
1585 | return LSM6DSO_ERROR;
1586 | }
1587 |
1588 | /* Save current embedded features */
1589 | if (lsm6dso_embedded_sens_get(®_ctx, &emb_sens) != LSM6DSO_OK)
1590 | {
1591 | return LSM6DSO_ERROR;
1592 | }
1593 |
1594 | /* Turn off embedded features */
1595 | if (lsm6dso_embedded_sens_off(®_ctx) != LSM6DSO_OK)
1596 | {
1597 | return LSM6DSO_ERROR;
1598 | }
1599 |
1600 | /* Wait for 10 ms */
1601 | delay(10);
1602 |
1603 | /* Enable tilt algorithm. */
1604 | emb_sens.tilt = PROPERTY_ENABLE;
1605 |
1606 | /* Turn on embedded features */
1607 | if (lsm6dso_embedded_sens_set(®_ctx, &emb_sens) != LSM6DSO_OK)
1608 | {
1609 | return LSM6DSO_ERROR;
1610 | }
1611 |
1612 | /* Enable tilt event on either INT1 or INT2 pin */
1613 | switch (IntPin)
1614 | {
1615 | case LSM6DSO_INT1_PIN:
1616 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
1617 | {
1618 | return LSM6DSO_ERROR;
1619 | }
1620 |
1621 | val1.tilt = PROPERTY_ENABLE;
1622 |
1623 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
1624 | {
1625 | return LSM6DSO_ERROR;
1626 | }
1627 | break;
1628 |
1629 | case LSM6DSO_INT2_PIN:
1630 | if (lsm6dso_pin_int2_route_get(®_ctx, NULL, &val2) != LSM6DSO_OK)
1631 | {
1632 | return LSM6DSO_ERROR;
1633 | }
1634 |
1635 | val2.tilt = PROPERTY_ENABLE;
1636 |
1637 | if (lsm6dso_pin_int2_route_set(®_ctx, NULL, val2) != LSM6DSO_OK)
1638 | {
1639 | return LSM6DSO_ERROR;
1640 | }
1641 | break;
1642 |
1643 | default:
1644 | ret = LSM6DSO_ERROR;
1645 | break;
1646 | }
1647 |
1648 | return ret;
1649 | }
1650 |
1651 | /**
1652 | * @brief Disable tilt detection
1653 | * @retval 0 in case of success, an error code otherwise
1654 | */
1655 | LSM6DSOStatusTypeDef LSM6DSOSensor::Disable_Tilt_Detection()
1656 | {
1657 | lsm6dso_pin_int1_route_t val1;
1658 | lsm6dso_pin_int2_route_t val2;
1659 | lsm6dso_emb_sens_t emb_sens;
1660 |
1661 | /* Disable tilt event on both INT1 and INT2 pins */
1662 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
1663 | {
1664 | return LSM6DSO_ERROR;
1665 | }
1666 |
1667 | val1.tilt = PROPERTY_DISABLE;
1668 |
1669 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
1670 | {
1671 | return LSM6DSO_ERROR;
1672 | }
1673 |
1674 | if (lsm6dso_pin_int2_route_get(®_ctx, NULL, &val2) != LSM6DSO_OK)
1675 | {
1676 | return LSM6DSO_ERROR;
1677 | }
1678 |
1679 | val2.tilt = PROPERTY_DISABLE;
1680 |
1681 | if (lsm6dso_pin_int2_route_set(®_ctx, NULL, val2) != LSM6DSO_OK)
1682 | {
1683 | return LSM6DSO_ERROR;
1684 | }
1685 |
1686 | /* Save current embedded features */
1687 | if (lsm6dso_embedded_sens_get(®_ctx, &emb_sens) != LSM6DSO_OK)
1688 | {
1689 | return LSM6DSO_ERROR;
1690 | }
1691 |
1692 | /* Disable tilt algorithm. */
1693 | emb_sens.tilt = PROPERTY_DISABLE;
1694 |
1695 | if (lsm6dso_embedded_sens_set(®_ctx, &emb_sens) != LSM6DSO_OK)
1696 | {
1697 | return LSM6DSO_ERROR;
1698 | }
1699 |
1700 | return LSM6DSO_OK;
1701 | }
1702 |
1703 | /**
1704 | * @brief Enable wake up detection
1705 | * @param IntPin interrupt pin line to be used
1706 | * @retval 0 in case of success, an error code otherwise
1707 | */
1708 | LSM6DSOStatusTypeDef LSM6DSOSensor::Enable_Wake_Up_Detection(LSM6DSO_SensorIntPin_t IntPin)
1709 | {
1710 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
1711 | lsm6dso_pin_int1_route_t val1;
1712 | lsm6dso_pin_int2_route_t val2;
1713 |
1714 | /* Output Data Rate selection */
1715 | if (Set_X_ODR(416.0f) != LSM6DSO_OK)
1716 | {
1717 | return LSM6DSO_ERROR;
1718 | }
1719 |
1720 | /* Full scale selection */
1721 | if (Set_X_FS(2) != LSM6DSO_OK)
1722 | {
1723 | return LSM6DSO_ERROR;
1724 | }
1725 |
1726 | /* WAKE_DUR setting */
1727 | if (lsm6dso_wkup_dur_set(®_ctx, 0x00) != LSM6DSO_OK)
1728 | {
1729 | return LSM6DSO_ERROR;
1730 | }
1731 |
1732 | /* Set wake up threshold. */
1733 | if (lsm6dso_wkup_threshold_set(®_ctx, 0x02) != LSM6DSO_OK)
1734 | {
1735 | return LSM6DSO_ERROR;
1736 | }
1737 |
1738 | /* Enable wake up event on either INT1 or INT2 pin */
1739 | switch (IntPin)
1740 | {
1741 | case LSM6DSO_INT1_PIN:
1742 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
1743 | {
1744 | return LSM6DSO_ERROR;
1745 | }
1746 |
1747 | val1.wake_up = PROPERTY_ENABLE;
1748 |
1749 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
1750 | {
1751 | return LSM6DSO_ERROR;
1752 | }
1753 | break;
1754 |
1755 | case LSM6DSO_INT2_PIN:
1756 | if (lsm6dso_pin_int2_route_get(®_ctx, NULL, &val2) != LSM6DSO_OK)
1757 | {
1758 | return LSM6DSO_ERROR;
1759 | }
1760 |
1761 | val2.wake_up = PROPERTY_ENABLE;
1762 |
1763 | if (lsm6dso_pin_int2_route_set(®_ctx, NULL, val2) != LSM6DSO_OK)
1764 | {
1765 | return LSM6DSO_ERROR;
1766 | }
1767 | break;
1768 |
1769 | default:
1770 | ret = LSM6DSO_ERROR;
1771 | break;
1772 | }
1773 |
1774 | return ret;
1775 | }
1776 |
1777 |
1778 | /**
1779 | * @brief Disable wake up detection
1780 | * @retval 0 in case of success, an error code otherwise
1781 | */
1782 | LSM6DSOStatusTypeDef LSM6DSOSensor::Disable_Wake_Up_Detection()
1783 | {
1784 | lsm6dso_pin_int1_route_t val1;
1785 | lsm6dso_pin_int2_route_t val2;
1786 |
1787 | /* Disable wake up event on both INT1 and INT2 pins */
1788 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
1789 | {
1790 | return LSM6DSO_ERROR;
1791 | }
1792 |
1793 | val1.wake_up = PROPERTY_DISABLE;
1794 |
1795 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
1796 | {
1797 | return LSM6DSO_ERROR;
1798 | }
1799 |
1800 | if (lsm6dso_pin_int2_route_get(®_ctx, NULL, &val2) != LSM6DSO_OK)
1801 | {
1802 | return LSM6DSO_ERROR;
1803 | }
1804 |
1805 | val2.wake_up = PROPERTY_DISABLE;
1806 |
1807 | if (lsm6dso_pin_int2_route_set(®_ctx, NULL, val2) != LSM6DSO_OK)
1808 | {
1809 | return LSM6DSO_ERROR;
1810 | }
1811 |
1812 | /* Reset wake up threshold. */
1813 | if (lsm6dso_wkup_threshold_set(®_ctx, 0x00) != LSM6DSO_OK)
1814 | {
1815 | return LSM6DSO_ERROR;
1816 | }
1817 |
1818 | /* WAKE_DUR setting */
1819 | if (lsm6dso_wkup_dur_set(®_ctx, 0x00) != LSM6DSO_OK)
1820 | {
1821 | return LSM6DSO_ERROR;
1822 | }
1823 |
1824 | return LSM6DSO_OK;
1825 | }
1826 |
1827 | /**
1828 | * @brief Set wake up threshold
1829 | * @param Threshold wake up detection threshold
1830 | * @retval 0 in case of success, an error code otherwise
1831 | */
1832 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_Wake_Up_Threshold(uint8_t Threshold)
1833 | {
1834 | /* Set wake up threshold. */
1835 | if (lsm6dso_wkup_threshold_set(®_ctx, Threshold) != LSM6DSO_OK)
1836 | {
1837 | return LSM6DSO_ERROR;
1838 | }
1839 |
1840 | return LSM6DSO_OK;
1841 | }
1842 |
1843 | /**
1844 | * @brief Set wake up duration
1845 | * @param Duration wake up detection duration
1846 | * @retval 0 in case of success, an error code otherwise
1847 | */
1848 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_Wake_Up_Duration(uint8_t Duration)
1849 | {
1850 | /* Set wake up duration. */
1851 | if (lsm6dso_wkup_dur_set(®_ctx, Duration) != LSM6DSO_OK)
1852 | {
1853 | return LSM6DSO_ERROR;
1854 | }
1855 |
1856 | return LSM6DSO_OK;
1857 | }
1858 |
1859 | /**
1860 | * @brief Enable single tap detection
1861 | * @param IntPin interrupt pin line to be used
1862 | * @retval 0 in case of success, an error code otherwise
1863 | */
1864 | LSM6DSOStatusTypeDef LSM6DSOSensor::Enable_Single_Tap_Detection(LSM6DSO_SensorIntPin_t IntPin)
1865 | {
1866 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
1867 | lsm6dso_pin_int1_route_t val1;
1868 | lsm6dso_pin_int2_route_t val2;
1869 |
1870 | /* Output Data Rate selection */
1871 | if (Set_X_ODR(416.0f) != LSM6DSO_OK)
1872 | {
1873 | return LSM6DSO_ERROR;
1874 | }
1875 |
1876 | /* Full scale selection */
1877 | if (Set_X_FS(2) != LSM6DSO_OK)
1878 | {
1879 | return LSM6DSO_ERROR;
1880 | }
1881 |
1882 | /* Enable X direction in tap recognition. */
1883 | if (lsm6dso_tap_detection_on_x_set(®_ctx, PROPERTY_ENABLE) != LSM6DSO_OK)
1884 | {
1885 | return LSM6DSO_ERROR;
1886 | }
1887 |
1888 | /* Enable Y direction in tap recognition. */
1889 | if (lsm6dso_tap_detection_on_y_set(®_ctx, PROPERTY_ENABLE) != LSM6DSO_OK)
1890 | {
1891 | return LSM6DSO_ERROR;
1892 | }
1893 |
1894 | /* Enable Z direction in tap recognition. */
1895 | if (lsm6dso_tap_detection_on_z_set(®_ctx, PROPERTY_ENABLE) != LSM6DSO_OK)
1896 | {
1897 | return LSM6DSO_ERROR;
1898 | }
1899 |
1900 | /* Set tap threshold. */
1901 | if (lsm6dso_tap_threshold_x_set(®_ctx, 0x08) != LSM6DSO_OK)
1902 | {
1903 | return LSM6DSO_ERROR;
1904 | }
1905 |
1906 | /* Set tap shock time window. */
1907 | if (lsm6dso_tap_shock_set(®_ctx, 0x02) != LSM6DSO_OK)
1908 | {
1909 | return LSM6DSO_ERROR;
1910 | }
1911 |
1912 | /* Set tap quiet time window. */
1913 | if (lsm6dso_tap_quiet_set(®_ctx, 0x01) != LSM6DSO_OK)
1914 | {
1915 | return LSM6DSO_ERROR;
1916 | }
1917 |
1918 | /* _NOTE_: Tap duration time window - don't care for single tap. */
1919 |
1920 | /* _NOTE_: Single/Double Tap event - don't care of this flag for single tap. */
1921 |
1922 | /* Enable single tap event on either INT1 or INT2 pin */
1923 | switch (IntPin)
1924 | {
1925 | case LSM6DSO_INT1_PIN:
1926 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
1927 | {
1928 | return LSM6DSO_ERROR;
1929 | }
1930 |
1931 | val1.single_tap = PROPERTY_ENABLE;
1932 |
1933 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
1934 | {
1935 | return LSM6DSO_ERROR;
1936 | }
1937 | break;
1938 |
1939 | case LSM6DSO_INT2_PIN:
1940 | if (lsm6dso_pin_int2_route_get(®_ctx, NULL, &val2) != LSM6DSO_OK)
1941 | {
1942 | return LSM6DSO_ERROR;
1943 | }
1944 |
1945 | val2.single_tap = PROPERTY_ENABLE;
1946 |
1947 | if (lsm6dso_pin_int2_route_set(®_ctx, NULL, val2) != LSM6DSO_OK)
1948 | {
1949 | return LSM6DSO_ERROR;
1950 | }
1951 | break;
1952 |
1953 | default:
1954 | ret = LSM6DSO_ERROR;
1955 | break;
1956 | }
1957 |
1958 | return ret;
1959 | }
1960 |
1961 | /**
1962 | * @brief Disable single tap detection
1963 | * @param pObj the device pObj
1964 | * @retval 0 in case of success, an error code otherwise
1965 | */
1966 | LSM6DSOStatusTypeDef LSM6DSOSensor::Disable_Single_Tap_Detection()
1967 | {
1968 | lsm6dso_pin_int1_route_t val1;
1969 | lsm6dso_pin_int2_route_t val2;
1970 |
1971 | /* Disable single tap event on both INT1 and INT2 pins */
1972 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
1973 | {
1974 | return LSM6DSO_ERROR;
1975 | }
1976 |
1977 | val1.single_tap = PROPERTY_DISABLE;
1978 |
1979 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
1980 | {
1981 | return LSM6DSO_ERROR;
1982 | }
1983 |
1984 | if (lsm6dso_pin_int2_route_get(®_ctx, NULL, &val2) != LSM6DSO_OK)
1985 | {
1986 | return LSM6DSO_ERROR;
1987 | }
1988 |
1989 | val2.single_tap = PROPERTY_DISABLE;
1990 |
1991 | if (lsm6dso_pin_int2_route_set(®_ctx, NULL, val2) != LSM6DSO_OK)
1992 | {
1993 | return LSM6DSO_ERROR;
1994 | }
1995 |
1996 | /* Reset tap quiet time window. */
1997 | if (lsm6dso_tap_quiet_set(®_ctx, 0x00) != LSM6DSO_OK)
1998 | {
1999 | return LSM6DSO_ERROR;
2000 | }
2001 |
2002 | /* Reset tap shock time window. */
2003 | if (lsm6dso_tap_shock_set(®_ctx, 0x00) != LSM6DSO_OK)
2004 | {
2005 | return LSM6DSO_ERROR;
2006 | }
2007 |
2008 | /* Reset tap threshold. */
2009 | if (lsm6dso_tap_threshold_x_set(®_ctx, 0x00) != LSM6DSO_OK)
2010 | {
2011 | return LSM6DSO_ERROR;
2012 | }
2013 |
2014 | /* Disable Z direction in tap recognition. */
2015 | if (lsm6dso_tap_detection_on_z_set(®_ctx, PROPERTY_DISABLE) != LSM6DSO_OK)
2016 | {
2017 | return LSM6DSO_ERROR;
2018 | }
2019 |
2020 | /* Disable Y direction in tap recognition. */
2021 | if (lsm6dso_tap_detection_on_y_set(®_ctx, PROPERTY_DISABLE) != LSM6DSO_OK)
2022 | {
2023 | return LSM6DSO_ERROR;
2024 | }
2025 |
2026 | /* Disable X direction in tap recognition. */
2027 | if (lsm6dso_tap_detection_on_x_set(®_ctx, PROPERTY_DISABLE) != LSM6DSO_OK)
2028 | {
2029 | return LSM6DSO_ERROR;
2030 | }
2031 |
2032 | return LSM6DSO_OK;
2033 | }
2034 |
2035 | /**
2036 | * @brief Enable double tap detection
2037 | * @param IntPin interrupt pin line to be used
2038 | * @retval 0 in case of success, an error code otherwise
2039 | */
2040 | LSM6DSOStatusTypeDef LSM6DSOSensor::Enable_Double_Tap_Detection(LSM6DSO_SensorIntPin_t IntPin)
2041 | {
2042 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
2043 | lsm6dso_pin_int1_route_t val1;
2044 | lsm6dso_pin_int2_route_t val2;
2045 |
2046 | /* Output Data Rate selection */
2047 | if (Set_X_ODR(416.0f) != LSM6DSO_OK)
2048 | {
2049 | return LSM6DSO_ERROR;
2050 | }
2051 |
2052 | /* Full scale selection */
2053 | if (Set_X_FS(2) != LSM6DSO_OK)
2054 | {
2055 | return LSM6DSO_ERROR;
2056 | }
2057 |
2058 | /* Enable X direction in tap recognition. */
2059 | if (lsm6dso_tap_detection_on_x_set(®_ctx, PROPERTY_ENABLE) != LSM6DSO_OK)
2060 | {
2061 | return LSM6DSO_ERROR;
2062 | }
2063 |
2064 | /* Enable Y direction in tap recognition. */
2065 | if (lsm6dso_tap_detection_on_y_set(®_ctx, PROPERTY_ENABLE) != LSM6DSO_OK)
2066 | {
2067 | return LSM6DSO_ERROR;
2068 | }
2069 |
2070 | /* Enable Z direction in tap recognition. */
2071 | if (lsm6dso_tap_detection_on_z_set(®_ctx, PROPERTY_ENABLE) != LSM6DSO_OK)
2072 | {
2073 | return LSM6DSO_ERROR;
2074 | }
2075 |
2076 | /* Set tap threshold. */
2077 | if (lsm6dso_tap_threshold_x_set(®_ctx, 0x08) != LSM6DSO_OK)
2078 | {
2079 | return LSM6DSO_ERROR;
2080 | }
2081 |
2082 | /* Set tap shock time window. */
2083 | if (lsm6dso_tap_shock_set(®_ctx, 0x03) != LSM6DSO_OK)
2084 | {
2085 | return LSM6DSO_ERROR;
2086 | }
2087 |
2088 | /* Set tap quiet time window. */
2089 | if (lsm6dso_tap_quiet_set(®_ctx, 0x03) != LSM6DSO_OK)
2090 | {
2091 | return LSM6DSO_ERROR;
2092 | }
2093 |
2094 | /* Set tap duration time window. */
2095 | if (lsm6dso_tap_dur_set(®_ctx, 0x08) != LSM6DSO_OK)
2096 | {
2097 | return LSM6DSO_ERROR;
2098 | }
2099 |
2100 | /* Single and double tap enabled. */
2101 | if (lsm6dso_tap_mode_set(®_ctx, LSM6DSO_BOTH_SINGLE_DOUBLE) != LSM6DSO_OK)
2102 | {
2103 | return LSM6DSO_ERROR;
2104 | }
2105 |
2106 | /* Enable double tap event on either INT1 or INT2 pin */
2107 | switch (IntPin)
2108 | {
2109 | case LSM6DSO_INT1_PIN:
2110 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
2111 | {
2112 | return LSM6DSO_ERROR;
2113 | }
2114 |
2115 | val1.double_tap = PROPERTY_ENABLE;
2116 |
2117 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
2118 | {
2119 | return LSM6DSO_ERROR;
2120 | }
2121 | break;
2122 |
2123 | case LSM6DSO_INT2_PIN:
2124 | if (lsm6dso_pin_int2_route_get(®_ctx, NULL, &val2) != LSM6DSO_OK)
2125 | {
2126 | return LSM6DSO_ERROR;
2127 | }
2128 |
2129 | val2.double_tap = PROPERTY_ENABLE;
2130 |
2131 | if (lsm6dso_pin_int2_route_set(®_ctx, NULL, val2) != LSM6DSO_OK)
2132 | {
2133 | return LSM6DSO_ERROR;
2134 | }
2135 | break;
2136 |
2137 | default:
2138 | ret = LSM6DSO_ERROR;
2139 | break;
2140 | }
2141 |
2142 | return ret;
2143 | }
2144 |
2145 | /**
2146 | * @brief Disable double tap detection
2147 | * @retval 0 in case of success, an error code otherwise
2148 | */
2149 | LSM6DSOStatusTypeDef LSM6DSOSensor::Disable_Double_Tap_Detection()
2150 | {
2151 | lsm6dso_pin_int1_route_t val1;
2152 | lsm6dso_pin_int2_route_t val2;
2153 |
2154 | /* Disable double tap event on both INT1 and INT2 pins */
2155 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
2156 | {
2157 | return LSM6DSO_ERROR;
2158 | }
2159 |
2160 | val1.double_tap = PROPERTY_DISABLE;
2161 |
2162 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
2163 | {
2164 | return LSM6DSO_ERROR;
2165 | }
2166 |
2167 | if (lsm6dso_pin_int2_route_get(®_ctx, NULL, &val2) != LSM6DSO_OK)
2168 | {
2169 | return LSM6DSO_ERROR;
2170 | }
2171 |
2172 | val2.double_tap = PROPERTY_DISABLE;
2173 |
2174 | if (lsm6dso_pin_int2_route_set(®_ctx, NULL, val2) != LSM6DSO_OK)
2175 | {
2176 | return LSM6DSO_ERROR;
2177 | }
2178 |
2179 | /* Only single tap enabled. */
2180 | if (lsm6dso_tap_mode_set(®_ctx, LSM6DSO_ONLY_SINGLE) != LSM6DSO_OK)
2181 | {
2182 | return LSM6DSO_ERROR;
2183 | }
2184 |
2185 | /* Reset tap duration time window. */
2186 | if (lsm6dso_tap_dur_set(®_ctx, 0x00) != LSM6DSO_OK)
2187 | {
2188 | return LSM6DSO_ERROR;
2189 | }
2190 |
2191 | /* Reset tap quiet time window. */
2192 | if (lsm6dso_tap_quiet_set(®_ctx, 0x00) != LSM6DSO_OK)
2193 | {
2194 | return LSM6DSO_ERROR;
2195 | }
2196 |
2197 | /* Reset tap shock time window. */
2198 | if (lsm6dso_tap_shock_set(®_ctx, 0x00) != LSM6DSO_OK)
2199 | {
2200 | return LSM6DSO_ERROR;
2201 | }
2202 |
2203 | /* Reset tap threshold. */
2204 | if (lsm6dso_tap_threshold_x_set(®_ctx, 0x00) != LSM6DSO_OK)
2205 | {
2206 | return LSM6DSO_ERROR;
2207 | }
2208 |
2209 | /* Disable Z direction in tap recognition. */
2210 | if (lsm6dso_tap_detection_on_z_set(®_ctx, PROPERTY_DISABLE) != LSM6DSO_OK)
2211 | {
2212 | return LSM6DSO_ERROR;
2213 | }
2214 |
2215 | /* Disable Y direction in tap recognition. */
2216 | if (lsm6dso_tap_detection_on_y_set(®_ctx, PROPERTY_DISABLE) != LSM6DSO_OK)
2217 | {
2218 | return LSM6DSO_ERROR;
2219 | }
2220 |
2221 | /* Disable X direction in tap recognition. */
2222 | if (lsm6dso_tap_detection_on_x_set(®_ctx, PROPERTY_DISABLE) != LSM6DSO_OK)
2223 | {
2224 | return LSM6DSO_ERROR;
2225 | }
2226 |
2227 | return LSM6DSO_OK;
2228 | }
2229 |
2230 | /**
2231 | * @brief Set tap threshold
2232 | * @param Threshold tap threshold
2233 | * @retval 0 in case of success, an error code otherwise
2234 | */
2235 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_Tap_Threshold(uint8_t Threshold)
2236 | {
2237 | /* Set tap threshold. */
2238 | if (lsm6dso_tap_threshold_x_set(®_ctx, Threshold) != LSM6DSO_OK)
2239 | {
2240 | return LSM6DSO_ERROR;
2241 | }
2242 |
2243 | return LSM6DSO_OK;
2244 | }
2245 |
2246 | /**
2247 | * @brief Set tap shock time
2248 | * @param Time tap shock time
2249 | * @retval 0 in case of success, an error code otherwise
2250 | */
2251 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_Tap_Shock_Time(uint8_t Time)
2252 | {
2253 | /* Set tap shock time window. */
2254 | if (lsm6dso_tap_shock_set(®_ctx, Time) != LSM6DSO_OK)
2255 | {
2256 | return LSM6DSO_ERROR;
2257 | }
2258 |
2259 | return LSM6DSO_OK;
2260 | }
2261 |
2262 | /**
2263 | * @brief Set tap quiet time
2264 | * @param Time tap quiet time
2265 | * @retval 0 in case of success, an error code otherwise
2266 | */
2267 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_Tap_Quiet_Time(uint8_t Time)
2268 | {
2269 | /* Set tap quiet time window. */
2270 | if (lsm6dso_tap_quiet_set(®_ctx, Time) != LSM6DSO_OK)
2271 | {
2272 | return LSM6DSO_ERROR;
2273 | }
2274 |
2275 | return LSM6DSO_OK;
2276 | }
2277 |
2278 | /**
2279 | * @brief Set tap duration time
2280 | * @param Time tap duration time
2281 | * @retval 0 in case of success, an error code otherwise
2282 | */
2283 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_Tap_Duration_Time(uint8_t Time)
2284 | {
2285 | /* Set tap duration time window. */
2286 | if (lsm6dso_tap_dur_set(®_ctx, Time) != LSM6DSO_OK)
2287 | {
2288 | return LSM6DSO_ERROR;
2289 | }
2290 |
2291 | return LSM6DSO_OK;
2292 | }
2293 |
2294 | /**
2295 | * @brief Enable 6D orientation detection
2296 | * @param IntPin interrupt pin line to be used
2297 | * @retval 0 in case of success, an error code otherwise
2298 | */
2299 | LSM6DSOStatusTypeDef LSM6DSOSensor::Enable_6D_Orientation(LSM6DSO_SensorIntPin_t IntPin)
2300 | {
2301 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
2302 | lsm6dso_pin_int1_route_t val1;
2303 | lsm6dso_pin_int2_route_t val2;
2304 |
2305 | /* Output Data Rate selection */
2306 | if (Set_X_ODR(416.0f) != LSM6DSO_OK)
2307 | {
2308 | return LSM6DSO_ERROR;
2309 | }
2310 |
2311 | /* Full scale selection */
2312 | if (Set_X_FS(2) != LSM6DSO_OK)
2313 | {
2314 | return LSM6DSO_ERROR;
2315 | }
2316 |
2317 | /* 6D orientation enabled. */
2318 | if (lsm6dso_6d_threshold_set(®_ctx, LSM6DSO_DEG_60) != LSM6DSO_OK)
2319 | {
2320 | return LSM6DSO_ERROR;
2321 | }
2322 |
2323 | /* Enable 6D orientation event on either INT1 or INT2 pin */
2324 | switch (IntPin)
2325 | {
2326 | case LSM6DSO_INT1_PIN:
2327 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
2328 | {
2329 | return LSM6DSO_ERROR;
2330 | }
2331 |
2332 | val1.six_d = PROPERTY_ENABLE;
2333 |
2334 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
2335 | {
2336 | return LSM6DSO_ERROR;
2337 | }
2338 | break;
2339 |
2340 | case LSM6DSO_INT2_PIN:
2341 | if (lsm6dso_pin_int2_route_get(®_ctx, NULL, &val2) != LSM6DSO_OK)
2342 | {
2343 | return LSM6DSO_ERROR;
2344 | }
2345 |
2346 | val2.six_d = PROPERTY_ENABLE;
2347 |
2348 | if (lsm6dso_pin_int2_route_set(®_ctx, NULL, val2) != LSM6DSO_OK)
2349 | {
2350 | return LSM6DSO_ERROR;
2351 | }
2352 | break;
2353 |
2354 | default:
2355 | ret = LSM6DSO_ERROR;
2356 | break;
2357 | }
2358 |
2359 | return ret;
2360 | }
2361 |
2362 | /**
2363 | * @brief Disable 6D orientation detection
2364 | * @retval 0 in case of success, an error code otherwise
2365 | */
2366 | LSM6DSOStatusTypeDef LSM6DSOSensor::Disable_6D_Orientation()
2367 | {
2368 | lsm6dso_pin_int1_route_t val1;
2369 | lsm6dso_pin_int2_route_t val2;
2370 |
2371 | /* Disable 6D orientation event on both INT1 and INT2 pins */
2372 | if (lsm6dso_pin_int1_route_get(®_ctx, &val1) != LSM6DSO_OK)
2373 | {
2374 | return LSM6DSO_ERROR;
2375 | }
2376 |
2377 | val1.six_d = PROPERTY_DISABLE;
2378 |
2379 | if (lsm6dso_pin_int1_route_set(®_ctx, val1) != LSM6DSO_OK)
2380 | {
2381 | return LSM6DSO_ERROR;
2382 | }
2383 |
2384 | if (lsm6dso_pin_int2_route_get(®_ctx, NULL, &val2) != LSM6DSO_OK)
2385 | {
2386 | return LSM6DSO_ERROR;
2387 | }
2388 |
2389 | val2.six_d = PROPERTY_DISABLE;
2390 |
2391 | if (lsm6dso_pin_int2_route_set(®_ctx, NULL, val2) != LSM6DSO_OK)
2392 | {
2393 | return LSM6DSO_ERROR;
2394 | }
2395 |
2396 | /* Reset 6D orientation. */
2397 | if (lsm6dso_6d_threshold_set(®_ctx, LSM6DSO_DEG_80) != LSM6DSO_OK)
2398 | {
2399 | return LSM6DSO_ERROR;
2400 | }
2401 |
2402 | return LSM6DSO_OK;
2403 | }
2404 |
2405 | /**
2406 | * @brief Set 6D orientation threshold
2407 | * @param Threshold 6D Orientation detection threshold
2408 | * @retval 0 in case of success, an error code otherwise
2409 | */
2410 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_6D_Orientation_Threshold(uint8_t Threshold)
2411 | {
2412 | if (lsm6dso_6d_threshold_set(®_ctx, (lsm6dso_sixd_ths_t)Threshold) != LSM6DSO_OK)
2413 | {
2414 | return LSM6DSO_ERROR;
2415 | }
2416 |
2417 | return LSM6DSO_OK;
2418 | }
2419 |
2420 | /**
2421 | * @brief Get the status of XLow orientation
2422 | * @param XLow the status of XLow orientation
2423 | * @retval 0 in case of success, an error code otherwise
2424 | */
2425 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_6D_Orientation_XL(uint8_t *XLow)
2426 | {
2427 | lsm6dso_d6d_src_t data;
2428 |
2429 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSO_OK)
2430 | {
2431 | return LSM6DSO_ERROR;
2432 | }
2433 |
2434 | *XLow = data.xl;
2435 |
2436 | return LSM6DSO_OK;
2437 | }
2438 |
2439 | /**
2440 | * @brief Get the status of XHigh orientation
2441 | * @param XHigh the status of XHigh orientation
2442 | * @retval 0 in case of success, an error code otherwise
2443 | */
2444 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_6D_Orientation_XH(uint8_t *XHigh)
2445 | {
2446 | lsm6dso_d6d_src_t data;
2447 |
2448 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSO_OK)
2449 | {
2450 | return LSM6DSO_ERROR;
2451 | }
2452 |
2453 | *XHigh = data.xh;
2454 |
2455 | return LSM6DSO_OK;
2456 | }
2457 |
2458 | /**
2459 | * @brief Get the status of YLow orientation
2460 | * @param YLow the status of YLow orientation
2461 | * @retval 0 in case of success, an error code otherwise
2462 | */
2463 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_6D_Orientation_YL(uint8_t *YLow)
2464 | {
2465 | lsm6dso_d6d_src_t data;
2466 |
2467 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSO_OK)
2468 | {
2469 | return LSM6DSO_ERROR;
2470 | }
2471 |
2472 | *YLow = data.yl;
2473 |
2474 | return LSM6DSO_OK;
2475 | }
2476 |
2477 | /**
2478 | * @brief Get the status of YHigh orientation
2479 | * @param YHigh the status of YHigh orientation
2480 | * @retval 0 in case of success, an error code otherwise
2481 | */
2482 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_6D_Orientation_YH(uint8_t *YHigh)
2483 | {
2484 | lsm6dso_d6d_src_t data;
2485 |
2486 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSO_OK)
2487 | {
2488 | return LSM6DSO_ERROR;
2489 | }
2490 |
2491 | *YHigh = data.yh;
2492 |
2493 | return LSM6DSO_OK;
2494 | }
2495 |
2496 | /**
2497 | * @brief Get the status of ZLow orientation
2498 | * @param ZLow the status of ZLow orientation
2499 | * @retval 0 in case of success, an error code otherwise
2500 | */
2501 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_6D_Orientation_ZL(uint8_t *ZLow)
2502 | {
2503 | lsm6dso_d6d_src_t data;
2504 |
2505 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSO_OK)
2506 | {
2507 | return LSM6DSO_ERROR;
2508 | }
2509 |
2510 | *ZLow = data.zl;
2511 |
2512 | return LSM6DSO_OK;
2513 | }
2514 |
2515 | /**
2516 | * @brief Get the status of ZHigh orientation
2517 | * @param ZHigh the status of ZHigh orientation
2518 | * @retval 0 in case of success, an error code otherwise
2519 | */
2520 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_6D_Orientation_ZH(uint8_t *ZHigh)
2521 | {
2522 | lsm6dso_d6d_src_t data;
2523 |
2524 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSO_OK)
2525 | {
2526 | return LSM6DSO_ERROR;
2527 | }
2528 |
2529 | *ZHigh = data.zh;
2530 |
2531 | return LSM6DSO_OK;
2532 | }
2533 |
2534 | /**
2535 | * @brief Get the LSM6DSO ACC data ready bit value
2536 | * @param Status the status of data ready bit
2537 | * @retval 0 in case of success, an error code otherwise
2538 | */
2539 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_X_DRDY_Status(uint8_t *Status)
2540 | {
2541 | if (lsm6dso_xl_flag_data_ready_get(®_ctx, Status) != LSM6DSO_OK)
2542 | {
2543 | return LSM6DSO_ERROR;
2544 | }
2545 |
2546 | return LSM6DSO_OK;
2547 | }
2548 |
2549 | /**
2550 | * @brief Get the status of all hardware events
2551 | * @param pObj the device pObj
2552 | * @param Status the status of all hardware events
2553 | * @retval 0 in case of success, an error code otherwise
2554 | */
2555 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_X_Event_Status(LSM6DSO_Event_Status_t *Status)
2556 | {
2557 | uint8_t tilt_ia = 0U;
2558 | lsm6dso_wake_up_src_t wake_up_src;
2559 | lsm6dso_tap_src_t tap_src;
2560 | lsm6dso_d6d_src_t d6d_src;
2561 | lsm6dso_emb_func_src_t func_src;
2562 | lsm6dso_md1_cfg_t md1_cfg;
2563 | lsm6dso_md2_cfg_t md2_cfg;
2564 | lsm6dso_emb_func_int1_t int1_ctrl;
2565 | lsm6dso_emb_func_int2_t int2_ctrl;
2566 |
2567 | (void)memset((void *)Status, 0x0, sizeof(LSM6DSO_Event_Status_t));
2568 |
2569 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_WAKE_UP_SRC, (uint8_t *)&wake_up_src, 1) != LSM6DSO_OK)
2570 | {
2571 | return LSM6DSO_ERROR;
2572 | }
2573 |
2574 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_TAP_SRC, (uint8_t *)&tap_src, 1) != LSM6DSO_OK)
2575 | {
2576 | return LSM6DSO_ERROR;
2577 | }
2578 |
2579 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&d6d_src, 1) != LSM6DSO_OK)
2580 | {
2581 | return LSM6DSO_ERROR;
2582 | }
2583 |
2584 | if (lsm6dso_mem_bank_set(®_ctx, LSM6DSO_EMBEDDED_FUNC_BANK) != LSM6DSO_OK)
2585 | {
2586 | return LSM6DSO_ERROR;
2587 | }
2588 |
2589 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_EMB_FUNC_SRC, (uint8_t *)&func_src, 1) != LSM6DSO_OK)
2590 | {
2591 | return LSM6DSO_ERROR;
2592 | }
2593 |
2594 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_EMB_FUNC_INT1, (uint8_t *)&int1_ctrl, 1) != LSM6DSO_OK)
2595 | {
2596 | return LSM6DSO_ERROR;
2597 | }
2598 |
2599 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_EMB_FUNC_INT2, (uint8_t *)&int2_ctrl, 1) != LSM6DSO_OK)
2600 | {
2601 | return LSM6DSO_ERROR;
2602 | }
2603 |
2604 | if (lsm6dso_mem_bank_set(®_ctx, LSM6DSO_USER_BANK) != 0)
2605 | {
2606 | return LSM6DSO_ERROR;
2607 | }
2608 |
2609 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_MD1_CFG, (uint8_t *)&md1_cfg, 1) != LSM6DSO_OK)
2610 | {
2611 | return LSM6DSO_ERROR;
2612 | }
2613 |
2614 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_MD2_CFG, (uint8_t *)&md2_cfg, 1) != LSM6DSO_OK)
2615 | {
2616 | return LSM6DSO_ERROR;
2617 | }
2618 |
2619 | if (lsm6dso_tilt_flag_data_ready_get(®_ctx, &tilt_ia) != LSM6DSO_OK)
2620 | {
2621 | return LSM6DSO_ERROR;
2622 | }
2623 |
2624 | if ((md1_cfg.int1_ff == 1U) || (md2_cfg.int2_ff == 1U))
2625 | {
2626 | if (wake_up_src.ff_ia == 1U)
2627 | {
2628 | Status->FreeFallStatus = 1;
2629 | }
2630 | }
2631 |
2632 | if ((md1_cfg.int1_wu == 1U) || (md2_cfg.int2_wu == 1U))
2633 | {
2634 | if (wake_up_src.wu_ia == 1U)
2635 | {
2636 | Status->WakeUpStatus = 1;
2637 | }
2638 | }
2639 |
2640 | if ((md1_cfg.int1_single_tap == 1U) || (md2_cfg.int2_single_tap == 1U))
2641 | {
2642 | if (tap_src.single_tap == 1U)
2643 | {
2644 | Status->TapStatus = 1;
2645 | }
2646 | }
2647 |
2648 | if ((md1_cfg.int1_double_tap == 1U) || (md2_cfg.int2_double_tap == 1U))
2649 | {
2650 | if (tap_src.double_tap == 1U)
2651 | {
2652 | Status->DoubleTapStatus = 1;
2653 | }
2654 | }
2655 |
2656 | if ((md1_cfg.int1_6d == 1U) || (md2_cfg.int2_6d == 1U))
2657 | {
2658 | if (d6d_src.d6d_ia == 1U)
2659 | {
2660 | Status->D6DOrientationStatus = 1;
2661 | }
2662 | }
2663 |
2664 | if (int1_ctrl.int1_step_detector == 1U)
2665 | {
2666 | if (func_src.step_detected == 1U)
2667 | {
2668 | Status->StepStatus = 1;
2669 | }
2670 | }
2671 |
2672 | if ((int1_ctrl.int1_tilt == 1U) || (int2_ctrl.int2_tilt == 1U))
2673 | {
2674 | if (tilt_ia == 1U)
2675 | {
2676 | Status->TiltStatus = 1;
2677 | }
2678 | }
2679 |
2680 | return LSM6DSO_OK;
2681 | }
2682 |
2683 | /**
2684 | * @brief Set self test
2685 | * @param val the value of st_xl in reg CTRL5_C
2686 | * @retval 0 in case of success, an error code otherwise
2687 | */
2688 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_X_SelfTest(uint8_t val)
2689 | {
2690 | lsm6dso_st_xl_t reg;
2691 |
2692 | reg = (val == 0U) ? LSM6DSO_XL_ST_DISABLE
2693 | : (val == 1U) ? LSM6DSO_XL_ST_POSITIVE
2694 | : (val == 2U) ? LSM6DSO_XL_ST_NEGATIVE
2695 | : LSM6DSO_XL_ST_DISABLE;
2696 |
2697 | if (lsm6dso_xl_self_test_set(®_ctx, reg) != LSM6DSO_OK)
2698 | {
2699 | return LSM6DSO_ERROR;
2700 | }
2701 |
2702 | return LSM6DSO_OK;
2703 | }
2704 |
2705 | /**
2706 | * @brief Get the LSM6DSO GYRO data ready bit value
2707 | * @param Status the status of data ready bit
2708 | * @retval 0 in case of success, an error code otherwise
2709 | */
2710 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_G_DRDY_Status(uint8_t *Status)
2711 | {
2712 | if (lsm6dso_gy_flag_data_ready_get(®_ctx, Status) != LSM6DSO_OK)
2713 | {
2714 | return LSM6DSO_ERROR;
2715 | }
2716 |
2717 | return LSM6DSO_OK;
2718 | }
2719 |
2720 | /**
2721 | * @brief Set self test
2722 | * @param val the value of st_xl in reg CTRL5_C
2723 | * @retval 0 in case of success, an error code otherwise
2724 | */
2725 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_G_SelfTest(uint8_t val)
2726 | {
2727 | lsm6dso_st_g_t reg;
2728 |
2729 | reg = (val == 0U) ? LSM6DSO_GY_ST_DISABLE
2730 | : (val == 1U) ? LSM6DSO_GY_ST_POSITIVE
2731 | : (val == 2U) ? LSM6DSO_GY_ST_NEGATIVE
2732 | : LSM6DSO_GY_ST_DISABLE;
2733 |
2734 |
2735 | if (lsm6dso_gy_self_test_set(®_ctx, reg) != LSM6DSO_OK)
2736 | {
2737 | return LSM6DSO_ERROR;
2738 | }
2739 |
2740 | return LSM6DSO_OK;
2741 | }
2742 |
2743 | /**
2744 | * @brief Get the LSM6DSO FIFO number of samples
2745 | * @param NumSamples number of samples
2746 | * @retval 0 in case of success, an error code otherwise
2747 | */
2748 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_FIFO_Num_Samples(uint16_t *NumSamples)
2749 | {
2750 | if (lsm6dso_fifo_data_level_get(®_ctx, NumSamples) != LSM6DSO_OK)
2751 | {
2752 | return LSM6DSO_ERROR;
2753 | }
2754 |
2755 | return LSM6DSO_OK;
2756 | }
2757 |
2758 | /**
2759 | * @brief Get the LSM6DSO FIFO full status
2760 | * @param Status FIFO full status
2761 | * @retval 0 in case of success, an error code otherwise
2762 | */
2763 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_FIFO_Full_Status(uint8_t *Status)
2764 | {
2765 | lsm6dso_reg_t reg;
2766 |
2767 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_FIFO_STATUS2, ®.byte, 1) != LSM6DSO_OK)
2768 | {
2769 | return LSM6DSO_ERROR;
2770 | }
2771 |
2772 | *Status = reg.fifo_status2.fifo_full_ia;
2773 |
2774 | return LSM6DSO_OK;
2775 | }
2776 |
2777 | /**
2778 | * @brief Set the LSM6DSO FIFO full interrupt on INT1 pin
2779 | * @param Status FIFO full interrupt on INT1 pin status
2780 | * @retval 0 in case of success, an error code otherwise
2781 | */
2782 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_FIFO_INT1_FIFO_Full(uint8_t Status)
2783 | {
2784 | lsm6dso_reg_t reg;
2785 |
2786 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_INT1_CTRL, ®.byte, 1) != LSM6DSO_OK)
2787 | {
2788 | return LSM6DSO_ERROR;
2789 | }
2790 |
2791 | reg.int1_ctrl.int1_fifo_full = Status;
2792 |
2793 | if (lsm6dso_write_reg(®_ctx, LSM6DSO_INT1_CTRL, ®.byte, 1) != LSM6DSO_OK)
2794 | {
2795 | return LSM6DSO_ERROR;
2796 | }
2797 |
2798 | return LSM6DSO_OK;
2799 | }
2800 |
2801 | /**
2802 | * @brief Set the LSM6DSO FIFO watermark level
2803 | * @param Watermark FIFO watermark level
2804 | * @retval 0 in case of success, an error code otherwise
2805 | */
2806 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_FIFO_Watermark_Level(uint16_t Watermark)
2807 | {
2808 | if (lsm6dso_fifo_watermark_set(®_ctx, Watermark) != LSM6DSO_OK)
2809 | {
2810 | return LSM6DSO_ERROR;
2811 | }
2812 |
2813 | return LSM6DSO_OK;
2814 | }
2815 |
2816 | /**
2817 | * @brief Set the LSM6DSO FIFO stop on watermark
2818 | * @param Status FIFO stop on watermark status
2819 | * @retval 0 in case of success, an error code otherwise
2820 | */
2821 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_FIFO_Stop_On_Fth(uint8_t Status)
2822 | {
2823 | if (lsm6dso_fifo_stop_on_wtm_set(®_ctx, Status) != LSM6DSO_OK)
2824 | {
2825 | return LSM6DSO_ERROR;
2826 | }
2827 |
2828 | return LSM6DSO_OK;
2829 | }
2830 |
2831 | /**
2832 | * @brief Set the LSM6DSO FIFO mode
2833 | * @param Mode FIFO mode
2834 | * @retval 0 in case of success, an error code otherwise
2835 | */
2836 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_FIFO_Mode(uint8_t Mode)
2837 | {
2838 | LSM6DSOStatusTypeDef ret = LSM6DSO_OK;
2839 |
2840 | /* Verify that the passed parameter contains one of the valid values. */
2841 | switch ((lsm6dso_fifo_mode_t)Mode)
2842 | {
2843 | case LSM6DSO_BYPASS_MODE:
2844 | case LSM6DSO_FIFO_MODE:
2845 | case LSM6DSO_STREAM_TO_FIFO_MODE:
2846 | case LSM6DSO_BYPASS_TO_STREAM_MODE:
2847 | case LSM6DSO_STREAM_MODE:
2848 | break;
2849 |
2850 | default:
2851 | ret = LSM6DSO_ERROR;
2852 | break;
2853 | }
2854 |
2855 | if (ret == LSM6DSO_ERROR)
2856 | {
2857 | return ret;
2858 | }
2859 |
2860 | if (lsm6dso_fifo_mode_set(®_ctx, (lsm6dso_fifo_mode_t)Mode) != LSM6DSO_OK)
2861 | {
2862 | return LSM6DSO_ERROR;
2863 | }
2864 |
2865 | return ret;
2866 | }
2867 |
2868 | /**
2869 | * @brief Get the LSM6DSO FIFO tag
2870 | * @param Tag FIFO tag
2871 | * @retval 0 in case of success, an error code otherwise
2872 | */
2873 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_FIFO_Tag(uint8_t *Tag)
2874 | {
2875 | lsm6dso_fifo_tag_t tag_local;
2876 |
2877 | if (lsm6dso_fifo_sensor_tag_get(®_ctx, &tag_local) != LSM6DSO_OK)
2878 | {
2879 | return LSM6DSO_ERROR;
2880 | }
2881 |
2882 | *Tag = (uint8_t)tag_local;
2883 |
2884 | return LSM6DSO_OK;
2885 | }
2886 |
2887 | /**
2888 | * @brief Get the LSM6DSO FIFO raw data
2889 | * @param Data FIFO raw data array [6]
2890 | * @retval 0 in case of success, an error code otherwise
2891 | */
2892 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_FIFO_Data(uint8_t *Data)
2893 | {
2894 | if (lsm6dso_read_reg(®_ctx, LSM6DSO_FIFO_DATA_OUT_X_L, Data, 6) != LSM6DSO_OK)
2895 | {
2896 | return LSM6DSO_ERROR;
2897 | }
2898 |
2899 | return LSM6DSO_OK;
2900 | }
2901 |
2902 | /**
2903 | * @brief Get the LSM6DSO FIFO accelero single sample (16-bit data per 3 axes) and calculate acceleration [mg]
2904 | * @param Acceleration FIFO accelero axes [mg]
2905 | * @retval 0 in case of success, an error code otherwise
2906 | */
2907 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_FIFO_X_Axes(int32_t *Acceleration)
2908 | {
2909 | uint8_t data[6];
2910 | int16_t data_raw[3];
2911 | float sensitivity = 0.0f;
2912 | float acceleration_float[3];
2913 |
2914 | if (Get_FIFO_Data(data) != LSM6DSO_OK)
2915 | {
2916 | return LSM6DSO_ERROR;
2917 | }
2918 |
2919 | data_raw[0] = ((int16_t)data[1] << 8) | data[0];
2920 | data_raw[1] = ((int16_t)data[3] << 8) | data[2];
2921 | data_raw[2] = ((int16_t)data[5] << 8) | data[4];
2922 |
2923 | if (Get_X_Sensitivity(&sensitivity) != LSM6DSO_OK)
2924 | {
2925 | return LSM6DSO_ERROR;
2926 | }
2927 |
2928 | acceleration_float[0] = (float)data_raw[0] * sensitivity;
2929 | acceleration_float[1] = (float)data_raw[1] * sensitivity;
2930 | acceleration_float[2] = (float)data_raw[2] * sensitivity;
2931 |
2932 | Acceleration[0] = (int32_t)acceleration_float[0];
2933 | Acceleration[1] = (int32_t)acceleration_float[1];
2934 | Acceleration[2] = (int32_t)acceleration_float[2];
2935 |
2936 | return LSM6DSO_OK;
2937 | }
2938 |
2939 | /**
2940 | * @brief Set the LSM6DSO FIFO accelero BDR value
2941 | * @param Bdr FIFO accelero BDR value
2942 | * @retval 0 in case of success, an error code otherwise
2943 | */
2944 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_FIFO_X_BDR(float Bdr)
2945 | {
2946 | lsm6dso_bdr_xl_t new_bdr;
2947 |
2948 | new_bdr = (Bdr <= 0.0f) ? LSM6DSO_XL_NOT_BATCHED
2949 | : (Bdr <= 12.5f) ? LSM6DSO_XL_BATCHED_AT_12Hz5
2950 | : (Bdr <= 26.0f) ? LSM6DSO_XL_BATCHED_AT_26Hz
2951 | : (Bdr <= 52.0f) ? LSM6DSO_XL_BATCHED_AT_52Hz
2952 | : (Bdr <= 104.0f) ? LSM6DSO_XL_BATCHED_AT_104Hz
2953 | : (Bdr <= 208.0f) ? LSM6DSO_XL_BATCHED_AT_208Hz
2954 | : (Bdr <= 416.0f) ? LSM6DSO_XL_BATCHED_AT_417Hz
2955 | : (Bdr <= 833.0f) ? LSM6DSO_XL_BATCHED_AT_833Hz
2956 | : (Bdr <= 1660.0f) ? LSM6DSO_XL_BATCHED_AT_1667Hz
2957 | : (Bdr <= 3330.0f) ? LSM6DSO_XL_BATCHED_AT_3333Hz
2958 | : LSM6DSO_XL_BATCHED_AT_6667Hz;
2959 |
2960 | if (lsm6dso_fifo_xl_batch_set(®_ctx, new_bdr) != LSM6DSO_OK)
2961 | {
2962 | return LSM6DSO_ERROR;
2963 | }
2964 |
2965 | return LSM6DSO_OK;
2966 | }
2967 |
2968 | /**
2969 | * @brief Get the LSM6DSO FIFO gyro single sample (16-bit data per 3 axes) and calculate angular velocity [mDPS]
2970 | * @param AngularVelocity FIFO gyro axes [mDPS]
2971 | * @retval 0 in case of success, an error code otherwise
2972 | */
2973 | LSM6DSOStatusTypeDef LSM6DSOSensor::Get_FIFO_G_Axes(int32_t *AngularVelocity)
2974 | {
2975 | uint8_t data[6];
2976 | int16_t data_raw[3];
2977 | float sensitivity = 0.0f;
2978 | float angular_velocity_float[3];
2979 |
2980 | if (Get_FIFO_Data(data) != LSM6DSO_OK)
2981 | {
2982 | return LSM6DSO_ERROR;
2983 | }
2984 |
2985 | data_raw[0] = ((int16_t)data[1] << 8) | data[0];
2986 | data_raw[1] = ((int16_t)data[3] << 8) | data[2];
2987 | data_raw[2] = ((int16_t)data[5] << 8) | data[4];
2988 |
2989 | if (Get_G_Sensitivity(&sensitivity) != LSM6DSO_OK)
2990 | {
2991 | return LSM6DSO_ERROR;
2992 | }
2993 |
2994 | angular_velocity_float[0] = (float)data_raw[0] * sensitivity;
2995 | angular_velocity_float[1] = (float)data_raw[1] * sensitivity;
2996 | angular_velocity_float[2] = (float)data_raw[2] * sensitivity;
2997 |
2998 | AngularVelocity[0] = (int32_t)angular_velocity_float[0];
2999 | AngularVelocity[1] = (int32_t)angular_velocity_float[1];
3000 | AngularVelocity[2] = (int32_t)angular_velocity_float[2];
3001 |
3002 | return LSM6DSO_OK;
3003 | }
3004 |
3005 | /**
3006 | * @brief Set the LSM6DSO FIFO gyro BDR value
3007 | * @param Bdr FIFO gyro BDR value
3008 | * @retval 0 in case of success, an error code otherwise
3009 | */
3010 | LSM6DSOStatusTypeDef LSM6DSOSensor::Set_FIFO_G_BDR(float Bdr)
3011 | {
3012 | lsm6dso_bdr_gy_t new_bdr;
3013 |
3014 | new_bdr = (Bdr <= 0.0f) ? LSM6DSO_GY_NOT_BATCHED
3015 | : (Bdr <= 12.5f) ? LSM6DSO_GY_BATCHED_AT_12Hz5
3016 | : (Bdr <= 26.0f) ? LSM6DSO_GY_BATCHED_AT_26Hz
3017 | : (Bdr <= 52.0f) ? LSM6DSO_GY_BATCHED_AT_52Hz
3018 | : (Bdr <= 104.0f) ? LSM6DSO_GY_BATCHED_AT_104Hz
3019 | : (Bdr <= 208.0f) ? LSM6DSO_GY_BATCHED_AT_208Hz
3020 | : (Bdr <= 416.0f) ? LSM6DSO_GY_BATCHED_AT_417Hz
3021 | : (Bdr <= 833.0f) ? LSM6DSO_GY_BATCHED_AT_833Hz
3022 | : (Bdr <= 1660.0f) ? LSM6DSO_GY_BATCHED_AT_1667Hz
3023 | : (Bdr <= 3330.0f) ? LSM6DSO_GY_BATCHED_AT_3333Hz
3024 | : LSM6DSO_GY_BATCHED_AT_6667Hz;
3025 |
3026 | if (lsm6dso_fifo_gy_batch_set(®_ctx, new_bdr) != LSM6DSO_OK)
3027 | {
3028 | return LSM6DSO_ERROR;
3029 | }
3030 |
3031 | return LSM6DSO_OK;
3032 | }
3033 |
3034 |
3035 |
3036 | int32_t LSM6DSO_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
3037 | {
3038 | return ((LSM6DSOSensor *)handle)->IO_Write(pBuffer, WriteAddr, nBytesToWrite);
3039 | }
3040 |
3041 | int32_t LSM6DSO_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
3042 | {
3043 | return ((LSM6DSOSensor *)handle)->IO_Read(pBuffer, ReadAddr, nBytesToRead);
3044 | }
3045 |
--------------------------------------------------------------------------------
/src/LSM6DSOSensor.h:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file LSM6DSOSensor.h
4 | * @author SRA
5 | * @version V1.0.0
6 | * @date February 2019
7 | * @brief Abstract Class of an LSM6DSO Inertial Measurement Unit (IMU) 3 axes
8 | * sensor.
9 | ******************************************************************************
10 | * @attention
11 | *
12 | * © COPYRIGHT(c) 2019 STMicroelectronics
13 | *
14 | * Redistribution and use in source and binary forms, with or without modification,
15 | * are permitted provided that the following conditions are met:
16 | * 1. Redistributions of source code must retain the above copyright notice,
17 | * this list of conditions and the following disclaimer.
18 | * 2. Redistributions in binary form must reproduce the above copyright notice,
19 | * this list of conditions and the following disclaimer in the documentation
20 | * and/or other materials provided with the distribution.
21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
22 | * may be used to endorse or promote products derived from this software
23 | * without specific prior written permission.
24 | *
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 | *
36 | ******************************************************************************
37 | */
38 |
39 |
40 | /* Prevent recursive inclusion -----------------------------------------------*/
41 |
42 | #ifndef __LSM6DSOSensor_H__
43 | #define __LSM6DSOSensor_H__
44 |
45 |
46 | /* Includes ------------------------------------------------------------------*/
47 |
48 | #include "Wire.h"
49 | #include "SPI.h"
50 | #include "lsm6dso_reg.h"
51 |
52 | /* Defines -------------------------------------------------------------------*/
53 | /* For compatibility with ESP32 platforms */
54 | #ifdef ESP32
55 | #ifndef MSBFIRST
56 | #define MSBFIRST SPI_MSBFIRST
57 | #endif
58 | #endif
59 |
60 | #define LSM6DSO_ACC_SENSITIVITY_FS_2G 0.061f
61 | #define LSM6DSO_ACC_SENSITIVITY_FS_4G 0.122f
62 | #define LSM6DSO_ACC_SENSITIVITY_FS_8G 0.244f
63 | #define LSM6DSO_ACC_SENSITIVITY_FS_16G 0.488f
64 |
65 | #define LSM6DSO_GYRO_SENSITIVITY_FS_125DPS 4.375f
66 | #define LSM6DSO_GYRO_SENSITIVITY_FS_250DPS 8.750f
67 | #define LSM6DSO_GYRO_SENSITIVITY_FS_500DPS 17.500f
68 | #define LSM6DSO_GYRO_SENSITIVITY_FS_1000DPS 35.000f
69 | #define LSM6DSO_GYRO_SENSITIVITY_FS_2000DPS 70.000f
70 |
71 |
72 | /* Typedefs ------------------------------------------------------------------*/
73 |
74 | typedef enum
75 | {
76 | LSM6DSO_OK = 0,
77 | LSM6DSO_ERROR =-1
78 | } LSM6DSOStatusTypeDef;
79 |
80 | typedef enum
81 | {
82 | LSM6DSO_INT1_PIN,
83 | LSM6DSO_INT2_PIN,
84 | } LSM6DSO_SensorIntPin_t;
85 |
86 | typedef enum
87 | {
88 | LSM6DSO_ACC_HIGH_PERFORMANCE_MODE,
89 | LSM6DSO_ACC_LOW_POWER_NORMAL_MODE,
90 | LSM6DSO_ACC_ULTRA_LOW_POWER_MODE
91 | } LSM6DSO_ACC_Operating_Mode_t;
92 |
93 | typedef enum
94 | {
95 | LSM6DSO_GYRO_HIGH_PERFORMANCE_MODE,
96 | LSM6DSO_GYRO_LOW_POWER_NORMAL_MODE
97 | } LSM6DSO_GYRO_Operating_Mode_t;
98 |
99 | typedef struct
100 | {
101 | unsigned int FreeFallStatus : 1;
102 | unsigned int TapStatus : 1;
103 | unsigned int DoubleTapStatus : 1;
104 | unsigned int WakeUpStatus : 1;
105 | unsigned int StepStatus : 1;
106 | unsigned int TiltStatus : 1;
107 | unsigned int D6DOrientationStatus : 1;
108 | unsigned int SleepStatus : 1;
109 | } LSM6DSO_Event_Status_t;
110 |
111 |
112 | /* Class Declaration ---------------------------------------------------------*/
113 |
114 | /**
115 | * Abstract class of an LSM6DSO Inertial Measurement Unit (IMU) 3 axes
116 | * sensor.
117 | */
118 | class LSM6DSOSensor
119 | {
120 | public:
121 | LSM6DSOSensor(TwoWire *i2c, uint8_t address=LSM6DSO_I2C_ADD_H);
122 | LSM6DSOSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed=2000000);
123 | LSM6DSOStatusTypeDef begin();
124 | LSM6DSOStatusTypeDef end();
125 | LSM6DSOStatusTypeDef ReadID(uint8_t *Id);
126 | LSM6DSOStatusTypeDef Enable_X();
127 | LSM6DSOStatusTypeDef Disable_X();
128 | LSM6DSOStatusTypeDef Get_X_Sensitivity(float *Sensitivity);
129 | LSM6DSOStatusTypeDef Get_X_ODR(float *Odr);
130 | LSM6DSOStatusTypeDef Set_X_ODR(float Odr);
131 | LSM6DSOStatusTypeDef Set_X_ODR_With_Mode(float Odr, LSM6DSO_ACC_Operating_Mode_t Mode);
132 | LSM6DSOStatusTypeDef Get_X_FS(int32_t *FullScale);
133 | LSM6DSOStatusTypeDef Set_X_FS(int32_t FullScale);
134 | LSM6DSOStatusTypeDef Get_X_AxesRaw(int16_t *Value);
135 | LSM6DSOStatusTypeDef Get_X_Axes(int32_t *Acceleration);
136 |
137 | LSM6DSOStatusTypeDef Enable_G();
138 | LSM6DSOStatusTypeDef Disable_G();
139 | LSM6DSOStatusTypeDef Get_G_Sensitivity(float *Sensitivity);
140 | LSM6DSOStatusTypeDef Get_G_ODR(float *Odr);
141 | LSM6DSOStatusTypeDef Set_G_ODR(float Odr);
142 | LSM6DSOStatusTypeDef Set_G_ODR_With_Mode(float Odr, LSM6DSO_GYRO_Operating_Mode_t Mode);
143 | LSM6DSOStatusTypeDef Get_G_FS(int32_t *FullScale);
144 | LSM6DSOStatusTypeDef Set_G_FS(int32_t FullScale);
145 | LSM6DSOStatusTypeDef Get_G_AxesRaw(int16_t *Value);
146 | LSM6DSOStatusTypeDef Get_G_Axes(int32_t *AngularRate);
147 |
148 | LSM6DSOStatusTypeDef Read_Reg(uint8_t reg, uint8_t *Data);
149 | LSM6DSOStatusTypeDef Write_Reg(uint8_t reg, uint8_t Data);
150 | LSM6DSOStatusTypeDef Set_Interrupt_Latch(uint8_t Status);
151 | LSM6DSOStatusTypeDef Set_Interrupt_Polarity(uint8_t Status);
152 | LSM6DSOStatusTypeDef Set_Interrupt_PinMode(uint8_t Status);
153 |
154 | LSM6DSOStatusTypeDef Enable_Free_Fall_Detection(LSM6DSO_SensorIntPin_t IntPin);
155 | LSM6DSOStatusTypeDef Disable_Free_Fall_Detection();
156 | LSM6DSOStatusTypeDef Set_Free_Fall_Threshold(uint8_t Threshold);
157 | LSM6DSOStatusTypeDef Set_Free_Fall_Duration(uint8_t Duration);
158 |
159 | LSM6DSOStatusTypeDef Enable_Pedometer();
160 | LSM6DSOStatusTypeDef Disable_Pedometer();
161 | LSM6DSOStatusTypeDef Get_Step_Count(uint16_t *StepCount);
162 | LSM6DSOStatusTypeDef Step_Counter_Reset();
163 |
164 | LSM6DSOStatusTypeDef Enable_Tilt_Detection(LSM6DSO_SensorIntPin_t IntPin);
165 | LSM6DSOStatusTypeDef Disable_Tilt_Detection();
166 |
167 | LSM6DSOStatusTypeDef Enable_Wake_Up_Detection(LSM6DSO_SensorIntPin_t IntPin);
168 | LSM6DSOStatusTypeDef Disable_Wake_Up_Detection();
169 | LSM6DSOStatusTypeDef Set_Wake_Up_Threshold(uint8_t Threshold);
170 | LSM6DSOStatusTypeDef Set_Wake_Up_Duration(uint8_t Duration);
171 |
172 | LSM6DSOStatusTypeDef Enable_Single_Tap_Detection(LSM6DSO_SensorIntPin_t IntPin);
173 | LSM6DSOStatusTypeDef Disable_Single_Tap_Detection();
174 | LSM6DSOStatusTypeDef Enable_Double_Tap_Detection(LSM6DSO_SensorIntPin_t IntPin);
175 | LSM6DSOStatusTypeDef Disable_Double_Tap_Detection();
176 | LSM6DSOStatusTypeDef Set_Tap_Threshold(uint8_t Threshold);
177 | LSM6DSOStatusTypeDef Set_Tap_Shock_Time(uint8_t Time);
178 | LSM6DSOStatusTypeDef Set_Tap_Quiet_Time(uint8_t Time);
179 | LSM6DSOStatusTypeDef Set_Tap_Duration_Time(uint8_t Time);
180 |
181 | LSM6DSOStatusTypeDef Enable_6D_Orientation(LSM6DSO_SensorIntPin_t IntPin);
182 | LSM6DSOStatusTypeDef Disable_6D_Orientation();
183 | LSM6DSOStatusTypeDef Set_6D_Orientation_Threshold(uint8_t Threshold);
184 | LSM6DSOStatusTypeDef Get_6D_Orientation_XL(uint8_t *XLow);
185 | LSM6DSOStatusTypeDef Get_6D_Orientation_XH(uint8_t *XHigh);
186 | LSM6DSOStatusTypeDef Get_6D_Orientation_YL(uint8_t *YLow);
187 | LSM6DSOStatusTypeDef Get_6D_Orientation_YH(uint8_t *YHigh);
188 | LSM6DSOStatusTypeDef Get_6D_Orientation_ZL(uint8_t *ZLow);
189 | LSM6DSOStatusTypeDef Get_6D_Orientation_ZH(uint8_t *ZHigh);
190 |
191 | LSM6DSOStatusTypeDef Get_X_DRDY_Status(uint8_t *Status);
192 | LSM6DSOStatusTypeDef Get_X_Event_Status(LSM6DSO_Event_Status_t *Status);
193 | LSM6DSOStatusTypeDef Set_X_SelfTest(uint8_t Status);
194 |
195 | LSM6DSOStatusTypeDef Get_G_DRDY_Status(uint8_t *Status);
196 | LSM6DSOStatusTypeDef Set_G_SelfTest(uint8_t Status);
197 |
198 | LSM6DSOStatusTypeDef Get_FIFO_Num_Samples(uint16_t *NumSamples);
199 | LSM6DSOStatusTypeDef Get_FIFO_Full_Status(uint8_t *Status);
200 | LSM6DSOStatusTypeDef Set_FIFO_INT1_FIFO_Full(uint8_t Status);
201 | LSM6DSOStatusTypeDef Set_FIFO_Watermark_Level(uint16_t Watermark);
202 | LSM6DSOStatusTypeDef Set_FIFO_Stop_On_Fth(uint8_t Status);
203 | LSM6DSOStatusTypeDef Set_FIFO_Mode(uint8_t Mode);
204 | LSM6DSOStatusTypeDef Get_FIFO_Tag(uint8_t *Tag);
205 | LSM6DSOStatusTypeDef Get_FIFO_Data(uint8_t *Data);
206 | LSM6DSOStatusTypeDef Get_FIFO_X_Axes(int32_t *Acceleration);
207 | LSM6DSOStatusTypeDef Set_FIFO_X_BDR(float Bdr);
208 | LSM6DSOStatusTypeDef Get_FIFO_G_Axes(int32_t *AngularVelocity);
209 | LSM6DSOStatusTypeDef Set_FIFO_G_BDR(float Bdr);
210 |
211 | /**
212 | * @brief Utility function to read data.
213 | * @param pBuffer: pointer to data to be read.
214 | * @param RegisterAddr: specifies internal address register to be read.
215 | * @param NumByteToRead: number of bytes to be read.
216 | * @retval 0 if ok, an error code otherwise.
217 | */
218 | uint8_t IO_Read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
219 | {
220 | if (dev_spi) {
221 | dev_spi->beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3));
222 |
223 | digitalWrite(cs_pin, LOW);
224 |
225 | /* Write Reg Address */
226 | dev_spi->transfer(RegisterAddr | 0x80);
227 | /* Read the data */
228 | for (uint16_t i=0; itransfer(0x00);
230 | }
231 |
232 | digitalWrite(cs_pin, HIGH);
233 |
234 | dev_spi->endTransaction();
235 |
236 | return 0;
237 | }
238 |
239 | if (dev_i2c) {
240 | dev_i2c->beginTransmission(((uint8_t)(((address) >> 1) & 0x7F)));
241 | dev_i2c->write(RegisterAddr);
242 | dev_i2c->endTransmission(false);
243 |
244 | dev_i2c->requestFrom(((uint8_t)(((address) >> 1) & 0x7F)), (uint8_t) NumByteToRead);
245 |
246 | int i=0;
247 | while (dev_i2c->available()) {
248 | pBuffer[i] = dev_i2c->read();
249 | i++;
250 | }
251 |
252 | return 0;
253 | }
254 |
255 | return 1;
256 | }
257 |
258 | /**
259 | * @brief Utility function to write data.
260 | * @param pBuffer: pointer to data to be written.
261 | * @param RegisterAddr: specifies internal address register to be written.
262 | * @param NumByteToWrite: number of bytes to write.
263 | * @retval 0 if ok, an error code otherwise.
264 | */
265 | uint8_t IO_Write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
266 | {
267 | if (dev_spi) {
268 | dev_spi->beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3));
269 |
270 | digitalWrite(cs_pin, LOW);
271 |
272 | /* Write Reg Address */
273 | dev_spi->transfer(RegisterAddr);
274 | /* Write the data */
275 | for (uint16_t i=0; itransfer(pBuffer[i]);
277 | }
278 |
279 | digitalWrite(cs_pin, HIGH);
280 |
281 | dev_spi->endTransaction();
282 |
283 | return 0;
284 | }
285 |
286 | if (dev_i2c) {
287 | dev_i2c->beginTransmission(((uint8_t)(((address) >> 1) & 0x7F)));
288 |
289 | dev_i2c->write(RegisterAddr);
290 | for (uint16_t i = 0 ; i < NumByteToWrite ; i++) {
291 | dev_i2c->write(pBuffer[i]);
292 | }
293 |
294 | dev_i2c->endTransmission(true);
295 |
296 | return 0;
297 | }
298 |
299 | return 1;
300 | }
301 |
302 | private:
303 |
304 | LSM6DSOStatusTypeDef Set_X_ODR_When_Enabled(float Odr);
305 | LSM6DSOStatusTypeDef Set_X_ODR_When_Disabled(float Odr);
306 | LSM6DSOStatusTypeDef Set_G_ODR_When_Enabled(float Odr);
307 | LSM6DSOStatusTypeDef Set_G_ODR_When_Disabled(float Odr);
308 |
309 |
310 |
311 | /* Helper classes. */
312 | TwoWire *dev_i2c;
313 | SPIClass *dev_spi;
314 |
315 | /* Configuration */
316 | uint8_t address;
317 | int cs_pin;
318 | uint32_t spi_speed;
319 |
320 | lsm6dso_odr_xl_t acc_odr;
321 | lsm6dso_odr_g_t gyro_odr;
322 |
323 | uint8_t acc_is_enabled;
324 | uint8_t gyro_is_enabled;
325 |
326 |
327 | lsm6dso_ctx_t reg_ctx;
328 |
329 | };
330 |
331 | #ifdef __cplusplus
332 | extern "C" {
333 | #endif
334 | int32_t LSM6DSO_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
335 | int32_t LSM6DSO_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );
336 | #ifdef __cplusplus
337 | }
338 | #endif
339 |
340 | #endif
341 |
--------------------------------------------------------------------------------