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
Dependents: X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3
LIS2DW12Sensor.h
00001 /** 00002 ****************************************************************************** 00003 * @file LIS2DW12Sensor.h 00004 * @author CLab 00005 * @version V1.0.0 00006 * @date 15 November 2018 00007 * @brief Abstract Class of an LIS2DW12 Inertial Measurement Unit (IMU) 3 axes 00008 * sensor. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2018 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 __LIS2DW12Sensor_H__ 00043 #define __LIS2DW12Sensor_H__ 00044 00045 00046 /* Includes ------------------------------------------------------------------*/ 00047 00048 #include "DevI2C.h" 00049 #include "lis2dw12_reg.h" 00050 #include "MotionSensor.h" 00051 #include <assert.h> 00052 00053 /* Defines -------------------------------------------------------------------*/ 00054 00055 #define LIS2DW12_ACC_SENSITIVITY_FOR_FS_2G_LOPOW1_MODE 0.976f /**< Sensitivity value for 2g full scale, Low-power1 mode [mg/LSB] */ 00056 #define LIS2DW12_ACC_SENSITIVITY_FOR_FS_2G_OTHER_MODES 0.244f /**< Sensitivity value for 2g full scale, all other modes except Low-power1 [mg/LSB] */ 00057 00058 #define LIS2DW12_ACC_SENSITIVITY_FOR_FS_4G_LOPOW1_MODE 1.952f /**< Sensitivity value for 4g full scale, Low-power1 mode [mg/LSB] */ 00059 #define LIS2DW12_ACC_SENSITIVITY_FOR_FS_4G_OTHER_MODES 0.488f /**< Sensitivity value for 4g full scale, all other modes except Low-power1 [mg/LSB] */ 00060 00061 #define LIS2DW12_ACC_SENSITIVITY_FOR_FS_8G_LOPOW1_MODE 3.904f /**< Sensitivity value for 8g full scale, Low-power1 mode [mg/LSB] */ 00062 #define LIS2DW12_ACC_SENSITIVITY_FOR_FS_8G_OTHER_MODES 0.976f /**< Sensitivity value for 8g full scale, all other modes except Low-power1 [mg/LSB] */ 00063 00064 #define LIS2DW12_ACC_SENSITIVITY_FOR_FS_16G_LOPOW1_MODE 7.808f /**< Sensitivity value for 16g full scale, Low-power1 mode [mg/LSB] */ 00065 #define LIS2DW12_ACC_SENSITIVITY_FOR_FS_16G_OTHER_MODES 1.952f /**< Sensitivity value for 16g full scale, all other modes except Low-power1 [mg/LSB] */ 00066 00067 /* Typedefs ------------------------------------------------------------------*/ 00068 00069 typedef struct { 00070 unsigned int WakeUpStatus : 1; 00071 unsigned int D6DOrientationStatus : 1; 00072 unsigned int SleepStatus : 1; 00073 } LIS2DW12_Event_Status_t; 00074 00075 typedef enum { 00076 LIS2DW12_HIGH_PERFORMANCE_MODE, 00077 LIS2DW12_LOW_POWER_MODE4, 00078 LIS2DW12_LOW_POWER_MODE3, 00079 LIS2DW12_LOW_POWER_MODE2, 00080 LIS2DW12_LOW_POWER_MODE1 00081 } LIS2DW12_Operating_Mode_t; 00082 00083 typedef enum { 00084 LIS2DW12_LOW_NOISE_DISABLE, 00085 LIS2DW12_LOW_NOISE_ENABLE 00086 } LIS2DW12_Low_Noise_t; 00087 00088 /* Class Declaration ---------------------------------------------------------*/ 00089 00090 /** 00091 * Abstract class of an LIS2DW12 Inertial Measurement Unit (IMU) 3 axes 00092 * sensor. 00093 */ 00094 class LIS2DW12Sensor : public MotionSensor { 00095 public: 00096 enum SPI_type_t {SPI3W, SPI4W}; 00097 LIS2DW12Sensor(DevI2C *i2c, uint8_t address = LIS2DW12_I2C_ADD_H, PinName int1_pin = NC, PinName int2_pin = NC); 00098 LIS2DW12Sensor(SPI *spi, PinName cs_pin, PinName int1_pin = NC, PinName int2_pin = NC, SPI_type_t spi_type = SPI4W); 00099 virtual int init(void *init); 00100 virtual int read_id(uint8_t *id); 00101 virtual int get_x_axes(int32_t *pData); 00102 virtual int get_x_sensitivity(float *pfData); 00103 virtual int get_x_axes_raw(int16_t *pData); 00104 virtual int get_x_odr(float *odr); 00105 virtual int set_x_odr(float odr); 00106 virtual int get_x_fs(float *fullScale); 00107 virtual int set_x_fs(float fullScale); 00108 int set_x_odr_with_mode(float odr, LIS2DW12_Operating_Mode_t mode = LIS2DW12_HIGH_PERFORMANCE_MODE, LIS2DW12_Low_Noise_t noise = LIS2DW12_LOW_NOISE_DISABLE); 00109 int enable_x(void); 00110 int disable_x(void); 00111 int enable_wake_up_detection(void); 00112 int disable_wake_up_detection(void); 00113 int set_wake_up_threshold(uint8_t thr); 00114 int set_wake_up_duration(uint8_t dur); 00115 int enable_inactivity_detection(void); 00116 int disable_inactivity_detection(void); 00117 int set_sleep_duration(uint8_t dur); 00118 int enable_6d_orientation(void); 00119 int disable_6d_orientation(void); 00120 int set_6d_orientation_threshold(uint8_t thr); 00121 int get_6d_orientation_xl(uint8_t *xl); 00122 int get_6d_orientation_xh(uint8_t *xh); 00123 int get_6d_orientation_yl(uint8_t *yl); 00124 int get_6d_orientation_yh(uint8_t *yh); 00125 int get_6d_orientation_zl(uint8_t *zl); 00126 int get_6d_orientation_zh(uint8_t *zh); 00127 int get_event_status(LIS2DW12_Event_Status_t *status); 00128 int get_fifo_num_samples(uint16_t *num_samples); 00129 int set_fifo_mode(uint8_t mode); 00130 int read_reg(uint8_t reg, uint8_t *data); 00131 int write_reg(uint8_t reg, uint8_t data); 00132 00133 /** 00134 * @brief Attaching an interrupt handler to the INT1 interrupt. 00135 * @param fptr An interrupt handler. 00136 * @retval None. 00137 */ 00138 void attach_int1_irq(void (*fptr)(void)) 00139 { 00140 _int1_irq.rise(fptr); 00141 } 00142 00143 /** 00144 * @brief Enabling the INT1 interrupt handling. 00145 * @param None. 00146 * @retval None. 00147 */ 00148 void enable_int1_irq(void) 00149 { 00150 _int1_irq.enable_irq(); 00151 } 00152 00153 /** 00154 * @brief Disabling the INT1 interrupt handling. 00155 * @param None. 00156 * @retval None. 00157 */ 00158 void disable_int1_irq(void) 00159 { 00160 _int1_irq.disable_irq(); 00161 } 00162 00163 /** 00164 * @brief Attaching an interrupt handler to the INT2 interrupt. 00165 * @param fptr An interrupt handler. 00166 * @retval None. 00167 */ 00168 void attach_int2_irq(void (*fptr)(void)) 00169 { 00170 _int2_irq.rise(fptr); 00171 } 00172 00173 /** 00174 * @brief Enabling the INT2 interrupt handling. 00175 * @param None. 00176 * @retval None. 00177 */ 00178 void enable_int2_irq(void) 00179 { 00180 _int2_irq.enable_irq(); 00181 } 00182 00183 /** 00184 * @brief Disabling the INT2 interrupt handling. 00185 * @param None. 00186 * @retval None. 00187 */ 00188 void disable_int2_irq(void) 00189 { 00190 _int2_irq.disable_irq(); 00191 } 00192 00193 /** 00194 * @brief Utility function to read data. 00195 * @param pBuffer: pointer to data to be read. 00196 * @param RegisterAddr: specifies internal address register to be read. 00197 * @param NumByteToRead: number of bytes to be read. 00198 * @retval 0 if ok, an error code otherwise. 00199 */ 00200 uint8_t io_read(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) 00201 { 00202 if (_dev_spi) { 00203 /* Write Reg Address */ 00204 _dev_spi->lock(); 00205 _cs_pin = 0; 00206 if (_spi_type == SPI4W) { 00207 _dev_spi->write(RegisterAddr | 0x80); 00208 for (int i = 0; i < NumByteToRead; i++) { 00209 *(pBuffer + i) = _dev_spi->write(0x00); 00210 } 00211 } else if (_spi_type == SPI3W) { 00212 /* Write RD Reg Address with RD bit*/ 00213 uint8_t TxByte = RegisterAddr | 0x80; 00214 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead); 00215 } 00216 _cs_pin = 1; 00217 _dev_spi->unlock(); 00218 return 0; 00219 } 00220 if (_dev_i2c) { 00221 return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); 00222 } 00223 return 1; 00224 } 00225 00226 /** 00227 * @brief Utility function to write data. 00228 * @param pBuffer: pointer to data to be written. 00229 * @param RegisterAddr: specifies internal address register to be written. 00230 * @param NumByteToWrite: number of bytes to write. 00231 * @retval 0 if ok, an error code otherwise. 00232 */ 00233 uint8_t io_write(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) 00234 { 00235 if (_dev_spi) { 00236 _dev_spi->lock(); 00237 _cs_pin = 0; 00238 _dev_spi->write(RegisterAddr); 00239 _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); 00240 _cs_pin = 1; 00241 _dev_spi->unlock(); 00242 return 0; 00243 } 00244 if (_dev_i2c) { 00245 return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); 00246 } 00247 return 1; 00248 } 00249 00250 private: 00251 int set_x_odr_when_enabled(float odr, LIS2DW12_Operating_Mode_t mode, LIS2DW12_Low_Noise_t noise); 00252 int set_x_odr_when_disabled(float odr, LIS2DW12_Operating_Mode_t mode, LIS2DW12_Low_Noise_t noise); 00253 00254 /* Helper classes. */ 00255 DevI2C *_dev_i2c; 00256 SPI *_dev_spi; 00257 00258 /* Configuration */ 00259 uint8_t _address; 00260 DigitalOut _cs_pin; 00261 InterruptIn _int1_irq; 00262 InterruptIn _int2_irq; 00263 SPI_type_t _spi_type; 00264 00265 uint8_t _x_is_enabled; 00266 float _x_last_odr; 00267 LIS2DW12_Operating_Mode_t _x_last_operating_mode; 00268 LIS2DW12_Low_Noise_t _x_last_noise; 00269 00270 lis2dw12_ctx_t _reg_ctx; 00271 }; 00272 00273 #ifdef __cplusplus 00274 extern "C" { 00275 #endif 00276 int32_t LIS2DW12_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite); 00277 int32_t LIS2DW12_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead); 00278 #ifdef __cplusplus 00279 } 00280 #endif 00281 00282 #endif
Generated on Thu Jul 14 2022 10:48:26 by
1.7.2