Firmware Library for X-NUCLEO-IKS01A1 (MEMS Inertial & Environmental Sensors) Expansion Board

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   MultiTech_Dragonfly_2015_ATT_Gov_Solutions_Hackathon_Example HelloWorld_IKS01A1 LoRaWAN-test-10secs ServoMotorDemo ... more

Fork of X_NUCLEO_IKS01A1 by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lsm6ds3_class.h Source File

lsm6ds3_class.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    lsm6ds3_class.h
00004  * @author  AST / EST
00005  * @version V0.0.1
00006  * @date    14-April-2015
00007  * @brief   Header file for component LSM6DS3
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *       without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036 */
00037 
00038 #ifndef __LSM6DS3_CLASS_H
00039 #define __LSM6DS3_CLASS_H
00040 
00041 /* Includes ------------------------------------------------------------------*/
00042 #include "mbed.h"
00043 #include "DevI2C.h"
00044 #include "lsm6ds3.h"
00045 #include "GyroSensor.h"
00046 #include "MotionSensor.h"
00047 
00048 /* Classes -------------------------------------------------------------------*/
00049 /** Class representing a LSM6DS3 sensor component
00050  */
00051 class LSM6DS3 : public GyroSensor, public MotionSensor {
00052  public:
00053     /** Constructor
00054      * @param[in] i2c device I2C to be used for communication
00055      * @param[in] irq_pin pin name for free fall detection interrupt
00056      */
00057         LSM6DS3(DevI2C &i2c, PinName irq_pin) : GyroSensor(), MotionSensor(), 
00058         dev_i2c(i2c), free_fall(irq_pin) {
00059     }
00060     
00061     /** Destructor
00062      */
00063         virtual ~LSM6DS3() {}
00064     
00065     /*** Interface Methods ***/
00066     virtual int init(void *init_struct) {
00067         return LSM6DS3_Init((IMU_6AXES_InitTypeDef*)init_struct);
00068     }
00069 
00070     virtual int read_id(uint8_t *xg_id) {
00071         return LSM6DS3_Read_XG_ID(xg_id);
00072     }
00073 
00074     virtual int get_x_axes(int32_t *pData) {
00075         return LSM6DS3_X_GetAxes(pData);
00076     }
00077 
00078     virtual int get_x_axes_raw(int16_t *pData) {
00079         return LSM6DS3_X_GetAxesRaw(pData);
00080     }
00081 
00082     virtual int get_g_axes(int32_t *pData) {
00083         return LSM6DS3_G_GetAxes(pData);
00084     }
00085 
00086     virtual int get_g_axes_raw(int16_t *pData) {
00087         return LSM6DS3_G_GetAxesRaw(pData);
00088     }
00089 
00090     virtual int get_x_odr(float *odr) {
00091         return LSM6DS3_X_Get_ODR(odr);
00092     }
00093 
00094     virtual int set_x_odr(float odr) {
00095         return LSM6DS3_X_Set_ODR(odr);
00096     }
00097 
00098     virtual int get_x_sensitivity(float *pfData) {
00099         return LSM6DS3_X_GetSensitivity(pfData);
00100     }
00101 
00102     virtual int get_x_fs(float *fullScale) {
00103         return LSM6DS3_X_Get_FS(fullScale);
00104     }
00105 
00106     virtual int set_x_fs(float fullScale) {
00107         return LSM6DS3_X_Set_FS(fullScale);
00108     }
00109 
00110     virtual int get_g_odr(float *odr) {
00111         return LSM6DS3_G_Get_ODR(odr);
00112     }
00113 
00114     virtual int set_g_odr(float odr) {
00115         return LSM6DS3_G_Set_ODR(odr);
00116     }
00117 
00118     virtual int get_g_sensitivity(float *pfData) {
00119         return LSM6DS3_G_GetSensitivity(pfData);
00120     }
00121 
00122     virtual int get_g_fs(float *fullScale) {
00123         return LSM6DS3_G_Get_FS(fullScale);
00124     }
00125 
00126     virtual int set_g_fs(float fullScale) {
00127         return LSM6DS3_G_Set_FS(fullScale);
00128     }
00129 
00130     /* Additional Public Methods */
00131     /**
00132      * @brief  Enable free fall detection
00133      * @return IMU_6AXES_OK in case of success, an error code otherwise
00134      */
00135     IMU_6AXES_StatusTypeDef Enable_Free_Fall_Detection(void) {
00136         return LSM6DS3_Enable_Free_Fall_Detection();
00137     }
00138 
00139     /**
00140      * @brief  Disable free fall detection
00141      * @return IMU_6AXES_OK in case of success, an error code otherwise
00142      */
00143     IMU_6AXES_StatusTypeDef Disable_Free_Fall_Detection(void) {
00144         return LSM6DS3_Disable_Free_Fall_Detection();
00145     }
00146 
00147     /**
00148      * @brief       Get status of free fall detection
00149      * @param[out]  status the pointer where the status of free fall detection is stored; 
00150      *              0 means no detection, 1 means detection happened
00151      * @return      IMU_6AXES_OK in case of success, an error code otherwise
00152      */
00153     IMU_6AXES_StatusTypeDef Get_Status_Free_Fall_Detection(uint8_t *status) {
00154         return LSM6DS3_Get_Status_Free_Fall_Detection(status);
00155     }
00156     
00157     /** Attach a function to call when a free fall is detected
00158      *
00159      *  @param[in] fptr A pointer to a void function, or 0 to set as none
00160      */
00161     void Attach_Free_Fall_Detection_IRQ(void (*fptr)(void)) {
00162         free_fall.rise(fptr);
00163     }
00164 
00165     /** Enable Free Fall IRQ
00166      */
00167     void Enable_Free_Fall_Detection_IRQ(void) {
00168         free_fall.enable_irq();
00169     }
00170 
00171     /** Disable free Fall IRQ
00172      */
00173     void Disable_Free_Fall_Detection_IRQ(void) {
00174         free_fall.disable_irq();
00175     }
00176 
00177  protected:
00178     /*** Methods ***/
00179     IMU_6AXES_StatusTypeDef LSM6DS3_Init(IMU_6AXES_InitTypeDef *LSM6DS3_Init);
00180     IMU_6AXES_StatusTypeDef LSM6DS3_Read_XG_ID(uint8_t *xg_id);
00181     IMU_6AXES_StatusTypeDef LSM6DS3_X_GetAxes(int32_t *pData);
00182     IMU_6AXES_StatusTypeDef LSM6DS3_X_GetAxesRaw(int16_t *pData);
00183     IMU_6AXES_StatusTypeDef LSM6DS3_G_GetAxes(int32_t *pData);
00184     IMU_6AXES_StatusTypeDef LSM6DS3_G_GetAxesRaw(int16_t *pData);
00185     IMU_6AXES_StatusTypeDef LSM6DS3_X_Get_ODR( float *odr );
00186     IMU_6AXES_StatusTypeDef LSM6DS3_X_Set_ODR( float odr );
00187     IMU_6AXES_StatusTypeDef LSM6DS3_X_GetSensitivity( float *pfData );
00188     IMU_6AXES_StatusTypeDef LSM6DS3_X_Get_FS( float *fullScale );
00189     IMU_6AXES_StatusTypeDef LSM6DS3_X_Set_FS( float fullScale );
00190     IMU_6AXES_StatusTypeDef LSM6DS3_G_Get_ODR( float *odr );
00191     IMU_6AXES_StatusTypeDef LSM6DS3_G_Set_ODR( float odr );
00192     IMU_6AXES_StatusTypeDef LSM6DS3_G_GetSensitivity( float *pfData );
00193     IMU_6AXES_StatusTypeDef LSM6DS3_G_Get_FS( float *fullScale );
00194     IMU_6AXES_StatusTypeDef LSM6DS3_G_Set_FS( float fullScale );
00195     IMU_6AXES_StatusTypeDef LSM6DS3_Enable_Free_Fall_Detection( void );
00196     IMU_6AXES_StatusTypeDef LSM6DS3_Disable_Free_Fall_Detection( void );
00197     IMU_6AXES_StatusTypeDef LSM6DS3_Get_Status_Free_Fall_Detection( uint8_t *status );
00198 
00199     IMU_6AXES_StatusTypeDef LSM6DS3_Common_Sensor_Enable(void);
00200     IMU_6AXES_StatusTypeDef LSM6DS3_X_Set_Axes_Status(uint8_t enableX, uint8_t enableY, uint8_t enableZ);
00201     IMU_6AXES_StatusTypeDef LSM6DS3_G_Set_Axes_Status(uint8_t enableX, uint8_t enableY, uint8_t enableZ);
00202 
00203     /**
00204      * @brief  Configures LSM6DS3 interrupt lines for NUCLEO boards
00205      */
00206     void LSM6DS3_IO_ITConfig(void)
00207     {
00208         free_fall.mode(PullNone); /* be precise about pin mode */
00209     }
00210 
00211     /**
00212      * @brief  Configures LSM6DS3 I2C interface
00213      * @return IMU_6AXES_OK in case of success, an error code otherwise
00214      */
00215     IMU_6AXES_StatusTypeDef LSM6DS3_IO_Init(void)
00216     {
00217         return IMU_6AXES_OK; /* done in constructor */
00218     }
00219 
00220     /**
00221      * @brief      Utility function to read data from LSM6DS3
00222      * @param[out] pBuffer pointer to the byte-array to read data in to
00223      * @param[in]  RegisterAddr specifies internal address register to read from.
00224      * @param[in]  NumByteToRead number of bytes to be read.
00225      * @retval     IMU_6AXES_OK if ok, 
00226      * @retval     IMU_6AXES_ERROR if an I2C error has occured
00227      */
00228     IMU_6AXES_StatusTypeDef LSM6DS3_IO_Read(uint8_t* pBuffer, 
00229                           uint8_t RegisterAddr, uint16_t NumByteToRead)
00230     {
00231         int ret = dev_i2c.i2c_read(pBuffer,
00232                        LSM6DS3_XG_MEMS_ADDRESS,
00233                        RegisterAddr,
00234                        NumByteToRead);
00235         if(ret != 0) {
00236             return IMU_6AXES_ERROR;
00237         }
00238         return IMU_6AXES_OK;
00239     }
00240     
00241     /**
00242      * @brief      Utility function to write data to LSM6DS3
00243      * @param[in]  pBuffer pointer to the byte-array data to send
00244      * @param[in]  RegisterAddr specifies internal address register to read from.
00245      * @param[in]  NumByteToWrite number of bytes to write.
00246      * @retval     IMU_6AXES_OK if ok, 
00247      * @retval     IMU_6AXES_ERROR if an I2C error has occured
00248      */
00249     IMU_6AXES_StatusTypeDef LSM6DS3_IO_Write(uint8_t* pBuffer, 
00250                            uint8_t RegisterAddr, uint16_t NumByteToWrite)
00251     {
00252         int ret = dev_i2c.i2c_write(pBuffer,
00253                         LSM6DS3_XG_MEMS_ADDRESS,
00254                         RegisterAddr,
00255                         NumByteToWrite);
00256         if(ret != 0) {
00257             return IMU_6AXES_ERROR;
00258         }
00259         return IMU_6AXES_OK;
00260     }
00261     
00262     /*** Instance Variables ***/
00263     /* IO Device */
00264     DevI2C &dev_i2c;
00265 
00266     /* Free Fall Detection IRQ */
00267     InterruptIn free_fall;
00268 };
00269 
00270 #endif // __LSM6DS3_CLASS_H