ST / IIS2DLPC

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IIS2DLPCSensor.h Source File

IIS2DLPCSensor.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    IIS2DLPCSensor.h
00004  * @author  CLab
00005  * @version V1.0.0
00006  * @date    15 November 2018
00007  * @brief   Abstract Class of an IIS2DLPC 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 __IIS2DLPCSensor_H__
00043 #define __IIS2DLPCSensor_H__
00044 
00045 
00046 /* Includes ------------------------------------------------------------------*/
00047 
00048 #include "DevI2C.h"
00049 #include "iis2dlpc_reg.h"
00050 #include "MotionSensor.h"
00051 #include <assert.h>
00052 
00053 /* Defines -------------------------------------------------------------------*/
00054 
00055 #define IIS2DLPC_ACC_SENSITIVITY_FOR_FS_2G_LOPOW1_MODE   0.976f  /**< Sensitivity value for 2g full scale, Low-power1 mode [mg/LSB] */
00056 #define IIS2DLPC_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 IIS2DLPC_ACC_SENSITIVITY_FOR_FS_4G_LOPOW1_MODE   1.952f  /**< Sensitivity value for 4g full scale, Low-power1 mode [mg/LSB] */
00059 #define IIS2DLPC_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 IIS2DLPC_ACC_SENSITIVITY_FOR_FS_8G_LOPOW1_MODE   3.904f  /**< Sensitivity value for 8g full scale, Low-power1 mode [mg/LSB] */
00062 #define IIS2DLPC_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 IIS2DLPC_ACC_SENSITIVITY_FOR_FS_16G_LOPOW1_MODE  7.808f  /**< Sensitivity value for 16g full scale, Low-power1 mode [mg/LSB] */
00065 #define IIS2DLPC_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 {
00071   unsigned int WakeUpStatus : 1;
00072   unsigned int D6DOrientationStatus : 1;
00073   unsigned int SleepStatus : 1;
00074 } IIS2DLPC_Event_Status_t;
00075 
00076 typedef enum
00077 {
00078   IIS2DLPC_HIGH_PERFORMANCE_MODE,
00079   IIS2DLPC_LOW_POWER_MODE4,
00080   IIS2DLPC_LOW_POWER_MODE3,
00081   IIS2DLPC_LOW_POWER_MODE2,
00082   IIS2DLPC_LOW_POWER_MODE1
00083 } IIS2DLPC_Operating_Mode_t;
00084 
00085 typedef enum
00086 {
00087   IIS2DLPC_LOW_NOISE_DISABLE,
00088   IIS2DLPC_LOW_NOISE_ENABLE
00089 } IIS2DLPC_Low_Noise_t;
00090 
00091 /* Class Declaration ---------------------------------------------------------*/
00092    
00093 /**
00094  * Abstract class of an IIS2DLPC Inertial Measurement Unit (IMU) 3 axes
00095  * sensor.
00096  */
00097 class IIS2DLPCSensor : public MotionSensor
00098 {
00099   public:
00100     enum SPI_type_t {SPI3W, SPI4W};
00101     IIS2DLPCSensor(DevI2C *i2c, uint8_t address=IIS2DLPC_I2C_ADD_H, PinName int1_pin=NC, PinName int2_pin=NC);
00102     IIS2DLPCSensor(SPI *spi, PinName cs_pin, PinName int1_pin=NC, PinName int2_pin=NC, SPI_type_t spi_type=SPI4W);  
00103     virtual int init(void *init);
00104     virtual int read_id(uint8_t *id);
00105     virtual int get_x_axes(int32_t *pData);
00106     virtual int get_x_sensitivity(float *pfData);
00107     virtual int get_x_axes_raw(int16_t *pData);
00108     virtual int get_x_odr(float *odr);
00109     virtual int set_x_odr(float odr);
00110     virtual int get_x_fs(float *fullScale);
00111     virtual int set_x_fs(float fullScale);
00112     int set_x_odr_with_mode(float odr, IIS2DLPC_Operating_Mode_t mode=IIS2DLPC_HIGH_PERFORMANCE_MODE, IIS2DLPC_Low_Noise_t noise=IIS2DLPC_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(IIS2DLPC_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) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
00225         return 1;
00226     }
00227     
00228     /**
00229      * @brief Utility function to write data.
00230      * @param  pBuffer: pointer to data to be written.
00231      * @param  RegisterAddr: specifies internal address register to be written.
00232      * @param  NumByteToWrite: number of bytes to write.
00233      * @retval 0 if ok, an error code otherwise.
00234      */
00235     uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
00236     {  
00237         if (_dev_spi) { 
00238             _dev_spi->lock();
00239             _cs_pin = 0;
00240             _dev_spi->write(RegisterAddr);                    
00241             _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);                     
00242             _cs_pin = 1;                    
00243             _dev_spi->unlock();
00244             return 0;                    
00245         }        
00246         if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);    
00247         return 1;
00248     }
00249 
00250   private:
00251     int set_x_odr_when_enabled(float odr, IIS2DLPC_Operating_Mode_t mode, IIS2DLPC_Low_Noise_t noise);
00252     int set_x_odr_when_disabled(float odr, IIS2DLPC_Operating_Mode_t mode, IIS2DLPC_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     IIS2DLPC_Operating_Mode_t _x_last_operating_mode;
00268     IIS2DLPC_Low_Noise_t _x_last_noise;
00269 
00270     iis2dlpc_ctx_t _reg_ctx;
00271 };
00272 
00273 #ifdef __cplusplus
00274  extern "C" {
00275 #endif
00276 int32_t IIS2DLPC_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
00277 int32_t IIS2DLPC_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );
00278 #ifdef __cplusplus
00279   }
00280 #endif
00281 
00282 #endif