1 /**
2   ******************************************************************************
3   * @file    stm32u5xx_ll_opamp.h
4   * @author  MCD Application Team
5   * @brief   Header file of OPAMP LL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2021 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 STM32U5xx_LL_OPAMP_H
21 #define STM32U5xx_LL_OPAMP_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32u5xx.h"
29 
30 /** @addtogroup STM32U5xx_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_LPOTR_REGOFFSET     0x00000002U
56 #define OPAMP_POWERMODE_OTR_REGOFFSET_MASK  (OPAMP_POWERMODE_OTR_REGOFFSET | OPAMP_POWERMODE_LPOTR_REGOFFSET)
57 
58 /* Mask for OPAMP power mode into control register */
59 #define OPAMP_POWERMODE_CSR_BIT_MASK        (OPAMP_CSR_OPALPM | OPAMP_CSR_HSM)
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          (OPAMP_CSR_CALSEL)
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   ((uint32_t *)((uint32_t) ((uint32_t)(&(__REG__)) + ((__REG_OFFSET__) << 1U))))
89 /**
90   * @}
91   */
92 
93 /* Exported types ------------------------------------------------------------*/
94 #if defined(USE_FULL_LL_DRIVER)
95 /** @defgroup OPAMP_LL_ES_INIT OPAMP Exported Init structure
96   * @{
97   */
98 
99 /**
100   * @brief  Structure definition of some features of OPAMP instance.
101   */
102 typedef struct
103 {
104   uint32_t PowerMode;                   /*!< Set the power mode Normal or Low-Power and the speed mode Normal or High.
105                                              This parameter can be a value of @ref OPAMP_LL_EC_POWERMODE
106                                              This feature can be modified afterwards using
107                                              unitary function @ref LL_OPAMP_SetPowerMode(). */
108 
109   uint32_t FunctionalMode;              /*!< Set OPAMP functional mode by setting internal connections:
110                                              OPAMP operation in standalone, follower, ...
111                                              This parameter can be a value of @ref OPAMP_LL_EC_FUNCTIONAL_MODE
112                                              @note If OPAMP is configured in mode PGA, the gain can be configured
113                                              using function @ref LL_OPAMP_SetPGAGain().
114                                              This feature can be modified afterwards using unitary
115                                              function @ref LL_OPAMP_SetFunctionalMode(). */
116 
117   uint32_t InputNonInverting;           /*!< Set OPAMP input non-inverting connection.
118                                              This parameter can be a value of @ref OPAMP_LL_EC_INPUT_NONINVERTING
119                                              This feature can be modified afterwards using
120                                              unitary function @ref LL_OPAMP_SetInputNonInverting(). */
121 
122   uint32_t InputInverting;              /*!< Set OPAMP inverting input connection.
123                                              This parameter can be a value of @ref OPAMP_LL_EC_INPUT_INVERTING
124                                              @note OPAMP inverting input is used with OPAMP in mode
125                                              standalone or PGA with external capacitors for filtering circuit.
126                                              Otherwise (OPAMP in mode follower), OPAMP inverting input is not used
127                                              (not connected to GPIO pin), this parameter is discarded.
128                                              This feature can be modified afterwards using
129                                              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_POWERSUPPLY_RANGE OPAMP power supply range
144   * @{
145   */
146 #define LL_OPAMP_POWERSUPPLY_RANGE_LOW   0x00000000U            /*!< Power supply range low  */
147 #define LL_OPAMP_POWERSUPPLY_RANGE_HIGH (OPAMP_CSR_OPARANGE)    /*!< Power supply range high - recommended for performance purpose */
148 /**
149   * @}
150   */
151 
152 /** @defgroup OPAMP_LL_EC_POWERMODE OPAMP power mode
153   * @{
154   */
155 #define LL_OPAMP_POWERMODE_NORMALPOWER_NORMALSPEED        0x00000000U                        /*!< OPAMP power mode normal speed normal    */
156 #define LL_OPAMP_POWERMODE_LOWPOWER_NORMALSPEED           OPAMP_CSR_OPALPM                   /*!< OPAMP power mode low-power speed normal */
157 #define LL_OPAMP_POWERMODE_NORMALPOWER_HIGHSPEED          OPAMP_CSR_HSM                      /*!< OPAMP power mode normal speed high      */
158 #define LL_OPAMP_POWERMODE_LOWPOWER_HIGHSPEED            (OPAMP_CSR_OPALPM | OPAMP_CSR_HSM)  /*!< OPAMP power mode low-power speed high   */
159 /**
160   * @}
161   */
162 
163 /** @defgroup OPAMP_LL_EC_MODE OPAMP mode calibration or functional.
164   * @{
165   */
166 #define LL_OPAMP_MODE_FUNCTIONAL         0x00000000U                                      /*!< OPAMP functional mode  */
167 #define LL_OPAMP_MODE_CALIBRATION        (OPAMP_CSR_CALON)                                /*!< OPAMP calibration mode */
168 /**
169   * @}
170   */
171 
172 /** @defgroup OPAMP_LL_EC_FUNCTIONAL_MODE OPAMP functional mode
173   * @{
174   */
175 #define LL_OPAMP_MODE_STANDALONE        0x00000000U                                       /*!< OPAMP functional mode, OPAMP operation in standalone */
176 #define LL_OPAMP_MODE_FOLLOWER          (OPAMP_CSR_OPAMODE_1 | OPAMP_CSR_OPAMODE_0)       /*!< OPAMP functional mode, OPAMP operation in follower   */
177 #define LL_OPAMP_MODE_PGA               (OPAMP_CSR_OPAMODE_1)                             /*!< OPAMP functional mode, OPAMP operation in PGA        */
178 /**
179   * @}
180   */
181 
182 /** @defgroup OPAMP_LL_EC_MODE_PGA_GAIN OPAMP PGA gain (relevant when OPAMP is in functional mode PGA)
183   * @{
184   */
185 #define LL_OPAMP_PGA_GAIN_2             0x00000000U                                        /*!< OPAMP PGA gain 2  */
186 #define LL_OPAMP_PGA_GAIN_4             (OPAMP_CSR_PGA_GAIN_0)                             /*!< OPAMP PGA gain 4  */
187 #define LL_OPAMP_PGA_GAIN_8             (OPAMP_CSR_PGA_GAIN_1)                             /*!< OPAMP PGA gain 8  */
188 #define LL_OPAMP_PGA_GAIN_16            (OPAMP_CSR_PGA_GAIN_1 | OPAMP_CSR_PGA_GAIN_0)      /*!< OPAMP PGA gain 16 */
189 /**
190   * @}
191   */
192 
193 /** @defgroup OPAMP_LL_EC_INPUT_NONINVERTING OPAMP input non-inverting
194   * @{
195   */
196 #define LL_OPAMP_INPUT_NONINVERT_IO0       0x00000000U           /*!< OPAMP non inverting input connected to GPIO pin (pin PA0 for OPAMP1, pin PA6 for OPAMP2)             */
197 #define LL_OPAMP_INPUT_NONINVERT_DAC       (OPAMP_CSR_VP_SEL)    /*!< OPAMP non inverting input connected to DAC1 channel output(channel1 for OPAMP1, channel2 for OPAMP2) */
198 /**
199   * @}
200   */
201 
202 /** @defgroup OPAMP_LL_EC_INPUT_INVERTING OPAMP input inverting
203   * @{
204   */
205 #define LL_OPAMP_INPUT_INVERT_IO0         0x00000000U              /*!< OPAMP inverting input connected to GPIO pin (valid also in PGA mode for filtering). 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).                 */
206 #define LL_OPAMP_INPUT_INVERT_IO1         (OPAMP_CSR_VM_SEL_0)     /*!< OPAMP inverting input (low leakage input) connected to GPIO pin (available only on package BGA132). 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). */
207 #define LL_OPAMP_INPUT_INVERT_CONNECT_NO  (OPAMP_CSR_VM_SEL_1)     /*!< OPAMP inverting input not externally connected (intended for OPAMP in mode follower or PGA without external capacitors for filtering)                                                                                                                                                                                           */
208 /**
209   * @}
210   */
211 
212 /** @defgroup OPAMP_LL_EC_INPUT_LEGACY OPAMP inputs legacy literals name
213   * @{
214   */
215 #define LL_OPAMP_NONINVERTINGINPUT_IO0      LL_OPAMP_INPUT_NONINVERT_IO0
216 #define LL_OPAMP_NONINVERTINGINPUT_DAC_CH   LL_OPAMP_INPUT_NONINVERT_DAC
217 
218 #define LL_OPAMP_INVERTINGINPUT_IO0         LL_OPAMP_INPUT_INVERT_IO0
219 #define LL_OPAMP_INVERTINGINPUT_IO1         LL_OPAMP_INPUT_INVERT_IO1
220 #define LL_OPAMP_INVERTINGINPUT_CONNECT_NO  LL_OPAMP_INPUT_INVERT_CONNECT_NO
221 
222 #define LL_OPAMP_INPUT_NONINV_DAC1_CH1      LL_OPAMP_INPUT_NONINVERT_DAC
223 
224 #define LL_OPAMP_INPUT_NONINVERT_DAC1_CH1   LL_OPAMP_INPUT_NONINVERT_DAC
225 /**
226   * @}
227   */
228 
229 /** @defgroup OPAMP_LL_EC_TRIMMING_MODE OPAMP trimming mode
230   * @{
231   */
232 #define LL_OPAMP_TRIMMING_FACTORY           0x00000000U              /*!< OPAMP trimming factors set to factory values */
233 #define LL_OPAMP_TRIMMING_USER              (OPAMP_CSR_USERTRIM)     /*!< OPAMP trimming factors set to user values    */
234 /**
235   * @}
236   */
237 
238 /** @defgroup OPAMP_LL_EC_TRIMMING_TRANSISTORS_DIFF_PAIR OPAMP trimming of transistors differential pair NMOS or PMOS
239   * @{
240   */
241 #define LL_OPAMP_TRIMMING_NMOS          (OPAMP_OTR_TRIMOFFSETN)                     /*!< OPAMP trimming of transistors differential pair NMOS */
242 #define LL_OPAMP_TRIMMING_PMOS          (OPAMP_OTR_TRIMOFFSETP | OPAMP_CSR_CALSEL)  /*!< OPAMP trimming of transistors differential pair PMOS */
243 /**
244   * @}
245   */
246 
247 /** @defgroup OPAMP_LL_EC_HW_DELAYS  Definitions of OPAMP hardware constraints delays
248   * @note   Only OPAMP IP HW delays are defined in OPAMP LL driver driver,
249   *         not timeout values.
250   *         For details on delays values, refer to descriptions in source code
251   *         above each literal definition.
252   * @{
253   */
254 
255 /* Delay for OPAMP startup time (transition from state disable to enable).    */
256 /* Note: OPAMP startup time depends on board application environment:         */
257 /*       impedance connected to OPAMP output.                                 */
258 /*       The delay below is specified under conditions:                       */
259 /*        - OPAMP in mode low power                                           */
260 /*        - OPAMP in functional mode follower                                 */
261 /*        - load impedance of 4kOhm (min), 50pF (max)                         */
262 /* Literal set to maximum value (refer to device datasheet,                   */
263 /* parameter "tWAKEUP").                                                      */
264 /* Unit: us                                                                   */
265 #define LL_OPAMP_DELAY_STARTUP_US         ((uint32_t) 30U)  /*!< Delay for OPAMP startup time */
266 
267 /**
268   * @}
269   */
270 
271 /**
272   * @}
273   */
274 
275 /* Exported macro ------------------------------------------------------------*/
276 /** @defgroup OPAMP_LL_Exported_Macros OPAMP Exported Macros
277   * @{
278   */
279 /** @defgroup OPAMP_LL_EM_WRITE_READ Common write and read registers macro
280   * @{
281   */
282 /**
283   * @brief  Write a value in OPAMP register
284   * @param  __INSTANCE__ OPAMP Instance
285   * @param  __REG__ Register to be written
286   * @param  __VALUE__ Value to be written in the register
287   * @retval None
288   */
289 #define LL_OPAMP_WriteReg(__INSTANCE__, __REG__, __VALUE__) WRITE_REG((__INSTANCE__)->__REG__, (__VALUE__))
290 
291 /**
292   * @brief  Read a value in OPAMP register
293   * @param  __INSTANCE__ OPAMP Instance
294   * @param  __REG__ Register to be read
295   * @retval Register value
296   */
297 #define LL_OPAMP_ReadReg(__INSTANCE__, __REG__) READ_REG((__INSTANCE__)->__REG__)
298 /**
299   * @}
300   */
301 
302 /** @defgroup OPAMP_LL_EM_HELPER_MACRO OPAMP helper macro
303   * @{
304   */
305 
306 /**
307   * @brief  Helper macro to select the OPAMP common instance
308   *         to which is belonging the selected OPAMP instance.
309   * @note   OPAMP common register instance can be used to
310   *         set parameters common to several OPAMP instances.
311   *         Refer to functions having argument "OPAMPxy_COMMON" as parameter.
312   * @param  __OPAMPx__ OPAMP instance
313   * @retval OPAMP common instance
314   */
315 #define __LL_OPAMP_COMMON_INSTANCE(__OPAMPx__) (OPAMP12_COMMON)
316 
317 /**
318   * @brief  Helper macro to check if all OPAMP instances sharing the same
319   *         OPAMP common instance are disabled.
320   * @note   This check is required by functions with setting conditioned to
321   *         OPAMP state:
322   *         All OPAMP instances of the OPAMP common group must be disabled.
323   *         Refer to functions having argument "OPAMPxy_COMMON" as parameter.
324   * @retval 0: All OPAMP instances sharing the same OPAMP common instance
325   *            are disabled.
326   *         1: At least one OPAMP instance sharing the same OPAMP common instance
327   *            is enabled
328   */
329 #if defined(OPAMP1) && defined(OPAMP2)
330 #define __LL_OPAMP_IS_ENABLED_ALL_COMMON_INSTANCE()                            \
331   (LL_OPAMP_IsEnabled(OPAMP1) |                                                \
332    LL_OPAMP_IsEnabled(OPAMP2)  )
333 #else
334 #define __LL_OPAMP_IS_ENABLED_ALL_COMMON_INSTANCE()                            \
335   (LL_OPAMP_IsEnabled(OPAMP1))
336 #endif /* defined(OPAMP1) && defined(OPAMP2) */
337 /**
338   * @}
339   */
340 
341 /* Exported functions --------------------------------------------------------*/
342 /** @defgroup OPAMP_LL_Exported_Functions OPAMP Exported Functions
343   * @{
344   */
345 
346 /** @defgroup OPAMP_LL_EF_Configuration_opamp_common Configuration of OPAMP hierarchical scope: common OPAMP instances
347   * @{
348   */
349 
350 /**
351   * @brief  Set OPAMP power range.
352   * @note   The OPAMP power range applies to several OPAMP instances
353   *         (if several OPAMP instances available on the selected device).
354   *         It is mandatory to set already the power range to high for performance purpose.
355   * @note   On this STM32 series, setting of this feature is conditioned to
356   *         OPAMP state:
357   *         All OPAMP instances of the OPAMP common group must be disabled.
358   *         This check can be done with function @ref LL_OPAMP_IsEnabled() for each
359   *         OPAMP instance.
360   * @note   LL_OPAMP_POWERSUPPLY_RANGE_HIGH parameter is highly recommended for performance purpose.
361   * @rmtoll CSR      OPARANGE       LL_OPAMP_SetCommonPowerRange
362   * @param  OPAMPxy_COMMON OPAMP common instance
363   * @param  PowerRange This parameter can be one of the following values:
364   *         @arg @ref LL_OPAMP_POWERSUPPLY_RANGE_LOW
365   *         @arg @ref LL_OPAMP_POWERSUPPLY_RANGE_HIGH
366   * @retval None
367   */
LL_OPAMP_SetCommonPowerRange(OPAMP_Common_TypeDef * OPAMPxy_COMMON,uint32_t PowerRange)368 __STATIC_INLINE void LL_OPAMP_SetCommonPowerRange(OPAMP_Common_TypeDef *OPAMPxy_COMMON, uint32_t PowerRange)
369 {
370   MODIFY_REG(OPAMPxy_COMMON->CSR, OPAMP_CSR_OPARANGE, PowerRange);
371 }
372 
373 /**
374   * @brief  Get OPAMP power range.
375   * @note   The OPAMP power range applies to several OPAMP instances
376   *         (if several OPAMP instances available on the selected device).
377   * @rmtoll CSR      OPARANGE       LL_OPAMP_GetCommonPowerRange
378   * @param  OPAMPxy_COMMON OPAMP common instance
379   * @retval Returned value can be one of the following values:
380   *         @arg @ref LL_OPAMP_POWERSUPPLY_RANGE_LOW
381   *         @arg @ref LL_OPAMP_POWERSUPPLY_RANGE_HIGH
382   */
LL_OPAMP_GetCommonPowerRange(const OPAMP_Common_TypeDef * OPAMPxy_COMMON)383 __STATIC_INLINE uint32_t LL_OPAMP_GetCommonPowerRange(const OPAMP_Common_TypeDef *OPAMPxy_COMMON)
384 {
385   return (uint32_t)(READ_BIT(OPAMPxy_COMMON->CSR, OPAMP_CSR_OPARANGE));
386 }
387 
388 /**
389   * @}
390   */
391 
392 /** @defgroup OPAMP_LL_EF_CONFIGURATION_OPAMP_INSTANCE Configuration of OPAMP hierarchical scope: OPAMP instance
393   * @{
394   */
395 
396 /**
397   * @brief  Set OPAMP power mode.
398   * @note   The OPAMP must be disabled to change this configuration.
399   * @rmtoll CSR      OPALPM         LL_OPAMP_SetPowerMode
400   * @param  OPAMPx OPAMP instance
401   * @param  PowerMode This parameter can be one of the following values:
402   *         @arg @ref LL_OPAMP_POWERMODE_NORMALPOWER_NORMALSPEED
403   *         @arg @ref LL_OPAMP_POWERMODE_LOWPOWER_NORMALSPEED
404   *         @arg @ref LL_OPAMP_POWERMODE_NORMALPOWER_HIGHSPEED
405   *         @arg @ref LL_OPAMP_POWERMODE_LOWPOWER_HIGHSPEED
406   * @retval None
407   */
LL_OPAMP_SetPowerMode(OPAMP_TypeDef * OPAMPx,uint32_t PowerMode)408 __STATIC_INLINE void LL_OPAMP_SetPowerMode(OPAMP_TypeDef *OPAMPx, uint32_t PowerMode)
409 {
410   MODIFY_REG(OPAMPx->CSR, OPAMP_POWERMODE_CSR_BIT_MASK, PowerMode);
411 }
412 
413 /**
414   * @brief  Get OPAMP power mode.
415   * @rmtoll CSR OPALPM & HSM      LL_OPAMP_GetPowerMode
416   * @param  OPAMPx OPAMP instance
417   * @retval Returned value can be one of the following values:
418   *         @arg @ref LL_OPAMP_POWERMODE_NORMALPOWER_NORMALSPEED
419   *         @arg @ref LL_OPAMP_POWERMODE_LOWPOWER_NORMALSPEED
420   *         @arg @ref LL_OPAMP_POWERMODE_NORMALPOWER_HIGHSPEED
421   *         @arg @ref LL_OPAMP_POWERMODE_LOWPOWER_HIGHSPEED
422   */
LL_OPAMP_GetPowerMode(const OPAMP_TypeDef * OPAMPx)423 __STATIC_INLINE uint32_t LL_OPAMP_GetPowerMode(const OPAMP_TypeDef *OPAMPx)
424 {
425   uint32_t power_mode = (READ_BIT(OPAMPx->CSR, OPAMP_POWERMODE_CSR_BIT_MASK));
426 
427   return (uint32_t)(power_mode);
428 }
429 
430 /**
431   * @brief  Set OPAMP mode calibration or functional.
432   * @note   OPAMP mode corresponds to functional or calibration mode:
433   *          - functional mode: OPAMP operation in standalone, follower, ...
434   *            Set functional mode using function
435   *            @ref LL_OPAMP_SetFunctionalMode().
436   *          - calibration mode: offset calibration of the selected
437   *            transistors differential pair NMOS or PMOS.
438   * @note   On this STM32 series, during calibration, OPAMP functional
439   *         mode must be set to standalone or follower mode
440   *         (in order to open internal connections to resistors
441   *         of PGA mode).
442   *         Refer to function @ref LL_OPAMP_SetFunctionalMode().
443   * @rmtoll CSR      CALON          LL_OPAMP_SetMode
444   * @param  OPAMPx OPAMP instance
445   * @param  Mode This parameter can be one of the following values:
446   *         @arg @ref LL_OPAMP_MODE_FUNCTIONAL
447   *         @arg @ref LL_OPAMP_MODE_CALIBRATION
448   * @retval None
449   */
LL_OPAMP_SetMode(OPAMP_TypeDef * OPAMPx,uint32_t Mode)450 __STATIC_INLINE void LL_OPAMP_SetMode(OPAMP_TypeDef *OPAMPx, uint32_t Mode)
451 {
452   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_CALON, Mode);
453 }
454 
455 /**
456   * @brief  Get OPAMP mode calibration or functional.
457   * @note   OPAMP mode corresponds to functional or calibration mode:
458   *          - functional mode: OPAMP operation in standalone, follower, ...
459   *            Set functional mode using function
460   *            @ref LL_OPAMP_SetFunctionalMode().
461   *          - calibration mode: offset calibration of the selected
462   *            transistors differential pair NMOS or PMOS.
463   * @rmtoll CSR      CALON          LL_OPAMP_GetMode
464   * @param  OPAMPx OPAMP instance
465   * @retval Returned value can be one of the following values:
466   *         @arg @ref LL_OPAMP_MODE_FUNCTIONAL
467   *         @arg @ref LL_OPAMP_MODE_CALIBRATION
468   */
LL_OPAMP_GetMode(const OPAMP_TypeDef * OPAMPx)469 __STATIC_INLINE uint32_t LL_OPAMP_GetMode(const OPAMP_TypeDef *OPAMPx)
470 {
471   return (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_CALON));
472 }
473 
474 /**
475   * @brief  Set OPAMP functional mode by setting internal connections.
476   *         OPAMP operation in standalone, follower, ...
477   * @note   This function reset bit of calibration mode to ensure
478   *         to be in functional mode, in order to have OPAMP parameters
479   *         (inputs selection, ...) set with the corresponding OPAMP mode
480   *         to be effective.
481   * @rmtoll CSR      OPAMODE        LL_OPAMP_SetFunctionalMode
482   * @param  OPAMPx OPAMP instance
483   * @param  FunctionalMode This parameter can be one of the following values:
484   *         @arg @ref LL_OPAMP_MODE_STANDALONE
485   *         @arg @ref LL_OPAMP_MODE_FOLLOWER
486   *         @arg @ref LL_OPAMP_MODE_PGA
487   * @retval None
488   */
LL_OPAMP_SetFunctionalMode(OPAMP_TypeDef * OPAMPx,uint32_t FunctionalMode)489 __STATIC_INLINE void LL_OPAMP_SetFunctionalMode(OPAMP_TypeDef *OPAMPx, uint32_t FunctionalMode)
490 {
491   /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode */
492   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_OPAMODE | OPAMP_CSR_CALON, FunctionalMode);
493 }
494 
495 /**
496   * @brief  Get OPAMP functional mode from setting of internal connections.
497   *         OPAMP operation in standalone, follower, ...
498   * @rmtoll CSR      OPAMODE        LL_OPAMP_GetFunctionalMode
499   * @param  OPAMPx OPAMP instance
500   * @retval Returned value can be one of the following values:
501   *         @arg @ref LL_OPAMP_MODE_STANDALONE
502   *         @arg @ref LL_OPAMP_MODE_FOLLOWER
503   *         @arg @ref LL_OPAMP_MODE_PGA
504   */
LL_OPAMP_GetFunctionalMode(const OPAMP_TypeDef * OPAMPx)505 __STATIC_INLINE uint32_t LL_OPAMP_GetFunctionalMode(const OPAMP_TypeDef *OPAMPx)
506 {
507   return (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_OPAMODE));
508 }
509 
510 /**
511   * @brief  Set OPAMP PGA gain.
512   * @note   Preliminarily, OPAMP must be set in mode PGA
513   *         using function @ref LL_OPAMP_SetFunctionalMode().
514   * @rmtoll CSR      PGGAIN         LL_OPAMP_SetPGAGain
515   * @param  OPAMPx OPAMP instance
516   * @param  PGAGain This parameter can be one of the following values:
517   *         @arg @ref LL_OPAMP_PGA_GAIN_2
518   *         @arg @ref LL_OPAMP_PGA_GAIN_4
519   *         @arg @ref LL_OPAMP_PGA_GAIN_8
520   *         @arg @ref LL_OPAMP_PGA_GAIN_16
521   * @retval None
522   */
LL_OPAMP_SetPGAGain(OPAMP_TypeDef * OPAMPx,uint32_t PGAGain)523 __STATIC_INLINE void LL_OPAMP_SetPGAGain(OPAMP_TypeDef *OPAMPx, uint32_t PGAGain)
524 {
525   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_PGA_GAIN, PGAGain);
526 }
527 
528 /**
529   * @brief  Get OPAMP PGA gain.
530   * @note   Preliminarily, OPAMP must be set in mode PGA
531   *         using function @ref LL_OPAMP_SetFunctionalMode().
532   * @rmtoll CSR      PGGAIN         LL_OPAMP_GetPGAGain
533   * @param  OPAMPx OPAMP instance
534   * @retval Returned value can be one of the following values:
535   *         @arg @ref LL_OPAMP_PGA_GAIN_2
536   *         @arg @ref LL_OPAMP_PGA_GAIN_4
537   *         @arg @ref LL_OPAMP_PGA_GAIN_8
538   *         @arg @ref LL_OPAMP_PGA_GAIN_16
539   */
LL_OPAMP_GetPGAGain(const OPAMP_TypeDef * OPAMPx)540 __STATIC_INLINE uint32_t LL_OPAMP_GetPGAGain(const OPAMP_TypeDef *OPAMPx)
541 {
542   return (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_PGA_GAIN));
543 }
544 
545 /**
546   * @}
547   */
548 
549 /** @defgroup OPAMP_LL_EF_CONFIGURATION_INPUTS Configuration of OPAMP inputs
550   * @{
551   */
552 
553 /**
554   * @brief  Set OPAMP non-inverting input connection.
555   * @rmtoll CSR      VPSEL          LL_OPAMP_SetInputNonInverting
556   * @param  OPAMPx OPAMP instance
557   * @param  InputNonInverting This parameter can be one of the following values:
558   *         @arg @ref LL_OPAMP_INPUT_NONINVERT_IO0
559   *         @arg @ref LL_OPAMP_INPUT_NONINVERT_DAC
560   * @retval None
561   */
LL_OPAMP_SetInputNonInverting(OPAMP_TypeDef * OPAMPx,uint32_t InputNonInverting)562 __STATIC_INLINE void LL_OPAMP_SetInputNonInverting(OPAMP_TypeDef *OPAMPx, uint32_t InputNonInverting)
563 {
564   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_VP_SEL, InputNonInverting);
565 }
566 
567 /**
568   * @brief  Get OPAMP non-inverting input connection.
569   * @rmtoll CSR      VPSEL          LL_OPAMP_GetInputNonInverting
570   * @param  OPAMPx OPAMP instance
571   * @retval Returned value can be one of the following values:
572   *         @arg @ref LL_OPAMP_INPUT_NONINVERT_IO0
573   *         @arg @ref LL_OPAMP_INPUT_NONINVERT_DAC
574   */
LL_OPAMP_GetInputNonInverting(const OPAMP_TypeDef * OPAMPx)575 __STATIC_INLINE uint32_t LL_OPAMP_GetInputNonInverting(const OPAMP_TypeDef *OPAMPx)
576 {
577   return (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_VP_SEL));
578 }
579 
580 /**
581   * @brief  Set OPAMP inverting input connection.
582   * @note   OPAMP inverting input is used with OPAMP in mode standalone
583   *         or PGA with external capacitors for filtering circuit.
584   *         Otherwise (OPAMP in mode follower), OPAMP inverting input
585   *         is not used (not connected to GPIO pin).
586   * @rmtoll CSR      VMSEL          LL_OPAMP_SetInputInverting
587   * @param  OPAMPx OPAMP instance
588   * @param  InputInverting This parameter 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   * @retval None
593   */
LL_OPAMP_SetInputInverting(OPAMP_TypeDef * OPAMPx,uint32_t InputInverting)594 __STATIC_INLINE void LL_OPAMP_SetInputInverting(OPAMP_TypeDef *OPAMPx, uint32_t InputInverting)
595 {
596   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_VM_SEL, InputInverting);
597 }
598 
599 /**
600   * @brief  Get OPAMP inverting input connection.
601   * @rmtoll CSR      VMSEL          LL_OPAMP_GetInputInverting
602   * @param  OPAMPx OPAMP instance
603   * @retval Returned value can be one of the following values:
604   *         @arg @ref LL_OPAMP_INPUT_INVERT_IO0
605   *         @arg @ref LL_OPAMP_INPUT_INVERT_IO1
606   *         @arg @ref LL_OPAMP_INPUT_INVERT_CONNECT_NO
607   */
LL_OPAMP_GetInputInverting(const OPAMP_TypeDef * OPAMPx)608 __STATIC_INLINE uint32_t LL_OPAMP_GetInputInverting(const OPAMP_TypeDef *OPAMPx)
609 {
610   return (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_VM_SEL));
611 }
612 
613 /**
614   * @}
615   */
616 
617 /** @defgroup OPAMP_LL_EF_Configuration_Legacy_Functions Configuration of OPAMP, legacy functions name
618   * @{
619   */
620 /* Old functions name kept for legacy purpose, to be replaced by the          */
621 /* current functions name.                                                    */
LL_OPAMP_SetNonInvertingInput(OPAMP_TypeDef * OPAMPx,uint32_t NonInvertingInput)622 __STATIC_INLINE void LL_OPAMP_SetNonInvertingInput(OPAMP_TypeDef *OPAMPx, uint32_t NonInvertingInput)
623 {
624   LL_OPAMP_SetInputNonInverting(OPAMPx, NonInvertingInput);
625 }
626 
LL_OPAMP_SetInvertingInput(OPAMP_TypeDef * OPAMPx,uint32_t InvertingInput)627 __STATIC_INLINE void LL_OPAMP_SetInvertingInput(OPAMP_TypeDef *OPAMPx, uint32_t InvertingInput)
628 {
629   LL_OPAMP_SetInputInverting(OPAMPx, InvertingInput);
630 }
631 
632 /**
633   * @}
634   */
635 
636 /** @defgroup OPAMP_LL_EF_OPAMP_TRIMMING Configuration and operation of OPAMP trimming
637   * @{
638   */
639 
640 /**
641   * @brief  Set OPAMP trimming mode.
642   * @rmtoll CSR      USERTRIM       LL_OPAMP_SetTrimmingMode
643   * @param  OPAMPx OPAMP instance
644   * @param  TrimmingMode This parameter can be one of the following values:
645   *         @arg @ref LL_OPAMP_TRIMMING_FACTORY
646   *         @arg @ref LL_OPAMP_TRIMMING_USER
647   * @retval None
648   */
LL_OPAMP_SetTrimmingMode(OPAMP_TypeDef * OPAMPx,uint32_t TrimmingMode)649 __STATIC_INLINE void LL_OPAMP_SetTrimmingMode(OPAMP_TypeDef *OPAMPx, uint32_t TrimmingMode)
650 {
651   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_USERTRIM, TrimmingMode);
652 }
653 
654 /**
655   * @brief  Get OPAMP trimming mode.
656   * @rmtoll CSR      USERTRIM       LL_OPAMP_GetTrimmingMode
657   * @param  OPAMPx OPAMP instance
658   * @retval Returned value can be one of the following values:
659   *         @arg @ref LL_OPAMP_TRIMMING_FACTORY
660   *         @arg @ref LL_OPAMP_TRIMMING_USER
661   */
LL_OPAMP_GetTrimmingMode(const OPAMP_TypeDef * OPAMPx)662 __STATIC_INLINE uint32_t LL_OPAMP_GetTrimmingMode(const OPAMP_TypeDef *OPAMPx)
663 {
664   return (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_USERTRIM));
665 }
666 
667 /**
668   * @brief  Set OPAMP offset to calibrate the selected transistors
669   *         differential pair NMOS or PMOS.
670   * @note   Preliminarily, OPAMP must be set in mode calibration
671   *         using function @ref LL_OPAMP_SetMode().
672   * @rmtoll CSR      CALSEL         LL_OPAMP_SetCalibrationSelection
673   * @param  OPAMPx OPAMP instance
674   * @param  TransistorsDiffPair This parameter can be one of the following values:
675   *         @arg @ref LL_OPAMP_TRIMMING_NMOS
676   *         @arg @ref LL_OPAMP_TRIMMING_PMOS
677   * @retval None
678   */
LL_OPAMP_SetCalibrationSelection(OPAMP_TypeDef * OPAMPx,uint32_t TransistorsDiffPair)679 __STATIC_INLINE void LL_OPAMP_SetCalibrationSelection(OPAMP_TypeDef *OPAMPx, uint32_t TransistorsDiffPair)
680 {
681   /* Parameter used with mask "OPAMP_TRIMMING_SELECT_MASK" because            */
682   /* containing other bits reserved for other purpose.                        */
683   MODIFY_REG(OPAMPx->CSR, OPAMP_CSR_CALSEL, (TransistorsDiffPair & OPAMP_TRIMMING_SELECT_MASK));
684 }
685 
686 /**
687   * @brief  Get OPAMP offset to calibrate the selected transistors
688   *         differential pair NMOS or PMOS.
689   * @note   Preliminarily, OPAMP must be set in mode calibration
690   *         using function @ref LL_OPAMP_SetMode().
691   * @rmtoll CSR      CALSEL         LL_OPAMP_GetCalibrationSelection
692   * @param  OPAMPx OPAMP instance
693   * @retval Returned value can be one of the following values:
694   *         @arg @ref LL_OPAMP_TRIMMING_NMOS
695   *         @arg @ref LL_OPAMP_TRIMMING_PMOS
696   */
LL_OPAMP_GetCalibrationSelection(const OPAMP_TypeDef * OPAMPx)697 __STATIC_INLINE uint32_t LL_OPAMP_GetCalibrationSelection(const OPAMP_TypeDef *OPAMPx)
698 {
699   uint32_t CalibrationSelection = (uint32_t)(READ_BIT(OPAMPx->CSR, OPAMP_CSR_CALSEL));
700 
701   return (CalibrationSelection |
702           (((CalibrationSelection & OPAMP_CSR_CALSEL) == 0UL) ? OPAMP_OTR_TRIMOFFSETN : OPAMP_OTR_TRIMOFFSETP));
703 }
704 
705 /**
706   * @brief  Get OPAMP calibration result of toggling output.
707   * @note   This functions returns:
708   *         0 if OPAMP calibration output is reset
709   *         1 if OPAMP calibration output is set
710   * @rmtoll CSR      CALOUT         LL_OPAMP_IsCalibrationOutputSet
711   * @param  OPAMPx OPAMP instance
712   * @retval State of bit (1 or 0).
713   */
LL_OPAMP_IsCalibrationOutputSet(const OPAMP_TypeDef * OPAMPx)714 __STATIC_INLINE uint32_t LL_OPAMP_IsCalibrationOutputSet(const OPAMP_TypeDef *OPAMPx)
715 {
716   return ((READ_BIT(OPAMPx->CSR, OPAMP_CSR_CALOUT) == OPAMP_CSR_CALOUT) ? 1UL : 0UL);
717 }
718 
719 /**
720   * @brief  Set OPAMP trimming factor for the selected transistors
721   *         differential pair NMOS or PMOS, corresponding to the selected
722   *         power mode.
723   * @rmtoll OTR      TRIMOFFSETN    LL_OPAMP_SetTrimmingValue
724   * @rmtoll OTR      TRIMOFFSETP    LL_OPAMP_SetTrimmingValue
725   * @rmtoll LPOTR    TRIMLPOFFSETN  LL_OPAMP_SetTrimmingValue
726   * @rmtoll LPOTR    TRIMLPOFFSETP  LL_OPAMP_SetTrimmingValue
727   * @param  OPAMPx OPAMP instance
728   * @param  PowerMode This parameter can be one of the following values:
729   *         @arg @ref LL_OPAMP_POWERMODE_NORMALPOWER_NORMALSPEED
730   *         @arg @ref LL_OPAMP_POWERMODE_LOWPOWER_NORMALSPEED
731   *         @arg @ref LL_OPAMP_POWERMODE_NORMALPOWER_HIGHSPEED
732   *         @arg @ref LL_OPAMP_POWERMODE_LOWPOWER_HIGHSPEED
733   * @param  TransistorsDiffPair This parameter can be one of the following values:
734   *         @arg @ref LL_OPAMP_TRIMMING_NMOS
735   *         @arg @ref LL_OPAMP_TRIMMING_PMOS
736   * @param  TrimmingValue 0x00...0x1F
737   * @retval None
738   */
LL_OPAMP_SetTrimmingValue(OPAMP_TypeDef * OPAMPx,uint32_t PowerMode,uint32_t TransistorsDiffPair,uint32_t TrimmingValue)739 __STATIC_INLINE void LL_OPAMP_SetTrimmingValue(OPAMP_TypeDef *OPAMPx, uint32_t PowerMode, uint32_t TransistorsDiffPair,
740                                                uint32_t TrimmingValue)
741 {
742   __IO uint32_t *preg = __OPAMP_PTR_REG_OFFSET(OPAMPx->OTR, (PowerMode & OPAMP_POWERMODE_OTR_REGOFFSET_MASK));
743 
744   /* Set bits with position in register depending on parameter                */
745   /* "TransistorsDiffPair".                                                   */
746   /* Parameter used with mask "OPAMP_TRIMMING_VALUE_MASK" because             */
747   /* containing other bits reserved for other purpose.                        */
748   MODIFY_REG(*preg,
749              (TransistorsDiffPair & OPAMP_TRIMMING_VALUE_MASK),
750              TrimmingValue << (
751                (TransistorsDiffPair == LL_OPAMP_TRIMMING_NMOS) ? OPAMP_OTR_TRIMOFFSETN_Pos : OPAMP_OTR_TRIMOFFSETP_Pos)
752             );
753 }
754 
755 /**
756   * @brief  Get OPAMP trimming factor for the selected transistors
757   *         differential pair NMOS or PMOS, corresponding to the selected
758   *         power mode.
759   * @rmtoll OTR      TRIMOFFSETN    LL_OPAMP_GetTrimmingValue
760   * @rmtoll OTR      TRIMOFFSETP    LL_OPAMP_GetTrimmingValue
761   * @rmtoll LPOTR    TRIMLPOFFSETN  LL_OPAMP_GetTrimmingValue
762   * @rmtoll LPOTR    TRIMLPOFFSETP  LL_OPAMP_GetTrimmingValue
763   * @param  OPAMPx OPAMP instance
764   * @param  PowerMode This parameter can be one of the following values:
765   *         @arg @ref LL_OPAMP_POWERMODE_NORMALPOWER_NORMALSPEED
766   *         @arg @ref LL_OPAMP_POWERMODE_LOWPOWER_NORMALSPEED
767   *         @arg @ref LL_OPAMP_POWERMODE_NORMALPOWER_HIGHSPEED
768   *         @arg @ref LL_OPAMP_POWERMODE_LOWPOWER_HIGHSPEED
769   * @param  TransistorsDiffPair This parameter can be one of the following values:
770   *         @arg @ref LL_OPAMP_TRIMMING_NMOS
771   *         @arg @ref LL_OPAMP_TRIMMING_PMOS
772   * @retval 0x0...0x1F
773   */
LL_OPAMP_GetTrimmingValue(const OPAMP_TypeDef * OPAMPx,uint32_t PowerMode,uint32_t TransistorsDiffPair)774 __STATIC_INLINE uint32_t LL_OPAMP_GetTrimmingValue(const OPAMP_TypeDef *OPAMPx, uint32_t PowerMode,
775                                                    uint32_t TransistorsDiffPair)
776 {
777   const __IO uint32_t *preg = __OPAMP_PTR_REG_OFFSET(OPAMPx->OTR, (PowerMode & OPAMP_POWERMODE_OTR_REGOFFSET_MASK));
778 
779   /* Retrieve bits with position in register depending on parameter           */
780   /* "TransistorsDiffPair".                                                   */
781   /* Parameter used with mask "OPAMP_TRIMMING_VALUE_MASK" because             */
782   /* containing other bits reserved for other purpose.                        */
783   return (uint32_t)(
784            READ_BIT(*preg, (TransistorsDiffPair & OPAMP_TRIMMING_VALUE_MASK)) >>
785            (
786              (TransistorsDiffPair == LL_OPAMP_TRIMMING_NMOS) ? OPAMP_OTR_TRIMOFFSETN_Pos : OPAMP_OTR_TRIMOFFSETP_Pos));
787 }
788 
789 /**
790   * @}
791   */
792 
793 /** @defgroup OPAMP_LL_EF_OPERATION Operation on OPAMP instance
794   * @{
795   */
796 /**
797   * @brief  Enable OPAMP instance.
798   * @note   After enable from off state, OPAMP requires a delay
799   *         to fulfill wake up time specification.
800   *         Refer to device datasheet, parameter "tWAKEUP".
801   * @rmtoll CSR      OPAMPXEN       LL_OPAMP_Enable
802   * @param  OPAMPx OPAMP instance
803   * @retval None
804   */
LL_OPAMP_Enable(OPAMP_TypeDef * OPAMPx)805 __STATIC_INLINE void LL_OPAMP_Enable(OPAMP_TypeDef *OPAMPx)
806 {
807   SET_BIT(OPAMPx->CSR, OPAMP_CSR_OPAEN);
808 }
809 
810 /**
811   * @brief  Disable OPAMP instance.
812   * @rmtoll CSR      OPAMPXEN       LL_OPAMP_Disable
813   * @param  OPAMPx OPAMP instance
814   * @retval None
815   */
LL_OPAMP_Disable(OPAMP_TypeDef * OPAMPx)816 __STATIC_INLINE void LL_OPAMP_Disable(OPAMP_TypeDef *OPAMPx)
817 {
818   CLEAR_BIT(OPAMPx->CSR, OPAMP_CSR_OPAEN);
819 }
820 
821 /**
822   * @brief  Get OPAMP instance enable state
823   *         (0: OPAMP is disabled, 1: OPAMP is enabled)
824   * @rmtoll CSR      OPAMPXEN       LL_OPAMP_IsEnabled
825   * @param  OPAMPx OPAMP instance
826   * @retval State of bit (1 or 0).
827   */
LL_OPAMP_IsEnabled(const OPAMP_TypeDef * OPAMPx)828 __STATIC_INLINE uint32_t LL_OPAMP_IsEnabled(const OPAMP_TypeDef *OPAMPx)
829 {
830   return ((READ_BIT(OPAMPx->CSR, OPAMP_CSR_OPAEN) == (OPAMP_CSR_OPAEN)) ? 1UL : 0UL);
831 }
832 
833 /**
834   * @}
835   */
836 
837 #if defined(USE_FULL_LL_DRIVER)
838 /** @defgroup OPAMP_LL_EF_Init Initialization and de-initialization functions
839   * @{
840   */
841 
842 ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef *OPAMPx);
843 ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, const LL_OPAMP_InitTypeDef *OPAMP_InitStruct);
844 void        LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct);
845 
846 /**
847   * @}
848   */
849 #endif /* USE_FULL_LL_DRIVER */
850 
851 /**
852   * @}
853   */
854 
855 /**
856   * @}
857   */
858 
859 #endif /* OPAMP1 || OPAMP2 */
860 
861 /**
862   * @}
863   */
864 
865 /**
866   * @}
867   */
868 
869 #ifdef __cplusplus
870 }
871 #endif
872 
873 #endif /* STM32U5xx_LL_OPAMP_H */
874 
875