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.h
00001 /** 00002 ****************************************************************************** 00003 * @file LSM6DSLSensor.h 00004 * @author CLab 00005 * @version V1.0.0 00006 * @date 5 August 2016 00007 * @brief Abstract Class 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 /* Prevent recursive inclusion -----------------------------------------------*/ 00041 00042 #ifndef __LSM6DSLSensor_H__ 00043 #define __LSM6DSLSensor_H__ 00044 00045 00046 /* Includes ------------------------------------------------------------------*/ 00047 #include "SPI3W.h" 00048 #include "DevI2C.h" 00049 #include "LSM6DSL_acc_gyro_driver.h" 00050 #include "MotionSensor.h" 00051 #include "GyroSensor.h" 00052 00053 /* Defines -------------------------------------------------------------------*/ 00054 00055 #define LSM6DSL_ACC_SENSITIVITY_FOR_FS_2G 0.061 /**< Sensitivity value for 2 g full scale [mg/LSB] */ 00056 #define LSM6DSL_ACC_SENSITIVITY_FOR_FS_4G 0.122 /**< Sensitivity value for 4 g full scale [mg/LSB] */ 00057 #define LSM6DSL_ACC_SENSITIVITY_FOR_FS_8G 0.244 /**< Sensitivity value for 8 g full scale [mg/LSB] */ 00058 #define LSM6DSL_ACC_SENSITIVITY_FOR_FS_16G 0.488 /**< Sensitivity value for 16 g full scale [mg/LSB] */ 00059 00060 #define LSM6DSL_GYRO_SENSITIVITY_FOR_FS_125DPS 04.375 /**< Sensitivity value for 125 dps full scale [mdps/LSB] */ 00061 #define LSM6DSL_GYRO_SENSITIVITY_FOR_FS_245DPS 08.750 /**< Sensitivity value for 245 dps full scale [mdps/LSB] */ 00062 #define LSM6DSL_GYRO_SENSITIVITY_FOR_FS_500DPS 17.500 /**< Sensitivity value for 500 dps full scale [mdps/LSB] */ 00063 #define LSM6DSL_GYRO_SENSITIVITY_FOR_FS_1000DPS 35.000 /**< Sensitivity value for 1000 dps full scale [mdps/LSB] */ 00064 #define LSM6DSL_GYRO_SENSITIVITY_FOR_FS_2000DPS 70.000 /**< Sensitivity value for 2000 dps full scale [mdps/LSB] */ 00065 00066 #define LSM6DSL_PEDOMETER_THRESHOLD_LOW 0x00 /**< Lowest value of pedometer threshold */ 00067 #define LSM6DSL_PEDOMETER_THRESHOLD_MID_LOW 0x07 00068 #define LSM6DSL_PEDOMETER_THRESHOLD_MID 0x0F 00069 #define LSM6DSL_PEDOMETER_THRESHOLD_MID_HIGH 0x17 00070 #define LSM6DSL_PEDOMETER_THRESHOLD_HIGH 0x1F /**< Highest value of pedometer threshold */ 00071 00072 #define LSM6DSL_WAKE_UP_THRESHOLD_LOW 0x01 /**< Lowest value of wake up threshold */ 00073 #define LSM6DSL_WAKE_UP_THRESHOLD_MID_LOW 0x0F 00074 #define LSM6DSL_WAKE_UP_THRESHOLD_MID 0x1F 00075 #define LSM6DSL_WAKE_UP_THRESHOLD_MID_HIGH 0x2F 00076 #define LSM6DSL_WAKE_UP_THRESHOLD_HIGH 0x3F /**< Highest value of wake up threshold */ 00077 00078 #define LSM6DSL_TAP_THRESHOLD_LOW 0x01 /**< Lowest value of wake up threshold */ 00079 #define LSM6DSL_TAP_THRESHOLD_MID_LOW 0x08 00080 #define LSM6DSL_TAP_THRESHOLD_MID 0x10 00081 #define LSM6DSL_TAP_THRESHOLD_MID_HIGH 0x18 00082 #define LSM6DSL_TAP_THRESHOLD_HIGH 0x1F /**< Highest value of wake up threshold */ 00083 00084 #define LSM6DSL_TAP_SHOCK_TIME_LOW 0x00 /**< Lowest value of wake up threshold */ 00085 #define LSM6DSL_TAP_SHOCK_TIME_MID_LOW 0x01 00086 #define LSM6DSL_TAP_SHOCK_TIME_MID_HIGH 0x02 00087 #define LSM6DSL_TAP_SHOCK_TIME_HIGH 0x03 /**< Highest value of wake up threshold */ 00088 00089 #define LSM6DSL_TAP_QUIET_TIME_LOW 0x00 /**< Lowest value of wake up threshold */ 00090 #define LSM6DSL_TAP_QUIET_TIME_MID_LOW 0x01 00091 #define LSM6DSL_TAP_QUIET_TIME_MID_HIGH 0x02 00092 #define LSM6DSL_TAP_QUIET_TIME_HIGH 0x03 /**< Highest value of wake up threshold */ 00093 00094 #define LSM6DSL_TAP_DURATION_TIME_LOW 0x00 /**< Lowest value of wake up threshold */ 00095 #define LSM6DSL_TAP_DURATION_TIME_MID_LOW 0x04 00096 #define LSM6DSL_TAP_DURATION_TIME_MID 0x08 00097 #define LSM6DSL_TAP_DURATION_TIME_MID_HIGH 0x0C 00098 #define LSM6DSL_TAP_DURATION_TIME_HIGH 0x0F /**< Highest value of wake up threshold */ 00099 00100 /* Typedefs ------------------------------------------------------------------*/ 00101 00102 typedef enum 00103 { 00104 LSM6DSL_INT1_PIN, 00105 LSM6DSL_INT2_PIN 00106 } LSM6DSL_Interrupt_Pin_t; 00107 00108 typedef struct 00109 { 00110 unsigned int FreeFallStatus : 1; 00111 unsigned int TapStatus : 1; 00112 unsigned int DoubleTapStatus : 1; 00113 unsigned int WakeUpStatus : 1; 00114 unsigned int StepStatus : 1; 00115 unsigned int TiltStatus : 1; 00116 unsigned int D6DOrientationStatus : 1; 00117 } LSM6DSL_Event_Status_t; 00118 00119 /* Class Declaration ---------------------------------------------------------*/ 00120 00121 /** 00122 * Abstract class of an LSM6DSL Inertial Measurement Unit (IMU) 6 axes 00123 * sensor. 00124 */ 00125 class LSM6DSLSensor : public MotionSensor, public GyroSensor 00126 { 00127 public: 00128 LSM6DSLSensor(SPI3W *spi3w, PinName cs_pin, PinName INT1_pin=NC, PinName INT2_pin=NC); 00129 LSM6DSLSensor(SPI *spi, PinName cs_pin, PinName INT1_pin=NC, PinName INT2_pin=NC); 00130 LSM6DSLSensor(DevI2C *i2c, uint8_t address=LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH, PinName INT1_pin=NC, PinName INT2_pin=NC); 00131 virtual int init(void *init); 00132 virtual int read_id(uint8_t *id); 00133 virtual int get_x_axes(int32_t *pData); 00134 virtual int get_g_axes(int32_t *pData); 00135 virtual int get_x_sensitivity(float *pfData); 00136 virtual int get_g_sensitivity(float *pfData); 00137 virtual int get_x_axes_raw(int16_t *pData); 00138 virtual int get_g_axes_raw(int16_t *pData); 00139 virtual int get_x_odr(float *odr); 00140 virtual int get_g_odr(float *odr); 00141 virtual int set_x_odr(float odr); 00142 virtual int set_g_odr(float odr); 00143 virtual int get_x_fs(float *fullScale); 00144 virtual int get_g_fs(float *fullScale); 00145 virtual int set_x_fs(float fullScale); 00146 virtual int set_g_fs(float fullScale); 00147 int enable_x(void); 00148 int enable_g(void); 00149 int disable_x(void); 00150 int disable_g(void); 00151 int enable_free_fall_detection(LSM6DSL_Interrupt_Pin_t pin = LSM6DSL_INT1_PIN); 00152 int disable_free_fall_detection(void); 00153 int set_free_fall_threshold(uint8_t thr); 00154 int enable_pedometer(void); 00155 int disable_pedometer(void); 00156 int get_step_counter(uint16_t *step_count); 00157 int reset_step_counter(void); 00158 int set_pedometer_threshold(uint8_t thr); 00159 int enable_tilt_detection(LSM6DSL_Interrupt_Pin_t pin = LSM6DSL_INT1_PIN); 00160 int disable_tilt_detection(void); 00161 int enable_wake_up_detection(LSM6DSL_Interrupt_Pin_t pin = LSM6DSL_INT2_PIN); 00162 int disable_wake_up_detection(void); 00163 int set_wake_up_threshold(uint8_t thr); 00164 int enable_single_tap_detection(LSM6DSL_Interrupt_Pin_t pin = LSM6DSL_INT1_PIN); 00165 int disable_single_tap_detection(void); 00166 int enable_double_tap_detection(LSM6DSL_Interrupt_Pin_t pin = LSM6DSL_INT1_PIN); 00167 int disable_double_tap_detection(void); 00168 int set_tap_threshold(uint8_t thr); 00169 int set_tap_shock_time(uint8_t time); 00170 int set_tap_quiet_time(uint8_t time); 00171 int set_tap_duration_time(uint8_t time); 00172 int enable_6d_orientation(LSM6DSL_Interrupt_Pin_t pin = LSM6DSL_INT1_PIN); 00173 int disable_6d_orientation(void); 00174 int get_6d_orientation_xl(uint8_t *xl); 00175 int get_6d_orientation_xh(uint8_t *xh); 00176 int get_6d_orientation_yl(uint8_t *yl); 00177 int get_6d_orientation_yh(uint8_t *yh); 00178 int get_6d_orientation_zl(uint8_t *zl); 00179 int get_6d_orientation_zh(uint8_t *zh); 00180 int get_event_status(LSM6DSL_Event_Status_t *status); 00181 int read_reg(uint8_t reg, uint8_t *data); 00182 int write_reg(uint8_t reg, uint8_t data); 00183 00184 /** 00185 * @brief Attaching an interrupt handler to the INT1 interrupt. 00186 * @param fptr An interrupt handler. 00187 * @retval None. 00188 */ 00189 void attach_int1_irq(void (*fptr)(void)) 00190 { 00191 _int1_irq.rise(fptr); 00192 } 00193 00194 /** 00195 * @brief Enabling the INT1 interrupt handling. 00196 * @param None. 00197 * @retval None. 00198 */ 00199 void enable_int1_irq(void) 00200 { 00201 _int1_irq.enable_irq(); 00202 } 00203 00204 /** 00205 * @brief Disabling the INT1 interrupt handling. 00206 * @param None. 00207 * @retval None. 00208 */ 00209 void disable_int1_irq(void) 00210 { 00211 _int1_irq.disable_irq(); 00212 } 00213 00214 /** 00215 * @brief Attaching an interrupt handler to the INT2 interrupt. 00216 * @param fptr An interrupt handler. 00217 * @retval None. 00218 */ 00219 void attach_int2_irq(void (*fptr)(void)) 00220 { 00221 _int2_irq.rise(fptr); 00222 } 00223 00224 /** 00225 * @brief Enabling the INT2 interrupt handling. 00226 * @param None. 00227 * @retval None. 00228 */ 00229 void enable_int2_irq(void) 00230 { 00231 _int2_irq.enable_irq(); 00232 } 00233 00234 /** 00235 * @brief Disabling the INT2 interrupt handling. 00236 * @param None. 00237 * @retval None. 00238 */ 00239 void disable_int2_irq(void) 00240 { 00241 _int2_irq.disable_irq(); 00242 } 00243 00244 /** 00245 * @brief Utility function to read data. 00246 * @param pBuffer: pointer to data to be read. 00247 * @param RegisterAddr: specifies internal address register to be read. 00248 * @param NumByteToRead: number of bytes to be read. 00249 * @retval 0 if ok, an error code otherwise. 00250 */ 00251 uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) 00252 { 00253 if (_dev_spi) { 00254 /* Write Reg Address */ 00255 _dev_spi->lock(); 00256 _cs_pin = 0; 00257 _dev_spi->write(RegisterAddr | 0x80); 00258 00259 for (int i=0; i<NumByteToRead; i++) { 00260 *(pBuffer+i) = _dev_spi->write(0x00); 00261 } 00262 00263 _cs_pin = 1; 00264 _dev_spi->unlock(); 00265 return 0; 00266 } 00267 00268 if (_dev_spi3w) return _dev_spi3w->spi3w_read(pBuffer, &_cs_pin, RegisterAddr, NumByteToRead); 00269 if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); 00270 return 1; 00271 } 00272 00273 /** 00274 * @brief Utility function to write data. 00275 * @param pBuffer: pointer to data to be written. 00276 * @param RegisterAddr: specifies internal address register to be written. 00277 * @param NumByteToWrite: number of bytes to write. 00278 * @retval 0 if ok, an error code otherwise. 00279 */ 00280 uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) 00281 { 00282 int data; 00283 if (_dev_spi) { 00284 _dev_spi->lock(); 00285 _cs_pin = 0; 00286 data = _dev_spi->write(RegisterAddr); 00287 00288 for (int i=0; i<NumByteToWrite; i++) { 00289 data = _dev_spi->write(*(pBuffer+i)); 00290 } 00291 00292 _cs_pin = 1; 00293 _dev_spi->unlock(); 00294 return data; 00295 } 00296 00297 if (_dev_spi3w) return _dev_spi3w->spi3w_write(pBuffer, &_cs_pin, RegisterAddr, NumByteToWrite); 00298 if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); 00299 return 1; 00300 } 00301 00302 private: 00303 int set_x_odr_when_enabled(float odr); 00304 int set_g_odr_when_enabled(float odr); 00305 int set_x_odr_when_disabled(float odr); 00306 int set_g_odr_when_disabled(float odr); 00307 00308 /* Helper classes. */ 00309 SPI3W *_dev_spi3w; 00310 SPI *_dev_spi; 00311 DevI2C *_dev_i2c; 00312 00313 /* Configuration */ 00314 uint8_t _address; 00315 DigitalOut _cs_pin; 00316 InterruptIn _int1_irq; 00317 InterruptIn _int2_irq; 00318 00319 uint8_t _x_is_enabled; 00320 float _x_last_odr; 00321 uint8_t _g_is_enabled; 00322 float _g_last_odr; 00323 }; 00324 00325 #ifdef __cplusplus 00326 extern "C" { 00327 #endif 00328 uint8_t LSM6DSL_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ); 00329 uint8_t LSM6DSL_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ); 00330 #ifdef __cplusplus 00331 } 00332 #endif 00333 00334 #endif
Generated on Wed Jul 13 2022 22:58:06 by
