1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_ll_opamp.c
4   * @author  MCD Application Team
5   * @brief   OPAMP LL module driver
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2016 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 #if defined(USE_FULL_LL_DRIVER)
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32f3xx_ll_opamp.h"
22 
23 #ifdef  USE_FULL_ASSERT
24   #include "stm32_assert.h"
25 #else
26   #define assert_param(expr) ((void)0U)
27 #endif
28 
29 /** @addtogroup STM32F3xx_LL_Driver
30   * @{
31   */
32 
33 #if defined (OPAMP1) || defined (OPAMP2) || defined (OPAMP3) || defined (OPAMP4)
34 
35 /** @addtogroup OPAMP_LL OPAMP
36   * @{
37   */
38 
39 /* Private types -------------------------------------------------------------*/
40 /* Private variables ---------------------------------------------------------*/
41 /* Private constants ---------------------------------------------------------*/
42 /* Private macros ------------------------------------------------------------*/
43 
44 /** @addtogroup OPAMP_LL_Private_Macros
45   * @{
46   */
47 
48 /* Check of parameters for configuration of OPAMP hierarchical scope:         */
49 /* OPAMP instance.                                                            */
50 
51 #define IS_LL_OPAMP_FUNCTIONAL_MODE(__FUNCTIONAL_MODE__)                       \
52   (   ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_STANDALONE)                      \
53    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_FOLLOWER)                        \
54    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA)                             \
55   )
56 
57 /* Note: Comparator non-inverting inputs parameters are the same on all       */
58 /*       OPAMP instances.                                                     */
59 /*       However, comparator instance kept as macro parameter for             */
60 /*       compatibility with other STM32 families.                             */
61 #define IS_LL_OPAMP_INPUT_NONINVERTING(__OPAMPX__, __INPUT_NONINVERTING__)     \
62   (   ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0)               \
63    || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO1)               \
64    || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO2)               \
65    || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO3)               \
66   )
67 
68 /* Note: Comparator non-inverting inputs parameters are the same on all       */
69 /*       OPAMP instances.                                                     */
70 /*       However, comparator instance kept as macro parameter for             */
71 /*       compatibility with other STM32 families.                             */
72 #define IS_LL_OPAMP_INPUT_INVERTING(__OPAMPX__, __INPUT_INVERTING__)           \
73   (   ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO0)                     \
74    || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO1)                     \
75    || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_CONNECT_NO)              \
76   )
77 
78 /**
79   * @}
80   */
81 
82 
83 /* Private function prototypes -----------------------------------------------*/
84 
85 /* Exported functions --------------------------------------------------------*/
86 /** @addtogroup OPAMP_LL_Exported_Functions
87   * @{
88   */
89 
90 /** @addtogroup OPAMP_LL_EF_Init
91   * @{
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   */
LL_OPAMP_DeInit(OPAMP_TypeDef * OPAMPx)105 ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef* OPAMPx)
106 {
107   ErrorStatus status = SUCCESS;
108 
109   /* Check the parameters */
110   assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
111 
112   /* Note: Hardware constraint (refer to description of this function):       */
113   /*       OPAMP instance must not be locked.                                 */
114   if (LL_OPAMP_IsLocked(OPAMPx) == 0U)
115   {
116     LL_OPAMP_WriteReg(OPAMPx, CSR, 0x00000000U);
117   }
118   else
119   {
120     /* OPAMP instance is locked: de-initialization by software is              */
121     /* not possible.                                                           */
122     /* The only way to unlock the OPAMP is a device hardware reset.            */
123     status = ERROR;
124   }
125 
126   return status;
127 }
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   * @param  OPAMPx OPAMP instance
136   * @param  OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
137   * @retval An ErrorStatus enumeration value:
138   *          - SUCCESS: OPAMP registers are initialized
139   *          - ERROR: OPAMP registers are not initialized
140   */
LL_OPAMP_Init(OPAMP_TypeDef * OPAMPx,LL_OPAMP_InitTypeDef * OPAMP_InitStruct)141 ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
142 {
143   ErrorStatus status = SUCCESS;
144 
145   /* Check the parameters */
146   assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
147   assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
148   assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMPx, OPAMP_InitStruct->InputNonInverting));
149 
150   /* Note: OPAMP inverting input can be used with OPAMP in mode standalone    */
151   /*       or PGA with external capacitors for filtering circuit.             */
152   /*       Otherwise (OPAMP in mode follower), OPAMP inverting input is       */
153   /*       not used (not connected to GPIO pin).                              */
154   if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
155   {
156     assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMPx, OPAMP_InitStruct->InputInverting));
157   }
158 
159   /* Note: Hardware constraint (refer to description of this function):       */
160   /*       OPAMP instance must not be locked.                                 */
161   if (LL_OPAMP_IsLocked(OPAMPx) == 0U)
162   {
163     /* Configuration of OPAMP instance :                                      */
164     /*  - Functional mode                                                     */
165     /*  - Input non-inverting                                                 */
166     /*  - Input inverting                                                     */
167     /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode.    */
168     if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
169     {
170       MODIFY_REG(OPAMPx->CSR,
171                    OPAMP_CSR_CALON
172                  | OPAMP_CSR_VMSEL
173                  | OPAMP_CSR_VPSEL
174                 ,
175                    OPAMP_InitStruct->FunctionalMode
176                  | OPAMP_InitStruct->InputNonInverting
177                  | OPAMP_InitStruct->InputInverting
178                 );
179     }
180     else
181     {
182       MODIFY_REG(OPAMPx->CSR,
183                    OPAMP_CSR_CALON
184                  | OPAMP_CSR_VMSEL
185                  | OPAMP_CSR_VPSEL
186                 ,
187                    LL_OPAMP_MODE_FOLLOWER
188                  | OPAMP_InitStruct->InputNonInverting
189                 );
190     }
191   }
192   else
193   {
194     /* Initialization error: OPAMP instance is locked.                        */
195     status = ERROR;
196   }
197 
198   return status;
199 }
200 
201 /**
202   * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
203   * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
204   *                         whose fields will be set to default values.
205   * @retval None
206   */
LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef * OPAMP_InitStruct)207 void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
208 {
209   /* Set OPAMP_InitStruct fields to default values */
210   OPAMP_InitStruct->FunctionalMode    = LL_OPAMP_MODE_FOLLOWER;
211   OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
212   /* Note: Parameter discarded if OPAMP in functional mode follower,          */
213   /*       set anyway to its default value.                                   */
214   OPAMP_InitStruct->InputInverting    = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
215 }
216 
217 /**
218   * @}
219   */
220 
221 /**
222   * @}
223   */
224 
225 /**
226   * @}
227   */
228 
229 #endif /* OPAMP1 || OPAMP2 || OPAMP3 || OPAMP4 */
230 
231 /**
232   * @}
233   */
234 
235 #endif /* USE_FULL_LL_DRIVER */
236 
237