Copied from STs implementation.

Dependents:   teensyIMU

Committer:
bclaus
Date:
Thu Oct 06 16:45:48 2016 +0000
Revision:
0:32f3441153b5
Child:
1:898554a35638
initial;

Who changed what in which revision?

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