1 /**
2   ******************************************************************************
3   * @file    stm32g4xx_ll_opamp.c
4   * @author  MCD Application Team
5   * @brief   OPAMP LL module driver
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2019 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 "stm32g4xx_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 STM32G4xx_LL_Driver
30   * @{
31   */
32 
33 #if defined (OPAMP1) || defined (OPAMP2) || defined (OPAMP3) || defined (OPAMP4) || defined (OPAMP5) || defined (OPAMP6)
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_POWER_MODE(__POWER_MODE__)                                 \
52   (   ((__POWER_MODE__) == LL_OPAMP_POWERMODE_NORMALSPEED)                     \
53    || ((__POWER_MODE__) == LL_OPAMP_POWERMODE_HIGHSPEED))
54 
55 #define IS_LL_OPAMP_FUNCTIONAL_MODE(__FUNCTIONAL_MODE__)                       \
56   (   ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_STANDALONE)                      \
57    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_FOLLOWER)                        \
58    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA)                             \
59    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA_IO0)                         \
60    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA_IO0_BIAS)                    \
61    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA_IO0_IO1_BIAS)                \
62   )
63 
64 #define IS_LL_OPAMP_INPUT_NONINVERTING(__INPUT_NONINVERTING__)                 \
65   (   ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0)               \
66    || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO1)               \
67    || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO2)               \
68    || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO3)               \
69    || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_DAC)               \
70   )
71 
72 #define IS_LL_OPAMP_INPUT_INVERTING(__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) == 0UL)
115   {
116     LL_OPAMP_WriteReg(OPAMPx, CSR, 0x00000000UL);
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   /* Timer controlled mux mode register reset                                  */
127   if (LL_OPAMP_IsTimerMuxLocked(OPAMPx) == 0UL)
128   {
129     LL_OPAMP_WriteReg(OPAMPx, TCMR, 0x00000000UL);
130   }
131   else if (LL_OPAMP_ReadReg(OPAMPx, TCMR) != 0x80000000UL)
132   {
133     /* OPAMP instance timer controlled mux is locked configured, deinit error  */
134     /* The only way to unlock the OPAMP is a device hardware reset.            */
135     status = ERROR;
136   }
137   else
138   {
139     /* OPAMP instance timer controlled mux is locked unconfigured, deinit OK */
140   }
141 
142   return status;
143 }
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   * @param  OPAMPx OPAMP instance
152   * @param  OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
153   * @retval An ErrorStatus enumeration value:
154   *          - SUCCESS: OPAMP registers are initialized
155   *          - ERROR: OPAMP registers are not initialized
156   */
LL_OPAMP_Init(OPAMP_TypeDef * OPAMPx,LL_OPAMP_InitTypeDef * OPAMP_InitStruct)157 ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
158 {
159   ErrorStatus status = SUCCESS;
160 
161   /* Check the parameters */
162   assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
163   assert_param(IS_LL_OPAMP_POWER_MODE(OPAMP_InitStruct->PowerMode));
164   assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
165   assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMP_InitStruct->InputNonInverting));
166 
167   /* Note: OPAMP inverting input can be used with OPAMP in mode standalone    */
168   /*       or PGA with external capacitors for filtering circuit.             */
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(OPAMP_InitStruct->InputInverting));
174   }
175 
176   /* Note: Hardware constraint (refer to description of this function):       */
177   /*       OPAMP instance must not be locked.                                 */
178   if (LL_OPAMP_IsLocked(OPAMPx) == 0U)
179   {
180     /* Configuration of OPAMP instance :                                      */
181     /*  - PowerMode                                                             */
182     /*  - Functional mode                                                     */
183     /*  - Input non-inverting                                                 */
184     /*  - Input inverting                                                     */
185     /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode.    */
186     if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
187     {
188       MODIFY_REG(OPAMPx->CSR,
189                  OPAMP_CSR_HIGHSPEEDEN
190                  | OPAMP_CSR_CALON
191                  | OPAMP_CSR_VMSEL
192                  | OPAMP_CSR_VPSEL
193                  | OPAMP_CSR_PGGAIN_4 | OPAMP_CSR_PGGAIN_3
194                  ,
195                  OPAMP_InitStruct->PowerMode
196                  | OPAMP_InitStruct->FunctionalMode
197                  | OPAMP_InitStruct->InputNonInverting
198                  | OPAMP_InitStruct->InputInverting
199                 );
200     }
201     else
202     {
203       MODIFY_REG(OPAMPx->CSR,
204                  OPAMP_CSR_HIGHSPEEDEN
205                  | OPAMP_CSR_CALON
206                  | OPAMP_CSR_VMSEL
207                  | OPAMP_CSR_VPSEL
208                  | OPAMP_CSR_PGGAIN_4 | OPAMP_CSR_PGGAIN_3
209                  ,
210                  OPAMP_InitStruct->PowerMode
211                  | LL_OPAMP_MODE_FOLLOWER
212                  | OPAMP_InitStruct->InputNonInverting
213                 );
214     }
215   }
216   else
217   {
218     /* Initialization error: OPAMP instance is locked.                        */
219     status = ERROR;
220   }
221 
222   return status;
223 }
224 
225 /**
226   * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
227   * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
228   *                         whose fields will be set to default values.
229   * @retval None
230   */
LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef * OPAMP_InitStruct)231 void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
232 {
233   /* Set OPAMP_InitStruct fields to default values */
234   OPAMP_InitStruct->PowerMode         = LL_OPAMP_POWERMODE_NORMALSPEED;
235   OPAMP_InitStruct->FunctionalMode    = LL_OPAMP_MODE_FOLLOWER;
236   OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
237   /* Note: Parameter discarded if OPAMP in functional mode follower,          */
238   /*       set anyway to its default value.                                   */
239   OPAMP_InitStruct->InputInverting    = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
240 }
241 
242 /**
243   * @}
244   */
245 
246 /**
247   * @}
248   */
249 
250 /**
251   * @}
252   */
253 
254 #endif /* OPAMP1 || OPAMP2 || OPAMP3 || OPAMP4  || OPAMP5 || OPAMP6 */
255 
256 /**
257   * @}
258   */
259 
260 #endif /* USE_FULL_LL_DRIVER */
261 
262