Finished project.

Dependencies:   X_NUCLEO_COMMON

Fork of ReferredCoursework2016 by Stage-1 Students SoCEM

Committer:
Wolfgang Betz
Date:
Wed Apr 15 12:10:42 2015 +0200
Revision:
5:8bab0f419849
Child:
24:92cc9c6e4b2b
Added component LIS3MDL

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 5:8bab0f419849 1 /**
Wolfgang Betz 5:8bab0f419849 2 ******************************************************************************
Wolfgang Betz 5:8bab0f419849 3 * @file lis3mdl_class.cpp
Wolfgang Betz 5:8bab0f419849 4 * @author AST / EST
Wolfgang Betz 5:8bab0f419849 5 * @version V0.0.1
Wolfgang Betz 5:8bab0f419849 6 * @date 14-April-2015
Wolfgang Betz 5:8bab0f419849 7 * @brief Implementation file for the LIS3MDL driver class
Wolfgang Betz 5:8bab0f419849 8 ******************************************************************************
Wolfgang Betz 5:8bab0f419849 9 * @attention
Wolfgang Betz 5:8bab0f419849 10 *
Wolfgang Betz 5:8bab0f419849 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Wolfgang Betz 5:8bab0f419849 12 *
Wolfgang Betz 5:8bab0f419849 13 * Redistribution and use in source and binary forms, with or without modification,
Wolfgang Betz 5:8bab0f419849 14 * are permitted provided that the following conditions are met:
Wolfgang Betz 5:8bab0f419849 15 * 1. Redistributions of source code must retain the above copyright notice,
Wolfgang Betz 5:8bab0f419849 16 * this list of conditions and the following disclaimer.
Wolfgang Betz 5:8bab0f419849 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
Wolfgang Betz 5:8bab0f419849 18 * this list of conditions and the following disclaimer in the documentation
Wolfgang Betz 5:8bab0f419849 19 * and/or other materials provided with the distribution.
Wolfgang Betz 5:8bab0f419849 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Wolfgang Betz 5:8bab0f419849 21 * may be used to endorse or promote products derived from this software
Wolfgang Betz 5:8bab0f419849 22 * without specific prior written permission.
Wolfgang Betz 5:8bab0f419849 23 *
Wolfgang Betz 5:8bab0f419849 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Wolfgang Betz 5:8bab0f419849 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Wolfgang Betz 5:8bab0f419849 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Wolfgang Betz 5:8bab0f419849 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Wolfgang Betz 5:8bab0f419849 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Wolfgang Betz 5:8bab0f419849 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Wolfgang Betz 5:8bab0f419849 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Wolfgang Betz 5:8bab0f419849 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Wolfgang Betz 5:8bab0f419849 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Wolfgang Betz 5:8bab0f419849 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wolfgang Betz 5:8bab0f419849 34 *
Wolfgang Betz 5:8bab0f419849 35 ******************************************************************************
Wolfgang Betz 5:8bab0f419849 36 */
Wolfgang Betz 5:8bab0f419849 37
Wolfgang Betz 5:8bab0f419849 38 /* Includes ------------------------------------------------------------------*/
Wolfgang Betz 5:8bab0f419849 39 #include "mbed.h"
Wolfgang Betz 5:8bab0f419849 40 #include "lis3mdl_class.h"
Wolfgang Betz 5:8bab0f419849 41 #include "lis3mdl.h"
Wolfgang Betz 5:8bab0f419849 42 #include "../../x_nucleo_iks01a1_targets.h"
Wolfgang Betz 5:8bab0f419849 43
Wolfgang Betz 5:8bab0f419849 44 /* Methods -------------------------------------------------------------------*/
Wolfgang Betz 5:8bab0f419849 45 /* betzw - based on:
Wolfgang Betz 5:8bab0f419849 46 X-CUBE-MEMS1/trunk/Drivers/BSP/Components/lis3mdl/lis3mdl.c: revision #184,
Wolfgang Betz 5:8bab0f419849 47 X-CUBE-MEMS1/trunk: revision #293
Wolfgang Betz 5:8bab0f419849 48 */
Wolfgang Betz 5:8bab0f419849 49 /**
Wolfgang Betz 5:8bab0f419849 50 * @brief Set LIS3MDL Initialization
Wolfgang Betz 5:8bab0f419849 51 * @param LIS3MDL_Init the configuration setting for the LIS3MDL
Wolfgang Betz 5:8bab0f419849 52 * @retval MAGNETO_OK in case of success, an error code otherwise
Wolfgang Betz 5:8bab0f419849 53 */
Wolfgang Betz 5:8bab0f419849 54 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init)
Wolfgang Betz 5:8bab0f419849 55 {
Wolfgang Betz 5:8bab0f419849 56 uint8_t tmp1 = 0x00;
Wolfgang Betz 5:8bab0f419849 57
Wolfgang Betz 5:8bab0f419849 58 /* Configure the low level interface ---------------------------------------*/
Wolfgang Betz 5:8bab0f419849 59 if(LIS3MDL_IO_Init() != MAGNETO_OK)
Wolfgang Betz 5:8bab0f419849 60 {
Wolfgang Betz 5:8bab0f419849 61 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 62 }
Wolfgang Betz 5:8bab0f419849 63
Wolfgang Betz 5:8bab0f419849 64 /****** Magnetic sensor *******/
Wolfgang Betz 5:8bab0f419849 65
Wolfgang Betz 5:8bab0f419849 66 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK)
Wolfgang Betz 5:8bab0f419849 67 {
Wolfgang Betz 5:8bab0f419849 68 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 69 }
Wolfgang Betz 5:8bab0f419849 70
Wolfgang Betz 5:8bab0f419849 71 /* Conversion mode selection */
Wolfgang Betz 5:8bab0f419849 72 tmp1 &= ~(LIS3MDL_M_MD_MASK);
Wolfgang Betz 5:8bab0f419849 73 tmp1 |= LIS3MDL_Init->M_OperatingMode;
Wolfgang Betz 5:8bab0f419849 74
Wolfgang Betz 5:8bab0f419849 75 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK)
Wolfgang Betz 5:8bab0f419849 76 {
Wolfgang Betz 5:8bab0f419849 77 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 78 }
Wolfgang Betz 5:8bab0f419849 79
Wolfgang Betz 5:8bab0f419849 80 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK)
Wolfgang Betz 5:8bab0f419849 81 {
Wolfgang Betz 5:8bab0f419849 82 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 83 }
Wolfgang Betz 5:8bab0f419849 84
Wolfgang Betz 5:8bab0f419849 85 /* Output data rate selection */
Wolfgang Betz 5:8bab0f419849 86 tmp1 &= ~(LIS3MDL_M_DO_MASK);
Wolfgang Betz 5:8bab0f419849 87 tmp1 |= LIS3MDL_Init->M_OutputDataRate;
Wolfgang Betz 5:8bab0f419849 88
Wolfgang Betz 5:8bab0f419849 89 /* X and Y axes Operative mode selection */
Wolfgang Betz 5:8bab0f419849 90 tmp1 &= ~(LIS3MDL_M_OM_MASK);
Wolfgang Betz 5:8bab0f419849 91 tmp1 |= LIS3MDL_Init->M_XYOperativeMode;
Wolfgang Betz 5:8bab0f419849 92
Wolfgang Betz 5:8bab0f419849 93 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK)
Wolfgang Betz 5:8bab0f419849 94 {
Wolfgang Betz 5:8bab0f419849 95 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 96 }
Wolfgang Betz 5:8bab0f419849 97
Wolfgang Betz 5:8bab0f419849 98 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
Wolfgang Betz 5:8bab0f419849 99 {
Wolfgang Betz 5:8bab0f419849 100 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 101 }
Wolfgang Betz 5:8bab0f419849 102
Wolfgang Betz 5:8bab0f419849 103 /* Full scale selection */
Wolfgang Betz 5:8bab0f419849 104 tmp1 &= ~(LIS3MDL_M_FS_MASK);
Wolfgang Betz 5:8bab0f419849 105 tmp1 |= LIS3MDL_Init->M_FullScale;
Wolfgang Betz 5:8bab0f419849 106
Wolfgang Betz 5:8bab0f419849 107 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
Wolfgang Betz 5:8bab0f419849 108 {
Wolfgang Betz 5:8bab0f419849 109 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 110 }
Wolfgang Betz 5:8bab0f419849 111
Wolfgang Betz 5:8bab0f419849 112 /* Configure interrupt lines */
Wolfgang Betz 5:8bab0f419849 113 LIS3MDL_IO_ITConfig();
Wolfgang Betz 5:8bab0f419849 114
Wolfgang Betz 5:8bab0f419849 115 return MAGNETO_OK;
Wolfgang Betz 5:8bab0f419849 116
Wolfgang Betz 5:8bab0f419849 117 /******************************/
Wolfgang Betz 5:8bab0f419849 118 }
Wolfgang Betz 5:8bab0f419849 119
Wolfgang Betz 5:8bab0f419849 120
Wolfgang Betz 5:8bab0f419849 121 /**
Wolfgang Betz 5:8bab0f419849 122 * @brief Read ID of LIS3MDL Magnetic sensor
Wolfgang Betz 5:8bab0f419849 123 * @param m_id the pointer where the ID of the device is stored
Wolfgang Betz 5:8bab0f419849 124 * @retval MAGNETO_OK in case of success, an error code otherwise
Wolfgang Betz 5:8bab0f419849 125 */
Wolfgang Betz 5:8bab0f419849 126 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Read_M_ID(uint8_t *m_id)
Wolfgang Betz 5:8bab0f419849 127 {
Wolfgang Betz 5:8bab0f419849 128 if(!m_id)
Wolfgang Betz 5:8bab0f419849 129 {
Wolfgang Betz 5:8bab0f419849 130 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 131 }
Wolfgang Betz 5:8bab0f419849 132
Wolfgang Betz 5:8bab0f419849 133 return LIS3MDL_IO_Read(m_id, LIS3MDL_M_WHO_AM_I_ADDR, 1);
Wolfgang Betz 5:8bab0f419849 134 }
Wolfgang Betz 5:8bab0f419849 135
Wolfgang Betz 5:8bab0f419849 136
Wolfgang Betz 5:8bab0f419849 137 /**
Wolfgang Betz 5:8bab0f419849 138 * @brief Read raw data from LIS3MDL Magnetic sensor output register
Wolfgang Betz 5:8bab0f419849 139 * @param pData the pointer where the magnetometer raw data are stored
Wolfgang Betz 5:8bab0f419849 140 * @retval MAGNETO_OK in case of success, an error code otherwise
Wolfgang Betz 5:8bab0f419849 141 */
Wolfgang Betz 5:8bab0f419849 142 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxesRaw(int16_t *pData)
Wolfgang Betz 5:8bab0f419849 143 {
Wolfgang Betz 5:8bab0f419849 144 uint8_t tempReg[2] = {0,0};
Wolfgang Betz 5:8bab0f419849 145
Wolfgang Betz 5:8bab0f419849 146 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_X_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD), 2) != MAGNETO_OK)
Wolfgang Betz 5:8bab0f419849 147 {
Wolfgang Betz 5:8bab0f419849 148 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 149 }
Wolfgang Betz 5:8bab0f419849 150
Wolfgang Betz 5:8bab0f419849 151 pData[0] = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
Wolfgang Betz 5:8bab0f419849 152
Wolfgang Betz 5:8bab0f419849 153 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Y_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD), 2) != MAGNETO_OK)
Wolfgang Betz 5:8bab0f419849 154 {
Wolfgang Betz 5:8bab0f419849 155 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 156 }
Wolfgang Betz 5:8bab0f419849 157
Wolfgang Betz 5:8bab0f419849 158 pData[1] = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
Wolfgang Betz 5:8bab0f419849 159
Wolfgang Betz 5:8bab0f419849 160 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Z_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD), 2) != MAGNETO_OK)
Wolfgang Betz 5:8bab0f419849 161 {
Wolfgang Betz 5:8bab0f419849 162 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 163 }
Wolfgang Betz 5:8bab0f419849 164
Wolfgang Betz 5:8bab0f419849 165 pData[2] = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
Wolfgang Betz 5:8bab0f419849 166
Wolfgang Betz 5:8bab0f419849 167 return MAGNETO_OK;
Wolfgang Betz 5:8bab0f419849 168 }
Wolfgang Betz 5:8bab0f419849 169
Wolfgang Betz 5:8bab0f419849 170
Wolfgang Betz 5:8bab0f419849 171 /**
Wolfgang Betz 5:8bab0f419849 172 * @brief Read data from LIS3MDL Magnetic sensor and calculate Magnetic in mgauss
Wolfgang Betz 5:8bab0f419849 173 * @param pData the pointer where the magnetometer data are stored
Wolfgang Betz 5:8bab0f419849 174 * @retval MAGNETO_OK in case of success, an error code otherwise
Wolfgang Betz 5:8bab0f419849 175 */
Wolfgang Betz 5:8bab0f419849 176 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxes(int32_t *pData)
Wolfgang Betz 5:8bab0f419849 177 {
Wolfgang Betz 5:8bab0f419849 178 uint8_t tempReg = 0x00;
Wolfgang Betz 5:8bab0f419849 179 int16_t pDataRaw[3];
Wolfgang Betz 5:8bab0f419849 180 float sensitivity = 0;
Wolfgang Betz 5:8bab0f419849 181
Wolfgang Betz 5:8bab0f419849 182 if(LIS3MDL_M_GetAxesRaw(pDataRaw) != MAGNETO_OK)
Wolfgang Betz 5:8bab0f419849 183 {
Wolfgang Betz 5:8bab0f419849 184 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 185 }
Wolfgang Betz 5:8bab0f419849 186
Wolfgang Betz 5:8bab0f419849 187 if(LIS3MDL_IO_Read(&tempReg, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
Wolfgang Betz 5:8bab0f419849 188 {
Wolfgang Betz 5:8bab0f419849 189 return MAGNETO_ERROR;
Wolfgang Betz 5:8bab0f419849 190 }
Wolfgang Betz 5:8bab0f419849 191
Wolfgang Betz 5:8bab0f419849 192 tempReg &= LIS3MDL_M_FS_MASK;
Wolfgang Betz 5:8bab0f419849 193
Wolfgang Betz 5:8bab0f419849 194 switch(tempReg)
Wolfgang Betz 5:8bab0f419849 195 {
Wolfgang Betz 5:8bab0f419849 196 case LIS3MDL_M_FS_4:
Wolfgang Betz 5:8bab0f419849 197 sensitivity = 0.14;
Wolfgang Betz 5:8bab0f419849 198 break;
Wolfgang Betz 5:8bab0f419849 199 case LIS3MDL_M_FS_8:
Wolfgang Betz 5:8bab0f419849 200 sensitivity = 0.29;
Wolfgang Betz 5:8bab0f419849 201 break;
Wolfgang Betz 5:8bab0f419849 202 case LIS3MDL_M_FS_12:
Wolfgang Betz 5:8bab0f419849 203 sensitivity = 0.43;
Wolfgang Betz 5:8bab0f419849 204 break;
Wolfgang Betz 5:8bab0f419849 205 case LIS3MDL_M_FS_16:
Wolfgang Betz 5:8bab0f419849 206 sensitivity = 0.58;
Wolfgang Betz 5:8bab0f419849 207 break;
Wolfgang Betz 5:8bab0f419849 208 }
Wolfgang Betz 5:8bab0f419849 209
Wolfgang Betz 5:8bab0f419849 210 pData[0] = (int32_t)(pDataRaw[0] * sensitivity);
Wolfgang Betz 5:8bab0f419849 211 pData[1] = (int32_t)(pDataRaw[1] * sensitivity);
Wolfgang Betz 5:8bab0f419849 212 pData[2] = (int32_t)(pDataRaw[2] * sensitivity);
Wolfgang Betz 5:8bab0f419849 213
Wolfgang Betz 5:8bab0f419849 214 return MAGNETO_OK;
Wolfgang Betz 5:8bab0f419849 215 }