LELEC2811 - I&S / LIS2DW12

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_IKS01A3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LIS2DW12Sensor.h Source File

LIS2DW12Sensor.h

Go to the documentation of this file.
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>&copy; 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     virtual int get_x_bw_filt(uint8_t* bw_filt);
00109     virtual int set_x_bw_filt(uint8_t bw_filt);
00110     virtual int get_x_power_mode(uint8_t* lp_mode, uint8_t* mode, uint8_t* low_noise);
00111     virtual int set_x_power_mode(uint8_t power_mode);
00112     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);
00113     int enable_x(void);
00114     int disable_x(void);
00115     int enable_wake_up_detection(void);
00116     int disable_wake_up_detection(void);
00117     int set_wake_up_threshold(uint8_t thr);
00118     int set_wake_up_duration(uint8_t dur);
00119     int enable_inactivity_detection(void);
00120     int disable_inactivity_detection(void);
00121     int set_sleep_duration(uint8_t dur);
00122     int enable_6d_orientation(void);
00123     int disable_6d_orientation(void);
00124     int set_6d_orientation_threshold(uint8_t thr);
00125     int get_6d_orientation_xl(uint8_t *xl);
00126     int get_6d_orientation_xh(uint8_t *xh);
00127     int get_6d_orientation_yl(uint8_t *yl);
00128     int get_6d_orientation_yh(uint8_t *yh);
00129     int get_6d_orientation_zl(uint8_t *zl);
00130     int get_6d_orientation_zh(uint8_t *zh);
00131     int get_event_status(LIS2DW12_Event_Status_t *status);
00132     int get_fifo_num_samples(uint16_t *num_samples);
00133     int set_fifo_mode(uint8_t mode);
00134     int read_reg(uint8_t reg, uint8_t *data);
00135     int write_reg(uint8_t reg, uint8_t data);
00136 
00137     /**
00138      * @brief  Attaching an interrupt handler to the INT1 interrupt.
00139      * @param  fptr An interrupt handler.
00140      * @retval None.
00141      */
00142     void attach_int1_irq(void (*fptr)(void))
00143     {
00144         _int1_irq.rise(fptr);
00145     }
00146 
00147     /**
00148      * @brief  Enabling the INT1 interrupt handling.
00149      * @param  None.
00150      * @retval None.
00151      */
00152     void enable_int1_irq(void)
00153     {
00154         _int1_irq.enable_irq();
00155     }
00156 
00157     /**
00158      * @brief  Disabling the INT1 interrupt handling.
00159      * @param  None.
00160      * @retval None.
00161      */
00162     void disable_int1_irq(void)
00163     {
00164         _int1_irq.disable_irq();
00165     }
00166 
00167     /**
00168      * @brief  Attaching an interrupt handler to the INT2 interrupt.
00169      * @param  fptr An interrupt handler.
00170      * @retval None.
00171      */
00172     void attach_int2_irq(void (*fptr)(void))
00173     {
00174         _int2_irq.rise(fptr);
00175     }
00176 
00177     /**
00178      * @brief  Enabling the INT2 interrupt handling.
00179      * @param  None.
00180      * @retval None.
00181      */
00182     void enable_int2_irq(void)
00183     {
00184         _int2_irq.enable_irq();
00185     }
00186 
00187     /**
00188      * @brief  Disabling the INT2 interrupt handling.
00189      * @param  None.
00190      * @retval None.
00191      */
00192     void disable_int2_irq(void)
00193     {
00194         _int2_irq.disable_irq();
00195     }
00196 
00197     /**
00198      * @brief Utility function to read data.
00199      * @param  pBuffer: pointer to data to be read.
00200      * @param  RegisterAddr: specifies internal address register to be read.
00201      * @param  NumByteToRead: number of bytes to be read.
00202      * @retval 0 if ok, an error code otherwise.
00203      */
00204     uint8_t io_read(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
00205     {
00206         if (_dev_spi) {
00207             /* Write Reg Address */
00208             _dev_spi->lock();
00209             _cs_pin = 0;
00210             if (_spi_type == SPI4W) {
00211                 _dev_spi->write(RegisterAddr | 0x80);
00212                 for (int i = 0; i < NumByteToRead; i++) {
00213                     *(pBuffer + i) = _dev_spi->write(0x00);
00214                 }
00215             } else if (_spi_type == SPI3W) {
00216                 /* Write RD Reg Address with RD bit*/
00217                 uint8_t TxByte = RegisterAddr | 0x80;
00218                 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
00219             }
00220             _cs_pin = 1;
00221             _dev_spi->unlock();
00222             return 0;
00223         }
00224         if (_dev_i2c) {
00225             return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
00226         }
00227         return 1;
00228     }
00229 
00230     /**
00231      * @brief Utility function to write data.
00232      * @param  pBuffer: pointer to data to be written.
00233      * @param  RegisterAddr: specifies internal address register to be written.
00234      * @param  NumByteToWrite: number of bytes to write.
00235      * @retval 0 if ok, an error code otherwise.
00236      */
00237     uint8_t io_write(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
00238     {
00239         if (_dev_spi) {
00240             _dev_spi->lock();
00241             _cs_pin = 0;
00242             _dev_spi->write(RegisterAddr);
00243             _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);
00244             _cs_pin = 1;
00245             _dev_spi->unlock();
00246             return 0;
00247         }
00248         if (_dev_i2c) {
00249             return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
00250         }
00251         return 1;
00252     }
00253 
00254 private:
00255     int set_x_odr_when_enabled(float odr, LIS2DW12_Operating_Mode_t mode, LIS2DW12_Low_Noise_t noise);
00256     int set_x_odr_when_disabled(float odr, LIS2DW12_Operating_Mode_t mode, LIS2DW12_Low_Noise_t noise);
00257 
00258     /* Helper classes. */
00259     DevI2C *_dev_i2c;
00260     SPI    *_dev_spi;
00261 
00262     /* Configuration */
00263     uint8_t _address;
00264     DigitalOut  _cs_pin;
00265     InterruptIn _int1_irq;
00266     InterruptIn _int2_irq;
00267     SPI_type_t _spi_type;
00268 
00269     uint8_t _x_is_enabled;
00270     float _x_last_odr;
00271     LIS2DW12_Operating_Mode_t _x_last_operating_mode;
00272     LIS2DW12_Low_Noise_t _x_last_noise;
00273 
00274     lis2dw12_ctx_t _reg_ctx;
00275 };
00276 
00277 #ifdef __cplusplus
00278 extern "C" {
00279 #endif
00280 int32_t LIS2DW12_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite);
00281 int32_t LIS2DW12_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead);
00282 #ifdef __cplusplus
00283 }
00284 #endif
00285 
00286 #endif