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

Committer:
mapellil
Date:
Wed Sep 27 16:49:51 2017 +0200
Revision:
1:86d530a7f949
Parent:
0:ec6e59cc6f40
Child:
2:e37be5550633
Fixed NonCopyable

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikapov 0:ec6e59cc6f40 1 /**
nikapov 0:ec6e59cc6f40 2 ******************************************************************************
nikapov 0:ec6e59cc6f40 3 * @file LSM303AGRAccSensor.h
nikapov 0:ec6e59cc6f40 4 * @author CLab
nikapov 0:ec6e59cc6f40 5 * @version V1.0.0
nikapov 0:ec6e59cc6f40 6 * @date 5 August 2016
nikapov 0:ec6e59cc6f40 7 * @brief Abstract Class of an LSM303AGR accelerometer sensor.
nikapov 0:ec6e59cc6f40 8 ******************************************************************************
nikapov 0:ec6e59cc6f40 9 * @attention
nikapov 0:ec6e59cc6f40 10 *
nikapov 0:ec6e59cc6f40 11 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
nikapov 0:ec6e59cc6f40 12 *
nikapov 0:ec6e59cc6f40 13 * Redistribution and use in source and binary forms, with or without modification,
nikapov 0:ec6e59cc6f40 14 * are permitted provided that the following conditions are met:
nikapov 0:ec6e59cc6f40 15 * 1. Redistributions of source code must retain the above copyright notice,
nikapov 0:ec6e59cc6f40 16 * this list of conditions and the following disclaimer.
nikapov 0:ec6e59cc6f40 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
nikapov 0:ec6e59cc6f40 18 * this list of conditions and the following disclaimer in the documentation
nikapov 0:ec6e59cc6f40 19 * and/or other materials provided with the distribution.
nikapov 0:ec6e59cc6f40 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
nikapov 0:ec6e59cc6f40 21 * may be used to endorse or promote products derived from this software
nikapov 0:ec6e59cc6f40 22 * without specific prior written permission.
nikapov 0:ec6e59cc6f40 23 *
nikapov 0:ec6e59cc6f40 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
nikapov 0:ec6e59cc6f40 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
nikapov 0:ec6e59cc6f40 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
nikapov 0:ec6e59cc6f40 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
nikapov 0:ec6e59cc6f40 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nikapov 0:ec6e59cc6f40 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
nikapov 0:ec6e59cc6f40 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
nikapov 0:ec6e59cc6f40 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
nikapov 0:ec6e59cc6f40 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nikapov 0:ec6e59cc6f40 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nikapov 0:ec6e59cc6f40 34 *
nikapov 0:ec6e59cc6f40 35 ******************************************************************************
nikapov 0:ec6e59cc6f40 36 */
nikapov 0:ec6e59cc6f40 37
nikapov 0:ec6e59cc6f40 38
nikapov 0:ec6e59cc6f40 39 /* Prevent recursive inclusion -----------------------------------------------*/
nikapov 0:ec6e59cc6f40 40
nikapov 0:ec6e59cc6f40 41 #ifndef __LSM303AGRAccSensor_H__
nikapov 0:ec6e59cc6f40 42 #define __LSM303AGRAccSensor_H__
nikapov 0:ec6e59cc6f40 43
nikapov 0:ec6e59cc6f40 44
nikapov 0:ec6e59cc6f40 45 /* Includes ------------------------------------------------------------------*/
nikapov 0:ec6e59cc6f40 46
nikapov 0:ec6e59cc6f40 47 #include "DevI2C.h"
nikapov 0:ec6e59cc6f40 48 #include "LSM303AGR_acc_driver.h"
nikapov 0:ec6e59cc6f40 49 #include "MotionSensor.h"
mapellil 1:86d530a7f949 50 #include <assert.h>
nikapov 0:ec6e59cc6f40 51
nikapov 0:ec6e59cc6f40 52 /* Defines -------------------------------------------------------------------*/
nikapov 0:ec6e59cc6f40 53 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_2G_NORMAL_MODE 3.900f /**< Sensitivity value for 2 g full scale and normal mode [mg/LSB] */
nikapov 0:ec6e59cc6f40 54 #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] */
nikapov 0:ec6e59cc6f40 55 #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] */
nikapov 0:ec6e59cc6f40 56 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_4G_NORMAL_MODE 7.820f /**< Sensitivity value for 4 g full scale and normal mode [mg/LSB] */
nikapov 0:ec6e59cc6f40 57 #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] */
nikapov 0:ec6e59cc6f40 58 #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] */
nikapov 0:ec6e59cc6f40 59 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_8G_NORMAL_MODE 15.630f /**< Sensitivity value for 8 g full scale and normal mode [mg/LSB] */
nikapov 0:ec6e59cc6f40 60 #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] */
nikapov 0:ec6e59cc6f40 61 #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] */
nikapov 0:ec6e59cc6f40 62 #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_16G_NORMAL_MODE 46.900f /**< Sensitivity value for 16 g full scale and normal mode [mg/LSB] */
nikapov 0:ec6e59cc6f40 63 #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] */
nikapov 0:ec6e59cc6f40 64 #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] */
nikapov 0:ec6e59cc6f40 65
nikapov 0:ec6e59cc6f40 66 /* Class Declaration ---------------------------------------------------------*/
nikapov 0:ec6e59cc6f40 67
nikapov 0:ec6e59cc6f40 68 /**
nikapov 0:ec6e59cc6f40 69 * Abstract class of an LSM303AGR Inertial Measurement Unit (IMU) 6 axes
nikapov 0:ec6e59cc6f40 70 * sensor.
nikapov 0:ec6e59cc6f40 71 */
nikapov 0:ec6e59cc6f40 72 class LSM303AGRAccSensor : public MotionSensor
nikapov 0:ec6e59cc6f40 73 {
nikapov 0:ec6e59cc6f40 74 public:
mapellil 1:86d530a7f949 75 LSM303AGRAccSensor(DevI2C *i2c, uint8_t address=LSM303AGR_ACC_I2C_ADDRESS, PinName int1_pin=NC, PinName int2_pin=NC);
nikapov 0:ec6e59cc6f40 76 virtual int init(void *init);
nikapov 0:ec6e59cc6f40 77 virtual int read_id(uint8_t *id);
nikapov 0:ec6e59cc6f40 78 virtual int get_x_axes(int32_t *pData);
nikapov 0:ec6e59cc6f40 79 virtual int get_x_axes_raw(int16_t *pData);
nikapov 0:ec6e59cc6f40 80 virtual int get_x_sensitivity(float *pfData);
nikapov 0:ec6e59cc6f40 81 virtual int get_x_odr(float *odr);
nikapov 0:ec6e59cc6f40 82 virtual int set_x_odr(float odr);
nikapov 0:ec6e59cc6f40 83 virtual int get_x_fs(float *fullScale);
nikapov 0:ec6e59cc6f40 84 virtual int set_x_fs(float fullScale);
nikapov 0:ec6e59cc6f40 85 int enable(void);
nikapov 0:ec6e59cc6f40 86 int disable(void);
nikapov 0:ec6e59cc6f40 87 int read_reg(uint8_t reg, uint8_t *data);
nikapov 0:ec6e59cc6f40 88 int write_reg(uint8_t reg, uint8_t data);
nikapov 0:ec6e59cc6f40 89
nikapov 0:ec6e59cc6f40 90 /**
nikapov 0:ec6e59cc6f40 91 * @brief Utility function to read data.
nikapov 0:ec6e59cc6f40 92 * @param pBuffer: pointer to data to be read.
nikapov 0:ec6e59cc6f40 93 * @param RegisterAddr: specifies internal address register to be read.
nikapov 0:ec6e59cc6f40 94 * @param NumByteToRead: number of bytes to be read.
nikapov 0:ec6e59cc6f40 95 * @retval 0 if ok, an error code otherwise.
nikapov 0:ec6e59cc6f40 96 */
nikapov 0:ec6e59cc6f40 97 uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
nikapov 0:ec6e59cc6f40 98 {
mapellil 1:86d530a7f949 99 return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
nikapov 0:ec6e59cc6f40 100 }
nikapov 0:ec6e59cc6f40 101
nikapov 0:ec6e59cc6f40 102 /**
nikapov 0:ec6e59cc6f40 103 * @brief Utility function to write data.
nikapov 0:ec6e59cc6f40 104 * @param pBuffer: pointer to data to be written.
nikapov 0:ec6e59cc6f40 105 * @param RegisterAddr: specifies internal address register to be written.
nikapov 0:ec6e59cc6f40 106 * @param NumByteToWrite: number of bytes to write.
nikapov 0:ec6e59cc6f40 107 * @retval 0 if ok, an error code otherwise.
nikapov 0:ec6e59cc6f40 108 */
nikapov 0:ec6e59cc6f40 109 uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
nikapov 0:ec6e59cc6f40 110 {
mapellil 1:86d530a7f949 111 return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
nikapov 0:ec6e59cc6f40 112 }
nikapov 0:ec6e59cc6f40 113
nikapov 0:ec6e59cc6f40 114 private:
nikapov 0:ec6e59cc6f40 115 int set_x_odr_when_enabled(float odr);
nikapov 0:ec6e59cc6f40 116 int set_x_odr_when_disabled(float odr);
nikapov 0:ec6e59cc6f40 117 int get_x_sensitivity_normal_mode(float *sensitivity );
nikapov 0:ec6e59cc6f40 118 int get_x_sensitivity_lp_mode(float *sensitivity );
nikapov 0:ec6e59cc6f40 119 int get_x_sensitivity_hr_mode(float *sensitivity );
nikapov 0:ec6e59cc6f40 120
nikapov 0:ec6e59cc6f40 121 /* Helper classes. */
mapellil 1:86d530a7f949 122 DevI2C *_dev_i2c;
nikapov 0:ec6e59cc6f40 123
nikapov 0:ec6e59cc6f40 124 /* Configuration */
nikapov 0:ec6e59cc6f40 125 uint8_t _address;
mapellil 1:86d530a7f949 126 DigitalOut _cs_pin;
mapellil 1:86d530a7f949 127 InterruptIn _int1_pin;
mapellil 1:86d530a7f949 128 InterruptIn _int2_pin;
nikapov 0:ec6e59cc6f40 129
nikapov 0:ec6e59cc6f40 130 uint8_t _is_enabled;
nikapov 0:ec6e59cc6f40 131 float _last_odr;
nikapov 0:ec6e59cc6f40 132 };
nikapov 0:ec6e59cc6f40 133
nikapov 0:ec6e59cc6f40 134 #ifdef __cplusplus
nikapov 0:ec6e59cc6f40 135 extern "C" {
nikapov 0:ec6e59cc6f40 136 #endif
nikapov 0:ec6e59cc6f40 137 uint8_t LSM303AGR_ACC_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
nikapov 0:ec6e59cc6f40 138 uint8_t LSM303AGR_ACC_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );
nikapov 0:ec6e59cc6f40 139 #ifdef __cplusplus
nikapov 0:ec6e59cc6f40 140 }
nikapov 0:ec6e59cc6f40 141 #endif
nikapov 0:ec6e59cc6f40 142
nikapov 0:ec6e59cc6f40 143 #endif