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