ESE project: portable camera safe box
Fork of X_NUCLEO_IKS01A1 by
Components/lis3mdl/lis3mdl_class.cpp@88:662bba2414a8, 2016-09-15 (annotated)
- Committer:
- hitzqt
- Date:
- Thu Sep 15 10:35:37 2016 +0000
- Revision:
- 88:662bba2414a8
- Parent:
- 57:04563dd74269
temperature, humidity and burglar alarm, safe mode
Who changed what in which revision?
User | Revision | Line number | New 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>© 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 "lis3mdl_class.h" |
Wolfgang Betz |
5:8bab0f419849 | 40 | #include "lis3mdl.h" |
Wolfgang Betz |
5:8bab0f419849 | 41 | |
Wolfgang Betz |
5:8bab0f419849 | 42 | /* Methods -------------------------------------------------------------------*/ |
Wolfgang Betz |
5:8bab0f419849 | 43 | /* betzw - based on: |
Wolfgang Betz |
24:92cc9c6e4b2b | 44 | X-CUBE-MEMS1/trunk/Drivers/BSP/Components/lis3mdl/lis3mdl.c: revision #400, |
Wolfgang Betz |
57:04563dd74269 | 45 | X-CUBE-MEMS1/trunk: revision #416 |
Wolfgang Betz |
5:8bab0f419849 | 46 | */ |
Wolfgang Betz |
57:04563dd74269 | 47 | |
Wolfgang Betz |
5:8bab0f419849 | 48 | /** |
Wolfgang Betz |
5:8bab0f419849 | 49 | * @brief Set LIS3MDL Initialization |
Wolfgang Betz |
5:8bab0f419849 | 50 | * @param LIS3MDL_Init the configuration setting for the LIS3MDL |
Wolfgang Betz |
5:8bab0f419849 | 51 | * @retval MAGNETO_OK in case of success, an error code otherwise |
Wolfgang Betz |
5:8bab0f419849 | 52 | */ |
Wolfgang Betz |
5:8bab0f419849 | 53 | MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init) |
Wolfgang Betz |
5:8bab0f419849 | 54 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 55 | uint8_t tmp1 = 0x00; |
Wolfgang Betz |
24:92cc9c6e4b2b | 56 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 57 | /* Configure the low level interface ---------------------------------------*/ |
Wolfgang Betz |
24:92cc9c6e4b2b | 58 | if(LIS3MDL_IO_Init() != MAGNETO_OK) |
Wolfgang Betz |
24:92cc9c6e4b2b | 59 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 60 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 61 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 62 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 63 | /****** Magnetic sensor *******/ |
Wolfgang Betz |
24:92cc9c6e4b2b | 64 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 65 | if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK) |
Wolfgang Betz |
24:92cc9c6e4b2b | 66 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 67 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 68 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 69 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 70 | /* Conversion mode selection */ |
Wolfgang Betz |
24:92cc9c6e4b2b | 71 | tmp1 &= ~(LIS3MDL_M_MD_MASK); |
Wolfgang Betz |
24:92cc9c6e4b2b | 72 | tmp1 |= LIS3MDL_Init->M_OperatingMode; |
Wolfgang Betz |
24:92cc9c6e4b2b | 73 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 74 | if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK) |
Wolfgang Betz |
24:92cc9c6e4b2b | 75 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 76 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 77 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 78 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 79 | if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK) |
Wolfgang Betz |
24:92cc9c6e4b2b | 80 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 81 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 82 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 83 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 84 | /* Output data rate selection */ |
Wolfgang Betz |
24:92cc9c6e4b2b | 85 | tmp1 &= ~(LIS3MDL_M_DO_MASK); |
Wolfgang Betz |
24:92cc9c6e4b2b | 86 | tmp1 |= LIS3MDL_Init->M_OutputDataRate; |
Wolfgang Betz |
24:92cc9c6e4b2b | 87 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 88 | /* X and Y axes Operative mode selection */ |
Wolfgang Betz |
24:92cc9c6e4b2b | 89 | tmp1 &= ~(LIS3MDL_M_OM_MASK); |
Wolfgang Betz |
24:92cc9c6e4b2b | 90 | tmp1 |= LIS3MDL_Init->M_XYOperativeMode; |
Wolfgang Betz |
24:92cc9c6e4b2b | 91 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 92 | if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK) |
Wolfgang Betz |
24:92cc9c6e4b2b | 93 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 94 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 95 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 96 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 97 | if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK) |
Wolfgang Betz |
24:92cc9c6e4b2b | 98 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 99 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 100 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 101 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 102 | /* Full scale selection */ |
Wolfgang Betz |
24:92cc9c6e4b2b | 103 | tmp1 &= ~(LIS3MDL_M_FS_MASK); |
Wolfgang Betz |
24:92cc9c6e4b2b | 104 | tmp1 |= LIS3MDL_Init->M_FullScale; |
Wolfgang Betz |
24:92cc9c6e4b2b | 105 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 106 | if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK) |
Wolfgang Betz |
24:92cc9c6e4b2b | 107 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 108 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 109 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 110 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 111 | /* Configure interrupt lines */ |
Wolfgang Betz |
24:92cc9c6e4b2b | 112 | LIS3MDL_IO_ITConfig(); |
Wolfgang Betz |
24:92cc9c6e4b2b | 113 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 114 | return MAGNETO_OK; |
Wolfgang Betz |
24:92cc9c6e4b2b | 115 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 116 | /******************************/ |
Wolfgang Betz |
5:8bab0f419849 | 117 | } |
Wolfgang Betz |
5:8bab0f419849 | 118 | |
Wolfgang Betz |
5:8bab0f419849 | 119 | |
Wolfgang Betz |
5:8bab0f419849 | 120 | /** |
Wolfgang Betz |
5:8bab0f419849 | 121 | * @brief Read ID of LIS3MDL Magnetic sensor |
Wolfgang Betz |
5:8bab0f419849 | 122 | * @param m_id the pointer where the ID of the device is stored |
Wolfgang Betz |
5:8bab0f419849 | 123 | * @retval MAGNETO_OK in case of success, an error code otherwise |
Wolfgang Betz |
5:8bab0f419849 | 124 | */ |
Wolfgang Betz |
5:8bab0f419849 | 125 | MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Read_M_ID(uint8_t *m_id) |
Wolfgang Betz |
5:8bab0f419849 | 126 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 127 | if(!m_id) |
Wolfgang Betz |
24:92cc9c6e4b2b | 128 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 129 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 130 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 131 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 132 | return LIS3MDL_IO_Read(m_id, LIS3MDL_M_WHO_AM_I_ADDR, 1); |
Wolfgang Betz |
5:8bab0f419849 | 133 | } |
Wolfgang Betz |
5:8bab0f419849 | 134 | |
Wolfgang Betz |
5:8bab0f419849 | 135 | |
Wolfgang Betz |
5:8bab0f419849 | 136 | /** |
Wolfgang Betz |
5:8bab0f419849 | 137 | * @brief Read raw data from LIS3MDL Magnetic sensor output register |
Wolfgang Betz |
5:8bab0f419849 | 138 | * @param pData the pointer where the magnetometer raw data are stored |
Wolfgang Betz |
5:8bab0f419849 | 139 | * @retval MAGNETO_OK in case of success, an error code otherwise |
Wolfgang Betz |
5:8bab0f419849 | 140 | */ |
Wolfgang Betz |
5:8bab0f419849 | 141 | MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxesRaw(int16_t *pData) |
Wolfgang Betz |
5:8bab0f419849 | 142 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 143 | uint8_t tempReg[2] = {0, 0}; |
Wolfgang Betz |
24:92cc9c6e4b2b | 144 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 145 | if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_X_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD), |
Wolfgang Betz |
24:92cc9c6e4b2b | 146 | 2) != MAGNETO_OK) |
Wolfgang Betz |
24:92cc9c6e4b2b | 147 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 148 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 149 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 150 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 151 | pData[0] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]); |
Wolfgang Betz |
24:92cc9c6e4b2b | 152 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 153 | if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Y_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD), |
Wolfgang Betz |
24:92cc9c6e4b2b | 154 | 2) != MAGNETO_OK) |
Wolfgang Betz |
24:92cc9c6e4b2b | 155 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 156 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 157 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 158 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 159 | pData[1] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]); |
Wolfgang Betz |
24:92cc9c6e4b2b | 160 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 161 | if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Z_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD), |
Wolfgang Betz |
24:92cc9c6e4b2b | 162 | 2) != MAGNETO_OK) |
Wolfgang Betz |
24:92cc9c6e4b2b | 163 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 164 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 165 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 166 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 167 | pData[2] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]); |
Wolfgang Betz |
24:92cc9c6e4b2b | 168 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 169 | return MAGNETO_OK; |
Wolfgang Betz |
5:8bab0f419849 | 170 | } |
Wolfgang Betz |
5:8bab0f419849 | 171 | |
Wolfgang Betz |
5:8bab0f419849 | 172 | |
Wolfgang Betz |
5:8bab0f419849 | 173 | /** |
Wolfgang Betz |
5:8bab0f419849 | 174 | * @brief Read data from LIS3MDL Magnetic sensor and calculate Magnetic in mgauss |
Wolfgang Betz |
5:8bab0f419849 | 175 | * @param pData the pointer where the magnetometer data are stored |
Wolfgang Betz |
5:8bab0f419849 | 176 | * @retval MAGNETO_OK in case of success, an error code otherwise |
Wolfgang Betz |
5:8bab0f419849 | 177 | */ |
Wolfgang Betz |
5:8bab0f419849 | 178 | MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxes(int32_t *pData) |
Wolfgang Betz |
5:8bab0f419849 | 179 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 180 | uint8_t tempReg = 0x00; |
Wolfgang Betz |
24:92cc9c6e4b2b | 181 | int16_t pDataRaw[3]; |
Wolfgang Betz |
24:92cc9c6e4b2b | 182 | float sensitivity = 0; |
Wolfgang Betz |
24:92cc9c6e4b2b | 183 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 184 | if(LIS3MDL_M_GetAxesRaw(pDataRaw) != MAGNETO_OK) |
Wolfgang Betz |
24:92cc9c6e4b2b | 185 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 186 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 187 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 188 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 189 | if(LIS3MDL_IO_Read(&tempReg, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK) |
Wolfgang Betz |
24:92cc9c6e4b2b | 190 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 191 | return MAGNETO_ERROR; |
Wolfgang Betz |
24:92cc9c6e4b2b | 192 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 193 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 194 | tempReg &= LIS3MDL_M_FS_MASK; |
Wolfgang Betz |
24:92cc9c6e4b2b | 195 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 196 | switch(tempReg) |
Wolfgang Betz |
24:92cc9c6e4b2b | 197 | { |
Wolfgang Betz |
24:92cc9c6e4b2b | 198 | case LIS3MDL_M_FS_4: |
Wolfgang Betz |
24:92cc9c6e4b2b | 199 | sensitivity = 0.14; |
Wolfgang Betz |
24:92cc9c6e4b2b | 200 | break; |
Wolfgang Betz |
24:92cc9c6e4b2b | 201 | case LIS3MDL_M_FS_8: |
Wolfgang Betz |
24:92cc9c6e4b2b | 202 | sensitivity = 0.29; |
Wolfgang Betz |
24:92cc9c6e4b2b | 203 | break; |
Wolfgang Betz |
24:92cc9c6e4b2b | 204 | case LIS3MDL_M_FS_12: |
Wolfgang Betz |
24:92cc9c6e4b2b | 205 | sensitivity = 0.43; |
Wolfgang Betz |
24:92cc9c6e4b2b | 206 | break; |
Wolfgang Betz |
24:92cc9c6e4b2b | 207 | case LIS3MDL_M_FS_16: |
Wolfgang Betz |
24:92cc9c6e4b2b | 208 | sensitivity = 0.58; |
Wolfgang Betz |
24:92cc9c6e4b2b | 209 | break; |
Wolfgang Betz |
24:92cc9c6e4b2b | 210 | } |
Wolfgang Betz |
24:92cc9c6e4b2b | 211 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 212 | pData[0] = (int32_t)(pDataRaw[0] * sensitivity); |
Wolfgang Betz |
24:92cc9c6e4b2b | 213 | pData[1] = (int32_t)(pDataRaw[1] * sensitivity); |
Wolfgang Betz |
24:92cc9c6e4b2b | 214 | pData[2] = (int32_t)(pDataRaw[2] * sensitivity); |
Wolfgang Betz |
24:92cc9c6e4b2b | 215 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 216 | return MAGNETO_OK; |
Wolfgang Betz |
24:92cc9c6e4b2b | 217 | } |
Wolfgang Betz |
5:8bab0f419849 | 218 | |
Wolfgang Betz |
24:92cc9c6e4b2b | 219 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |