Finished project.

Dependencies:   X_NUCLEO_COMMON

Fork of ReferredCoursework2016 by Stage-1 Students SoCEM

Committer:
Wolfgang Betz
Date:
Wed Jun 03 14:57:57 2015 +0200
Revision:
24:92cc9c6e4b2b
Parent:
5:8bab0f419849
Child:
57:04563dd74269
Upgrade to match X-CUBE-MEMS1/trunk, revision #402

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 24:92cc9c6e4b2b 46 X-CUBE-MEMS1/trunk/Drivers/BSP/Components/lis3mdl/lis3mdl.c: revision #400,
Wolfgang Betz 24:92cc9c6e4b2b 47 X-CUBE-MEMS1/trunk: revision #402
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 24:92cc9c6e4b2b 56 uint8_t tmp1 = 0x00;
Wolfgang Betz 24:92cc9c6e4b2b 57
Wolfgang Betz 24:92cc9c6e4b2b 58 /* Configure the low level interface ---------------------------------------*/
Wolfgang Betz 24:92cc9c6e4b2b 59 if(LIS3MDL_IO_Init() != MAGNETO_OK)
Wolfgang Betz 24:92cc9c6e4b2b 60 {
Wolfgang Betz 24:92cc9c6e4b2b 61 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 62 }
Wolfgang Betz 24:92cc9c6e4b2b 63
Wolfgang Betz 24:92cc9c6e4b2b 64 /****** Magnetic sensor *******/
Wolfgang Betz 24:92cc9c6e4b2b 65
Wolfgang Betz 24:92cc9c6e4b2b 66 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK)
Wolfgang Betz 24:92cc9c6e4b2b 67 {
Wolfgang Betz 24:92cc9c6e4b2b 68 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 69 }
Wolfgang Betz 24:92cc9c6e4b2b 70
Wolfgang Betz 24:92cc9c6e4b2b 71 /* Conversion mode selection */
Wolfgang Betz 24:92cc9c6e4b2b 72 tmp1 &= ~(LIS3MDL_M_MD_MASK);
Wolfgang Betz 24:92cc9c6e4b2b 73 tmp1 |= LIS3MDL_Init->M_OperatingMode;
Wolfgang Betz 24:92cc9c6e4b2b 74
Wolfgang Betz 24:92cc9c6e4b2b 75 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK)
Wolfgang Betz 24:92cc9c6e4b2b 76 {
Wolfgang Betz 24:92cc9c6e4b2b 77 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 78 }
Wolfgang Betz 24:92cc9c6e4b2b 79
Wolfgang Betz 24:92cc9c6e4b2b 80 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK)
Wolfgang Betz 24:92cc9c6e4b2b 81 {
Wolfgang Betz 24:92cc9c6e4b2b 82 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 83 }
Wolfgang Betz 24:92cc9c6e4b2b 84
Wolfgang Betz 24:92cc9c6e4b2b 85 /* Output data rate selection */
Wolfgang Betz 24:92cc9c6e4b2b 86 tmp1 &= ~(LIS3MDL_M_DO_MASK);
Wolfgang Betz 24:92cc9c6e4b2b 87 tmp1 |= LIS3MDL_Init->M_OutputDataRate;
Wolfgang Betz 24:92cc9c6e4b2b 88
Wolfgang Betz 24:92cc9c6e4b2b 89 /* X and Y axes Operative mode selection */
Wolfgang Betz 24:92cc9c6e4b2b 90 tmp1 &= ~(LIS3MDL_M_OM_MASK);
Wolfgang Betz 24:92cc9c6e4b2b 91 tmp1 |= LIS3MDL_Init->M_XYOperativeMode;
Wolfgang Betz 24:92cc9c6e4b2b 92
Wolfgang Betz 24:92cc9c6e4b2b 93 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK)
Wolfgang Betz 24:92cc9c6e4b2b 94 {
Wolfgang Betz 24:92cc9c6e4b2b 95 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 96 }
Wolfgang Betz 24:92cc9c6e4b2b 97
Wolfgang Betz 24:92cc9c6e4b2b 98 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
Wolfgang Betz 24:92cc9c6e4b2b 99 {
Wolfgang Betz 24:92cc9c6e4b2b 100 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 101 }
Wolfgang Betz 24:92cc9c6e4b2b 102
Wolfgang Betz 24:92cc9c6e4b2b 103 /* Full scale selection */
Wolfgang Betz 24:92cc9c6e4b2b 104 tmp1 &= ~(LIS3MDL_M_FS_MASK);
Wolfgang Betz 24:92cc9c6e4b2b 105 tmp1 |= LIS3MDL_Init->M_FullScale;
Wolfgang Betz 24:92cc9c6e4b2b 106
Wolfgang Betz 24:92cc9c6e4b2b 107 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
Wolfgang Betz 24:92cc9c6e4b2b 108 {
Wolfgang Betz 24:92cc9c6e4b2b 109 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 110 }
Wolfgang Betz 24:92cc9c6e4b2b 111
Wolfgang Betz 24:92cc9c6e4b2b 112 /* Configure interrupt lines */
Wolfgang Betz 24:92cc9c6e4b2b 113 LIS3MDL_IO_ITConfig();
Wolfgang Betz 24:92cc9c6e4b2b 114
Wolfgang Betz 24:92cc9c6e4b2b 115 return MAGNETO_OK;
Wolfgang Betz 24:92cc9c6e4b2b 116
Wolfgang Betz 24:92cc9c6e4b2b 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 24:92cc9c6e4b2b 128 if(!m_id)
Wolfgang Betz 24:92cc9c6e4b2b 129 {
Wolfgang Betz 24:92cc9c6e4b2b 130 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 131 }
Wolfgang Betz 24:92cc9c6e4b2b 132
Wolfgang Betz 24:92cc9c6e4b2b 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 24:92cc9c6e4b2b 144 uint8_t tempReg[2] = {0, 0};
Wolfgang Betz 24:92cc9c6e4b2b 145
Wolfgang Betz 24:92cc9c6e4b2b 146 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_X_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD),
Wolfgang Betz 24:92cc9c6e4b2b 147 2) != MAGNETO_OK)
Wolfgang Betz 24:92cc9c6e4b2b 148 {
Wolfgang Betz 24:92cc9c6e4b2b 149 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 150 }
Wolfgang Betz 24:92cc9c6e4b2b 151
Wolfgang Betz 24:92cc9c6e4b2b 152 pData[0] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
Wolfgang Betz 24:92cc9c6e4b2b 153
Wolfgang Betz 24:92cc9c6e4b2b 154 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Y_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD),
Wolfgang Betz 24:92cc9c6e4b2b 155 2) != MAGNETO_OK)
Wolfgang Betz 24:92cc9c6e4b2b 156 {
Wolfgang Betz 24:92cc9c6e4b2b 157 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 158 }
Wolfgang Betz 24:92cc9c6e4b2b 159
Wolfgang Betz 24:92cc9c6e4b2b 160 pData[1] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
Wolfgang Betz 24:92cc9c6e4b2b 161
Wolfgang Betz 24:92cc9c6e4b2b 162 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Z_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD),
Wolfgang Betz 24:92cc9c6e4b2b 163 2) != MAGNETO_OK)
Wolfgang Betz 24:92cc9c6e4b2b 164 {
Wolfgang Betz 24:92cc9c6e4b2b 165 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 166 }
Wolfgang Betz 24:92cc9c6e4b2b 167
Wolfgang Betz 24:92cc9c6e4b2b 168 pData[2] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
Wolfgang Betz 24:92cc9c6e4b2b 169
Wolfgang Betz 24:92cc9c6e4b2b 170 return MAGNETO_OK;
Wolfgang Betz 5:8bab0f419849 171 }
Wolfgang Betz 5:8bab0f419849 172
Wolfgang Betz 5:8bab0f419849 173
Wolfgang Betz 5:8bab0f419849 174 /**
Wolfgang Betz 5:8bab0f419849 175 * @brief Read data from LIS3MDL Magnetic sensor and calculate Magnetic in mgauss
Wolfgang Betz 5:8bab0f419849 176 * @param pData the pointer where the magnetometer data are stored
Wolfgang Betz 5:8bab0f419849 177 * @retval MAGNETO_OK in case of success, an error code otherwise
Wolfgang Betz 5:8bab0f419849 178 */
Wolfgang Betz 5:8bab0f419849 179 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxes(int32_t *pData)
Wolfgang Betz 5:8bab0f419849 180 {
Wolfgang Betz 24:92cc9c6e4b2b 181 uint8_t tempReg = 0x00;
Wolfgang Betz 24:92cc9c6e4b2b 182 int16_t pDataRaw[3];
Wolfgang Betz 24:92cc9c6e4b2b 183 float sensitivity = 0;
Wolfgang Betz 24:92cc9c6e4b2b 184
Wolfgang Betz 24:92cc9c6e4b2b 185 if(LIS3MDL_M_GetAxesRaw(pDataRaw) != MAGNETO_OK)
Wolfgang Betz 24:92cc9c6e4b2b 186 {
Wolfgang Betz 24:92cc9c6e4b2b 187 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 188 }
Wolfgang Betz 24:92cc9c6e4b2b 189
Wolfgang Betz 24:92cc9c6e4b2b 190 if(LIS3MDL_IO_Read(&tempReg, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
Wolfgang Betz 24:92cc9c6e4b2b 191 {
Wolfgang Betz 24:92cc9c6e4b2b 192 return MAGNETO_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 193 }
Wolfgang Betz 24:92cc9c6e4b2b 194
Wolfgang Betz 24:92cc9c6e4b2b 195 tempReg &= LIS3MDL_M_FS_MASK;
Wolfgang Betz 24:92cc9c6e4b2b 196
Wolfgang Betz 24:92cc9c6e4b2b 197 switch(tempReg)
Wolfgang Betz 24:92cc9c6e4b2b 198 {
Wolfgang Betz 24:92cc9c6e4b2b 199 case LIS3MDL_M_FS_4:
Wolfgang Betz 24:92cc9c6e4b2b 200 sensitivity = 0.14;
Wolfgang Betz 24:92cc9c6e4b2b 201 break;
Wolfgang Betz 24:92cc9c6e4b2b 202 case LIS3MDL_M_FS_8:
Wolfgang Betz 24:92cc9c6e4b2b 203 sensitivity = 0.29;
Wolfgang Betz 24:92cc9c6e4b2b 204 break;
Wolfgang Betz 24:92cc9c6e4b2b 205 case LIS3MDL_M_FS_12:
Wolfgang Betz 24:92cc9c6e4b2b 206 sensitivity = 0.43;
Wolfgang Betz 24:92cc9c6e4b2b 207 break;
Wolfgang Betz 24:92cc9c6e4b2b 208 case LIS3MDL_M_FS_16:
Wolfgang Betz 24:92cc9c6e4b2b 209 sensitivity = 0.58;
Wolfgang Betz 24:92cc9c6e4b2b 210 break;
Wolfgang Betz 24:92cc9c6e4b2b 211 }
Wolfgang Betz 24:92cc9c6e4b2b 212
Wolfgang Betz 24:92cc9c6e4b2b 213 pData[0] = (int32_t)(pDataRaw[0] * sensitivity);
Wolfgang Betz 24:92cc9c6e4b2b 214 pData[1] = (int32_t)(pDataRaw[1] * sensitivity);
Wolfgang Betz 24:92cc9c6e4b2b 215 pData[2] = (int32_t)(pDataRaw[2] * sensitivity);
Wolfgang Betz 24:92cc9c6e4b2b 216
Wolfgang Betz 24:92cc9c6e4b2b 217 return MAGNETO_OK;
Wolfgang Betz 24:92cc9c6e4b2b 218 }
Wolfgang Betz 5:8bab0f419849 219
Wolfgang Betz 24:92cc9c6e4b2b 220 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/