iNEMO inertial module: 3D accelerometer and 3D gyroscope.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3
LSM6DSOSensor.h
00001 /** 00002 ****************************************************************************** 00003 * @file LSM6DSOSensor.h 00004 * @author SRA 00005 * @version V1.0.0 00006 * @date February 2019 00007 * @brief Abstract Class of an LSM6DSO Inertial Measurement Unit (IMU) 6 axes 00008 * sensor. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2019 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 __LSM6DSOSensor_H__ 00043 #define __LSM6DSOSensor_H__ 00044 00045 00046 /* Includes ------------------------------------------------------------------*/ 00047 #include "DevI2C.h" 00048 #include "lsm6dso_reg.h" 00049 #include "MotionSensor.h" 00050 #include "GyroSensor.h" 00051 #include "mbed.h" 00052 #include <assert.h> 00053 00054 /* Defines -------------------------------------------------------------------*/ 00055 00056 #define LSM6DSO_ACC_SENSITIVITY_FS_2G 0.061f 00057 #define LSM6DSO_ACC_SENSITIVITY_FS_4G 0.122f 00058 #define LSM6DSO_ACC_SENSITIVITY_FS_8G 0.244f 00059 #define LSM6DSO_ACC_SENSITIVITY_FS_16G 0.488f 00060 00061 #define LSM6DSO_GYRO_SENSITIVITY_FS_125DPS 4.375f 00062 #define LSM6DSO_GYRO_SENSITIVITY_FS_250DPS 8.750f 00063 #define LSM6DSO_GYRO_SENSITIVITY_FS_500DPS 17.500f 00064 #define LSM6DSO_GYRO_SENSITIVITY_FS_1000DPS 35.000f 00065 #define LSM6DSO_GYRO_SENSITIVITY_FS_2000DPS 70.000f 00066 00067 00068 /* Typedefs ------------------------------------------------------------------*/ 00069 00070 typedef enum { 00071 LSM6DSO_INT1_PIN, 00072 LSM6DSO_INT2_PIN, 00073 } LSM6DSO_Interrupt_Pin_t; 00074 00075 typedef enum 00076 { 00077 LSM6DSO_ACC_HIGH_PERFORMANCE_MODE, 00078 LSM6DSO_ACC_LOW_POWER_NORMAL_MODE, 00079 LSM6DSO_ACC_ULTRA_LOW_POWER_MODE 00080 } LSM6DSO_ACC_Operating_Mode_t; 00081 00082 typedef enum 00083 { 00084 LSM6DSO_GYRO_HIGH_PERFORMANCE_MODE, 00085 LSM6DSO_GYRO_LOW_POWER_NORMAL_MODE 00086 } LSM6DSO_GYRO_Operating_Mode_t; 00087 00088 typedef struct { 00089 unsigned int FreeFallStatus : 1; 00090 unsigned int TapStatus : 1; 00091 unsigned int DoubleTapStatus : 1; 00092 unsigned int WakeUpStatus : 1; 00093 unsigned int StepStatus : 1; 00094 unsigned int TiltStatus : 1; 00095 unsigned int D6DOrientationStatus : 1; 00096 unsigned int SleepStatus : 1; 00097 } LSM6DSO_Event_Status_t; 00098 00099 00100 /* Class Declaration ---------------------------------------------------------*/ 00101 00102 /** 00103 * Abstract class of an LSM6DSO Inertial Measurement Unit (IMU) 6 axes 00104 * sensor. 00105 */ 00106 class LSM6DSOSensor : public MotionSensor, public GyroSensor { 00107 public: 00108 enum SPI_type_t {SPI3W, SPI4W}; 00109 LSM6DSOSensor(SPI *spi, PinName cs_pin, PinName int1_pin = NC, PinName int2_pin = NC, SPI_type_t spi_type = SPI4W); 00110 LSM6DSOSensor(DevI2C *i2c, uint8_t address = LSM6DSO_I2C_ADD_H, PinName int1_pin = NC, PinName int2_pin = NC); 00111 virtual int init(void *init); 00112 virtual int read_id(uint8_t *id); 00113 virtual int get_x_axes(int32_t *acceleration); 00114 virtual int get_g_axes(int32_t *angular_rate); 00115 virtual int get_x_sensitivity(float *sensitivity); 00116 virtual int get_g_sensitivity(float *sensitivity); 00117 virtual int get_x_axes_raw(int16_t *value); 00118 virtual int get_g_axes_raw(int16_t *value); 00119 virtual int get_x_odr(float *odr); 00120 virtual int get_g_odr(float *odr); 00121 virtual int set_x_odr(float odr); 00122 virtual int set_x_odr_with_mode(float odr, LSM6DSO_ACC_Operating_Mode_t mode); 00123 virtual int set_g_odr(float odr); 00124 virtual int set_g_odr_with_mode(float odr, LSM6DSO_GYRO_Operating_Mode_t mode); 00125 virtual int get_x_fs(float *full_scale); 00126 virtual int get_g_fs(float *full_scale); 00127 virtual int set_x_fs(float full_scale); 00128 virtual int set_g_fs(float full_scale); 00129 int enable_x(void); 00130 int enable_g(void); 00131 int disable_x(void); 00132 int disable_g(void); 00133 int enable_free_fall_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); 00134 int disable_free_fall_detection(void); 00135 int set_free_fall_threshold(uint8_t thr); 00136 int set_free_fall_duration(uint8_t dur); 00137 int enable_pedometer(void); 00138 int disable_pedometer(void); 00139 int get_step_counter(uint16_t *step_count); 00140 int reset_step_counter(void); 00141 int enable_tilt_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); 00142 int disable_tilt_detection(void); 00143 int enable_wake_up_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); 00144 int disable_wake_up_detection(void); 00145 int set_wake_up_threshold(uint8_t thr); 00146 int set_wake_up_duration(uint8_t dur); 00147 int enable_single_tap_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); 00148 int disable_single_tap_detection(void); 00149 int enable_double_tap_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); 00150 int disable_double_tap_detection(void); 00151 int set_tap_threshold(uint8_t thr); 00152 int set_tap_shock_time(uint8_t time); 00153 int set_tap_quiet_time(uint8_t time); 00154 int set_tap_duration_time(uint8_t time); 00155 int enable_6d_orientation(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); 00156 int disable_6d_orientation(void); 00157 int set_6d_orientation_threshold(uint8_t thr); 00158 int get_6d_orientation_xl(uint8_t *xl); 00159 int get_6d_orientation_xh(uint8_t *xh); 00160 int get_6d_orientation_yl(uint8_t *yl); 00161 int get_6d_orientation_yh(uint8_t *yh); 00162 int get_6d_orientation_zl(uint8_t *zl); 00163 int get_6d_orientation_zh(uint8_t *zh); 00164 int get_event_status(LSM6DSO_Event_Status_t *status); 00165 int read_reg(uint8_t reg, uint8_t *data); 00166 int write_reg(uint8_t reg, uint8_t data); 00167 int set_interrupt_latch(uint8_t status); 00168 int get_x_drdy_status(uint8_t *status); 00169 int set_x_self_test(uint8_t status); 00170 int get_g_drdy_status(uint8_t *status); 00171 int set_g_self_test(uint8_t status); 00172 int get_fifo_num_samples(uint16_t *num_samples); 00173 int get_fifo_full_status(uint8_t *status); 00174 int set_fifo_int1_fifo_full(uint8_t status); 00175 int set_fifo_watermark_level(uint16_t watermark); 00176 int set_fifo_stop_on_fth(uint8_t status); 00177 int set_fifo_mode(uint8_t mode); 00178 int get_fifo_tag(uint8_t *tag); 00179 int get_fifo_data(uint8_t *data); 00180 int get_fifo_x_axes(int32_t *acceleration); 00181 int set_fifo_x_bdr(float bdr); 00182 int get_fifo_g_axes(int32_t *angular_velocity); 00183 int set_fifo_g_bdr(float bdr); 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) { 00273 return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); 00274 } 00275 return 1; 00276 } 00277 00278 /** 00279 * @brief Utility function to write data. 00280 * @param pBuffer: pointer to data to be written. 00281 * @param RegisterAddr: specifies internal address register to be written. 00282 * @param NumByteToWrite: number of bytes to write. 00283 * @retval 0 if ok, an error code otherwise. 00284 */ 00285 uint8_t io_write(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) 00286 { 00287 if (_dev_spi) { 00288 _dev_spi->lock(); 00289 _cs_pin = 0; 00290 _dev_spi->write(RegisterAddr); 00291 _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); 00292 _cs_pin = 1; 00293 _dev_spi->unlock(); 00294 return 0; 00295 } 00296 if (_dev_i2c) { 00297 return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); 00298 } 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 DevI2C *_dev_i2c; 00310 SPI *_dev_spi; 00311 00312 /* Configuration */ 00313 uint8_t _address; 00314 DigitalOut _cs_pin; 00315 InterruptIn _int1_irq; 00316 InterruptIn _int2_irq; 00317 SPI_type_t _spi_type; 00318 00319 uint8_t _x_is_enabled; 00320 lsm6dso_odr_xl_t _x_last_odr; 00321 uint8_t _g_is_enabled; 00322 lsm6dso_odr_g_t _g_last_odr; 00323 00324 lsm6dso_ctx_t _reg_ctx; 00325 }; 00326 00327 #ifdef __cplusplus 00328 extern "C" { 00329 #endif 00330 int32_t LSM6DSO_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite); 00331 int32_t LSM6DSO_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead); 00332 #ifdef __cplusplus 00333 } 00334 #endif 00335 00336 #endif
Generated on Wed Jul 13 2022 04:15:40 by 1.7.2