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