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:
Wed Oct 18 11:14:26 2017 +0200
Revision:
6:308889c4d074
Parent:
4:00493226e59f
Fixed sensor initialization

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.cpp
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 Implementation file for the LIS3MDL driver class
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 /* Includes ------------------------------------------------------------------*/
nikapov 0:7376fbeb1d4a 39 #include "lis3mdl_class.h"
nikapov 0:7376fbeb1d4a 40 #include "lis3mdl.h"
nikapov 0:7376fbeb1d4a 41
nikapov 0:7376fbeb1d4a 42 /* Methods -------------------------------------------------------------------*/
nikapov 0:7376fbeb1d4a 43 /* betzw - based on:
nikapov 0:7376fbeb1d4a 44 X-CUBE-MEMS1/trunk/Drivers/BSP/Components/lis3mdl/lis3mdl.c: revision #400,
nikapov 0:7376fbeb1d4a 45 X-CUBE-MEMS1/trunk: revision #416
nikapov 0:7376fbeb1d4a 46 */
nikapov 0:7376fbeb1d4a 47
mapellil 4:00493226e59f 48 LIS3MDL::LIS3MDL(SPI *spi, PinName cs_pin, PinName int_pin, SPI_type_t spi_type) :
mapellil 4:00493226e59f 49 _dev_spi(spi), _cs_pin(cs_pin), _int_pin(int_pin), _spi_type(spi_type)
mapellil 4:00493226e59f 50 {
mapellil 4:00493226e59f 51 assert (spi);
mapellil 4:00493226e59f 52 if (cs_pin == NC)
mapellil 4:00493226e59f 53 {
mapellil 4:00493226e59f 54 printf ("ERROR LIS3MDL CS MUST NOT BE NC\n\r");
mapellil 4:00493226e59f 55 _dev_spi = NULL;
mapellil 4:00493226e59f 56 _dev_i2c=NULL;
mapellil 4:00493226e59f 57 return;
mapellil 4:00493226e59f 58 }
mapellil 4:00493226e59f 59
mapellil 4:00493226e59f 60 _cs_pin = 0;
mapellil 4:00493226e59f 61 _dev_i2c=NULL;
mapellil 4:00493226e59f 62
mapellil 4:00493226e59f 63 if (_spi_type == SPI3W) LIS3MDL_Set_SpiInterface ((void *)this, LIS3MDL_SPI_3_WIRE);
mapellil 4:00493226e59f 64 else if (_spi_type == SPI4W) LIS3MDL_Set_SpiInterface ((void *)this, LIS3MDL_SPI_4_WIRE);
mapellil 4:00493226e59f 65 }
mapellil 4:00493226e59f 66
mapellil 4:00493226e59f 67 LIS3MDL::LIS3MDL(DevI2C *i2c, uint8_t address, PinName int_pin) :
mapellil 4:00493226e59f 68 _dev_i2c(i2c), _address(address), _cs_pin(NC), _int_pin(int_pin)
mapellil 4:00493226e59f 69 {
mapellil 4:00493226e59f 70 assert (i2c);
mapellil 4:00493226e59f 71 _dev_spi = NULL;
mapellil 4:00493226e59f 72 }
mapellil 4:00493226e59f 73
mapellil 4:00493226e59f 74
mapellil 4:00493226e59f 75 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Set_SpiInterface (void *handle, LIS3MDL_SPIMode_t spimode)
mapellil 4:00493226e59f 76 {
mapellil 4:00493226e59f 77 uint8_t tmp=0x03; //deft LIS3MDL_CTRL_REG3 value
mapellil 4:00493226e59f 78
mapellil 4:00493226e59f 79 tmp |= (uint8_t)spimode;
mapellil 4:00493226e59f 80 if (LIS3MDL_IO_Write(&tmp, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK) return MAGNETO_ERROR;
mapellil 4:00493226e59f 81 return MAGNETO_OK;
mapellil 4:00493226e59f 82 }
mapellil 4:00493226e59f 83
nikapov 0:7376fbeb1d4a 84 /**
nikapov 0:7376fbeb1d4a 85 * @brief Set LIS3MDL Initialization
nikapov 0:7376fbeb1d4a 86 * @param LIS3MDL_Init the configuration setting for the LIS3MDL
nikapov 0:7376fbeb1d4a 87 * @retval MAGNETO_OK in case of success, an error code otherwise
nikapov 0:7376fbeb1d4a 88 */
nikapov 0:7376fbeb1d4a 89 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init)
nikapov 0:7376fbeb1d4a 90 {
nikapov 0:7376fbeb1d4a 91 uint8_t tmp1 = 0x00;
nikapov 1:d85092ab306e 92 MAGNETO_InitTypeDef *initStructure = LIS3MDL_Init;
mapellil 6:308889c4d074 93 MAGNETO_InitTypeDef tempInit;
nikapov 1:d85092ab306e 94
nikapov 1:d85092ab306e 95 if (initStructure == NULL) {// default params
nikapov 1:d85092ab306e 96 tempInit.M_FullScale = LIS3MDL_M_FS_4;
nikapov 1:d85092ab306e 97 tempInit.M_OperatingMode = LIS3MDL_M_MD_CONTINUOUS;
nikapov 1:d85092ab306e 98 tempInit.M_XYOperativeMode = LIS3MDL_M_OM_HP;
nikapov 1:d85092ab306e 99 tempInit.M_OutputDataRate = LIS3MDL_M_DO_80;
nikapov 1:d85092ab306e 100 initStructure = &tempInit;
nikapov 1:d85092ab306e 101 }
nikapov 1:d85092ab306e 102
nikapov 0:7376fbeb1d4a 103
nikapov 0:7376fbeb1d4a 104 /* Configure the low level interface ---------------------------------------*/
nikapov 0:7376fbeb1d4a 105 if(LIS3MDL_IO_Init() != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 106 {
nikapov 0:7376fbeb1d4a 107 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 108 }
nikapov 0:7376fbeb1d4a 109
nikapov 0:7376fbeb1d4a 110 /****** Magnetic sensor *******/
nikapov 0:7376fbeb1d4a 111
nikapov 0:7376fbeb1d4a 112 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 113 {
nikapov 0:7376fbeb1d4a 114 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 115 }
nikapov 0:7376fbeb1d4a 116
nikapov 0:7376fbeb1d4a 117 /* Conversion mode selection */
nikapov 0:7376fbeb1d4a 118 tmp1 &= ~(LIS3MDL_M_MD_MASK);
nikapov 1:d85092ab306e 119 tmp1 |= initStructure->M_OperatingMode;
nikapov 0:7376fbeb1d4a 120
nikapov 0:7376fbeb1d4a 121 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 122 {
nikapov 0:7376fbeb1d4a 123 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 124 }
nikapov 0:7376fbeb1d4a 125
nikapov 0:7376fbeb1d4a 126 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 127 {
nikapov 0:7376fbeb1d4a 128 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 129 }
nikapov 0:7376fbeb1d4a 130
nikapov 0:7376fbeb1d4a 131 /* Output data rate selection */
nikapov 0:7376fbeb1d4a 132 tmp1 &= ~(LIS3MDL_M_DO_MASK);
nikapov 1:d85092ab306e 133 tmp1 |= initStructure->M_OutputDataRate;
nikapov 0:7376fbeb1d4a 134
nikapov 0:7376fbeb1d4a 135 /* X and Y axes Operative mode selection */
nikapov 0:7376fbeb1d4a 136 tmp1 &= ~(LIS3MDL_M_OM_MASK);
nikapov 1:d85092ab306e 137 tmp1 |= initStructure->M_XYOperativeMode;
nikapov 0:7376fbeb1d4a 138
nikapov 0:7376fbeb1d4a 139 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 140 {
nikapov 0:7376fbeb1d4a 141 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 142 }
nikapov 0:7376fbeb1d4a 143
nikapov 0:7376fbeb1d4a 144 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 145 {
nikapov 0:7376fbeb1d4a 146 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 147 }
nikapov 0:7376fbeb1d4a 148
nikapov 0:7376fbeb1d4a 149 /* Full scale selection */
nikapov 0:7376fbeb1d4a 150 tmp1 &= ~(LIS3MDL_M_FS_MASK);
nikapov 1:d85092ab306e 151 tmp1 |= initStructure->M_FullScale;
nikapov 0:7376fbeb1d4a 152
nikapov 0:7376fbeb1d4a 153 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 154 {
nikapov 0:7376fbeb1d4a 155 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 156 }
nikapov 0:7376fbeb1d4a 157
nikapov 0:7376fbeb1d4a 158 /* Configure interrupt lines */
nikapov 0:7376fbeb1d4a 159 LIS3MDL_IO_ITConfig();
nikapov 0:7376fbeb1d4a 160
nikapov 0:7376fbeb1d4a 161 return MAGNETO_OK;
nikapov 0:7376fbeb1d4a 162
nikapov 0:7376fbeb1d4a 163 /******************************/
nikapov 0:7376fbeb1d4a 164 }
nikapov 0:7376fbeb1d4a 165
nikapov 0:7376fbeb1d4a 166
nikapov 0:7376fbeb1d4a 167 /**
nikapov 0:7376fbeb1d4a 168 * @brief Read ID of LIS3MDL Magnetic sensor
nikapov 0:7376fbeb1d4a 169 * @param m_id the pointer where the ID of the device is stored
nikapov 0:7376fbeb1d4a 170 * @retval MAGNETO_OK in case of success, an error code otherwise
nikapov 0:7376fbeb1d4a 171 */
nikapov 0:7376fbeb1d4a 172 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Read_M_ID(uint8_t *m_id)
nikapov 0:7376fbeb1d4a 173 {
nikapov 0:7376fbeb1d4a 174 if(!m_id)
nikapov 0:7376fbeb1d4a 175 {
nikapov 0:7376fbeb1d4a 176 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 177 }
nikapov 0:7376fbeb1d4a 178
nikapov 0:7376fbeb1d4a 179 return LIS3MDL_IO_Read(m_id, LIS3MDL_M_WHO_AM_I_ADDR, 1);
nikapov 0:7376fbeb1d4a 180 }
nikapov 0:7376fbeb1d4a 181
nikapov 0:7376fbeb1d4a 182
nikapov 0:7376fbeb1d4a 183 /**
nikapov 0:7376fbeb1d4a 184 * @brief Read raw data from LIS3MDL Magnetic sensor output register
nikapov 0:7376fbeb1d4a 185 * @param pData the pointer where the magnetometer raw data are stored
nikapov 0:7376fbeb1d4a 186 * @retval MAGNETO_OK in case of success, an error code otherwise
nikapov 0:7376fbeb1d4a 187 */
nikapov 0:7376fbeb1d4a 188 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxesRaw(int16_t *pData)
nikapov 0:7376fbeb1d4a 189 {
nikapov 0:7376fbeb1d4a 190 uint8_t tempReg[2] = {0, 0};
nikapov 0:7376fbeb1d4a 191
nikapov 0:7376fbeb1d4a 192 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_X_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD),
nikapov 0:7376fbeb1d4a 193 2) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 194 {
nikapov 0:7376fbeb1d4a 195 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 196 }
nikapov 0:7376fbeb1d4a 197
nikapov 0:7376fbeb1d4a 198 pData[0] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
nikapov 0:7376fbeb1d4a 199
nikapov 0:7376fbeb1d4a 200 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Y_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD),
nikapov 0:7376fbeb1d4a 201 2) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 202 {
nikapov 0:7376fbeb1d4a 203 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 204 }
nikapov 0:7376fbeb1d4a 205
nikapov 0:7376fbeb1d4a 206 pData[1] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
nikapov 0:7376fbeb1d4a 207
nikapov 0:7376fbeb1d4a 208 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Z_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD),
nikapov 0:7376fbeb1d4a 209 2) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 210 {
nikapov 0:7376fbeb1d4a 211 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 212 }
nikapov 0:7376fbeb1d4a 213
nikapov 0:7376fbeb1d4a 214 pData[2] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
nikapov 0:7376fbeb1d4a 215
nikapov 0:7376fbeb1d4a 216 return MAGNETO_OK;
nikapov 0:7376fbeb1d4a 217 }
nikapov 0:7376fbeb1d4a 218
nikapov 0:7376fbeb1d4a 219
nikapov 0:7376fbeb1d4a 220 /**
nikapov 0:7376fbeb1d4a 221 * @brief Read data from LIS3MDL Magnetic sensor and calculate Magnetic in mgauss
nikapov 0:7376fbeb1d4a 222 * @param pData the pointer where the magnetometer data are stored
nikapov 0:7376fbeb1d4a 223 * @retval MAGNETO_OK in case of success, an error code otherwise
nikapov 0:7376fbeb1d4a 224 */
nikapov 0:7376fbeb1d4a 225 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxes(int32_t *pData)
nikapov 0:7376fbeb1d4a 226 {
nikapov 0:7376fbeb1d4a 227 uint8_t tempReg = 0x00;
nikapov 0:7376fbeb1d4a 228 int16_t pDataRaw[3];
nikapov 0:7376fbeb1d4a 229 float sensitivity = 0;
nikapov 0:7376fbeb1d4a 230
nikapov 0:7376fbeb1d4a 231 if(LIS3MDL_M_GetAxesRaw(pDataRaw) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 232 {
nikapov 0:7376fbeb1d4a 233 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 234 }
nikapov 0:7376fbeb1d4a 235
nikapov 0:7376fbeb1d4a 236 if(LIS3MDL_IO_Read(&tempReg, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 237 {
nikapov 0:7376fbeb1d4a 238 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 239 }
nikapov 0:7376fbeb1d4a 240
nikapov 0:7376fbeb1d4a 241 tempReg &= LIS3MDL_M_FS_MASK;
nikapov 0:7376fbeb1d4a 242
nikapov 0:7376fbeb1d4a 243 switch(tempReg)
nikapov 0:7376fbeb1d4a 244 {
nikapov 0:7376fbeb1d4a 245 case LIS3MDL_M_FS_4:
nikapov 0:7376fbeb1d4a 246 sensitivity = 0.14;
nikapov 0:7376fbeb1d4a 247 break;
nikapov 0:7376fbeb1d4a 248 case LIS3MDL_M_FS_8:
nikapov 0:7376fbeb1d4a 249 sensitivity = 0.29;
nikapov 0:7376fbeb1d4a 250 break;
nikapov 0:7376fbeb1d4a 251 case LIS3MDL_M_FS_12:
nikapov 0:7376fbeb1d4a 252 sensitivity = 0.43;
nikapov 0:7376fbeb1d4a 253 break;
nikapov 0:7376fbeb1d4a 254 case LIS3MDL_M_FS_16:
nikapov 0:7376fbeb1d4a 255 sensitivity = 0.58;
nikapov 0:7376fbeb1d4a 256 break;
nikapov 0:7376fbeb1d4a 257 }
nikapov 0:7376fbeb1d4a 258
nikapov 0:7376fbeb1d4a 259 pData[0] = (int32_t)(pDataRaw[0] * sensitivity);
nikapov 0:7376fbeb1d4a 260 pData[1] = (int32_t)(pDataRaw[1] * sensitivity);
nikapov 0:7376fbeb1d4a 261 pData[2] = (int32_t)(pDataRaw[2] * sensitivity);
nikapov 0:7376fbeb1d4a 262
nikapov 0:7376fbeb1d4a 263 return MAGNETO_OK;
nikapov 0:7376fbeb1d4a 264 }
nikapov 0:7376fbeb1d4a 265
nikapov 0:7376fbeb1d4a 266 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/