stm32l4xx_ll_opamp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_ll_opamp.c
  4. * @author MCD Application Team
  5. * @brief OPAMP LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2017 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 "stm32l4xx_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 STM32L4xx_LL_Driver
  27. * @{
  28. */
  29. #if defined (OPAMP1) || defined (OPAMP2)
  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_POWER_MODE(__POWER_MODE__) \
  43. ( ((__POWER_MODE__) == LL_OPAMP_POWERMODE_NORMALPOWER) \
  44. || ((__POWER_MODE__) == LL_OPAMP_POWERMODE_LOWPOWER))
  45. #define IS_LL_OPAMP_FUNCTIONAL_MODE(__FUNCTIONAL_MODE__) \
  46. ( ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_STANDALONE) \
  47. || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_FOLLOWER) \
  48. || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA) \
  49. )
  50. /* Note: Comparator non-inverting inputs parameters are the same on all */
  51. /* OPAMP instances. */
  52. /* However, comparator instance kept as macro parameter for */
  53. /* compatibility with other STM32 families. */
  54. #define IS_LL_OPAMP_INPUT_NONINVERTING(__OPAMPX__, __INPUT_NONINVERTING__) \
  55. ( ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  56. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1) \
  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. * @param OPAMPx OPAMP instance
  82. * @retval An ErrorStatus enumeration value:
  83. * - SUCCESS: OPAMP registers are de-initialized
  84. * - ERROR: OPAMP registers are not de-initialized
  85. */
  86. ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef* OPAMPx)
  87. {
  88. ErrorStatus status = SUCCESS;
  89. /* Check the parameters */
  90. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  91. LL_OPAMP_WriteReg(OPAMPx, CSR, 0x00000000U);
  92. return status;
  93. }
  94. /**
  95. * @brief Initialize some features of OPAMP instance.
  96. * @note This function reset bit of calibration mode to ensure
  97. * to be in functional mode, in order to have OPAMP parameters
  98. * (inputs selection, ...) set with the corresponding OPAMP mode
  99. * to be effective.
  100. * @note This function configures features of the selected OPAMP instance.
  101. * Some features are also available at scope OPAMP common instance
  102. * (common to several OPAMP instances).
  103. * Refer to functions having argument "OPAMPxy_COMMON" as parameter.
  104. * @param OPAMPx OPAMP instance
  105. * @param OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
  106. * @retval An ErrorStatus enumeration value:
  107. * - SUCCESS: OPAMP registers are initialized
  108. * - ERROR: OPAMP registers are not initialized
  109. */
  110. ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  111. {
  112. /* Check the parameters */
  113. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  114. assert_param(IS_LL_OPAMP_POWER_MODE(OPAMP_InitStruct->PowerMode));
  115. assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
  116. assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMPx, OPAMP_InitStruct->InputNonInverting));
  117. /* Note: OPAMP inverting input can be used with OPAMP in mode standalone */
  118. /* or PGA with external capacitors for filtering circuit. */
  119. /* Otherwise (OPAMP in mode follower), OPAMP inverting input is */
  120. /* not used (not connected to GPIO pin). */
  121. if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  122. {
  123. assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMPx, OPAMP_InitStruct->InputInverting));
  124. }
  125. /* Configuration of OPAMP instance : */
  126. /* - PowerMode */
  127. /* - Functional mode */
  128. /* - Input non-inverting */
  129. /* - Input inverting */
  130. /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode. */
  131. if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  132. {
  133. MODIFY_REG(OPAMPx->CSR,
  134. OPAMP_CSR_OPALPM
  135. | OPAMP_CSR_OPAMODE
  136. | OPAMP_CSR_CALON
  137. | OPAMP_CSR_VMSEL
  138. | OPAMP_CSR_VPSEL
  139. ,
  140. (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
  141. | OPAMP_InitStruct->FunctionalMode
  142. | OPAMP_InitStruct->InputNonInverting
  143. | OPAMP_InitStruct->InputInverting
  144. );
  145. }
  146. else
  147. {
  148. MODIFY_REG(OPAMPx->CSR,
  149. OPAMP_CSR_OPALPM
  150. | OPAMP_CSR_OPAMODE
  151. | OPAMP_CSR_CALON
  152. | OPAMP_CSR_VMSEL
  153. | OPAMP_CSR_VPSEL
  154. ,
  155. (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
  156. | LL_OPAMP_MODE_FOLLOWER
  157. | OPAMP_InitStruct->InputNonInverting
  158. | LL_OPAMP_INPUT_INVERT_CONNECT_NO
  159. );
  160. }
  161. return SUCCESS;
  162. }
  163. /**
  164. * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
  165. * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
  166. * whose fields will be set to default values.
  167. * @retval None
  168. */
  169. void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  170. {
  171. /* Set OPAMP_InitStruct fields to default values */
  172. OPAMP_InitStruct->PowerMode = LL_OPAMP_POWERMODE_NORMALPOWER;
  173. OPAMP_InitStruct->FunctionalMode = LL_OPAMP_MODE_FOLLOWER;
  174. OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
  175. /* Note: Parameter discarded if OPAMP in functional mode follower, */
  176. /* set anyway to its default value. */
  177. OPAMP_InitStruct->InputInverting = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
  178. }
  179. /**
  180. * @}
  181. */
  182. /**
  183. * @}
  184. */
  185. /**
  186. * @}
  187. */
  188. #endif /* OPAMP1 || OPAMP2 */
  189. /**
  190. * @}
  191. */
  192. #endif /* USE_FULL_LL_DRIVER */