stm32l1xx_ll_opamp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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</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 "stm32l1xx_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 STM32L1xx_LL_Driver
  44. * @{
  45. */
  46. #if defined (OPAMP1) || defined (OPAMP2) || defined (OPAMP3)
  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. )
  66. /* Note: Comparator non-inverting inputs parameters are different */
  67. /* depending on OPAMP instance. */
  68. #if defined(OPAMP3)
  69. #define IS_LL_OPAMP_INPUT_NONINVERTING(__OPAMPX__, __INPUT_NONINVERTING__) \
  70. (((__OPAMPX__) == OPAMP1) \
  71. ? ( \
  72. ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  73. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1) \
  74. ) \
  75. : \
  76. (((__OPAMPX__) == OPAMP2) \
  77. ? ( \
  78. ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  79. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1) \
  80. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH2) \
  81. ) \
  82. : \
  83. ( \
  84. ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  85. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH2_OPAMP3) \
  86. ) \
  87. ) \
  88. )
  89. #else
  90. #define IS_LL_OPAMP_INPUT_NONINVERTING(__OPAMPX__, __INPUT_NONINVERTING__) \
  91. (((__OPAMPX__) == OPAMP1) \
  92. ? ( \
  93. ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  94. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1) \
  95. ) \
  96. : \
  97. ( \
  98. ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  99. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1) \
  100. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH2) \
  101. ) \
  102. )
  103. #endif
  104. /* Note: Comparator non-inverting inputs parameters are the same on all */
  105. /* OPAMP instances. */
  106. /* However, comparator instance kept as macro parameter for */
  107. /* compatibility with other STM32 families. */
  108. #define IS_LL_OPAMP_INPUT_INVERTING(__OPAMPX__, __INPUT_INVERTING__) \
  109. ( ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO0) \
  110. || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO1) \
  111. || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_CONNECT_NO) \
  112. )
  113. /**
  114. * @}
  115. */
  116. /* Private function prototypes -----------------------------------------------*/
  117. /* Exported functions --------------------------------------------------------*/
  118. /** @addtogroup OPAMP_LL_Exported_Functions
  119. * @{
  120. */
  121. /** @addtogroup OPAMP_LL_EF_Init
  122. * @{
  123. */
  124. /**
  125. * @brief De-initialize registers of the selected OPAMP instance
  126. * to their default reset values.
  127. * @param OPAMPx OPAMP instance
  128. * @retval An ErrorStatus enumeration value:
  129. * - SUCCESS: OPAMP registers are de-initialized
  130. * - ERROR: OPAMP registers are not de-initialized
  131. */
  132. ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef* OPAMPx)
  133. {
  134. ErrorStatus status = SUCCESS;
  135. /* Check the parameters */
  136. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  137. MODIFY_REG(OPAMP->CSR,
  138. (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)
  139. | (OPAMP_CSR_ANAWSEL1) << __OPAMP_INSTANCE_DECIMAL(OPAMPx)
  140. | (OPAMP_CSR_S7SEL2 * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx)) ,
  141. (OPAMP_CSR_OPA1PD) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
  142. );
  143. return status;
  144. }
  145. /**
  146. * @brief Initialize some features of OPAMP instance.
  147. * @note This function reset bit of calibration mode to ensure
  148. * to be in functional mode, in order to have OPAMP parameters
  149. * (inputs selection, ...) set with the corresponding OPAMP mode
  150. * to be effective.
  151. * @note This function configures features of the selected OPAMP instance.
  152. * Some features are also available at scope OPAMP common instance
  153. * (common to several OPAMP instances).
  154. * Refer to functions having argument "OPAMPxy_COMMON" as parameter.
  155. * @param OPAMPx OPAMP instance
  156. * @param OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
  157. * @retval An ErrorStatus enumeration value:
  158. * - SUCCESS: OPAMP registers are initialized
  159. * - ERROR: OPAMP registers are not initialized
  160. */
  161. ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  162. {
  163. /* Check the parameters */
  164. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  165. assert_param(IS_LL_OPAMP_POWER_MODE(OPAMP_InitStruct->PowerMode));
  166. assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
  167. assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMPx, OPAMP_InitStruct->InputNonInverting));
  168. /* Note: OPAMP inverting input can be used with OPAMP in mode standalone. */
  169. /* Otherwise (OPAMP in mode follower), OPAMP inverting input is */
  170. /* not used (not connected to GPIO pin). */
  171. if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  172. {
  173. assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMPx, OPAMP_InitStruct->InputInverting));
  174. }
  175. /* Configuration of OPAMP instance : */
  176. /* - PowerMode */
  177. /* - Functional mode */
  178. /* - Input non-inverting */
  179. /* - Input inverting */
  180. /* Note: Bits OPAMP_CSR_OPAxCAL_y reset to ensure to be in functional mode */
  181. if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  182. {
  183. MODIFY_REG(OPAMP->CSR,
  184. ((
  185. OPAMP_CSR_OPA1LPM
  186. | OPAMP_CSR_S3SEL1
  187. | OPAMP_CSR_OPA1CAL_H
  188. | OPAMP_CSR_OPA1CAL_L
  189. | OPAMP_CSR_S5SEL1
  190. | OPAMP_CSR_S6SEL1
  191. | OPAMP_CSR_S4SEL1
  192. ) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
  193. )
  194. | ((OPAMP_CSR_ANAWSEL1) << __OPAMP_INSTANCE_DECIMAL(OPAMPx))
  195. | (OPAMP_CSR_S7SEL2 * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx))
  196. ,
  197. ((
  198. (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
  199. | OPAMP_InitStruct->FunctionalMode
  200. | OPAMP_InitStruct->InputNonInverting
  201. | (OPAMP_InitStruct->InputInverting & OPAMP_CSR_S4SEL1)
  202. ) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
  203. )
  204. | ((OPAMP_InitStruct->InputInverting & OPAMP_CSR_ANAWSEL1) << __OPAMP_INSTANCE_DECIMAL(OPAMPx))
  205. | ((OPAMP_InitStruct->InputNonInverting & OPAMP_CSR_S7SEL2) * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx))
  206. );
  207. }
  208. else
  209. {
  210. MODIFY_REG(OPAMP->CSR,
  211. ((
  212. OPAMP_CSR_OPA1LPM
  213. | OPAMP_CSR_OPA1CAL_H
  214. | OPAMP_CSR_OPA1CAL_L
  215. | OPAMP_CSR_S5SEL1
  216. | OPAMP_CSR_S6SEL1
  217. | OPAMP_CSR_S4SEL1
  218. ) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
  219. )
  220. | ((OPAMP_CSR_ANAWSEL1) << __OPAMP_INSTANCE_DECIMAL(OPAMPx))
  221. | (OPAMP_CSR_S7SEL2 * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx))
  222. ,
  223. ((
  224. (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
  225. | OPAMP_InitStruct->FunctionalMode
  226. | OPAMP_InitStruct->InputNonInverting
  227. | OPAMP_CSR_S3SEL1
  228. ) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
  229. )
  230. | ((OPAMP_InitStruct->InputNonInverting & OPAMP_CSR_S7SEL2) * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx))
  231. );
  232. }
  233. return SUCCESS;
  234. }
  235. /**
  236. * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
  237. * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
  238. * whose fields will be set to default values.
  239. * @retval None
  240. */
  241. void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  242. {
  243. /* Set OPAMP_InitStruct fields to default values */
  244. OPAMP_InitStruct->PowerMode = LL_OPAMP_POWERMODE_NORMAL;
  245. OPAMP_InitStruct->FunctionalMode = LL_OPAMP_MODE_FOLLOWER;
  246. OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
  247. /* Note: Parameter discarded if OPAMP in functional mode follower, */
  248. /* set anyway to its default value. */
  249. OPAMP_InitStruct->InputInverting = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
  250. }
  251. /**
  252. * @}
  253. */
  254. /**
  255. * @}
  256. */
  257. /**
  258. * @}
  259. */
  260. #endif /* OPAMP1 || OPAMP2 || OPAMP3 */
  261. /**
  262. * @}
  263. */
  264. #endif /* USE_FULL_LL_DRIVER */
  265. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/