LSM6DSO accelerometer and gyroscope sensor library

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_IKS01A3

Committer:
martlefebvre94
Date:
Tue Sep 17 08:40:49 2019 +0000
Revision:
4:bcf0cf6e43a7
Parent:
3:4274d9103f1d
Child:
5:213250c75a9e
Addition of functions for the configuration of the accelerometer and gyroscope sensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:6d69e896ce38 1 /**
cparata 0:6d69e896ce38 2 ******************************************************************************
cparata 0:6d69e896ce38 3 * @file LSM6DSOSensor.cpp
cparata 0:6d69e896ce38 4 * @author SRA
cparata 0:6d69e896ce38 5 * @version V1.0.0
cparata 0:6d69e896ce38 6 * @date February 2019
cparata 0:6d69e896ce38 7 * @brief Implementation of an LSM6DSO Inertial Measurement Unit (IMU) 6 axes
cparata 0:6d69e896ce38 8 * sensor.
cparata 0:6d69e896ce38 9 ******************************************************************************
cparata 0:6d69e896ce38 10 * @attention
cparata 0:6d69e896ce38 11 *
cparata 0:6d69e896ce38 12 * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
cparata 0:6d69e896ce38 13 *
cparata 0:6d69e896ce38 14 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:6d69e896ce38 15 * are permitted provided that the following conditions are met:
cparata 0:6d69e896ce38 16 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:6d69e896ce38 17 * this list of conditions and the following disclaimer.
cparata 0:6d69e896ce38 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:6d69e896ce38 19 * this list of conditions and the following disclaimer in the documentation
cparata 0:6d69e896ce38 20 * and/or other materials provided with the distribution.
cparata 0:6d69e896ce38 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:6d69e896ce38 22 * may be used to endorse or promote products derived from this software
cparata 0:6d69e896ce38 23 * without specific prior written permission.
cparata 0:6d69e896ce38 24 *
cparata 0:6d69e896ce38 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:6d69e896ce38 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:6d69e896ce38 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:6d69e896ce38 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:6d69e896ce38 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:6d69e896ce38 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:6d69e896ce38 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:6d69e896ce38 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:6d69e896ce38 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:6d69e896ce38 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:6d69e896ce38 35 *
cparata 0:6d69e896ce38 36 ******************************************************************************
cparata 0:6d69e896ce38 37 */
cparata 0:6d69e896ce38 38
cparata 0:6d69e896ce38 39
cparata 0:6d69e896ce38 40 /* Includes ------------------------------------------------------------------*/
cparata 0:6d69e896ce38 41
cparata 0:6d69e896ce38 42 #include "LSM6DSOSensor.h"
cparata 0:6d69e896ce38 43
cparata 0:6d69e896ce38 44
cparata 0:6d69e896ce38 45 /* Class Implementation ------------------------------------------------------*/
cparata 0:6d69e896ce38 46
cparata 0:6d69e896ce38 47 /** Constructor
cparata 0:6d69e896ce38 48 * @param spi object of an helper class which handles the SPI peripheral
cparata 0:6d69e896ce38 49 * @param cs_pin the chip select pin
cparata 0:6d69e896ce38 50 * @param int1_pin the interrupt 1 pin
cparata 0:6d69e896ce38 51 * @param int2_pin the interrupt 2 pin
cparata 0:6d69e896ce38 52 * @param spi_type the SPI type
cparata 0:6d69e896ce38 53 */
cparata 0:6d69e896ce38 54 LSM6DSOSensor::LSM6DSOSensor(SPI *spi, PinName cs_pin, PinName int1_pin, PinName int2_pin, SPI_type_t spi_type) : _dev_spi(spi), _cs_pin(cs_pin), _int1_irq(int1_pin), _int2_irq(int2_pin), _spi_type(spi_type)
cparata 0:6d69e896ce38 55 {
cparata 3:4274d9103f1d 56 assert(spi);
cparata 3:4274d9103f1d 57 if (cs_pin == NC) {
cparata 3:4274d9103f1d 58 printf("ERROR LSM6DSO CS MUST NOT BE NC\n\r");
cparata 3:4274d9103f1d 59 _dev_spi = NULL;
cparata 3:4274d9103f1d 60 _dev_i2c = NULL;
cparata 3:4274d9103f1d 61 return;
cparata 3:4274d9103f1d 62 }
cparata 3:4274d9103f1d 63
cparata 3:4274d9103f1d 64 _reg_ctx.write_reg = LSM6DSO_io_write;
cparata 3:4274d9103f1d 65 _reg_ctx.read_reg = LSM6DSO_io_read;
cparata 3:4274d9103f1d 66 _reg_ctx.handle = (void *)this;
cparata 3:4274d9103f1d 67 _cs_pin = 1;
cparata 0:6d69e896ce38 68 _dev_i2c = NULL;
cparata 3:4274d9103f1d 69 _address = 0;
cparata 3:4274d9103f1d 70
cparata 3:4274d9103f1d 71 if (_spi_type == SPI3W) {
cparata 3:4274d9103f1d 72 /* Enable SPI 3-Wires on the component */
cparata 3:4274d9103f1d 73 uint8_t data = 0x0C;
cparata 3:4274d9103f1d 74 lsm6dso_write_reg(&_reg_ctx, LSM6DSO_CTRL3_C, &data, 1);
cparata 3:4274d9103f1d 75 }
cparata 0:6d69e896ce38 76 }
cparata 0:6d69e896ce38 77
cparata 0:6d69e896ce38 78
cparata 0:6d69e896ce38 79 /** Constructor
cparata 0:6d69e896ce38 80 * @param i2c object of an helper class which handles the I2C peripheral
cparata 0:6d69e896ce38 81 * @param address the address of the component's instance
cparata 0:6d69e896ce38 82 * @param int1_pin the interrupt 1 pin
cparata 0:6d69e896ce38 83 * @param int2_pin the interrupt 2 pin
cparata 0:6d69e896ce38 84 */
cparata 0:6d69e896ce38 85 LSM6DSOSensor::LSM6DSOSensor(DevI2C *i2c, uint8_t address, PinName int1_pin, PinName int2_pin) : _dev_i2c(i2c), _address(address), _cs_pin(NC), _int1_irq(int1_pin), _int2_irq(int2_pin)
cparata 0:6d69e896ce38 86 {
cparata 3:4274d9103f1d 87 assert(i2c);
cparata 3:4274d9103f1d 88 _dev_spi = NULL;
cparata 3:4274d9103f1d 89 _reg_ctx.write_reg = LSM6DSO_io_write;
cparata 3:4274d9103f1d 90 _reg_ctx.read_reg = LSM6DSO_io_read;
cparata 3:4274d9103f1d 91 _reg_ctx.handle = (void *)this;
cparata 0:6d69e896ce38 92 }
cparata 0:6d69e896ce38 93
cparata 0:6d69e896ce38 94 /**
cparata 0:6d69e896ce38 95 * @brief Initializing the component
cparata 0:6d69e896ce38 96 * @param init pointer to device specific initalization structure
cparata 0:6d69e896ce38 97 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 98 */
cparata 0:6d69e896ce38 99 int LSM6DSOSensor::init(void *init)
cparata 0:6d69e896ce38 100 {
cparata 3:4274d9103f1d 101 /* Disable I3C */
cparata 3:4274d9103f1d 102 if (lsm6dso_i3c_disable_set(&_reg_ctx, LSM6DSO_I3C_DISABLE) != 0) {
cparata 3:4274d9103f1d 103 return 1;
cparata 3:4274d9103f1d 104 }
cparata 3:4274d9103f1d 105
cparata 3:4274d9103f1d 106 /* Enable register address automatically incremented during a multiple byte
cparata 3:4274d9103f1d 107 access with a serial interface. */
cparata 3:4274d9103f1d 108 if (lsm6dso_auto_increment_set(&_reg_ctx, PROPERTY_ENABLE) != 0) {
cparata 3:4274d9103f1d 109 return 1;
cparata 3:4274d9103f1d 110 }
cparata 3:4274d9103f1d 111
cparata 3:4274d9103f1d 112 /* Enable BDU */
cparata 3:4274d9103f1d 113 if (lsm6dso_block_data_update_set(&_reg_ctx, PROPERTY_ENABLE) != 0) {
cparata 3:4274d9103f1d 114 return 1;
cparata 3:4274d9103f1d 115 }
cparata 3:4274d9103f1d 116
cparata 3:4274d9103f1d 117 /* FIFO mode selection */
cparata 3:4274d9103f1d 118 if (lsm6dso_fifo_mode_set(&_reg_ctx, LSM6DSO_BYPASS_MODE) != 0) {
cparata 3:4274d9103f1d 119 return 1;
cparata 3:4274d9103f1d 120 }
cparata 3:4274d9103f1d 121
cparata 3:4274d9103f1d 122 /* Output data rate selection - power down. */
cparata 3:4274d9103f1d 123 if (lsm6dso_xl_data_rate_set(&_reg_ctx, LSM6DSO_XL_ODR_OFF) != 0) {
cparata 3:4274d9103f1d 124 return 1;
cparata 3:4274d9103f1d 125 }
cparata 3:4274d9103f1d 126
cparata 3:4274d9103f1d 127 /* Full scale selection. */
cparata 3:4274d9103f1d 128 if (lsm6dso_xl_full_scale_set(&_reg_ctx, LSM6DSO_2g) != 0) {
cparata 3:4274d9103f1d 129 return 1;
cparata 3:4274d9103f1d 130 }
cparata 3:4274d9103f1d 131
cparata 3:4274d9103f1d 132 /* Output data rate selection - power down. */
cparata 3:4274d9103f1d 133 if (lsm6dso_gy_data_rate_set(&_reg_ctx, LSM6DSO_GY_ODR_OFF) != 0) {
cparata 3:4274d9103f1d 134 return 1;
cparata 3:4274d9103f1d 135 }
cparata 3:4274d9103f1d 136
cparata 3:4274d9103f1d 137 /* Full scale selection. */
cparata 3:4274d9103f1d 138 if (lsm6dso_gy_full_scale_set(&_reg_ctx, LSM6DSO_2000dps) != 0) {
cparata 3:4274d9103f1d 139 return 1;
cparata 3:4274d9103f1d 140 }
cparata 3:4274d9103f1d 141
cparata 3:4274d9103f1d 142 /* Select default output data rate. */
cparata 3:4274d9103f1d 143 _x_last_odr = LSM6DSO_XL_ODR_104Hz;
cparata 3:4274d9103f1d 144
cparata 3:4274d9103f1d 145 /* Select default output data rate. */
cparata 3:4274d9103f1d 146 _g_last_odr = LSM6DSO_GY_ODR_104Hz;
cparata 3:4274d9103f1d 147
cparata 3:4274d9103f1d 148 _x_is_enabled = 0;
cparata 3:4274d9103f1d 149
cparata 3:4274d9103f1d 150 _g_is_enabled = 0;
cparata 3:4274d9103f1d 151
cparata 3:4274d9103f1d 152 return 0;
cparata 0:6d69e896ce38 153 }
cparata 0:6d69e896ce38 154
cparata 0:6d69e896ce38 155 /**
cparata 0:6d69e896ce38 156 * @brief Read component ID
cparata 0:6d69e896ce38 157 * @param id the WHO_AM_I value
cparata 0:6d69e896ce38 158 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 159 */
cparata 0:6d69e896ce38 160 int LSM6DSOSensor::read_id(uint8_t *id)
cparata 0:6d69e896ce38 161 {
cparata 3:4274d9103f1d 162 if (lsm6dso_device_id_get(&_reg_ctx, id) != 0) {
cparata 3:4274d9103f1d 163 return 1;
cparata 3:4274d9103f1d 164 }
cparata 3:4274d9103f1d 165
cparata 3:4274d9103f1d 166 return 0;
cparata 0:6d69e896ce38 167 }
cparata 0:6d69e896ce38 168
cparata 0:6d69e896ce38 169 /**
cparata 0:6d69e896ce38 170 * @brief Enable the LSM6DSO accelerometer sensor
cparata 0:6d69e896ce38 171 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 172 */
cparata 0:6d69e896ce38 173 int LSM6DSOSensor::enable_x()
cparata 0:6d69e896ce38 174 {
cparata 3:4274d9103f1d 175 /* Check if the component is already enabled */
cparata 3:4274d9103f1d 176 if (_x_is_enabled == 1U) {
cparata 3:4274d9103f1d 177 return 0;
cparata 3:4274d9103f1d 178 }
cparata 3:4274d9103f1d 179
cparata 3:4274d9103f1d 180 /* Output data rate selection. */
cparata 3:4274d9103f1d 181 if (lsm6dso_xl_data_rate_set(&_reg_ctx, _x_last_odr) != 0) {
cparata 3:4274d9103f1d 182 return 1;
cparata 3:4274d9103f1d 183 }
cparata 3:4274d9103f1d 184
cparata 3:4274d9103f1d 185 _x_is_enabled = 1;
cparata 3:4274d9103f1d 186
cparata 0:6d69e896ce38 187 return 0;
cparata 0:6d69e896ce38 188 }
cparata 0:6d69e896ce38 189
cparata 0:6d69e896ce38 190 /**
cparata 0:6d69e896ce38 191 * @brief Disable the LSM6DSO accelerometer sensor
cparata 0:6d69e896ce38 192 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 193 */
cparata 0:6d69e896ce38 194 int LSM6DSOSensor::disable_x()
cparata 0:6d69e896ce38 195 {
cparata 3:4274d9103f1d 196 /* Check if the component is already disabled */
cparata 3:4274d9103f1d 197 if (_x_is_enabled == 0U) {
cparata 3:4274d9103f1d 198 return 0;
cparata 3:4274d9103f1d 199 }
cparata 3:4274d9103f1d 200
cparata 3:4274d9103f1d 201 /* Get current output data rate. */
cparata 3:4274d9103f1d 202 if (lsm6dso_xl_data_rate_get(&_reg_ctx, &_x_last_odr) != 0) {
cparata 3:4274d9103f1d 203 return 1;
cparata 3:4274d9103f1d 204 }
cparata 3:4274d9103f1d 205
cparata 3:4274d9103f1d 206 /* Output data rate selection - power down. */
cparata 3:4274d9103f1d 207 if (lsm6dso_xl_data_rate_set(&_reg_ctx, LSM6DSO_XL_ODR_OFF) != 0) {
cparata 3:4274d9103f1d 208 return 1;
cparata 3:4274d9103f1d 209 }
cparata 3:4274d9103f1d 210
cparata 3:4274d9103f1d 211 _x_is_enabled = 0;
cparata 3:4274d9103f1d 212
cparata 0:6d69e896ce38 213 return 0;
cparata 0:6d69e896ce38 214 }
cparata 0:6d69e896ce38 215
cparata 0:6d69e896ce38 216 /**
cparata 0:6d69e896ce38 217 * @brief Get the LSM6DSO accelerometer sensor sensitivity
cparata 0:6d69e896ce38 218 * @param sensitivity pointer where the sensitivity is written
cparata 0:6d69e896ce38 219 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 220 */
cparata 0:6d69e896ce38 221 int LSM6DSOSensor::get_x_sensitivity(float *sensitivity)
cparata 0:6d69e896ce38 222 {
cparata 3:4274d9103f1d 223 int ret = 0;
cparata 3:4274d9103f1d 224 lsm6dso_fs_xl_t full_scale;
cparata 3:4274d9103f1d 225
cparata 3:4274d9103f1d 226 /* Read actual full scale selection from sensor. */
cparata 3:4274d9103f1d 227 if (lsm6dso_xl_full_scale_get(&_reg_ctx, &full_scale) != 0) {
cparata 3:4274d9103f1d 228 return 1;
cparata 3:4274d9103f1d 229 }
cparata 3:4274d9103f1d 230
cparata 3:4274d9103f1d 231 /* Store the sensitivity based on actual full scale. */
cparata 3:4274d9103f1d 232 switch (full_scale) {
cparata 3:4274d9103f1d 233 case LSM6DSO_2g:
cparata 3:4274d9103f1d 234 *sensitivity = LSM6DSO_ACC_SENSITIVITY_FS_2G;
cparata 3:4274d9103f1d 235 break;
cparata 3:4274d9103f1d 236
cparata 3:4274d9103f1d 237 case LSM6DSO_4g:
cparata 3:4274d9103f1d 238 *sensitivity = LSM6DSO_ACC_SENSITIVITY_FS_4G;
cparata 3:4274d9103f1d 239 break;
cparata 3:4274d9103f1d 240
cparata 3:4274d9103f1d 241 case LSM6DSO_8g:
cparata 3:4274d9103f1d 242 *sensitivity = LSM6DSO_ACC_SENSITIVITY_FS_8G;
cparata 3:4274d9103f1d 243 break;
cparata 3:4274d9103f1d 244
cparata 3:4274d9103f1d 245 case LSM6DSO_16g:
cparata 3:4274d9103f1d 246 *sensitivity = LSM6DSO_ACC_SENSITIVITY_FS_16G;
cparata 3:4274d9103f1d 247 break;
cparata 3:4274d9103f1d 248
cparata 3:4274d9103f1d 249 default:
cparata 3:4274d9103f1d 250 ret = 1;
cparata 3:4274d9103f1d 251 break;
cparata 3:4274d9103f1d 252 }
cparata 3:4274d9103f1d 253
cparata 3:4274d9103f1d 254 return ret;
cparata 0:6d69e896ce38 255 }
cparata 0:6d69e896ce38 256
cparata 0:6d69e896ce38 257 /**
cparata 0:6d69e896ce38 258 * @brief Get the LSM6DSO accelerometer sensor output data rate
cparata 0:6d69e896ce38 259 * @param odr pointer where the output data rate is written
cparata 0:6d69e896ce38 260 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 261 */
cparata 0:6d69e896ce38 262 int LSM6DSOSensor::get_x_odr(float *odr)
cparata 0:6d69e896ce38 263 {
cparata 3:4274d9103f1d 264 int ret = 0;
cparata 3:4274d9103f1d 265 lsm6dso_odr_xl_t odr_low_level;
cparata 3:4274d9103f1d 266
cparata 3:4274d9103f1d 267 /* Get current output data rate. */
cparata 3:4274d9103f1d 268 if (lsm6dso_xl_data_rate_get(&_reg_ctx, &odr_low_level) != 0) {
cparata 3:4274d9103f1d 269 return 1;
cparata 3:4274d9103f1d 270 }
cparata 3:4274d9103f1d 271
cparata 3:4274d9103f1d 272 switch (odr_low_level) {
cparata 3:4274d9103f1d 273 case LSM6DSO_XL_ODR_OFF:
cparata 3:4274d9103f1d 274 *odr = 0.0f;
cparata 3:4274d9103f1d 275 break;
cparata 3:4274d9103f1d 276
cparata 3:4274d9103f1d 277 case LSM6DSO_XL_ODR_6Hz5:
cparata 3:4274d9103f1d 278 *odr = 6.5f;
cparata 3:4274d9103f1d 279 break;
cparata 3:4274d9103f1d 280
cparata 3:4274d9103f1d 281 case LSM6DSO_XL_ODR_12Hz5:
cparata 3:4274d9103f1d 282 *odr = 12.5f;
cparata 3:4274d9103f1d 283 break;
cparata 3:4274d9103f1d 284
cparata 3:4274d9103f1d 285 case LSM6DSO_XL_ODR_26Hz:
cparata 3:4274d9103f1d 286 *odr = 26.0f;
cparata 3:4274d9103f1d 287 break;
cparata 3:4274d9103f1d 288
cparata 3:4274d9103f1d 289 case LSM6DSO_XL_ODR_52Hz:
cparata 3:4274d9103f1d 290 *odr = 52.0f;
cparata 3:4274d9103f1d 291 break;
cparata 3:4274d9103f1d 292
cparata 3:4274d9103f1d 293 case LSM6DSO_XL_ODR_104Hz:
cparata 3:4274d9103f1d 294 *odr = 104.0f;
cparata 3:4274d9103f1d 295 break;
cparata 3:4274d9103f1d 296
cparata 3:4274d9103f1d 297 case LSM6DSO_XL_ODR_208Hz:
cparata 3:4274d9103f1d 298 *odr = 208.0f;
cparata 3:4274d9103f1d 299 break;
cparata 3:4274d9103f1d 300
cparata 3:4274d9103f1d 301 case LSM6DSO_XL_ODR_417Hz:
cparata 3:4274d9103f1d 302 *odr = 417.0f;
cparata 3:4274d9103f1d 303 break;
cparata 3:4274d9103f1d 304
cparata 3:4274d9103f1d 305 case LSM6DSO_XL_ODR_833Hz:
cparata 3:4274d9103f1d 306 *odr = 833.0f;
cparata 3:4274d9103f1d 307 break;
cparata 3:4274d9103f1d 308
cparata 3:4274d9103f1d 309 case LSM6DSO_XL_ODR_1667Hz:
cparata 3:4274d9103f1d 310 *odr = 1667.0f;
cparata 3:4274d9103f1d 311 break;
cparata 3:4274d9103f1d 312
cparata 3:4274d9103f1d 313 case LSM6DSO_XL_ODR_3333Hz:
cparata 3:4274d9103f1d 314 *odr = 3333.0f;
cparata 3:4274d9103f1d 315 break;
cparata 3:4274d9103f1d 316
cparata 3:4274d9103f1d 317 case LSM6DSO_XL_ODR_6667Hz:
cparata 3:4274d9103f1d 318 *odr = 6667.0f;
cparata 3:4274d9103f1d 319 break;
cparata 3:4274d9103f1d 320
cparata 3:4274d9103f1d 321 default:
cparata 3:4274d9103f1d 322 ret = 1;
cparata 3:4274d9103f1d 323 break;
cparata 3:4274d9103f1d 324 }
cparata 3:4274d9103f1d 325
cparata 3:4274d9103f1d 326 return ret;
cparata 0:6d69e896ce38 327 }
cparata 0:6d69e896ce38 328
cparata 0:6d69e896ce38 329 /**
cparata 0:6d69e896ce38 330 * @brief Set the LSM6DSO accelerometer sensor output data rate
cparata 0:6d69e896ce38 331 * @param odr the output data rate value to be set
cparata 0:6d69e896ce38 332 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 333 */
cparata 0:6d69e896ce38 334 int LSM6DSOSensor::set_x_odr(float odr)
cparata 0:6d69e896ce38 335 {
cparata 3:4274d9103f1d 336 /* Check if the component is enabled */
cparata 3:4274d9103f1d 337 if (_x_is_enabled == 1U) {
cparata 3:4274d9103f1d 338 return set_x_odr_when_enabled(odr);
cparata 3:4274d9103f1d 339 } else {
cparata 3:4274d9103f1d 340 return set_x_odr_when_disabled(odr);
cparata 3:4274d9103f1d 341 }
cparata 0:6d69e896ce38 342 }
cparata 0:6d69e896ce38 343
cparata 0:6d69e896ce38 344 /**
cparata 0:6d69e896ce38 345 * @brief Set the LSM6DSO accelerometer sensor output data rate when enabled
cparata 0:6d69e896ce38 346 * @param odr the functional output data rate to be set
cparata 0:6d69e896ce38 347 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 348 */
cparata 0:6d69e896ce38 349 int LSM6DSOSensor::set_x_odr_when_enabled(float odr)
cparata 0:6d69e896ce38 350 {
cparata 3:4274d9103f1d 351 lsm6dso_odr_xl_t new_odr;
cparata 3:4274d9103f1d 352
cparata 3:4274d9103f1d 353 new_odr = (odr <= 12.5f) ? LSM6DSO_XL_ODR_12Hz5
cparata 3:4274d9103f1d 354 : (odr <= 26.0f) ? LSM6DSO_XL_ODR_26Hz
cparata 3:4274d9103f1d 355 : (odr <= 52.0f) ? LSM6DSO_XL_ODR_52Hz
cparata 3:4274d9103f1d 356 : (odr <= 104.0f) ? LSM6DSO_XL_ODR_104Hz
cparata 3:4274d9103f1d 357 : (odr <= 208.0f) ? LSM6DSO_XL_ODR_208Hz
cparata 3:4274d9103f1d 358 : (odr <= 417.0f) ? LSM6DSO_XL_ODR_417Hz
cparata 3:4274d9103f1d 359 : (odr <= 833.0f) ? LSM6DSO_XL_ODR_833Hz
cparata 3:4274d9103f1d 360 : (odr <= 1667.0f) ? LSM6DSO_XL_ODR_1667Hz
cparata 3:4274d9103f1d 361 : (odr <= 3333.0f) ? LSM6DSO_XL_ODR_3333Hz
cparata 3:4274d9103f1d 362 : LSM6DSO_XL_ODR_6667Hz;
cparata 3:4274d9103f1d 363
cparata 3:4274d9103f1d 364 /* Output data rate selection. */
cparata 3:4274d9103f1d 365 if (lsm6dso_xl_data_rate_set(&_reg_ctx, new_odr) != 0) {
cparata 3:4274d9103f1d 366 return 1;
cparata 3:4274d9103f1d 367 }
cparata 3:4274d9103f1d 368
cparata 3:4274d9103f1d 369 return 0;
cparata 0:6d69e896ce38 370 }
cparata 0:6d69e896ce38 371
cparata 0:6d69e896ce38 372 /**
cparata 0:6d69e896ce38 373 * @brief Set the LSM6DSO accelerometer sensor output data rate when disabled
cparata 0:6d69e896ce38 374 * @param odr the functional output data rate to be set
cparata 0:6d69e896ce38 375 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 376 */
cparata 0:6d69e896ce38 377 int LSM6DSOSensor::set_x_odr_when_disabled(float odr)
cparata 0:6d69e896ce38 378 {
cparata 3:4274d9103f1d 379 _x_last_odr = (odr <= 12.5f) ? LSM6DSO_XL_ODR_12Hz5
cparata 3:4274d9103f1d 380 : (odr <= 26.0f) ? LSM6DSO_XL_ODR_26Hz
cparata 3:4274d9103f1d 381 : (odr <= 52.0f) ? LSM6DSO_XL_ODR_52Hz
cparata 3:4274d9103f1d 382 : (odr <= 104.0f) ? LSM6DSO_XL_ODR_104Hz
cparata 3:4274d9103f1d 383 : (odr <= 208.0f) ? LSM6DSO_XL_ODR_208Hz
cparata 3:4274d9103f1d 384 : (odr <= 417.0f) ? LSM6DSO_XL_ODR_417Hz
cparata 3:4274d9103f1d 385 : (odr <= 833.0f) ? LSM6DSO_XL_ODR_833Hz
cparata 3:4274d9103f1d 386 : (odr <= 1667.0f) ? LSM6DSO_XL_ODR_1667Hz
cparata 3:4274d9103f1d 387 : (odr <= 3333.0f) ? LSM6DSO_XL_ODR_3333Hz
cparata 3:4274d9103f1d 388 : LSM6DSO_XL_ODR_6667Hz;
cparata 3:4274d9103f1d 389
cparata 3:4274d9103f1d 390 return 0;
cparata 0:6d69e896ce38 391 }
cparata 0:6d69e896ce38 392
cparata 0:6d69e896ce38 393 /**
cparata 0:6d69e896ce38 394 * @brief Get the LSM6DSO accelerometer sensor full scale
cparata 0:6d69e896ce38 395 * @param full_scale pointer where the full scale is written
cparata 0:6d69e896ce38 396 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 397 */
cparata 0:6d69e896ce38 398 int LSM6DSOSensor::get_x_fs(float *full_scale)
cparata 0:6d69e896ce38 399 {
cparata 3:4274d9103f1d 400 int ret = 0;
cparata 3:4274d9103f1d 401 lsm6dso_fs_xl_t fs_low_level;
cparata 3:4274d9103f1d 402
cparata 3:4274d9103f1d 403 /* Read actual full scale selection from sensor. */
cparata 3:4274d9103f1d 404 if (lsm6dso_xl_full_scale_get(&_reg_ctx, &fs_low_level) != 0) {
cparata 3:4274d9103f1d 405 return 1;
cparata 3:4274d9103f1d 406 }
cparata 3:4274d9103f1d 407
cparata 3:4274d9103f1d 408 switch (fs_low_level) {
cparata 3:4274d9103f1d 409 case LSM6DSO_2g:
cparata 3:4274d9103f1d 410 *full_scale = 2.0f;
cparata 3:4274d9103f1d 411 break;
cparata 3:4274d9103f1d 412
cparata 3:4274d9103f1d 413 case LSM6DSO_4g:
cparata 3:4274d9103f1d 414 *full_scale = 4.0f;
cparata 3:4274d9103f1d 415 break;
cparata 3:4274d9103f1d 416
cparata 3:4274d9103f1d 417 case LSM6DSO_8g:
cparata 3:4274d9103f1d 418 *full_scale = 8.0f;
cparata 3:4274d9103f1d 419 break;
cparata 3:4274d9103f1d 420
cparata 3:4274d9103f1d 421 case LSM6DSO_16g:
cparata 3:4274d9103f1d 422 *full_scale = 16.0f;
cparata 3:4274d9103f1d 423 break;
cparata 3:4274d9103f1d 424
cparata 3:4274d9103f1d 425 default:
cparata 3:4274d9103f1d 426 ret = 1;
cparata 3:4274d9103f1d 427 break;
cparata 3:4274d9103f1d 428 }
cparata 3:4274d9103f1d 429
cparata 3:4274d9103f1d 430 return ret;
cparata 0:6d69e896ce38 431 }
cparata 0:6d69e896ce38 432
cparata 0:6d69e896ce38 433 /**
cparata 0:6d69e896ce38 434 * @brief Set the LSM6DSO accelerometer sensor full scale
cparata 0:6d69e896ce38 435 * @param full_scale the functional full scale to be set
cparata 0:6d69e896ce38 436 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 437 */
cparata 0:6d69e896ce38 438 int LSM6DSOSensor::set_x_fs(float full_scale)
cparata 0:6d69e896ce38 439 {
cparata 3:4274d9103f1d 440 lsm6dso_fs_xl_t new_fs;
cparata 3:4274d9103f1d 441
cparata 3:4274d9103f1d 442 /* Seems like MISRA C-2012 rule 14.3a violation but only from single file statical analysis point of view because
cparata 3:4274d9103f1d 443 the parameter passed to the function is not known at the moment of analysis */
cparata 3:4274d9103f1d 444 new_fs = (full_scale <= 2.0f) ? LSM6DSO_2g
cparata 3:4274d9103f1d 445 : (full_scale <= 4.0f) ? LSM6DSO_4g
cparata 3:4274d9103f1d 446 : (full_scale <= 8.0f) ? LSM6DSO_8g
cparata 3:4274d9103f1d 447 : LSM6DSO_16g;
cparata 3:4274d9103f1d 448
cparata 3:4274d9103f1d 449 if (lsm6dso_xl_full_scale_set(&_reg_ctx, new_fs) != 0) {
cparata 3:4274d9103f1d 450 return 1;
cparata 3:4274d9103f1d 451 }
cparata 3:4274d9103f1d 452
cparata 3:4274d9103f1d 453 return 0;
cparata 0:6d69e896ce38 454 }
cparata 0:6d69e896ce38 455
cparata 0:6d69e896ce38 456 /**
martlefebvre94 4:bcf0cf6e43a7 457 * @brief Get the LSM6DSO accelerometer sensor power mode
martlefebvre94 4:bcf0cf6e43a7 458 * @param xl_hm_mode the pointer where the high-performance mode is written, xl_ulp_en the pointer where the ultra-low-power enable is written
martlefebvre94 4:bcf0cf6e43a7 459 * @retval 0 in case of success, an error code otherwise
martlefebvre94 4:bcf0cf6e43a7 460 */
martlefebvre94 4:bcf0cf6e43a7 461 int LSM6DSOSensor::get_x_power_mode(uint8_t *xl_hm_mode, uint8_t *xl_ulp_en)
martlefebvre94 4:bcf0cf6e43a7 462 {
martlefebvre94 4:bcf0cf6e43a7 463 int ret = 0;
martlefebvre94 4:bcf0cf6e43a7 464 lsm6dso_xl_hm_mode_t power_mode_low_level;
martlefebvre94 4:bcf0cf6e43a7 465
martlefebvre94 4:bcf0cf6e43a7 466 /* Read actual power mode from sensor. */
martlefebvre94 4:bcf0cf6e43a7 467 if (lsm6dso_xl_power_mode_get(&_reg_ctx, &power_mode_low_level) != 0) {
martlefebvre94 4:bcf0cf6e43a7 468 return 1;
martlefebvre94 4:bcf0cf6e43a7 469 }
martlefebvre94 4:bcf0cf6e43a7 470
martlefebvre94 4:bcf0cf6e43a7 471 switch (power_mode_low_level) {
martlefebvre94 4:bcf0cf6e43a7 472 case LSM6DSO_HIGH_PERFORMANCE_MD:
martlefebvre94 4:bcf0cf6e43a7 473 *xl_hm_mode = 0;
martlefebvre94 4:bcf0cf6e43a7 474 *xl_ulp_en = 0;
martlefebvre94 4:bcf0cf6e43a7 475 break;
martlefebvre94 4:bcf0cf6e43a7 476
martlefebvre94 4:bcf0cf6e43a7 477 case LSM6DSO_LOW_NORMAL_POWER_MD:
martlefebvre94 4:bcf0cf6e43a7 478 *xl_hm_mode = 1;
martlefebvre94 4:bcf0cf6e43a7 479 *xl_ulp_en = 0;
martlefebvre94 4:bcf0cf6e43a7 480 break;
martlefebvre94 4:bcf0cf6e43a7 481
martlefebvre94 4:bcf0cf6e43a7 482 case LSM6DSO_ULTRA_LOW_POWER_MD:
martlefebvre94 4:bcf0cf6e43a7 483 *xl_hm_mode = 0;
martlefebvre94 4:bcf0cf6e43a7 484 *xl_ulp_en = 1;
martlefebvre94 4:bcf0cf6e43a7 485 break;
martlefebvre94 4:bcf0cf6e43a7 486
martlefebvre94 4:bcf0cf6e43a7 487 default:
martlefebvre94 4:bcf0cf6e43a7 488 ret = 1;
martlefebvre94 4:bcf0cf6e43a7 489 break;
martlefebvre94 4:bcf0cf6e43a7 490 }
martlefebvre94 4:bcf0cf6e43a7 491
martlefebvre94 4:bcf0cf6e43a7 492 return ret;
martlefebvre94 4:bcf0cf6e43a7 493 }
martlefebvre94 4:bcf0cf6e43a7 494
martlefebvre94 4:bcf0cf6e43a7 495 /**
martlefebvre94 4:bcf0cf6e43a7 496 * @brief Set the LSM6DSO accelerometer sensor power mode
martlefebvre94 4:bcf0cf6e43a7 497 * @param xl_hm_mode the high-performance mode to be set, xl_ulp_en the ultra-low-power enable to be set
martlefebvre94 4:bcf0cf6e43a7 498 * @retval 0 in case of success, an error code otherwise
martlefebvre94 4:bcf0cf6e43a7 499 */
martlefebvre94 4:bcf0cf6e43a7 500 int LSM6DSOSensor::set_x_power_mode(uint8_t xl_hm_mode, uint8_t xl_ulp_en)
martlefebvre94 4:bcf0cf6e43a7 501 {
martlefebvre94 4:bcf0cf6e43a7 502 uint8_t power_mode = xl_hm_mode + (xl_ulp_en << 1);
martlefebvre94 4:bcf0cf6e43a7 503 lsm6dso_xl_hm_mode_t new_power_mode;
martlefebvre94 4:bcf0cf6e43a7 504
martlefebvre94 4:bcf0cf6e43a7 505 new_power_mode = (power_mode == 0) ? LSM6DSO_HIGH_PERFORMANCE_MD
martlefebvre94 4:bcf0cf6e43a7 506 : (power_mode == 1) ? LSM6DSO_LOW_NORMAL_POWER_MD
martlefebvre94 4:bcf0cf6e43a7 507 : LSM6DSO_ULTRA_LOW_POWER_MD;
martlefebvre94 4:bcf0cf6e43a7 508
martlefebvre94 4:bcf0cf6e43a7 509 if (lsm6dso_xl_power_mode_set(&_reg_ctx, new_power_mode) != 0) {
martlefebvre94 4:bcf0cf6e43a7 510 return 1;
martlefebvre94 4:bcf0cf6e43a7 511 }
martlefebvre94 4:bcf0cf6e43a7 512
martlefebvre94 4:bcf0cf6e43a7 513 return 0;
martlefebvre94 4:bcf0cf6e43a7 514 }
martlefebvre94 4:bcf0cf6e43a7 515
martlefebvre94 4:bcf0cf6e43a7 516 /**
cparata 0:6d69e896ce38 517 * @brief Get the LSM6DSO accelerometer sensor raw axes
cparata 0:6d69e896ce38 518 * @param value pointer where the raw values of the axes are written
cparata 0:6d69e896ce38 519 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 520 */
cparata 0:6d69e896ce38 521 int LSM6DSOSensor::get_x_axes_raw(int16_t *value)
cparata 0:6d69e896ce38 522 {
cparata 3:4274d9103f1d 523 axis3bit16_t data_raw;
cparata 3:4274d9103f1d 524
cparata 3:4274d9103f1d 525 /* Read raw data values. */
cparata 3:4274d9103f1d 526 if (lsm6dso_acceleration_raw_get(&_reg_ctx, data_raw.u8bit) != 0) {
cparata 3:4274d9103f1d 527 return 1;
cparata 3:4274d9103f1d 528 }
cparata 3:4274d9103f1d 529
cparata 3:4274d9103f1d 530 /* Format the data. */
cparata 3:4274d9103f1d 531 value[0] = data_raw.i16bit[0];
cparata 3:4274d9103f1d 532 value[1] = data_raw.i16bit[1];
cparata 3:4274d9103f1d 533 value[2] = data_raw.i16bit[2];
cparata 3:4274d9103f1d 534
cparata 3:4274d9103f1d 535 return 0;
cparata 0:6d69e896ce38 536 }
cparata 0:6d69e896ce38 537
cparata 0:6d69e896ce38 538
cparata 0:6d69e896ce38 539 /**
cparata 0:6d69e896ce38 540 * @brief Get the LSM6DSO accelerometer sensor axes
cparata 0:6d69e896ce38 541 * @param acceleration pointer where the values of the axes are written
cparata 0:6d69e896ce38 542 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 543 */
cparata 0:6d69e896ce38 544 int LSM6DSOSensor::get_x_axes(int32_t *acceleration)
cparata 0:6d69e896ce38 545 {
cparata 3:4274d9103f1d 546 axis3bit16_t data_raw;
cparata 3:4274d9103f1d 547 float sensitivity = 0.0f;
cparata 3:4274d9103f1d 548
cparata 3:4274d9103f1d 549 /* Read raw data values. */
cparata 3:4274d9103f1d 550 if (lsm6dso_acceleration_raw_get(&_reg_ctx, data_raw.u8bit) != 0) {
cparata 3:4274d9103f1d 551 return 1;
cparata 3:4274d9103f1d 552 }
cparata 3:4274d9103f1d 553
cparata 3:4274d9103f1d 554 /* Get LSM6DSO actual sensitivity. */
cparata 3:4274d9103f1d 555 if (get_x_sensitivity(&sensitivity) != 0) {
cparata 3:4274d9103f1d 556 return 1;
cparata 3:4274d9103f1d 557 }
cparata 3:4274d9103f1d 558
cparata 3:4274d9103f1d 559 /* Calculate the data. */
cparata 3:4274d9103f1d 560 acceleration[0] = (int32_t)((float)((float)data_raw.i16bit[0] * sensitivity));
cparata 3:4274d9103f1d 561 acceleration[1] = (int32_t)((float)((float)data_raw.i16bit[1] * sensitivity));
cparata 3:4274d9103f1d 562 acceleration[2] = (int32_t)((float)((float)data_raw.i16bit[2] * sensitivity));
cparata 3:4274d9103f1d 563
cparata 3:4274d9103f1d 564 return 0;
cparata 0:6d69e896ce38 565 }
cparata 0:6d69e896ce38 566
cparata 0:6d69e896ce38 567
cparata 0:6d69e896ce38 568 /**
cparata 0:6d69e896ce38 569 * @brief Enable the LSM6DSO gyroscope sensor
cparata 0:6d69e896ce38 570 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 571 */
cparata 0:6d69e896ce38 572 int LSM6DSOSensor::enable_g()
cparata 0:6d69e896ce38 573 {
cparata 3:4274d9103f1d 574 /* Check if the component is already enabled */
cparata 3:4274d9103f1d 575 if (_g_is_enabled == 1U) {
cparata 3:4274d9103f1d 576 return 0;
cparata 3:4274d9103f1d 577 }
cparata 3:4274d9103f1d 578
cparata 3:4274d9103f1d 579 /* Output data rate selection. */
cparata 3:4274d9103f1d 580 if (lsm6dso_gy_data_rate_set(&_reg_ctx, _g_last_odr) != 0) {
cparata 3:4274d9103f1d 581 return 1;
cparata 3:4274d9103f1d 582 }
cparata 3:4274d9103f1d 583
cparata 3:4274d9103f1d 584 _g_is_enabled = 1;
cparata 3:4274d9103f1d 585
cparata 0:6d69e896ce38 586 return 0;
cparata 0:6d69e896ce38 587 }
cparata 0:6d69e896ce38 588
cparata 0:6d69e896ce38 589
cparata 0:6d69e896ce38 590 /**
cparata 0:6d69e896ce38 591 * @brief Disable the LSM6DSO gyroscope sensor
cparata 0:6d69e896ce38 592 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 593 */
cparata 0:6d69e896ce38 594 int LSM6DSOSensor::disable_g()
cparata 0:6d69e896ce38 595 {
cparata 3:4274d9103f1d 596 /* Check if the component is already disabled */
cparata 3:4274d9103f1d 597 if (_g_is_enabled == 0U) {
cparata 3:4274d9103f1d 598 return 0;
cparata 3:4274d9103f1d 599 }
cparata 3:4274d9103f1d 600
cparata 3:4274d9103f1d 601 /* Get current output data rate. */
cparata 3:4274d9103f1d 602 if (lsm6dso_gy_data_rate_get(&_reg_ctx, &_g_last_odr) != 0) {
cparata 3:4274d9103f1d 603 return 1;
cparata 3:4274d9103f1d 604 }
cparata 3:4274d9103f1d 605
cparata 3:4274d9103f1d 606 /* Output data rate selection - power down. */
cparata 3:4274d9103f1d 607 if (lsm6dso_gy_data_rate_set(&_reg_ctx, LSM6DSO_GY_ODR_OFF) != 0) {
cparata 3:4274d9103f1d 608 return 1;
cparata 3:4274d9103f1d 609 }
cparata 3:4274d9103f1d 610
cparata 3:4274d9103f1d 611 _g_is_enabled = 0;
cparata 3:4274d9103f1d 612
cparata 0:6d69e896ce38 613 return 0;
cparata 0:6d69e896ce38 614 }
cparata 0:6d69e896ce38 615
cparata 0:6d69e896ce38 616 /**
cparata 0:6d69e896ce38 617 * @brief Get the LSM6DSO gyroscope sensor sensitivity
cparata 0:6d69e896ce38 618 * @param sensitivity pointer where the sensitivity is written
cparata 0:6d69e896ce38 619 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 620 */
cparata 0:6d69e896ce38 621 int LSM6DSOSensor::get_g_sensitivity(float *sensitivity)
cparata 0:6d69e896ce38 622 {
cparata 3:4274d9103f1d 623 int ret = 0;
cparata 3:4274d9103f1d 624 lsm6dso_fs_g_t full_scale;
cparata 3:4274d9103f1d 625
cparata 3:4274d9103f1d 626 /* Read actual full scale selection from sensor. */
cparata 3:4274d9103f1d 627 if (lsm6dso_gy_full_scale_get(&_reg_ctx, &full_scale) != 0) {
cparata 3:4274d9103f1d 628 return 1;
cparata 3:4274d9103f1d 629 }
cparata 3:4274d9103f1d 630
cparata 3:4274d9103f1d 631 /* Store the sensitivity based on actual full scale. */
cparata 3:4274d9103f1d 632 switch (full_scale) {
cparata 3:4274d9103f1d 633 case LSM6DSO_125dps:
cparata 3:4274d9103f1d 634 *sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_125DPS;
cparata 3:4274d9103f1d 635 break;
cparata 3:4274d9103f1d 636
cparata 3:4274d9103f1d 637 case LSM6DSO_250dps:
cparata 3:4274d9103f1d 638 *sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_250DPS;
cparata 3:4274d9103f1d 639 break;
cparata 3:4274d9103f1d 640
cparata 3:4274d9103f1d 641 case LSM6DSO_500dps:
cparata 3:4274d9103f1d 642 *sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_500DPS;
cparata 3:4274d9103f1d 643 break;
cparata 3:4274d9103f1d 644
cparata 3:4274d9103f1d 645 case LSM6DSO_1000dps:
cparata 3:4274d9103f1d 646 *sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_1000DPS;
cparata 3:4274d9103f1d 647 break;
cparata 3:4274d9103f1d 648
cparata 3:4274d9103f1d 649 case LSM6DSO_2000dps:
cparata 3:4274d9103f1d 650 *sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_2000DPS;
cparata 3:4274d9103f1d 651 break;
cparata 3:4274d9103f1d 652
cparata 3:4274d9103f1d 653 default:
cparata 3:4274d9103f1d 654 ret = 1;
cparata 3:4274d9103f1d 655 break;
cparata 3:4274d9103f1d 656 }
cparata 3:4274d9103f1d 657
cparata 3:4274d9103f1d 658 return ret;
cparata 0:6d69e896ce38 659 }
cparata 0:6d69e896ce38 660
cparata 0:6d69e896ce38 661 /**
cparata 0:6d69e896ce38 662 * @brief Get the LSM6DSO gyroscope sensor output data rate
cparata 0:6d69e896ce38 663 * @param odr pointer where the output data rate is written
cparata 0:6d69e896ce38 664 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 665 */
cparata 0:6d69e896ce38 666 int LSM6DSOSensor::get_g_odr(float *odr)
cparata 0:6d69e896ce38 667 {
cparata 3:4274d9103f1d 668 int ret = 0;
cparata 3:4274d9103f1d 669 lsm6dso_odr_g_t odr_low_level;
cparata 3:4274d9103f1d 670
cparata 3:4274d9103f1d 671 /* Get current output data rate. */
cparata 3:4274d9103f1d 672 if (lsm6dso_gy_data_rate_get(&_reg_ctx, &odr_low_level) != 0) {
cparata 3:4274d9103f1d 673 return 1;
cparata 3:4274d9103f1d 674 }
cparata 3:4274d9103f1d 675
cparata 3:4274d9103f1d 676 switch (odr_low_level) {
cparata 3:4274d9103f1d 677 case LSM6DSO_GY_ODR_OFF:
cparata 3:4274d9103f1d 678 *odr = 0.0f;
cparata 3:4274d9103f1d 679 break;
cparata 3:4274d9103f1d 680
cparata 3:4274d9103f1d 681 case LSM6DSO_GY_ODR_12Hz5:
cparata 3:4274d9103f1d 682 *odr = 12.5f;
cparata 3:4274d9103f1d 683 break;
cparata 3:4274d9103f1d 684
cparata 3:4274d9103f1d 685 case LSM6DSO_GY_ODR_26Hz:
cparata 3:4274d9103f1d 686 *odr = 26.0f;
cparata 3:4274d9103f1d 687 break;
cparata 3:4274d9103f1d 688
cparata 3:4274d9103f1d 689 case LSM6DSO_GY_ODR_52Hz:
cparata 3:4274d9103f1d 690 *odr = 52.0f;
cparata 3:4274d9103f1d 691 break;
cparata 3:4274d9103f1d 692
cparata 3:4274d9103f1d 693 case LSM6DSO_GY_ODR_104Hz:
cparata 3:4274d9103f1d 694 *odr = 104.0f;
cparata 3:4274d9103f1d 695 break;
cparata 3:4274d9103f1d 696
cparata 3:4274d9103f1d 697 case LSM6DSO_GY_ODR_208Hz:
cparata 3:4274d9103f1d 698 *odr = 208.0f;
cparata 3:4274d9103f1d 699 break;
cparata 3:4274d9103f1d 700
cparata 3:4274d9103f1d 701 case LSM6DSO_GY_ODR_417Hz:
cparata 3:4274d9103f1d 702 *odr = 417.0f;
cparata 3:4274d9103f1d 703 break;
cparata 3:4274d9103f1d 704
cparata 3:4274d9103f1d 705 case LSM6DSO_GY_ODR_833Hz:
cparata 3:4274d9103f1d 706 *odr = 833.0f;
cparata 3:4274d9103f1d 707 break;
cparata 3:4274d9103f1d 708
cparata 3:4274d9103f1d 709 case LSM6DSO_GY_ODR_1667Hz:
cparata 3:4274d9103f1d 710 *odr = 1667.0f;
cparata 3:4274d9103f1d 711 break;
cparata 3:4274d9103f1d 712
cparata 3:4274d9103f1d 713 case LSM6DSO_GY_ODR_3333Hz:
cparata 3:4274d9103f1d 714 *odr = 3333.0f;
cparata 3:4274d9103f1d 715 break;
cparata 3:4274d9103f1d 716
cparata 3:4274d9103f1d 717 case LSM6DSO_GY_ODR_6667Hz:
cparata 3:4274d9103f1d 718 *odr = 6667.0f;
cparata 3:4274d9103f1d 719 break;
cparata 3:4274d9103f1d 720
cparata 3:4274d9103f1d 721 default:
cparata 3:4274d9103f1d 722 ret = 1;
cparata 3:4274d9103f1d 723 break;
cparata 3:4274d9103f1d 724 }
cparata 3:4274d9103f1d 725
cparata 3:4274d9103f1d 726 return ret;
cparata 0:6d69e896ce38 727 }
cparata 0:6d69e896ce38 728
cparata 0:6d69e896ce38 729 /**
cparata 0:6d69e896ce38 730 * @brief Set the LSM6DSO gyroscope sensor output data rate
cparata 0:6d69e896ce38 731 * @param odr the output data rate value to be set
cparata 0:6d69e896ce38 732 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 733 */
cparata 0:6d69e896ce38 734 int LSM6DSOSensor::set_g_odr(float odr)
cparata 0:6d69e896ce38 735 {
cparata 3:4274d9103f1d 736 /* Check if the component is enabled */
cparata 3:4274d9103f1d 737 if (_g_is_enabled == 1U) {
cparata 3:4274d9103f1d 738 return set_g_odr_when_enabled(odr);
cparata 3:4274d9103f1d 739 } else {
cparata 3:4274d9103f1d 740 return set_g_odr_when_disabled(odr);
cparata 3:4274d9103f1d 741 }
cparata 0:6d69e896ce38 742 }
cparata 0:6d69e896ce38 743
cparata 0:6d69e896ce38 744 /**
cparata 0:6d69e896ce38 745 * @brief Set the LSM6DSO gyroscope sensor output data rate when enabled
cparata 0:6d69e896ce38 746 * @param odr the functional output data rate to be set
cparata 0:6d69e896ce38 747 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 748 */
cparata 0:6d69e896ce38 749 int LSM6DSOSensor::set_g_odr_when_enabled(float odr)
cparata 0:6d69e896ce38 750 {
cparata 3:4274d9103f1d 751 lsm6dso_odr_g_t new_odr;
cparata 3:4274d9103f1d 752
cparata 3:4274d9103f1d 753 new_odr = (odr <= 12.5f) ? LSM6DSO_GY_ODR_12Hz5
cparata 3:4274d9103f1d 754 : (odr <= 26.0f) ? LSM6DSO_GY_ODR_26Hz
cparata 3:4274d9103f1d 755 : (odr <= 52.0f) ? LSM6DSO_GY_ODR_52Hz
cparata 3:4274d9103f1d 756 : (odr <= 104.0f) ? LSM6DSO_GY_ODR_104Hz
cparata 3:4274d9103f1d 757 : (odr <= 208.0f) ? LSM6DSO_GY_ODR_208Hz
cparata 3:4274d9103f1d 758 : (odr <= 417.0f) ? LSM6DSO_GY_ODR_417Hz
cparata 3:4274d9103f1d 759 : (odr <= 833.0f) ? LSM6DSO_GY_ODR_833Hz
cparata 3:4274d9103f1d 760 : (odr <= 1667.0f) ? LSM6DSO_GY_ODR_1667Hz
cparata 3:4274d9103f1d 761 : (odr <= 3333.0f) ? LSM6DSO_GY_ODR_3333Hz
cparata 3:4274d9103f1d 762 : LSM6DSO_GY_ODR_6667Hz;
cparata 3:4274d9103f1d 763
cparata 3:4274d9103f1d 764 /* Output data rate selection. */
cparata 3:4274d9103f1d 765 if (lsm6dso_gy_data_rate_set(&_reg_ctx, new_odr) != 0) {
cparata 3:4274d9103f1d 766 return 1;
cparata 3:4274d9103f1d 767 }
cparata 3:4274d9103f1d 768
cparata 3:4274d9103f1d 769 return 0;
cparata 0:6d69e896ce38 770 }
cparata 0:6d69e896ce38 771
cparata 0:6d69e896ce38 772 /**
cparata 0:6d69e896ce38 773 * @brief Set the LSM6DSO gyroscope sensor output data rate when disabled
cparata 0:6d69e896ce38 774 * @param odr the functional output data rate to be set
cparata 0:6d69e896ce38 775 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 776 */
cparata 0:6d69e896ce38 777 int LSM6DSOSensor::set_g_odr_when_disabled(float odr)
cparata 0:6d69e896ce38 778 {
cparata 3:4274d9103f1d 779 _g_last_odr = (odr <= 12.5f) ? LSM6DSO_GY_ODR_12Hz5
cparata 3:4274d9103f1d 780 : (odr <= 26.0f) ? LSM6DSO_GY_ODR_26Hz
cparata 3:4274d9103f1d 781 : (odr <= 52.0f) ? LSM6DSO_GY_ODR_52Hz
cparata 3:4274d9103f1d 782 : (odr <= 104.0f) ? LSM6DSO_GY_ODR_104Hz
cparata 3:4274d9103f1d 783 : (odr <= 208.0f) ? LSM6DSO_GY_ODR_208Hz
cparata 3:4274d9103f1d 784 : (odr <= 417.0f) ? LSM6DSO_GY_ODR_417Hz
cparata 3:4274d9103f1d 785 : (odr <= 833.0f) ? LSM6DSO_GY_ODR_833Hz
cparata 3:4274d9103f1d 786 : (odr <= 1667.0f) ? LSM6DSO_GY_ODR_1667Hz
cparata 3:4274d9103f1d 787 : (odr <= 3333.0f) ? LSM6DSO_GY_ODR_3333Hz
cparata 3:4274d9103f1d 788 : LSM6DSO_GY_ODR_6667Hz;
cparata 3:4274d9103f1d 789
cparata 3:4274d9103f1d 790 return 0;
cparata 0:6d69e896ce38 791 }
cparata 0:6d69e896ce38 792
cparata 0:6d69e896ce38 793
cparata 0:6d69e896ce38 794 /**
cparata 0:6d69e896ce38 795 * @brief Get the LSM6DSO gyroscope sensor full scale
cparata 0:6d69e896ce38 796 * @param full_scale pointer where the full scale is written
cparata 0:6d69e896ce38 797 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 798 */
cparata 0:6d69e896ce38 799 int LSM6DSOSensor::get_g_fs(float *full_scale)
cparata 0:6d69e896ce38 800 {
cparata 3:4274d9103f1d 801 int ret = 0;
cparata 3:4274d9103f1d 802 lsm6dso_fs_g_t fs_low_level;
cparata 3:4274d9103f1d 803
cparata 3:4274d9103f1d 804 /* Read actual full scale selection from sensor. */
cparata 3:4274d9103f1d 805 if (lsm6dso_gy_full_scale_get(&_reg_ctx, &fs_low_level) != 0) {
cparata 3:4274d9103f1d 806 return 1;
cparata 3:4274d9103f1d 807 }
cparata 3:4274d9103f1d 808
cparata 3:4274d9103f1d 809 switch (fs_low_level) {
cparata 3:4274d9103f1d 810 case LSM6DSO_125dps:
cparata 3:4274d9103f1d 811 *full_scale = 125.0f;
cparata 3:4274d9103f1d 812 break;
cparata 3:4274d9103f1d 813
cparata 3:4274d9103f1d 814 case LSM6DSO_250dps:
cparata 3:4274d9103f1d 815 *full_scale = 250.0f;
cparata 3:4274d9103f1d 816 break;
cparata 3:4274d9103f1d 817
cparata 3:4274d9103f1d 818 case LSM6DSO_500dps:
cparata 3:4274d9103f1d 819 *full_scale = 500.0f;
cparata 3:4274d9103f1d 820 break;
cparata 3:4274d9103f1d 821
cparata 3:4274d9103f1d 822 case LSM6DSO_1000dps:
cparata 3:4274d9103f1d 823 *full_scale = 1000.0f;
cparata 3:4274d9103f1d 824 break;
cparata 3:4274d9103f1d 825
cparata 3:4274d9103f1d 826 case LSM6DSO_2000dps:
cparata 3:4274d9103f1d 827 *full_scale = 2000.0f;
cparata 3:4274d9103f1d 828 break;
cparata 3:4274d9103f1d 829
cparata 3:4274d9103f1d 830 default:
cparata 3:4274d9103f1d 831 ret = 1;
cparata 3:4274d9103f1d 832 break;
cparata 3:4274d9103f1d 833 }
cparata 3:4274d9103f1d 834
cparata 3:4274d9103f1d 835 return ret;
cparata 0:6d69e896ce38 836 }
cparata 0:6d69e896ce38 837
cparata 0:6d69e896ce38 838 /**
cparata 0:6d69e896ce38 839 * @brief Set the LSM6DSO gyroscope sensor full scale
cparata 0:6d69e896ce38 840 * @param full_scale the functional full scale to be set
cparata 0:6d69e896ce38 841 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 842 */
cparata 0:6d69e896ce38 843 int LSM6DSOSensor::set_g_fs(float full_scale)
cparata 0:6d69e896ce38 844 {
cparata 3:4274d9103f1d 845 lsm6dso_fs_g_t new_fs;
cparata 3:4274d9103f1d 846
cparata 3:4274d9103f1d 847 new_fs = (full_scale <= 125.0f) ? LSM6DSO_125dps
cparata 3:4274d9103f1d 848 : (full_scale <= 250.0f) ? LSM6DSO_250dps
cparata 3:4274d9103f1d 849 : (full_scale <= 500.0f) ? LSM6DSO_500dps
cparata 3:4274d9103f1d 850 : (full_scale <= 1000.0f) ? LSM6DSO_1000dps
cparata 3:4274d9103f1d 851 : LSM6DSO_2000dps;
cparata 3:4274d9103f1d 852
cparata 3:4274d9103f1d 853 if (lsm6dso_gy_full_scale_set(&_reg_ctx, new_fs) != 0) {
cparata 3:4274d9103f1d 854 return 1;
cparata 3:4274d9103f1d 855 }
cparata 3:4274d9103f1d 856
cparata 3:4274d9103f1d 857 return 0;
cparata 0:6d69e896ce38 858 }
cparata 0:6d69e896ce38 859
cparata 0:6d69e896ce38 860 /**
cparata 0:6d69e896ce38 861 * @brief Get the LSM6DSO gyroscope sensor raw axes
cparata 0:6d69e896ce38 862 * @param value pointer where the raw values of the axes are written
cparata 0:6d69e896ce38 863 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 864 */
cparata 0:6d69e896ce38 865 int LSM6DSOSensor::get_g_axes_raw(int16_t *value)
cparata 0:6d69e896ce38 866 {
cparata 3:4274d9103f1d 867 axis3bit16_t data_raw;
cparata 3:4274d9103f1d 868
cparata 3:4274d9103f1d 869 /* Read raw data values. */
cparata 3:4274d9103f1d 870 if (lsm6dso_angular_rate_raw_get(&_reg_ctx, data_raw.u8bit) != 0) {
cparata 3:4274d9103f1d 871 return 1;
cparata 3:4274d9103f1d 872 }
cparata 3:4274d9103f1d 873
cparata 3:4274d9103f1d 874 /* Format the data. */
cparata 3:4274d9103f1d 875 value[0] = data_raw.i16bit[0];
cparata 3:4274d9103f1d 876 value[1] = data_raw.i16bit[1];
cparata 3:4274d9103f1d 877 value[2] = data_raw.i16bit[2];
cparata 3:4274d9103f1d 878
cparata 3:4274d9103f1d 879 return 0;
cparata 0:6d69e896ce38 880 }
cparata 0:6d69e896ce38 881
cparata 0:6d69e896ce38 882
cparata 0:6d69e896ce38 883 /**
cparata 0:6d69e896ce38 884 * @brief Get the LSM6DSO gyroscope sensor axes
cparata 0:6d69e896ce38 885 * @param angular_rate pointer where the values of the axes are written
cparata 0:6d69e896ce38 886 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 887 */
cparata 0:6d69e896ce38 888 int LSM6DSOSensor::get_g_axes(int32_t *angular_rate)
cparata 0:6d69e896ce38 889 {
cparata 3:4274d9103f1d 890 axis3bit16_t data_raw;
cparata 3:4274d9103f1d 891 float sensitivity;
cparata 3:4274d9103f1d 892
cparata 3:4274d9103f1d 893 /* Read raw data values. */
cparata 3:4274d9103f1d 894 if (lsm6dso_angular_rate_raw_get(&_reg_ctx, data_raw.u8bit) != 0) {
cparata 3:4274d9103f1d 895 return 1;
cparata 3:4274d9103f1d 896 }
cparata 3:4274d9103f1d 897
cparata 3:4274d9103f1d 898 /* Get LSM6DSO actual sensitivity. */
cparata 3:4274d9103f1d 899 if (get_g_sensitivity(&sensitivity) != 0) {
cparata 3:4274d9103f1d 900 return 1;
cparata 3:4274d9103f1d 901 }
cparata 3:4274d9103f1d 902
cparata 3:4274d9103f1d 903 /* Calculate the data. */
cparata 3:4274d9103f1d 904 angular_rate[0] = (int32_t)((float)((float)data_raw.i16bit[0] * sensitivity));
cparata 3:4274d9103f1d 905 angular_rate[1] = (int32_t)((float)((float)data_raw.i16bit[1] * sensitivity));
cparata 3:4274d9103f1d 906 angular_rate[2] = (int32_t)((float)((float)data_raw.i16bit[2] * sensitivity));
cparata 3:4274d9103f1d 907
cparata 3:4274d9103f1d 908 return 0;
cparata 0:6d69e896ce38 909 }
cparata 0:6d69e896ce38 910
cparata 0:6d69e896ce38 911
cparata 0:6d69e896ce38 912 /**
cparata 0:6d69e896ce38 913 * @brief Get the LSM6DSO register value
cparata 0:6d69e896ce38 914 * @param reg address to be read
cparata 0:6d69e896ce38 915 * @param data pointer where the value is written
cparata 0:6d69e896ce38 916 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 917 */
cparata 0:6d69e896ce38 918 int LSM6DSOSensor::read_reg(uint8_t reg, uint8_t *data)
cparata 0:6d69e896ce38 919 {
cparata 3:4274d9103f1d 920 if (lsm6dso_read_reg(&_reg_ctx, reg, data, 1) != 0) {
cparata 3:4274d9103f1d 921 return 1;
cparata 3:4274d9103f1d 922 }
cparata 3:4274d9103f1d 923
cparata 3:4274d9103f1d 924 return 0;
cparata 0:6d69e896ce38 925 }
cparata 0:6d69e896ce38 926
cparata 0:6d69e896ce38 927
cparata 0:6d69e896ce38 928 /**
cparata 0:6d69e896ce38 929 * @brief Set the LSM6DSO register value
cparata 0:6d69e896ce38 930 * @param reg address to be written
cparata 0:6d69e896ce38 931 * @param data value to be written
cparata 0:6d69e896ce38 932 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 933 */
cparata 0:6d69e896ce38 934 int LSM6DSOSensor::write_reg(uint8_t reg, uint8_t data)
cparata 0:6d69e896ce38 935 {
cparata 3:4274d9103f1d 936 if (lsm6dso_write_reg(&_reg_ctx, reg, &data, 1) != 0) {
cparata 3:4274d9103f1d 937 return 1;
cparata 3:4274d9103f1d 938 }
cparata 3:4274d9103f1d 939
cparata 3:4274d9103f1d 940 return 0;
cparata 0:6d69e896ce38 941 }
cparata 0:6d69e896ce38 942
cparata 0:6d69e896ce38 943 /**
cparata 0:6d69e896ce38 944 * @brief Set the interrupt latch
cparata 0:6d69e896ce38 945 * @param status value to be written
cparata 0:6d69e896ce38 946 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 947 */
cparata 0:6d69e896ce38 948 int LSM6DSOSensor::set_interrupt_latch(uint8_t status)
cparata 0:6d69e896ce38 949 {
cparata 3:4274d9103f1d 950 if (status > 1U) {
cparata 3:4274d9103f1d 951 return 1;
cparata 3:4274d9103f1d 952 }
cparata 3:4274d9103f1d 953
cparata 3:4274d9103f1d 954 if (lsm6dso_int_notification_set(&_reg_ctx, (lsm6dso_lir_t)status) != 0) {
cparata 3:4274d9103f1d 955 return 1;
cparata 3:4274d9103f1d 956 }
cparata 3:4274d9103f1d 957
cparata 3:4274d9103f1d 958 return 0;
cparata 0:6d69e896ce38 959 }
cparata 0:6d69e896ce38 960
cparata 0:6d69e896ce38 961 /**
cparata 0:6d69e896ce38 962 * @brief Enable free fall detection
cparata 0:6d69e896ce38 963 * @param int_pin interrupt pin line to be used
cparata 0:6d69e896ce38 964 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 965 */
cparata 0:6d69e896ce38 966 int LSM6DSOSensor::enable_free_fall_detection(LSM6DSO_Interrupt_Pin_t int_pin)
cparata 0:6d69e896ce38 967 {
cparata 3:4274d9103f1d 968 int ret = 0;
cparata 3:4274d9103f1d 969 lsm6dso_pin_int1_route_t val1;
cparata 3:4274d9103f1d 970 lsm6dso_pin_int2_route_t val2;
cparata 3:4274d9103f1d 971
cparata 3:4274d9103f1d 972 /* Output Data Rate selection */
cparata 3:4274d9103f1d 973 if (set_x_odr(416.0f) != 0) {
cparata 3:4274d9103f1d 974 return 1;
cparata 3:4274d9103f1d 975 }
cparata 3:4274d9103f1d 976
cparata 3:4274d9103f1d 977 /* Full scale selection */
cparata 3:4274d9103f1d 978 if (set_x_fs(2.0f) != 0) {
cparata 3:4274d9103f1d 979 return 1;
cparata 3:4274d9103f1d 980 }
cparata 3:4274d9103f1d 981
cparata 3:4274d9103f1d 982 /* FF_DUR setting */
cparata 3:4274d9103f1d 983 if (lsm6dso_ff_dur_set(&_reg_ctx, 0x06) != 0) {
cparata 3:4274d9103f1d 984 return 1;
cparata 3:4274d9103f1d 985 }
cparata 3:4274d9103f1d 986
cparata 3:4274d9103f1d 987 /* WAKE_DUR setting */
cparata 3:4274d9103f1d 988 if (lsm6dso_wkup_dur_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 989 return 1;
cparata 3:4274d9103f1d 990 }
cparata 3:4274d9103f1d 991
cparata 3:4274d9103f1d 992 /* SLEEP_DUR setting */
cparata 3:4274d9103f1d 993 if (lsm6dso_act_sleep_dur_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 994 return 1;
cparata 3:4274d9103f1d 995 }
cparata 3:4274d9103f1d 996
cparata 3:4274d9103f1d 997 /* FF_THS setting */
cparata 3:4274d9103f1d 998 if (lsm6dso_ff_threshold_set(&_reg_ctx, LSM6DSO_FF_TSH_312mg) != 0) {
cparata 0:6d69e896ce38 999 return 1;
cparata 3:4274d9103f1d 1000 }
cparata 3:4274d9103f1d 1001
cparata 3:4274d9103f1d 1002 /* Enable free fall event on either INT1 or INT2 pin */
cparata 3:4274d9103f1d 1003 switch (int_pin) {
cparata 3:4274d9103f1d 1004 case LSM6DSO_INT1_PIN:
cparata 3:4274d9103f1d 1005 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1006 return 1;
cparata 3:4274d9103f1d 1007 }
cparata 3:4274d9103f1d 1008
cparata 3:4274d9103f1d 1009 val1.md1_cfg.int1_ff = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1010
cparata 3:4274d9103f1d 1011 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1012 return 1;
cparata 3:4274d9103f1d 1013 }
cparata 3:4274d9103f1d 1014 break;
cparata 3:4274d9103f1d 1015
cparata 3:4274d9103f1d 1016 case LSM6DSO_INT2_PIN:
cparata 3:4274d9103f1d 1017 if (lsm6dso_pin_int2_route_get(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1018 return 1;
cparata 3:4274d9103f1d 1019 }
cparata 3:4274d9103f1d 1020
cparata 3:4274d9103f1d 1021 val2.md2_cfg.int2_ff = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1022
cparata 3:4274d9103f1d 1023 if (lsm6dso_pin_int2_route_set(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1024 return 1;
cparata 3:4274d9103f1d 1025 }
cparata 3:4274d9103f1d 1026 break;
cparata 3:4274d9103f1d 1027
cparata 3:4274d9103f1d 1028 default:
cparata 3:4274d9103f1d 1029 ret = 1;
cparata 3:4274d9103f1d 1030 break;
cparata 3:4274d9103f1d 1031 }
cparata 3:4274d9103f1d 1032
cparata 3:4274d9103f1d 1033 return ret;
cparata 0:6d69e896ce38 1034 }
cparata 0:6d69e896ce38 1035
cparata 0:6d69e896ce38 1036 /**
cparata 0:6d69e896ce38 1037 * @brief Disable free fall detection
cparata 0:6d69e896ce38 1038 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1039 */
cparata 0:6d69e896ce38 1040 int LSM6DSOSensor::disable_free_fall_detection()
cparata 0:6d69e896ce38 1041 {
cparata 3:4274d9103f1d 1042 lsm6dso_pin_int1_route_t val1;
cparata 3:4274d9103f1d 1043 lsm6dso_pin_int2_route_t val2;
cparata 3:4274d9103f1d 1044
cparata 3:4274d9103f1d 1045 /* Disable free fall event on both INT1 and INT2 pins */
cparata 3:4274d9103f1d 1046 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1047 return 1;
cparata 3:4274d9103f1d 1048 }
cparata 3:4274d9103f1d 1049
cparata 3:4274d9103f1d 1050 val1.md1_cfg.int1_ff = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1051
cparata 3:4274d9103f1d 1052 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1053 return 1;
cparata 3:4274d9103f1d 1054 }
cparata 3:4274d9103f1d 1055
cparata 3:4274d9103f1d 1056 if (lsm6dso_pin_int2_route_get(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1057 return 1;
cparata 3:4274d9103f1d 1058 }
cparata 3:4274d9103f1d 1059
cparata 3:4274d9103f1d 1060 val2.md2_cfg.int2_ff = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1061
cparata 3:4274d9103f1d 1062 if (lsm6dso_pin_int2_route_set(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1063 return 1;
cparata 3:4274d9103f1d 1064 }
cparata 3:4274d9103f1d 1065
cparata 3:4274d9103f1d 1066 /* FF_DUR setting */
cparata 3:4274d9103f1d 1067 if (lsm6dso_ff_dur_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 1068 return 1;
cparata 3:4274d9103f1d 1069 }
cparata 3:4274d9103f1d 1070
cparata 3:4274d9103f1d 1071 /* FF_THS setting */
cparata 3:4274d9103f1d 1072 if (lsm6dso_ff_threshold_set(&_reg_ctx, LSM6DSO_FF_TSH_156mg) != 0) {
cparata 3:4274d9103f1d 1073 return 1;
cparata 3:4274d9103f1d 1074 }
cparata 3:4274d9103f1d 1075
cparata 3:4274d9103f1d 1076 return 0;
cparata 0:6d69e896ce38 1077 }
cparata 0:6d69e896ce38 1078
cparata 0:6d69e896ce38 1079 /**
cparata 0:6d69e896ce38 1080 * @brief Set free fall threshold
cparata 0:6d69e896ce38 1081 * @param thr free fall detection threshold
cparata 0:6d69e896ce38 1082 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1083 */
cparata 0:6d69e896ce38 1084 int LSM6DSOSensor::set_free_fall_threshold(uint8_t thr)
cparata 0:6d69e896ce38 1085 {
cparata 3:4274d9103f1d 1086 if (lsm6dso_ff_threshold_set(&_reg_ctx, (lsm6dso_ff_ths_t)thr) != 0) {
cparata 3:4274d9103f1d 1087 return 1;
cparata 3:4274d9103f1d 1088 }
cparata 3:4274d9103f1d 1089
cparata 3:4274d9103f1d 1090 return 0;
cparata 0:6d69e896ce38 1091 }
cparata 0:6d69e896ce38 1092
cparata 0:6d69e896ce38 1093
cparata 0:6d69e896ce38 1094 /**
cparata 0:6d69e896ce38 1095 * @brief Set free fall duration
cparata 0:6d69e896ce38 1096 * @param dur free fall detection duration
cparata 0:6d69e896ce38 1097 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1098 */
cparata 0:6d69e896ce38 1099 int LSM6DSOSensor::set_free_fall_duration(uint8_t dur)
cparata 0:6d69e896ce38 1100 {
cparata 3:4274d9103f1d 1101 if (lsm6dso_ff_dur_set(&_reg_ctx, dur) != 0) {
cparata 3:4274d9103f1d 1102 return 1;
cparata 3:4274d9103f1d 1103 }
cparata 3:4274d9103f1d 1104
cparata 3:4274d9103f1d 1105 return 0;
cparata 0:6d69e896ce38 1106 }
cparata 0:6d69e896ce38 1107
cparata 0:6d69e896ce38 1108
cparata 0:6d69e896ce38 1109 /**
cparata 0:6d69e896ce38 1110 * @brief Enable pedometer
cparata 0:6d69e896ce38 1111 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1112 */
cparata 0:6d69e896ce38 1113 int LSM6DSOSensor::enable_pedometer()
cparata 0:6d69e896ce38 1114 {
cparata 0:6d69e896ce38 1115 lsm6dso_pin_int1_route_t val;
cparata 0:6d69e896ce38 1116
cparata 3:4274d9103f1d 1117 /* Output Data Rate selection */
cparata 3:4274d9103f1d 1118 if (set_x_odr(26.0f) != 0) {
cparata 3:4274d9103f1d 1119 return 1;
cparata 3:4274d9103f1d 1120 }
cparata 3:4274d9103f1d 1121
cparata 3:4274d9103f1d 1122 /* Full scale selection */
cparata 3:4274d9103f1d 1123 if (set_x_fs(2.0f) != 0) {
cparata 3:4274d9103f1d 1124 return 1;
cparata 3:4274d9103f1d 1125 }
cparata 3:4274d9103f1d 1126
cparata 3:4274d9103f1d 1127 /* Enable pedometer algorithm. */
cparata 3:4274d9103f1d 1128 if (lsm6dso_pedo_sens_set(&_reg_ctx, LSM6DSO_PEDO_BASE_MODE) != 0) {
cparata 3:4274d9103f1d 1129 return 1;
cparata 3:4274d9103f1d 1130 }
cparata 3:4274d9103f1d 1131
cparata 3:4274d9103f1d 1132 /* Enable step detector on INT1 pin */
cparata 3:4274d9103f1d 1133 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val) != 0) {
cparata 3:4274d9103f1d 1134 return 1;
cparata 3:4274d9103f1d 1135 }
cparata 3:4274d9103f1d 1136
cparata 3:4274d9103f1d 1137 val.emb_func_int1.int1_step_detector = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1138
cparata 3:4274d9103f1d 1139 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val) != 0) {
cparata 3:4274d9103f1d 1140 return 1;
cparata 3:4274d9103f1d 1141 }
cparata 3:4274d9103f1d 1142
cparata 3:4274d9103f1d 1143 return 0;
cparata 0:6d69e896ce38 1144 }
cparata 0:6d69e896ce38 1145
cparata 0:6d69e896ce38 1146 /**
cparata 0:6d69e896ce38 1147 * @brief Disable pedometer
cparata 0:6d69e896ce38 1148 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1149 */
cparata 0:6d69e896ce38 1150 int LSM6DSOSensor::disable_pedometer()
cparata 0:6d69e896ce38 1151 {
cparata 3:4274d9103f1d 1152 lsm6dso_pin_int1_route_t val1;
cparata 3:4274d9103f1d 1153
cparata 3:4274d9103f1d 1154 /* Disable step detector on INT1 pin */
cparata 3:4274d9103f1d 1155 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1156 return 1;
cparata 3:4274d9103f1d 1157 }
cparata 3:4274d9103f1d 1158
cparata 3:4274d9103f1d 1159 val1.emb_func_int1.int1_step_detector = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1160
cparata 3:4274d9103f1d 1161 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1162 return 1;
cparata 3:4274d9103f1d 1163 }
cparata 3:4274d9103f1d 1164
cparata 3:4274d9103f1d 1165 /* Disable pedometer algorithm. */
cparata 3:4274d9103f1d 1166 if (lsm6dso_pedo_sens_set(&_reg_ctx, LSM6DSO_PEDO_DISABLE) != 0) {
cparata 3:4274d9103f1d 1167 return 1;
cparata 3:4274d9103f1d 1168 }
cparata 3:4274d9103f1d 1169
cparata 3:4274d9103f1d 1170 return 0;
cparata 0:6d69e896ce38 1171 }
cparata 0:6d69e896ce38 1172
cparata 0:6d69e896ce38 1173 /**
cparata 0:6d69e896ce38 1174 * @brief Get step count
cparata 0:6d69e896ce38 1175 * @param step_count step counter
cparata 0:6d69e896ce38 1176 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1177 */
cparata 0:6d69e896ce38 1178 int LSM6DSOSensor::get_step_counter(uint16_t *step_count)
cparata 0:6d69e896ce38 1179 {
cparata 3:4274d9103f1d 1180 if (lsm6dso_number_of_steps_get(&_reg_ctx, (uint8_t *)step_count) != 0) {
cparata 3:4274d9103f1d 1181 return 1;
cparata 3:4274d9103f1d 1182 }
cparata 3:4274d9103f1d 1183
cparata 3:4274d9103f1d 1184 return 0;
cparata 0:6d69e896ce38 1185 }
cparata 0:6d69e896ce38 1186
cparata 0:6d69e896ce38 1187 /**
cparata 0:6d69e896ce38 1188 * @brief Enable step counter reset
cparata 0:6d69e896ce38 1189 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1190 */
cparata 0:6d69e896ce38 1191 int LSM6DSOSensor::reset_step_counter()
cparata 0:6d69e896ce38 1192 {
cparata 3:4274d9103f1d 1193 if (lsm6dso_steps_reset(&_reg_ctx) != 0) {
cparata 3:4274d9103f1d 1194 return 1;
cparata 3:4274d9103f1d 1195 }
cparata 3:4274d9103f1d 1196
cparata 3:4274d9103f1d 1197 return 0;
cparata 0:6d69e896ce38 1198 }
cparata 0:6d69e896ce38 1199
cparata 0:6d69e896ce38 1200 /**
cparata 0:6d69e896ce38 1201 * @brief Enable tilt detection
cparata 0:6d69e896ce38 1202 * @param int_pin interrupt pin line to be used
cparata 0:6d69e896ce38 1203 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1204 */
cparata 0:6d69e896ce38 1205 int LSM6DSOSensor::enable_tilt_detection(LSM6DSO_Interrupt_Pin_t int_pin)
cparata 0:6d69e896ce38 1206 {
cparata 3:4274d9103f1d 1207 int ret = 0;
cparata 3:4274d9103f1d 1208 lsm6dso_pin_int1_route_t val1;
cparata 3:4274d9103f1d 1209 lsm6dso_pin_int2_route_t val2;
cparata 3:4274d9103f1d 1210
cparata 3:4274d9103f1d 1211 /* Output Data Rate selection */
cparata 3:4274d9103f1d 1212 if (set_x_odr(26.0f) != 0) {
cparata 3:4274d9103f1d 1213 return 1;
cparata 3:4274d9103f1d 1214 }
cparata 3:4274d9103f1d 1215
cparata 3:4274d9103f1d 1216 /* Full scale selection */
cparata 3:4274d9103f1d 1217 if (set_x_fs(2.0f) != 0) {
cparata 3:4274d9103f1d 1218 return 1;
cparata 3:4274d9103f1d 1219 }
cparata 3:4274d9103f1d 1220
cparata 3:4274d9103f1d 1221 /* Enable tilt calculation. */
cparata 3:4274d9103f1d 1222 if (lsm6dso_tilt_sens_set(&_reg_ctx, PROPERTY_ENABLE) != 0) {
cparata 0:6d69e896ce38 1223 return 1;
cparata 3:4274d9103f1d 1224 }
cparata 3:4274d9103f1d 1225
cparata 3:4274d9103f1d 1226 /* Enable tilt event on either INT1 or INT2 pin */
cparata 3:4274d9103f1d 1227 switch (int_pin) {
cparata 3:4274d9103f1d 1228 case LSM6DSO_INT1_PIN:
cparata 3:4274d9103f1d 1229 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1230 return 1;
cparata 3:4274d9103f1d 1231 }
cparata 3:4274d9103f1d 1232
cparata 3:4274d9103f1d 1233 val1.emb_func_int1.int1_tilt = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1234
cparata 3:4274d9103f1d 1235 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1236 return 1;
cparata 3:4274d9103f1d 1237 }
cparata 3:4274d9103f1d 1238 break;
cparata 3:4274d9103f1d 1239
cparata 3:4274d9103f1d 1240 case LSM6DSO_INT2_PIN:
cparata 3:4274d9103f1d 1241 if (lsm6dso_pin_int2_route_get(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1242 return 1;
cparata 3:4274d9103f1d 1243 }
cparata 3:4274d9103f1d 1244
cparata 3:4274d9103f1d 1245 val2.emb_func_int2.int2_tilt = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1246
cparata 3:4274d9103f1d 1247 if (lsm6dso_pin_int2_route_set(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1248 return 1;
cparata 3:4274d9103f1d 1249 }
cparata 3:4274d9103f1d 1250 break;
cparata 3:4274d9103f1d 1251
cparata 3:4274d9103f1d 1252 default:
cparata 3:4274d9103f1d 1253 ret = 1;
cparata 3:4274d9103f1d 1254 break;
cparata 3:4274d9103f1d 1255 }
cparata 3:4274d9103f1d 1256
cparata 3:4274d9103f1d 1257 return ret;
cparata 0:6d69e896ce38 1258 }
cparata 0:6d69e896ce38 1259
cparata 0:6d69e896ce38 1260 /**
cparata 0:6d69e896ce38 1261 * @brief Disable tilt detection
cparata 0:6d69e896ce38 1262 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1263 */
cparata 0:6d69e896ce38 1264 int LSM6DSOSensor::disable_tilt_detection()
cparata 0:6d69e896ce38 1265 {
cparata 3:4274d9103f1d 1266 lsm6dso_pin_int1_route_t val1;
cparata 3:4274d9103f1d 1267 lsm6dso_pin_int2_route_t val2;
cparata 3:4274d9103f1d 1268
cparata 3:4274d9103f1d 1269 /* Disable tilt event on both INT1 and INT2 pins */
cparata 3:4274d9103f1d 1270 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1271 return 1;
cparata 3:4274d9103f1d 1272 }
cparata 3:4274d9103f1d 1273
cparata 3:4274d9103f1d 1274 val1.emb_func_int1.int1_tilt = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1275
cparata 3:4274d9103f1d 1276 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1277 return 1;
cparata 3:4274d9103f1d 1278 }
cparata 3:4274d9103f1d 1279
cparata 3:4274d9103f1d 1280 if (lsm6dso_pin_int2_route_get(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1281 return 1;
cparata 3:4274d9103f1d 1282 }
cparata 3:4274d9103f1d 1283
cparata 3:4274d9103f1d 1284 val2.emb_func_int2.int2_tilt = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1285
cparata 3:4274d9103f1d 1286 if (lsm6dso_pin_int2_route_set(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1287 return 1;
cparata 3:4274d9103f1d 1288 }
cparata 3:4274d9103f1d 1289
cparata 3:4274d9103f1d 1290 /* Disable tilt calculation. */
cparata 3:4274d9103f1d 1291 if (lsm6dso_tilt_sens_set(&_reg_ctx, PROPERTY_DISABLE) != 0) {
cparata 3:4274d9103f1d 1292 return 1;
cparata 3:4274d9103f1d 1293 }
cparata 3:4274d9103f1d 1294
cparata 3:4274d9103f1d 1295 return 0;
cparata 0:6d69e896ce38 1296 }
cparata 0:6d69e896ce38 1297
cparata 0:6d69e896ce38 1298 /**
cparata 0:6d69e896ce38 1299 * @brief Enable wake up detection
cparata 0:6d69e896ce38 1300 * @param int_pin interrupt pin line to be used
cparata 0:6d69e896ce38 1301 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1302 */
cparata 0:6d69e896ce38 1303 int LSM6DSOSensor::enable_wake_up_detection(LSM6DSO_Interrupt_Pin_t int_pin)
cparata 0:6d69e896ce38 1304 {
cparata 3:4274d9103f1d 1305 int ret = 0;
cparata 3:4274d9103f1d 1306 lsm6dso_pin_int1_route_t val1;
cparata 3:4274d9103f1d 1307 lsm6dso_pin_int2_route_t val2;
cparata 3:4274d9103f1d 1308
cparata 3:4274d9103f1d 1309 /* Output Data Rate selection */
cparata 3:4274d9103f1d 1310 if (set_x_odr(416.0f) != 0) {
cparata 3:4274d9103f1d 1311 return 1;
cparata 3:4274d9103f1d 1312 }
cparata 3:4274d9103f1d 1313
cparata 3:4274d9103f1d 1314 /* Full scale selection */
cparata 3:4274d9103f1d 1315 if (set_x_fs(2.0f) != 0) {
cparata 3:4274d9103f1d 1316 return 1;
cparata 3:4274d9103f1d 1317 }
cparata 3:4274d9103f1d 1318
cparata 3:4274d9103f1d 1319 /* WAKE_DUR setting */
cparata 3:4274d9103f1d 1320 if (lsm6dso_wkup_dur_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 1321 return 1;
cparata 3:4274d9103f1d 1322 }
cparata 3:4274d9103f1d 1323
cparata 3:4274d9103f1d 1324 /* Set wake up threshold. */
cparata 3:4274d9103f1d 1325 if (lsm6dso_wkup_threshold_set(&_reg_ctx, 0x02) != 0) {
cparata 0:6d69e896ce38 1326 return 1;
cparata 3:4274d9103f1d 1327 }
cparata 3:4274d9103f1d 1328
cparata 3:4274d9103f1d 1329 /* Enable wake up event on either INT1 or INT2 pin */
cparata 3:4274d9103f1d 1330 switch (int_pin) {
cparata 3:4274d9103f1d 1331 case LSM6DSO_INT1_PIN:
cparata 3:4274d9103f1d 1332 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1333 return 1;
cparata 3:4274d9103f1d 1334 }
cparata 3:4274d9103f1d 1335
cparata 3:4274d9103f1d 1336 val1.md1_cfg.int1_wu = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1337
cparata 3:4274d9103f1d 1338 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1339 return 1;
cparata 3:4274d9103f1d 1340 }
cparata 3:4274d9103f1d 1341 break;
cparata 3:4274d9103f1d 1342
cparata 3:4274d9103f1d 1343 case LSM6DSO_INT2_PIN:
cparata 3:4274d9103f1d 1344 if (lsm6dso_pin_int2_route_get(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1345 return 1;
cparata 3:4274d9103f1d 1346 }
cparata 3:4274d9103f1d 1347
cparata 3:4274d9103f1d 1348 val2.md2_cfg.int2_wu = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1349
cparata 3:4274d9103f1d 1350 if (lsm6dso_pin_int2_route_set(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1351 return 1;
cparata 3:4274d9103f1d 1352 }
cparata 3:4274d9103f1d 1353 break;
cparata 3:4274d9103f1d 1354
cparata 3:4274d9103f1d 1355 default:
cparata 3:4274d9103f1d 1356 ret = 1;
cparata 3:4274d9103f1d 1357 break;
cparata 3:4274d9103f1d 1358 }
cparata 3:4274d9103f1d 1359
cparata 3:4274d9103f1d 1360 return ret;
cparata 0:6d69e896ce38 1361 }
cparata 0:6d69e896ce38 1362
cparata 0:6d69e896ce38 1363
cparata 0:6d69e896ce38 1364 /**
cparata 0:6d69e896ce38 1365 * @brief Disable wake up detection
cparata 0:6d69e896ce38 1366 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1367 */
cparata 0:6d69e896ce38 1368 int LSM6DSOSensor::disable_wake_up_detection()
cparata 0:6d69e896ce38 1369 {
cparata 0:6d69e896ce38 1370 lsm6dso_pin_int1_route_t val1;
cparata 0:6d69e896ce38 1371 lsm6dso_pin_int2_route_t val2;
cparata 0:6d69e896ce38 1372
cparata 3:4274d9103f1d 1373 /* Disable wake up event on both INT1 and INT2 pins */
cparata 3:4274d9103f1d 1374 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1375 return 1;
cparata 3:4274d9103f1d 1376 }
cparata 3:4274d9103f1d 1377
cparata 3:4274d9103f1d 1378 val1.md1_cfg.int1_wu = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1379
cparata 3:4274d9103f1d 1380 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1381 return 1;
cparata 3:4274d9103f1d 1382 }
cparata 3:4274d9103f1d 1383
cparata 3:4274d9103f1d 1384 if (lsm6dso_pin_int2_route_get(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1385 return 1;
cparata 3:4274d9103f1d 1386 }
cparata 3:4274d9103f1d 1387
cparata 3:4274d9103f1d 1388 val2.md2_cfg.int2_wu = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1389
cparata 3:4274d9103f1d 1390 if (lsm6dso_pin_int2_route_set(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1391 return 1;
cparata 3:4274d9103f1d 1392 }
cparata 3:4274d9103f1d 1393
cparata 3:4274d9103f1d 1394 /* Reset wake up threshold. */
cparata 3:4274d9103f1d 1395 if (lsm6dso_wkup_threshold_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 1396 return 1;
cparata 3:4274d9103f1d 1397 }
cparata 3:4274d9103f1d 1398
cparata 3:4274d9103f1d 1399 /* WAKE_DUR setting */
cparata 3:4274d9103f1d 1400 if (lsm6dso_wkup_dur_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 1401 return 1;
cparata 3:4274d9103f1d 1402 }
cparata 3:4274d9103f1d 1403
cparata 3:4274d9103f1d 1404 return 0;
cparata 0:6d69e896ce38 1405 }
cparata 0:6d69e896ce38 1406
cparata 0:6d69e896ce38 1407 /**
cparata 0:6d69e896ce38 1408 * @brief Set wake up threshold
cparata 0:6d69e896ce38 1409 * @param thr wake up detection threshold
cparata 0:6d69e896ce38 1410 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1411 */
cparata 0:6d69e896ce38 1412 int LSM6DSOSensor::set_wake_up_threshold(uint8_t thr)
cparata 0:6d69e896ce38 1413 {
cparata 3:4274d9103f1d 1414 /* Set wake up threshold. */
cparata 3:4274d9103f1d 1415 if (lsm6dso_wkup_threshold_set(&_reg_ctx, thr) != 0) {
cparata 3:4274d9103f1d 1416 return 1;
cparata 3:4274d9103f1d 1417 }
cparata 3:4274d9103f1d 1418
cparata 3:4274d9103f1d 1419 return 0;
cparata 0:6d69e896ce38 1420 }
cparata 0:6d69e896ce38 1421
cparata 0:6d69e896ce38 1422 /**
cparata 0:6d69e896ce38 1423 * @brief Set wake up duration
cparata 0:6d69e896ce38 1424 * @param dur wake up detection duration
cparata 0:6d69e896ce38 1425 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1426 */
cparata 0:6d69e896ce38 1427 int LSM6DSOSensor::set_wake_up_duration(uint8_t dur)
cparata 0:6d69e896ce38 1428 {
cparata 3:4274d9103f1d 1429 /* Set wake up duration. */
cparata 3:4274d9103f1d 1430 if (lsm6dso_wkup_dur_set(&_reg_ctx, dur) != 0) {
cparata 3:4274d9103f1d 1431 return 1;
cparata 3:4274d9103f1d 1432 }
cparata 3:4274d9103f1d 1433
cparata 3:4274d9103f1d 1434 return 0;
cparata 0:6d69e896ce38 1435 }
cparata 0:6d69e896ce38 1436
cparata 0:6d69e896ce38 1437 /**
cparata 0:6d69e896ce38 1438 * @brief Enable single tap detection
cparata 0:6d69e896ce38 1439 * @param int_pin interrupt pin line to be used
cparata 0:6d69e896ce38 1440 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1441 */
cparata 0:6d69e896ce38 1442 int LSM6DSOSensor::enable_single_tap_detection(LSM6DSO_Interrupt_Pin_t int_pin)
cparata 0:6d69e896ce38 1443 {
cparata 3:4274d9103f1d 1444 int ret = 0;
cparata 3:4274d9103f1d 1445 lsm6dso_pin_int1_route_t val1;
cparata 3:4274d9103f1d 1446 lsm6dso_pin_int2_route_t val2;
cparata 3:4274d9103f1d 1447
cparata 3:4274d9103f1d 1448 /* Output Data Rate selection */
cparata 3:4274d9103f1d 1449 if (set_x_odr(416.0f) != 0) {
cparata 3:4274d9103f1d 1450 return 1;
cparata 3:4274d9103f1d 1451 }
cparata 3:4274d9103f1d 1452
cparata 3:4274d9103f1d 1453 /* Full scale selection */
cparata 3:4274d9103f1d 1454 if (set_x_fs(2.0f) != 0) {
cparata 3:4274d9103f1d 1455 return 1;
cparata 3:4274d9103f1d 1456 }
cparata 3:4274d9103f1d 1457
cparata 3:4274d9103f1d 1458 /* Enable X direction in tap recognition. */
cparata 3:4274d9103f1d 1459 if (lsm6dso_tap_detection_on_x_set(&_reg_ctx, PROPERTY_ENABLE) != 0) {
cparata 3:4274d9103f1d 1460 return 1;
cparata 3:4274d9103f1d 1461 }
cparata 3:4274d9103f1d 1462
cparata 3:4274d9103f1d 1463 /* Enable Y direction in tap recognition. */
cparata 3:4274d9103f1d 1464 if (lsm6dso_tap_detection_on_y_set(&_reg_ctx, PROPERTY_ENABLE) != 0) {
cparata 3:4274d9103f1d 1465 return 1;
cparata 3:4274d9103f1d 1466 }
cparata 3:4274d9103f1d 1467
cparata 3:4274d9103f1d 1468 /* Enable Z direction in tap recognition. */
cparata 3:4274d9103f1d 1469 if (lsm6dso_tap_detection_on_z_set(&_reg_ctx, PROPERTY_ENABLE) != 0) {
cparata 3:4274d9103f1d 1470 return 1;
cparata 3:4274d9103f1d 1471 }
cparata 3:4274d9103f1d 1472
cparata 3:4274d9103f1d 1473 /* Set tap threshold. */
cparata 3:4274d9103f1d 1474 if (lsm6dso_tap_threshold_x_set(&_reg_ctx, 0x08) != 0) {
cparata 3:4274d9103f1d 1475 return 1;
cparata 3:4274d9103f1d 1476 }
cparata 3:4274d9103f1d 1477
cparata 3:4274d9103f1d 1478 /* Set tap shock time window. */
cparata 3:4274d9103f1d 1479 if (lsm6dso_tap_shock_set(&_reg_ctx, 0x02) != 0) {
cparata 0:6d69e896ce38 1480 return 1;
cparata 3:4274d9103f1d 1481 }
cparata 3:4274d9103f1d 1482
cparata 3:4274d9103f1d 1483 /* Set tap quiet time window. */
cparata 3:4274d9103f1d 1484 if (lsm6dso_tap_quiet_set(&_reg_ctx, 0x01) != 0) {
cparata 0:6d69e896ce38 1485 return 1;
cparata 3:4274d9103f1d 1486 }
cparata 3:4274d9103f1d 1487
cparata 3:4274d9103f1d 1488 /* _NOTE_: Tap duration time window - don't care for single tap. */
cparata 3:4274d9103f1d 1489
cparata 3:4274d9103f1d 1490 /* _NOTE_: Single/Double Tap event - don't care of this flag for single tap. */
cparata 3:4274d9103f1d 1491
cparata 3:4274d9103f1d 1492 /* Enable single tap event on either INT1 or INT2 pin */
cparata 3:4274d9103f1d 1493 switch (int_pin) {
cparata 3:4274d9103f1d 1494 case LSM6DSO_INT1_PIN:
cparata 3:4274d9103f1d 1495 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1496 return 1;
cparata 3:4274d9103f1d 1497 }
cparata 3:4274d9103f1d 1498
cparata 3:4274d9103f1d 1499 val1.md1_cfg.int1_single_tap = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1500
cparata 3:4274d9103f1d 1501 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1502 return 1;
cparata 3:4274d9103f1d 1503 }
cparata 3:4274d9103f1d 1504 break;
cparata 3:4274d9103f1d 1505
cparata 3:4274d9103f1d 1506 case LSM6DSO_INT2_PIN:
cparata 3:4274d9103f1d 1507 if (lsm6dso_pin_int2_route_get(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1508 return 1;
cparata 3:4274d9103f1d 1509 }
cparata 3:4274d9103f1d 1510
cparata 3:4274d9103f1d 1511 val2.md2_cfg.int2_single_tap = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1512
cparata 3:4274d9103f1d 1513 if (lsm6dso_pin_int2_route_set(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1514 return 1;
cparata 3:4274d9103f1d 1515 }
cparata 3:4274d9103f1d 1516 break;
cparata 3:4274d9103f1d 1517
cparata 3:4274d9103f1d 1518 default:
cparata 3:4274d9103f1d 1519 ret = 1;
cparata 3:4274d9103f1d 1520 break;
cparata 3:4274d9103f1d 1521 }
cparata 3:4274d9103f1d 1522
cparata 3:4274d9103f1d 1523 return ret;
cparata 0:6d69e896ce38 1524 }
cparata 0:6d69e896ce38 1525
cparata 0:6d69e896ce38 1526 /**
cparata 0:6d69e896ce38 1527 * @brief Disable single tap detection
cparata 0:6d69e896ce38 1528 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1529 */
cparata 0:6d69e896ce38 1530 int LSM6DSOSensor::disable_single_tap_detection()
cparata 0:6d69e896ce38 1531 {
cparata 0:6d69e896ce38 1532 lsm6dso_pin_int1_route_t val1;
cparata 0:6d69e896ce38 1533 lsm6dso_pin_int2_route_t val2;
cparata 0:6d69e896ce38 1534
cparata 3:4274d9103f1d 1535 /* Disable single tap event on both INT1 and INT2 pins */
cparata 3:4274d9103f1d 1536 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1537 return 1;
cparata 3:4274d9103f1d 1538 }
cparata 3:4274d9103f1d 1539
cparata 3:4274d9103f1d 1540 val1.md1_cfg.int1_single_tap = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1541
cparata 3:4274d9103f1d 1542 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1543 return 1;
cparata 3:4274d9103f1d 1544 }
cparata 3:4274d9103f1d 1545
cparata 3:4274d9103f1d 1546 if (lsm6dso_pin_int2_route_get(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1547 return 1;
cparata 3:4274d9103f1d 1548 }
cparata 3:4274d9103f1d 1549
cparata 3:4274d9103f1d 1550 val2.md2_cfg.int2_single_tap = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1551
cparata 3:4274d9103f1d 1552 if (lsm6dso_pin_int2_route_set(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1553 return 1;
cparata 3:4274d9103f1d 1554 }
cparata 3:4274d9103f1d 1555
cparata 3:4274d9103f1d 1556 /* Reset tap quiet time window. */
cparata 3:4274d9103f1d 1557 if (lsm6dso_tap_quiet_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 1558 return 1;
cparata 3:4274d9103f1d 1559 }
cparata 3:4274d9103f1d 1560
cparata 3:4274d9103f1d 1561 /* Reset tap shock time window. */
cparata 3:4274d9103f1d 1562 if (lsm6dso_tap_shock_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 1563 return 1;
cparata 3:4274d9103f1d 1564 }
cparata 3:4274d9103f1d 1565
cparata 3:4274d9103f1d 1566 /* Reset tap threshold. */
cparata 3:4274d9103f1d 1567 if (lsm6dso_tap_threshold_x_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 1568 return 1;
cparata 3:4274d9103f1d 1569 }
cparata 3:4274d9103f1d 1570
cparata 3:4274d9103f1d 1571 /* Disable Z direction in tap recognition. */
cparata 3:4274d9103f1d 1572 if (lsm6dso_tap_detection_on_z_set(&_reg_ctx, PROPERTY_DISABLE) != 0) {
cparata 3:4274d9103f1d 1573 return 1;
cparata 3:4274d9103f1d 1574 }
cparata 3:4274d9103f1d 1575
cparata 3:4274d9103f1d 1576 /* Disable Y direction in tap recognition. */
cparata 3:4274d9103f1d 1577 if (lsm6dso_tap_detection_on_y_set(&_reg_ctx, PROPERTY_DISABLE) != 0) {
cparata 3:4274d9103f1d 1578 return 1;
cparata 3:4274d9103f1d 1579 }
cparata 3:4274d9103f1d 1580
cparata 3:4274d9103f1d 1581 /* Disable X direction in tap recognition. */
cparata 3:4274d9103f1d 1582 if (lsm6dso_tap_detection_on_x_set(&_reg_ctx, PROPERTY_DISABLE) != 0) {
cparata 3:4274d9103f1d 1583 return 1;
cparata 3:4274d9103f1d 1584 }
cparata 3:4274d9103f1d 1585
cparata 3:4274d9103f1d 1586 return 0;
cparata 0:6d69e896ce38 1587 }
cparata 0:6d69e896ce38 1588
cparata 0:6d69e896ce38 1589 /**
cparata 0:6d69e896ce38 1590 * @brief Enable double tap detection
cparata 0:6d69e896ce38 1591 * @param int_pin interrupt pin line to be used
cparata 0:6d69e896ce38 1592 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1593 */
cparata 0:6d69e896ce38 1594 int LSM6DSOSensor::enable_double_tap_detection(LSM6DSO_Interrupt_Pin_t int_pin)
cparata 0:6d69e896ce38 1595 {
cparata 3:4274d9103f1d 1596 int ret = 0;
cparata 3:4274d9103f1d 1597 lsm6dso_pin_int1_route_t val1;
cparata 3:4274d9103f1d 1598 lsm6dso_pin_int2_route_t val2;
cparata 3:4274d9103f1d 1599
cparata 3:4274d9103f1d 1600 /* Output Data Rate selection */
cparata 3:4274d9103f1d 1601 if (set_x_odr(416.0f) != 0) {
cparata 3:4274d9103f1d 1602 return 1;
cparata 3:4274d9103f1d 1603 }
cparata 3:4274d9103f1d 1604
cparata 3:4274d9103f1d 1605 /* Full scale selection */
cparata 3:4274d9103f1d 1606 if (set_x_fs(2.0f) != 0) {
cparata 3:4274d9103f1d 1607 return 1;
cparata 3:4274d9103f1d 1608 }
cparata 3:4274d9103f1d 1609
cparata 3:4274d9103f1d 1610 /* Enable X direction in tap recognition. */
cparata 3:4274d9103f1d 1611 if (lsm6dso_tap_detection_on_x_set(&_reg_ctx, PROPERTY_ENABLE) != 0) {
cparata 3:4274d9103f1d 1612 return 1;
cparata 3:4274d9103f1d 1613 }
cparata 3:4274d9103f1d 1614
cparata 3:4274d9103f1d 1615 /* Enable Y direction in tap recognition. */
cparata 3:4274d9103f1d 1616 if (lsm6dso_tap_detection_on_y_set(&_reg_ctx, PROPERTY_ENABLE) != 0) {
cparata 3:4274d9103f1d 1617 return 1;
cparata 3:4274d9103f1d 1618 }
cparata 3:4274d9103f1d 1619
cparata 3:4274d9103f1d 1620 /* Enable Z direction in tap recognition. */
cparata 3:4274d9103f1d 1621 if (lsm6dso_tap_detection_on_z_set(&_reg_ctx, PROPERTY_ENABLE) != 0) {
cparata 3:4274d9103f1d 1622 return 1;
cparata 3:4274d9103f1d 1623 }
cparata 3:4274d9103f1d 1624
cparata 3:4274d9103f1d 1625 /* Set tap threshold. */
cparata 3:4274d9103f1d 1626 if (lsm6dso_tap_threshold_x_set(&_reg_ctx, 0x08) != 0) {
cparata 3:4274d9103f1d 1627 return 1;
cparata 3:4274d9103f1d 1628 }
cparata 3:4274d9103f1d 1629
cparata 3:4274d9103f1d 1630 /* Set tap shock time window. */
cparata 3:4274d9103f1d 1631 if (lsm6dso_tap_shock_set(&_reg_ctx, 0x03) != 0) {
cparata 3:4274d9103f1d 1632 return 1;
cparata 3:4274d9103f1d 1633 }
cparata 3:4274d9103f1d 1634
cparata 3:4274d9103f1d 1635 /* Set tap quiet time window. */
cparata 3:4274d9103f1d 1636 if (lsm6dso_tap_quiet_set(&_reg_ctx, 0x03) != 0) {
cparata 0:6d69e896ce38 1637 return 1;
cparata 3:4274d9103f1d 1638 }
cparata 3:4274d9103f1d 1639
cparata 3:4274d9103f1d 1640 /* Set tap duration time window. */
cparata 3:4274d9103f1d 1641 if (lsm6dso_tap_dur_set(&_reg_ctx, 0x08) != 0) {
cparata 0:6d69e896ce38 1642 return 1;
cparata 3:4274d9103f1d 1643 }
cparata 3:4274d9103f1d 1644
cparata 3:4274d9103f1d 1645 /* Single and double tap enabled. */
cparata 3:4274d9103f1d 1646 if (lsm6dso_tap_mode_set(&_reg_ctx, LSM6DSO_BOTH_SINGLE_DOUBLE) != 0) {
cparata 0:6d69e896ce38 1647 return 1;
cparata 3:4274d9103f1d 1648 }
cparata 3:4274d9103f1d 1649
cparata 3:4274d9103f1d 1650 /* Enable double tap event on either INT1 or INT2 pin */
cparata 3:4274d9103f1d 1651 switch (int_pin) {
cparata 3:4274d9103f1d 1652 case LSM6DSO_INT1_PIN:
cparata 3:4274d9103f1d 1653 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1654 return 1;
cparata 3:4274d9103f1d 1655 }
cparata 3:4274d9103f1d 1656
cparata 3:4274d9103f1d 1657 val1.md1_cfg.int1_double_tap = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1658
cparata 3:4274d9103f1d 1659 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1660 return 1;
cparata 3:4274d9103f1d 1661 }
cparata 3:4274d9103f1d 1662 break;
cparata 3:4274d9103f1d 1663
cparata 3:4274d9103f1d 1664 case LSM6DSO_INT2_PIN:
cparata 3:4274d9103f1d 1665 if (lsm6dso_pin_int2_route_get(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1666 return 1;
cparata 3:4274d9103f1d 1667 }
cparata 3:4274d9103f1d 1668
cparata 3:4274d9103f1d 1669 val2.md2_cfg.int2_double_tap = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1670
cparata 3:4274d9103f1d 1671 if (lsm6dso_pin_int2_route_set(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1672 return 1;
cparata 3:4274d9103f1d 1673 }
cparata 3:4274d9103f1d 1674 break;
cparata 3:4274d9103f1d 1675
cparata 3:4274d9103f1d 1676 default:
cparata 3:4274d9103f1d 1677 ret = 1;
cparata 3:4274d9103f1d 1678 break;
cparata 3:4274d9103f1d 1679 }
cparata 3:4274d9103f1d 1680
cparata 3:4274d9103f1d 1681 return ret;
cparata 0:6d69e896ce38 1682 }
cparata 0:6d69e896ce38 1683
cparata 0:6d69e896ce38 1684 /**
cparata 0:6d69e896ce38 1685 * @brief Disable double tap detection
cparata 0:6d69e896ce38 1686 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1687 */
cparata 0:6d69e896ce38 1688 int LSM6DSOSensor::disable_double_tap_detection()
cparata 0:6d69e896ce38 1689 {
cparata 3:4274d9103f1d 1690 lsm6dso_pin_int1_route_t val1;
cparata 3:4274d9103f1d 1691 lsm6dso_pin_int2_route_t val2;
cparata 3:4274d9103f1d 1692
cparata 3:4274d9103f1d 1693 /* Disable double tap event on both INT1 and INT2 pins */
cparata 3:4274d9103f1d 1694 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1695 return 1;
cparata 3:4274d9103f1d 1696 }
cparata 3:4274d9103f1d 1697
cparata 3:4274d9103f1d 1698 val1.md1_cfg.int1_double_tap = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1699
cparata 3:4274d9103f1d 1700 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1701 return 1;
cparata 3:4274d9103f1d 1702 }
cparata 3:4274d9103f1d 1703
cparata 3:4274d9103f1d 1704 if (lsm6dso_pin_int2_route_get(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1705 return 1;
cparata 3:4274d9103f1d 1706 }
cparata 3:4274d9103f1d 1707
cparata 3:4274d9103f1d 1708 val2.md2_cfg.int2_double_tap = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1709
cparata 3:4274d9103f1d 1710 if (lsm6dso_pin_int2_route_set(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1711 return 1;
cparata 3:4274d9103f1d 1712 }
cparata 3:4274d9103f1d 1713
cparata 3:4274d9103f1d 1714 /* Only single tap enabled. */
cparata 3:4274d9103f1d 1715 if (lsm6dso_tap_mode_set(&_reg_ctx, LSM6DSO_ONLY_SINGLE) != 0) {
cparata 3:4274d9103f1d 1716 return 1;
cparata 3:4274d9103f1d 1717 }
cparata 3:4274d9103f1d 1718
cparata 3:4274d9103f1d 1719 /* Reset tap duration time window. */
cparata 3:4274d9103f1d 1720 if (lsm6dso_tap_dur_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 1721 return 1;
cparata 3:4274d9103f1d 1722 }
cparata 3:4274d9103f1d 1723
cparata 3:4274d9103f1d 1724 /* Reset tap quiet time window. */
cparata 3:4274d9103f1d 1725 if (lsm6dso_tap_quiet_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 1726 return 1;
cparata 3:4274d9103f1d 1727 }
cparata 3:4274d9103f1d 1728
cparata 3:4274d9103f1d 1729 /* Reset tap shock time window. */
cparata 3:4274d9103f1d 1730 if (lsm6dso_tap_shock_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 1731 return 1;
cparata 3:4274d9103f1d 1732 }
cparata 3:4274d9103f1d 1733
cparata 3:4274d9103f1d 1734 /* Reset tap threshold. */
cparata 3:4274d9103f1d 1735 if (lsm6dso_tap_threshold_x_set(&_reg_ctx, 0x00) != 0) {
cparata 3:4274d9103f1d 1736 return 1;
cparata 3:4274d9103f1d 1737 }
cparata 3:4274d9103f1d 1738
cparata 3:4274d9103f1d 1739 /* Disable Z direction in tap recognition. */
cparata 3:4274d9103f1d 1740 if (lsm6dso_tap_detection_on_z_set(&_reg_ctx, PROPERTY_DISABLE) != 0) {
cparata 3:4274d9103f1d 1741 return 1;
cparata 3:4274d9103f1d 1742 }
cparata 3:4274d9103f1d 1743
cparata 3:4274d9103f1d 1744 /* Disable Y direction in tap recognition. */
cparata 3:4274d9103f1d 1745 if (lsm6dso_tap_detection_on_y_set(&_reg_ctx, PROPERTY_DISABLE) != 0) {
cparata 3:4274d9103f1d 1746 return 1;
cparata 3:4274d9103f1d 1747 }
cparata 3:4274d9103f1d 1748
cparata 3:4274d9103f1d 1749 /* Disable X direction in tap recognition. */
cparata 3:4274d9103f1d 1750 if (lsm6dso_tap_detection_on_x_set(&_reg_ctx, PROPERTY_DISABLE) != 0) {
cparata 3:4274d9103f1d 1751 return 1;
cparata 3:4274d9103f1d 1752 }
cparata 3:4274d9103f1d 1753
cparata 3:4274d9103f1d 1754 return 0;
cparata 0:6d69e896ce38 1755 }
cparata 0:6d69e896ce38 1756
cparata 0:6d69e896ce38 1757 /**
cparata 0:6d69e896ce38 1758 * @brief Set tap threshold
cparata 0:6d69e896ce38 1759 * @param thr tap threshold
cparata 0:6d69e896ce38 1760 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1761 */
cparata 0:6d69e896ce38 1762 int LSM6DSOSensor::set_tap_threshold(uint8_t thr)
cparata 0:6d69e896ce38 1763 {
cparata 3:4274d9103f1d 1764 /* Set tap threshold. */
cparata 3:4274d9103f1d 1765 if (lsm6dso_tap_threshold_x_set(&_reg_ctx, thr) != 0) {
cparata 3:4274d9103f1d 1766 return 1;
cparata 3:4274d9103f1d 1767 }
cparata 3:4274d9103f1d 1768
cparata 3:4274d9103f1d 1769 return 0;
cparata 0:6d69e896ce38 1770 }
cparata 0:6d69e896ce38 1771
cparata 0:6d69e896ce38 1772 /**
cparata 0:6d69e896ce38 1773 * @brief Set tap shock time
cparata 0:6d69e896ce38 1774 * @param time tap shock time
cparata 0:6d69e896ce38 1775 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1776 */
cparata 0:6d69e896ce38 1777 int LSM6DSOSensor::set_tap_shock_time(uint8_t time)
cparata 0:6d69e896ce38 1778 {
cparata 3:4274d9103f1d 1779 /* Set tap shock time window. */
cparata 3:4274d9103f1d 1780 if (lsm6dso_tap_shock_set(&_reg_ctx, time) != 0) {
cparata 3:4274d9103f1d 1781 return 1;
cparata 3:4274d9103f1d 1782 }
cparata 3:4274d9103f1d 1783
cparata 3:4274d9103f1d 1784 return 0;
cparata 0:6d69e896ce38 1785 }
cparata 0:6d69e896ce38 1786
cparata 0:6d69e896ce38 1787 /**
cparata 0:6d69e896ce38 1788 * @brief Set tap quiet time
cparata 0:6d69e896ce38 1789 * @param time tap quiet time
cparata 0:6d69e896ce38 1790 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1791 */
cparata 0:6d69e896ce38 1792 int LSM6DSOSensor::set_tap_quiet_time(uint8_t time)
cparata 0:6d69e896ce38 1793 {
cparata 3:4274d9103f1d 1794 /* Set tap quiet time window. */
cparata 3:4274d9103f1d 1795 if (lsm6dso_tap_quiet_set(&_reg_ctx, time) != 0) {
cparata 3:4274d9103f1d 1796 return 1;
cparata 3:4274d9103f1d 1797 }
cparata 3:4274d9103f1d 1798
cparata 3:4274d9103f1d 1799 return 0;
cparata 0:6d69e896ce38 1800 }
cparata 0:6d69e896ce38 1801
cparata 0:6d69e896ce38 1802 /**
cparata 0:6d69e896ce38 1803 * @brief Set tap duration time
cparata 0:6d69e896ce38 1804 * @param time tap duration time
cparata 0:6d69e896ce38 1805 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1806 */
cparata 0:6d69e896ce38 1807 int LSM6DSOSensor::set_tap_duration_time(uint8_t time)
cparata 0:6d69e896ce38 1808 {
cparata 3:4274d9103f1d 1809 /* Set tap duration time window. */
cparata 3:4274d9103f1d 1810 if (lsm6dso_tap_dur_set(&_reg_ctx, time) != 0) {
cparata 3:4274d9103f1d 1811 return 1;
cparata 3:4274d9103f1d 1812 }
cparata 3:4274d9103f1d 1813
cparata 3:4274d9103f1d 1814 return 0;
cparata 0:6d69e896ce38 1815 }
cparata 0:6d69e896ce38 1816
cparata 0:6d69e896ce38 1817 /**
cparata 0:6d69e896ce38 1818 * @brief Enable 6D orientation detection
cparata 0:6d69e896ce38 1819 * @param int_pin interrupt pin line to be used
cparata 0:6d69e896ce38 1820 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1821 */
cparata 0:6d69e896ce38 1822 int LSM6DSOSensor::enable_6d_orientation(LSM6DSO_Interrupt_Pin_t int_pin)
cparata 0:6d69e896ce38 1823 {
cparata 3:4274d9103f1d 1824 int ret = 0;
cparata 3:4274d9103f1d 1825 lsm6dso_pin_int1_route_t val1;
cparata 3:4274d9103f1d 1826 lsm6dso_pin_int2_route_t val2;
cparata 3:4274d9103f1d 1827
cparata 3:4274d9103f1d 1828 /* Output Data Rate selection */
cparata 3:4274d9103f1d 1829 if (set_x_odr(416.0f) != 0) {
cparata 3:4274d9103f1d 1830 return 1;
cparata 3:4274d9103f1d 1831 }
cparata 3:4274d9103f1d 1832
cparata 3:4274d9103f1d 1833 /* Full scale selection */
cparata 3:4274d9103f1d 1834 if (set_x_fs(2.0f) != 0) {
cparata 3:4274d9103f1d 1835 return 1;
cparata 3:4274d9103f1d 1836 }
cparata 3:4274d9103f1d 1837
cparata 3:4274d9103f1d 1838 /* 6D orientation enabled. */
cparata 3:4274d9103f1d 1839 if (lsm6dso_6d_threshold_set(&_reg_ctx, LSM6DSO_DEG_60) != 0) {
cparata 0:6d69e896ce38 1840 return 1;
cparata 3:4274d9103f1d 1841 }
cparata 3:4274d9103f1d 1842
cparata 3:4274d9103f1d 1843 /* Enable 6D orientation event on either INT1 or INT2 pin */
cparata 3:4274d9103f1d 1844 switch (int_pin) {
cparata 3:4274d9103f1d 1845 case LSM6DSO_INT1_PIN:
cparata 3:4274d9103f1d 1846 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1847 return 1;
cparata 3:4274d9103f1d 1848 }
cparata 3:4274d9103f1d 1849
cparata 3:4274d9103f1d 1850 val1.md1_cfg.int1_6d = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1851
cparata 3:4274d9103f1d 1852 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1853 return 1;
cparata 3:4274d9103f1d 1854 }
cparata 3:4274d9103f1d 1855 break;
cparata 3:4274d9103f1d 1856
cparata 3:4274d9103f1d 1857 case LSM6DSO_INT2_PIN:
cparata 3:4274d9103f1d 1858 if (lsm6dso_pin_int2_route_get(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1859 return 1;
cparata 3:4274d9103f1d 1860 }
cparata 3:4274d9103f1d 1861
cparata 3:4274d9103f1d 1862 val2.md2_cfg.int2_6d = PROPERTY_ENABLE;
cparata 3:4274d9103f1d 1863
cparata 3:4274d9103f1d 1864 if (lsm6dso_pin_int2_route_set(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1865 return 1;
cparata 3:4274d9103f1d 1866 }
cparata 3:4274d9103f1d 1867 break;
cparata 3:4274d9103f1d 1868
cparata 3:4274d9103f1d 1869 default:
cparata 3:4274d9103f1d 1870 ret = 1;
cparata 3:4274d9103f1d 1871 break;
cparata 3:4274d9103f1d 1872 }
cparata 3:4274d9103f1d 1873
cparata 3:4274d9103f1d 1874 return ret;
cparata 0:6d69e896ce38 1875 }
cparata 0:6d69e896ce38 1876
cparata 0:6d69e896ce38 1877 /**
cparata 0:6d69e896ce38 1878 * @brief Disable 6D orientation detection
cparata 0:6d69e896ce38 1879 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1880 */
cparata 0:6d69e896ce38 1881 int LSM6DSOSensor::disable_6d_orientation()
cparata 0:6d69e896ce38 1882 {
cparata 3:4274d9103f1d 1883 lsm6dso_pin_int1_route_t val1;
cparata 3:4274d9103f1d 1884 lsm6dso_pin_int2_route_t val2;
cparata 3:4274d9103f1d 1885
cparata 3:4274d9103f1d 1886 /* Disable 6D orientation event on both INT1 and INT2 pins */
cparata 3:4274d9103f1d 1887 if (lsm6dso_pin_int1_route_get(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1888 return 1;
cparata 3:4274d9103f1d 1889 }
cparata 3:4274d9103f1d 1890
cparata 3:4274d9103f1d 1891 val1.md1_cfg.int1_6d = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1892
cparata 3:4274d9103f1d 1893 if (lsm6dso_pin_int1_route_set(&_reg_ctx, &val1) != 0) {
cparata 3:4274d9103f1d 1894 return 1;
cparata 3:4274d9103f1d 1895 }
cparata 3:4274d9103f1d 1896
cparata 3:4274d9103f1d 1897 if (lsm6dso_pin_int2_route_get(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1898 return 1;
cparata 3:4274d9103f1d 1899 }
cparata 3:4274d9103f1d 1900
cparata 3:4274d9103f1d 1901 val2.md2_cfg.int2_6d = PROPERTY_DISABLE;
cparata 3:4274d9103f1d 1902
cparata 3:4274d9103f1d 1903 if (lsm6dso_pin_int2_route_set(&_reg_ctx, &val2) != 0) {
cparata 3:4274d9103f1d 1904 return 1;
cparata 3:4274d9103f1d 1905 }
cparata 3:4274d9103f1d 1906
cparata 3:4274d9103f1d 1907 /* Reset 6D orientation. */
cparata 3:4274d9103f1d 1908 if (lsm6dso_6d_threshold_set(&_reg_ctx, LSM6DSO_DEG_80) != 0) {
cparata 3:4274d9103f1d 1909 return 1;
cparata 3:4274d9103f1d 1910 }
cparata 3:4274d9103f1d 1911
cparata 3:4274d9103f1d 1912 return 0;
cparata 0:6d69e896ce38 1913 }
cparata 0:6d69e896ce38 1914
cparata 0:6d69e896ce38 1915 /**
cparata 0:6d69e896ce38 1916 * @brief Set 6D orientation threshold
cparata 0:6d69e896ce38 1917 * @param thr 6D Orientation detection threshold
cparata 0:6d69e896ce38 1918 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1919 */
cparata 0:6d69e896ce38 1920 int LSM6DSOSensor::set_6d_orientation_threshold(uint8_t thr)
cparata 0:6d69e896ce38 1921 {
cparata 3:4274d9103f1d 1922 if (lsm6dso_6d_threshold_set(&_reg_ctx, (lsm6dso_sixd_ths_t)thr) != 0) {
cparata 3:4274d9103f1d 1923 return 1;
cparata 3:4274d9103f1d 1924 }
cparata 3:4274d9103f1d 1925
cparata 3:4274d9103f1d 1926 return 0;
cparata 0:6d69e896ce38 1927 }
cparata 0:6d69e896ce38 1928
cparata 0:6d69e896ce38 1929 /**
cparata 0:6d69e896ce38 1930 * @brief Get the status of XLow orientation
cparata 0:6d69e896ce38 1931 * @param xl the status of XLow orientation
cparata 0:6d69e896ce38 1932 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1933 */
cparata 0:6d69e896ce38 1934 int LSM6DSOSensor::get_6d_orientation_xl(uint8_t *xl)
cparata 0:6d69e896ce38 1935 {
cparata 3:4274d9103f1d 1936 lsm6dso_d6d_src_t data;
cparata 3:4274d9103f1d 1937
cparata 3:4274d9103f1d 1938 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != 0) {
cparata 3:4274d9103f1d 1939 return 1;
cparata 3:4274d9103f1d 1940 }
cparata 3:4274d9103f1d 1941
cparata 3:4274d9103f1d 1942 *xl = data.xl;
cparata 3:4274d9103f1d 1943
cparata 3:4274d9103f1d 1944 return 0;
cparata 0:6d69e896ce38 1945 }
cparata 0:6d69e896ce38 1946
cparata 0:6d69e896ce38 1947 /**
cparata 0:6d69e896ce38 1948 * @brief Get the status of XHigh orientation
cparata 0:6d69e896ce38 1949 * @param xh the status of XHigh orientation
cparata 0:6d69e896ce38 1950 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1951 */
cparata 0:6d69e896ce38 1952 int LSM6DSOSensor::get_6d_orientation_xh(uint8_t *xh)
cparata 0:6d69e896ce38 1953 {
cparata 3:4274d9103f1d 1954 lsm6dso_d6d_src_t data;
cparata 3:4274d9103f1d 1955
cparata 3:4274d9103f1d 1956 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != 0) {
cparata 3:4274d9103f1d 1957 return 1;
cparata 3:4274d9103f1d 1958 }
cparata 3:4274d9103f1d 1959
cparata 3:4274d9103f1d 1960 *xh = data.xh;
cparata 3:4274d9103f1d 1961
cparata 3:4274d9103f1d 1962 return 0;
cparata 0:6d69e896ce38 1963 }
cparata 0:6d69e896ce38 1964
cparata 0:6d69e896ce38 1965 /**
cparata 0:6d69e896ce38 1966 * @brief Get the status of YLow orientation
cparata 0:6d69e896ce38 1967 * @param yl the status of YLow orientation
cparata 0:6d69e896ce38 1968 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1969 */
cparata 0:6d69e896ce38 1970 int LSM6DSOSensor::get_6d_orientation_yl(uint8_t *yl)
cparata 0:6d69e896ce38 1971 {
cparata 3:4274d9103f1d 1972 lsm6dso_d6d_src_t data;
cparata 3:4274d9103f1d 1973
cparata 3:4274d9103f1d 1974 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != 0) {
cparata 3:4274d9103f1d 1975 return 1;
cparata 3:4274d9103f1d 1976 }
cparata 3:4274d9103f1d 1977
cparata 3:4274d9103f1d 1978 *yl = data.yl;
cparata 3:4274d9103f1d 1979
cparata 3:4274d9103f1d 1980 return 0;
cparata 0:6d69e896ce38 1981 }
cparata 0:6d69e896ce38 1982
cparata 0:6d69e896ce38 1983 /**
cparata 0:6d69e896ce38 1984 * @brief Get the status of YHigh orientation
cparata 0:6d69e896ce38 1985 * @param yh the status of YHigh orientation
cparata 0:6d69e896ce38 1986 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 1987 */
cparata 0:6d69e896ce38 1988 int LSM6DSOSensor::get_6d_orientation_yh(uint8_t *yh)
cparata 0:6d69e896ce38 1989 {
cparata 3:4274d9103f1d 1990 lsm6dso_d6d_src_t data;
cparata 3:4274d9103f1d 1991
cparata 3:4274d9103f1d 1992 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != 0) {
cparata 3:4274d9103f1d 1993 return 1;
cparata 3:4274d9103f1d 1994 }
cparata 3:4274d9103f1d 1995
cparata 3:4274d9103f1d 1996 *yh = data.yh;
cparata 3:4274d9103f1d 1997
cparata 3:4274d9103f1d 1998 return 0;
cparata 0:6d69e896ce38 1999 }
cparata 0:6d69e896ce38 2000
cparata 0:6d69e896ce38 2001 /**
cparata 0:6d69e896ce38 2002 * @brief Get the status of ZLow orientation
cparata 0:6d69e896ce38 2003 * @param zl the status of ZLow orientation
cparata 0:6d69e896ce38 2004 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2005 */
cparata 0:6d69e896ce38 2006 int LSM6DSOSensor::get_6d_orientation_zl(uint8_t *zl)
cparata 0:6d69e896ce38 2007 {
cparata 3:4274d9103f1d 2008 lsm6dso_d6d_src_t data;
cparata 3:4274d9103f1d 2009
cparata 3:4274d9103f1d 2010 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != 0) {
cparata 3:4274d9103f1d 2011 return 1;
cparata 3:4274d9103f1d 2012 }
cparata 3:4274d9103f1d 2013
cparata 3:4274d9103f1d 2014 *zl = data.zl;
cparata 3:4274d9103f1d 2015
cparata 3:4274d9103f1d 2016 return 0;
cparata 0:6d69e896ce38 2017 }
cparata 0:6d69e896ce38 2018
cparata 0:6d69e896ce38 2019 /**
cparata 0:6d69e896ce38 2020 * @brief Get the status of ZHigh orientation
cparata 0:6d69e896ce38 2021 * @param zh the status of ZHigh orientation
cparata 0:6d69e896ce38 2022 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2023 */
cparata 0:6d69e896ce38 2024 int LSM6DSOSensor::get_6d_orientation_zh(uint8_t *zh)
cparata 0:6d69e896ce38 2025 {
cparata 3:4274d9103f1d 2026 lsm6dso_d6d_src_t data;
cparata 3:4274d9103f1d 2027
cparata 3:4274d9103f1d 2028 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != 0) {
cparata 3:4274d9103f1d 2029 return 1;
cparata 3:4274d9103f1d 2030 }
cparata 3:4274d9103f1d 2031
cparata 3:4274d9103f1d 2032 *zh = data.zh;
cparata 3:4274d9103f1d 2033
cparata 3:4274d9103f1d 2034 return 0;
cparata 0:6d69e896ce38 2035 }
cparata 0:6d69e896ce38 2036
cparata 0:6d69e896ce38 2037 /**
cparata 0:6d69e896ce38 2038 * @brief Get the LSM6DSO ACC data ready bit value
cparata 0:6d69e896ce38 2039 * @param status the status of data ready bit
cparata 0:6d69e896ce38 2040 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2041 */
cparata 0:6d69e896ce38 2042 int LSM6DSOSensor::get_x_drdy_status(uint8_t *status)
cparata 0:6d69e896ce38 2043 {
cparata 3:4274d9103f1d 2044 if (lsm6dso_xl_flag_data_ready_get(&_reg_ctx, status) != 0) {
cparata 3:4274d9103f1d 2045 return 1;
cparata 3:4274d9103f1d 2046 }
cparata 3:4274d9103f1d 2047
cparata 3:4274d9103f1d 2048 return 0;
cparata 0:6d69e896ce38 2049 }
cparata 0:6d69e896ce38 2050
cparata 0:6d69e896ce38 2051 /**
cparata 0:6d69e896ce38 2052 * @brief Get the status of all hardware events
cparata 0:6d69e896ce38 2053 * @param status the status of all hardware events
cparata 0:6d69e896ce38 2054 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2055 */
cparata 0:6d69e896ce38 2056 int LSM6DSOSensor::get_event_status(LSM6DSO_Event_Status_t *status)
cparata 0:6d69e896ce38 2057 {
cparata 3:4274d9103f1d 2058 uint8_t tilt_ia = 0U;
cparata 3:4274d9103f1d 2059 lsm6dso_wake_up_src_t wake_up_src;
cparata 3:4274d9103f1d 2060 lsm6dso_tap_src_t tap_src;
cparata 3:4274d9103f1d 2061 lsm6dso_d6d_src_t d6d_src;
cparata 3:4274d9103f1d 2062 lsm6dso_emb_func_src_t func_src;
cparata 3:4274d9103f1d 2063 lsm6dso_md1_cfg_t md1_cfg;
cparata 3:4274d9103f1d 2064 lsm6dso_md2_cfg_t md2_cfg;
cparata 3:4274d9103f1d 2065 lsm6dso_emb_func_int1_t int1_ctrl;
cparata 3:4274d9103f1d 2066 lsm6dso_emb_func_int2_t int2_ctrl;
cparata 3:4274d9103f1d 2067
cparata 3:4274d9103f1d 2068 (void)memset((void *)status, 0x0, sizeof(LSM6DSO_Event_Status_t));
cparata 3:4274d9103f1d 2069
cparata 3:4274d9103f1d 2070 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_WAKE_UP_SRC, (uint8_t *)&wake_up_src, 1) != 0) {
cparata 3:4274d9103f1d 2071 return 1;
cparata 3:4274d9103f1d 2072 }
cparata 3:4274d9103f1d 2073
cparata 3:4274d9103f1d 2074 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_TAP_SRC, (uint8_t *)&tap_src, 1) != 0) {
cparata 3:4274d9103f1d 2075 return 1;
cparata 3:4274d9103f1d 2076 }
cparata 3:4274d9103f1d 2077
cparata 3:4274d9103f1d 2078 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_D6D_SRC, (uint8_t *)&d6d_src, 1) != 0) {
cparata 3:4274d9103f1d 2079 return 1;
cparata 3:4274d9103f1d 2080 }
cparata 3:4274d9103f1d 2081
cparata 3:4274d9103f1d 2082 if (lsm6dso_mem_bank_set(&_reg_ctx, LSM6DSO_EMBEDDED_FUNC_BANK) != 0) {
cparata 3:4274d9103f1d 2083 return 1;
cparata 3:4274d9103f1d 2084 }
cparata 3:4274d9103f1d 2085
cparata 3:4274d9103f1d 2086 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_EMB_FUNC_SRC, (uint8_t *)&func_src, 1) != 0) {
cparata 3:4274d9103f1d 2087 return 1;
cparata 3:4274d9103f1d 2088 }
cparata 3:4274d9103f1d 2089
cparata 3:4274d9103f1d 2090 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_EMB_FUNC_INT1, (uint8_t *)&int1_ctrl, 1) != 0) {
cparata 3:4274d9103f1d 2091 return 1;
cparata 3:4274d9103f1d 2092 }
cparata 3:4274d9103f1d 2093
cparata 3:4274d9103f1d 2094 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_EMB_FUNC_INT2, (uint8_t *)&int2_ctrl, 1) != 0) {
cparata 3:4274d9103f1d 2095 return 1;
cparata 3:4274d9103f1d 2096 }
cparata 3:4274d9103f1d 2097
cparata 3:4274d9103f1d 2098 if (lsm6dso_mem_bank_set(&_reg_ctx, LSM6DSO_USER_BANK) != 0) {
cparata 3:4274d9103f1d 2099 return 1;
cparata 3:4274d9103f1d 2100 }
cparata 3:4274d9103f1d 2101
cparata 3:4274d9103f1d 2102 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_MD1_CFG, (uint8_t *)&md1_cfg, 1) != 0) {
cparata 3:4274d9103f1d 2103 return 1;
cparata 0:6d69e896ce38 2104 }
cparata 3:4274d9103f1d 2105
cparata 3:4274d9103f1d 2106 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_MD2_CFG, (uint8_t *)&md2_cfg, 1) != 0) {
cparata 3:4274d9103f1d 2107 return 1;
cparata 3:4274d9103f1d 2108 }
cparata 3:4274d9103f1d 2109
cparata 3:4274d9103f1d 2110 if (lsm6dso_tilt_flag_data_ready_get(&_reg_ctx, &tilt_ia) != 0) {
cparata 3:4274d9103f1d 2111 return 1;
cparata 0:6d69e896ce38 2112 }
cparata 3:4274d9103f1d 2113
cparata 3:4274d9103f1d 2114 if ((md1_cfg.int1_ff == 1U) || (md2_cfg.int2_ff == 1U)) {
cparata 3:4274d9103f1d 2115 if (wake_up_src.ff_ia == 1U) {
cparata 3:4274d9103f1d 2116 status->FreeFallStatus = 1;
cparata 3:4274d9103f1d 2117 }
cparata 0:6d69e896ce38 2118 }
cparata 3:4274d9103f1d 2119
cparata 3:4274d9103f1d 2120 if ((md1_cfg.int1_wu == 1U) || (md2_cfg.int2_wu == 1U)) {
cparata 3:4274d9103f1d 2121 if (wake_up_src.wu_ia == 1U) {
cparata 3:4274d9103f1d 2122 status->WakeUpStatus = 1;
cparata 3:4274d9103f1d 2123 }
cparata 3:4274d9103f1d 2124 }
cparata 3:4274d9103f1d 2125
cparata 3:4274d9103f1d 2126 if ((md1_cfg.int1_single_tap == 1U) || (md2_cfg.int2_single_tap == 1U)) {
cparata 3:4274d9103f1d 2127 if (tap_src.single_tap == 1U) {
cparata 3:4274d9103f1d 2128 status->TapStatus = 1;
cparata 3:4274d9103f1d 2129 }
cparata 0:6d69e896ce38 2130 }
cparata 3:4274d9103f1d 2131
cparata 3:4274d9103f1d 2132 if ((md1_cfg.int1_double_tap == 1U) || (md2_cfg.int2_double_tap == 1U)) {
cparata 3:4274d9103f1d 2133 if (tap_src.double_tap == 1U) {
cparata 3:4274d9103f1d 2134 status->DoubleTapStatus = 1;
cparata 3:4274d9103f1d 2135 }
cparata 3:4274d9103f1d 2136 }
cparata 3:4274d9103f1d 2137
cparata 3:4274d9103f1d 2138 if ((md1_cfg.int1_6d == 1U) || (md2_cfg.int2_6d == 1U)) {
cparata 3:4274d9103f1d 2139 if (d6d_src.d6d_ia == 1U) {
cparata 3:4274d9103f1d 2140 status->D6DOrientationStatus = 1;
cparata 3:4274d9103f1d 2141 }
cparata 0:6d69e896ce38 2142 }
cparata 3:4274d9103f1d 2143
cparata 3:4274d9103f1d 2144 if (int1_ctrl.int1_step_detector == 1U) {
cparata 3:4274d9103f1d 2145 if (func_src.step_detected == 1U) {
cparata 3:4274d9103f1d 2146 status->StepStatus = 1;
cparata 3:4274d9103f1d 2147 }
cparata 0:6d69e896ce38 2148 }
cparata 3:4274d9103f1d 2149
cparata 3:4274d9103f1d 2150 if ((int1_ctrl.int1_tilt == 1U) || (int2_ctrl.int2_tilt == 1U)) {
cparata 3:4274d9103f1d 2151 if (tilt_ia == 1U) {
cparata 3:4274d9103f1d 2152 status->TiltStatus = 1;
cparata 3:4274d9103f1d 2153 }
cparata 0:6d69e896ce38 2154 }
cparata 3:4274d9103f1d 2155
cparata 3:4274d9103f1d 2156 return 0;
cparata 0:6d69e896ce38 2157 }
cparata 0:6d69e896ce38 2158
cparata 0:6d69e896ce38 2159 /**
cparata 0:6d69e896ce38 2160 * @brief Set self test
cparata 0:6d69e896ce38 2161 * @param val the value of st_xl in reg CTRL5_C
cparata 0:6d69e896ce38 2162 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2163 */
cparata 0:6d69e896ce38 2164 int LSM6DSOSensor::set_x_self_test(uint8_t val)
cparata 0:6d69e896ce38 2165 {
cparata 3:4274d9103f1d 2166 lsm6dso_st_xl_t reg;
cparata 3:4274d9103f1d 2167
cparata 3:4274d9103f1d 2168 reg = (val == 0U) ? LSM6DSO_XL_ST_DISABLE
cparata 3:4274d9103f1d 2169 : (val == 1U) ? LSM6DSO_XL_ST_POSITIVE
cparata 3:4274d9103f1d 2170 : (val == 2U) ? LSM6DSO_XL_ST_NEGATIVE
cparata 3:4274d9103f1d 2171 : LSM6DSO_XL_ST_DISABLE;
cparata 3:4274d9103f1d 2172
cparata 3:4274d9103f1d 2173 if (lsm6dso_xl_self_test_set(&_reg_ctx, reg) != 0) {
cparata 3:4274d9103f1d 2174 return 1;
cparata 3:4274d9103f1d 2175 }
cparata 3:4274d9103f1d 2176
cparata 3:4274d9103f1d 2177 return 0;
cparata 0:6d69e896ce38 2178 }
cparata 0:6d69e896ce38 2179
cparata 0:6d69e896ce38 2180 /**
cparata 0:6d69e896ce38 2181 * @brief Get the LSM6DSO GYRO data ready bit value
cparata 0:6d69e896ce38 2182 * @param status the status of data ready bit
cparata 0:6d69e896ce38 2183 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2184 */
cparata 0:6d69e896ce38 2185 int LSM6DSOSensor::get_g_drdy_status(uint8_t *status)
cparata 0:6d69e896ce38 2186 {
cparata 3:4274d9103f1d 2187 if (lsm6dso_gy_flag_data_ready_get(&_reg_ctx, status) != 0) {
cparata 3:4274d9103f1d 2188 return 1;
cparata 3:4274d9103f1d 2189 }
cparata 3:4274d9103f1d 2190
cparata 3:4274d9103f1d 2191 return 0;
cparata 0:6d69e896ce38 2192 }
cparata 0:6d69e896ce38 2193
cparata 0:6d69e896ce38 2194 /**
cparata 0:6d69e896ce38 2195 * @brief Set self test
cparata 0:6d69e896ce38 2196 * @param val the value of st_xl in reg CTRL5_C
cparata 0:6d69e896ce38 2197 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2198 */
cparata 0:6d69e896ce38 2199 int LSM6DSOSensor::set_g_self_test(uint8_t val)
cparata 0:6d69e896ce38 2200 {
cparata 3:4274d9103f1d 2201 lsm6dso_st_g_t reg;
cparata 3:4274d9103f1d 2202
cparata 3:4274d9103f1d 2203 reg = (val == 0U) ? LSM6DSO_GY_ST_DISABLE
cparata 3:4274d9103f1d 2204 : (val == 1U) ? LSM6DSO_GY_ST_POSITIVE
cparata 3:4274d9103f1d 2205 : (val == 2U) ? LSM6DSO_GY_ST_NEGATIVE
cparata 3:4274d9103f1d 2206 : LSM6DSO_GY_ST_DISABLE;
cparata 3:4274d9103f1d 2207
cparata 3:4274d9103f1d 2208
cparata 3:4274d9103f1d 2209 if (lsm6dso_gy_self_test_set(&_reg_ctx, reg) != 0) {
cparata 3:4274d9103f1d 2210 return 1;
cparata 3:4274d9103f1d 2211 }
cparata 3:4274d9103f1d 2212
cparata 3:4274d9103f1d 2213 return 0;
cparata 0:6d69e896ce38 2214 }
cparata 0:6d69e896ce38 2215
cparata 0:6d69e896ce38 2216 /**
cparata 0:6d69e896ce38 2217 * @brief Get the LSM6DSO FIFO number of samples
cparata 0:6d69e896ce38 2218 * @param num_samples number of samples
cparata 0:6d69e896ce38 2219 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2220 */
cparata 0:6d69e896ce38 2221 int LSM6DSOSensor::get_fifo_num_samples(uint16_t *num_samples)
cparata 0:6d69e896ce38 2222 {
cparata 3:4274d9103f1d 2223 if (lsm6dso_fifo_data_level_get(&_reg_ctx, num_samples) != 0) {
cparata 3:4274d9103f1d 2224 return 1;
cparata 3:4274d9103f1d 2225 }
cparata 3:4274d9103f1d 2226
cparata 3:4274d9103f1d 2227 return 0;
cparata 0:6d69e896ce38 2228 }
cparata 0:6d69e896ce38 2229
cparata 0:6d69e896ce38 2230 /**
cparata 0:6d69e896ce38 2231 * @brief Get the LSM6DSO FIFO full status
cparata 0:6d69e896ce38 2232 * @param status FIFO full status
cparata 0:6d69e896ce38 2233 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2234 */
cparata 0:6d69e896ce38 2235 int LSM6DSOSensor::get_fifo_full_status(uint8_t *status)
cparata 0:6d69e896ce38 2236 {
cparata 3:4274d9103f1d 2237 lsm6dso_reg_t reg;
cparata 3:4274d9103f1d 2238
cparata 3:4274d9103f1d 2239 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_FIFO_STATUS2, &reg.byte, 1) != 0) {
cparata 3:4274d9103f1d 2240 return 1;
cparata 3:4274d9103f1d 2241 }
cparata 3:4274d9103f1d 2242
cparata 3:4274d9103f1d 2243 *status = reg.fifo_status2.fifo_full_ia;
cparata 3:4274d9103f1d 2244
cparata 3:4274d9103f1d 2245 return 0;
cparata 0:6d69e896ce38 2246 }
cparata 0:6d69e896ce38 2247
cparata 0:6d69e896ce38 2248 /**
cparata 0:6d69e896ce38 2249 * @brief Set the LSM6DSO FIFO full interrupt on INT1 pin
cparata 0:6d69e896ce38 2250 * @param status FIFO full interrupt on INT1 pin status
cparata 0:6d69e896ce38 2251 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2252 */
cparata 0:6d69e896ce38 2253 int LSM6DSOSensor::set_fifo_int1_fifo_full(uint8_t status)
cparata 0:6d69e896ce38 2254 {
cparata 3:4274d9103f1d 2255 lsm6dso_reg_t reg;
cparata 3:4274d9103f1d 2256
cparata 3:4274d9103f1d 2257 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_INT1_CTRL, &reg.byte, 1) != 0) {
cparata 3:4274d9103f1d 2258 return 1;
cparata 3:4274d9103f1d 2259 }
cparata 3:4274d9103f1d 2260
cparata 3:4274d9103f1d 2261 reg.int1_ctrl.int1_fifo_full = status;
cparata 3:4274d9103f1d 2262
cparata 3:4274d9103f1d 2263 if (lsm6dso_write_reg(&_reg_ctx, LSM6DSO_INT1_CTRL, &reg.byte, 1) != 0) {
cparata 3:4274d9103f1d 2264 return 1;
cparata 3:4274d9103f1d 2265 }
cparata 3:4274d9103f1d 2266
cparata 3:4274d9103f1d 2267 return 0;
cparata 0:6d69e896ce38 2268 }
cparata 0:6d69e896ce38 2269
cparata 0:6d69e896ce38 2270 /**
cparata 0:6d69e896ce38 2271 * @brief Set the LSM6DSO FIFO watermark level
cparata 0:6d69e896ce38 2272 * @param watermark FIFO watermark level
cparata 0:6d69e896ce38 2273 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2274 */
cparata 0:6d69e896ce38 2275 int LSM6DSOSensor::set_fifo_watermark_level(uint16_t watermark)
cparata 0:6d69e896ce38 2276 {
cparata 3:4274d9103f1d 2277 if (lsm6dso_fifo_watermark_set(&_reg_ctx, watermark) != 0) {
cparata 3:4274d9103f1d 2278 return 1;
cparata 3:4274d9103f1d 2279 }
cparata 3:4274d9103f1d 2280
cparata 3:4274d9103f1d 2281 return 0;
cparata 0:6d69e896ce38 2282 }
cparata 0:6d69e896ce38 2283
cparata 0:6d69e896ce38 2284 /**
cparata 0:6d69e896ce38 2285 * @brief Set the LSM6DSO FIFO stop on watermark
cparata 0:6d69e896ce38 2286 * @param status FIFO stop on watermark status
cparata 0:6d69e896ce38 2287 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2288 */
cparata 0:6d69e896ce38 2289 int LSM6DSOSensor::set_fifo_stop_on_fth(uint8_t status)
cparata 0:6d69e896ce38 2290 {
cparata 3:4274d9103f1d 2291 if (lsm6dso_fifo_stop_on_wtm_set(&_reg_ctx, status) != 0) {
cparata 3:4274d9103f1d 2292 return 1;
cparata 3:4274d9103f1d 2293 }
cparata 3:4274d9103f1d 2294
cparata 3:4274d9103f1d 2295 return 0;
cparata 0:6d69e896ce38 2296 }
cparata 0:6d69e896ce38 2297
cparata 0:6d69e896ce38 2298 /**
cparata 0:6d69e896ce38 2299 * @brief Set the LSM6DSO FIFO mode
cparata 0:6d69e896ce38 2300 * @param mode FIFO mode
cparata 0:6d69e896ce38 2301 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2302 */
cparata 0:6d69e896ce38 2303 int LSM6DSOSensor::set_fifo_mode(uint8_t mode)
cparata 0:6d69e896ce38 2304 {
cparata 3:4274d9103f1d 2305 int ret = 0;
cparata 3:4274d9103f1d 2306
cparata 3:4274d9103f1d 2307 /* Verify that the passed parameter contains one of the valid values. */
cparata 3:4274d9103f1d 2308 switch ((lsm6dso_fifo_mode_t)mode) {
cparata 3:4274d9103f1d 2309 case LSM6DSO_BYPASS_MODE:
cparata 3:4274d9103f1d 2310 case LSM6DSO_FIFO_MODE:
cparata 3:4274d9103f1d 2311 case LSM6DSO_STREAM_TO_FIFO_MODE:
cparata 3:4274d9103f1d 2312 case LSM6DSO_BYPASS_TO_STREAM_MODE:
cparata 3:4274d9103f1d 2313 case LSM6DSO_STREAM_MODE:
cparata 3:4274d9103f1d 2314 break;
cparata 3:4274d9103f1d 2315
cparata 3:4274d9103f1d 2316 default:
cparata 3:4274d9103f1d 2317 ret = 1;
cparata 3:4274d9103f1d 2318 break;
cparata 3:4274d9103f1d 2319 }
cparata 3:4274d9103f1d 2320
cparata 3:4274d9103f1d 2321 if (ret == 1) {
cparata 3:4274d9103f1d 2322 return ret;
cparata 3:4274d9103f1d 2323 }
cparata 3:4274d9103f1d 2324
cparata 3:4274d9103f1d 2325 if (lsm6dso_fifo_mode_set(&_reg_ctx, (lsm6dso_fifo_mode_t)mode) != 0) {
cparata 3:4274d9103f1d 2326 return 1;
cparata 3:4274d9103f1d 2327 }
cparata 3:4274d9103f1d 2328
cparata 0:6d69e896ce38 2329 return ret;
cparata 0:6d69e896ce38 2330 }
cparata 0:6d69e896ce38 2331
cparata 0:6d69e896ce38 2332 /**
cparata 0:6d69e896ce38 2333 * @brief Get the LSM6DSO FIFO tag
cparata 0:6d69e896ce38 2334 * @param tag FIFO tag
cparata 0:6d69e896ce38 2335 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2336 */
cparata 0:6d69e896ce38 2337 int LSM6DSOSensor::get_fifo_tag(uint8_t *tag)
cparata 0:6d69e896ce38 2338 {
cparata 3:4274d9103f1d 2339 lsm6dso_fifo_tag_t tag_local;
cparata 3:4274d9103f1d 2340
cparata 3:4274d9103f1d 2341 if (lsm6dso_fifo_sensor_tag_get(&_reg_ctx, &tag_local) != 0) {
cparata 3:4274d9103f1d 2342 return 1;
cparata 3:4274d9103f1d 2343 }
cparata 3:4274d9103f1d 2344
cparata 3:4274d9103f1d 2345 *tag = (uint8_t)tag_local;
cparata 3:4274d9103f1d 2346
cparata 3:4274d9103f1d 2347 return 0;
cparata 0:6d69e896ce38 2348 }
cparata 0:6d69e896ce38 2349
cparata 0:6d69e896ce38 2350 /**
cparata 0:6d69e896ce38 2351 * @brief Get the LSM6DSO FIFO raw data
cparata 0:6d69e896ce38 2352 * @param data FIFO raw data array [6]
cparata 0:6d69e896ce38 2353 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2354 */
cparata 0:6d69e896ce38 2355 int LSM6DSOSensor::get_fifo_data(uint8_t *data)
cparata 0:6d69e896ce38 2356 {
cparata 3:4274d9103f1d 2357 if (lsm6dso_read_reg(&_reg_ctx, LSM6DSO_FIFO_DATA_OUT_X_L, data, 6) != 0) {
cparata 3:4274d9103f1d 2358 return 1;
cparata 3:4274d9103f1d 2359 }
cparata 3:4274d9103f1d 2360
cparata 3:4274d9103f1d 2361 return 0;
cparata 0:6d69e896ce38 2362 }
cparata 0:6d69e896ce38 2363
cparata 0:6d69e896ce38 2364 /**
cparata 0:6d69e896ce38 2365 * @brief Get the LSM6DSO FIFO accelero single sample (16-bit data per 3 axes) and calculate acceleration [mg]
cparata 0:6d69e896ce38 2366 * @param acceleration FIFO accelero axes [mg]
cparata 0:6d69e896ce38 2367 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2368 */
cparata 0:6d69e896ce38 2369 int LSM6DSOSensor::get_fifo_x_axes(int32_t *acceleration)
cparata 0:6d69e896ce38 2370 {
cparata 3:4274d9103f1d 2371 uint8_t data[6];
cparata 3:4274d9103f1d 2372 int16_t data_raw[3];
cparata 3:4274d9103f1d 2373 float sensitivity = 0.0f;
cparata 3:4274d9103f1d 2374 float acceleration_float[3];
cparata 3:4274d9103f1d 2375
cparata 3:4274d9103f1d 2376 if (get_fifo_data(data) != 0) {
cparata 3:4274d9103f1d 2377 return 1;
cparata 3:4274d9103f1d 2378 }
cparata 3:4274d9103f1d 2379
cparata 3:4274d9103f1d 2380 data_raw[0] = ((int16_t)data[1] << 8) | data[0];
cparata 3:4274d9103f1d 2381 data_raw[1] = ((int16_t)data[3] << 8) | data[2];
cparata 3:4274d9103f1d 2382 data_raw[2] = ((int16_t)data[5] << 8) | data[4];
cparata 3:4274d9103f1d 2383
cparata 3:4274d9103f1d 2384 if (get_x_sensitivity(&sensitivity) != 0) {
cparata 3:4274d9103f1d 2385 return 1;
cparata 3:4274d9103f1d 2386 }
cparata 3:4274d9103f1d 2387
cparata 3:4274d9103f1d 2388 acceleration_float[0] = (float)data_raw[0] * sensitivity;
cparata 3:4274d9103f1d 2389 acceleration_float[1] = (float)data_raw[1] * sensitivity;
cparata 3:4274d9103f1d 2390 acceleration_float[2] = (float)data_raw[2] * sensitivity;
cparata 3:4274d9103f1d 2391
cparata 3:4274d9103f1d 2392 acceleration[0] = (int32_t)acceleration_float[0];
cparata 3:4274d9103f1d 2393 acceleration[1] = (int32_t)acceleration_float[1];
cparata 3:4274d9103f1d 2394 acceleration[2] = (int32_t)acceleration_float[2];
cparata 3:4274d9103f1d 2395
cparata 3:4274d9103f1d 2396 return 0;
cparata 0:6d69e896ce38 2397 }
cparata 0:6d69e896ce38 2398
cparata 0:6d69e896ce38 2399 /**
cparata 0:6d69e896ce38 2400 * @brief Set the LSM6DSO FIFO accelero BDR value
cparata 0:6d69e896ce38 2401 * @param bdr FIFO accelero BDR value
cparata 0:6d69e896ce38 2402 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2403 */
cparata 0:6d69e896ce38 2404 int LSM6DSOSensor::set_fifo_x_bdr(float bdr)
cparata 0:6d69e896ce38 2405 {
cparata 3:4274d9103f1d 2406 lsm6dso_bdr_xl_t new_bdr;
cparata 3:4274d9103f1d 2407
cparata 3:4274d9103f1d 2408 new_bdr = (bdr <= 0.0f) ? LSM6DSO_XL_NOT_BATCHED
cparata 3:4274d9103f1d 2409 : (bdr <= 12.5f) ? LSM6DSO_XL_BATCHED_AT_12Hz5
cparata 3:4274d9103f1d 2410 : (bdr <= 26.0f) ? LSM6DSO_XL_BATCHED_AT_26Hz
cparata 3:4274d9103f1d 2411 : (bdr <= 52.0f) ? LSM6DSO_XL_BATCHED_AT_52Hz
cparata 3:4274d9103f1d 2412 : (bdr <= 104.0f) ? LSM6DSO_XL_BATCHED_AT_104Hz
cparata 3:4274d9103f1d 2413 : (bdr <= 208.0f) ? LSM6DSO_XL_BATCHED_AT_208Hz
cparata 3:4274d9103f1d 2414 : (bdr <= 416.0f) ? LSM6DSO_XL_BATCHED_AT_417Hz
cparata 3:4274d9103f1d 2415 : (bdr <= 833.0f) ? LSM6DSO_XL_BATCHED_AT_833Hz
cparata 3:4274d9103f1d 2416 : (bdr <= 1660.0f) ? LSM6DSO_XL_BATCHED_AT_1667Hz
cparata 3:4274d9103f1d 2417 : (bdr <= 3330.0f) ? LSM6DSO_XL_BATCHED_AT_3333Hz
cparata 3:4274d9103f1d 2418 : LSM6DSO_XL_BATCHED_AT_6667Hz;
cparata 3:4274d9103f1d 2419
cparata 3:4274d9103f1d 2420 if (lsm6dso_fifo_xl_batch_set(&_reg_ctx, new_bdr) != 0) {
cparata 3:4274d9103f1d 2421 return 1;
cparata 3:4274d9103f1d 2422 }
cparata 3:4274d9103f1d 2423
cparata 3:4274d9103f1d 2424 return 0;
cparata 0:6d69e896ce38 2425 }
cparata 0:6d69e896ce38 2426
cparata 0:6d69e896ce38 2427 /**
cparata 0:6d69e896ce38 2428 * @brief Get the LSM6DSO FIFO gyro single sample (16-bit data per 3 axes) and calculate angular velocity [mDPS]
cparata 0:6d69e896ce38 2429 * @param angular_velocity FIFO gyro axes [mDPS]
cparata 0:6d69e896ce38 2430 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2431 */
cparata 0:6d69e896ce38 2432 int LSM6DSOSensor::get_fifo_g_axes(int32_t *angular_velocity)
cparata 0:6d69e896ce38 2433 {
cparata 3:4274d9103f1d 2434 uint8_t data[6];
cparata 3:4274d9103f1d 2435 int16_t data_raw[3];
cparata 3:4274d9103f1d 2436 float sensitivity = 0.0f;
cparata 3:4274d9103f1d 2437 float angular_velocity_float[3];
cparata 3:4274d9103f1d 2438
cparata 3:4274d9103f1d 2439 if (get_fifo_data(data) != 0) {
cparata 3:4274d9103f1d 2440 return 1;
cparata 3:4274d9103f1d 2441 }
cparata 3:4274d9103f1d 2442
cparata 3:4274d9103f1d 2443 data_raw[0] = ((int16_t)data[1] << 8) | data[0];
cparata 3:4274d9103f1d 2444 data_raw[1] = ((int16_t)data[3] << 8) | data[2];
cparata 3:4274d9103f1d 2445 data_raw[2] = ((int16_t)data[5] << 8) | data[4];
cparata 3:4274d9103f1d 2446
cparata 3:4274d9103f1d 2447 if (get_g_sensitivity(&sensitivity) != 0) {
cparata 3:4274d9103f1d 2448 return 1;
cparata 3:4274d9103f1d 2449 }
cparata 3:4274d9103f1d 2450
cparata 3:4274d9103f1d 2451 angular_velocity_float[0] = (float)data_raw[0] * sensitivity;
cparata 3:4274d9103f1d 2452 angular_velocity_float[1] = (float)data_raw[1] * sensitivity;
cparata 3:4274d9103f1d 2453 angular_velocity_float[2] = (float)data_raw[2] * sensitivity;
cparata 3:4274d9103f1d 2454
cparata 3:4274d9103f1d 2455 angular_velocity[0] = (int32_t)angular_velocity_float[0];
cparata 3:4274d9103f1d 2456 angular_velocity[1] = (int32_t)angular_velocity_float[1];
cparata 3:4274d9103f1d 2457 angular_velocity[2] = (int32_t)angular_velocity_float[2];
cparata 3:4274d9103f1d 2458
cparata 3:4274d9103f1d 2459 return 0;
cparata 0:6d69e896ce38 2460 }
cparata 0:6d69e896ce38 2461
cparata 0:6d69e896ce38 2462 /**
cparata 0:6d69e896ce38 2463 * @brief Set the LSM6DSO FIFO gyro BDR value
cparata 0:6d69e896ce38 2464 * @param bdr FIFO gyro BDR value
cparata 0:6d69e896ce38 2465 * @retval 0 in case of success, an error code otherwise
cparata 0:6d69e896ce38 2466 */
cparata 0:6d69e896ce38 2467 int LSM6DSOSensor::set_fifo_g_bdr(float bdr)
cparata 0:6d69e896ce38 2468 {
cparata 3:4274d9103f1d 2469 lsm6dso_bdr_gy_t new_bdr;
cparata 3:4274d9103f1d 2470
cparata 3:4274d9103f1d 2471 new_bdr = (bdr <= 0.0f) ? LSM6DSO_GY_NOT_BATCHED
cparata 3:4274d9103f1d 2472 : (bdr <= 12.5f) ? LSM6DSO_GY_BATCHED_AT_12Hz5
cparata 3:4274d9103f1d 2473 : (bdr <= 26.0f) ? LSM6DSO_GY_BATCHED_AT_26Hz
cparata 3:4274d9103f1d 2474 : (bdr <= 52.0f) ? LSM6DSO_GY_BATCHED_AT_52Hz
cparata 3:4274d9103f1d 2475 : (bdr <= 104.0f) ? LSM6DSO_GY_BATCHED_AT_104Hz
cparata 3:4274d9103f1d 2476 : (bdr <= 208.0f) ? LSM6DSO_GY_BATCHED_AT_208Hz
cparata 3:4274d9103f1d 2477 : (bdr <= 416.0f) ? LSM6DSO_GY_BATCHED_AT_417Hz
cparata 3:4274d9103f1d 2478 : (bdr <= 833.0f) ? LSM6DSO_GY_BATCHED_AT_833Hz
cparata 3:4274d9103f1d 2479 : (bdr <= 1660.0f) ? LSM6DSO_GY_BATCHED_AT_1667Hz
cparata 3:4274d9103f1d 2480 : (bdr <= 3330.0f) ? LSM6DSO_GY_BATCHED_AT_3333Hz
cparata 3:4274d9103f1d 2481 : LSM6DSO_GY_BATCHED_AT_6667Hz;
cparata 3:4274d9103f1d 2482
cparata 3:4274d9103f1d 2483 if (lsm6dso_fifo_gy_batch_set(&_reg_ctx, new_bdr) != 0) {
cparata 3:4274d9103f1d 2484 return 1;
cparata 3:4274d9103f1d 2485 }
cparata 3:4274d9103f1d 2486
cparata 3:4274d9103f1d 2487 return 0;
cparata 0:6d69e896ce38 2488 }
cparata 0:6d69e896ce38 2489
cparata 0:6d69e896ce38 2490
cparata 0:6d69e896ce38 2491
cparata 0:6d69e896ce38 2492 int32_t LSM6DSO_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
cparata 0:6d69e896ce38 2493 {
cparata 3:4274d9103f1d 2494 return ((LSM6DSOSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite);
cparata 0:6d69e896ce38 2495 }
cparata 0:6d69e896ce38 2496
cparata 0:6d69e896ce38 2497 int32_t LSM6DSO_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
cparata 0:6d69e896ce38 2498 {
cparata 3:4274d9103f1d 2499 return ((LSM6DSOSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead);
cparata 0:6d69e896ce38 2500 }