1 /**
2   **********************************************************************************************************************
3   * @file    stm32h5xx_ll_opamp.c
4   * @author  MCD Application Team
5   * @brief   OPAMP LL module driver
6   **********************************************************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2023 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 "stm32h5xx_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 /* USE_FULL_ASSERT */
28 
29 /** @addtogroup STM32H5xx_LL_Driver
30   * @{
31   */
32 
33 #if defined (OPAMP1)
34 /** @addtogroup OPAMP_LL OPAMP
35   * @{
36   */
37 
38 /* Private types -----------------------------------------------------------------------------------------------------*/
39 /* Private variables -------------------------------------------------------------------------------------------------*/
40 /* Private constants -------------------------------------------------------------------------------------------------*/
41 /* Private macros ----------------------------------------------------------------------------------------------------*/
42 /** @addtogroup OPAMP_LL_Private_Macros
43   * @{
44   */
45 
46 /* Check of parameters for configuration of OPAMP hierarchical scope:         */
47 /* OPAMP instance.                                                            */
48 
49 #define IS_LL_OPAMP_POWER_MODE(__POWER_MODE__)                              \
50   (((__POWER_MODE__) == LL_OPAMP_POWERMODE_NORMAL)                          \
51    || ((__POWER_MODE__) == LL_OPAMP_POWERMODE_HIGHSPEED))
52 
53 #define IS_LL_OPAMP_FUNCTIONAL_MODE(__FUNCTIONAL_MODE__)                    \
54   (((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_STANDALONE)                      \
55    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_FOLLOWER)                     \
56    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA)                          \
57    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA_IO0)                      \
58    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA_IO0_BIAS)                 \
59    || ((__FUNCTIONAL_MODE__) == LL_OPAMP_MODE_PGA_IO0_IO1_BIAS)             \
60   )
61 
62 #define IS_LL_OPAMP_INPUT_NONINVERTING(__INPUT_NONINVERTING__)              \
63   (((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO0)               \
64    || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_IO1)            \
65    || ((__INPUT_NONINVERTING__) == LL_OPAMP_INPUT_NONINVERT_DAC)            \
66   )
67 
68 #define IS_LL_OPAMP_INPUT_INVERTING(__INPUT_INVERTING__)                    \
69   (((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO0)                     \
70    || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_IO1)                  \
71    || ((__INPUT_INVERTING__) == LL_OPAMP_INPUT_INVERT_CONNECT_NO)           \
72   )
73 
74 /**
75   * @}
76   */
77 
78 /* Private function prototypes ---------------------------------------------------------------------------------------*/
79 /* Exported functions ------------------------------------------------------------------------------------------------*/
80 /** @addtogroup OPAMP_LL_Exported_Functions
81   * @{
82   */
83 
84 /** @addtogroup OPAMP_LL_EF_Init
85   * @{
86   */
87 
88 /**
89   * @brief  De-initialize registers of the selected OPAMP instance
90   *         to their default reset values.
91   * @note   If comparator is locked, de-initialization by software is
92   *         not possible.
93   *         The only way to unlock the comparator is a device hardware reset.
94   * @param  OPAMPx OPAMP instance
95   * @retval An ErrorStatus enumeration value:
96   *          - SUCCESS: OPAMP registers are de-initialized
97   *          - ERROR: OPAMP registers are not de-initialized
98   */
LL_OPAMP_DeInit(OPAMP_TypeDef * OPAMPx)99 ErrorStatus LL_OPAMP_DeInit(OPAMP_TypeDef *OPAMPx)
100 {
101   ErrorStatus status = SUCCESS;
102 
103   /* Check the parameters */
104   assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
105 
106   LL_OPAMP_WriteReg(OPAMPx, CSR, 0x00000000U);
107 
108   return status;
109 }
110 
111 /**
112   * @brief  Initialize some features of OPAMP instance.
113   * @note   This function reset bit of calibration mode to ensure
114   *         to be in functional mode, in order to have OPAMP parameters
115   *         (inputs selection, ...) set with the corresponding OPAMP mode
116   *         to be effective.
117   * @param  OPAMPx OPAMP instance
118   * @param  OPAMP_InitStruct Pointer to a @ref LL_OPAMP_InitTypeDef structure
119   * @retval An ErrorStatus enumeration value:
120   *          - SUCCESS: OPAMP registers are initialized
121   *          - ERROR: OPAMP registers are not initialized
122   */
LL_OPAMP_Init(OPAMP_TypeDef * OPAMPx,const LL_OPAMP_InitTypeDef * OPAMP_InitStruct)123 ErrorStatus LL_OPAMP_Init(OPAMP_TypeDef *OPAMPx, const LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
124 {
125   ErrorStatus status = SUCCESS;
126 
127   /* Check the parameters */
128   assert_param(IS_OPAMP_ALL_INSTANCE(OPAMPx));
129   assert_param(IS_LL_OPAMP_POWER_MODE(OPAMP_InitStruct->PowerMode));
130   assert_param(IS_LL_OPAMP_FUNCTIONAL_MODE(OPAMP_InitStruct->FunctionalMode));
131   assert_param(IS_LL_OPAMP_INPUT_NONINVERTING(OPAMP_InitStruct->InputNonInverting));
132 
133   /* Note: OPAMP inverting input can be used with OPAMP in mode standalone    */
134   /*       or PGA with external capacitors for filtering circuit.             */
135   /*       Otherwise (OPAMP in mode follower), OPAMP inverting input is       */
136   /*       not used (not connected to GPIO pin).                              */
137   if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
138   {
139     assert_param(IS_LL_OPAMP_INPUT_INVERTING(OPAMP_InitStruct->InputInverting));
140   }
141 
142   /* Configuration of OPAMP instance :                                      */
143   /*  - PowerMode                                                           */
144   /*  - Functional mode                                                     */
145   /*  - Input non-inverting                                                 */
146   /*  - Input inverting                                                     */
147   /* Note: Bit OPAMP_CSR_CALON reset to ensure to be in functional mode.    */
148   if (OPAMP_InitStruct->FunctionalMode != LL_OPAMP_MODE_FOLLOWER)
149   {
150     MODIFY_REG(OPAMPx->CSR,
151                OPAMP_CSR_OPAHSM
152                | OPAMP_CSR_CALON
153                | OPAMP_CSR_VMSEL
154                | OPAMP_CSR_VPSEL
155                | OPAMP_CSR_PGGAIN_2 | OPAMP_CSR_PGGAIN_1
156                ,
157                (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
158                | OPAMP_InitStruct->FunctionalMode
159                | OPAMP_InitStruct->InputNonInverting
160                | OPAMP_InitStruct->InputInverting
161               );
162   }
163   else
164   {
165     MODIFY_REG(OPAMPx->CSR,
166                OPAMP_CSR_OPAHSM
167                | OPAMP_CSR_CALON
168                | OPAMP_CSR_VMSEL
169                | OPAMP_CSR_VPSEL
170                | OPAMP_CSR_PGGAIN_2 | OPAMP_CSR_PGGAIN_1
171                ,
172                (OPAMP_InitStruct->PowerMode & OPAMP_POWERMODE_CSR_BIT_MASK)
173                | LL_OPAMP_MODE_FOLLOWER
174                | OPAMP_InitStruct->InputNonInverting
175               );
176   }
177 
178   return status;
179 }
180 
181 /**
182   * @brief Set each @ref LL_OPAMP_InitTypeDef field to default value.
183   * @param OPAMP_InitStruct pointer to a @ref LL_OPAMP_InitTypeDef structure
184   *                         whose fields will be set to default values.
185   * @retval None
186   */
LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef * OPAMP_InitStruct)187 void LL_OPAMP_StructInit(LL_OPAMP_InitTypeDef *OPAMP_InitStruct)
188 {
189   /* Set OPAMP_InitStruct fields to default values */
190   OPAMP_InitStruct->PowerMode         = LL_OPAMP_POWERMODE_NORMAL;
191   OPAMP_InitStruct->FunctionalMode    = LL_OPAMP_MODE_FOLLOWER;
192   OPAMP_InitStruct->InputNonInverting = LL_OPAMP_INPUT_NONINVERT_IO0;
193   /* Note: Parameter discarded if OPAMP in functional mode follower,          */
194   /*       set anyway to its default value.                                   */
195   OPAMP_InitStruct->InputInverting    = LL_OPAMP_INPUT_INVERT_CONNECT_NO;
196 }
197 
198 /**
199   * @}
200   */
201 
202 /**
203   * @}
204   */
205 
206 /**
207   * @}
208   */
209 
210 #endif /* OPAMP1 */
211 
212 /**
213   * @}
214   */
215 
216 #endif /* USE_FULL_LL_DRIVER */
217