Copied from STs implementation.

Dependents:   teensyIMU

Committer:
bclaus
Date:
Thu Oct 06 23:28:08 2016 +0000
Revision:
1:898554a35638
Parent:
0:32f3441153b5
functional;

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 1:898554a35638 51 * @brief Read data from LIS3MDL Magnetic sensor and calculate Magnetic in microTeslas.
bclaus 0:32f3441153b5 52 * @param float *pfData
bclaus 0:32f3441153b5 53 * @retval None.
bclaus 0:32f3441153b5 54 */
bclaus 1:898554a35638 55 void LIS3MDL::readMag(void)
bclaus 0:32f3441153b5 56 {
bclaus 1:898554a35638 57
bclaus 0:32f3441153b5 58 float sensitivity = 0;
bclaus 1:898554a35638 59
bclaus 0:32f3441153b5 60
bclaus 1:898554a35638 61 GetAxesRaw();
bclaus 1:898554a35638 62 /*
bclaus 0:32f3441153b5 63 case LIS3MDL_M_FS_4:
bclaus 0:32f3441153b5 64 sensitivity = 0.14;
bclaus 0:32f3441153b5 65 break;
bclaus 0:32f3441153b5 66 case LIS3MDL_M_FS_8:
bclaus 0:32f3441153b5 67 sensitivity = 0.29;
bclaus 0:32f3441153b5 68 break;
bclaus 0:32f3441153b5 69 case LIS3MDL_M_FS_12:
bclaus 0:32f3441153b5 70 sensitivity = 0.43;
bclaus 0:32f3441153b5 71 break;
bclaus 0:32f3441153b5 72 case LIS3MDL_M_FS_16:
bclaus 0:32f3441153b5 73 sensitivity = 0.58;
bclaus 0:32f3441153b5 74 break;
bclaus 0:32f3441153b5 75 }
bclaus 1:898554a35638 76 }*/
bclaus 1:898554a35638 77 sensitivity = 0.12207;
bclaus 1:898554a35638 78 mx = mx_raw * sensitivity;
bclaus 1:898554a35638 79 my = my_raw * sensitivity;
bclaus 1:898554a35638 80 mz = mz_raw * sensitivity;
bclaus 0:32f3441153b5 81
bclaus 0:32f3441153b5 82 }
bclaus 0:32f3441153b5 83
bclaus 0:32f3441153b5 84 /**
bclaus 0:32f3441153b5 85 * @brief Read raw data from LIS3MDL Magnetic sensor output register.
bclaus 0:32f3441153b5 86 * @param float *pfData
bclaus 0:32f3441153b5 87 * @retval None.
bclaus 0:32f3441153b5 88 */
bclaus 1:898554a35638 89 void LIS3MDL::GetAxesRaw()
bclaus 0:32f3441153b5 90 {
bclaus 1:898554a35638 91 char data[6];
bclaus 1:898554a35638 92
bclaus 1:898554a35638 93 char subAddressXL = LIS3MDL_M_OUT_X_L_M;
bclaus 0:32f3441153b5 94
bclaus 1:898554a35638 95 _i2c.write(LIS3MDL_M_MEMS_ADDRESS, &subAddressXL, 1, true);
bclaus 1:898554a35638 96 _i2c.read(LIS3MDL_M_MEMS_ADDRESS, data, 6);
bclaus 1:898554a35638 97
bclaus 1:898554a35638 98 mx_raw = data[0] | (data[1] << 8);
bclaus 1:898554a35638 99 my_raw = data[2] | (data[3] << 8);
bclaus 1:898554a35638 100 mz_raw = data[4] | (data[5] << 8);
bclaus 0:32f3441153b5 101 }
bclaus 0:32f3441153b5 102
bclaus 0:32f3441153b5 103 /**
bclaus 0:32f3441153b5 104 * @brief Read ID address of HTS221
bclaus 0:32f3441153b5 105 * @param Device ID address
bclaus 0:32f3441153b5 106 * @retval ID name
bclaus 0:32f3441153b5 107 */
bclaus 0:32f3441153b5 108 uint8_t LIS3MDL::ReadID(void)
bclaus 0:32f3441153b5 109 {
bclaus 1:898554a35638 110 char data[1];
bclaus 0:32f3441153b5 111 int ret;
bclaus 1:898554a35638 112 char subAddress = LIS3MDL_M_WHO_AM_I_ADDR;
bclaus 0:32f3441153b5 113
bclaus 0:32f3441153b5 114 /* Read WHO I AM register */
bclaus 1:898554a35638 115 _i2c.write(LIS3MDL_M_MEMS_ADDRESS, &subAddress, 1, true);
bclaus 1:898554a35638 116 ret=_i2c.read(LIS3MDL_M_MEMS_ADDRESS, data, 1);
bclaus 0:32f3441153b5 117
bclaus 0:32f3441153b5 118 /* Return the ID */
bclaus 1:898554a35638 119 return ((ret == 0) ? (uint8_t)data[0] : 0);
bclaus 0:32f3441153b5 120 }
bclaus 0:32f3441153b5 121
bclaus 0:32f3441153b5 122 /**
bclaus 0:32f3441153b5 123 * @brief Set HTS221 Initialization.
bclaus 0:32f3441153b5 124 * @param InitStruct: it contains the configuration setting for the HTS221.
bclaus 0:32f3441153b5 125 * @retval None
bclaus 0:32f3441153b5 126 */
bclaus 0:32f3441153b5 127 void LIS3MDL::Init() {
bclaus 0:32f3441153b5 128
bclaus 1:898554a35638 129 char tmp1[2];
bclaus 0:32f3441153b5 130
bclaus 0:32f3441153b5 131 /****** Magnetic sensor *******/
bclaus 1:898554a35638 132
bclaus 0:32f3441153b5 133 /* Conversion mode selection */
bclaus 1:898554a35638 134 tmp1[0] = LIS3MDL_M_CTRL_REG3_M;
bclaus 1:898554a35638 135 tmp1[1] = 0x00;
bclaus 1:898554a35638 136
bclaus 1:898554a35638 137 _i2c.write(LIS3MDL_M_MEMS_ADDRESS, tmp1, 2);
bclaus 1:898554a35638 138
bclaus 0:32f3441153b5 139
bclaus 1:898554a35638 140 //OM=UHP for XY
bclaus 1:898554a35638 141 //FAST_ODR enabled - 155Hz
bclaus 1:898554a35638 142 tmp1[0] = LIS3MDL_M_CTRL_REG1_M;
bclaus 1:898554a35638 143 tmp1[1] = 0x8E;
bclaus 1:898554a35638 144
bclaus 1:898554a35638 145 _i2c.write(LIS3MDL_M_MEMS_ADDRESS, tmp1, 2);
bclaus 0:32f3441153b5 146
bclaus 1:898554a35638 147 /* Full scale selection */
bclaus 1:898554a35638 148 tmp1[0] = LIS3MDL_M_CTRL_REG2_M;
bclaus 1:898554a35638 149 tmp1[1] = 0x00;
bclaus 0:32f3441153b5 150
bclaus 1:898554a35638 151 _i2c.write(LIS3MDL_M_MEMS_ADDRESS, tmp1, 2);
bclaus 0:32f3441153b5 152
bclaus 1:898554a35638 153 /*Fast Z*/
bclaus 1:898554a35638 154 tmp1[0] = LIS3MDL_M_CTRL_REG4_M;
bclaus 1:898554a35638 155 tmp1[1] = 0x0C;
bclaus 0:32f3441153b5 156
bclaus 1:898554a35638 157 _i2c.write(LIS3MDL_M_MEMS_ADDRESS, tmp1, 2);
bclaus 0:32f3441153b5 158
bclaus 0:32f3441153b5 159 /******************************/
bclaus 0:32f3441153b5 160
bclaus 0:32f3441153b5 161 if(ReadID() == I_AM_LIS3MDL_M)
bclaus 0:32f3441153b5 162 {
bclaus 0:32f3441153b5 163 LIS3MDLInitialized = 1;
bclaus 0:32f3441153b5 164 }
bclaus 1:898554a35638 165
bclaus 0:32f3441153b5 166
bclaus 0:32f3441153b5 167 return;
bclaus 0:32f3441153b5 168 }