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:
Mon Oct 02 09:29:33 2017 +0200
Revision:
2:e37be5550633
Parent:
1:86d530a7f949
Child:
4:6c7e83ae885e
Added SPI3/3Wires sensor support, some API modifcations

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 2:e37be5550633 75 LSM303AGRAccSensor(SPI *spi, PinName cs_pin, PinName int1_pin=NC, PinName int2_pin=NC); // SPI3W ONLY
mapellil 1:86d530a7f949 76 LSM303AGRAccSensor(DevI2C *i2c, uint8_t address=LSM303AGR_ACC_I2C_ADDRESS, PinName int1_pin=NC, PinName int2_pin=NC);
nikapov 0:ec6e59cc6f40 77 virtual int init(void *init);
nikapov 0:ec6e59cc6f40 78 virtual int read_id(uint8_t *id);
nikapov 0:ec6e59cc6f40 79 virtual int get_x_axes(int32_t *pData);
nikapov 0:ec6e59cc6f40 80 virtual int get_x_axes_raw(int16_t *pData);
nikapov 0:ec6e59cc6f40 81 virtual int get_x_sensitivity(float *pfData);
nikapov 0:ec6e59cc6f40 82 virtual int get_x_odr(float *odr);
nikapov 0:ec6e59cc6f40 83 virtual int set_x_odr(float odr);
nikapov 0:ec6e59cc6f40 84 virtual int get_x_fs(float *fullScale);
nikapov 0:ec6e59cc6f40 85 virtual int set_x_fs(float fullScale);
nikapov 0:ec6e59cc6f40 86 int enable(void);
nikapov 0:ec6e59cc6f40 87 int disable(void);
nikapov 0:ec6e59cc6f40 88 int read_reg(uint8_t reg, uint8_t *data);
nikapov 0:ec6e59cc6f40 89 int write_reg(uint8_t reg, uint8_t data);
nikapov 0:ec6e59cc6f40 90
nikapov 0:ec6e59cc6f40 91 /**
nikapov 0:ec6e59cc6f40 92 * @brief Utility function to read data.
nikapov 0:ec6e59cc6f40 93 * @param pBuffer: pointer to data to be read.
nikapov 0:ec6e59cc6f40 94 * @param RegisterAddr: specifies internal address register to be read.
nikapov 0:ec6e59cc6f40 95 * @param NumByteToRead: number of bytes to be read.
nikapov 0:ec6e59cc6f40 96 * @retval 0 if ok, an error code otherwise.
nikapov 0:ec6e59cc6f40 97 */
nikapov 0:ec6e59cc6f40 98 uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
nikapov 0:ec6e59cc6f40 99 {
mapellil 2:e37be5550633 100 if (_dev_spi) {
mapellil 2:e37be5550633 101 /* Write Reg Address */
mapellil 2:e37be5550633 102 _dev_spi->lock();
mapellil 2:e37be5550633 103 _cs_pin = 0;
mapellil 2:e37be5550633 104 /* Write RD Reg Address with RD bit*/
mapellil 2:e37be5550633 105 uint8_t TxByte = RegisterAddr | 0x80;
mapellil 2:e37be5550633 106 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
mapellil 2:e37be5550633 107 _cs_pin = 1;
mapellil 2:e37be5550633 108 _dev_spi->unlock();
mapellil 2:e37be5550633 109 return 0;
mapellil 2:e37be5550633 110 }
mapellil 2:e37be5550633 111 if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
mapellil 2:e37be5550633 112 return 1;
nikapov 0:ec6e59cc6f40 113 }
nikapov 0:ec6e59cc6f40 114
nikapov 0:ec6e59cc6f40 115 /**
nikapov 0:ec6e59cc6f40 116 * @brief Utility function to write data.
nikapov 0:ec6e59cc6f40 117 * @param pBuffer: pointer to data to be written.
nikapov 0:ec6e59cc6f40 118 * @param RegisterAddr: specifies internal address register to be written.
nikapov 0:ec6e59cc6f40 119 * @param NumByteToWrite: number of bytes to write.
nikapov 0:ec6e59cc6f40 120 * @retval 0 if ok, an error code otherwise.
nikapov 0:ec6e59cc6f40 121 */
nikapov 0:ec6e59cc6f40 122 uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
nikapov 0:ec6e59cc6f40 123 {
mapellil 2:e37be5550633 124 if (_dev_spi) {
mapellil 2:e37be5550633 125 _dev_spi->lock();
mapellil 2:e37be5550633 126 _cs_pin = 0;
mapellil 2:e37be5550633 127 int data = _dev_spi->write(RegisterAddr);
mapellil 2:e37be5550633 128 _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);
mapellil 2:e37be5550633 129 _cs_pin = 1;
mapellil 2:e37be5550633 130 _dev_spi->unlock();
mapellil 2:e37be5550633 131 return data;
mapellil 2:e37be5550633 132 }
mapellil 2:e37be5550633 133 if (_dev_i2c) return (uint8_t)_dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
mapellil 2:e37be5550633 134 return 1;
nikapov 0:ec6e59cc6f40 135 }
nikapov 0:ec6e59cc6f40 136
nikapov 0:ec6e59cc6f40 137 private:
nikapov 0:ec6e59cc6f40 138 int set_x_odr_when_enabled(float odr);
nikapov 0:ec6e59cc6f40 139 int set_x_odr_when_disabled(float odr);
nikapov 0:ec6e59cc6f40 140 int get_x_sensitivity_normal_mode(float *sensitivity );
nikapov 0:ec6e59cc6f40 141 int get_x_sensitivity_lp_mode(float *sensitivity );
nikapov 0:ec6e59cc6f40 142 int get_x_sensitivity_hr_mode(float *sensitivity );
nikapov 0:ec6e59cc6f40 143
nikapov 0:ec6e59cc6f40 144 /* Helper classes. */
mapellil 1:86d530a7f949 145 DevI2C *_dev_i2c;
mapellil 2:e37be5550633 146 SPI *_dev_spi;
nikapov 0:ec6e59cc6f40 147
nikapov 0:ec6e59cc6f40 148 /* Configuration */
nikapov 0:ec6e59cc6f40 149 uint8_t _address;
mapellil 1:86d530a7f949 150 DigitalOut _cs_pin;
mapellil 1:86d530a7f949 151 InterruptIn _int1_pin;
mapellil 1:86d530a7f949 152 InterruptIn _int2_pin;
nikapov 0:ec6e59cc6f40 153
nikapov 0:ec6e59cc6f40 154 uint8_t _is_enabled;
nikapov 0:ec6e59cc6f40 155 float _last_odr;
nikapov 0:ec6e59cc6f40 156 };
nikapov 0:ec6e59cc6f40 157
nikapov 0:ec6e59cc6f40 158 #ifdef __cplusplus
nikapov 0:ec6e59cc6f40 159 extern "C" {
nikapov 0:ec6e59cc6f40 160 #endif
nikapov 0:ec6e59cc6f40 161 uint8_t LSM303AGR_ACC_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
nikapov 0:ec6e59cc6f40 162 uint8_t LSM303AGR_ACC_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );
nikapov 0:ec6e59cc6f40 163 #ifdef __cplusplus
nikapov 0:ec6e59cc6f40 164 }
nikapov 0:ec6e59cc6f40 165 #endif
nikapov 0:ec6e59cc6f40 166
nikapov 0:ec6e59cc6f40 167 #endif