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