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: X_NUCLEO_COMMON ST_INTERFACES
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 <assert.h> 00052 00053 /* Defines -------------------------------------------------------------------*/ 00054 00055 #define LSM6DSO_ACC_SENSITIVITY_FS_2G 0.061f 00056 #define LSM6DSO_ACC_SENSITIVITY_FS_4G 0.122f 00057 #define LSM6DSO_ACC_SENSITIVITY_FS_8G 0.244f 00058 #define LSM6DSO_ACC_SENSITIVITY_FS_16G 0.488f 00059 00060 #define LSM6DSO_GYRO_SENSITIVITY_FS_125DPS 4.375f 00061 #define LSM6DSO_GYRO_SENSITIVITY_FS_250DPS 8.750f 00062 #define LSM6DSO_GYRO_SENSITIVITY_FS_500DPS 17.500f 00063 #define LSM6DSO_GYRO_SENSITIVITY_FS_1000DPS 35.000f 00064 #define LSM6DSO_GYRO_SENSITIVITY_FS_2000DPS 70.000f 00065 00066 00067 /* Typedefs ------------------------------------------------------------------*/ 00068 00069 typedef enum { 00070 LSM6DSO_INT1_PIN, 00071 LSM6DSO_INT2_PIN, 00072 } LSM6DSO_Interrupt_Pin_t; 00073 00074 typedef struct { 00075 unsigned int FreeFallStatus : 1; 00076 unsigned int TapStatus : 1; 00077 unsigned int DoubleTapStatus : 1; 00078 unsigned int WakeUpStatus : 1; 00079 unsigned int StepStatus : 1; 00080 unsigned int TiltStatus : 1; 00081 unsigned int D6DOrientationStatus : 1; 00082 unsigned int SleepStatus : 1; 00083 } LSM6DSO_Event_Status_t; 00084 00085 00086 /* Class Declaration ---------------------------------------------------------*/ 00087 00088 /** 00089 * Abstract class of an LSM6DSO Inertial Measurement Unit (IMU) 6 axes 00090 * sensor. 00091 */ 00092 class LSM6DSOSensor : public MotionSensor, public GyroSensor { 00093 public: 00094 enum SPI_type_t {SPI3W, SPI4W}; 00095 LSM6DSOSensor(SPI *spi, PinName cs_pin, PinName int1_pin = NC, PinName int2_pin = NC, SPI_type_t spi_type = SPI4W); 00096 LSM6DSOSensor(DevI2C *i2c, uint8_t address = LSM6DSO_I2C_ADD_H, PinName int1_pin = NC, PinName int2_pin = NC); 00097 virtual int init(void *init); 00098 virtual int read_id(uint8_t *id); 00099 virtual int get_x_axes(int32_t *acceleration); 00100 virtual int get_g_axes(int32_t *angular_rate); 00101 virtual int get_x_sensitivity(float *sensitivity); 00102 virtual int get_g_sensitivity(float *sensitivity); 00103 virtual int get_x_axes_raw(int16_t *value); 00104 virtual int get_g_axes_raw(int16_t *value); 00105 virtual int get_x_odr(float *odr); 00106 virtual int get_g_odr(float *odr); 00107 virtual int set_x_odr(float odr); 00108 virtual int set_g_odr(float odr); 00109 virtual int get_x_fs(float *full_scale); 00110 virtual int get_g_fs(float *full_scale); 00111 virtual int set_x_fs(float full_scale); 00112 virtual int set_g_fs(float full_scale); 00113 virtual int get_x_power_mode(uint8_t *xl_hm_mode, uint8_t *xl_ulp_en); 00114 virtual int set_x_power_mode(uint8_t xl_hm_mode, uint8_t xl_ulp_en); 00115 virtual int get_x_lpf2_en(uint8_t *lpf2_en); 00116 virtual int set_x_lpf2_en(uint8_t lpf2_en); 00117 virtual int get_x_filter_config(uint8_t *hp_slope_xl_en, uint8_t *hpcf_xl); 00118 virtual int set_x_filter_config(uint8_t hp_slope_xl_en, uint8_t hpcf_xl); 00119 virtual int get_g_power_mode(uint8_t *g_hm_mode); 00120 virtual int set_g_power_mode(uint8_t g_hm_mode); 00121 virtual int get_g_lpf_config(uint8_t *lpf1_sel_g, uint8_t *ftype); 00122 virtual int set_g_lpf_config(uint8_t lpf1_sel_g, uint8_t ftype); 00123 virtual int get_g_hpf_config(uint8_t *hp_en_g, uint8_t *hpm_g); 00124 virtual int set_g_hpf_config(uint8_t hp_en_g, uint8_t hpm_g); 00125 int enable_x(void); 00126 int enable_g(void); 00127 int disable_x(void); 00128 int disable_g(void); 00129 int enable_free_fall_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); 00130 int disable_free_fall_detection(void); 00131 int set_free_fall_threshold(uint8_t thr); 00132 int set_free_fall_duration(uint8_t dur); 00133 int enable_pedometer(void); 00134 int disable_pedometer(void); 00135 int get_step_counter(uint16_t *step_count); 00136 int reset_step_counter(void); 00137 int enable_tilt_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); 00138 int disable_tilt_detection(void); 00139 int enable_wake_up_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); 00140 int disable_wake_up_detection(void); 00141 int set_wake_up_threshold(uint8_t thr); 00142 int set_wake_up_duration(uint8_t dur); 00143 int enable_single_tap_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); 00144 int disable_single_tap_detection(void); 00145 int enable_double_tap_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); 00146 int disable_double_tap_detection(void); 00147 int set_tap_threshold(uint8_t thr); 00148 int set_tap_shock_time(uint8_t time); 00149 int set_tap_quiet_time(uint8_t time); 00150 int set_tap_duration_time(uint8_t time); 00151 int enable_6d_orientation(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); 00152 int disable_6d_orientation(void); 00153 int set_6d_orientation_threshold(uint8_t thr); 00154 int get_6d_orientation_xl(uint8_t *xl); 00155 int get_6d_orientation_xh(uint8_t *xh); 00156 int get_6d_orientation_yl(uint8_t *yl); 00157 int get_6d_orientation_yh(uint8_t *yh); 00158 int get_6d_orientation_zl(uint8_t *zl); 00159 int get_6d_orientation_zh(uint8_t *zh); 00160 int get_event_status(LSM6DSO_Event_Status_t *status); 00161 int read_reg(uint8_t reg, uint8_t *data); 00162 int write_reg(uint8_t reg, uint8_t data); 00163 int set_interrupt_latch(uint8_t status); 00164 int get_x_drdy_status(uint8_t *status); 00165 int set_x_self_test(uint8_t status); 00166 int get_g_drdy_status(uint8_t *status); 00167 int set_g_self_test(uint8_t status); 00168 int get_fifo_num_samples(uint16_t *num_samples); 00169 int get_fifo_full_status(uint8_t *status); 00170 int set_fifo_int1_fifo_full(uint8_t status); 00171 int set_fifo_watermark_level(uint16_t watermark); 00172 int set_fifo_stop_on_fth(uint8_t status); 00173 int set_fifo_mode(uint8_t mode); 00174 int get_fifo_tag(uint8_t *tag); 00175 int get_fifo_data(uint8_t *data); 00176 int get_fifo_x_axes(int32_t *acceleration); 00177 int set_fifo_x_bdr(float bdr); 00178 int get_fifo_g_axes(int32_t *angular_velocity); 00179 int set_fifo_g_bdr(float bdr); 00180 00181 /** 00182 * @brief Attaching an interrupt handler to the INT1 interrupt. 00183 * @param fptr An interrupt handler. 00184 * @retval None. 00185 */ 00186 void attach_int1_irq(void (*fptr)(void)) 00187 { 00188 _int1_irq.rise(fptr); 00189 } 00190 00191 /** 00192 * @brief Enabling the INT1 interrupt handling. 00193 * @param None. 00194 * @retval None. 00195 */ 00196 void enable_int1_irq(void) 00197 { 00198 _int1_irq.enable_irq(); 00199 } 00200 00201 /** 00202 * @brief Disabling the INT1 interrupt handling. 00203 * @param None. 00204 * @retval None. 00205 */ 00206 void disable_int1_irq(void) 00207 { 00208 _int1_irq.disable_irq(); 00209 } 00210 00211 /** 00212 * @brief Attaching an interrupt handler to the INT2 interrupt. 00213 * @param fptr An interrupt handler. 00214 * @retval None. 00215 */ 00216 void attach_int2_irq(void (*fptr)(void)) 00217 { 00218 _int2_irq.rise(fptr); 00219 } 00220 00221 /** 00222 * @brief Enabling the INT2 interrupt handling. 00223 * @param None. 00224 * @retval None. 00225 */ 00226 void enable_int2_irq(void) 00227 { 00228 _int2_irq.enable_irq(); 00229 } 00230 00231 /** 00232 * @brief Disabling the INT2 interrupt handling. 00233 * @param None. 00234 * @retval None. 00235 */ 00236 void disable_int2_irq(void) 00237 { 00238 _int2_irq.disable_irq(); 00239 } 00240 00241 /** 00242 * @brief Utility function to read data. 00243 * @param pBuffer: pointer to data to be read. 00244 * @param RegisterAddr: specifies internal address register to be read. 00245 * @param NumByteToRead: number of bytes to be read. 00246 * @retval 0 if ok, an error code otherwise. 00247 */ 00248 uint8_t io_read(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) 00249 { 00250 if (_dev_spi) { 00251 /* Write Reg Address */ 00252 _dev_spi->lock(); 00253 _cs_pin = 0; 00254 if (_spi_type == SPI4W) { 00255 _dev_spi->write(RegisterAddr | 0x80); 00256 for (int i = 0; i < NumByteToRead; i++) { 00257 *(pBuffer + i) = _dev_spi->write(0x00); 00258 } 00259 } else if (_spi_type == SPI3W) { 00260 /* Write RD Reg Address with RD bit*/ 00261 uint8_t TxByte = RegisterAddr | 0x80; 00262 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead); 00263 } 00264 _cs_pin = 1; 00265 _dev_spi->unlock(); 00266 return 0; 00267 } 00268 if (_dev_i2c) { 00269 return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); 00270 } 00271 return 1; 00272 } 00273 00274 /** 00275 * @brief Utility function to write data. 00276 * @param pBuffer: pointer to data to be written. 00277 * @param RegisterAddr: specifies internal address register to be written. 00278 * @param NumByteToWrite: number of bytes to write. 00279 * @retval 0 if ok, an error code otherwise. 00280 */ 00281 uint8_t io_write(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) 00282 { 00283 if (_dev_spi) { 00284 _dev_spi->lock(); 00285 _cs_pin = 0; 00286 _dev_spi->write(RegisterAddr); 00287 _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); 00288 _cs_pin = 1; 00289 _dev_spi->unlock(); 00290 return 0; 00291 } 00292 if (_dev_i2c) { 00293 return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); 00294 } 00295 return 1; 00296 } 00297 00298 private: 00299 int set_x_odr_when_enabled(float odr); 00300 int set_g_odr_when_enabled(float odr); 00301 int set_x_odr_when_disabled(float odr); 00302 int set_g_odr_when_disabled(float odr); 00303 00304 /* Helper classes. */ 00305 DevI2C *_dev_i2c; 00306 SPI *_dev_spi; 00307 00308 /* Configuration */ 00309 uint8_t _address; 00310 DigitalOut _cs_pin; 00311 InterruptIn _int1_irq; 00312 InterruptIn _int2_irq; 00313 SPI_type_t _spi_type; 00314 00315 uint8_t _x_is_enabled; 00316 lsm6dso_odr_xl_t _x_last_odr; 00317 uint8_t _g_is_enabled; 00318 lsm6dso_odr_g_t _g_last_odr; 00319 00320 lsm6dso_ctx_t _reg_ctx; 00321 }; 00322 00323 #ifdef __cplusplus 00324 extern "C" { 00325 #endif 00326 int32_t LSM6DSO_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite); 00327 int32_t LSM6DSO_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead); 00328 #ifdef __cplusplus 00329 } 00330 #endif 00331 00332 #endif
Generated on Sat Jul 16 2022 04:44:06 by
1.7.2