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