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