E&R S3 prime / BSP_DISCO_L476VG
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l476g_discovery_compass.c Source File

stm32l476g_discovery_compass.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l476g_discovery_compass.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides a set of functions needed to manage the E-Compass
00006   *          (ACCELEROMETER + MAGNETOMETER) MEMS LSM303C available on STM32L476G-Discovery
00007   *          board.
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00012   * All rights reserved.</center></h2>
00013   *
00014   * This software component is licensed by ST under BSD 3-Clause license,
00015   * the "License"; You may not use this file except in compliance with the
00016   * License. You may obtain a copy of the License at:
00017   *                        opensource.org/licenses/BSD-3-Clause
00018   *
00019   ******************************************************************************
00020   */
00021 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32l476g_discovery.h"
00024 #include "stm32l476g_discovery_compass.h"
00025 #include "../Components/lsm303c/lsm303c.h"
00026 #include <math.h>
00027 
00028 /** @addtogroup BSP
00029   * @{
00030   */
00031 
00032 /** @addtogroup STM32L476G_DISCOVERY
00033   * @{
00034   */
00035 
00036 /** @defgroup STM32L476G_DISCOVERY_COMPASS STM32L476G-DISCOVERY COMPASS
00037   * @{
00038   */
00039 
00040 /* Private typedef -----------------------------------------------------------*/
00041 /** @defgroup STM32L476G_DISCOVERY_COMPASS_Private_Types Private Types
00042   * @{
00043   */
00044 /**
00045   * @}
00046   */
00047 
00048 /* Private defines ------------------------------------------------------------*/
00049 /** @defgroup STM32L476G_DISCOVERY_COMPASS_Private_Constants Private Constants
00050   * @{
00051   */
00052 /**
00053   * @}
00054   */
00055 
00056 /* Private macros ------------------------------------------------------------*/
00057 /** @defgroup STM32L476G_DISCOVERY_COMPASS_Private_Macros Private Macros
00058   * @{
00059   */
00060 /**
00061   * @}
00062   */
00063 
00064 /* Private variables ---------------------------------------------------------*/
00065 /** @defgroup STM32L476G_DISCOVERY_COMPASS_Private_Variables Private Variables
00066   * @{
00067   */
00068 static ACCELERO_DrvTypeDef *AccelerometerDrv;
00069 static MAGNETO_DrvTypeDef  *MagnetoDrv;
00070 
00071 /**
00072   * @}
00073   */
00074 
00075 /* Private function prototypes -----------------------------------------------*/
00076 /** @addtogroup STM32L476G_DISCOVERY_COMPASS_Private_FunctionPrototypes Private Functions
00077   * @{
00078   */
00079 /**
00080   * @}
00081   */
00082 
00083 /* Exported functions ---------------------------------------------------------*/
00084 /** @addtogroup STM32L476G_DISCOVERY_COMPASS_Exported_Functions
00085   * @{
00086   */
00087 extern void  ACCELERO_IO_DeInit(void);
00088 extern void  MAGNETO_IO_DeInit(void);
00089 
00090 /**
00091   * @brief  Initialize the COMPASS.
00092   * @retval COMPASS_OK or COMPASS_ERROR
00093   */
00094 COMPASS_StatusTypeDef BSP_COMPASS_Init(void)
00095 {
00096   COMPASS_StatusTypeDef ret = COMPASS_OK;
00097   uint16_t ctrl = 0x0000;
00098   ACCELERO_InitTypeDef LSM303C_InitStructure;
00099   ACCELERO_FilterConfigTypeDef LSM303C_FilterStructure;
00100   MAGNETO_InitTypeDef LSM303C_InitStructureMag;
00101 
00102   if (Lsm303cDrv_accelero.ReadID() != LMS303C_ACC_ID)
00103   {
00104     ret = COMPASS_ERROR;
00105   }
00106   else
00107   {
00108     /* Initialize the COMPASS accelerometer driver structure */
00109     AccelerometerDrv = &Lsm303cDrv_accelero;
00110 
00111     /* MEMS configuration ------------------------------------------------------*/
00112     /* Fill the COMPASS accelerometer structure */
00113     LSM303C_InitStructure.AccOutput_DataRate = LSM303C_ACC_ODR_50_HZ;
00114     LSM303C_InitStructure.Axes_Enable = LSM303C_ACC_AXES_ENABLE;
00115     LSM303C_InitStructure.AccFull_Scale = LSM303C_ACC_FULLSCALE_2G;
00116     LSM303C_InitStructure.BlockData_Update = LSM303C_ACC_BDU_CONTINUOUS;
00117     LSM303C_InitStructure.High_Resolution = LSM303C_ACC_HR_DISABLE;
00118     LSM303C_InitStructure.Communication_Mode = LSM303C_ACC_SPI_MODE;
00119 
00120     /* Configure MEMS: data rate, power mode, full scale and axes */
00121     ctrl = (LSM303C_InitStructure.High_Resolution | LSM303C_InitStructure.AccOutput_DataRate | \
00122             LSM303C_InitStructure.Axes_Enable | LSM303C_InitStructure.BlockData_Update);
00123 
00124     ctrl |= (LSM303C_InitStructure.AccFull_Scale | LSM303C_InitStructure.Communication_Mode) << 8;
00125 
00126     /* Configure the COMPASS accelerometer main parameters */
00127     AccelerometerDrv->Init(ctrl);
00128 
00129     /* Fill the COMPASS accelerometer HPF structure */
00130     LSM303C_FilterStructure.HighPassFilter_Mode_Selection = LSM303C_ACC_HPM_NORMAL_MODE;
00131     LSM303C_FilterStructure.HighPassFilter_CutOff_Frequency = LSM303C_ACC_DFC1_ODRDIV50;
00132     LSM303C_FilterStructure.HighPassFilter_Stat = LSM303C_ACC_HPI2S_INT1_DISABLE | LSM303C_ACC_HPI2S_INT2_DISABLE;
00133 
00134     /* Configure MEMS: mode, cutoff frequency, Filter status, Click, AOI1 and AOI2 */
00135     ctrl = (uint8_t)(LSM303C_FilterStructure.HighPassFilter_Mode_Selection | \
00136                      LSM303C_FilterStructure.HighPassFilter_CutOff_Frequency | \
00137                      LSM303C_FilterStructure.HighPassFilter_Stat);
00138 
00139     /* Configure the COMPASS accelerometer LPF main parameters */
00140     AccelerometerDrv->FilterConfig(ctrl);
00141   }
00142 
00143   if (Lsm303cDrv_magneto.ReadID() != LMS303C_MAG_ID)
00144   {
00145     ret = COMPASS_ERROR;
00146   }
00147   else
00148   {
00149     /* Initialize the COMPASS magnetometer driver structure */
00150     MagnetoDrv = &Lsm303cDrv_magneto;
00151 
00152     /* MEMS configuration ------------------------------------------------------*/
00153     /* Fill the COMPASS magnetometer structure */
00154     LSM303C_InitStructureMag.Register1 = LSM303C_MAG_TEMPSENSOR_DISABLE | LSM303C_MAG_OM_XY_ULTRAHIGH | LSM303C_MAG_ODR_40_HZ;
00155     LSM303C_InitStructureMag.Register2 = LSM303C_MAG_FS_16_GA | LSM303C_MAG_REBOOT_DEFAULT | LSM303C_MAG_SOFT_RESET_DEFAULT;
00156     LSM303C_InitStructureMag.Register3 = LSM303C_MAG_SPI_MODE | LSM303C_MAG_CONFIG_NORMAL_MODE | LSM303C_MAG_CONTINUOUS_MODE;
00157     LSM303C_InitStructureMag.Register4 = LSM303C_MAG_OM_Z_ULTRAHIGH | LSM303C_MAG_BLE_LSB;
00158     LSM303C_InitStructureMag.Register5 = LSM303C_MAG_BDU_CONTINUOUS;
00159     /* Configure the COMPASS magnetometer main parameters */
00160     MagnetoDrv->Init(LSM303C_InitStructureMag);
00161   }
00162 
00163   return ret;
00164 }
00165 
00166 /**
00167   * @brief  DeInitialize the COMPASS.
00168   * @retval None.
00169   */
00170 void BSP_COMPASS_DeInit(void)
00171 {
00172   /* DeInitialize the COMPASS accelerometer & magnetometer IO interfaces */
00173   ACCELERO_IO_DeInit();
00174   MAGNETO_IO_DeInit();
00175 }
00176 
00177 /**
00178   * @brief  Put the COMPASS in low power mode.
00179   * @retval None
00180   */
00181 void BSP_COMPASS_LowPower(void)
00182 {
00183   /* Put the COMPASS accelerometer in low power mode */
00184   if (AccelerometerDrv != NULL)
00185   {
00186     if (AccelerometerDrv->LowPower != NULL)
00187     {
00188       AccelerometerDrv->LowPower(LSM303C_ACC_ODR_OFF);
00189     }
00190   }
00191   /* Put the COMPASS magnetometer in low power mode */
00192   if (MagnetoDrv != NULL)
00193   {
00194     if (MagnetoDrv->LowPower != NULL)
00195     {
00196       MagnetoDrv->LowPower(LSM303C_MAG_POWERDOWN2_MODE);
00197     }
00198   }
00199 }
00200 
00201 /**
00202   * @brief  Get XYZ acceleration values.
00203   * @param  pDataXYZ Pointer on 3 angular accelerations table with
00204   *                  pDataXYZ[0] = X axis, pDataXYZ[1] = Y axis, pDataXYZ[2] = Z axis
00205   * @retval None
00206   */
00207 void BSP_COMPASS_AccGetXYZ(int16_t *pDataXYZ)
00208 {
00209   if (AccelerometerDrv != NULL)
00210   {
00211     if (AccelerometerDrv->GetXYZ != NULL)
00212     {
00213       AccelerometerDrv->GetXYZ(pDataXYZ);
00214     }
00215   }
00216 }
00217 
00218 /**
00219   * @brief  Get XYZ magnetometer values.
00220   * @param  pDataXYZ Pointer on 3 magnetometer values table with
00221   *                  pDataXYZ[0] = X axis, pDataXYZ[1] = Y axis, pDataXYZ[2] = Z axis
00222   * @retval None
00223   */
00224 void BSP_COMPASS_MagGetXYZ(int16_t *pDataXYZ)
00225 {
00226   if (MagnetoDrv != NULL)
00227   {
00228     if (MagnetoDrv->GetXYZ != NULL)
00229     {
00230       MagnetoDrv->GetXYZ(pDataXYZ);
00231     }
00232   }
00233 }
00234 
00235 /**
00236   * @}
00237   */
00238 
00239 /**
00240   * @}
00241   */
00242 
00243 /**
00244   * @}
00245   */
00246 
00247 /**
00248   * @}
00249   */
00250 
00251 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/