stm32f3xx_ll_opamp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_ll_opamp.c
  4. * @author MCD Application Team
  5. * @brief OPAMP LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2016 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 "stm32f3xx_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 STM32F3xx_LL_Driver
  44. * @{
  45. */
  46. #if defined (OPAMP1) || defined (OPAMP2) || defined (OPAMP3) || defined (OPAMP4)
  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_FUNCTIONAL_MODE(__FUNCTIONAL_MODE__) \
  60. ( ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_STANDALONE) \
  61. || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_FOLLOWER) \
  62. || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA) \
  63. )
  64. /* Note: Comparator non-inverting inputs parameters are the same on all */
  65. /* OPAMP instances. */
  66. /* However, comparator instance kept as macro parameter for */
  67. /* compatibility with other STM32 families. */
  68. #define IS_LL_OPAMP_INPUT_NONINVERTING(__OPAMPX__, __INPUT_NONINVERTING__) \
  69. ( ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
  70. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO1) \
  71. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO2) \
  72. || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO3) \
  73. )
  74. /* Note: Comparator non-inverting inputs parameters are the same on all */
  75. /* OPAMP instances. */
  76. /* However, comparator instance kept as macro parameter for */
  77. /* compatibility with other STM32 families. */
  78. #define IS_LL_OPAMP_INPUT_INVERTING(__OPAMPX__, __INPUT_INVERTING__) \
  79. ( ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO0) \
  80. || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO1) \
  81. || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_CONNECT_NO) \
  82. )
  83. /**
  84. * @}
  85. */
  86. /* Private function prototypes -----------------------------------------------*/
  87. /* Exported functions --------------------------------------------------------*/
  88. /** @addtogroup OPAMP_LL_Exported_Functions
  89. * @{
  90. */
  91. /** @addtogroup OPAMP_LL_EF_Init
  92. * @{
  93. */
  94. /**
  95. * @brief De-initialize registers of the selected OPAMP instance
  96. * to their default reset values.
  97. * @note If comparator is locked, de-initialization by software is
  98. * not possible.
  99. * The only way to unlock the comparator is a device hardware reset.
  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. /* Note: Hardware constraint (refer to description of this function): */
  111. /* OPAMP instance must not be locked. */
  112. if(LL_OPAMP_IsLocked(OPAMPx) == 0U)
  113. {
  114. LL_OPAMP_WriteReg(OPAMPx, CSR, 0x00000000U);
  115. }
  116. else
  117. {
  118. /* OPAMP instance is locked: de-initialization by software is */
  119. /* not possible. */
  120. /* The only way to unlock the OPAMP is a device hardware reset. */
  121. status = ERROR;
  122. }
  123. return status;
  124. }
  125. /**
  126. * @brief Initialize some features of OPAMP instance.
  127. * @note This function reset bit of calibration mode to ensure
  128. * to be in functional mode, in order to have OPAMP parameters
  129. * (inputs selection, ...) set with the corresponding OPAMP mode
  130. * to be effective.
  131. * @param OPAMPx OPAMP instance
  132. * @param OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
  133. * @retval An ErrorStatus enumeration value:
  134. * - SUCCESS: OPAMP registers are initialized
  135. * - ERROR: OPAMP registers are not initialized
  136. */
  137. ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  138. {
  139. ErrorStatus status = SUCCESS;
  140. /* Check the parameters */
  141. assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
  142. assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
  143. assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMPx, OPAMP_InitStruct->InputNonInverting));
  144. /* Note: OPAMP inverting input can be used with OPAMP in mode standalone */
  145. /* or PGA with external capacitors for filtering circuit. */
  146. /* Otherwise (OPAMP in mode follower), OPAMP inverting input is */
  147. /* not used (not connected to GPIO pin). */
  148. if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  149. {
  150. assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMPx, OPAMP_InitStruct->InputInverting));
  151. }
  152. /* Note: Hardware constraint (refer to description of this function): */
  153. /* OPAMP instance must not be locked. */
  154. if(LL_OPAMP_IsLocked(OPAMPx) == 0U)
  155. {
  156. /* Configuration of OPAMP instance : */
  157. /* - Functional mode */
  158. /* - Input non-inverting */
  159. /* - Input inverting */
  160. /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode. */
  161. if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
  162. {
  163. MODIFY_REG(OPAMPx->CSR,
  164. OPAMP_CSR_CALON
  165. | OPAMP_CSR_VMSEL
  166. | OPAMP_CSR_VPSEL
  167. ,
  168. OPAMP_InitStruct->FunctionalMode
  169. | OPAMP_InitStruct->InputNonInverting
  170. | OPAMP_InitStruct->InputInverting
  171. );
  172. }
  173. else
  174. {
  175. MODIFY_REG(OPAMPx->CSR,
  176. OPAMP_CSR_CALON
  177. | OPAMP_CSR_VMSEL
  178. | OPAMP_CSR_VPSEL
  179. ,
  180. LL_OPAMP_MODE_FOLLOWER
  181. | OPAMP_InitStruct->InputNonInverting
  182. );
  183. }
  184. }
  185. else
  186. {
  187. /* Initialization error: OPAMP instance is locked. */
  188. status = ERROR;
  189. }
  190. return status;
  191. }
  192. /**
  193. * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
  194. * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
  195. * whose fields will be set to default values.
  196. * @retval None
  197. */
  198. void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
  199. {
  200. /* Set OPAMP_InitStruct fields to default values */
  201. OPAMP_InitStruct->FunctionalMode = LL_OPAMP_MODE_FOLLOWER;
  202. OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
  203. /* Note: Parameter discarded if OPAMP in functional mode follower, */
  204. /* set anyway to its default value. */
  205. OPAMP_InitStruct->InputInverting = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
  206. }
  207. /**
  208. * @}
  209. */
  210. /**
  211. * @}
  212. */
  213. /**
  214. * @}
  215. */
  216. #endif /* OPAMP1 || OPAMP2 || OPAMP3 || OPAMP4 */
  217. /**
  218. * @}
  219. */
  220. #endif /* USE_FULL_LL_DRIVER */
  221. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/