/system/Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_ll_opamp.c

https://github.com/stm32duino/Arduino_Core_STM32 · C · 238 lines · 88 code · 33 blank · 117 comment · 28 complexity · e6b5e28f72c10c8dd7ae7228d3ef2cce MD5 · raw file

  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_ll_opamp.c
  4. * @author MCD Application Team
  5. * @brief OPAMP LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. #if defined(USE_FULL_LL_DRIVER)
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f3xx_ll_opamp.h"
  22. #ifdef USE_FULL_ASSERT
  23. #include "stm32_assert.h"
  24. #else
  25. #define assert_param(expr) ((void)0U)
  26. #endif
  27. /** @addtogroup STM32F3xx_LL_Driver
  28. * @{
  29. */
  30. #if defined (OPAMP1) || defined (OPAMP2) || defined (OPAMP3) || defined (OPAMP4)
  31. /** @addtogroup OPAMP_LL OPAMP
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /* Private macros ------------------------------------------------------------*/
  38. /** @addtogroup OPAMP_LL_Private_Macros
  39. * @{
  40. */
  41. /* Check of parameters for configuration of OPAMP hierarchical scope: */
  42. /* OPAMP instance. */
  43. #define IS_LL_OPAMP_FUNCTIONAL_MODE(__FUNCTIONAL_MODE__) \
  44. ( ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_STANDALONE) \
  45. || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_FOLLOWER) \
  46. || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA) \
  47. )
  48. /* Note: Comparator non-inverting inputs parameters are the same on all */
  49. /* OPAMP instances. */
  50. /* However, comparator instance kept as macro parameter for */
  51. /* compatibility with other STM32 families. */
  52. #define IS_LL_OPAMP_INPUT_NONINVERTING(__OPAMPX__, __INPUT_NONINVERTING__) \
  53. ( ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  54. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO1) \
  55. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO2) \
  56. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO3) \
  57. )
  58. /* Note: Comparator non-inverting inputs parameters are the same on all */
  59. /* OPAMP instances. */
  60. /* However, comparator instance kept as macro parameter for */
  61. /* compatibility with other STM32 families. */
  62. #define IS_LL_OPAMP_INPUT_INVERTING(__OPAMPX__, __INPUT_INVERTING__) \
  63. ( ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO0) \
  64. || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO1) \
  65. || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_CONNECT_NO) \
  66. )
  67. /**
  68. * @}
  69. */
  70. /* Private function prototypes -----------------------------------------------*/
  71. /* Exported functions --------------------------------------------------------*/
  72. /** @addtogroup OPAMP_LL_Exported_Functions
  73. * @{
  74. */
  75. /** @addtogroup OPAMP_LL_EF_Init
  76. * @{
  77. */
  78. /**
  79. * @brief De-initialize registers of the selected OPAMP instance
  80. * to their default reset values.
  81. * @note If comparator is locked, de-initialization by software is
  82. * not possible.
  83. * The only way to unlock the comparator is a device hardware reset.
  84. * @param OPAMPx OPAMP instance
  85. * @retval An ErrorStatus enumeration value:
  86. * - SUCCESS: OPAMP registers are de-initialized
  87. * - ERROR: OPAMP registers are not de-initialized
  88. */
  89. ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef* OPAMPx)
  90. {
  91. ErrorStatus status = SUCCESS;
  92. /* Check the parameters */
  93. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  94. /* Note: Hardware constraint (refer to description of this function): */
  95. /* OPAMP instance must not be locked. */
  96. if (LL_OPAMP_IsLocked(OPAMPx) == 0U)
  97. {
  98. LL_OPAMP_WriteReg(OPAMPx, CSR, 0x00000000U);
  99. }
  100. else
  101. {
  102. /* OPAMP instance is locked: de-initialization by software is */
  103. /* not possible. */
  104. /* The only way to unlock the OPAMP is a device hardware reset. */
  105. status = ERROR;
  106. }
  107. return status;
  108. }
  109. /**
  110. * @brief Initialize some features of OPAMP instance.
  111. * @note This function reset bit of calibration mode to ensure
  112. * to be in functional mode, in order to have OPAMP parameters
  113. * (inputs selection, ...) set with the corresponding OPAMP mode
  114. * to be effective.
  115. * @param OPAMPx OPAMP instance
  116. * @param OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
  117. * @retval An ErrorStatus enumeration value:
  118. * - SUCCESS: OPAMP registers are initialized
  119. * - ERROR: OPAMP registers are not initialized
  120. */
  121. ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  122. {
  123. ErrorStatus status = SUCCESS;
  124. /* Check the parameters */
  125. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  126. assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
  127. assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMPx, OPAMP_InitStruct->InputNonInverting));
  128. /* Note: OPAMP inverting input can be used with OPAMP in mode standalone */
  129. /* or PGA with external capacitors for filtering circuit. */
  130. /* Otherwise (OPAMP in mode follower), OPAMP inverting input is */
  131. /* not used (not connected to GPIO pin). */
  132. if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  133. {
  134. assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMPx, OPAMP_InitStruct->InputInverting));
  135. }
  136. /* Note: Hardware constraint (refer to description of this function): */
  137. /* OPAMP instance must not be locked. */
  138. if (LL_OPAMP_IsLocked(OPAMPx) == 0U)
  139. {
  140. /* Configuration of OPAMP instance : */
  141. /* - Functional mode */
  142. /* - Input non-inverting */
  143. /* - Input inverting */
  144. /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode. */
  145. if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  146. {
  147. MODIFY_REG(OPAMPx->CSR,
  148. OPAMP_CSR_CALON
  149. | OPAMP_CSR_VMSEL
  150. | OPAMP_CSR_VPSEL
  151. ,
  152. OPAMP_InitStruct->FunctionalMode
  153. | OPAMP_InitStruct->InputNonInverting
  154. | OPAMP_InitStruct->InputInverting
  155. );
  156. }
  157. else
  158. {
  159. MODIFY_REG(OPAMPx->CSR,
  160. OPAMP_CSR_CALON
  161. | OPAMP_CSR_VMSEL
  162. | OPAMP_CSR_VPSEL
  163. ,
  164. LL_OPAMP_MODE_FOLLOWER
  165. | OPAMP_InitStruct->InputNonInverting
  166. );
  167. }
  168. }
  169. else
  170. {
  171. /* Initialization error: OPAMP instance is locked. */
  172. status = ERROR;
  173. }
  174. return status;
  175. }
  176. /**
  177. * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
  178. * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
  179. * whose fields will be set to default values.
  180. * @retval None
  181. */
  182. void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  183. {
  184. /* Set OPAMP_InitStruct fields to default values */
  185. OPAMP_InitStruct->FunctionalMode = LL_OPAMP_MODE_FOLLOWER;
  186. OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
  187. /* Note: Parameter discarded if OPAMP in functional mode follower, */
  188. /* set anyway to its default value. */
  189. OPAMP_InitStruct->InputInverting = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
  190. }
  191. /**
  192. * @}
  193. */
  194. /**
  195. * @}
  196. */
  197. /**
  198. * @}
  199. */
  200. #endif /* OPAMP1 || OPAMP2 || OPAMP3 || OPAMP4 */
  201. /**
  202. * @}
  203. */
  204. #endif /* USE_FULL_LL_DRIVER */
  205. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/