Farnell-Element14 Bologna IOT Team / BSP_B-L475E-IOT01

Dependencies:   VL53L0X

Fork of BSP_B-L475E-IOT01 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lis3mdl.c Source File

lis3mdl.c

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    lis3mdl.c
00004  * @author  MCD Application Team
00005  * @version V1.0.0
00006  * @date    14-February-2017
00007  * @brief   This file provides a set of functions needed to manage the LIS3MDL
00008  *          magnetometer devices
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 /* Includes ------------------------------------------------------------------*/
00040 #include "lis3mdl.h"
00041 
00042 /** @addtogroup BSP
00043   * @{
00044   */
00045 
00046 /** @addtogroup Component
00047   * @{
00048   */
00049 
00050 /** @defgroup LIS3MDL LIS3MDL
00051   * @{
00052   */
00053 
00054 /** @defgroup LIS3MDL_Mag_Private_Variables LIS3MDL Mag Private Variables
00055   * @{
00056   */ 
00057 MAGNETO_DrvTypeDef Lis3mdlMagDrv =
00058 {
00059   LIS3MDL_MagInit,
00060   LIS3MDL_MagDeInit,
00061   LIS3MDL_MagReadID,
00062   0,
00063   LIS3MDL_MagLowPower,
00064   0,
00065   0,
00066   0,
00067   0,
00068   0,
00069   0,
00070   0,
00071   LIS3MDL_MagReadXYZ
00072 };
00073 /**
00074   * @}
00075   */ 
00076 
00077 
00078 /** @defgroup LIS3MDL_Mag_Private_Functions LIS3MDL Mag Private Functions
00079   * @{
00080   */
00081 /**
00082   * @brief  Set LIS3MDL Magnetometer Initialization.
00083   * @param  LIS3MDL_InitStruct: pointer to a LIS3MDL_MagInitTypeDef structure 
00084   *         that contains the configuration setting for the LIS3MDL.
00085   */
00086 void LIS3MDL_MagInit(MAGNETO_InitTypeDef LIS3MDL_InitStruct)
00087 {  
00088   SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG1, LIS3MDL_InitStruct.Register1);
00089   SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG2, LIS3MDL_InitStruct.Register2);
00090   SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG3, LIS3MDL_InitStruct.Register3);
00091   SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG4, LIS3MDL_InitStruct.Register4);
00092   SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG5, LIS3MDL_InitStruct.Register5);
00093 }
00094 
00095 /**
00096   * @brief  LIS3MDL Magnetometer De-initialization.
00097   */
00098 void LIS3MDL_MagDeInit(void)
00099 {
00100   uint8_t ctrl = 0x00;
00101   
00102   /* Read control register 1 value */
00103   ctrl = SENSOR_IO_Read(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG3);
00104 
00105   /* Clear Selection Mode bits */
00106   ctrl &= ~(LIS3MDL_MAG_SELECTION_MODE);
00107 
00108   /* Set Power down */
00109   ctrl |= LIS3MDL_MAG_POWERDOWN2_MODE;
00110   
00111   /* write back control register */
00112   SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG3, ctrl);  
00113 }
00114 
00115 /**
00116   * @brief  Read LIS3MDL ID.
00117   * @retval ID 
00118   */
00119 uint8_t LIS3MDL_MagReadID(void)
00120 {
00121   /* IO interface initialization */
00122   SENSOR_IO_Init();  
00123   /* Read value at Who am I register address */
00124   return (SENSOR_IO_Read(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_WHO_AM_I_REG));
00125 }
00126 
00127 /**
00128   * @brief  Set/Unset Magnetometer in low power mode.
00129   * @param  status 0 means disable Low Power Mode, otherwise Low Power Mode is enabled
00130   */
00131 void LIS3MDL_MagLowPower(uint16_t status)
00132 {  
00133   uint8_t ctrl = 0;
00134   
00135   /* Read control register 1 value */
00136   ctrl = SENSOR_IO_Read(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG3);
00137 
00138   /* Clear Low Power Mode bit */
00139   ctrl &= ~(0x20);
00140 
00141   /* Set Low Power Mode */
00142   if(status)
00143   {
00144     ctrl |= LIS3MDL_MAG_CONFIG_LOWPOWER_MODE;
00145   }else
00146   {
00147     ctrl |= LIS3MDL_MAG_CONFIG_NORMAL_MODE;
00148   }
00149   
00150   /* write back control register */
00151   SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG3, ctrl);  
00152 }
00153 
00154 /**
00155   * @brief  Read X, Y & Z Magnetometer values 
00156   * @param  pData: Data out pointer
00157   */
00158 void LIS3MDL_MagReadXYZ(int16_t* pData)
00159 {
00160   int16_t pnRawData[3];
00161   uint8_t ctrlm= 0;
00162   uint8_t buffer[6];
00163   uint8_t i = 0;
00164   float sensitivity = 0;
00165   
00166   /* Read the magnetometer control register content */
00167   ctrlm = SENSOR_IO_Read(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG2);
00168   
00169   /* Read output register X, Y & Z acceleration */
00170   SENSOR_IO_ReadMultiple(LIS3MDL_MAG_I2C_ADDRESS_HIGH, (LIS3MDL_MAG_OUTX_L | 0x80), buffer, 6);
00171   
00172   for(i=0; i<3; i++)
00173   {
00174     pnRawData[i]=((((uint16_t)buffer[2*i+1]) << 8) + (uint16_t)buffer[2*i]);
00175   }
00176   
00177   /* Normal mode */
00178   /* Switch the sensitivity value set in the CRTL_REG2 */
00179   switch(ctrlm & 0x60)
00180   {
00181   case LIS3MDL_MAG_FS_4_GA:
00182     sensitivity = LIS3MDL_MAG_SENSITIVITY_FOR_FS_4GA;
00183     break;
00184   case LIS3MDL_MAG_FS_8_GA:
00185     sensitivity = LIS3MDL_MAG_SENSITIVITY_FOR_FS_8GA;
00186     break;
00187   case LIS3MDL_MAG_FS_12_GA:
00188     sensitivity = LIS3MDL_MAG_SENSITIVITY_FOR_FS_12GA;
00189     break;
00190   case LIS3MDL_MAG_FS_16_GA:
00191     sensitivity = LIS3MDL_MAG_SENSITIVITY_FOR_FS_16GA;
00192     break;    
00193   }
00194   
00195   /* Obtain the mGauss value for the three axis */
00196   for(i=0; i<3; i++)
00197   {
00198     pData[i]=( int16_t )(pnRawData[i] * sensitivity);
00199   }
00200 }
00201 
00202 
00203 /**
00204   * @}
00205   */ 
00206 
00207 /**
00208   * @}
00209   */
00210   
00211 /**
00212   * @}
00213   */
00214   
00215 /**
00216   * @}
00217   */
00218   
00219 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00220