Ultra-compact high-performance eCompass module: ultra-low power 3D accelerometer and 3D magnetometer.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   HelloWorld_ST_Sensors MOTENV_Mbed mbed-os-mqtt-client LSM303AGR_JS ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LSM303AGRAccSensor.h Source File

LSM303AGRAccSensor.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    LSM303AGRAccSensor.h
00004  * @author  CLab
00005  * @version V1.0.0
00006  * @date    5 August 2016
00007  * @brief   Abstract Class of an LSM303AGR accelerometer sensor.
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2016 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 
00039 /* Prevent recursive inclusion -----------------------------------------------*/
00040 
00041 #ifndef __LSM303AGRAccSensor_H__
00042 #define __LSM303AGRAccSensor_H__
00043 
00044 
00045 /* Includes ------------------------------------------------------------------*/
00046 
00047 #include "DevI2C.h"
00048 #include "LSM303AGR_acc_driver.h"
00049 #include "MotionSensor.h"
00050 #include <assert.h>
00051 
00052 /* Defines -------------------------------------------------------------------*/
00053 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_2G_NORMAL_MODE               3.900f  /**< Sensitivity value for 2 g full scale and normal mode [mg/LSB] */
00054 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_2G_HIGH_RESOLUTION_MODE      0.980f  /**< Sensitivity value for 2 g full scale and high resolution mode [mg/LSB] */
00055 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_2G_LOW_POWER_MODE           15.630f  /**< Sensitivity value for 2 g full scale and low power mode [mg/LSB] */
00056 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_4G_NORMAL_MODE               7.820f  /**< Sensitivity value for 4 g full scale and normal mode [mg/LSB] */
00057 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_4G_HIGH_RESOLUTION_MODE      1.950f  /**< Sensitivity value for 4 g full scale and high resolution mode [mg/LSB] */
00058 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_4G_LOW_POWER_MODE           31.260f  /**< Sensitivity value for 4 g full scale and low power mode [mg/LSB] */
00059 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_8G_NORMAL_MODE              15.630f  /**< Sensitivity value for 8 g full scale and normal mode [mg/LSB] */
00060 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_8G_HIGH_RESOLUTION_MODE      3.900f  /**< Sensitivity value for 8 g full scale and high resolution mode [mg/LSB] */
00061 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_8G_LOW_POWER_MODE           62.520f  /**< Sensitivity value for 8 g full scale and low power mode [mg/LSB] */
00062 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_16G_NORMAL_MODE             46.900f  /**< Sensitivity value for 16 g full scale and normal mode [mg/LSB] */
00063 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_16G_HIGH_RESOLUTION_MODE    11.720f  /**< Sensitivity value for 16 g full scale and high resolution mode [mg/LSB] */
00064 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_16G_LOW_POWER_MODE         187.580f  /**< Sensitivity value for 16 g full scale and low power mode [mg/LSB] */
00065 
00066 /* Class Declaration ---------------------------------------------------------*/
00067 
00068 /**
00069  * Abstract class of an LSM303AGR Inertial Measurement Unit (IMU) 6 axes
00070  * sensor.
00071  */
00072 class LSM303AGRAccSensor : public MotionSensor
00073 {
00074   public:
00075     LSM303AGRAccSensor(SPI *spi, PinName cs_pin, PinName int1_pin=NC, PinName int2_pin=NC); // SPI3W ONLY
00076     LSM303AGRAccSensor(DevI2C *i2c, uint8_t address=LSM303AGR_ACC_I2C_ADDRESS, PinName int1_pin=NC, PinName int2_pin=NC);
00077     virtual int init(void *init);
00078     virtual int read_id(uint8_t *id);
00079     virtual int get_x_axes(int32_t *pData);
00080     virtual int get_x_axes_raw(int16_t *pData);
00081     virtual int get_x_sensitivity(float *pfData);
00082     virtual int get_x_odr(float *odr);
00083     virtual int set_x_odr(float odr);
00084     virtual int get_x_fs(float *fullScale);
00085     virtual int set_x_fs(float fullScale);
00086     int enable(void);
00087     int disable(void);
00088     int read_reg(uint8_t reg, uint8_t *data);
00089     int write_reg(uint8_t reg, uint8_t data);
00090     
00091     /**
00092      * @brief Utility function to read data.
00093      * @param  pBuffer: pointer to data to be read.
00094      * @param  RegisterAddr: specifies internal address register to be read.
00095      * @param  NumByteToRead: number of bytes to be read.
00096      * @retval 0 if ok, an error code otherwise.
00097      */
00098     uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
00099     {
00100         if (_dev_spi) {
00101         /* Write Reg Address */
00102             _dev_spi->lock();
00103             _cs_pin = 0;           
00104             /* Write RD Reg Address with RD bit*/
00105             uint8_t TxByte = RegisterAddr | 0x80;    
00106             _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
00107             _cs_pin = 1;
00108             _dev_spi->unlock(); 
00109             return 0;
00110         }                       
00111         if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
00112         return 1;
00113     }
00114     
00115     /**
00116      * @brief Utility function to write data.
00117      * @param  pBuffer: pointer to data to be written.
00118      * @param  RegisterAddr: specifies internal address register to be written.
00119      * @param  NumByteToWrite: number of bytes to write.
00120      * @retval 0 if ok, an error code otherwise.
00121      */
00122     uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
00123     {
00124         if (_dev_spi) { 
00125             _dev_spi->lock();
00126             _cs_pin = 0;
00127             _dev_spi->write(RegisterAddr);                    
00128             _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);                     
00129             _cs_pin = 1;                    
00130             _dev_spi->unlock();
00131             return 0;                    
00132         }                
00133         if (_dev_i2c) return (uint8_t)_dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
00134         return 1;
00135     }
00136 
00137   private:
00138     int set_x_odr_when_enabled(float odr);
00139     int set_x_odr_when_disabled(float odr);
00140     int get_x_sensitivity_normal_mode(float *sensitivity );
00141     int get_x_sensitivity_lp_mode(float *sensitivity );
00142     int get_x_sensitivity_hr_mode(float *sensitivity );
00143 
00144     /* Helper classes. */
00145     DevI2C *_dev_i2c;
00146     SPI    *_dev_spi;    
00147     
00148     /* Configuration */
00149     uint8_t _address;
00150     DigitalOut  _cs_pin;        
00151     InterruptIn _int1_pin; 
00152     InterruptIn _int2_pin;  
00153     
00154     uint8_t _is_enabled;
00155     float _last_odr;
00156 };
00157 
00158 #ifdef __cplusplus
00159  extern "C" {
00160 #endif
00161 uint8_t LSM303AGR_ACC_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
00162 uint8_t LSM303AGR_ACC_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );
00163 #ifdef __cplusplus
00164   }
00165 #endif
00166 
00167 #endif