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