stm32f4_discovery_accelerometer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4_discovery_accelerometer.c
  4. * @author MCD Application Team
  5. * @brief This file provides a set of functions needed to manage the
  6. * MEMS accelerometers available on STM32F4-Discovery Kit.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2017 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32f4_discovery_accelerometer.h"
  21. /** @addtogroup BSP
  22. * @{
  23. */
  24. /** @addtogroup STM32F4_DISCOVERY
  25. * @{
  26. */
  27. /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER STM32F4 DISCOVERY ACCELEROMETER
  28. * @brief This file includes the motion sensor driver for ACCELEROMETER motion sensor
  29. * devices.
  30. * @{
  31. */
  32. /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER_Private_TypesDefinitions STM32F4 DISCOVERY ACCELEROMETER Private TypesDefinitions
  33. * @{
  34. */
  35. /**
  36. * @}
  37. */
  38. /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER_Private_Defines STM32F4 DISCOVERY ACCELEROMETER Private Defines
  39. * @{
  40. */
  41. /**
  42. * @}
  43. */
  44. /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER_Private_Macros STM32F4 DISCOVERY ACCELEROMETER Private Macros
  45. * @{
  46. */
  47. /**
  48. * @}
  49. */
  50. /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER_Private_Variables STM32F4 DISCOVERY ACCELEROMETER Private Variables
  51. * @{
  52. */
  53. static ACCELERO_DrvTypeDef *AcceleroDrv;
  54. /**
  55. * @}
  56. */
  57. /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER_Private_FunctionPrototypes STM32F4 DISCOVERY ACCELEROMETER Private FunctionPrototypes
  58. * @{
  59. */
  60. /**
  61. * @}
  62. */
  63. /** @defgroup STM32F4_DISCOVERY_ACCELEROMETER_Private_Functions STM32F4 DISCOVERY ACCELEROMETER Private Functions
  64. * @{
  65. */
  66. /**
  67. * @brief Setx Accelerometer Initialization.
  68. * @retval ACCELERO_OK if no problem during initialization
  69. */
  70. uint8_t BSP_ACCELERO_Init(void)
  71. {
  72. uint8_t ret = ACCELERO_ERROR;
  73. uint16_t ctrl = 0x0000;
  74. LIS302DL_InitTypeDef lis302dl_initstruct;
  75. LIS302DL_FilterConfigTypeDef lis302dl_filter = {0,0,0};
  76. LIS3DSH_InitTypeDef l1s3dsh_InitStruct;
  77. if(Lis302dlDrv.ReadID() == I_AM_LIS302DL)
  78. {
  79. /* Initialize the accelerometer driver structure */
  80. AcceleroDrv = &Lis302dlDrv;
  81. /* Set configuration of LIS302DL MEMS Accelerometer *********************/
  82. lis302dl_initstruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
  83. lis302dl_initstruct.Output_DataRate = LIS302DL_DATARATE_100;
  84. lis302dl_initstruct.Axes_Enable = LIS302DL_XYZ_ENABLE;
  85. lis302dl_initstruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
  86. lis302dl_initstruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
  87. /* Configure MEMS: data rate, power mode, full scale, self test and axes */
  88. ctrl = (uint16_t) (lis302dl_initstruct.Output_DataRate | lis302dl_initstruct.Power_Mode | \
  89. lis302dl_initstruct.Full_Scale | lis302dl_initstruct.Self_Test | \
  90. lis302dl_initstruct.Axes_Enable);
  91. /* Configure the accelerometer main parameters */
  92. AcceleroDrv->Init(ctrl);
  93. /* MEMS High Pass Filter configuration */
  94. lis302dl_filter.HighPassFilter_Data_Selection = LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER;
  95. lis302dl_filter.HighPassFilter_CutOff_Frequency = LIS302DL_HIGHPASSFILTER_LEVEL_1;
  96. lis302dl_filter.HighPassFilter_Interrupt = LIS302DL_HIGHPASSFILTERINTERRUPT_1_2;
  97. /* Configure MEMS high pass filter cut-off level, interrupt and data selection bits */
  98. ctrl = (uint8_t)(lis302dl_filter.HighPassFilter_Data_Selection | \
  99. lis302dl_filter.HighPassFilter_CutOff_Frequency | \
  100. lis302dl_filter.HighPassFilter_Interrupt);
  101. /* Configure the accelerometer LPF main parameters */
  102. AcceleroDrv->FilterConfig(ctrl);
  103. ret = ACCELERO_OK;
  104. }
  105. else if(Lis3dshDrv.ReadID() == I_AM_LIS3DSH)
  106. {
  107. /* Initialize the accelerometer driver structure */
  108. AcceleroDrv = &Lis3dshDrv;
  109. /* Set configuration of LIS3DSH MEMS Accelerometer **********************/
  110. l1s3dsh_InitStruct.Output_DataRate = LIS3DSH_DATARATE_100;
  111. l1s3dsh_InitStruct.Axes_Enable = LIS3DSH_XYZ_ENABLE;
  112. l1s3dsh_InitStruct.SPI_Wire = LIS3DSH_SERIALINTERFACE_4WIRE;
  113. l1s3dsh_InitStruct.Self_Test = LIS3DSH_SELFTEST_NORMAL;
  114. l1s3dsh_InitStruct.Full_Scale = LIS3DSH_FULLSCALE_2;
  115. l1s3dsh_InitStruct.Filter_BW = LIS3DSH_FILTER_BW_800;
  116. /* Configure MEMS: power mode(ODR) and axes enable */
  117. ctrl = (uint16_t) (l1s3dsh_InitStruct.Output_DataRate | \
  118. l1s3dsh_InitStruct.Axes_Enable);
  119. /* Configure MEMS: full scale and self test */
  120. ctrl |= (uint16_t) ((l1s3dsh_InitStruct.SPI_Wire | \
  121. l1s3dsh_InitStruct.Self_Test | \
  122. l1s3dsh_InitStruct.Full_Scale | \
  123. l1s3dsh_InitStruct.Filter_BW) << 8);
  124. /* Configure the accelerometer main parameters */
  125. AcceleroDrv->Init(ctrl);
  126. ret = ACCELERO_OK;
  127. }
  128. else
  129. {
  130. ret = ACCELERO_ERROR;
  131. }
  132. return ret;
  133. }
  134. /**
  135. * @brief Read ID of Accelerometer component.
  136. * @retval ID
  137. */
  138. uint8_t BSP_ACCELERO_ReadID(void)
  139. {
  140. uint8_t id = 0x00;
  141. if(AcceleroDrv->ReadID != NULL)
  142. {
  143. id = AcceleroDrv->ReadID();
  144. }
  145. return id;
  146. }
  147. /**
  148. * @brief Reboot memory content of Accelerometer.
  149. */
  150. void BSP_ACCELERO_Reset(void)
  151. {
  152. if(AcceleroDrv->Reset != NULL)
  153. {
  154. AcceleroDrv->Reset();
  155. }
  156. }
  157. /**
  158. * @brief Configure Accelerometer click IT.
  159. */
  160. void BSP_ACCELERO_Click_ITConfig(void)
  161. {
  162. if(AcceleroDrv->ConfigIT != NULL)
  163. {
  164. AcceleroDrv->ConfigIT();
  165. }
  166. }
  167. /**
  168. * @brief Clear Accelerometer click IT.
  169. */
  170. void BSP_ACCELERO_Click_ITClear(void)
  171. {
  172. if(AcceleroDrv->ClearIT != NULL)
  173. {
  174. AcceleroDrv->ClearIT();
  175. }
  176. }
  177. /**
  178. * @brief Get XYZ axes acceleration.
  179. * @param pDataXYZ: Pointer to 3 angular acceleration axes.
  180. * pDataXYZ[0] = X axis, pDataXYZ[1] = Y axis, pDataXYZ[2] = Z axis
  181. */
  182. void BSP_ACCELERO_GetXYZ(int16_t *pDataXYZ)
  183. {
  184. int16_t SwitchXY = 0;
  185. if(AcceleroDrv->GetXYZ != NULL)
  186. {
  187. AcceleroDrv->GetXYZ(pDataXYZ);
  188. /* Switch X and Y Axes in case of LIS302DL MEMS */
  189. if(AcceleroDrv == &Lis302dlDrv)
  190. {
  191. SwitchXY = pDataXYZ[0];
  192. pDataXYZ[0] = pDataXYZ[1];
  193. /* Invert Y Axis to be compliant with LIS3DSH MEMS */
  194. pDataXYZ[1] = -SwitchXY;
  195. }
  196. }
  197. }
  198. /**
  199. * @}
  200. */
  201. /**
  202. * @}
  203. */
  204. /**
  205. * @}
  206. */
  207. /**
  208. * @}
  209. */