STM32F4-Discovery BSP User Manual: stm32f4_discovery_accelerometer.c Source File

STM32F4-Discovery BSP

stm32f4_discovery_accelerometer.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4_discovery_accelerometer.c
00004   * @author  MCD Application Team
00005   * @version V2.1.2
00006   * @date    31-January-2017
00007   * @brief   This file provides a set of functions needed to manage the
00008   *          MEMS accelerometers available on STM32F4-Discovery Kit.
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 "stm32f4_discovery_accelerometer.h"
00041 
00042 /** @addtogroup BSP 
00043   * @{
00044   */
00045 
00046 /** @addtogroup STM32F4_DISCOVERY
00047   * @{
00048   */ 
00049 
00050 /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER STM32F4 DISCOVERY ACCELEROMETER
00051   * @brief  This file includes the motion sensor driver for ACCELEROMETER motion sensor 
00052   *         devices.
00053   * @{
00054   */
00055 
00056 /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER_Private_TypesDefinitions STM32F4 DISCOVERY ACCELEROMETER Private TypesDefinitions
00057   * @{
00058   */
00059 /**
00060   * @}
00061   */
00062 
00063 /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER_Private_Defines STM32F4 DISCOVERY ACCELEROMETER Private Defines
00064   * @{
00065   */
00066 /**
00067   * @}
00068   */
00069 
00070 /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER_Private_Macros STM32F4 DISCOVERY ACCELEROMETER Private Macros
00071   * @{
00072   */
00073 /**
00074   * @}
00075   */ 
00076   
00077 /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER_Private_Variables STM32F4 DISCOVERY ACCELEROMETER Private Variables
00078   * @{
00079   */ 
00080 static ACCELERO_DrvTypeDef *AcceleroDrv;
00081 /**
00082   * @}
00083   */
00084 
00085 /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER_Private_FunctionPrototypes STM32F4 DISCOVERY ACCELEROMETER Private FunctionPrototypes
00086   * @{
00087   */
00088 /**
00089   * @}
00090   */
00091 
00092 /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER_Private_Functions STM32F4 DISCOVERY ACCELEROMETER Private Functions
00093   * @{
00094   */
00095 
00096 /**
00097   * @brief  Setx Accelerometer Initialization.
00098   * @retval ACCELERO_OK if no problem during initialization
00099   */
00100 uint8_t BSP_ACCELERO_Init(void)
00101 { 
00102   uint8_t ret = ACCELERO_ERROR;
00103   uint16_t ctrl = 0x0000;
00104   LIS302DL_InitTypeDef         lis302dl_initstruct;
00105   LIS302DL_FilterConfigTypeDef lis302dl_filter = {0,0,0};
00106   LIS3DSH_InitTypeDef          l1s3dsh_InitStruct;
00107 
00108   if(Lis302dlDrv.ReadID() == I_AM_LIS302DL)
00109   {
00110     /* Initialize the accelerometer driver structure */
00111     AcceleroDrv = &Lis302dlDrv;
00112 
00113     /* Set configuration of LIS302DL MEMS Accelerometer *********************/
00114     lis302dl_initstruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
00115     lis302dl_initstruct.Output_DataRate = LIS302DL_DATARATE_100;
00116     lis302dl_initstruct.Axes_Enable = LIS302DL_XYZ_ENABLE;
00117     lis302dl_initstruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
00118     lis302dl_initstruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
00119     
00120     /* Configure MEMS: data rate, power mode, full scale, self test and axes */
00121     ctrl = (uint16_t) (lis302dl_initstruct.Output_DataRate | lis302dl_initstruct.Power_Mode | \
00122                        lis302dl_initstruct.Full_Scale | lis302dl_initstruct.Self_Test | \
00123                        lis302dl_initstruct.Axes_Enable);
00124     
00125     /* Configure the accelerometer main parameters */
00126     AcceleroDrv->Init(ctrl);
00127     
00128     /* MEMS High Pass Filter configuration */
00129     lis302dl_filter.HighPassFilter_Data_Selection = LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER;
00130     lis302dl_filter.HighPassFilter_CutOff_Frequency = LIS302DL_HIGHPASSFILTER_LEVEL_1;
00131     lis302dl_filter.HighPassFilter_Interrupt = LIS302DL_HIGHPASSFILTERINTERRUPT_1_2;
00132     
00133     /* Configure MEMS high pass filter cut-off level, interrupt and data selection bits */                     
00134     ctrl = (uint8_t)(lis302dl_filter.HighPassFilter_Data_Selection | \
00135                      lis302dl_filter.HighPassFilter_CutOff_Frequency | \
00136                      lis302dl_filter.HighPassFilter_Interrupt);
00137 
00138     /* Configure the accelerometer LPF main parameters */
00139     AcceleroDrv->FilterConfig(ctrl);
00140 
00141     ret = ACCELERO_OK;
00142   }
00143   else if(Lis3dshDrv.ReadID() == I_AM_LIS3DSH)
00144   {
00145     /* Initialize the accelerometer driver structure */
00146     AcceleroDrv = &Lis3dshDrv;
00147 
00148     /* Set configuration of LIS3DSH MEMS Accelerometer **********************/
00149     l1s3dsh_InitStruct.Output_DataRate = LIS3DSH_DATARATE_100;
00150     l1s3dsh_InitStruct.Axes_Enable = LIS3DSH_XYZ_ENABLE;
00151     l1s3dsh_InitStruct.SPI_Wire = LIS3DSH_SERIALINTERFACE_4WIRE;
00152     l1s3dsh_InitStruct.Self_Test = LIS3DSH_SELFTEST_NORMAL;
00153     l1s3dsh_InitStruct.Full_Scale = LIS3DSH_FULLSCALE_2;
00154     l1s3dsh_InitStruct.Filter_BW = LIS3DSH_FILTER_BW_800;
00155     
00156     /* Configure MEMS: power mode(ODR) and axes enable */
00157     ctrl = (uint16_t) (l1s3dsh_InitStruct.Output_DataRate | \
00158                        l1s3dsh_InitStruct.Axes_Enable);
00159     
00160     /* Configure MEMS: full scale and self test */
00161     ctrl |= (uint16_t) ((l1s3dsh_InitStruct.SPI_Wire    | \
00162                          l1s3dsh_InitStruct.Self_Test   | \
00163                          l1s3dsh_InitStruct.Full_Scale  | \
00164                          l1s3dsh_InitStruct.Filter_BW) << 8);
00165 
00166     /* Configure the accelerometer main parameters */
00167     AcceleroDrv->Init(ctrl);
00168     
00169     ret = ACCELERO_OK;
00170   }
00171 
00172   else
00173   {
00174     ret = ACCELERO_ERROR;
00175   }
00176   return ret;
00177 }
00178 
00179 /**
00180   * @brief  Read ID of Accelerometer component.
00181   * @retval ID
00182   */
00183 uint8_t BSP_ACCELERO_ReadID(void)
00184 {
00185   uint8_t id = 0x00;
00186 
00187   if(AcceleroDrv->ReadID != NULL)
00188   {
00189     id = AcceleroDrv->ReadID();
00190   }  
00191   return id;
00192 }
00193 
00194 /**
00195   * @brief  Reboot memory content of Accelerometer.
00196   */
00197 void BSP_ACCELERO_Reset(void)
00198 {
00199   if(AcceleroDrv->Reset != NULL)
00200   {
00201     AcceleroDrv->Reset();
00202   }
00203 }
00204 
00205 /**
00206   * @brief  Configure Accelerometer click IT. 
00207   */
00208 void BSP_ACCELERO_Click_ITConfig(void)
00209 {
00210   if(AcceleroDrv->ConfigIT != NULL)
00211   {
00212     AcceleroDrv->ConfigIT();
00213   }
00214 }
00215 
00216 /**
00217   * @brief  Clear Accelerometer click IT.
00218   */
00219 void BSP_ACCELERO_Click_ITClear(void)
00220 {
00221   if(AcceleroDrv->ClearIT != NULL)
00222   {
00223     AcceleroDrv->ClearIT();
00224   }
00225 }
00226 
00227 /**
00228   * @brief  Get XYZ axes acceleration.
00229   * @param  pDataXYZ: Pointer to 3 angular acceleration axes.  
00230   *                   pDataXYZ[0] = X axis, pDataXYZ[1] = Y axis, pDataXYZ[2] = Z axis
00231   */
00232 void BSP_ACCELERO_GetXYZ(int16_t *pDataXYZ)
00233 {
00234   int16_t SwitchXY = 0;
00235   
00236   if(AcceleroDrv->GetXYZ != NULL)
00237   {   
00238     AcceleroDrv->GetXYZ(pDataXYZ);
00239     
00240     /* Switch X and Y Axes in case of LIS302DL MEMS */
00241     if(AcceleroDrv == &Lis302dlDrv)
00242     { 
00243       SwitchXY  = pDataXYZ[0];
00244       pDataXYZ[0] = pDataXYZ[1];
00245       /* Invert Y Axis to be compliant with LIS3DSH MEMS */
00246       pDataXYZ[1] = -SwitchXY;
00247     } 
00248   }
00249 }
00250 
00251 /**
00252   * @}
00253   */ 
00254 
00255 /**
00256   * @}
00257   */ 
00258   
00259 /**
00260   * @}
00261   */ 
00262 
00263 /**
00264   * @}
00265   */ 
00266   
00267 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Thu Jan 19 2017 15:34:14 for STM32F4-Discovery BSP User Manual by   doxygen 1.7.6.1