Firmware Library for X-NUCLEO-IKS01A1 (MEMS Inertial & Environmental Sensors) Expansion Board
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: MultiTech_Dragonfly_2015_ATT_Gov_Solutions_Hackathon_Example HelloWorld_IKS01A1 LoRaWAN-test-10secs ServoMotorDemo ... more
Fork of X_NUCLEO_IKS01A1 by
lis3mdl_class.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file lis3mdl_class.cpp 00004 * @author AST / EST 00005 * @version V0.0.1 00006 * @date 14-April-2015 00007 * @brief Implementation file for the LIS3MDL driver class 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 1. Redistributions of source code must retain the above copyright notice, 00016 * this list of conditions and the following disclaimer. 00017 * 2. Redistributions in binary form must reproduce the above copyright notice, 00018 * this list of conditions and the following disclaimer in the documentation 00019 * and/or other materials provided with the distribution. 00020 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 ****************************************************************************** 00036 */ 00037 00038 /* Includes ------------------------------------------------------------------*/ 00039 #include "lis3mdl_class.h" 00040 #include "lis3mdl.h" 00041 00042 /* Methods -------------------------------------------------------------------*/ 00043 /* betzw - based on: 00044 X-CUBE-MEMS1/trunk/Drivers/BSP/Components/lis3mdl/lis3mdl.c: revision #400, 00045 X-CUBE-MEMS1/trunk: revision #416 00046 */ 00047 00048 /** 00049 * @brief Set LIS3MDL Initialization 00050 * @param LIS3MDL_Init the configuration setting for the LIS3MDL 00051 * @retval MAGNETO_OK in case of success, an error code otherwise 00052 */ 00053 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init) 00054 { 00055 uint8_t tmp1 = 0x00; 00056 00057 /* Configure the low level interface ---------------------------------------*/ 00058 if(LIS3MDL_IO_Init() != MAGNETO_OK) 00059 { 00060 return MAGNETO_ERROR; 00061 } 00062 00063 /****** Magnetic sensor *******/ 00064 00065 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK) 00066 { 00067 return MAGNETO_ERROR; 00068 } 00069 00070 /* Conversion mode selection */ 00071 tmp1 &= ~(LIS3MDL_M_MD_MASK); 00072 tmp1 |= LIS3MDL_Init->M_OperatingMode; 00073 00074 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK) 00075 { 00076 return MAGNETO_ERROR; 00077 } 00078 00079 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK) 00080 { 00081 return MAGNETO_ERROR; 00082 } 00083 00084 /* Output data rate selection */ 00085 tmp1 &= ~(LIS3MDL_M_DO_MASK); 00086 tmp1 |= LIS3MDL_Init->M_OutputDataRate; 00087 00088 /* X and Y axes Operative mode selection */ 00089 tmp1 &= ~(LIS3MDL_M_OM_MASK); 00090 tmp1 |= LIS3MDL_Init->M_XYOperativeMode; 00091 00092 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK) 00093 { 00094 return MAGNETO_ERROR; 00095 } 00096 00097 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK) 00098 { 00099 return MAGNETO_ERROR; 00100 } 00101 00102 /* Full scale selection */ 00103 tmp1 &= ~(LIS3MDL_M_FS_MASK); 00104 tmp1 |= LIS3MDL_Init->M_FullScale; 00105 00106 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK) 00107 { 00108 return MAGNETO_ERROR; 00109 } 00110 00111 /* Configure interrupt lines */ 00112 LIS3MDL_IO_ITConfig(); 00113 00114 return MAGNETO_OK; 00115 00116 /******************************/ 00117 } 00118 00119 00120 /** 00121 * @brief Read ID of LIS3MDL Magnetic sensor 00122 * @param m_id the pointer where the ID of the device is stored 00123 * @retval MAGNETO_OK in case of success, an error code otherwise 00124 */ 00125 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Read_M_ID(uint8_t *m_id) 00126 { 00127 if(!m_id) 00128 { 00129 return MAGNETO_ERROR; 00130 } 00131 00132 return LIS3MDL_IO_Read(m_id, LIS3MDL_M_WHO_AM_I_ADDR, 1); 00133 } 00134 00135 00136 /** 00137 * @brief Read raw data from LIS3MDL Magnetic sensor output register 00138 * @param pData the pointer where the magnetometer raw data are stored 00139 * @retval MAGNETO_OK in case of success, an error code otherwise 00140 */ 00141 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxesRaw(int16_t *pData) 00142 { 00143 uint8_t tempReg[2] = {0, 0}; 00144 00145 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_X_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD), 00146 2) != MAGNETO_OK) 00147 { 00148 return MAGNETO_ERROR; 00149 } 00150 00151 pData[0] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]); 00152 00153 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Y_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD), 00154 2) != MAGNETO_OK) 00155 { 00156 return MAGNETO_ERROR; 00157 } 00158 00159 pData[1] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]); 00160 00161 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Z_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD), 00162 2) != MAGNETO_OK) 00163 { 00164 return MAGNETO_ERROR; 00165 } 00166 00167 pData[2] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]); 00168 00169 return MAGNETO_OK; 00170 } 00171 00172 00173 /** 00174 * @brief Read data from LIS3MDL Magnetic sensor and calculate Magnetic in mgauss 00175 * @param pData the pointer where the magnetometer data are stored 00176 * @retval MAGNETO_OK in case of success, an error code otherwise 00177 */ 00178 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxes(int32_t *pData) 00179 { 00180 uint8_t tempReg = 0x00; 00181 int16_t pDataRaw[3]; 00182 float sensitivity = 0; 00183 00184 if(LIS3MDL_M_GetAxesRaw(pDataRaw) != MAGNETO_OK) 00185 { 00186 return MAGNETO_ERROR; 00187 } 00188 00189 if(LIS3MDL_IO_Read(&tempReg, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK) 00190 { 00191 return MAGNETO_ERROR; 00192 } 00193 00194 tempReg &= LIS3MDL_M_FS_MASK; 00195 00196 switch(tempReg) 00197 { 00198 case LIS3MDL_M_FS_4: 00199 sensitivity = 0.14; 00200 break; 00201 case LIS3MDL_M_FS_8: 00202 sensitivity = 0.29; 00203 break; 00204 case LIS3MDL_M_FS_12: 00205 sensitivity = 0.43; 00206 break; 00207 case LIS3MDL_M_FS_16: 00208 sensitivity = 0.58; 00209 break; 00210 } 00211 00212 pData[0] = (int32_t)(pDataRaw[0] * sensitivity); 00213 pData[1] = (int32_t)(pDataRaw[1] * sensitivity); 00214 pData[2] = (int32_t)(pDataRaw[2] * sensitivity); 00215 00216 return MAGNETO_OK; 00217 } 00218 00219 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 17:19:45 by 1.7.2