1 /**
2   ******************************************************************************
3   * @file    stm32h7xx_ll_opamp.h
4   * @author  MCD Application Team
5   * @brief   Header file of OPAMP LL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2017 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 
19 /* Define to prevent recursive inclusion -------------------------------------*/
20 #ifndef __STM32H7xx_LL_OPAMP_H
21 #define __STM32H7xx_LL_OPAMP_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32h7xx.h"
29 
30 /** @addtogroup STM32H7xx_LL_Driver
31   * @{
32   */
33 
34 #if defined (OPAMP1) || defined (OPAMP2)
35 
36 /** @defgroup OPAMP_LL OPAMP
37   * @{
38   */
39 
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 
43 /* Private constants ---------------------------------------------------------*/
44 /** @defgroup OPAMP_LL_Private_Constants OPAMP Private Constants
45   * @{
46   */
47 
48 /* Internal mask for OPAMP power mode:                                        */
49 /* To select into literal LL_OPAMP_POWERMODE_x the relevant bits for:         */
50 /* - OPAMP power mode into control register                                   */
51 /* - OPAMP trimming register offset                                           */
52 
53 /* Internal register offset for OPAMP trimming configuration */
54 #define OPAMP_POWERMODE_OTR_REGOFFSET       0x00000000U
55 #define OPAMP_POWERMODE_HSOTR_REGOFFSET     0x00000001U
56 #define OPAMP_POWERMODE_OTR_REGOFFSET_MASK  (OPAMP_POWERMODE_OTR_REGOFFSET | OPAMP_POWERMODE_HSOTR_REGOFFSET)
57 
58 /* Mask for OPAMP power mode into control register */
59 #define OPAMP_POWERMODE_CSR_BIT_MASK        (OPAMP_CSR_OPAHSM)
60 
61 /* Internal mask for OPAMP trimming of transistors differential pair NMOS     */
62 /* or PMOS.                                                                   */
63 /* To select into literal LL_OPAMP_TRIMMING_x the relevant bits for:          */
64 /* - OPAMP trimming selection of transistors differential pair                */
65 /* - OPAMP trimming values of transistors differential pair                   */
66 #define OPAMP_TRIMMING_SELECT_MASK          0x00030000U
67 #define OPAMP_TRIMMING_VALUE_MASK           (OPAMP_OTR_TRIMOFFSETP | OPAMP_OTR_TRIMOFFSETN)
68 
69 /**
70   * @}
71   */
72 
73 
74 /* Private macros ------------------------------------------------------------*/
75 /** @defgroup OPAMP_LL_Private_Macros OPAMP Private Macros
76   * @{
77   */
78 
79 /**
80   * @brief  Driver macro reserved for internal use: set a pointer to
81   *         a register from a register basis from which an offset
82   *         is applied.
83   * @param  __REG__ Register basis from which the offset is applied.
84   * @param  __REG_OFFSET__ Offset to be applied (unit: number of registers).
85   * @retval Register address
86 */
87 #define __OPAMP_PTR_REG_OFFSET(__REG__, __REG_OFFSET__)                        \
88  ((__IO uint32_t *)((uint32_t) ((uint32_t)(&(__REG__)) + ((__REG_OFFSET__) << 2U))))
89 
90 
91 
92 /**
93   * @}
94   */
95 
96 
97 /* Exported types ------------------------------------------------------------*/
98 #if defined(USE_FULL_LL_DRIVER)
99 /** @defgroup OPAMP_LL_ES_INIT OPAMP Exported Init structure
100   * @{
101   */
102 
103 /**
104   * @brief  Structure definition of some features of OPAMP instance.
105   */
106 typedef struct
107 {
108   uint32_t PowerMode;                   /*!< Set OPAMP power mode.
109                                              This parameter can be a value of @ref OPAMP_LL_EC_POWER_MODE
110 
111                                              This feature can be modified afterwards using unitary function @ref LL_OPAMP_SetPowerMode(). */
112 
113 
114   uint32_t FunctionalMode;              /*!< Set OPAMP functional mode by setting internal connections: OPAMP operation in standalone, follower, ...
115                                              This parameter can be a value of @ref OPAMP_LL_EC_FUNCTIONAL_MODE
116                                              @note If OPAMP is configured in mode PGA, the gain can be configured using function @ref LL_OPAMP_SetPGAGain().
117 
118                                              This feature can be modified afterwards using unitary function @ref LL_OPAMP_SetFunctionalMode(). */
119 
120   uint32_t InputNonInverting;           /*!< Set OPAMP input non-inverting connection.
121                                              This parameter can be a value of @ref OPAMP_LL_EC_INPUT_NONINVERTING
122 
123                                              This feature can be modified afterwards using unitary function @ref LL_OPAMP_SetInputNonInverting(). */
124 
125   uint32_t InputInverting;              /*!< Set OPAMP inverting input connection.
126                                              This parameter can be a value of @ref OPAMP_LL_EC_INPUT_INVERTING
127                                              @note OPAMP inverting input is used with OPAMP in mode standalone or PGA with external capacitors for filtering circuit. Otherwise (OPAMP in mode follower), OPAMP inverting input is not used (not connected to GPIO pin), this parameter is discarded.
128 
129                                              This feature can be modified afterwards using unitary function @ref LL_OPAMP_SetInputInverting(). */
130 
131 } LL_OPAMP_InitTypeDef;
132 
133 /**
134   * @}
135   */
136 #endif /* USE_FULL_LL_DRIVER */
137 
138 /* Exported constants --------------------------------------------------------*/
139 /** @defgroup OPAMP_LL_Exported_Constants OPAMP Exported Constants
140   * @{
141   */
142 
143 /** @defgroup OPAMP_LL_EC_MODE OPAMP mode calibration or functional.
144   * @{
145   */
146 #define LL_OPAMP_MODE_FUNCTIONAL        0x00000000U                               /*!< OPAMP functional mode */
147 #define LL_OPAMP_MODE_CALIBRATION       (OPAMP_CSR_CALON)                           /*!< OPAMP calibration mode */
148 /**
149   * @}
150   */
151 
152 /** @defgroup OPAMP_LL_EC_FUNCTIONAL_MODE OPAMP functional mode
153   * @{
154   */
155 #define LL_OPAMP_MODE_STANDALONE        0x00000000U                                                 /*!< OPAMP functional mode, OPAMP operation in standalone */
156 #define LL_OPAMP_MODE_FOLLOWER          (OPAMP_CSR_VMSEL_1 | OPAMP_CSR_VMSEL_0)                       /*!< OPAMP functional mode, OPAMP operation in follower */
157 #define LL_OPAMP_MODE_PGA               (OPAMP_CSR_VMSEL_1)                                           /*!< OPAMP functional mode, OPAMP operation in PGA */
158 #define LL_OPAMP_MODE_PGA_IO0           (OPAMP_CSR_PGGAIN_2|OPAMP_CSR_VMSEL_1)                        /*!< In PGA mode, the inverting input is connected to VINM0 for filtering */
159 #define LL_OPAMP_MODE_PGA_IO0_BIAS      (OPAMP_CSR_PGGAIN_3|OPAMP_CSR_VMSEL_1)                        /*!< In PGA mode, the inverting input is connected to VINM0
160                                                                                                           - Input signal on VINM0, bias on VINPx: negative gain
161                                                                                                           - Bias on VINM0, input signal on VINPx: positive gain */
162 #define LL_OPAMP_MODE_PGA_IO0_IO1_BIAS  (OPAMP_CSR_PGGAIN_3|OPAMP_CSR_PGGAIN_2|OPAMP_CSR_VMSEL_1)    /*!< In PGA mode, the inverting input is connected to VINM0
163                                                                                                           - Input signal on VINM0, bias on VINPx: negative gain
164                                                                                                           - Bias on VINM0, input signal on VINPx: positive gain
165                                                                                                           And VINM1 is connected too for filtering */
166 
167 /**
168   * @}
169   */
170 
171 /** @defgroup OPAMP_LL_EC_MODE_PGA_GAIN OPAMP PGA gain (relevant when OPAMP is in functional mode PGA)
172   * @note Gain sign:
173   *         - is positive if the @ref OPAMP_LL_EC_FUNCTIONAL_MODE configuration is
174   *           @ref LL_OPAMP_MODE_PGA or LL_OPAMP_MODE_PGA_IO0
175   *         - may be positive or negative if the @ref OPAMP_LL_EC_FUNCTIONAL_MODE configuration is
176   *           @ref LL_OPAMP_MODE_PGA_IO0_BIAS or LL_OPAMP_MODE_PGA_IO0_IO1_BIAS
177   *       see @ref OPAMP_LL_EC_FUNCTIONAL_MODE for more details
178   * @{
179   */
180 #define LL_OPAMP_PGA_GAIN_2_OR_MINUS_1             0x00000000U                                                    /*!< OPAMP PGA gain 2  or -1  */
181 #define LL_OPAMP_PGA_GAIN_4_OR_MINUS_3             (                                          OPAMP_CSR_PGGAIN_0) /*!< OPAMP PGA gain 4  or -3  */
182 #define LL_OPAMP_PGA_GAIN_8_OR_MINUS_7             (                     OPAMP_CSR_PGGAIN_1                     ) /*!< OPAMP PGA gain 8  or -7  */
183 #define LL_OPAMP_PGA_GAIN_16_OR_MINUS_15           (                     OPAMP_CSR_PGGAIN_1 | OPAMP_CSR_PGGAIN_0) /*!< OPAMP PGA gain 16 or -15 */
184 /**
185   * @}
186   */
187 
188 /** @defgroup OPAMP_LL_EC_INPUT_NONINVERTING OPAMP input non-inverting
189   * @{
190   */
191 #define LL_OPAMP_INPUT_NONINVERT_IO0         0x00000000U           /*!< OPAMP non inverting input connected to I/O VINP0
192                                                                         (PB0  for OPAMP1, PE9  for OPAMP2)
193                                                                         Note: On this STM32 series, all OPAMPx are not available on all devices. Refer to device datasheet for more details */
194 #define LL_OPAMP_INPUT_NONINVERT_DAC         OPAMP_CSR_VPSEL_0     /*!< OPAMP non inverting input connected internally to DAC channel
195                                                                         (DAC1_CH1 for OPAMP1, DAC1_CH2  for OPAMP2)
196                                                                         Note: On this STM32 series, all OPAMPx are not available on all devices. Refer to device datasheet for more details */
197 #if defined(DAC2)
198 #define LL_OPAMP_INPUT_NONINVERT_DAC2        OPAMP_CSR_VPSEL_1     /*!< OPAMP non inverting input connected internally to DAC2 channel
199                                                                         (DAC3 only for OPAMP2)*/
200 #endif /* DAC2 */
201 
202 /**
203   * @}
204   */
205 
206 /** @defgroup OPAMP_LL_EC_INPUT_INVERTING OPAMP input inverting
207   * @note OPAMP inverting input is used with OPAMP in mode standalone or PGA with negative gain or bias.
208   *       Otherwise (OPAMP in mode follower), OPAMP inverting input is not used (not connected to GPIO pin).
209   * @{
210   */
211 #define LL_OPAMP_INPUT_INVERT_IO0         0x00000000U              /*!< OPAMP inverting input connected to I/O VINM0
212                                                                         (PC5  for OPAMP1, PE8  for OPAMP2)
213                                                                         Note: On this STM32 series, all OPAMPx are not available on all devices. Refer to device datasheet for more details */
214 #define LL_OPAMP_INPUT_INVERT_IO1         OPAMP_CSR_VMSEL_0        /*!< OPAMP inverting input connected to I/0 VINM1
215                                                                         (PA7  for OPAMP1, PG1  for OPAMP2)
216                                                                         Note: On this STM32 series, all OPAMPx are not available on all devices. Refer to device datasheet for more details */
217 #define LL_OPAMP_INPUT_INVERT_CONNECT_NO  OPAMP_CSR_VMSEL_1        /*!< OPAMP inverting input not externally connected (intended for OPAMP in mode follower or PGA with positive gain without bias).
218                                                                         Note: On this STM32 series, this literal include cases of value 0x11 for mode follower and value 0x10 for mode PGA. */
219 /**
220   * @}
221   */
222 
223 
224 
225 /** @defgroup OPAMP_LL_EC_POWER_MODE OPAMP PowerMode
226   * @{
227   */
228 #define LL_OPAMP_POWERMODE_NORMAL        (OPAMP_POWERMODE_OTR_REGOFFSET)                            /*!< OPAMP output in normal mode */
229 #define LL_OPAMP_POWERMODE_HIGHSPEED     (OPAMP_POWERMODE_HSOTR_REGOFFSET | OPAMP_CSR_OPAHSM)       /*!< OPAMP output in highspeed mode */
230 /**
231   * @}
232   */
233 
234 /** @defgroup OPAMP_LL_EC_TRIMMING_MODE OPAMP trimming mode
235   * @{
236   */
237 #define LL_OPAMP_TRIMMING_FACTORY       0x00000000U             /*!< OPAMP trimming factors set to factory values */
238 #define LL_OPAMP_TRIMMING_USER          OPAMP_CSR_USERTRIM      /*!< OPAMP trimming factors set to user values */
239 /**
240   * @}
241   */
242 
243 /** @defgroup OPAMP_LL_EC_TRIMMING_TRANSISTORS_DIFF_PAIR OPAMP trimming of transistors differential pair NMOS or PMOS
244   * @{
245   */
246 #define LL_OPAMP_TRIMMING_NMOS_VREF_90PC_VDDA  (OPAMP_OTR_TRIMOFFSETN | ((OPAMP_CSR_CALSEL_1 | OPAMP_CSR_CALSEL_0) << 4)) /*!< OPAMP trimming of transistors differential pair NMOS (internal reference voltage set to 0.9*Vdda). Default parameters to be used for calibration using two trimming steps (one with each transistors differential pair NMOS and PMOS). */
247 #define LL_OPAMP_TRIMMING_NMOS_VREF_50PC_VDDA  (OPAMP_OTR_TRIMOFFSETN                      | (OPAMP_CSR_CALSEL_1 << 4))   /*!< OPAMP trimming of transistors differential pair NMOS (internal reference voltage set to 0.5*Vdda). */
248 #define LL_OPAMP_TRIMMING_PMOS_VREF_10PC_VDDA  (OPAMP_OTR_TRIMOFFSETP                      | (OPAMP_CSR_CALSEL_0 << 4))   /*!< OPAMP trimming of transistors differential pair PMOS (internal reference voltage set to 0.1*Vdda). Default parameters to be used for calibration using two trimming steps (one with each transistors differential pair NMOS and PMOS). */
249 #define LL_OPAMP_TRIMMING_PMOS_VREF_3_3PC_VDDA (OPAMP_OTR_TRIMOFFSETP                                          )          /*!< OPAMP trimming of transistors differential pair PMOS (internal reference voltage set to 0.33*Vdda). */
250 #define LL_OPAMP_TRIMMING_NMOS                 (LL_OPAMP_TRIMMING_NMOS_VREF_90PC_VDDA)                                    /*!< OPAMP trimming of transistors differential pair NMOS (internal reference voltage set to 0.9*Vdda). Default parameters to be used for calibration using two trimming steps (one with each transistors differential pair NMOS and PMOS). */
251 #define LL_OPAMP_TRIMMING_PMOS                 (LL_OPAMP_TRIMMING_PMOS_VREF_10PC_VDDA)                                    /*!< OPAMP trimming of transistors differential pair PMOS (internal reference voltage set to 0.1*Vdda). Default parameters to be used for calibration using two trimming steps (one with each transistors differential pair NMOS and PMOS). */
252 /**
253   * @}
254   */
255 
256 /** @defgroup OPAMP_LL_EC_HW_DELAYS  Definitions of OPAMP hardware constraints delays
257   * @note   Only OPAMP peripheral HW delays are defined in OPAMP LL driver driver,
258   *         not timeout values.
259   *         For details on delays values, refer to descriptions in source code
260   *         above each literal definition.
261   * @{
262   */
263 
264 /* Delay for OPAMP startup time (transition from state disable to enable).    */
265 /* Note: OPAMP startup time depends on board application environment:         */
266 /*       impedance connected to OPAMP output.                                 */
267 /*       The delay below is specified under conditions:                       */
268 /*        - OPAMP in functional mode follower                                 */
269 /*        - load impedance of 4kOhm (min), 50pF (max)                         */
270 /* Literal set to maximum value (refer to device datasheet,                   */
271 /* parameter "tWAKEUP").                                                      */
272 /* Unit: us                                                                   */
273 #define LL_OPAMP_DELAY_STARTUP_US         (3U)  /*!< Delay for OPAMP startup time */
274 /**
275   * @}
276   */
277 
278 /**
279   * @}
280   */
281 
282 /* Exported macro ------------------------------------------------------------*/
283 /** @defgroup OPAMP_LL_Exported_Macros OPAMP Exported Macros
284   * @{
285   */
286 /** @defgroup OPAMP_LL_EM_WRITE_READ Common write and read registers macro
287   * @{
288   */
289 /**
290   * @brief  Write a value in OPAMP LL_OPAMP_GetPowerModeregister
291   * @param  __INSTANCE__ OPAMP Instance
292   * @param  __REG__ Register to be written
293   * @param  __VALUE__ Value to be written in the register
294   * @retval None
295   */
296 #define LL_OPAMP_WriteReg(__INSTANCE__, __REG__, __VALUE__) WRITE_REG((__INSTANCE__)->__REG__, (__VALUE__))
297 
298 /**
299   * @brief  Read a value in OPAMP register
300   * @param  __INSTANCE__ OPAMP Instance
301   * @param  __REG__ Register to be read
302   * @retval Register value
303   */
304 #define LL_OPAMP_ReadReg(__INSTANCE__, __REG__) READ_REG(__INSTANCE__->__REG__)
305 /**
306   * @}
307   */
308 /** @defgroup OPAMP_LL_EM_HELPER_MACRO OPAMP helper macro
309   * @{
310   */
311 
312 /**
313   * @brief  Helper macro to select the OPAMP common instance
314   *         to which is belonging the selected OPAMP instance.
315   * @note   OPAMP common register instance can be used to
316   *         set parameters common to several OPAMP instances.
317   *         Refer to functions having argument "OPAMPxy_COMMON" as parameter.
318   * @param  __OPAMPx__ OPAMP instance
319   * @retval OPAMP common instance
320   */
321 #if defined(OPAMP1) && defined(OPAMP2)
322 #define __LL_OPAMP_COMMON_INSTANCE(__OPAMPx__)                                 \
323   (OPAMP12_COMMON)
324 #endif
325 
326 /**
327   * @brief  Helper macro to check if all OPAMP instances sharing the same
328   *         OPAMP common instance are disabled.
329   * @note   This check is required by functions with setting conditioned to
330   *         OPAMP state:
331   *         All OPAMP instances of the OPAMP common group must be disabled.
332   *         Refer to functions having argument "OPAMPxy_COMMON" as parameter.
333   * @retval 0: All OPAMP instances sharing the same OPAMP common instance
334   *            are disabled.
335   *         1: At least one OPAMP instance sharing the same OPAMP common instance
336   *            is enabled
337   */
338 #if defined(OPAMP1) && defined(OPAMP2)
339 #define __LL_OPAMP_IS_ENABLED_ALL_COMMON_INSTANCE()                            \
340   (LL_OPAMP_IsEnabled(OPAMP1) |                                                \
341    LL_OPAMP_IsEnabled(OPAMP2)  )
342 #endif
343 
344 /**
345   * @}
346   */
347 
348 /**
349   * @}
350   */
351 
352 /* Exported functions --------------------------------------------------------*/
353 /** @defgroup OPAMP_LL_Exported_Functions OPAMP Exported Functions
354   * @{
355   */
356 
357 /** @defgroup OPAMP_LL_EF_CONFIGURATION_OPAMP_INSTANCE Configuration of OPAMP hierarchical scope: OPAMP instance
358   * @{
359   */
360 
361 /**
362   * @brief  Set OPAMP mode calibration or functional.
363   * @note   OPAMP mode corresponds to functional or calibration mode:
364   *          - functional mode: OPAMP operation in standalone, follower, ...
365   *            Set functional mode using function
366   *            @ref LL_OPAMP_SetFunctionalMode().
367   *          - calibration mode: offset calibration of the selected
368   *            transistors differential pair NMOS or PMOS.
369   * @rmtoll CSR      CALON          LL_OPAMP_SetMode
370   * @param  OPAMPx OPAMP instance
371   * @param  Mode This parameter can be one of the following values:
372   *         @arg @ref LL_OPAMP_MODE_FUNCTIONAL
373   *         @arg @ref LL_OPAMP_MODE_CALIBRATION
374   * @retval None
375   */
LL_OPAMP_SetMode(OPAMP_TypeDef * OPAMPx,uint32_t Mode)376 __STATIC_INLINE void LL_OPAMP_SetMode(OPAMP_TypeDef *OPAMPx, uint32_t Mode)
377 {
378   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_CALON, Mode);
379 }
380 
381 /**
382   * @brief  Get OPAMP mode calibration or functional.
383   * @note   OPAMP mode corresponds to functional or calibration mode:
384   *          - functional mode: OPAMP operation in standalone, follower, ...
385   *            Set functional mode using function
386   *            @ref LL_OPAMP_SetFunctionalMode().
387   *          - calibration mode: offset calibration of the selected
388   *            transistors differential pair NMOS or PMOS.
389   * @rmtoll CSR      CALON          LL_OPAMP_GetMode
390   * @param  OPAMPx OPAMP instance
391   * @retval Returned value can be one of the following values:
392   *         @arg @ref LL_OPAMP_MODE_FUNCTIONAL
393   *         @arg @ref LL_OPAMP_MODE_CALIBRATION
394   */
LL_OPAMP_GetMode(OPAMP_TypeDef * OPAMPx)395 __STATIC_INLINE uint32_t LL_OPAMP_GetMode(OPAMP_TypeDef *OPAMPx)
396 {
397   return (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_CALON));
398 }
399 
400 /**
401   * @brief  Set OPAMP functional mode by setting internal connections.
402   *         OPAMP operation in standalone, follower, ...
403   * @note   This function reset bit of calibration mode to ensure
404   *         to be in functional mode, in order to have OPAMP parameters
405   *         (inputs selection, ...) set with the corresponding OPAMP mode
406   *         to be effective.
407   * @rmtoll CSR      VMSEL          LL_OPAMP_SetFunctionalMode
408   * @param  OPAMPx OPAMP instance
409   * @param  FunctionalMode This parameter can be one of the following values:
410   *         @arg @ref LL_OPAMP_MODE_STANDALONE
411   *         @arg @ref LL_OPAMP_MODE_FOLLOWER
412   *         @arg @ref LL_OPAMP_MODE_PGA
413   *         @arg @ref LL_OPAMP_MODE_PGA_IO0
414   *         @arg @ref LL_OPAMP_MODE_PGA_IO0_BIAS
415   *         @arg @ref LL_OPAMP_MODE_PGA_IO0_IO1_BIAS
416   * @retval None
417   */
LL_OPAMP_SetFunctionalMode(OPAMP_TypeDef * OPAMPx,uint32_t FunctionalMode)418 __STATIC_INLINE void LL_OPAMP_SetFunctionalMode(OPAMP_TypeDef *OPAMPx, uint32_t FunctionalMode)
419 {
420   /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode */
421   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_PGGAIN_3 | OPAMP_CSR_PGGAIN_2 | OPAMP_CSR_VMSEL | OPAMP_CSR_CALON, FunctionalMode);
422 }
423 
424 /**
425   * @brief  Get OPAMP functional mode from setting of internal connections.
426   *         OPAMP operation in standalone, follower, ...
427   * @rmtoll CSR      VMSEL          LL_OPAMP_GetFunctionalMode
428   * @param  OPAMPx OPAMP instance
429   * @retval Returned value can be one of the following values:
430   *         @arg @ref LL_OPAMP_MODE_STANDALONE
431   *         @arg @ref LL_OPAMP_MODE_FOLLOWER
432   *         @arg @ref LL_OPAMP_MODE_PGA
433   *         @arg @ref LL_OPAMP_MODE_PGA_IO0
434   *         @arg @ref LL_OPAMP_MODE_PGA_IO0_BIAS
435   *         @arg @ref LL_OPAMP_MODE_PGA_IO0_IO1_BIAS
436   */
LL_OPAMP_GetFunctionalMode(OPAMP_TypeDef * OPAMPx)437 __STATIC_INLINE uint32_t LL_OPAMP_GetFunctionalMode(OPAMP_TypeDef *OPAMPx)
438 {
439   return (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_PGGAIN_3 | OPAMP_CSR_PGGAIN_2 | OPAMP_CSR_VMSEL));
440 }
441 
442 /**
443   * @brief  Set OPAMP PGA gain.
444   * @note   Preliminarily, OPAMP must be set in mode PGA
445   *         using function @ref LL_OPAMP_SetFunctionalMode().
446   * @rmtoll CSR      PGGAIN         LL_OPAMP_SetPGAGain
447   * @param  OPAMPx OPAMP instance
448   * @param  PGAGain This parameter can be one of the following values:
449   *         @arg @ref LL_OPAMP_PGA_GAIN_2_OR_MINUS_1
450   *         @arg @ref LL_OPAMP_PGA_GAIN_4_OR_MINUS_3
451   *         @arg @ref LL_OPAMP_PGA_GAIN_8_OR_MINUS_7
452   *         @arg @ref LL_OPAMP_PGA_GAIN_16_OR_MINUS_15
453   * @retval None
454   */
LL_OPAMP_SetPGAGain(OPAMP_TypeDef * OPAMPx,uint32_t PGAGain)455 __STATIC_INLINE void LL_OPAMP_SetPGAGain(OPAMP_TypeDef *OPAMPx, uint32_t PGAGain)
456 {
457   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_PGGAIN_1 | OPAMP_CSR_PGGAIN_0, PGAGain);
458 }
459 
460 /**
461   * @brief  Get OPAMP PGA gain.
462   * @note   Preliminarily, OPAMP must be set in mode PGA
463   *         using function @ref LL_OPAMP_SetFunctionalMode().
464   * @rmtoll CSR      PGGAIN         LL_OPAMP_GetPGAGain
465   * @param  OPAMPx OPAMP instance
466   * @retval Returned value can be one of the following values:
467   *         @arg @ref LL_OPAMP_PGA_GAIN_2_OR_MINUS_1
468   *         @arg @ref LL_OPAMP_PGA_GAIN_4_OR_MINUS_3
469   *         @arg @ref LL_OPAMP_PGA_GAIN_8_OR_MINUS_7
470   *         @arg @ref LL_OPAMP_PGA_GAIN_16_OR_MINUS_15
471   */
LL_OPAMP_GetPGAGain(OPAMP_TypeDef * OPAMPx)472 __STATIC_INLINE uint32_t LL_OPAMP_GetPGAGain(OPAMP_TypeDef *OPAMPx)
473 {
474   return (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_PGGAIN_1 | OPAMP_CSR_PGGAIN_0));
475 }
476 
477 /**
478   * @brief  Set OPAMP power mode normal or highspeed.
479   * @note   OPAMP highspeed mode allows output stage to have a better slew rate.
480   * @rmtoll CSR      OPAHSM     LL_OPAMP_SetPowerMode
481   * @param  OPAMPx OPAMP instance
482   * @param  PowerMode This parameter can be one of the following values:
483   *         @arg @ref LL_OPAMP_POWERMODE_NORMAL
484   *         @arg @ref LL_OPAMP_POWERMODE_HIGHSPEED
485   * @retval None
486   */
LL_OPAMP_SetPowerMode(OPAMP_TypeDef * OPAMPx,uint32_t PowerMode)487 __STATIC_INLINE void LL_OPAMP_SetPowerMode(OPAMP_TypeDef *OPAMPx, uint32_t PowerMode)
488 {
489   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_OPAHSM, (PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK));
490 }
491 
492 /**
493   * @brief  Get OPAMP power mode normal or highspeed.
494   * @note   OPAMP highspeed mode allows output stage to have a better slew rate.
495   * @rmtoll CSR      OPAHSM     LL_OPAMP_GetPowerMode
496   * @param  OPAMPx OPAMP instance
497   * @retval Returned value can be one of the following values:
498   *         @arg @ref LL_OPAMP_POWERMODE_NORMAL
499   *         @arg @ref LL_OPAMP_POWERMODE_HIGHSPEED
500   */
LL_OPAMP_GetPowerMode(OPAMP_TypeDef * OPAMPx)501 __STATIC_INLINE uint32_t LL_OPAMP_GetPowerMode(OPAMP_TypeDef *OPAMPx)
502 {
503   uint32_t power_mode = (READ_BIT(OPAMPx->CSR, OPAMP_CSR_OPAHSM));
504 
505   return (uint32_t)(power_mode | (power_mode >> (OPAMP_CSR_OPAHSM_Pos)));
506 }
507 /**
508   * @}
509   */
510 
511 /** @defgroup OPAMP_LL_EF_CONFIGURATION_INPUTS Configuration of OPAMP inputs
512   * @{
513   */
514 
515 /**
516   * @brief  Set OPAMP non-inverting input connection.
517   * @rmtoll CSR      VPSEL          LL_OPAMP_SetInputNonInverting
518   * @param  OPAMPx OPAMP instance
519   * @param  InputNonInverting This parameter can be one of the following values:
520   *         @arg @ref LL_OPAMP_INPUT_NONINVERT_IO0
521   *         @arg @ref LL_OPAMP_INPUT_NONINVERT_DAC
522   *         @arg @ref LL_OPAMP_INPUT_NONINVERT_DAC2 (Only for OPAMP2)
523   * @retval None
524   */
LL_OPAMP_SetInputNonInverting(OPAMP_TypeDef * OPAMPx,uint32_t InputNonInverting)525 __STATIC_INLINE void LL_OPAMP_SetInputNonInverting(OPAMP_TypeDef *OPAMPx, uint32_t InputNonInverting)
526 {
527   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_VPSEL, InputNonInverting);
528 }
529 
530 /**
531   * @brief  Get OPAMP non-inverting input connection.
532   * @rmtoll CSR      VPSEL          LL_OPAMP_GetInputNonInverting
533   * @param  OPAMPx OPAMP instance
534   * @retval Returned value can be one of the following values:
535   *         @arg @ref LL_OPAMP_INPUT_NONINVERT_IO0
536   *         @arg @ref LL_OPAMP_INPUT_NONINVERT_DAC
537   *         @arg @ref LL_OPAMP_INPUT_NONINVERT_DAC2 (Only for OPAMP2)
538   */
LL_OPAMP_GetInputNonInverting(OPAMP_TypeDef * OPAMPx)539 __STATIC_INLINE uint32_t LL_OPAMP_GetInputNonInverting(OPAMP_TypeDef *OPAMPx)
540 {
541   return (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_VPSEL));
542 }
543 
544 /**
545   * @brief  Set OPAMP inverting input connection.
546   * @note   OPAMP inverting input is used with OPAMP in mode standalone
547   *         or PGA with external capacitors for filtering circuit.
548   *         Otherwise (OPAMP in mode follower), OPAMP inverting input
549   *         is not used (not connected to GPIO pin).
550   * @rmtoll CSR      VMSEL          LL_OPAMP_SetInputInverting
551   * @param  OPAMPx OPAMP instance
552   * @param  InputInverting This parameter can be one of the following values:
553   *         @arg @ref LL_OPAMP_INPUT_INVERT_IO0
554   *         @arg @ref LL_OPAMP_INPUT_INVERT_IO1
555   *         @arg @ref LL_OPAMP_INPUT_INVERT_CONNECT_NO
556   * @retval None
557   */
LL_OPAMP_SetInputInverting(OPAMP_TypeDef * OPAMPx,uint32_t InputInverting)558 __STATIC_INLINE void LL_OPAMP_SetInputInverting(OPAMP_TypeDef *OPAMPx, uint32_t InputInverting)
559 {
560   /* Manage cases of OPAMP inverting input not connected (0x10 and 0x11)      */
561   /* to not modify OPAMP mode follower or PGA.                                */
562   /* Bit OPAMP_CSR_VMSEL_1 is set by OPAMP mode (follower, PGA). */
563   MODIFY_REG(OPAMPx->CSR, (~(InputInverting >> 1)) & OPAMP_CSR_VMSEL_0, InputInverting);
564 }
565 
566 /**
567   * @brief  Get OPAMP inverting input connection.
568   * @rmtoll CSR      VMSEL          LL_OPAMP_GetInputInverting
569   * @param  OPAMPx OPAMP instance
570   * @retval Returned value can be one of the following values:
571   *         @arg @ref LL_OPAMP_INPUT_INVERT_IO0
572   *         @arg @ref LL_OPAMP_INPUT_INVERT_IO1
573   *         @arg @ref LL_OPAMP_INPUT_INVERT_CONNECT_NO
574   */
LL_OPAMP_GetInputInverting(OPAMP_TypeDef * OPAMPx)575 __STATIC_INLINE uint32_t LL_OPAMP_GetInputInverting(OPAMP_TypeDef *OPAMPx)
576 {
577   uint32_t input_inverting = READ_BIT(OPAMPx->CSR, OPAMP_CSR_VMSEL);
578 
579   /* Manage cases 0x10 and 0x11 to return the same value: OPAMP inverting     */
580   /* input not connected.                                                     */
581   return (input_inverting & ~((input_inverting >> 1) & OPAMP_CSR_VMSEL_0));
582 }
583 
584 /**
585   * @}
586   */
587 
588 /** @defgroup OPAMP_LL_EF_OPAMP_TRIMMING Configuration and operation of OPAMP trimming
589   * @{
590   */
591 
592 /**
593   * @brief  Set OPAMP trimming mode.
594   * @rmtoll CSR      USERTRIM       LL_OPAMP_SetTrimmingMode
595   * @param  OPAMPx OPAMP instance
596   * @param  TrimmingMode This parameter can be one of the following values:
597   *         @arg @ref LL_OPAMP_TRIMMING_FACTORY
598   *         @arg @ref LL_OPAMP_TRIMMING_USER
599   * @retval None
600   */
LL_OPAMP_SetTrimmingMode(OPAMP_TypeDef * OPAMPx,uint32_t TrimmingMode)601 __STATIC_INLINE void LL_OPAMP_SetTrimmingMode(OPAMP_TypeDef *OPAMPx, uint32_t TrimmingMode)
602 {
603   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_USERTRIM, TrimmingMode);
604 }
605 
606 /**
607   * @brief  Get OPAMP trimming mode.
608   * @rmtoll CSR      USERTRIM       LL_OPAMP_GetTrimmingMode
609   * @param  OPAMPx OPAMP instance
610   * @retval Returned value can be one of the following values:
611   *         @arg @ref LL_OPAMP_TRIMMING_FACTORY
612   *         @arg @ref LL_OPAMP_TRIMMING_USER
613   */
LL_OPAMP_GetTrimmingMode(OPAMP_TypeDef * OPAMPx)614 __STATIC_INLINE uint32_t LL_OPAMP_GetTrimmingMode(OPAMP_TypeDef *OPAMPx)
615 {
616   return (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_USERTRIM));
617 }
618 
619 /**
620   * @brief  Set OPAMP offset to calibrate the selected transistors
621   *         differential pair NMOS or PMOS.
622   * @note   Preliminarily, OPAMP must be set in mode calibration
623   *         using function @ref LL_OPAMP_SetMode().
624   * @rmtoll CSR      CALSEL         LL_OPAMP_SetCalibrationSelection
625   * @param  OPAMPx OPAMP instance
626   * @param  TransistorsDiffPair This parameter can be one of the following values:
627   *         @arg @ref LL_OPAMP_TRIMMING_NMOS            (1)
628   *         @arg @ref LL_OPAMP_TRIMMING_PMOS            (1)
629   *         @arg @ref LL_OPAMP_TRIMMING_NMOS_VREF_50PC_VDDA
630   *         @arg @ref LL_OPAMP_TRIMMING_PMOS_VREF_3_3PC_VDDA
631   *
632   *         (1) Default parameters to be used for calibration
633   *             using two trimming steps (one with each transistors differential
634   *             pair NMOS and PMOS)
635   * @retval None
636   */
LL_OPAMP_SetCalibrationSelection(OPAMP_TypeDef * OPAMPx,uint32_t TransistorsDiffPair)637 __STATIC_INLINE void LL_OPAMP_SetCalibrationSelection(OPAMP_TypeDef *OPAMPx, uint32_t TransistorsDiffPair)
638 {
639   /* Parameter used with mask "OPAMP_TRIMMING_SELECT_MASK" because            */
640   /* containing other bits reserved for other purpose.                        */
641   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_CALSEL, ((TransistorsDiffPair & OPAMP_TRIMMING_SELECT_MASK) >> 4));
642 }
643 
644 /**
645   * @brief  Get OPAMP offset to calibrate the selected transistors
646   *         differential pair NMOS or PMOS.
647   * @note   Preliminarily, OPAMP must be set in mode calibration
648   *         using function @ref LL_OPAMP_SetMode().
649   * @rmtoll CSR      CALSEL         LL_OPAMP_GetCalibrationSelection
650   * @param  OPAMPx OPAMP instance
651   * @retval Returned value can be one of the following values:
652   *         @arg @ref LL_OPAMP_TRIMMING_NMOS            (1)
653   *         @arg @ref LL_OPAMP_TRIMMING_PMOS            (1)
654   *         @arg @ref LL_OPAMP_TRIMMING_NMOS_VREF_50PC_VDDA
655   *         @arg @ref LL_OPAMP_TRIMMING_PMOS_VREF_3_3PC_VDDA
656   *
657   *         (1) Default parameters to be used for calibration
658   *             using two trimming steps (one with each transistors differential
659   *             pair NMOS and PMOS)
660   */
LL_OPAMP_GetCalibrationSelection(OPAMP_TypeDef * OPAMPx)661 __STATIC_INLINE uint32_t LL_OPAMP_GetCalibrationSelection(OPAMP_TypeDef *OPAMPx)
662 {
663   uint32_t CalibrationSelection = (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_CALSEL));
664 
665   return (uint32_t)((CalibrationSelection << 4)|
666           (((CalibrationSelection & OPAMP_CSR_CALSEL_1) == 0UL) ? OPAMP_OTR_TRIMOFFSETN : OPAMP_OTR_TRIMOFFSETP));
667 }
668 
669 /**
670   * @brief  Get OPAMP calibration result of toggling output.
671   * @note   This functions returns:
672   *         0 if OPAMP calibration output is reset
673   *         1 if OPAMP calibration output is set
674   * @rmtoll CSR      OUTCAL         LL_OPAMP_IsCalibrationOutputSet
675   * @param  OPAMPx OPAMP instance
676   * @retval State of bit (1 or 0).
677   */
LL_OPAMP_IsCalibrationOutputSet(OPAMP_TypeDef * OPAMPx)678 __STATIC_INLINE uint32_t LL_OPAMP_IsCalibrationOutputSet(OPAMP_TypeDef *OPAMPx)
679 {
680   return ((READ_BIT(OPAMPx->CSR, OPAMP_CSR_CALOUT) == OPAMP_CSR_CALOUT)?1UL:0UL);
681 }
682 
683 /**
684   * @brief  Set OPAMP trimming factor for the selected transistors
685   *         differential pair NMOS or PMOS, corresponding to the selected
686   *         power mode.
687   * @rmtoll OTR      TRIMOFFSETN    LL_OPAMP_SetTrimmingValue\n
688   *         OTR      TRIMOFFSETP    LL_OPAMP_SetTrimmingValue\n
689   *         HSOTR    TRIMHSOFFSETN  LL_OPAMP_SetTrimmingValue\n
690   *         HSOTR    TRIMHSOFFSETP  LL_OPAMP_SetTrimmingValue
691   * @param  OPAMPx OPAMP instance
692   * @param  PowerMode This parameter can be one of the following values:
693   *         @arg @ref LL_OPAMP_POWERMODE_NORMAL
694   *         @arg @ref LL_OPAMP_POWERMODE_HIGHSPEED
695   * @param  TransistorsDiffPair This parameter can be one of the following values:
696   *         @arg @ref LL_OPAMP_TRIMMING_NMOS
697   *         @arg @ref LL_OPAMP_TRIMMING_PMOS
698   * @param  TrimmingValue 0x00...0x1F
699   * @retval None
700   */
LL_OPAMP_SetTrimmingValue(OPAMP_TypeDef * OPAMPx,uint32_t PowerMode,uint32_t TransistorsDiffPair,uint32_t TrimmingValue)701 __STATIC_INLINE void LL_OPAMP_SetTrimmingValue(OPAMP_TypeDef* OPAMPx, uint32_t PowerMode, uint32_t TransistorsDiffPair, uint32_t TrimmingValue)
702 {
703   __IO uint32_t *preg = __OPAMP_PTR_REG_OFFSET(OPAMPx->OTR, (PowerMode & OPAMP_POWERMODE_OTR_REGOFFSET_MASK));
704 
705   /* Set bits with position in register depending on parameter                */
706   /* "TransistorsDiffPair".                                                   */
707   /* Parameter used with mask "OPAMP_TRIMMING_VALUE_MASK" because             */
708   /* containing other bits reserved for other purpose.                        */
709   MODIFY_REG(*preg,
710              (TransistorsDiffPair & OPAMP_TRIMMING_VALUE_MASK) << 1U,
711              TrimmingValue << ((TransistorsDiffPair == LL_OPAMP_TRIMMING_NMOS) ? OPAMP_OTR_TRIMOFFSETN_Pos : OPAMP_OTR_TRIMOFFSETP_Pos));
712 }
713 
714 /**
715   * @brief  Get OPAMP trimming factor for the selected transistors
716   *         differential pair NMOS or PMOS, corresponding to the selected
717   *         power mode.
718   * @rmtoll OTR      TRIMOFFSETN    LL_OPAMP_GetTrimmingValue\n
719   *         OTR      TRIMOFFSETP    LL_OPAMP_GetTrimmingValue\n
720   *         HSOTR    TRIMHSOFFSETN  LL_OPAMP_GetTrimmingValue\n
721   *         HSOTR    TRIMHSOFFSETP  LL_OPAMP_GetTrimmingValue
722   * @param  OPAMPx OPAMP instance
723   * @param  PowerMode This parameter can be one of the following values:
724   *         @arg @ref LL_OPAMP_POWERMODE_NORMAL
725   *         @arg @ref LL_OPAMP_POWERMODE_HIGHSPEED
726   * @param  TransistorsDiffPair This parameter can be one of the following values:
727   *         @arg @ref LL_OPAMP_TRIMMING_NMOS
728   *         @arg @ref LL_OPAMP_TRIMMING_PMOS
729   * @retval 0x0...0x1F
730   */
LL_OPAMP_GetTrimmingValue(OPAMP_TypeDef * OPAMPx,uint32_t PowerMode,uint32_t TransistorsDiffPair)731 __STATIC_INLINE uint32_t LL_OPAMP_GetTrimmingValue(OPAMP_TypeDef* OPAMPx, uint32_t PowerMode, uint32_t TransistorsDiffPair)
732 {
733   const __IO uint32_t *preg = __OPAMP_PTR_REG_OFFSET(OPAMPx->OTR, (PowerMode & OPAMP_POWERMODE_OTR_REGOFFSET_MASK));
734 
735   /* Retrieve bits with position in register depending on parameter           */
736   /* "TransistorsDiffPair".                                                   */
737   /* Parameter used with mask "OPAMP_TRIMMING_VALUE_MASK" because             */
738   /* containing other bits reserved for other purpose.                        */
739   return (uint32_t)(READ_BIT(*preg, (TransistorsDiffPair & OPAMP_TRIMMING_VALUE_MASK))
740                     >> ((TransistorsDiffPair == LL_OPAMP_TRIMMING_NMOS) ? OPAMP_OTR_TRIMOFFSETN_Pos : OPAMP_OTR_TRIMOFFSETP_Pos));
741 }
742 
743 /**
744   * @}
745   */
746 
747 /** @defgroup OPAMP_LL_EF_OPERATION Operation on OPAMP instance
748   * @{
749   */
750 /**
751   * @brief  Enable OPAMP instance.
752   * @note   After enable from off state, OPAMP requires a delay
753   *         to fulfill wake up time specification.
754   *         Refer to device datasheet, parameter "tWAKEUP".
755   * @rmtoll CSR      OPAMPXEN       LL_OPAMP_Enable
756   * @param  OPAMPx OPAMP instance
757   * @retval None
758   */
LL_OPAMP_Enable(OPAMP_TypeDef * OPAMPx)759 __STATIC_INLINE void LL_OPAMP_Enable(OPAMP_TypeDef *OPAMPx)
760 {
761   SET_BIT(OPAMPx->CSR, OPAMP_CSR_OPAMPxEN);
762 }
763 
764 /**
765   * @brief  Disable OPAMP instance.
766   * @rmtoll CSR      OPAMPXEN       LL_OPAMP_Disable
767   * @param  OPAMPx OPAMP instance
768   * @retval None
769   */
LL_OPAMP_Disable(OPAMP_TypeDef * OPAMPx)770 __STATIC_INLINE void LL_OPAMP_Disable(OPAMP_TypeDef *OPAMPx)
771 {
772   CLEAR_BIT(OPAMPx->CSR, OPAMP_CSR_OPAMPxEN);
773 }
774 
775 /**
776   * @brief  Get OPAMP instance enable state
777   *         (0: OPAMP is disabled, 1: OPAMP is enabled)
778   * @rmtoll CSR      OPAMPXEN       LL_OPAMP_IsEnabled
779   * @param  OPAMPx OPAMP instance
780   * @retval State of bit (1 or 0).
781   */
LL_OPAMP_IsEnabled(OPAMP_TypeDef * OPAMPx)782 __STATIC_INLINE uint32_t LL_OPAMP_IsEnabled(OPAMP_TypeDef *OPAMPx)
783 {
784   return ((READ_BIT(OPAMPx->CSR, OPAMP_CSR_OPAMPxEN) == (OPAMP_CSR_OPAMPxEN))?1UL:0UL);
785 }
786 /**
787   * @}
788   */
789 
790 #if defined(USE_FULL_LL_DRIVER)
791 /** @defgroup OPAMP_LL_EF_Init Initialization and de-initialization functions
792   * @{
793   */
794 
795 ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef *OPAMPx);
796 ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct);
797 void        LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct);
798 
799 /**
800   * @}
801   */
802 #endif /* USE_FULL_LL_DRIVER */
803 
804 /**
805   * @}
806   */
807 
808 /**
809   * @}
810   */
811 
812 #endif /* OPAMP1 || OPAMP2 */
813 
814 /**
815   * @}
816   */
817 
818 #ifdef __cplusplus
819 }
820 #endif
821 
822 #endif /* __STM32H7xx_LL_OPAMP_H */
823 
824