xref: /btstack/port/stm32-l451-miromico-sx1280/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_opamp.c (revision 2fd737d36a1de5d778cacc671d4b4d8c4f3fed82)
1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_ll_opamp.c
4   * @author  MCD Application Team
5   * @brief   OPAMP LL module driver
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
10   * All rights reserved.</center></h2>
11   *
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
16   *
17   ******************************************************************************
18   */
19 #if defined(USE_FULL_LL_DRIVER)
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32l4xx_ll_opamp.h"
23 
24 #ifdef  USE_FULL_ASSERT
25   #include "stm32_assert.h"
26 #else
27   #define assert_param(expr) ((void)0U)
28 #endif
29 
30 /** @addtogroup STM32L4xx_LL_Driver
31   * @{
32   */
33 
34 #if defined (OPAMP1) || defined (OPAMP2)
35 
36 /** @addtogroup OPAMP_LL OPAMP
37   * @{
38   */
39 
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private constants ---------------------------------------------------------*/
43 /* Private macros ------------------------------------------------------------*/
44 
45 /** @addtogroup OPAMP_LL_Private_Macros
46   * @{
47   */
48 
49 /* Check of parameters for configuration of OPAMP hierarchical scope:         */
50 /* OPAMP instance.                                                            */
51 
52 #define IS_LL_OPAMP_POWER_MODE(__POWER_MODE__)                                 \
53   (   ((__POWER_MODE__) == LL_OPAMP_POWERMODE_NORMAL)                          \
54    || ((__POWER_MODE__) == LL_OPAMP_POWERMODE_LOWPOWER))
55 
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   )
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   *         Refer to functions having argument "OPAMPxy_COMMON" as parameter.
127   * @param  OPAMPx OPAMP instance
128   * @param  OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
129   * @retval An ErrorStatus enumeration value:
130   *          - SUCCESS: OPAMP registers are initialized
131   *          - ERROR: OPAMP registers are not initialized
132   */
LL_OPAMP_Init(OPAMP_TypeDef * OPAMPx,LL_OPAMP_InitTypeDef * OPAMP_InitStruct)133 ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
134 {
135   /* Check the parameters */
136   assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
137   assert_param(IS_LL_OPAMP_POWER_MODE(OPAMP_InitStruct->PowerMode));
138   assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
139   assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMPx, OPAMP_InitStruct->InputNonInverting));
140 
141   /* Note: OPAMP inverting input can be used with OPAMP in mode standalone    */
142   /*       or PGA with external capacitors for filtering circuit.             */
143   /*       Otherwise (OPAMP in mode follower), OPAMP inverting input is       */
144   /*       not used (not connected to GPIO pin).                              */
145   if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
146   {
147     assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMPx, OPAMP_InitStruct->InputInverting));
148   }
149 
150   /* Configuration of OPAMP instance :                                        */
151   /*  - PowerMode                                                             */
152   /*  - Functional mode                                                       */
153   /*  - Input non-inverting                                                   */
154   /*  - Input inverting                                                       */
155   /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode.      */
156   if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
157   {
158     MODIFY_REG(OPAMPx->CSR,
159                  OPAMP_CSR_OPALPM
160                | OPAMP_CSR_OPAMODE
161                | OPAMP_CSR_CALON
162                | OPAMP_CSR_VMSEL
163                | OPAMP_CSR_VPSEL
164               ,
165                  (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
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_VMSEL
178                | OPAMP_CSR_VPSEL
179               ,
180                  (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
181                | LL_OPAMP_MODE_FOLLOWER
182                | OPAMP_InitStruct->InputNonInverting
183                | LL_OPAMP_INPUT_INVERT_CONNECT_NO
184               );
185   }
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_NORMAL;
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 || OPAMP2 */
220 
221 /**
222   * @}
223   */
224 
225 #endif /* USE_FULL_LL_DRIVER */
226 
227 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
228