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:
nikapov
Date:
Tue Sep 05 10:15:14 2017 +0000
Revision:
0:7376fbeb1d4a
Child:
1:d85092ab306e
First version.

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
nikapov 0:7376fbeb1d4a 48 /**
nikapov 0:7376fbeb1d4a 49 * @brief Set LIS3MDL Initialization
nikapov 0:7376fbeb1d4a 50 * @param LIS3MDL_Init the configuration setting for the LIS3MDL
nikapov 0:7376fbeb1d4a 51 * @retval MAGNETO_OK in case of success, an error code otherwise
nikapov 0:7376fbeb1d4a 52 */
nikapov 0:7376fbeb1d4a 53 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init)
nikapov 0:7376fbeb1d4a 54 {
nikapov 0:7376fbeb1d4a 55 uint8_t tmp1 = 0x00;
nikapov 0:7376fbeb1d4a 56
nikapov 0:7376fbeb1d4a 57 /* Configure the low level interface ---------------------------------------*/
nikapov 0:7376fbeb1d4a 58 if(LIS3MDL_IO_Init() != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 59 {
nikapov 0:7376fbeb1d4a 60 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 61 }
nikapov 0:7376fbeb1d4a 62
nikapov 0:7376fbeb1d4a 63 /****** Magnetic sensor *******/
nikapov 0:7376fbeb1d4a 64
nikapov 0:7376fbeb1d4a 65 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 66 {
nikapov 0:7376fbeb1d4a 67 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 68 }
nikapov 0:7376fbeb1d4a 69
nikapov 0:7376fbeb1d4a 70 /* Conversion mode selection */
nikapov 0:7376fbeb1d4a 71 tmp1 &= ~(LIS3MDL_M_MD_MASK);
nikapov 0:7376fbeb1d4a 72 tmp1 |= LIS3MDL_Init->M_OperatingMode;
nikapov 0:7376fbeb1d4a 73
nikapov 0:7376fbeb1d4a 74 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 75 {
nikapov 0:7376fbeb1d4a 76 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 77 }
nikapov 0:7376fbeb1d4a 78
nikapov 0:7376fbeb1d4a 79 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 80 {
nikapov 0:7376fbeb1d4a 81 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 82 }
nikapov 0:7376fbeb1d4a 83
nikapov 0:7376fbeb1d4a 84 /* Output data rate selection */
nikapov 0:7376fbeb1d4a 85 tmp1 &= ~(LIS3MDL_M_DO_MASK);
nikapov 0:7376fbeb1d4a 86 tmp1 |= LIS3MDL_Init->M_OutputDataRate;
nikapov 0:7376fbeb1d4a 87
nikapov 0:7376fbeb1d4a 88 /* X and Y axes Operative mode selection */
nikapov 0:7376fbeb1d4a 89 tmp1 &= ~(LIS3MDL_M_OM_MASK);
nikapov 0:7376fbeb1d4a 90 tmp1 |= LIS3MDL_Init->M_XYOperativeMode;
nikapov 0:7376fbeb1d4a 91
nikapov 0:7376fbeb1d4a 92 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 93 {
nikapov 0:7376fbeb1d4a 94 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 95 }
nikapov 0:7376fbeb1d4a 96
nikapov 0:7376fbeb1d4a 97 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 98 {
nikapov 0:7376fbeb1d4a 99 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 100 }
nikapov 0:7376fbeb1d4a 101
nikapov 0:7376fbeb1d4a 102 /* Full scale selection */
nikapov 0:7376fbeb1d4a 103 tmp1 &= ~(LIS3MDL_M_FS_MASK);
nikapov 0:7376fbeb1d4a 104 tmp1 |= LIS3MDL_Init->M_FullScale;
nikapov 0:7376fbeb1d4a 105
nikapov 0:7376fbeb1d4a 106 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 107 {
nikapov 0:7376fbeb1d4a 108 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 109 }
nikapov 0:7376fbeb1d4a 110
nikapov 0:7376fbeb1d4a 111 /* Configure interrupt lines */
nikapov 0:7376fbeb1d4a 112 LIS3MDL_IO_ITConfig();
nikapov 0:7376fbeb1d4a 113
nikapov 0:7376fbeb1d4a 114 return MAGNETO_OK;
nikapov 0:7376fbeb1d4a 115
nikapov 0:7376fbeb1d4a 116 /******************************/
nikapov 0:7376fbeb1d4a 117 }
nikapov 0:7376fbeb1d4a 118
nikapov 0:7376fbeb1d4a 119
nikapov 0:7376fbeb1d4a 120 /**
nikapov 0:7376fbeb1d4a 121 * @brief Read ID of LIS3MDL Magnetic sensor
nikapov 0:7376fbeb1d4a 122 * @param m_id the pointer where the ID of the device is stored
nikapov 0:7376fbeb1d4a 123 * @retval MAGNETO_OK in case of success, an error code otherwise
nikapov 0:7376fbeb1d4a 124 */
nikapov 0:7376fbeb1d4a 125 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Read_M_ID(uint8_t *m_id)
nikapov 0:7376fbeb1d4a 126 {
nikapov 0:7376fbeb1d4a 127 if(!m_id)
nikapov 0:7376fbeb1d4a 128 {
nikapov 0:7376fbeb1d4a 129 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 130 }
nikapov 0:7376fbeb1d4a 131
nikapov 0:7376fbeb1d4a 132 return LIS3MDL_IO_Read(m_id, LIS3MDL_M_WHO_AM_I_ADDR, 1);
nikapov 0:7376fbeb1d4a 133 }
nikapov 0:7376fbeb1d4a 134
nikapov 0:7376fbeb1d4a 135
nikapov 0:7376fbeb1d4a 136 /**
nikapov 0:7376fbeb1d4a 137 * @brief Read raw data from LIS3MDL Magnetic sensor output register
nikapov 0:7376fbeb1d4a 138 * @param pData the pointer where the magnetometer raw data are stored
nikapov 0:7376fbeb1d4a 139 * @retval MAGNETO_OK in case of success, an error code otherwise
nikapov 0:7376fbeb1d4a 140 */
nikapov 0:7376fbeb1d4a 141 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxesRaw(int16_t *pData)
nikapov 0:7376fbeb1d4a 142 {
nikapov 0:7376fbeb1d4a 143 uint8_t tempReg[2] = {0, 0};
nikapov 0:7376fbeb1d4a 144
nikapov 0:7376fbeb1d4a 145 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_X_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD),
nikapov 0:7376fbeb1d4a 146 2) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 147 {
nikapov 0:7376fbeb1d4a 148 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 149 }
nikapov 0:7376fbeb1d4a 150
nikapov 0:7376fbeb1d4a 151 pData[0] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
nikapov 0:7376fbeb1d4a 152
nikapov 0:7376fbeb1d4a 153 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Y_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD),
nikapov 0:7376fbeb1d4a 154 2) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 155 {
nikapov 0:7376fbeb1d4a 156 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 157 }
nikapov 0:7376fbeb1d4a 158
nikapov 0:7376fbeb1d4a 159 pData[1] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
nikapov 0:7376fbeb1d4a 160
nikapov 0:7376fbeb1d4a 161 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Z_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD),
nikapov 0:7376fbeb1d4a 162 2) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 163 {
nikapov 0:7376fbeb1d4a 164 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 165 }
nikapov 0:7376fbeb1d4a 166
nikapov 0:7376fbeb1d4a 167 pData[2] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
nikapov 0:7376fbeb1d4a 168
nikapov 0:7376fbeb1d4a 169 return MAGNETO_OK;
nikapov 0:7376fbeb1d4a 170 }
nikapov 0:7376fbeb1d4a 171
nikapov 0:7376fbeb1d4a 172
nikapov 0:7376fbeb1d4a 173 /**
nikapov 0:7376fbeb1d4a 174 * @brief Read data from LIS3MDL Magnetic sensor and calculate Magnetic in mgauss
nikapov 0:7376fbeb1d4a 175 * @param pData the pointer where the magnetometer data are stored
nikapov 0:7376fbeb1d4a 176 * @retval MAGNETO_OK in case of success, an error code otherwise
nikapov 0:7376fbeb1d4a 177 */
nikapov 0:7376fbeb1d4a 178 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxes(int32_t *pData)
nikapov 0:7376fbeb1d4a 179 {
nikapov 0:7376fbeb1d4a 180 uint8_t tempReg = 0x00;
nikapov 0:7376fbeb1d4a 181 int16_t pDataRaw[3];
nikapov 0:7376fbeb1d4a 182 float sensitivity = 0;
nikapov 0:7376fbeb1d4a 183
nikapov 0:7376fbeb1d4a 184 if(LIS3MDL_M_GetAxesRaw(pDataRaw) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 185 {
nikapov 0:7376fbeb1d4a 186 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 187 }
nikapov 0:7376fbeb1d4a 188
nikapov 0:7376fbeb1d4a 189 if(LIS3MDL_IO_Read(&tempReg, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
nikapov 0:7376fbeb1d4a 190 {
nikapov 0:7376fbeb1d4a 191 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 192 }
nikapov 0:7376fbeb1d4a 193
nikapov 0:7376fbeb1d4a 194 tempReg &= LIS3MDL_M_FS_MASK;
nikapov 0:7376fbeb1d4a 195
nikapov 0:7376fbeb1d4a 196 switch(tempReg)
nikapov 0:7376fbeb1d4a 197 {
nikapov 0:7376fbeb1d4a 198 case LIS3MDL_M_FS_4:
nikapov 0:7376fbeb1d4a 199 sensitivity = 0.14;
nikapov 0:7376fbeb1d4a 200 break;
nikapov 0:7376fbeb1d4a 201 case LIS3MDL_M_FS_8:
nikapov 0:7376fbeb1d4a 202 sensitivity = 0.29;
nikapov 0:7376fbeb1d4a 203 break;
nikapov 0:7376fbeb1d4a 204 case LIS3MDL_M_FS_12:
nikapov 0:7376fbeb1d4a 205 sensitivity = 0.43;
nikapov 0:7376fbeb1d4a 206 break;
nikapov 0:7376fbeb1d4a 207 case LIS3MDL_M_FS_16:
nikapov 0:7376fbeb1d4a 208 sensitivity = 0.58;
nikapov 0:7376fbeb1d4a 209 break;
nikapov 0:7376fbeb1d4a 210 }
nikapov 0:7376fbeb1d4a 211
nikapov 0:7376fbeb1d4a 212 pData[0] = (int32_t)(pDataRaw[0] * sensitivity);
nikapov 0:7376fbeb1d4a 213 pData[1] = (int32_t)(pDataRaw[1] * sensitivity);
nikapov 0:7376fbeb1d4a 214 pData[2] = (int32_t)(pDataRaw[2] * sensitivity);
nikapov 0:7376fbeb1d4a 215
nikapov 0:7376fbeb1d4a 216 return MAGNETO_OK;
nikapov 0:7376fbeb1d4a 217 }
nikapov 0:7376fbeb1d4a 218
nikapov 0:7376fbeb1d4a 219 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/