Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ST_INTERFACES X_NUCLEO_COMMON_SPI3W
Fork of X_NUCLEO_IKS01A2 by
LSM6DSLSensor.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file LSM6DSLSensor.cpp 00004 * @author CLab 00005 * @version V1.0.0 00006 * @date 5 August 2016 00007 * @brief Implementation of an LSM6DSL Inertial Measurement Unit (IMU) 6 axes 00008 * sensor. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00013 * 00014 * Redistribution and use in source and binary forms, with or without modification, 00015 * are permitted provided that the following conditions are met: 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 2. Redistributions in binary form must reproduce the above copyright notice, 00019 * this list of conditions and the following disclaimer in the documentation 00020 * and/or other materials provided with the distribution. 00021 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00022 * may be used to endorse or promote products derived from this software 00023 * without specific prior written permission. 00024 * 00025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00026 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00028 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00029 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00030 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00031 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00032 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00033 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00034 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 ****************************************************************************** 00037 */ 00038 00039 00040 /* Includes ------------------------------------------------------------------*/ 00041 00042 #include "SPI3W.h" 00043 #include "mbed.h" 00044 #include "DevI2C.h" 00045 #include "LSM6DSLSensor.h" 00046 #include "LSM6DSL_acc_gyro_driver.h" 00047 #include <assert.h> 00048 00049 /* Class Implementation ------------------------------------------------------*/ 00050 00051 /** Constructor 00052 * @param spi3w object of an helper class which handles the spi peripheral 00053 * @param 00054 */ 00055 LSM6DSLSensor::LSM6DSLSensor(SPI3W *spi3w, PinName cs_pin, PinName int1_pin, PinName int2_pin ) : 00056 _dev_spi3w(spi3w), _cs_pin(cs_pin), _int1_irq(int1_pin), _int2_irq(int2_pin) 00057 { 00058 assert (spi3w); 00059 if (cs_pin == NC) 00060 { 00061 printf ("ERROR LSM6DSLSensor CS MUST NOT BE NC\n\r"); 00062 _dev_spi = NULL; 00063 _dev_spi3w = NULL; 00064 _dev_i2c=NULL; 00065 return; 00066 } 00067 _cs_pin = 1; 00068 _dev_spi = NULL; 00069 _dev_i2c=NULL; 00070 00071 LSM6DSL_ACC_GYRO_W_SPI_Mode((void *)this, LSM6DSL_ACC_GYRO_SIM_3_WIRE); 00072 LSM6DSL_ACC_GYRO_W_I2C_MASTER_Enable((void *)this, LSM6DSL_ACC_GYRO_MASTER_ON_DISABLED); 00073 } 00074 00075 LSM6DSLSensor::LSM6DSLSensor(SPI *spi, PinName cs_pin, PinName int1_pin, PinName int2_pin) : 00076 _dev_spi(spi), _cs_pin(cs_pin), _int1_irq(int1_pin), _int2_irq(int2_pin) 00077 { 00078 assert (spi); 00079 if (cs_pin == NC) 00080 { 00081 printf ("ERROR LSM6DSLSensor CS MUST NOT BE NC\n\r"); 00082 _dev_spi = NULL; 00083 _dev_spi3w = NULL; 00084 _dev_i2c=NULL; 00085 return; 00086 } 00087 _cs_pin = 1; 00088 _dev_i2c=NULL; 00089 00090 LSM6DSL_ACC_GYRO_W_SPI_Mode((void *)this, LSM6DSL_ACC_GYRO_SIM_4_WIRE); 00091 LSM6DSL_ACC_GYRO_W_I2C_MASTER_Enable((void *)this, LSM6DSL_ACC_GYRO_MASTER_ON_DISABLED); 00092 } 00093 00094 00095 /** Constructor 00096 * @param i2c object of an helper class which handles the I2C peripheral 00097 * @param address the address of the component's instance 00098 */ 00099 LSM6DSLSensor::LSM6DSLSensor(DevI2C *i2c, uint8_t address, PinName int1_pin, PinName int2_pin) : 00100 _dev_i2c(i2c), _address(address), _cs_pin(NC), _int1_irq(int1_pin), _int2_irq(int2_pin) 00101 { 00102 assert (i2c); 00103 _dev_spi3w = NULL; 00104 _dev_spi = NULL; 00105 }; 00106 00107 00108 /** 00109 * @brief Initializing the component. 00110 * @param[in] init pointer to device specific initalization structure. 00111 * @retval "0" in case of success, an error code otherwise. 00112 */ 00113 int LSM6DSLSensor::init(void *init) 00114 { 00115 /* Enable register address automatically incremented during a multiple byte 00116 access with a serial interface. */ 00117 if ( LSM6DSL_ACC_GYRO_W_IF_Addr_Incr( (void *)this, LSM6DSL_ACC_GYRO_IF_INC_ENABLED ) == MEMS_ERROR ) 00118 { 00119 return 1; 00120 } 00121 00122 /* Enable BDU */ 00123 if ( LSM6DSL_ACC_GYRO_W_BDU( (void *)this, LSM6DSL_ACC_GYRO_BDU_BLOCK_UPDATE ) == MEMS_ERROR ) 00124 { 00125 return 1; 00126 } 00127 00128 /* FIFO mode selection */ 00129 if ( LSM6DSL_ACC_GYRO_W_FIFO_MODE( (void *)this, LSM6DSL_ACC_GYRO_FIFO_MODE_BYPASS ) == MEMS_ERROR ) 00130 { 00131 return 1; 00132 } 00133 00134 /* Output data rate selection - power down. */ 00135 if ( LSM6DSL_ACC_GYRO_W_ODR_XL( (void *)this, LSM6DSL_ACC_GYRO_ODR_XL_POWER_DOWN ) == MEMS_ERROR ) 00136 { 00137 return 1; 00138 } 00139 00140 /* Full scale selection. */ 00141 if ( set_x_fs( 2.0f ) == 1 ) 00142 { 00143 return 1; 00144 } 00145 00146 /* Output data rate selection - power down */ 00147 if ( LSM6DSL_ACC_GYRO_W_ODR_G( (void *)this, LSM6DSL_ACC_GYRO_ODR_G_POWER_DOWN ) == MEMS_ERROR ) 00148 { 00149 return 1; 00150 } 00151 00152 /* Full scale selection. */ 00153 if ( set_g_fs( 2000.0f ) == 1 ) 00154 { 00155 return 1; 00156 } 00157 00158 _x_last_odr = 104.0f; 00159 00160 _x_is_enabled = 0; 00161 00162 _g_last_odr = 104.0f; 00163 00164 _g_is_enabled = 0; 00165 00166 return 0; 00167 } 00168 00169 /** 00170 * @brief Enable LSM6DSL Accelerator 00171 * @retval 0 in case of success, an error code otherwise 00172 */ 00173 int LSM6DSLSensor::enable_x(void) 00174 { 00175 /* Check if the component is already enabled */ 00176 if ( _x_is_enabled == 1 ) 00177 { 00178 return 0; 00179 } 00180 00181 /* Output data rate selection. */ 00182 if ( set_x_odr_when_enabled( _x_last_odr ) == 1 ) 00183 { 00184 return 1; 00185 } 00186 00187 _x_is_enabled = 1; 00188 00189 return 0; 00190 } 00191 00192 /** 00193 * @brief Enable LSM6DSL Gyroscope 00194 * @retval 0 in case of success, an error code otherwise 00195 */ 00196 int LSM6DSLSensor::enable_g(void) 00197 { 00198 /* Check if the component is already enabled */ 00199 if ( _g_is_enabled == 1 ) 00200 { 00201 return 0; 00202 } 00203 00204 /* Output data rate selection. */ 00205 if ( set_g_odr_when_enabled( _g_last_odr ) == 1 ) 00206 { 00207 return 1; 00208 } 00209 00210 _g_is_enabled = 1; 00211 00212 return 0; 00213 } 00214 00215 /** 00216 * @brief Disable LSM6DSL Accelerator 00217 * @retval 0 in case of success, an error code otherwise 00218 */ 00219 int LSM6DSLSensor::disable_x(void) 00220 { 00221 /* Check if the component is already disabled */ 00222 if ( _x_is_enabled == 0 ) 00223 { 00224 return 0; 00225 } 00226 00227 /* Store actual output data rate. */ 00228 if ( get_x_odr( &_x_last_odr ) == 1 ) 00229 { 00230 return 1; 00231 } 00232 00233 /* Output data rate selection - power down. */ 00234 if ( LSM6DSL_ACC_GYRO_W_ODR_XL( (void *)this, LSM6DSL_ACC_GYRO_ODR_XL_POWER_DOWN ) == MEMS_ERROR ) 00235 { 00236 return 1; 00237 } 00238 00239 _x_is_enabled = 0; 00240 00241 return 0; 00242 } 00243 00244 /** 00245 * @brief Disable LSM6DSL Gyroscope 00246 * @retval 0 in case of success, an error code otherwise 00247 */ 00248 int LSM6DSLSensor::disable_g(void) 00249 { 00250 /* Check if the component is already disabled */ 00251 if ( _g_is_enabled == 0 ) 00252 { 00253 return 0; 00254 } 00255 00256 /* Store actual output data rate. */ 00257 if ( get_g_odr( &_g_last_odr ) == 1 ) 00258 { 00259 return 1; 00260 } 00261 00262 /* Output data rate selection - power down */ 00263 if ( LSM6DSL_ACC_GYRO_W_ODR_G( (void *)this, LSM6DSL_ACC_GYRO_ODR_G_POWER_DOWN ) == MEMS_ERROR ) 00264 { 00265 return 1; 00266 } 00267 00268 _g_is_enabled = 0; 00269 00270 return 0; 00271 } 00272 00273 /** 00274 * @brief Read ID of LSM6DSL Accelerometer and Gyroscope 00275 * @param p_id the pointer where the ID of the device is stored 00276 * @retval 0 in case of success, an error code otherwise 00277 */ 00278 int LSM6DSLSensor::read_id(uint8_t *id) 00279 { 00280 if(!id) 00281 { 00282 return 1; 00283 } 00284 00285 /* Read WHO AM I register */ 00286 if ( LSM6DSL_ACC_GYRO_R_WHO_AM_I( (void *)this, id ) == MEMS_ERROR ) 00287 { 00288 return 1; 00289 } 00290 00291 return 0; 00292 } 00293 00294 /** 00295 * @brief Read data from LSM6DSL Accelerometer 00296 * @param pData the pointer where the accelerometer data are stored 00297 * @retval 0 in case of success, an error code otherwise 00298 */ 00299 int LSM6DSLSensor::get_x_axes(int32_t *pData) 00300 { 00301 int16_t dataRaw[3]; 00302 float sensitivity = 0; 00303 00304 /* Read raw data from LSM6DSL output register. */ 00305 if ( get_x_axes_raw( dataRaw ) == 1 ) 00306 { 00307 return 1; 00308 } 00309 00310 /* Get LSM6DSL actual sensitivity. */ 00311 if ( get_x_sensitivity( &sensitivity ) == 1 ) 00312 { 00313 return 1; 00314 } 00315 00316 /* Calculate the data. */ 00317 pData[0] = ( int32_t )( dataRaw[0] * sensitivity ); 00318 pData[1] = ( int32_t )( dataRaw[1] * sensitivity ); 00319 pData[2] = ( int32_t )( dataRaw[2] * sensitivity ); 00320 00321 return 0; 00322 } 00323 00324 /** 00325 * @brief Read data from LSM6DSL Gyroscope 00326 * @param pData the pointer where the gyroscope data are stored 00327 * @retval 0 in case of success, an error code otherwise 00328 */ 00329 int LSM6DSLSensor::get_g_axes(int32_t *pData) 00330 { 00331 int16_t dataRaw[3]; 00332 float sensitivity = 0; 00333 00334 /* Read raw data from LSM6DSL output register. */ 00335 if ( get_g_axes_raw( dataRaw ) == 1 ) 00336 { 00337 return 1; 00338 } 00339 00340 /* Get LSM6DSL actual sensitivity. */ 00341 if ( get_g_sensitivity( &sensitivity ) == 1 ) 00342 { 00343 return 1; 00344 } 00345 00346 /* Calculate the data. */ 00347 pData[0] = ( int32_t )( dataRaw[0] * sensitivity ); 00348 pData[1] = ( int32_t )( dataRaw[1] * sensitivity ); 00349 pData[2] = ( int32_t )( dataRaw[2] * sensitivity ); 00350 00351 return 0; 00352 } 00353 00354 /** 00355 * @brief Read Accelerometer Sensitivity 00356 * @param pfData the pointer where the accelerometer sensitivity is stored 00357 * @retval 0 in case of success, an error code otherwise 00358 */ 00359 int LSM6DSLSensor::get_x_sensitivity(float *pfData) 00360 { 00361 LSM6DSL_ACC_GYRO_FS_XL_t fullScale; 00362 00363 /* Read actual full scale selection from sensor. */ 00364 if ( LSM6DSL_ACC_GYRO_R_FS_XL( (void *)this, &fullScale ) == MEMS_ERROR ) 00365 { 00366 return 1; 00367 } 00368 00369 /* Store the sensitivity based on actual full scale. */ 00370 switch( fullScale ) 00371 { 00372 case LSM6DSL_ACC_GYRO_FS_XL_2g: 00373 *pfData = ( float )LSM6DSL_ACC_SENSITIVITY_FOR_FS_2G; 00374 break; 00375 case LSM6DSL_ACC_GYRO_FS_XL_4g: 00376 *pfData = ( float )LSM6DSL_ACC_SENSITIVITY_FOR_FS_4G; 00377 break; 00378 case LSM6DSL_ACC_GYRO_FS_XL_8g: 00379 *pfData = ( float )LSM6DSL_ACC_SENSITIVITY_FOR_FS_8G; 00380 break; 00381 case LSM6DSL_ACC_GYRO_FS_XL_16g: 00382 *pfData = ( float )LSM6DSL_ACC_SENSITIVITY_FOR_FS_16G; 00383 break; 00384 default: 00385 *pfData = -1.0f; 00386 return 1; 00387 } 00388 00389 return 0; 00390 } 00391 00392 /** 00393 * @brief Read Gyroscope Sensitivity 00394 * @param pfData the pointer where the gyroscope sensitivity is stored 00395 * @retval 0 in case of success, an error code otherwise 00396 */ 00397 int LSM6DSLSensor::get_g_sensitivity(float *pfData) 00398 { 00399 LSM6DSL_ACC_GYRO_FS_125_t fullScale125; 00400 LSM6DSL_ACC_GYRO_FS_G_t fullScale; 00401 00402 /* Read full scale 125 selection from sensor. */ 00403 if ( LSM6DSL_ACC_GYRO_R_FS_125( (void *)this, &fullScale125 ) == MEMS_ERROR ) 00404 { 00405 return 1; 00406 } 00407 00408 if ( fullScale125 == LSM6DSL_ACC_GYRO_FS_125_ENABLED ) 00409 { 00410 *pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_125DPS; 00411 } 00412 00413 else 00414 { 00415 00416 /* Read actual full scale selection from sensor. */ 00417 if ( LSM6DSL_ACC_GYRO_R_FS_G( (void *)this, &fullScale ) == MEMS_ERROR ) 00418 { 00419 return 1; 00420 } 00421 00422 /* Store the sensitivity based on actual full scale. */ 00423 switch( fullScale ) 00424 { 00425 case LSM6DSL_ACC_GYRO_FS_G_245dps: 00426 *pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_245DPS; 00427 break; 00428 case LSM6DSL_ACC_GYRO_FS_G_500dps: 00429 *pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_500DPS; 00430 break; 00431 case LSM6DSL_ACC_GYRO_FS_G_1000dps: 00432 *pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_1000DPS; 00433 break; 00434 case LSM6DSL_ACC_GYRO_FS_G_2000dps: 00435 *pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_2000DPS; 00436 break; 00437 default: 00438 *pfData = -1.0f; 00439 return 1; 00440 } 00441 } 00442 00443 return 0; 00444 } 00445 00446 /** 00447 * @brief Read raw data from LSM6DSL Accelerometer 00448 * @param pData the pointer where the accelerometer raw data are stored 00449 * @retval 0 in case of success, an error code otherwise 00450 */ 00451 int LSM6DSLSensor::get_x_axes_raw(int16_t *pData) 00452 { 00453 uint8_t regValue[6] = {0, 0, 0, 0, 0, 0}; 00454 00455 /* Read output registers from LSM6DSL_ACC_GYRO_OUTX_L_XL to LSM6DSL_ACC_GYRO_OUTZ_H_XL. */ 00456 if ( LSM6DSL_ACC_GYRO_GetRawAccData( (void *)this, regValue ) == MEMS_ERROR ) 00457 { 00458 return 1; 00459 } 00460 00461 /* Format the data. */ 00462 pData[0] = ( ( ( ( int16_t )regValue[1] ) << 8 ) + ( int16_t )regValue[0] ); 00463 pData[1] = ( ( ( ( int16_t )regValue[3] ) << 8 ) + ( int16_t )regValue[2] ); 00464 pData[2] = ( ( ( ( int16_t )regValue[5] ) << 8 ) + ( int16_t )regValue[4] ); 00465 00466 return 0; 00467 } 00468 00469 /** 00470 * @brief Read raw data from LSM6DSL Gyroscope 00471 * @param pData the pointer where the gyroscope raw data are stored 00472 * @retval 0 in case of success, an error code otherwise 00473 */ 00474 int LSM6DSLSensor::get_g_axes_raw(int16_t *pData) 00475 { 00476 uint8_t regValue[6] = {0, 0, 0, 0, 0, 0}; 00477 00478 /* Read output registers from LSM6DSL_ACC_GYRO_OUTX_L_G to LSM6DSL_ACC_GYRO_OUTZ_H_G. */ 00479 if ( LSM6DSL_ACC_GYRO_GetRawGyroData( (void *)this, regValue ) == MEMS_ERROR ) 00480 { 00481 return 1; 00482 } 00483 00484 /* Format the data. */ 00485 pData[0] = ( ( ( ( int16_t )regValue[1] ) << 8 ) + ( int16_t )regValue[0] ); 00486 pData[1] = ( ( ( ( int16_t )regValue[3] ) << 8 ) + ( int16_t )regValue[2] ); 00487 pData[2] = ( ( ( ( int16_t )regValue[5] ) << 8 ) + ( int16_t )regValue[4] ); 00488 00489 return 0; 00490 } 00491 00492 /** 00493 * @brief Read LSM6DSL Accelerometer output data rate 00494 * @param odr the pointer to the output data rate 00495 * @retval 0 in case of success, an error code otherwise 00496 */ 00497 int LSM6DSLSensor::get_x_odr(float* odr) 00498 { 00499 LSM6DSL_ACC_GYRO_ODR_XL_t odr_low_level; 00500 00501 if ( LSM6DSL_ACC_GYRO_R_ODR_XL( (void *)this, &odr_low_level ) == MEMS_ERROR ) 00502 { 00503 return 1; 00504 } 00505 00506 switch( odr_low_level ) 00507 { 00508 case LSM6DSL_ACC_GYRO_ODR_XL_POWER_DOWN: 00509 *odr = 0.0f; 00510 break; 00511 case LSM6DSL_ACC_GYRO_ODR_XL_13Hz: 00512 *odr = 13.0f; 00513 break; 00514 case LSM6DSL_ACC_GYRO_ODR_XL_26Hz: 00515 *odr = 26.0f; 00516 break; 00517 case LSM6DSL_ACC_GYRO_ODR_XL_52Hz: 00518 *odr = 52.0f; 00519 break; 00520 case LSM6DSL_ACC_GYRO_ODR_XL_104Hz: 00521 *odr = 104.0f; 00522 break; 00523 case LSM6DSL_ACC_GYRO_ODR_XL_208Hz: 00524 *odr = 208.0f; 00525 break; 00526 case LSM6DSL_ACC_GYRO_ODR_XL_416Hz: 00527 *odr = 416.0f; 00528 break; 00529 case LSM6DSL_ACC_GYRO_ODR_XL_833Hz: 00530 *odr = 833.0f; 00531 break; 00532 case LSM6DSL_ACC_GYRO_ODR_XL_1660Hz: 00533 *odr = 1660.0f; 00534 break; 00535 case LSM6DSL_ACC_GYRO_ODR_XL_3330Hz: 00536 *odr = 3330.0f; 00537 break; 00538 case LSM6DSL_ACC_GYRO_ODR_XL_6660Hz: 00539 *odr = 6660.0f; 00540 break; 00541 default: 00542 *odr = -1.0f; 00543 return 1; 00544 } 00545 00546 return 0; 00547 } 00548 00549 /** 00550 * @brief Read LSM6DSL Gyroscope output data rate 00551 * @param odr the pointer to the output data rate 00552 * @retval 0 in case of success, an error code otherwise 00553 */ 00554 int LSM6DSLSensor::get_g_odr(float* odr) 00555 { 00556 LSM6DSL_ACC_GYRO_ODR_G_t odr_low_level; 00557 00558 if ( LSM6DSL_ACC_GYRO_R_ODR_G( (void *)this, &odr_low_level ) == MEMS_ERROR ) 00559 { 00560 return 1; 00561 } 00562 00563 switch( odr_low_level ) 00564 { 00565 case LSM6DSL_ACC_GYRO_ODR_G_POWER_DOWN: 00566 *odr = 0.0f; 00567 break; 00568 case LSM6DSL_ACC_GYRO_ODR_G_13Hz: 00569 *odr = 13.0f; 00570 break; 00571 case LSM6DSL_ACC_GYRO_ODR_G_26Hz: 00572 *odr = 26.0f; 00573 break; 00574 case LSM6DSL_ACC_GYRO_ODR_G_52Hz: 00575 *odr = 52.0f; 00576 break; 00577 case LSM6DSL_ACC_GYRO_ODR_G_104Hz: 00578 *odr = 104.0f; 00579 break; 00580 case LSM6DSL_ACC_GYRO_ODR_G_208Hz: 00581 *odr = 208.0f; 00582 break; 00583 case LSM6DSL_ACC_GYRO_ODR_G_416Hz: 00584 *odr = 416.0f; 00585 break; 00586 case LSM6DSL_ACC_GYRO_ODR_G_833Hz: 00587 *odr = 833.0f; 00588 break; 00589 case LSM6DSL_ACC_GYRO_ODR_G_1660Hz: 00590 *odr = 1660.0f; 00591 break; 00592 case LSM6DSL_ACC_GYRO_ODR_G_3330Hz: 00593 *odr = 3330.0f; 00594 break; 00595 case LSM6DSL_ACC_GYRO_ODR_G_6660Hz: 00596 *odr = 6660.0f; 00597 break; 00598 default: 00599 *odr = -1.0f; 00600 return 1; 00601 } 00602 00603 return 0; 00604 } 00605 00606 /** 00607 * @brief Set LSM6DSL Accelerometer output data rate 00608 * @param odr the output data rate to be set 00609 * @retval 0 in case of success, an error code otherwise 00610 */ 00611 int LSM6DSLSensor::set_x_odr(float odr) 00612 { 00613 if(_x_is_enabled == 1) 00614 { 00615 if(set_x_odr_when_enabled(odr) == 1) 00616 { 00617 return 1; 00618 } 00619 } 00620 else 00621 { 00622 if(set_x_odr_when_disabled(odr) == 1) 00623 { 00624 return 1; 00625 } 00626 } 00627 00628 return 0; 00629 } 00630 00631 /** 00632 * @brief Set LSM6DSL Accelerometer output data rate when enabled 00633 * @param odr the output data rate to be set 00634 * @retval 0 in case of success, an error code otherwise 00635 */ 00636 int LSM6DSLSensor::set_x_odr_when_enabled(float odr) 00637 { 00638 LSM6DSL_ACC_GYRO_ODR_XL_t new_odr; 00639 00640 new_odr = ( odr <= 13.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_13Hz 00641 : ( odr <= 26.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_26Hz 00642 : ( odr <= 52.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_52Hz 00643 : ( odr <= 104.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_104Hz 00644 : ( odr <= 208.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_208Hz 00645 : ( odr <= 416.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_416Hz 00646 : ( odr <= 833.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_833Hz 00647 : ( odr <= 1660.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_1660Hz 00648 : ( odr <= 3330.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_3330Hz 00649 : LSM6DSL_ACC_GYRO_ODR_XL_6660Hz; 00650 00651 if ( LSM6DSL_ACC_GYRO_W_ODR_XL( (void *)this, new_odr ) == MEMS_ERROR ) 00652 { 00653 return 1; 00654 } 00655 00656 return 0; 00657 } 00658 00659 /** 00660 * @brief Set LSM6DSL Accelerometer output data rate when disabled 00661 * @param odr the output data rate to be set 00662 * @retval 0 in case of success, an error code otherwise 00663 */ 00664 int LSM6DSLSensor::set_x_odr_when_disabled(float odr) 00665 { 00666 _x_last_odr = ( odr <= 13.0f ) ? 13.0f 00667 : ( odr <= 26.0f ) ? 26.0f 00668 : ( odr <= 52.0f ) ? 52.0f 00669 : ( odr <= 104.0f ) ? 104.0f 00670 : ( odr <= 208.0f ) ? 208.0f 00671 : ( odr <= 416.0f ) ? 416.0f 00672 : ( odr <= 833.0f ) ? 833.0f 00673 : ( odr <= 1660.0f ) ? 1660.0f 00674 : ( odr <= 3330.0f ) ? 3330.0f 00675 : 6660.0f; 00676 00677 return 0; 00678 } 00679 00680 /** 00681 * @brief Set LSM6DSL Gyroscope output data rate 00682 * @param odr the output data rate to be set 00683 * @retval 0 in case of success, an error code otherwise 00684 */ 00685 int LSM6DSLSensor::set_g_odr(float odr) 00686 { 00687 if(_g_is_enabled == 1) 00688 { 00689 if(set_g_odr_when_enabled(odr) == 1) 00690 { 00691 return 1; 00692 } 00693 } 00694 else 00695 { 00696 if(set_g_odr_when_disabled(odr) == 1) 00697 { 00698 return 1; 00699 } 00700 } 00701 00702 return 0; 00703 } 00704 00705 /** 00706 * @brief Set LSM6DSL Gyroscope output data rate when enabled 00707 * @param odr the output data rate to be set 00708 * @retval 0 in case of success, an error code otherwise 00709 */ 00710 int LSM6DSLSensor::set_g_odr_when_enabled(float odr) 00711 { 00712 LSM6DSL_ACC_GYRO_ODR_G_t new_odr; 00713 00714 new_odr = ( odr <= 13.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_13Hz 00715 : ( odr <= 26.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_26Hz 00716 : ( odr <= 52.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_52Hz 00717 : ( odr <= 104.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_104Hz 00718 : ( odr <= 208.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_208Hz 00719 : ( odr <= 416.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_416Hz 00720 : ( odr <= 833.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_833Hz 00721 : ( odr <= 1660.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_1660Hz 00722 : ( odr <= 3330.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_3330Hz 00723 : LSM6DSL_ACC_GYRO_ODR_G_6660Hz; 00724 00725 if ( LSM6DSL_ACC_GYRO_W_ODR_G( (void *)this, new_odr ) == MEMS_ERROR ) 00726 { 00727 return 1; 00728 } 00729 00730 return 0; 00731 } 00732 00733 /** 00734 * @brief Set LSM6DSL Gyroscope output data rate when disabled 00735 * @param odr the output data rate to be set 00736 * @retval 0 in case of success, an error code otherwise 00737 */ 00738 int LSM6DSLSensor::set_g_odr_when_disabled(float odr) 00739 { 00740 _g_last_odr = ( odr <= 13.0f ) ? 13.0f 00741 : ( odr <= 26.0f ) ? 26.0f 00742 : ( odr <= 52.0f ) ? 52.0f 00743 : ( odr <= 104.0f ) ? 104.0f 00744 : ( odr <= 208.0f ) ? 208.0f 00745 : ( odr <= 416.0f ) ? 416.0f 00746 : ( odr <= 833.0f ) ? 833.0f 00747 : ( odr <= 1660.0f ) ? 1660.0f 00748 : ( odr <= 3330.0f ) ? 3330.0f 00749 : 6660.0f; 00750 00751 return 0; 00752 } 00753 00754 /** 00755 * @brief Read LSM6DSL Accelerometer full scale 00756 * @param fullScale the pointer to the full scale 00757 * @retval 0 in case of success, an error code otherwise 00758 */ 00759 int LSM6DSLSensor::get_x_fs(float* fullScale) 00760 { 00761 LSM6DSL_ACC_GYRO_FS_XL_t fs_low_level; 00762 00763 if ( LSM6DSL_ACC_GYRO_R_FS_XL( (void *)this, &fs_low_level ) == MEMS_ERROR ) 00764 { 00765 return 1; 00766 } 00767 00768 switch( fs_low_level ) 00769 { 00770 case LSM6DSL_ACC_GYRO_FS_XL_2g: 00771 *fullScale = 2.0f; 00772 break; 00773 case LSM6DSL_ACC_GYRO_FS_XL_4g: 00774 *fullScale = 4.0f; 00775 break; 00776 case LSM6DSL_ACC_GYRO_FS_XL_8g: 00777 *fullScale = 8.0f; 00778 break; 00779 case LSM6DSL_ACC_GYRO_FS_XL_16g: 00780 *fullScale = 16.0f; 00781 break; 00782 default: 00783 *fullScale = -1.0f; 00784 return 1; 00785 } 00786 00787 return 0; 00788 } 00789 00790 /** 00791 * @brief Read LSM6DSL Gyroscope full scale 00792 * @param fullScale the pointer to the full scale 00793 * @retval 0 in case of success, an error code otherwise 00794 */ 00795 int LSM6DSLSensor::get_g_fs(float* fullScale) 00796 { 00797 LSM6DSL_ACC_GYRO_FS_G_t fs_low_level; 00798 LSM6DSL_ACC_GYRO_FS_125_t fs_125; 00799 00800 if ( LSM6DSL_ACC_GYRO_R_FS_125( (void *)this, &fs_125 ) == MEMS_ERROR ) 00801 { 00802 return 1; 00803 } 00804 if ( LSM6DSL_ACC_GYRO_R_FS_G( (void *)this, &fs_low_level ) == MEMS_ERROR ) 00805 { 00806 return 1; 00807 } 00808 00809 if ( fs_125 == LSM6DSL_ACC_GYRO_FS_125_ENABLED ) 00810 { 00811 *fullScale = 125.0f; 00812 } 00813 00814 else 00815 { 00816 switch( fs_low_level ) 00817 { 00818 case LSM6DSL_ACC_GYRO_FS_G_245dps: 00819 *fullScale = 245.0f; 00820 break; 00821 case LSM6DSL_ACC_GYRO_FS_G_500dps: 00822 *fullScale = 500.0f; 00823 break; 00824 case LSM6DSL_ACC_GYRO_FS_G_1000dps: 00825 *fullScale = 1000.0f; 00826 break; 00827 case LSM6DSL_ACC_GYRO_FS_G_2000dps: 00828 *fullScale = 2000.0f; 00829 break; 00830 default: 00831 *fullScale = -1.0f; 00832 return 1; 00833 } 00834 } 00835 00836 return 0; 00837 } 00838 00839 /** 00840 * @brief Set LSM6DSL Accelerometer full scale 00841 * @param fullScale the full scale to be set 00842 * @retval 0 in case of success, an error code otherwise 00843 */ 00844 int LSM6DSLSensor::set_x_fs(float fullScale) 00845 { 00846 LSM6DSL_ACC_GYRO_FS_XL_t new_fs; 00847 00848 new_fs = ( fullScale <= 2.0f ) ? LSM6DSL_ACC_GYRO_FS_XL_2g 00849 : ( fullScale <= 4.0f ) ? LSM6DSL_ACC_GYRO_FS_XL_4g 00850 : ( fullScale <= 8.0f ) ? LSM6DSL_ACC_GYRO_FS_XL_8g 00851 : LSM6DSL_ACC_GYRO_FS_XL_16g; 00852 00853 if ( LSM6DSL_ACC_GYRO_W_FS_XL( (void *)this, new_fs ) == MEMS_ERROR ) 00854 { 00855 return 1; 00856 } 00857 00858 return 0; 00859 } 00860 00861 /** 00862 * @brief Set LSM6DSL Gyroscope full scale 00863 * @param fullScale the full scale to be set 00864 * @retval 0 in case of success, an error code otherwise 00865 */ 00866 int LSM6DSLSensor::set_g_fs(float fullScale) 00867 { 00868 LSM6DSL_ACC_GYRO_FS_G_t new_fs; 00869 00870 if ( fullScale <= 125.0f ) 00871 { 00872 if ( LSM6DSL_ACC_GYRO_W_FS_125( (void *)this, LSM6DSL_ACC_GYRO_FS_125_ENABLED ) == MEMS_ERROR ) 00873 { 00874 return 1; 00875 } 00876 } 00877 else 00878 { 00879 new_fs = ( fullScale <= 245.0f ) ? LSM6DSL_ACC_GYRO_FS_G_245dps 00880 : ( fullScale <= 500.0f ) ? LSM6DSL_ACC_GYRO_FS_G_500dps 00881 : ( fullScale <= 1000.0f ) ? LSM6DSL_ACC_GYRO_FS_G_1000dps 00882 : LSM6DSL_ACC_GYRO_FS_G_2000dps; 00883 00884 if ( LSM6DSL_ACC_GYRO_W_FS_125( (void *)this, LSM6DSL_ACC_GYRO_FS_125_DISABLED ) == MEMS_ERROR ) 00885 { 00886 return 1; 00887 } 00888 if ( LSM6DSL_ACC_GYRO_W_FS_G( (void *)this, new_fs ) == MEMS_ERROR ) 00889 { 00890 return 1; 00891 } 00892 } 00893 00894 return 0; 00895 } 00896 00897 /** 00898 * @brief Enable free fall detection 00899 * @param pin the interrupt pin to be used 00900 * @note This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g 00901 * @retval 0 in case of success, an error code otherwise 00902 */ 00903 int LSM6DSLSensor::enable_free_fall_detection(LSM6DSL_Interrupt_Pin_t pin) 00904 { 00905 /* Output Data Rate selection */ 00906 if(set_x_odr(416.0f) == 1) 00907 { 00908 return 1; 00909 } 00910 00911 /* Full scale selection */ 00912 if ( LSM6DSL_ACC_GYRO_W_FS_XL( (void *)this, LSM6DSL_ACC_GYRO_FS_XL_2g ) == MEMS_ERROR ) 00913 { 00914 return 1; 00915 } 00916 00917 /* FF_DUR setting */ 00918 if ( LSM6DSL_ACC_GYRO_W_FF_Duration( (void *)this, 0x06 ) == MEMS_ERROR ) 00919 { 00920 return 1; 00921 } 00922 00923 /* WAKE_DUR setting */ 00924 if ( LSM6DSL_ACC_GYRO_W_WAKE_DUR( (void *)this, 0x00 ) == MEMS_ERROR ) 00925 { 00926 return 1; 00927 } 00928 00929 /* TIMER_HR setting */ 00930 if ( LSM6DSL_ACC_GYRO_W_TIMER_HR( (void *)this, LSM6DSL_ACC_GYRO_TIMER_HR_6_4ms ) == MEMS_ERROR ) 00931 { 00932 return 1; 00933 } 00934 00935 /* SLEEP_DUR setting */ 00936 if ( LSM6DSL_ACC_GYRO_W_SLEEP_DUR( (void *)this, 0x00 ) == MEMS_ERROR ) 00937 { 00938 return 1; 00939 } 00940 00941 /* FF_THS setting */ 00942 if ( LSM6DSL_ACC_GYRO_W_FF_THS( (void *)this, LSM6DSL_ACC_GYRO_FF_THS_312mg ) == MEMS_ERROR ) 00943 { 00944 return 1; 00945 } 00946 00947 /* Enable basic Interrupts */ 00948 if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_ENABLED ) == MEMS_ERROR ) 00949 { 00950 return 1; 00951 } 00952 00953 /* Enable free fall event on either INT1 or INT2 pin */ 00954 switch (pin) 00955 { 00956 case LSM6DSL_INT1_PIN: 00957 if ( LSM6DSL_ACC_GYRO_W_FFEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_FF_ENABLED ) == MEMS_ERROR ) 00958 { 00959 return 1; 00960 } 00961 break; 00962 00963 case LSM6DSL_INT2_PIN: 00964 if ( LSM6DSL_ACC_GYRO_W_FFEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_FF_ENABLED ) == MEMS_ERROR ) 00965 { 00966 return 1; 00967 } 00968 break; 00969 00970 default: 00971 return 1; 00972 } 00973 00974 return 0; 00975 } 00976 00977 /** 00978 * @brief Disable free fall detection 00979 * @param None 00980 * @retval 0 in case of success, an error code otherwise 00981 */ 00982 int LSM6DSLSensor::disable_free_fall_detection(void) 00983 { 00984 /* Disable free fall event on INT1 pin */ 00985 if ( LSM6DSL_ACC_GYRO_W_FFEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_FF_DISABLED ) == MEMS_ERROR ) 00986 { 00987 return 1; 00988 } 00989 00990 /* Disable free fall event on INT2 pin */ 00991 if ( LSM6DSL_ACC_GYRO_W_FFEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_FF_DISABLED ) == MEMS_ERROR ) 00992 { 00993 return 1; 00994 } 00995 00996 /* Disable basic Interrupts */ 00997 if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_DISABLED ) == MEMS_ERROR ) 00998 { 00999 return 1; 01000 } 01001 01002 /* FF_DUR setting */ 01003 if ( LSM6DSL_ACC_GYRO_W_FF_Duration( (void *)this, 0x00 ) == MEMS_ERROR ) 01004 { 01005 return 1; 01006 } 01007 01008 /* FF_THS setting */ 01009 if ( LSM6DSL_ACC_GYRO_W_FF_THS( (void *)this, LSM6DSL_ACC_GYRO_FF_THS_156mg ) == MEMS_ERROR ) 01010 { 01011 return 1; 01012 } 01013 01014 return 0; 01015 } 01016 01017 /** 01018 * @brief Set the free fall detection threshold for LSM6DSL accelerometer sensor 01019 * @param thr the threshold to be set 01020 * @retval 0 in case of success, an error code otherwise 01021 */ 01022 int LSM6DSLSensor::set_free_fall_threshold(uint8_t thr) 01023 { 01024 01025 if ( LSM6DSL_ACC_GYRO_W_FF_THS( (void *)this, (LSM6DSL_ACC_GYRO_FF_THS_t)thr ) == MEMS_ERROR ) 01026 { 01027 return 1; 01028 } 01029 01030 return 0; 01031 } 01032 01033 /** 01034 * @brief Enable the pedometer feature for LSM6DSL accelerometer sensor 01035 * @note This function sets the LSM6DSL accelerometer ODR to 26Hz and the LSM6DSL accelerometer full scale to 2g 01036 * @retval 0 in case of success, an error code otherwise 01037 */ 01038 int LSM6DSLSensor::enable_pedometer(void) 01039 { 01040 /* Output Data Rate selection */ 01041 if( set_x_odr(26.0f) == 1 ) 01042 { 01043 return 1; 01044 } 01045 01046 /* Full scale selection. */ 01047 if( set_x_fs(2.0f) == 1 ) 01048 { 01049 return 1; 01050 } 01051 01052 /* Set pedometer threshold. */ 01053 if ( set_pedometer_threshold(LSM6DSL_PEDOMETER_THRESHOLD_MID_HIGH) == 1 ) 01054 { 01055 return 1; 01056 } 01057 01058 /* Enable embedded functionalities. */ 01059 if ( LSM6DSL_ACC_GYRO_W_FUNC_EN( (void *)this, LSM6DSL_ACC_GYRO_FUNC_EN_ENABLED ) == MEMS_ERROR ) 01060 { 01061 return 1; 01062 } 01063 01064 /* Enable pedometer algorithm. */ 01065 if ( LSM6DSL_ACC_GYRO_W_PEDO( (void *)this, LSM6DSL_ACC_GYRO_PEDO_ENABLED ) == MEMS_ERROR ) 01066 { 01067 return 1; 01068 } 01069 01070 /* Enable pedometer on INT1. */ 01071 if ( LSM6DSL_ACC_GYRO_W_STEP_DET_on_INT1( (void *)this, LSM6DSL_ACC_GYRO_INT1_PEDO_ENABLED ) == MEMS_ERROR ) 01072 { 01073 return 1; 01074 } 01075 01076 return 0; 01077 } 01078 01079 /** 01080 * @brief Disable the pedometer feature for LSM6DSL accelerometer sensor 01081 * @retval 0 in case of success, an error code otherwise 01082 */ 01083 int LSM6DSLSensor::disable_pedometer(void) 01084 { 01085 /* Disable pedometer on INT1. */ 01086 if ( LSM6DSL_ACC_GYRO_W_STEP_DET_on_INT1( (void *)this, LSM6DSL_ACC_GYRO_INT1_PEDO_DISABLED ) == MEMS_ERROR ) 01087 { 01088 return 1; 01089 } 01090 01091 /* Disable pedometer algorithm. */ 01092 if ( LSM6DSL_ACC_GYRO_W_PEDO( (void *)this, LSM6DSL_ACC_GYRO_PEDO_DISABLED ) == MEMS_ERROR ) 01093 { 01094 return 1; 01095 } 01096 01097 /* Disable embedded functionalities. */ 01098 if ( LSM6DSL_ACC_GYRO_W_FUNC_EN( (void *)this, LSM6DSL_ACC_GYRO_FUNC_EN_DISABLED ) == MEMS_ERROR ) 01099 { 01100 return 1; 01101 } 01102 01103 /* Reset pedometer threshold. */ 01104 if ( set_pedometer_threshold(0x0) == 1 ) 01105 { 01106 return 1; 01107 } 01108 01109 return 0; 01110 } 01111 01112 /** 01113 * @brief Get the step counter for LSM6DSL accelerometer sensor 01114 * @param step_count the pointer to the step counter 01115 * @retval 0 in case of success, an error code otherwise 01116 */ 01117 int LSM6DSLSensor::get_step_counter(uint16_t *step_count) 01118 { 01119 if ( LSM6DSL_ACC_GYRO_Get_GetStepCounter( (void *)this, ( uint8_t* )step_count ) == MEMS_ERROR ) 01120 { 01121 return 1; 01122 } 01123 01124 return 0; 01125 } 01126 01127 /** 01128 * @brief Reset of the step counter for LSM6DSL accelerometer sensor 01129 * @retval 0 in case of success, an error code otherwise 01130 */ 01131 int LSM6DSLSensor::reset_step_counter(void) 01132 { 01133 if ( LSM6DSL_ACC_GYRO_W_PedoStepReset( (void *)this, LSM6DSL_ACC_GYRO_PEDO_RST_STEP_ENABLED ) == MEMS_ERROR ) 01134 { 01135 return 1; 01136 } 01137 01138 wait_ms(10); 01139 01140 if ( LSM6DSL_ACC_GYRO_W_PedoStepReset( (void *)this, LSM6DSL_ACC_GYRO_PEDO_RST_STEP_DISABLED ) == MEMS_ERROR ) 01141 { 01142 return 1; 01143 } 01144 01145 return 0; 01146 } 01147 01148 /** 01149 * @brief Set the pedometer threshold for LSM6DSL accelerometer sensor 01150 * @param thr the threshold to be set 01151 * @retval 0 in case of success, an error code otherwise 01152 */ 01153 int LSM6DSLSensor::set_pedometer_threshold(uint8_t thr) 01154 { 01155 if ( LSM6DSL_ACC_GYRO_W_PedoThreshold( (void *)this, thr ) == MEMS_ERROR ) 01156 { 01157 return 1; 01158 } 01159 01160 return 0; 01161 } 01162 01163 /** 01164 * @brief Enable the tilt detection for LSM6DSL accelerometer sensor 01165 * @param pin the interrupt pin to be used 01166 * @note This function sets the LSM6DSL accelerometer ODR to 26Hz and the LSM6DSL accelerometer full scale to 2g 01167 * @retval 0 in case of success, an error code otherwise 01168 */ 01169 int LSM6DSLSensor::enable_tilt_detection(LSM6DSL_Interrupt_Pin_t pin) 01170 { 01171 /* Output Data Rate selection */ 01172 if( set_x_odr(26.0f) == 1 ) 01173 { 01174 return 1; 01175 } 01176 01177 /* Full scale selection. */ 01178 if( set_x_fs(2.0f) == 1 ) 01179 { 01180 return 1; 01181 } 01182 01183 /* Enable embedded functionalities */ 01184 if ( LSM6DSL_ACC_GYRO_W_FUNC_EN( (void *)this, LSM6DSL_ACC_GYRO_FUNC_EN_ENABLED ) == MEMS_ERROR ) 01185 { 01186 return 1; 01187 } 01188 01189 /* Enable tilt calculation. */ 01190 if ( LSM6DSL_ACC_GYRO_W_TILT( (void *)this, LSM6DSL_ACC_GYRO_TILT_ENABLED ) == MEMS_ERROR ) 01191 { 01192 return 1; 01193 } 01194 01195 /* Enable tilt detection on either INT1 or INT2 pin */ 01196 switch (pin) 01197 { 01198 case LSM6DSL_INT1_PIN: 01199 if ( LSM6DSL_ACC_GYRO_W_TiltEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_TILT_ENABLED ) == MEMS_ERROR ) 01200 { 01201 return 1; 01202 } 01203 break; 01204 01205 case LSM6DSL_INT2_PIN: 01206 if ( LSM6DSL_ACC_GYRO_W_TiltEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_TILT_ENABLED ) == MEMS_ERROR ) 01207 { 01208 return 1; 01209 } 01210 break; 01211 01212 default: 01213 return 1; 01214 } 01215 01216 return 0; 01217 } 01218 01219 /** 01220 * @brief Disable the tilt detection for LSM6DSL accelerometer sensor 01221 * @retval 0 in case of success, an error code otherwise 01222 */ 01223 int LSM6DSLSensor::disable_tilt_detection(void) 01224 { 01225 /* Disable tilt event on INT1. */ 01226 if ( LSM6DSL_ACC_GYRO_W_TiltEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_TILT_DISABLED ) == MEMS_ERROR ) 01227 { 01228 return 1; 01229 } 01230 01231 /* Disable tilt event on INT2. */ 01232 if ( LSM6DSL_ACC_GYRO_W_TiltEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_TILT_DISABLED ) == MEMS_ERROR ) 01233 { 01234 return 1; 01235 } 01236 01237 /* Disable tilt calculation. */ 01238 if ( LSM6DSL_ACC_GYRO_W_TILT( (void *)this, LSM6DSL_ACC_GYRO_TILT_DISABLED ) == MEMS_ERROR ) 01239 { 01240 return 1; 01241 } 01242 01243 /* Disable embedded functionalities */ 01244 if ( LSM6DSL_ACC_GYRO_W_FUNC_EN( (void *)this, LSM6DSL_ACC_GYRO_FUNC_EN_DISABLED ) == MEMS_ERROR ) 01245 { 01246 return 1; 01247 } 01248 01249 return 0; 01250 } 01251 01252 /** 01253 * @brief Enable the wake up detection for LSM6DSL accelerometer sensor 01254 * @param pin the interrupt pin to be used 01255 * @note This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g 01256 * @retval 0 in case of success, an error code otherwise 01257 */ 01258 int LSM6DSLSensor::enable_wake_up_detection(LSM6DSL_Interrupt_Pin_t pin) 01259 { 01260 /* Output Data Rate selection */ 01261 if( set_x_odr(416.0f) == 1 ) 01262 { 01263 return 1; 01264 } 01265 01266 /* Full scale selection. */ 01267 if( set_x_fs(2.0f) == 1 ) 01268 { 01269 return 1; 01270 } 01271 01272 /* WAKE_DUR setting */ 01273 if ( LSM6DSL_ACC_GYRO_W_WAKE_DUR( (void *)this, 0x00 ) == MEMS_ERROR ) 01274 { 01275 return 1; 01276 } 01277 01278 /* Set wake up threshold. */ 01279 if ( LSM6DSL_ACC_GYRO_W_WK_THS( (void *)this, 0x02 ) == MEMS_ERROR ) 01280 { 01281 return 1; 01282 } 01283 01284 /* Enable basic Interrupts */ 01285 if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_ENABLED ) == MEMS_ERROR ) 01286 { 01287 return 1; 01288 } 01289 01290 /* Enable wake up detection on either INT1 or INT2 pin */ 01291 switch (pin) 01292 { 01293 case LSM6DSL_INT1_PIN: 01294 if ( LSM6DSL_ACC_GYRO_W_WUEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_WU_ENABLED ) == MEMS_ERROR ) 01295 { 01296 return 1; 01297 } 01298 break; 01299 01300 case LSM6DSL_INT2_PIN: 01301 if ( LSM6DSL_ACC_GYRO_W_WUEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_WU_ENABLED ) == MEMS_ERROR ) 01302 { 01303 return 1; 01304 } 01305 break; 01306 01307 default: 01308 return 1; 01309 } 01310 01311 return 0; 01312 } 01313 01314 /** 01315 * @brief Disable the wake up detection for LSM6DSL accelerometer sensor 01316 * @retval 0 in case of success, an error code otherwise 01317 */ 01318 int LSM6DSLSensor::disable_wake_up_detection(void) 01319 { 01320 /* Disable wake up event on INT1 */ 01321 if ( LSM6DSL_ACC_GYRO_W_WUEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_WU_DISABLED ) == MEMS_ERROR ) 01322 { 01323 return 1; 01324 } 01325 01326 /* Disable wake up event on INT2 */ 01327 if ( LSM6DSL_ACC_GYRO_W_WUEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_WU_DISABLED ) == MEMS_ERROR ) 01328 { 01329 return 1; 01330 } 01331 01332 /* Disable basic Interrupts */ 01333 if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_DISABLED ) == MEMS_ERROR ) 01334 { 01335 return 1; 01336 } 01337 01338 /* WU_DUR setting */ 01339 if ( LSM6DSL_ACC_GYRO_W_WAKE_DUR( (void *)this, 0x00 ) == MEMS_ERROR ) 01340 { 01341 return 1; 01342 } 01343 01344 /* WU_THS setting */ 01345 if ( LSM6DSL_ACC_GYRO_W_WK_THS( (void *)this, 0x00 ) == MEMS_ERROR ) 01346 { 01347 return 1; 01348 } 01349 01350 return 0; 01351 } 01352 01353 /** 01354 * @brief Set the wake up threshold for LSM6DSL accelerometer sensor 01355 * @param thr the threshold to be set 01356 * @retval 0 in case of success, an error code otherwise 01357 */ 01358 int LSM6DSLSensor::set_wake_up_threshold(uint8_t thr) 01359 { 01360 if ( LSM6DSL_ACC_GYRO_W_WK_THS( (void *)this, thr ) == MEMS_ERROR ) 01361 { 01362 return 1; 01363 } 01364 01365 return 0; 01366 } 01367 01368 /** 01369 * @brief Enable the single tap detection for LSM6DSL accelerometer sensor 01370 * @param pin the interrupt pin to be used 01371 * @note This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g 01372 * @retval 0 in case of success, an error code otherwise 01373 */ 01374 int LSM6DSLSensor::enable_single_tap_detection(LSM6DSL_Interrupt_Pin_t pin) 01375 { 01376 /* Output Data Rate selection */ 01377 if( set_x_odr(416.0f) == 1 ) 01378 { 01379 return 1; 01380 } 01381 01382 /* Full scale selection. */ 01383 if( set_x_fs(2.0f) == 1 ) 01384 { 01385 return 1; 01386 } 01387 01388 /* Enable X direction in tap recognition. */ 01389 if ( LSM6DSL_ACC_GYRO_W_TAP_X_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_X_EN_ENABLED ) == MEMS_ERROR ) 01390 { 01391 return 1; 01392 } 01393 01394 /* Enable Y direction in tap recognition. */ 01395 if ( LSM6DSL_ACC_GYRO_W_TAP_Y_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Y_EN_ENABLED ) == MEMS_ERROR ) 01396 { 01397 return 1; 01398 } 01399 01400 /* Enable Z direction in tap recognition. */ 01401 if ( LSM6DSL_ACC_GYRO_W_TAP_Z_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Z_EN_ENABLED ) == MEMS_ERROR ) 01402 { 01403 return 1; 01404 } 01405 01406 /* Set tap threshold. */ 01407 if ( set_tap_threshold( LSM6DSL_TAP_THRESHOLD_MID_LOW ) == 1 ) 01408 { 01409 return 1; 01410 } 01411 01412 /* Set tap shock time window. */ 01413 if ( set_tap_shock_time( LSM6DSL_TAP_SHOCK_TIME_MID_HIGH ) == 1 ) 01414 { 01415 return 1; 01416 } 01417 01418 /* Set tap quiet time window. */ 01419 if ( set_tap_quiet_time( LSM6DSL_TAP_QUIET_TIME_MID_LOW ) == 1 ) 01420 { 01421 return 1; 01422 } 01423 01424 /* _NOTE_: Tap duration time window - don't care for single tap. */ 01425 01426 /* _NOTE_: Single/Double Tap event - don't care of this flag for single tap. */ 01427 01428 /* Enable basic Interrupts */ 01429 if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_ENABLED ) == MEMS_ERROR ) 01430 { 01431 return 1; 01432 } 01433 01434 /* Enable single tap on either INT1 or INT2 pin */ 01435 switch (pin) 01436 { 01437 case LSM6DSL_INT1_PIN: 01438 if ( LSM6DSL_ACC_GYRO_W_SingleTapOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_SINGLE_TAP_ENABLED ) == MEMS_ERROR ) 01439 { 01440 return 1; 01441 } 01442 break; 01443 01444 case LSM6DSL_INT2_PIN: 01445 if ( LSM6DSL_ACC_GYRO_W_SingleTapOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_SINGLE_TAP_ENABLED ) == MEMS_ERROR ) 01446 { 01447 return 1; 01448 } 01449 break; 01450 01451 default: 01452 return 1; 01453 } 01454 01455 return 0; 01456 } 01457 01458 /** 01459 * @brief Disable the single tap detection for LSM6DSL accelerometer sensor 01460 * @retval 0 in case of success, an error code otherwise 01461 */ 01462 int LSM6DSLSensor::disable_single_tap_detection(void) 01463 { 01464 /* Disable single tap interrupt on INT1 pin. */ 01465 if ( LSM6DSL_ACC_GYRO_W_SingleTapOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_SINGLE_TAP_DISABLED ) == MEMS_ERROR ) 01466 { 01467 return 1; 01468 } 01469 01470 /* Disable single tap interrupt on INT2 pin. */ 01471 if ( LSM6DSL_ACC_GYRO_W_SingleTapOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_SINGLE_TAP_DISABLED ) == MEMS_ERROR ) 01472 { 01473 return 1; 01474 } 01475 01476 /* Disable basic Interrupts */ 01477 if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_DISABLED ) == MEMS_ERROR ) 01478 { 01479 return 1; 01480 } 01481 01482 /* Reset tap threshold. */ 01483 if ( set_tap_threshold( 0x0 ) == 1 ) 01484 { 01485 return 1; 01486 } 01487 01488 /* Reset tap shock time window. */ 01489 if ( set_tap_shock_time( 0x0 ) == 1 ) 01490 { 01491 return 1; 01492 } 01493 01494 /* Reset tap quiet time window. */ 01495 if ( set_tap_quiet_time( 0x0 ) == 1 ) 01496 { 01497 return 1; 01498 } 01499 01500 /* _NOTE_: Tap duration time window - don't care for single tap. */ 01501 01502 /* _NOTE_: Single/Double Tap event - don't care of this flag for single tap. */ 01503 01504 /* Disable Z direction in tap recognition. */ 01505 if ( LSM6DSL_ACC_GYRO_W_TAP_Z_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Z_EN_DISABLED ) == MEMS_ERROR ) 01506 { 01507 return 1; 01508 } 01509 01510 /* Disable Y direction in tap recognition. */ 01511 if ( LSM6DSL_ACC_GYRO_W_TAP_Y_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Y_EN_DISABLED ) == MEMS_ERROR ) 01512 { 01513 return 1; 01514 } 01515 01516 /* Disable X direction in tap recognition. */ 01517 if ( LSM6DSL_ACC_GYRO_W_TAP_X_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_X_EN_DISABLED ) == MEMS_ERROR ) 01518 { 01519 return 1; 01520 } 01521 01522 return 0; 01523 } 01524 01525 /** 01526 * @brief Enable the double tap detection for LSM6DSL accelerometer sensor 01527 * @param pin the interrupt pin to be used 01528 * @note This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g 01529 * @retval 0 in case of success, an error code otherwise 01530 */ 01531 int LSM6DSLSensor::enable_double_tap_detection(LSM6DSL_Interrupt_Pin_t pin) 01532 { 01533 /* Output Data Rate selection */ 01534 if( set_x_odr(416.0f) == 1 ) 01535 { 01536 return 1; 01537 } 01538 01539 /* Full scale selection. */ 01540 if( set_x_fs(2.0f) == 1 ) 01541 { 01542 return 1; 01543 } 01544 01545 /* Enable X direction in tap recognition. */ 01546 if ( LSM6DSL_ACC_GYRO_W_TAP_X_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_X_EN_ENABLED ) == MEMS_ERROR ) 01547 { 01548 return 1; 01549 } 01550 01551 /* Enable Y direction in tap recognition. */ 01552 if ( LSM6DSL_ACC_GYRO_W_TAP_Y_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Y_EN_ENABLED ) == MEMS_ERROR ) 01553 { 01554 return 1; 01555 } 01556 01557 /* Enable Z direction in tap recognition. */ 01558 if ( LSM6DSL_ACC_GYRO_W_TAP_Z_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Z_EN_ENABLED ) == MEMS_ERROR ) 01559 { 01560 return 1; 01561 } 01562 01563 /* Set tap threshold. */ 01564 if ( set_tap_threshold( LSM6DSL_TAP_THRESHOLD_MID_LOW ) == 1 ) 01565 { 01566 return 1; 01567 } 01568 01569 /* Set tap shock time window. */ 01570 if ( set_tap_shock_time( LSM6DSL_TAP_SHOCK_TIME_HIGH ) == 1 ) 01571 { 01572 return 1; 01573 } 01574 01575 /* Set tap quiet time window. */ 01576 if ( set_tap_quiet_time( LSM6DSL_TAP_QUIET_TIME_HIGH ) == 1 ) 01577 { 01578 return 1; 01579 } 01580 01581 /* Set tap duration time window. */ 01582 if ( set_tap_duration_time( LSM6DSL_TAP_DURATION_TIME_MID ) == 1 ) 01583 { 01584 return 1; 01585 } 01586 01587 /* Single and double tap enabled. */ 01588 if ( LSM6DSL_ACC_GYRO_W_SINGLE_DOUBLE_TAP_EV( (void *)this, LSM6DSL_ACC_GYRO_SINGLE_DOUBLE_TAP_DOUBLE_TAP ) == MEMS_ERROR ) 01589 { 01590 return 1; 01591 } 01592 01593 /* Enable basic Interrupts */ 01594 if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_ENABLED ) == MEMS_ERROR ) 01595 { 01596 return 1; 01597 } 01598 01599 /* Enable double tap on either INT1 or INT2 pin */ 01600 switch (pin) 01601 { 01602 case LSM6DSL_INT1_PIN: 01603 if ( LSM6DSL_ACC_GYRO_W_TapEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_TAP_ENABLED ) == MEMS_ERROR ) 01604 { 01605 return 1; 01606 } 01607 break; 01608 01609 case LSM6DSL_INT2_PIN: 01610 if ( LSM6DSL_ACC_GYRO_W_TapEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_TAP_ENABLED ) == MEMS_ERROR ) 01611 { 01612 return 1; 01613 } 01614 break; 01615 01616 default: 01617 return 1; 01618 } 01619 01620 return 0; 01621 } 01622 01623 /** 01624 * @brief Disable the double tap detection for LSM6DSL accelerometer sensor 01625 * @retval 0 in case of success, an error code otherwise 01626 */ 01627 int LSM6DSLSensor::disable_double_tap_detection(void) 01628 { 01629 /* Disable double tap interrupt on INT1 pin. */ 01630 if ( LSM6DSL_ACC_GYRO_W_TapEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_TAP_DISABLED ) == MEMS_ERROR ) 01631 { 01632 return 1; 01633 } 01634 01635 /* Disable double tap interrupt on INT2 pin. */ 01636 if ( LSM6DSL_ACC_GYRO_W_TapEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_TAP_DISABLED ) == MEMS_ERROR ) 01637 { 01638 return 1; 01639 } 01640 01641 /* Disable basic Interrupts */ 01642 if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_DISABLED ) == MEMS_ERROR ) 01643 { 01644 return 1; 01645 } 01646 01647 /* Reset tap threshold. */ 01648 if ( set_tap_threshold( 0x0 ) == 1 ) 01649 { 01650 return 1; 01651 } 01652 01653 /* Reset tap shock time window. */ 01654 if ( set_tap_shock_time( 0x0 ) == 1 ) 01655 { 01656 return 1; 01657 } 01658 01659 /* Reset tap quiet time window. */ 01660 if ( set_tap_quiet_time( 0x0 ) == 1 ) 01661 { 01662 return 1; 01663 } 01664 01665 /* Reset tap duration time window. */ 01666 if ( set_tap_duration_time( 0x0 ) == 1 ) 01667 { 01668 return 1; 01669 } 01670 01671 /* Only single tap enabled. */ 01672 if ( LSM6DSL_ACC_GYRO_W_SINGLE_DOUBLE_TAP_EV( (void *)this, LSM6DSL_ACC_GYRO_SINGLE_DOUBLE_TAP_SINGLE_TAP ) == MEMS_ERROR ) 01673 { 01674 return 1; 01675 } 01676 01677 /* Disable Z direction in tap recognition. */ 01678 if ( LSM6DSL_ACC_GYRO_W_TAP_Z_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Z_EN_DISABLED ) == MEMS_ERROR ) 01679 { 01680 return 1; 01681 } 01682 01683 /* Disable Y direction in tap recognition. */ 01684 if ( LSM6DSL_ACC_GYRO_W_TAP_Y_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Y_EN_DISABLED ) == MEMS_ERROR ) 01685 { 01686 return 1; 01687 } 01688 01689 /* Disable X direction in tap recognition. */ 01690 if ( LSM6DSL_ACC_GYRO_W_TAP_X_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_X_EN_DISABLED ) == MEMS_ERROR ) 01691 { 01692 return 1; 01693 } 01694 01695 return 0; 01696 } 01697 01698 /** 01699 * @brief Set the tap threshold for LSM6DSL accelerometer sensor 01700 * @param thr the threshold to be set 01701 * @retval 0 in case of success, an error code otherwise 01702 */ 01703 int LSM6DSLSensor::set_tap_threshold(uint8_t thr) 01704 { 01705 if ( LSM6DSL_ACC_GYRO_W_TAP_THS( (void *)this, thr ) == MEMS_ERROR ) 01706 { 01707 return 1; 01708 } 01709 01710 return 0; 01711 } 01712 01713 /** 01714 * @brief Set the tap shock time window for LSM6DSL accelerometer sensor 01715 * @param time the shock time window to be set 01716 * @retval 0 in case of success, an error code otherwise 01717 */ 01718 int LSM6DSLSensor::set_tap_shock_time(uint8_t time) 01719 { 01720 if ( LSM6DSL_ACC_GYRO_W_SHOCK_Duration( (void *)this, time ) == MEMS_ERROR ) 01721 { 01722 return 1; 01723 } 01724 01725 return 0; 01726 } 01727 01728 /** 01729 * @brief Set the tap quiet time window for LSM6DSL accelerometer sensor 01730 * @param time the quiet time window to be set 01731 * @retval 0 in case of success, an error code otherwise 01732 */ 01733 int LSM6DSLSensor::set_tap_quiet_time(uint8_t time) 01734 { 01735 if ( LSM6DSL_ACC_GYRO_W_QUIET_Duration( (void *)this, time ) == MEMS_ERROR ) 01736 { 01737 return 1; 01738 } 01739 01740 return 0; 01741 } 01742 01743 /** 01744 * @brief Set the tap duration of the time window for LSM6DSL accelerometer sensor 01745 * @param time the duration of the time window to be set 01746 * @retval 0 in case of success, an error code otherwise 01747 */ 01748 int LSM6DSLSensor::set_tap_duration_time(uint8_t time) 01749 { 01750 if ( LSM6DSL_ACC_GYRO_W_DUR( (void *)this, time ) == MEMS_ERROR ) 01751 { 01752 return 1; 01753 } 01754 01755 return 0; 01756 } 01757 01758 /** 01759 * @brief Enable the 6D orientation detection for LSM6DSL accelerometer sensor 01760 * @param pin the interrupt pin to be used 01761 * @note This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g 01762 * @retval 0 in case of success, an error code otherwise 01763 */ 01764 int LSM6DSLSensor::enable_6d_orientation(LSM6DSL_Interrupt_Pin_t pin) 01765 { 01766 /* Output Data Rate selection */ 01767 if( set_x_odr(416.0f) == 1 ) 01768 { 01769 return 1; 01770 } 01771 01772 /* Full scale selection. */ 01773 if( set_x_fs(2.0f) == 1 ) 01774 { 01775 return 1; 01776 } 01777 01778 /* Set 6D threshold. */ 01779 if ( LSM6DSL_ACC_GYRO_W_SIXD_THS( (void *)this, LSM6DSL_ACC_GYRO_SIXD_THS_60_degree ) == MEMS_ERROR ) 01780 { 01781 return 1; 01782 } 01783 01784 /* Enable basic Interrupts */ 01785 if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_ENABLED ) == MEMS_ERROR ) 01786 { 01787 return 1; 01788 } 01789 01790 /* Enable 6D orientation on either INT1 or INT2 pin */ 01791 switch (pin) 01792 { 01793 case LSM6DSL_INT1_PIN: 01794 if ( LSM6DSL_ACC_GYRO_W_6DEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_6D_ENABLED ) == MEMS_ERROR ) 01795 { 01796 return 1; 01797 } 01798 break; 01799 01800 case LSM6DSL_INT2_PIN: 01801 if ( LSM6DSL_ACC_GYRO_W_6DEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_6D_ENABLED ) == MEMS_ERROR ) 01802 { 01803 return 1; 01804 } 01805 break; 01806 01807 default: 01808 return 1; 01809 } 01810 01811 return 0; 01812 } 01813 01814 /** 01815 * @brief Disable the 6D orientation detection for LSM6DSL accelerometer sensor 01816 * @retval 0 in case of success, an error code otherwise 01817 */ 01818 int LSM6DSLSensor::disable_6d_orientation(void) 01819 { 01820 /* Disable 6D orientation interrupt on INT1 pin. */ 01821 if ( LSM6DSL_ACC_GYRO_W_6DEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_6D_DISABLED ) == MEMS_ERROR ) 01822 { 01823 return 1; 01824 } 01825 01826 /* Disable 6D orientation interrupt on INT2 pin. */ 01827 if ( LSM6DSL_ACC_GYRO_W_6DEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_6D_DISABLED ) == MEMS_ERROR ) 01828 { 01829 return 1; 01830 } 01831 01832 /* Disable basic Interrupts */ 01833 if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_DISABLED ) == MEMS_ERROR ) 01834 { 01835 return 1; 01836 } 01837 01838 /* Reset 6D threshold. */ 01839 if ( LSM6DSL_ACC_GYRO_W_SIXD_THS( (void *)this, LSM6DSL_ACC_GYRO_SIXD_THS_80_degree ) == MEMS_ERROR ) 01840 { 01841 return 1; 01842 } 01843 01844 return 0; 01845 } 01846 01847 /** 01848 * @brief Get the 6D orientation XL axis for LSM6DSL accelerometer sensor 01849 * @param xl the pointer to the 6D orientation XL axis 01850 * @retval 0 in case of success, an error code otherwise 01851 */ 01852 int LSM6DSLSensor::get_6d_orientation_xl(uint8_t *xl) 01853 { 01854 LSM6DSL_ACC_GYRO_DSD_XL_t xl_raw; 01855 01856 if ( LSM6DSL_ACC_GYRO_R_DSD_XL( (void *)this, &xl_raw ) == MEMS_ERROR ) 01857 { 01858 return 1; 01859 } 01860 01861 switch( xl_raw ) 01862 { 01863 case LSM6DSL_ACC_GYRO_DSD_XL_DETECTED: 01864 *xl = 1; 01865 break; 01866 case LSM6DSL_ACC_GYRO_DSD_XL_NOT_DETECTED: 01867 *xl = 0; 01868 break; 01869 default: 01870 return 1; 01871 } 01872 01873 return 0; 01874 } 01875 01876 /** 01877 * @brief Get the 6D orientation XH axis for LSM6DSL accelerometer sensor 01878 * @param xh the pointer to the 6D orientation XH axis 01879 * @retval 0 in case of success, an error code otherwise 01880 */ 01881 int LSM6DSLSensor::get_6d_orientation_xh(uint8_t *xh) 01882 { 01883 LSM6DSL_ACC_GYRO_DSD_XH_t xh_raw; 01884 01885 if ( LSM6DSL_ACC_GYRO_R_DSD_XH( (void *)this, &xh_raw ) == MEMS_ERROR ) 01886 { 01887 return 1; 01888 } 01889 01890 switch( xh_raw ) 01891 { 01892 case LSM6DSL_ACC_GYRO_DSD_XH_DETECTED: 01893 *xh = 1; 01894 break; 01895 case LSM6DSL_ACC_GYRO_DSD_XH_NOT_DETECTED: 01896 *xh = 0; 01897 break; 01898 default: 01899 return 1; 01900 } 01901 01902 return 0; 01903 } 01904 01905 /** 01906 * @brief Get the 6D orientation YL axis for LSM6DSL accelerometer sensor 01907 * @param yl the pointer to the 6D orientation YL axis 01908 * @retval 0 in case of success, an error code otherwise 01909 */ 01910 int LSM6DSLSensor::get_6d_orientation_yl(uint8_t *yl) 01911 { 01912 LSM6DSL_ACC_GYRO_DSD_YL_t yl_raw; 01913 01914 if ( LSM6DSL_ACC_GYRO_R_DSD_YL( (void *)this, &yl_raw ) == MEMS_ERROR ) 01915 { 01916 return 1; 01917 } 01918 01919 switch( yl_raw ) 01920 { 01921 case LSM6DSL_ACC_GYRO_DSD_YL_DETECTED: 01922 *yl = 1; 01923 break; 01924 case LSM6DSL_ACC_GYRO_DSD_YL_NOT_DETECTED: 01925 *yl = 0; 01926 break; 01927 default: 01928 return 1; 01929 } 01930 01931 return 0; 01932 } 01933 01934 /** 01935 * @brief Get the 6D orientation YH axis for LSM6DSL accelerometer sensor 01936 * @param yh the pointer to the 6D orientation YH axis 01937 * @retval 0 in case of success, an error code otherwise 01938 */ 01939 int LSM6DSLSensor::get_6d_orientation_yh(uint8_t *yh) 01940 { 01941 LSM6DSL_ACC_GYRO_DSD_YH_t yh_raw; 01942 01943 if ( LSM6DSL_ACC_GYRO_R_DSD_YH( (void *)this, &yh_raw ) == MEMS_ERROR ) 01944 { 01945 return 1; 01946 } 01947 01948 switch( yh_raw ) 01949 { 01950 case LSM6DSL_ACC_GYRO_DSD_YH_DETECTED: 01951 *yh = 1; 01952 break; 01953 case LSM6DSL_ACC_GYRO_DSD_YH_NOT_DETECTED: 01954 *yh = 0; 01955 break; 01956 default: 01957 return 1; 01958 } 01959 01960 return 0; 01961 } 01962 01963 /** 01964 * @brief Get the 6D orientation ZL axis for LSM6DSL accelerometer sensor 01965 * @param zl the pointer to the 6D orientation ZL axis 01966 * @retval 0 in case of success, an error code otherwise 01967 */ 01968 int LSM6DSLSensor::get_6d_orientation_zl(uint8_t *zl) 01969 { 01970 LSM6DSL_ACC_GYRO_DSD_ZL_t zl_raw; 01971 01972 if ( LSM6DSL_ACC_GYRO_R_DSD_ZL( (void *)this, &zl_raw ) == MEMS_ERROR ) 01973 { 01974 return 1; 01975 } 01976 01977 switch( zl_raw ) 01978 { 01979 case LSM6DSL_ACC_GYRO_DSD_ZL_DETECTED: 01980 *zl = 1; 01981 break; 01982 case LSM6DSL_ACC_GYRO_DSD_ZL_NOT_DETECTED: 01983 *zl = 0; 01984 break; 01985 default: 01986 return 1; 01987 } 01988 01989 return 0; 01990 } 01991 01992 /** 01993 * @brief Get the 6D orientation ZH axis for LSM6DSL accelerometer sensor 01994 * @param zh the pointer to the 6D orientation ZH axis 01995 * @retval 0 in case of success, an error code otherwise 01996 */ 01997 int LSM6DSLSensor::get_6d_orientation_zh(uint8_t *zh) 01998 { 01999 LSM6DSL_ACC_GYRO_DSD_ZH_t zh_raw; 02000 02001 if ( LSM6DSL_ACC_GYRO_R_DSD_ZH( (void *)this, &zh_raw ) == MEMS_ERROR ) 02002 { 02003 return 1; 02004 } 02005 02006 switch( zh_raw ) 02007 { 02008 case LSM6DSL_ACC_GYRO_DSD_ZH_DETECTED: 02009 *zh = 1; 02010 break; 02011 case LSM6DSL_ACC_GYRO_DSD_ZH_NOT_DETECTED: 02012 *zh = 0; 02013 break; 02014 default: 02015 return 1; 02016 } 02017 02018 return 0; 02019 } 02020 02021 /** 02022 * @brief Get the status of all hardware events for LSM6DSL accelerometer sensor 02023 * @param status the pointer to the status of all hardware events 02024 * @retval 0 in case of success, an error code otherwise 02025 */ 02026 int LSM6DSLSensor::get_event_status(LSM6DSL_Event_Status_t *status) 02027 { 02028 uint8_t Wake_Up_Src = 0, Tap_Src = 0, D6D_Src = 0, Func_Src = 0, Md1_Cfg = 0, Md2_Cfg = 0, Int1_Ctrl = 0; 02029 02030 memset((void *)status, 0x0, sizeof(LSM6DSL_Event_Status_t)); 02031 02032 if(read_reg(LSM6DSL_ACC_GYRO_WAKE_UP_SRC, &Wake_Up_Src) != 0) 02033 { 02034 return 1; 02035 } 02036 02037 if(read_reg(LSM6DSL_ACC_GYRO_TAP_SRC, &Tap_Src) != 0) 02038 { 02039 return 1; 02040 } 02041 02042 if(read_reg(LSM6DSL_ACC_GYRO_D6D_SRC, &D6D_Src) != 0) 02043 { 02044 return 1; 02045 } 02046 02047 if(read_reg(LSM6DSL_ACC_GYRO_FUNC_SRC, &Func_Src) != 0) 02048 { 02049 return 1; 02050 } 02051 02052 if(read_reg(LSM6DSL_ACC_GYRO_MD1_CFG, &Md1_Cfg ) != 0 ) 02053 { 02054 return 1; 02055 } 02056 02057 if(read_reg(LSM6DSL_ACC_GYRO_MD2_CFG, &Md2_Cfg ) != 0) 02058 { 02059 return 1; 02060 } 02061 02062 if(read_reg(LSM6DSL_ACC_GYRO_INT1_CTRL, &Int1_Ctrl ) != 0) 02063 { 02064 return 1; 02065 } 02066 02067 if((Md1_Cfg & LSM6DSL_ACC_GYRO_INT1_FF_MASK) || (Md2_Cfg & LSM6DSL_ACC_GYRO_INT2_FF_MASK)) 02068 { 02069 if((Wake_Up_Src & LSM6DSL_ACC_GYRO_FF_EV_STATUS_MASK)) 02070 { 02071 status->FreeFallStatus = 1; 02072 } 02073 } 02074 02075 if((Md1_Cfg & LSM6DSL_ACC_GYRO_INT1_WU_MASK) || (Md2_Cfg & LSM6DSL_ACC_GYRO_INT2_WU_MASK)) 02076 { 02077 if((Wake_Up_Src & LSM6DSL_ACC_GYRO_WU_EV_STATUS_MASK)) 02078 { 02079 status->WakeUpStatus = 1; 02080 } 02081 } 02082 02083 if((Md1_Cfg & LSM6DSL_ACC_GYRO_INT1_SINGLE_TAP_MASK) || (Md2_Cfg & LSM6DSL_ACC_GYRO_INT2_SINGLE_TAP_MASK)) 02084 { 02085 if((Tap_Src & LSM6DSL_ACC_GYRO_SINGLE_TAP_EV_STATUS_MASK)) 02086 { 02087 status->TapStatus = 1; 02088 } 02089 } 02090 02091 if((Md1_Cfg & LSM6DSL_ACC_GYRO_INT1_TAP_MASK) || (Md2_Cfg & LSM6DSL_ACC_GYRO_INT2_TAP_MASK)) 02092 { 02093 if((Tap_Src & LSM6DSL_ACC_GYRO_DOUBLE_TAP_EV_STATUS_MASK)) 02094 { 02095 status->DoubleTapStatus = 1; 02096 } 02097 } 02098 02099 if((Md1_Cfg & LSM6DSL_ACC_GYRO_INT1_6D_MASK) || (Md2_Cfg & LSM6DSL_ACC_GYRO_INT2_6D_MASK)) 02100 { 02101 if((D6D_Src & LSM6DSL_ACC_GYRO_D6D_EV_STATUS_MASK)) 02102 { 02103 status->D6DOrientationStatus = 1; 02104 } 02105 } 02106 02107 if((Int1_Ctrl & LSM6DSL_ACC_GYRO_INT1_PEDO_MASK)) 02108 { 02109 if((Func_Src & LSM6DSL_ACC_GYRO_PEDO_EV_STATUS_MASK)) 02110 { 02111 status->StepStatus = 1; 02112 } 02113 } 02114 02115 if((Md1_Cfg & LSM6DSL_ACC_GYRO_INT1_TILT_MASK) || (Md2_Cfg & LSM6DSL_ACC_GYRO_INT2_TILT_MASK)) 02116 { 02117 if((Func_Src & LSM6DSL_ACC_GYRO_TILT_EV_STATUS_MASK)) 02118 { 02119 status->TiltStatus = 1; 02120 } 02121 } 02122 02123 return 0; 02124 } 02125 02126 /** 02127 * @brief Read the data from register 02128 * @param reg register address 02129 * @param data register data 02130 * @retval 0 in case of success, an error code otherwise 02131 */ 02132 int LSM6DSLSensor::read_reg( uint8_t reg, uint8_t *data ) 02133 { 02134 02135 if ( LSM6DSL_ACC_GYRO_read_reg( (void *)this, reg, data, 1 ) == MEMS_ERROR ) 02136 { 02137 return 1; 02138 } 02139 02140 return 0; 02141 } 02142 02143 /** 02144 * @brief Write the data to register 02145 * @param reg register address 02146 * @param data register data 02147 * @retval 0 in case of success, an error code otherwise 02148 */ 02149 int LSM6DSLSensor::write_reg( uint8_t reg, uint8_t data ) 02150 { 02151 02152 if ( LSM6DSL_ACC_GYRO_write_reg( (void *)this, reg, &data, 1 ) == MEMS_ERROR ) 02153 { 02154 return 1; 02155 } 02156 02157 return 0; 02158 } 02159 02160 02161 uint8_t LSM6DSL_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ) 02162 { 02163 return ((LSM6DSLSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); 02164 } 02165 02166 uint8_t LSM6DSL_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) 02167 { 02168 return ((LSM6DSLSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); 02169 }
Generated on Wed Jul 13 2022 22:58:06 by
