stm32f3xx_ll_opamp.c 8.4 KB

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