1 /**
2   ******************************************************************************
3   * @file    stm32u0xx_ll_opamp.c
4   * @author  MCD Application Team
5   * @brief   OPAMP LL module driver
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 #if defined(USE_FULL_LL_DRIVER)
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32u0xx_ll_opamp.h"
22 
23 #ifdef  USE_FULL_ASSERT
24 #include "stm32_assert.h"
25 #else
26 #define assert_param(expr) ((void)0U)
27 #endif /* USE_FULL_ASSERT */
28 
29 /** @addtogroup STM32U0xx_LL_Driver
30   * @{
31   */
32 
33 #if defined (OPAMP1)
34 
35 /** @addtogroup OPAMP_LL OPAMP
36   * @{
37   */
38 
39 /* Private types -------------------------------------------------------------*/
40 /* Private variables ---------------------------------------------------------*/
41 /* Private constants ---------------------------------------------------------*/
42 /* Private macros ------------------------------------------------------------*/
43 
44 /** @addtogroup OPAMP_LL_Private_Macros
45   * @{
46   */
47 
48 /* Check of parameters for configuration of OPAMP hierarchical scope:         */
49 /* OPAMP instance.                                                            */
50 
51 #define IS_LL_OPAMP_POWER_MODE(__POWER_MODE__)    (((__POWER_MODE__) == LL_OPAMP_POWERMODE_NORMALPOWER_NORMALSPEED)  ||\
52                                                    ((__POWER_MODE__) == LL_OPAMP_POWERMODE_LOWPOWER_NORMALSPEED))
53 
54 
55 #define IS_LL_OPAMP_FUNCTIONAL_MODE(__FUNCTIONAL_MODE__)     (((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_STANDALONE) ||\
56                                                               ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_FOLLOWER)   ||\
57                                                               ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA))
58 
59 /* Note: Comparator non-inverting inputs parameters are the same on all       */
60 /*       OPAMP instances.                                                     */
61 /*       However, comparator instance kept as macro parameter for             */
62 /*       compatibility with other STM32 families.                             */
63 #define IS_LL_OPAMP_INPUT_NONINVERTING(__OPAMPX__, __INPUT_NONINVERTING__)     \
64   (   ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0)               \
65       || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1)          \
66   )
67 
68 /* Note: Comparator non-inverting inputs parameters are the same on all       */
69 /*       OPAMP instances.                                                     */
70 /*       However, comparator instance kept as macro parameter for             */
71 /*       compatibility with other STM32 families.                             */
72 #define IS_LL_OPAMP_INPUT_INVERTING(__OPAMPX__, __INPUT_INVERTING__)           \
73   (   ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO0)                     \
74       || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_CONNECT_NO)           \
75   )
76 
77 /**
78   * @}
79   */
80 
81 
82 /* Private function prototypes -----------------------------------------------*/
83 
84 /* Exported functions --------------------------------------------------------*/
85 /** @addtogroup OPAMP_LL_Exported_Functions
86   * @{
87   */
88 
89 /** @addtogroup OPAMP_LL_EF_Init
90   * @{
91   */
92 
93 /**
94   * @brief  De-initialize registers of the selected OPAMP instance
95   *         to their default reset values.
96   * @param  OPAMPx OPAMP instance
97   * @retval An ErrorStatus enumeration value:
98   *          - SUCCESS: OPAMP registers are de-initialized
99   *          - ERROR: OPAMP registers are not de-initialized
100   */
LL_OPAMP_DeInit(OPAMP_TypeDef * OPAMPx)101 ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef *OPAMPx)
102 {
103   ErrorStatus status = SUCCESS;
104 
105   /* Check the parameters */
106   assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
107 
108   LL_OPAMP_WriteReg(OPAMPx, CSR, 0x00000000U);
109 
110   return status;
111 }
112 
113 /**
114   * @brief  Initialize some features of OPAMP instance.
115   * @note   This function reset bit of calibration mode to ensure
116   *         to be in functional mode, in order to have OPAMP parameters
117   *         (inputs selection, ...) set with the corresponding OPAMP mode
118   *         to be effective.
119   * @note   This function configures features of the selected OPAMP instance.
120   *         Some features are also available at scope OPAMP common instance
121   *         (common to several OPAMP instances).
122   * @param  OPAMPx OPAMP instance
123   * @param  OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
124   * @retval An ErrorStatus enumeration value:
125   *          - SUCCESS: OPAMP registers are initialized
126   *          - ERROR: OPAMP registers are not initialized
127   */
LL_OPAMP_Init(OPAMP_TypeDef * OPAMPx,const LL_OPAMP_InitTypeDef * OPAMP_InitStruct)128 ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, const LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
129 {
130   /* Check the parameters */
131   assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
132   assert_param(IS_LL_OPAMP_POWER_MODE(OPAMP_InitStruct->PowerMode));
133   assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
134   assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMPx, OPAMP_InitStruct->InputNonInverting));
135 
136   /* Note: OPAMP inverting input can be used with OPAMP in mode standalone    */
137   /*       or PGA with external capacitors for filtering circuit.             */
138   /*       Otherwise (OPAMP in mode follower), OPAMP inverting input is       */
139   /*       not used (not connected to GPIO pin).                              */
140   if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
141   {
142     assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMPx, OPAMP_InitStruct->InputInverting));
143   }
144 
145   /* Configuration of OPAMP instance :                                        */
146   /*  - PowerMode                                                             */
147   /*  - Functional mode                                                       */
148   /*  - Input non-inverting                                                   */
149   /*  - Input inverting                                                       */
150   /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode.      */
151   if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
152   {
153     MODIFY_REG(OPAMPx->CSR,
154                OPAMP_CSR_OPALPM
155                | OPAMP_CSR_OPAMODE
156                | OPAMP_CSR_CALON
157                | OPAMP_CSR_VM_SEL
158                | OPAMP_CSR_VP_SEL
159                ,
160                OPAMP_InitStruct->PowerMode
161                | OPAMP_InitStruct->FunctionalMode
162                | OPAMP_InitStruct->InputNonInverting
163                | OPAMP_InitStruct->InputInverting
164               );
165   }
166   else
167   {
168     MODIFY_REG(OPAMPx->CSR,
169                OPAMP_CSR_OPALPM
170                | OPAMP_CSR_OPAMODE
171                | OPAMP_CSR_CALON
172                | OPAMP_CSR_VM_SEL
173                | OPAMP_CSR_VP_SEL
174                ,
175                OPAMP_InitStruct->PowerMode
176                | LL_OPAMP_MODE_FOLLOWER
177                | OPAMP_InitStruct->InputNonInverting
178                | LL_OPAMP_INPUT_INVERT_CONNECT_NO
179               );
180   }
181 
182   /* Set the power supply range to high for performance purpose    */
183   /* The OPAMP_CSR_OPARANGE is common configuration for all OPAMPs */
184   /* bit OPAMP_CSR_OPARANGE applies for both OPAMPs                */
185   MODIFY_REG(OPAMP1->CSR, OPAMP_CSR_OPARANGE, OPAMP_CSR_OPARANGE);
186 
187   return SUCCESS;
188 }
189 
190 /**
191   * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
192   * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
193   *                         whose fields will be set to default values.
194   * @retval None
195   */
LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef * OPAMP_InitStruct)196 void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
197 {
198   /* Set OPAMP_InitStruct fields to default values */
199   OPAMP_InitStruct->PowerMode         = LL_OPAMP_POWERMODE_NORMALPOWER_NORMALSPEED;
200   OPAMP_InitStruct->FunctionalMode    = LL_OPAMP_MODE_FOLLOWER;
201   OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
202   /* Note: Parameter discarded if OPAMP in functional mode follower,          */
203   /*       set anyway to its default value.                                   */
204   OPAMP_InitStruct->InputInverting    = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
205 }
206 
207 /**
208   * @}
209   */
210 
211 /**
212   * @}
213   */
214 
215 /**
216   * @}
217   */
218 
219 #endif /* OPAMP1 */
220 
221 /**
222   * @}
223   */
224 
225 #endif /* USE_FULL_LL_DRIVER */
226 
227