stm32g4xx_ll_opamp.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_ll_opamp.c
  4. * @author MCD Application Team
  5. * @brief OPAMP LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2019 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 "stm32g4xx_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 STM32G4xx_LL_Driver
  27. * @{
  28. */
  29. #if defined (OPAMP1) || defined (OPAMP2) || defined (OPAMP3) || defined (OPAMP4) || defined (OPAMP5) || defined (OPAMP6)
  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_NORMALSPEED) \
  44. || ((__POWER_MODE__) == LL_OPAMP_POWERMODE_HIGHSPEED))
  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. || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA_IO0) \
  50. || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA_IO0_BIAS) \
  51. || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA_IO0_IO1_BIAS) \
  52. )
  53. #define IS_LL_OPAMP_INPUT_NONINVERTING(__INPUT_NONINVERTING__) \
  54. ( ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  55. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO1) \
  56. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO2) \
  57. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO3) \
  58. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_DAC) \
  59. )
  60. #define IS_LL_OPAMP_INPUT_INVERTING(__INPUT_INVERTING__) \
  61. ( ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO0) \
  62. || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO1) \
  63. || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_CONNECT_NO) \
  64. )
  65. /**
  66. * @}
  67. */
  68. /* Private function prototypes -----------------------------------------------*/
  69. /* Exported functions --------------------------------------------------------*/
  70. /** @addtogroup OPAMP_LL_Exported_Functions
  71. * @{
  72. */
  73. /** @addtogroup OPAMP_LL_EF_Init
  74. * @{
  75. */
  76. /**
  77. * @brief De-initialize registers of the selected OPAMP instance
  78. * to their default reset values.
  79. * @note If comparator is locked, de-initialization by software is
  80. * not possible.
  81. * The only way to unlock the comparator is a device hardware reset.
  82. * @param OPAMPx OPAMP instance
  83. * @retval An ErrorStatus enumeration value:
  84. * - SUCCESS: OPAMP registers are de-initialized
  85. * - ERROR: OPAMP registers are not de-initialized
  86. */
  87. ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef *OPAMPx)
  88. {
  89. ErrorStatus status = SUCCESS;
  90. /* Check the parameters */
  91. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  92. /* Note: Hardware constraint (refer to description of this function): */
  93. /* OPAMP instance must not be locked. */
  94. if (LL_OPAMP_IsLocked(OPAMPx) == 0UL)
  95. {
  96. LL_OPAMP_WriteReg(OPAMPx, CSR, 0x00000000UL);
  97. }
  98. else
  99. {
  100. /* OPAMP instance is locked: de-initialization by software is */
  101. /* not possible. */
  102. /* The only way to unlock the OPAMP is a device hardware reset. */
  103. status = ERROR;
  104. }
  105. /* Timer controlled mux mode register reset */
  106. if (LL_OPAMP_IsTimerMuxLocked(OPAMPx) == 0UL)
  107. {
  108. LL_OPAMP_WriteReg(OPAMPx, TCMR, 0x00000000UL);
  109. }
  110. else if (LL_OPAMP_ReadReg(OPAMPx, TCMR) != 0x80000000UL)
  111. {
  112. /* OPAMP instance timer controlled mux is locked configured, deinit error */
  113. /* The only way to unlock the OPAMP is a device hardware reset. */
  114. status = ERROR;
  115. }
  116. else
  117. {
  118. /* OPAMP instance timer controlled mux is locked unconfigured, deinit OK */
  119. }
  120. return status;
  121. }
  122. /**
  123. * @brief Initialize some features of OPAMP instance.
  124. * @note This function reset bit of calibration mode to ensure
  125. * to be in functional mode, in order to have OPAMP parameters
  126. * (inputs selection, ...) set with the corresponding OPAMP mode
  127. * to be effective.
  128. * @param OPAMPx OPAMP instance
  129. * @param OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
  130. * @retval An ErrorStatus enumeration value:
  131. * - SUCCESS: OPAMP registers are initialized
  132. * - ERROR: OPAMP registers are not initialized
  133. */
  134. ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  135. {
  136. ErrorStatus status = SUCCESS;
  137. /* Check the parameters */
  138. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  139. assert_param(IS_LL_OPAMP_POWER_MODE(OPAMP_InitStruct->PowerMode));
  140. assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
  141. assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMP_InitStruct->InputNonInverting));
  142. /* Note: OPAMP inverting input can be used with OPAMP in mode standalone */
  143. /* or PGA with external capacitors for filtering circuit. */
  144. /* Otherwise (OPAMP in mode follower), OPAMP inverting input is */
  145. /* not used (not connected to GPIO pin). */
  146. if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  147. {
  148. assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMP_InitStruct->InputInverting));
  149. }
  150. /* Note: Hardware constraint (refer to description of this function): */
  151. /* OPAMP instance must not be locked. */
  152. if (LL_OPAMP_IsLocked(OPAMPx) == 0U)
  153. {
  154. /* Configuration of OPAMP instance : */
  155. /* - PowerMode */
  156. /* - Functional mode */
  157. /* - Input non-inverting */
  158. /* - Input inverting */
  159. /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode. */
  160. if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  161. {
  162. MODIFY_REG(OPAMPx->CSR,
  163. OPAMP_CSR_HIGHSPEEDEN
  164. | OPAMP_CSR_CALON
  165. | OPAMP_CSR_VMSEL
  166. | OPAMP_CSR_VPSEL
  167. | OPAMP_CSR_PGGAIN_4 | OPAMP_CSR_PGGAIN_3
  168. ,
  169. OPAMP_InitStruct->PowerMode
  170. | OPAMP_InitStruct->FunctionalMode
  171. | OPAMP_InitStruct->InputNonInverting
  172. | OPAMP_InitStruct->InputInverting
  173. );
  174. }
  175. else
  176. {
  177. MODIFY_REG(OPAMPx->CSR,
  178. OPAMP_CSR_HIGHSPEEDEN
  179. | OPAMP_CSR_CALON
  180. | OPAMP_CSR_VMSEL
  181. | OPAMP_CSR_VPSEL
  182. | OPAMP_CSR_PGGAIN_4 | OPAMP_CSR_PGGAIN_3
  183. ,
  184. OPAMP_InitStruct->PowerMode
  185. | LL_OPAMP_MODE_FOLLOWER
  186. | OPAMP_InitStruct->InputNonInverting
  187. );
  188. }
  189. }
  190. else
  191. {
  192. /* Initialization error: OPAMP instance is locked. */
  193. status = ERROR;
  194. }
  195. return status;
  196. }
  197. /**
  198. * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
  199. * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
  200. * whose fields will be set to default values.
  201. * @retval None
  202. */
  203. void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  204. {
  205. /* Set OPAMP_InitStruct fields to default values */
  206. OPAMP_InitStruct->PowerMode = LL_OPAMP_POWERMODE_NORMALSPEED;
  207. OPAMP_InitStruct->FunctionalMode = LL_OPAMP_MODE_FOLLOWER;
  208. OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
  209. /* Note: Parameter discarded if OPAMP in functional mode follower, */
  210. /* set anyway to its default value. */
  211. OPAMP_InitStruct->InputInverting = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
  212. }
  213. /**
  214. * @}
  215. */
  216. /**
  217. * @}
  218. */
  219. /**
  220. * @}
  221. */
  222. #endif /* OPAMP1 || OPAMP2 || OPAMP3 || OPAMP4 || OPAMP5 || OPAMP6 */
  223. /**
  224. * @}
  225. */
  226. #endif /* USE_FULL_LL_DRIVER */