1
0

stm32l4xx_ll_opamp.c 9.6 KB

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