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