1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_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 "stm32l4xx_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 STM32L4xx_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__)                                 \
52   (   ((__POWER_MODE__) == LL_OPAMP_POWERMODE_NORMALPOWER)                     \
53    || ((__POWER_MODE__) == LL_OPAMP_POWERMODE_LOWPOWER))
54 
55 #define IS_LL_OPAMP_FUNCTIONAL_MODE(__FUNCTIONAL_MODE__)                       \
56   (   ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_STANDALONE)                      \
57    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_FOLLOWER)                        \
58    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA)                             \
59   )
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_NONINV_DAC1_CH1)             \
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   *         Refer to functions having argument "OPAMPxy_COMMON" as parameter.
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,LL_OPAMP_InitTypeDef * OPAMP_InitStruct)132 ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, 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_VMSEL
162                | OPAMP_CSR_VPSEL
163               ,
164                  (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
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_VMSEL
177                | OPAMP_CSR_VPSEL
178               ,
179                  (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
180                | LL_OPAMP_MODE_FOLLOWER
181                | OPAMP_InitStruct->InputNonInverting
182                | LL_OPAMP_INPUT_INVERT_CONNECT_NO
183               );
184   }
185 
186   return SUCCESS;
187 }
188 
189 /**
190   * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
191   * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
192   *                         whose fields will be set to default values.
193   * @retval None
194   */
LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef * OPAMP_InitStruct)195 void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
196 {
197   /* Set OPAMP_InitStruct fields to default values */
198   OPAMP_InitStruct->PowerMode         = LL_OPAMP_POWERMODE_NORMALPOWER;
199   OPAMP_InitStruct->FunctionalMode    = LL_OPAMP_MODE_FOLLOWER;
200   OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
201   /* Note: Parameter discarded if OPAMP in functional mode follower,          */
202   /*       set anyway to its default value.                                   */
203   OPAMP_InitStruct->InputInverting    = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
204 }
205 
206 /**
207   * @}
208   */
209 
210 /**
211   * @}
212   */
213 
214 /**
215   * @}
216   */
217 
218 #endif /* OPAMP1 || OPAMP2 */
219 
220 /**
221   * @}
222   */
223 
224 #endif /* USE_FULL_LL_DRIVER */
225