Ultra low-power, high performance 3-axis magnetometer.

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Dependents:   HelloWorld_ST_Sensors MOTENV_Mbed mbed-os-mqtt-client DISCO-IOT01_HomeEnv ... more

Committer:
mapellil
Date:
Mon Oct 02 09:28:58 2017 +0200
Revision:
3:a35a16f3da39
Parent:
2:b3212ad3c306
Child:
4:00493226e59f
Added SPI3/3Wires sensor support, some API modifcations

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikapov 0:7376fbeb1d4a 1 /**
nikapov 0:7376fbeb1d4a 2 ******************************************************************************
nikapov 0:7376fbeb1d4a 3 * @file lis3mdl_class.h
nikapov 0:7376fbeb1d4a 4 * @author AST / EST
nikapov 0:7376fbeb1d4a 5 * @version V0.0.1
nikapov 0:7376fbeb1d4a 6 * @date 14-April-2015
nikapov 0:7376fbeb1d4a 7 * @brief Header file for component LIS3MDL
nikapov 0:7376fbeb1d4a 8 ******************************************************************************
nikapov 0:7376fbeb1d4a 9 * @attention
nikapov 0:7376fbeb1d4a 10 *
nikapov 0:7376fbeb1d4a 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
nikapov 0:7376fbeb1d4a 12 *
nikapov 0:7376fbeb1d4a 13 * Redistribution and use in source and binary forms, with or without modification,
nikapov 0:7376fbeb1d4a 14 * are permitted provided that the following conditions are met:
nikapov 0:7376fbeb1d4a 15 * 1. Redistributions of source code must retain the above copyright notice,
nikapov 0:7376fbeb1d4a 16 * this list of conditions and the following disclaimer.
nikapov 0:7376fbeb1d4a 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
nikapov 0:7376fbeb1d4a 18 * this list of conditions and the following disclaimer in the documentation
nikapov 0:7376fbeb1d4a 19 * and/or other materials provided with the distribution.
nikapov 0:7376fbeb1d4a 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
nikapov 0:7376fbeb1d4a 21 * may be used to endorse or promote products derived from this software
nikapov 0:7376fbeb1d4a 22 * without specific prior written permission.
nikapov 0:7376fbeb1d4a 23 *
nikapov 0:7376fbeb1d4a 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
nikapov 0:7376fbeb1d4a 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
nikapov 0:7376fbeb1d4a 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
nikapov 0:7376fbeb1d4a 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
nikapov 0:7376fbeb1d4a 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nikapov 0:7376fbeb1d4a 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
nikapov 0:7376fbeb1d4a 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
nikapov 0:7376fbeb1d4a 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
nikapov 0:7376fbeb1d4a 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nikapov 0:7376fbeb1d4a 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nikapov 0:7376fbeb1d4a 34 *
nikapov 0:7376fbeb1d4a 35 ******************************************************************************
nikapov 0:7376fbeb1d4a 36 */
nikapov 0:7376fbeb1d4a 37
nikapov 0:7376fbeb1d4a 38 #ifndef __LIS3MDL_CLASS_H
nikapov 0:7376fbeb1d4a 39 #define __LIS3MDL_CLASS_H
nikapov 0:7376fbeb1d4a 40
nikapov 0:7376fbeb1d4a 41 /* Includes ------------------------------------------------------------------*/
nikapov 0:7376fbeb1d4a 42 #include "mbed.h"
nikapov 0:7376fbeb1d4a 43 #include "DevI2C.h"
nikapov 0:7376fbeb1d4a 44 #include "lis3mdl.h"
nikapov 0:7376fbeb1d4a 45 #include "MagneticSensor.h"
mapellil 3:a35a16f3da39 46 #include <assert.h>
nikapov 0:7376fbeb1d4a 47
nikapov 0:7376fbeb1d4a 48 /* Classes -------------------------------------------------------------------*/
nikapov 0:7376fbeb1d4a 49 /** Class representing a LIS3MDL sensor component
nikapov 0:7376fbeb1d4a 50 */
nikapov 0:7376fbeb1d4a 51 class LIS3MDL : public MagneticSensor {
nikapov 0:7376fbeb1d4a 52 public:
mapellil 3:a35a16f3da39 53 enum SPI_type_t {SPI3W, SPI4W};
mapellil 3:a35a16f3da39 54 // LIS3MDL(SPI *spi, PinName cs_pin, PinName int_pin=NC, SPI_type_t spi_type=SPI4W);
mapellil 3:a35a16f3da39 55 // LIS3MDL(DevI2C *i2c, uint8_t address=LPS22HB_ADDRESS_HIGH, PinName int_pin=NC);
mapellil 3:a35a16f3da39 56
nikapov 0:7376fbeb1d4a 57 /** Constructor
nikapov 0:7376fbeb1d4a 58 * @param[in] i2c device I2C to be used for communication
nikapov 0:7376fbeb1d4a 59 */
mapellil 3:a35a16f3da39 60 // LIS3MDL(DevI2C *i2c) : MagneticSensor(), dev_i2c(i2c) {
mapellil 3:a35a16f3da39 61 LIS3MDL(DevI2C *i2c) : dev_i2c(i2c) {
nikapov 0:7376fbeb1d4a 62 }
nikapov 0:7376fbeb1d4a 63
nikapov 0:7376fbeb1d4a 64 /** Destructor
nikapov 0:7376fbeb1d4a 65 */
nikapov 0:7376fbeb1d4a 66 virtual ~LIS3MDL() {}
nikapov 0:7376fbeb1d4a 67
nikapov 0:7376fbeb1d4a 68 /*** Interface Methods ***/
nikapov 0:7376fbeb1d4a 69 virtual int init(void *init_struct) {
nikapov 0:7376fbeb1d4a 70 return LIS3MDL_Init((MAGNETO_InitTypeDef*)init_struct);
nikapov 0:7376fbeb1d4a 71 }
nikapov 0:7376fbeb1d4a 72
nikapov 0:7376fbeb1d4a 73 virtual int read_id(uint8_t *m_id) {
nikapov 0:7376fbeb1d4a 74 return LIS3MDL_Read_M_ID(m_id);
nikapov 0:7376fbeb1d4a 75 }
nikapov 0:7376fbeb1d4a 76
nikapov 0:7376fbeb1d4a 77 virtual int get_m_axes(int32_t *pData) {
nikapov 0:7376fbeb1d4a 78 return LIS3MDL_M_GetAxes(pData);
nikapov 0:7376fbeb1d4a 79 }
nikapov 0:7376fbeb1d4a 80
nikapov 0:7376fbeb1d4a 81 virtual int get_m_axes_raw(int16_t *pData) {
nikapov 0:7376fbeb1d4a 82 return LIS3MDL_M_GetAxesRaw(pData);
nikapov 0:7376fbeb1d4a 83 }
nikapov 0:7376fbeb1d4a 84
nikapov 0:7376fbeb1d4a 85 protected:
nikapov 0:7376fbeb1d4a 86 /*** Methods ***/
nikapov 0:7376fbeb1d4a 87 MAGNETO_StatusTypeDef LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init);
nikapov 0:7376fbeb1d4a 88 MAGNETO_StatusTypeDef LIS3MDL_Read_M_ID(uint8_t *m_id);
nikapov 0:7376fbeb1d4a 89 MAGNETO_StatusTypeDef LIS3MDL_M_GetAxes(int32_t *pData);
nikapov 0:7376fbeb1d4a 90 MAGNETO_StatusTypeDef LIS3MDL_M_GetAxesRaw(int16_t *pData);
nikapov 0:7376fbeb1d4a 91
nikapov 0:7376fbeb1d4a 92 /**
nikapov 0:7376fbeb1d4a 93 * @brief Configures LIS3MDL interrupt lines for NUCLEO boards
nikapov 0:7376fbeb1d4a 94 */
nikapov 0:7376fbeb1d4a 95 void LIS3MDL_IO_ITConfig(void)
nikapov 0:7376fbeb1d4a 96 {
nikapov 0:7376fbeb1d4a 97 /* To be implemented */
nikapov 0:7376fbeb1d4a 98 }
nikapov 0:7376fbeb1d4a 99
nikapov 0:7376fbeb1d4a 100 /**
nikapov 0:7376fbeb1d4a 101 * @brief Configures LIS3MDL I2C interface
nikapov 0:7376fbeb1d4a 102 * @return MAGNETO_OK in case of success, an error code otherwise
nikapov 0:7376fbeb1d4a 103 */
nikapov 0:7376fbeb1d4a 104 MAGNETO_StatusTypeDef LIS3MDL_IO_Init(void)
nikapov 0:7376fbeb1d4a 105 {
nikapov 0:7376fbeb1d4a 106 return MAGNETO_OK; /* done in constructor */
nikapov 0:7376fbeb1d4a 107 }
nikapov 0:7376fbeb1d4a 108
nikapov 0:7376fbeb1d4a 109 /**
nikapov 0:7376fbeb1d4a 110 * @brief Utility function to read data from LIS3MDL
nikapov 0:7376fbeb1d4a 111 * @param[out] pBuffer pointer to the byte-array to read data in to
nikapov 0:7376fbeb1d4a 112 * @param[in] RegisterAddr specifies internal address register to read from.
nikapov 0:7376fbeb1d4a 113 * @param[in] NumByteToRead number of bytes to be read.
nikapov 0:7376fbeb1d4a 114 * @retval MAGNETO_OK if ok,
nikapov 0:7376fbeb1d4a 115 * @retval MAGNETO_ERROR if an I2C error has occured
nikapov 0:7376fbeb1d4a 116 */
nikapov 0:7376fbeb1d4a 117 MAGNETO_StatusTypeDef LIS3MDL_IO_Read(uint8_t* pBuffer,
nikapov 0:7376fbeb1d4a 118 uint8_t RegisterAddr, uint16_t NumByteToRead)
nikapov 0:7376fbeb1d4a 119 {
mapellil 2:b3212ad3c306 120 int ret = dev_i2c->i2c_read(pBuffer,
nikapov 0:7376fbeb1d4a 121 LIS3MDL_M_MEMS_ADDRESS,
nikapov 0:7376fbeb1d4a 122 RegisterAddr,
nikapov 0:7376fbeb1d4a 123 NumByteToRead);
nikapov 0:7376fbeb1d4a 124 if(ret != 0) {
nikapov 0:7376fbeb1d4a 125 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 126 }
nikapov 0:7376fbeb1d4a 127 return MAGNETO_OK;
nikapov 0:7376fbeb1d4a 128 }
nikapov 0:7376fbeb1d4a 129
nikapov 0:7376fbeb1d4a 130 /**
nikapov 0:7376fbeb1d4a 131 * @brief Utility function to write data to LIS3MDL
nikapov 0:7376fbeb1d4a 132 * @param[in] pBuffer pointer to the byte-array data to send
nikapov 0:7376fbeb1d4a 133 * @param[in] RegisterAddr specifies internal address register to read from.
nikapov 0:7376fbeb1d4a 134 * @param[in] NumByteToWrite number of bytes to write.
nikapov 0:7376fbeb1d4a 135 * @retval MAGNETO_OK if ok,
nikapov 0:7376fbeb1d4a 136 * @retval MAGNETO_ERROR if an I2C error has occured
nikapov 0:7376fbeb1d4a 137 */
nikapov 0:7376fbeb1d4a 138 MAGNETO_StatusTypeDef LIS3MDL_IO_Write(uint8_t* pBuffer,
nikapov 0:7376fbeb1d4a 139 uint8_t RegisterAddr, uint16_t NumByteToWrite)
nikapov 0:7376fbeb1d4a 140 {
mapellil 2:b3212ad3c306 141 int ret = dev_i2c->i2c_write(pBuffer,
nikapov 0:7376fbeb1d4a 142 LIS3MDL_M_MEMS_ADDRESS,
nikapov 0:7376fbeb1d4a 143 RegisterAddr,
nikapov 0:7376fbeb1d4a 144 NumByteToWrite);
nikapov 0:7376fbeb1d4a 145 if(ret != 0) {
nikapov 0:7376fbeb1d4a 146 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 147 }
nikapov 0:7376fbeb1d4a 148 return MAGNETO_OK;
nikapov 0:7376fbeb1d4a 149 }
nikapov 0:7376fbeb1d4a 150
nikapov 0:7376fbeb1d4a 151 /*** Instance Variables ***/
nikapov 0:7376fbeb1d4a 152 /* IO Device */
mapellil 2:b3212ad3c306 153 DevI2C *dev_i2c;
nikapov 0:7376fbeb1d4a 154 };
nikapov 0:7376fbeb1d4a 155
nikapov 0:7376fbeb1d4a 156 #endif // __LIS3MDL_CLASS_H