stm32l4xx_ll_opamp.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_ll_opamp.c
  4. * @author MCD Application Team
  5. * @brief OPAMP LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. #if defined(USE_FULL_LL_DRIVER)
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32l4xx_ll_opamp.h"
  38. #ifdef USE_FULL_ASSERT
  39. #include "stm32_assert.h"
  40. #else
  41. #define assert_param(expr) ((void)0U)
  42. #endif
  43. /** @addtogroup STM32L4xx_LL_Driver
  44. * @{
  45. */
  46. #if defined (OPAMP1) || defined (OPAMP2)
  47. /** @addtogroup OPAMP_LL OPAMP
  48. * @{
  49. */
  50. /* Private types -------------------------------------------------------------*/
  51. /* Private variables ---------------------------------------------------------*/
  52. /* Private constants ---------------------------------------------------------*/
  53. /* Private macros ------------------------------------------------------------*/
  54. /** @addtogroup OPAMP_LL_Private_Macros
  55. * @{
  56. */
  57. /* Check of parameters for configuration of OPAMP hierarchical scope: */
  58. /* OPAMP instance. */
  59. #define IS_LL_OPAMP_POWER_MODE(__POWER_MODE__) \
  60. ( ((__POWER_MODE__) == LL_OPAMP_POWERMODE_NORMAL) \
  61. || ((__POWER_MODE__) == LL_OPAMP_POWERMODE_LOWPOWER))
  62. #define IS_LL_OPAMP_FUNCTIONAL_MODE(__FUNCTIONAL_MODE__) \
  63. ( ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_STANDALONE) \
  64. || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_FOLLOWER) \
  65. || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA) \
  66. )
  67. /* Note: Comparator non-inverting inputs parameters are the same on all */
  68. /* OPAMP instances. */
  69. /* However, comparator instance kept as macro parameter for */
  70. /* compatibility with other STM32 families. */
  71. #define IS_LL_OPAMP_INPUT_NONINVERTING(__OPAMPX__, __INPUT_NONINVERTING__) \
  72. ( ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  73. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1) \
  74. )
  75. /* Note: Comparator non-inverting inputs parameters are the same on all */
  76. /* OPAMP instances. */
  77. /* However, comparator instance kept as macro parameter for */
  78. /* compatibility with other STM32 families. */
  79. #define IS_LL_OPAMP_INPUT_INVERTING(__OPAMPX__, __INPUT_INVERTING__) \
  80. ( ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO0) \
  81. || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO1) \
  82. || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_CONNECT_NO) \
  83. )
  84. /**
  85. * @}
  86. */
  87. /* Private function prototypes -----------------------------------------------*/
  88. /* Exported functions --------------------------------------------------------*/
  89. /** @addtogroup OPAMP_LL_Exported_Functions
  90. * @{
  91. */
  92. /** @addtogroup OPAMP_LL_EF_Init
  93. * @{
  94. */
  95. /**
  96. * @brief De-initialize registers of the selected OPAMP instance
  97. * to their default reset values.
  98. * @param OPAMPx OPAMP instance
  99. * @retval An ErrorStatus enumeration value:
  100. * - SUCCESS: OPAMP registers are de-initialized
  101. * - ERROR: OPAMP registers are not de-initialized
  102. */
  103. ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef* OPAMPx)
  104. {
  105. ErrorStatus status = SUCCESS;
  106. /* Check the parameters */
  107. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  108. LL_OPAMP_WriteReg(OPAMPx, CSR, 0x00000000U);
  109. return status;
  110. }
  111. /**
  112. * @brief Initialize some features of OPAMP instance.
  113. * @note This function reset bit of calibration mode to ensure
  114. * to be in functional mode, in order to have OPAMP parameters
  115. * (inputs selection, ...) set with the corresponding OPAMP mode
  116. * to be effective.
  117. * @note This function configures features of the selected OPAMP instance.
  118. * Some features are also available at scope OPAMP common instance
  119. * (common to several OPAMP instances).
  120. * Refer to functions having argument "OPAMPxy_COMMON" as parameter.
  121. * @param OPAMPx OPAMP instance
  122. * @param OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
  123. * @retval An ErrorStatus enumeration value:
  124. * - SUCCESS: OPAMP registers are initialized
  125. * - ERROR: OPAMP registers are not initialized
  126. */
  127. ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  128. {
  129. /* Check the parameters */
  130. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  131. assert_param(IS_LL_OPAMP_POWER_MODE(OPAMP_InitStruct->PowerMode));
  132. assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
  133. assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMPx, OPAMP_InitStruct->InputNonInverting));
  134. /* Note: OPAMP inverting input can be used with OPAMP in mode standalone */
  135. /* or PGA with external capacitors for filtering circuit. */
  136. /* Otherwise (OPAMP in mode follower), OPAMP inverting input is */
  137. /* not used (not connected to GPIO pin). */
  138. if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  139. {
  140. assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMPx, OPAMP_InitStruct->InputInverting));
  141. }
  142. /* Configuration of OPAMP instance : */
  143. /* - PowerMode */
  144. /* - Functional mode */
  145. /* - Input non-inverting */
  146. /* - Input inverting */
  147. /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode. */
  148. if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  149. {
  150. MODIFY_REG(OPAMPx->CSR,
  151. OPAMP_CSR_OPALPM
  152. | OPAMP_CSR_OPAMODE
  153. | OPAMP_CSR_CALON
  154. | OPAMP_CSR_VMSEL
  155. | OPAMP_CSR_VPSEL
  156. ,
  157. (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
  158. | OPAMP_InitStruct->FunctionalMode
  159. | OPAMP_InitStruct->InputNonInverting
  160. | OPAMP_InitStruct->InputInverting
  161. );
  162. }
  163. else
  164. {
  165. MODIFY_REG(OPAMPx->CSR,
  166. OPAMP_CSR_OPALPM
  167. | OPAMP_CSR_OPAMODE
  168. | OPAMP_CSR_CALON
  169. | OPAMP_CSR_VMSEL
  170. | OPAMP_CSR_VPSEL
  171. ,
  172. (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
  173. | LL_OPAMP_MODE_FOLLOWER
  174. | OPAMP_InitStruct->InputNonInverting
  175. | LL_OPAMP_INPUT_INVERT_CONNECT_NO
  176. );
  177. }
  178. return SUCCESS;
  179. }
  180. /**
  181. * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
  182. * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
  183. * whose fields will be set to default values.
  184. * @retval None
  185. */
  186. void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  187. {
  188. /* Set OPAMP_InitStruct fields to default values */
  189. OPAMP_InitStruct->PowerMode = LL_OPAMP_POWERMODE_NORMAL;
  190. OPAMP_InitStruct->FunctionalMode = LL_OPAMP_MODE_FOLLOWER;
  191. OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
  192. /* Note: Parameter discarded if OPAMP in functional mode follower, */
  193. /* set anyway to its default value. */
  194. OPAMP_InitStruct->InputInverting = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
  195. }
  196. /**
  197. * @}
  198. */
  199. /**
  200. * @}
  201. */
  202. /**
  203. * @}
  204. */
  205. #endif /* OPAMP1 || OPAMP2 */
  206. /**
  207. * @}
  208. */
  209. #endif /* USE_FULL_LL_DRIVER */
  210. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/