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