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