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