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 LSM303AGRMagSensor.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 magnetometer 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 __LSM303AGRMagSensor_H__
nikapov 0:ec6e59cc6f40 42 #define __LSM303AGRMagSensor_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_mag_driver.h"
mapellil 2:e37be5550633 49 #include "LSM303AGR_acc_driver.h"
nikapov 0:ec6e59cc6f40 50 #include "MagneticSensor.h"
mapellil 1:86d530a7f949 51 #include <assert.h>
nikapov 0:ec6e59cc6f40 52
nikapov 0:ec6e59cc6f40 53 /* Class Declaration ---------------------------------------------------------*/
nikapov 0:ec6e59cc6f40 54
nikapov 0:ec6e59cc6f40 55 /**
nikapov 0:ec6e59cc6f40 56 * Abstract class of an LSM303AGR Inertial Measurement Unit (IMU) 6 axes
nikapov 0:ec6e59cc6f40 57 * sensor.
nikapov 0:ec6e59cc6f40 58 */
nikapov 0:ec6e59cc6f40 59 class LSM303AGRMagSensor : public MagneticSensor
nikapov 0:ec6e59cc6f40 60 {
nikapov 0:ec6e59cc6f40 61 public:
mapellil 2:e37be5550633 62 LSM303AGRMagSensor(SPI *spi, PinName cs_pin, PinName intmag_pin=NC); // SPI3W ONLY
mapellil 2:e37be5550633 63 LSM303AGRMagSensor(DevI2C *i2c, uint8_t address=LSM303AGR_MAG_I2C_ADDRESS, PinName intmag_pin=NC);
nikapov 0:ec6e59cc6f40 64 virtual int init(void *init);
nikapov 0:ec6e59cc6f40 65 virtual int read_id(uint8_t *id);
nikapov 0:ec6e59cc6f40 66 virtual int get_m_axes(int32_t *pData);
nikapov 0:ec6e59cc6f40 67 virtual int get_m_axes_raw(int16_t *pData);
nikapov 0:ec6e59cc6f40 68 int enable(void);
nikapov 0:ec6e59cc6f40 69 int disable(void);
nikapov 0:ec6e59cc6f40 70 int get_m_sensitivity(float *pfData);
nikapov 0:ec6e59cc6f40 71 int get_m_odr(float *odr);
nikapov 0:ec6e59cc6f40 72 int set_m_odr(float odr);
nikapov 0:ec6e59cc6f40 73 int get_m_fs(float *fullScale);
nikapov 0:ec6e59cc6f40 74 int set_m_fs(float fullScale);
nikapov 0:ec6e59cc6f40 75 int read_reg(uint8_t reg, uint8_t *data);
nikapov 0:ec6e59cc6f40 76 int write_reg(uint8_t reg, uint8_t data);
nikapov 0:ec6e59cc6f40 77
nikapov 0:ec6e59cc6f40 78 /**
nikapov 0:ec6e59cc6f40 79 * @brief Utility function to read data.
nikapov 0:ec6e59cc6f40 80 * @param pBuffer: pointer to data to be read.
nikapov 0:ec6e59cc6f40 81 * @param RegisterAddr: specifies internal address register to be read.
nikapov 0:ec6e59cc6f40 82 * @param NumByteToRead: number of bytes to be read.
nikapov 0:ec6e59cc6f40 83 * @retval 0 if ok, an error code otherwise.
nikapov 0:ec6e59cc6f40 84 */
nikapov 0:ec6e59cc6f40 85 uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
nikapov 0:ec6e59cc6f40 86 {
mapellil 2:e37be5550633 87 if (_dev_spi) {
mapellil 2:e37be5550633 88 /* Write Reg Address */
mapellil 2:e37be5550633 89 _dev_spi->lock();
mapellil 2:e37be5550633 90 _cs_pin = 0;
mapellil 2:e37be5550633 91 /* Write RD Reg Address with RD bit*/
mapellil 2:e37be5550633 92 uint8_t TxByte = RegisterAddr | 0x80;
mapellil 2:e37be5550633 93 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
mapellil 2:e37be5550633 94 _cs_pin = 1;
mapellil 2:e37be5550633 95 _dev_spi->unlock();
mapellil 2:e37be5550633 96 return 0;
mapellil 2:e37be5550633 97 }
mapellil 2:e37be5550633 98 if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
mapellil 2:e37be5550633 99 return 1;
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 2:e37be5550633 111 if (_dev_spi) {
mapellil 2:e37be5550633 112 _dev_spi->lock();
mapellil 2:e37be5550633 113 _cs_pin = 0;
mapellil 2:e37be5550633 114 int data = _dev_spi->write(RegisterAddr);
mapellil 2:e37be5550633 115 _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);
mapellil 2:e37be5550633 116 _cs_pin = 1;
mapellil 2:e37be5550633 117 _dev_spi->unlock();
mapellil 2:e37be5550633 118 return data;
mapellil 2:e37be5550633 119 }
mapellil 2:e37be5550633 120 if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
mapellil 2:e37be5550633 121 return 1;
nikapov 0:ec6e59cc6f40 122 }
nikapov 0:ec6e59cc6f40 123
nikapov 0:ec6e59cc6f40 124 private:
nikapov 0:ec6e59cc6f40 125
nikapov 0:ec6e59cc6f40 126 /* Helper classes. */
mapellil 1:86d530a7f949 127 DevI2C *_dev_i2c;
mapellil 2:e37be5550633 128 SPI *_dev_spi;
nikapov 0:ec6e59cc6f40 129
nikapov 0:ec6e59cc6f40 130 /* Configuration */
nikapov 0:ec6e59cc6f40 131 uint8_t _address;
mapellil 1:86d530a7f949 132 DigitalOut _cs_pin;
mapellil 2:e37be5550633 133 InterruptIn _intmag_pin;
nikapov 0:ec6e59cc6f40 134 };
nikapov 0:ec6e59cc6f40 135
nikapov 0:ec6e59cc6f40 136 #ifdef __cplusplus
nikapov 0:ec6e59cc6f40 137 extern "C" {
nikapov 0:ec6e59cc6f40 138 #endif
nikapov 0:ec6e59cc6f40 139 uint8_t LSM303AGR_MAG_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
nikapov 0:ec6e59cc6f40 140 uint8_t LSM303AGR_MAG_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );
nikapov 0:ec6e59cc6f40 141 #ifdef __cplusplus
nikapov 0:ec6e59cc6f40 142 }
nikapov 0:ec6e59cc6f40 143 #endif
nikapov 0:ec6e59cc6f40 144
nikapov 0:ec6e59cc6f40 145 #endif