1 /**
2   ******************************************************************************
3   * @file    stm32l1xx_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 "stm32l1xx_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 STM32L1xx_LL_Driver
30   * @{
31   */
32 
33 #if defined (OPAMP1) || defined (OPAMP2) || defined (OPAMP3)
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_NORMAL)                          \
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   )
59 
60 /* Note: Comparator non-inverting inputs parameters are different             */
61 /*       depending on OPAMP instance.                                         */
62 #if defined(OPAMP3)
63 #define IS_LL_OPAMP_INPUT_NONINVERTING(__OPAMPX__, __INPUT_NONINVERTING__)        \
64   (((__OPAMPX__) == OPAMP1)                                                       \
65     ? (                                                                           \
66           ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0)              \
67        || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1)            \
68       )                                                                           \
69       :                                                                           \
70       (((__OPAMPX__) == OPAMP2)                                                   \
71         ? (                                                                       \
72               ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0)          \
73            || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1)        \
74            || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH2)        \
75           )                                                                       \
76           :                                                                       \
77           (                                                                       \
78               ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0)          \
79            || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH2_OPAMP3) \
80           )                                                                       \
81       )                                                                           \
82   )
83 #else
84 #define IS_LL_OPAMP_INPUT_NONINVERTING(__OPAMPX__, __INPUT_NONINVERTING__)     \
85   (((__OPAMPX__) == OPAMP1)                                                    \
86     ? (                                                                        \
87           ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0)           \
88        || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1)         \
89       )                                                                        \
90       :                                                                        \
91       (                                                                        \
92           ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0)           \
93        || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH1)         \
94        || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINV_DAC1_CH2)         \
95       )                                                                        \
96   )
97 #endif
98 
99 /* Note: Comparator non-inverting inputs parameters are the same on all       */
100 /*       OPAMP instances.                                                     */
101 /*       However, comparator instance kept as macro parameter for             */
102 /*       compatibility with other STM32 families.                             */
103 #define IS_LL_OPAMP_INPUT_INVERTING(__OPAMPX__, __INPUT_INVERTING__)           \
104   (   ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO0)                     \
105    || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO1)                     \
106    || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_CONNECT_NO)              \
107   )
108 
109 /**
110   * @}
111   */
112 
113 
114 /* Private function prototypes -----------------------------------------------*/
115 
116 /* Exported functions --------------------------------------------------------*/
117 /** @addtogroup OPAMP_LL_Exported_Functions
118   * @{
119   */
120 
121 /** @addtogroup OPAMP_LL_EF_Init
122   * @{
123   */
124 
125 /**
126   * @brief  De-initialize registers of the selected OPAMP instance
127   *         to their default reset values.
128   * @param  OPAMPx OPAMP instance
129   * @retval An ErrorStatus enumeration value:
130   *          - SUCCESS: OPAMP registers are de-initialized
131   *          - ERROR: OPAMP registers are not de-initialized
132   */
LL_OPAMP_DeInit(OPAMP_TypeDef * OPAMPx)133 ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef* OPAMPx)
134 {
135   ErrorStatus status = SUCCESS;
136 
137   /* Check the parameters */
138   assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
139 
140   MODIFY_REG(OPAMP->CSR,
141                (OPAMP_CSR_S3SEL1 | OPAMP_CSR_S4SEL1 | OPAMP_CSR_S5SEL1 | OPAMP_CSR_S6SEL1 | OPAMP_CSR_OPA1CAL_L | OPAMP_CSR_OPA1CAL_H | OPAMP_CSR_OPA1LPM) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
142              | (OPAMP_CSR_ANAWSEL1) << __OPAMP_INSTANCE_DECIMAL(OPAMPx)
143              | (OPAMP_CSR_S7SEL2 * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx)) ,
144              (OPAMP_CSR_OPA1PD) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
145             );
146   return status;
147 }
148 
149 /**
150   * @brief  Initialize some features of OPAMP instance.
151   * @note   This function reset bit of calibration mode to ensure
152   *         to be in functional mode, in order to have OPAMP parameters
153   *         (inputs selection, ...) set with the corresponding OPAMP mode
154   *         to be effective.
155   * @note   This function configures features of the selected OPAMP instance.
156   *         Some features are also available at scope OPAMP common instance
157   *         (common to several OPAMP instances).
158   *         Refer to functions having argument "OPAMPxy_COMMON" as parameter.
159   * @param  OPAMPx OPAMP instance
160   * @param  OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
161   * @retval An ErrorStatus enumeration value:
162   *          - SUCCESS: OPAMP registers are initialized
163   *          - ERROR: OPAMP registers are not initialized
164   */
LL_OPAMP_Init(OPAMP_TypeDef * OPAMPx,LL_OPAMP_InitTypeDef * OPAMP_InitStruct)165 ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
166 {
167   /* Check the parameters */
168   assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
169   assert_param(IS_LL_OPAMP_POWER_MODE(OPAMP_InitStruct->PowerMode));
170   assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
171   assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMPx, OPAMP_InitStruct->InputNonInverting));
172 
173   /* Note: OPAMP inverting input can be used with OPAMP in mode standalone.   */
174   /*       Otherwise (OPAMP in mode follower), OPAMP inverting input is       */
175   /*       not used (not connected to GPIO pin).                              */
176   if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
177   {
178     assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMPx, OPAMP_InitStruct->InputInverting));
179   }
180 
181   /* Configuration of OPAMP instance :                                        */
182   /*  - PowerMode                                                             */
183   /*  - Functional mode                                                       */
184   /*  - Input non-inverting                                                   */
185   /*  - Input inverting                                                       */
186   /* Note: Bits OPAMP_CSR_OPAxCAL_y reset to ensure to be in functional mode  */
187   if(OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
188   {
189     MODIFY_REG(OPAMP->CSR,
190                ((
191                    OPAMP_CSR_OPA1LPM
192                  | OPAMP_CSR_S3SEL1
193                  | OPAMP_CSR_OPA1CAL_H
194                  | OPAMP_CSR_OPA1CAL_L
195                  | OPAMP_CSR_S5SEL1
196                  | OPAMP_CSR_S6SEL1
197                  | OPAMP_CSR_S4SEL1
198                 ) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
199                )
200                | ((OPAMP_CSR_ANAWSEL1) << __OPAMP_INSTANCE_DECIMAL(OPAMPx))
201                | (OPAMP_CSR_S7SEL2 * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx))
202               ,
203                ((
204                    (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
205                  | OPAMP_InitStruct->FunctionalMode
206                  | OPAMP_InitStruct->InputNonInverting
207                  | (OPAMP_InitStruct->InputInverting & OPAMP_CSR_S4SEL1)
208                 ) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
209                )
210                | ((OPAMP_InitStruct->InputInverting & OPAMP_CSR_ANAWSEL1) << __OPAMP_INSTANCE_DECIMAL(OPAMPx))
211                | ((OPAMP_InitStruct->InputNonInverting & OPAMP_CSR_S7SEL2) * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx))
212               );
213   }
214   else
215   {
216     MODIFY_REG(OPAMP->CSR,
217                ((
218                    OPAMP_CSR_OPA1LPM
219                  | OPAMP_CSR_OPA1CAL_H
220                  | OPAMP_CSR_OPA1CAL_L
221                  | OPAMP_CSR_S5SEL1
222                  | OPAMP_CSR_S6SEL1
223                  | OPAMP_CSR_S4SEL1
224                 ) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
225                )
226                | ((OPAMP_CSR_ANAWSEL1) << __OPAMP_INSTANCE_DECIMAL(OPAMPx))
227                | (OPAMP_CSR_S7SEL2 * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx))
228               ,
229                ((
230                    (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
231                  | OPAMP_InitStruct->FunctionalMode
232                  | OPAMP_InitStruct->InputNonInverting
233                  | OPAMP_CSR_S3SEL1
234                 ) << __OPAMP_INSTANCE_BITOFFSET(OPAMPx)
235                )
236                | ((OPAMP_InitStruct->InputNonInverting & OPAMP_CSR_S7SEL2) * __OPAMP_IS_INSTANCE_OPAMP2(OPAMPx))
237               );
238   }
239   return SUCCESS;
240 }
241 
242 /**
243   * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
244   * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
245   *                         whose fields will be set to default values.
246   * @retval None
247   */
LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef * OPAMP_InitStruct)248 void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
249 {
250   /* Set OPAMP_InitStruct fields to default values */
251   OPAMP_InitStruct->PowerMode         = LL_OPAMP_POWERMODE_NORMAL;
252   OPAMP_InitStruct->FunctionalMode    = LL_OPAMP_MODE_FOLLOWER;
253   OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
254   /* Note: Parameter discarded if OPAMP in functional mode follower,          */
255   /*       set anyway to its default value.                                   */
256   OPAMP_InitStruct->InputInverting    = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
257 }
258 
259 /**
260   * @}
261   */
262 
263 /**
264   * @}
265   */
266 
267 /**
268   * @}
269   */
270 
271 #endif /* OPAMP1 || OPAMP2 || OPAMP3 */
272 
273 /**
274   * @}
275   */
276 
277 #endif /* USE_FULL_LL_DRIVER */
278 
279