You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_ll_opamp.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. #if defined(USE_FULL_LL_DRIVER)
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32l1xx_ll_opamp.h"
  22. #ifdef USE_FULL_ASSERT
  23. #include "stm32_assert.h"
  24. #else
  25. #define assert_param(expr) ((void)0U)
  26. #endif
  27. /** @addtogroup STM32L1xx_LL_Driver
  28. * @{
  29. */
  30. #if defined (OPAMP1) || defined (OPAMP2) || defined (OPAMP3)
  31. /** @addtogroup OPAMP_LL OPAMP
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /* Private macros ------------------------------------------------------------*/
  38. /** @addtogroup OPAMP_LL_Private_Macros
  39. * @{
  40. */
  41. /* Check of parameters for configuration of OPAMP hierarchical scope: */
  42. /* OPAMP instance. */
  43. #define IS_LL_OPAMP_POWER_MODE(__POWER_MODE__) \
  44. ( ((__POWER_MODE__) == LL_OPAMP_POWERMODE_NORMAL) \
  45. || ((__POWER_MODE__) == LL_OPAMP_POWERMODE_LOWPOWER))
  46. #define IS_LL_OPAMP_FUNCTIONAL_MODE(__FUNCTIONAL_MODE__) \
  47. ( ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_STANDALONE) \
  48. || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_FOLLOWER) \
  49. )
  50. /* Note: Comparator non-inverting inputs parameters are different */
  51. /* depending on OPAMP instance. */
  52. #if defined(OPAMP3)
  53. #define IS_LL_OPAMP_INPUT_NONINVERTING(__OPAMPX__, __INPUT_NONINVERTING__) \
  54. (((__OPAMPX__) == OPAMP1) \
  55. ? ( \
  56. ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  57. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1) \
  58. ) \
  59. : \
  60. (((__OPAMPX__) == OPAMP2) \
  61. ? ( \
  62. ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  63. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1) \
  64. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH2) \
  65. ) \
  66. : \
  67. ( \
  68. ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  69. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH2_OPAMP3) \
  70. ) \
  71. ) \
  72. )
  73. #else
  74. #define IS_LL_OPAMP_INPUT_NONINVERTING(__OPAMPX__, __INPUT_NONINVERTING__) \
  75. (((__OPAMPX__) == OPAMP1) \
  76. ? ( \
  77. ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  78. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1) \
  79. ) \
  80. : \
  81. ( \
  82. ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  83. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1) \
  84. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH2) \
  85. ) \
  86. )
  87. #endif
  88. /* Note: Comparator non-inverting inputs parameters are the same on all */
  89. /* OPAMP instances. */
  90. /* However, comparator instance kept as macro parameter for */
  91. /* compatibility with other STM32 families. */
  92. #define IS_LL_OPAMP_INPUT_INVERTING(__OPAMPX__, __INPUT_INVERTING__) \
  93. ( ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO0) \
  94. || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO1) \
  95. || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_CONNECT_NO) \
  96. )
  97. /**
  98. * @}
  99. */
  100. /* Private function prototypes -----------------------------------------------*/
  101. /* Exported functions --------------------------------------------------------*/
  102. /** @addtogroup OPAMP_LL_Exported_Functions
  103. * @{
  104. */
  105. /** @addtogroup OPAMP_LL_EF_Init
  106. * @{
  107. */
  108. /**
  109. * @brief De-initialize registers of the selected OPAMP instance
  110. * to their default reset values.
  111. * @param OPAMPx OPAMP instance
  112. * @retval An ErrorStatus enumeration value:
  113. * - SUCCESS: OPAMP registers are de-initialized
  114. * - ERROR: OPAMP registers are not de-initialized
  115. */
  116. ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef* OPAMPx)
  117. {
  118. ErrorStatus status = SUCCESS;
  119. /* Check the parameters */
  120. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  121. MODIFY_REG(OPAMP->CSR,
  122. (OPAMP_CSR_S3SEL1 | OPAMP_CSR_S4SEL1 | OPAMP_CSR_S5SEL1 | OPAMP_CSR_S6SEL1 | OPAMP_CSR_OPA1CAL_L | OPAMP_CSR_OPA1CAL_H | OPAMP_CSR_OPA1LPM) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
  123. | (OPAMP_CSR_ANAWSEL1) << __OPAMP_INSTANCE_DECIMAL(OPAMPx)
  124. | (OPAMP_CSR_S7SEL2 * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx)) ,
  125. (OPAMP_CSR_OPA1PD) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
  126. );
  127. return status;
  128. }
  129. /**
  130. * @brief Initialize some features of OPAMP instance.
  131. * @note This function reset bit of calibration mode to ensure
  132. * to be in functional mode, in order to have OPAMP parameters
  133. * (inputs selection, ...) set with the corresponding OPAMP mode
  134. * to be effective.
  135. * @note This function configures features of the selected OPAMP instance.
  136. * Some features are also available at scope OPAMP common instance
  137. * (common to several OPAMP instances).
  138. * Refer to functions having argument "OPAMPxy_COMMON" as parameter.
  139. * @param OPAMPx OPAMP instance
  140. * @param OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
  141. * @retval An ErrorStatus enumeration value:
  142. * - SUCCESS: OPAMP registers are initialized
  143. * - ERROR: OPAMP registers are not initialized
  144. */
  145. ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  146. {
  147. /* Check the parameters */
  148. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  149. assert_param(IS_LL_OPAMP_POWER_MODE(OPAMP_InitStruct->PowerMode));
  150. assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
  151. assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMPx, OPAMP_InitStruct->InputNonInverting));
  152. /* Note: OPAMP inverting input can be used with OPAMP in mode standalone. */
  153. /* Otherwise (OPAMP in mode follower), OPAMP inverting input is */
  154. /* not used (not connected to GPIO pin). */
  155. if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  156. {
  157. assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMPx, OPAMP_InitStruct->InputInverting));
  158. }
  159. /* Configuration of OPAMP instance : */
  160. /* - PowerMode */
  161. /* - Functional mode */
  162. /* - Input non-inverting */
  163. /* - Input inverting */
  164. /* Note: Bits OPAMP_CSR_OPAxCAL_y reset to ensure to be in functional mode */
  165. if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  166. {
  167. MODIFY_REG(OPAMP->CSR,
  168. ((
  169. OPAMP_CSR_OPA1LPM
  170. | OPAMP_CSR_S3SEL1
  171. | OPAMP_CSR_OPA1CAL_H
  172. | OPAMP_CSR_OPA1CAL_L
  173. | OPAMP_CSR_S5SEL1
  174. | OPAMP_CSR_S6SEL1
  175. | OPAMP_CSR_S4SEL1
  176. ) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
  177. )
  178. | ((OPAMP_CSR_ANAWSEL1) << __OPAMP_INSTANCE_DECIMAL(OPAMPx))
  179. | (OPAMP_CSR_S7SEL2 * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx))
  180. ,
  181. ((
  182. (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
  183. | OPAMP_InitStruct->FunctionalMode
  184. | OPAMP_InitStruct->InputNonInverting
  185. | (OPAMP_InitStruct->InputInverting & OPAMP_CSR_S4SEL1)
  186. ) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
  187. )
  188. | ((OPAMP_InitStruct->InputInverting & OPAMP_CSR_ANAWSEL1) << __OPAMP_INSTANCE_DECIMAL(OPAMPx))
  189. | ((OPAMP_InitStruct->InputNonInverting & OPAMP_CSR_S7SEL2) * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx))
  190. );
  191. }
  192. else
  193. {
  194. MODIFY_REG(OPAMP->CSR,
  195. ((
  196. OPAMP_CSR_OPA1LPM
  197. | OPAMP_CSR_OPA1CAL_H
  198. | OPAMP_CSR_OPA1CAL_L
  199. | OPAMP_CSR_S5SEL1
  200. | OPAMP_CSR_S6SEL1
  201. | OPAMP_CSR_S4SEL1
  202. ) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
  203. )
  204. | ((OPAMP_CSR_ANAWSEL1) << __OPAMP_INSTANCE_DECIMAL(OPAMPx))
  205. | (OPAMP_CSR_S7SEL2 * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx))
  206. ,
  207. ((
  208. (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
  209. | OPAMP_InitStruct->FunctionalMode
  210. | OPAMP_InitStruct->InputNonInverting
  211. | OPAMP_CSR_S3SEL1
  212. ) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
  213. )
  214. | ((OPAMP_InitStruct->InputNonInverting & OPAMP_CSR_S7SEL2) * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx))
  215. );
  216. }
  217. return SUCCESS;
  218. }
  219. /**
  220. * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
  221. * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
  222. * whose fields will be set to default values.
  223. * @retval None
  224. */
  225. void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  226. {
  227. /* Set OPAMP_InitStruct fields to default values */
  228. OPAMP_InitStruct->PowerMode = LL_OPAMP_POWERMODE_NORMAL;
  229. OPAMP_InitStruct->FunctionalMode = LL_OPAMP_MODE_FOLLOWER;
  230. OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
  231. /* Note: Parameter discarded if OPAMP in functional mode follower, */
  232. /* set anyway to its default value. */
  233. OPAMP_InitStruct->InputInverting = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
  234. }
  235. /**
  236. * @}
  237. */
  238. /**
  239. * @}
  240. */
  241. /**
  242. * @}
  243. */
  244. #endif /* OPAMP1 || OPAMP2 || OPAMP3 */
  245. /**
  246. * @}
  247. */
  248. #endif /* USE_FULL_LL_DRIVER */
  249. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/