Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32G4 / Drivers / STM32G4xx_HAL_Driver / Src / stm32g4xx_ll_opamp.c
blob76392dd3ad356365b0133e1419e427eb380402e9
1 /**
2 ******************************************************************************
3 * @file stm32g4xx_ll_opamp.c
4 * @author MCD Application Team
5 * @brief OPAMP LL module driver
6 ******************************************************************************
7 * @attention
9 * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
10 * All rights reserved.</center></h2>
12 * This software component is licensed by ST under BSD 3-Clause license,
13 * the "License"; You may not use this file except in compliance with the
14 * License. You may obtain a copy of the License at:
15 * opensource.org/licenses/BSD-3-Clause
17 ******************************************************************************
19 #if defined(USE_FULL_LL_DRIVER)
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32g4xx_ll_opamp.h"
24 #ifdef USE_FULL_ASSERT
25 #include "stm32_assert.h"
26 #else
27 #define assert_param(expr) ((void)0U)
28 #endif
30 /** @addtogroup STM32G4xx_LL_Driver
31 * @{
34 #if defined (OPAMP1) || defined (OPAMP2) || defined (OPAMP3) || defined (OPAMP4) || defined (OPAMP5) || defined (OPAMP6)
36 /** @addtogroup OPAMP_LL OPAMP
37 * @{
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private constants ---------------------------------------------------------*/
43 /* Private macros ------------------------------------------------------------*/
45 /** @addtogroup OPAMP_LL_Private_Macros
46 * @{
49 /* Check of parameters for configuration of OPAMP hierarchical scope: */
50 /* OPAMP instance. */
52 #define IS_LL_OPAMP_POWER_MODE(__POWER_MODE__) \
53 ( ((__POWER_MODE__) == LL_OPAMP_POWERMODE_NORMAL) \
54 || ((__POWER_MODE__) == LL_OPAMP_POWERMODE_HIGHSPEED))
56 #define IS_LL_OPAMP_FUNCTIONAL_MODE(__FUNCTIONAL_MODE__) \
57 ( ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_STANDALONE) \
58 || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_FOLLOWER) \
59 || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA) \
60 || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA_IO0) \
61 || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA_IO0_BIAS) \
62 || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA_IO0_IO1_BIAS) \
65 #define IS_LL_OPAMP_INPUT_NONINVERTING(__INPUT_NONINVERTING__) \
66 ( ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0) \
67 || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO1) \
68 || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO2) \
69 || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO3) \
70 || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_DAC) \
73 #define IS_LL_OPAMP_INPUT_INVERTING(__INPUT_INVERTING__) \
74 ( ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO0) \
75 || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO1) \
76 || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_CONNECT_NO) \
79 /**
80 * @}
84 /* Private function prototypes -----------------------------------------------*/
86 /* Exported functions --------------------------------------------------------*/
87 /** @addtogroup OPAMP_LL_Exported_Functions
88 * @{
91 /** @addtogroup OPAMP_LL_EF_Init
92 * @{
95 /**
96 * @brief De-initialize registers of the selected OPAMP instance
97 * to their default reset values.
98 * @note If comparator is locked, de-initialization by software is
99 * not possible.
100 * The only way to unlock the comparator is a device hardware reset.
101 * @param OPAMPx OPAMP instance
102 * @retval An ErrorStatus enumeration value:
103 * - SUCCESS: OPAMP registers are de-initialized
104 * - ERROR: OPAMP registers are not de-initialized
106 ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef *OPAMPx)
108 ErrorStatus status = SUCCESS;
110 /* Check the parameters */
111 assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
113 /* Note: Hardware constraint (refer to description of this function): */
114 /* OPAMP instance must not be locked. */
115 if (LL_OPAMP_IsLocked(OPAMPx) == 0UL)
117 LL_OPAMP_WriteReg(OPAMPx, CSR, 0x00000000UL);
119 else
121 /* OPAMP instance is locked: de-initialization by software is */
122 /* not possible. */
123 /* The only way to unlock the OPAMP is a device hardware reset. */
124 status = ERROR;
127 /* Timer controlled mux mode register reset */
128 if (LL_OPAMP_IsTimerMuxLocked(OPAMPx) == 0UL)
130 LL_OPAMP_WriteReg(OPAMPx, TCMR, 0x00000000UL);
132 else if (LL_OPAMP_ReadReg(OPAMPx, TCMR) != 0x80000000UL)
134 /* OPAMP instance timer controlled mux is locked configured, deinit error */
135 /* The only way to unlock the OPAMP is a device hardware reset. */
136 status = ERROR;
138 else
140 /* OPAMP instance timer controlled mux is locked unconfigured, deinit OK */
143 return status;
147 * @brief Initialize some features of OPAMP instance.
148 * @note This function reset bit of calibration mode to ensure
149 * to be in functional mode, in order to have OPAMP parameters
150 * (inputs selection, ...) set with the corresponding OPAMP mode
151 * to be effective.
152 * @param OPAMPx OPAMP instance
153 * @param OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
154 * @retval An ErrorStatus enumeration value:
155 * - SUCCESS: OPAMP registers are initialized
156 * - ERROR: OPAMP registers are not initialized
158 ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
160 ErrorStatus status = SUCCESS;
162 /* Check the parameters */
163 assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
164 assert_param(IS_LL_OPAMP_POWER_MODE(OPAMP_InitStruct->PowerMode));
165 assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
166 assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMP_InitStruct->InputNonInverting));
168 /* Note: OPAMP inverting input can be used with OPAMP in mode standalone */
169 /* or PGA with external capacitors for filtering circuit. */
170 /* Otherwise (OPAMP in mode follower), OPAMP inverting input is */
171 /* not used (not connected to GPIO pin). */
172 if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
174 assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMP_InitStruct->InputInverting));
177 /* Note: Hardware constraint (refer to description of this function): */
178 /* OPAMP instance must not be locked. */
179 if (LL_OPAMP_IsLocked(OPAMPx) == 0U)
181 /* Configuration of OPAMP instance : */
182 /* - PowerMode */
183 /* - Functional mode */
184 /* - Input non-inverting */
185 /* - Input inverting */
186 /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode. */
187 if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
189 MODIFY_REG(OPAMPx->CSR,
190 OPAMP_CSR_HIGHSPEEDEN
191 | OPAMP_CSR_CALON
192 | OPAMP_CSR_VMSEL
193 | OPAMP_CSR_VPSEL
194 | OPAMP_CSR_PGGAIN_4 | OPAMP_CSR_PGGAIN_3
196 OPAMP_InitStruct->PowerMode
197 | OPAMP_InitStruct->FunctionalMode
198 | OPAMP_InitStruct->InputNonInverting
199 | OPAMP_InitStruct->InputInverting
202 else
204 MODIFY_REG(OPAMPx->CSR,
205 OPAMP_CSR_HIGHSPEEDEN
206 | OPAMP_CSR_CALON
207 | OPAMP_CSR_VMSEL
208 | OPAMP_CSR_VPSEL
209 | OPAMP_CSR_PGGAIN_4 | OPAMP_CSR_PGGAIN_3
211 OPAMP_InitStruct->PowerMode
212 | LL_OPAMP_MODE_FOLLOWER
213 | OPAMP_InitStruct->InputNonInverting
217 else
219 /* Initialization error: OPAMP instance is locked. */
220 status = ERROR;
223 return status;
227 * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
228 * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
229 * whose fields will be set to default values.
230 * @retval None
232 void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
234 /* Set OPAMP_InitStruct fields to default values */
235 OPAMP_InitStruct->PowerMode = LL_OPAMP_POWERMODE_NORMAL;
236 OPAMP_InitStruct->FunctionalMode = LL_OPAMP_MODE_FOLLOWER;
237 OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
238 /* Note: Parameter discarded if OPAMP in functional mode follower, */
239 /* set anyway to its default value. */
240 OPAMP_InitStruct->InputInverting = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
244 * @}
248 * @}
252 * @}
255 #endif /* OPAMP1 || OPAMP2 || OPAMP3 || OPAMP4 || OPAMP5 || OPAMP6 */
258 * @}
261 #endif /* USE_FULL_LL_DRIVER */
263 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/