1 /**
2   ******************************************************************************
3   * @file    stm32u0xx_hal_opamp.h
4   * @author  MCD Application Team
5   * @brief   Header file of OPAMP HAL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2021 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 
19 /* Define to prevent recursive inclusion -------------------------------------*/
20 #ifndef STM32U0xx_HAL_OPAMP_H
21 #define STM32U0xx_HAL_OPAMP_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32u0xx_hal_def.h"
29 
30 /** @addtogroup STM32U0xx_HAL_Driver
31   * @{
32   */
33 
34 /** @addtogroup OPAMP
35   * @{
36   */
37 
38 /* Exported types ------------------------------------------------------------*/
39 
40 /** @defgroup OPAMP_Exported_Types OPAMP Exported Types
41   * @{
42   */
43 
44 /**
45   * @brief  OPAMP Init structure definition
46   */
47 
48 typedef struct
49 {
50   uint32_t PowerSupplyRange;            /*!< Specifies the power supply range: above or under 2.4V.
51                                              This parameter is not used, but kept for compatibility with other families.
52                                              Caution: Whatever this parameter, the effective PowerSupplyRange is
53                                              forced to OPAMP_POWERSUPPLY_HIGH in HAL_OPAMP_Init.
54                                              This parameter applies to the both OPAMP instances. */
55 
56   uint32_t PowerMode;                   /*!< Specifies the power mode Normal or Low-Power and the speed mode Normal or High.
57                                              This parameter must be a value of @ref OPAMP_PowerMode */
58 
59   uint32_t Mode;                        /*!< Specifies the OPAMP mode (Standalone, Follower or PGA)
60                                              This parameter must be a value of @ref OPAMP_Mode */
61 
62   uint32_t InvertingInput;              /*!< Specifies the inverting input in Standalone & PGA modes.
63                                              In Follower mode this parameter is Not Applicable.
64                                              This parameter must be a value of @ref OPAMP_InvertingInput */
65 
66   uint32_t NonInvertingInput;           /*!< Specifies the non inverting input of the opamp.
67                                              This parameter must be a value of @ref OPAMP_NonInvertingInput */
68 
69   uint32_t PgaGain;                     /*!< Specifies the gain (2, 4, 8 or 16 ) in PGA mode
70                                              i.e. when mode is OPAMP_PGA_MODE.
71                                              This parameter must be a value of @ref OPAMP_PgaGain */
72 
73   uint32_t UserTrimming;                /*!< Specifies the trimming mode is either factory or user trimming
74                                              This parameter must be a value of @ref OPAMP_UserTrimming.*/
75 
76   uint32_t TrimmingValueP;              /*!< Specifies the offset trimming value (PMOS)
77                                              i.e. when UserTrimming is OPAMP_TRIMMING_USER.
78                                              This parameter must be a number between Min_Data = 0 and Max_Data = 31
79                                              16 is typical default value */
80 
81   uint32_t TrimmingValueN;              /*!< Specifies the offset trimming value (NMOS)
82                                              i.e. when UserTrimming is OPAMP_TRIMMING_USER.
83                                              This parameter must be a number between Min_Data = 0 and Max_Data = 31
84                                              16 is typical default value */
85 
86   uint32_t TrimmingValuePLowPower;      /*!< Specifies the offset trimming value (PMOS) in lowpower mode
87                                              i.e. when UserTrimming is OPAMP_TRIMMING_USER.
88                                              This parameter must be a number between Min_Data = 0 and Max_Data = 31
89                                              16 is typical default value */
90 
91   uint32_t TrimmingValueNLowPower;      /*!< Specifies the offset trimming value (NMOS) in lowpower mode
92                                              i.e. when UserTrimming is OPAMP_TRIMMING_USER.
93                                              This parameter must be a number between Min_Data = 0 and Max_Data = 31
94                                              16 is typical default value */
95 
96 } OPAMP_InitTypeDef;
97 
98 /**
99   * @brief  HAL State structures definition
100   */
101 
102 typedef enum
103 {
104   HAL_OPAMP_STATE_RESET               = 0x00000000, /*!< OPAMP is not yet Initialized                                      */
105   HAL_OPAMP_STATE_READY               = 0x00000001, /*!< OPAMP is initialized and ready for use                            */
106   HAL_OPAMP_STATE_CALIBBUSY           = 0x00000002, /*!< OPAMP is enabled in auto calibration mode                         */
107   HAL_OPAMP_STATE_BUSY                = 0x00000004, /*!< OPAMP is enabled and running in normal mode                       */
108   HAL_OPAMP_STATE_BUSYLOCKED          = 0x00000005  /*!< OPAMP is locked. Only system reset allows reconfiguring the opamp */
109 
110 } HAL_OPAMP_StateTypeDef;
111 
112 /**
113   * @brief OPAMP Handle Structure definition
114   */
115 #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
116 typedef struct __OPAMP_HandleTypeDef
117 #else
118 typedef struct
119 #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
120 {
121   OPAMP_TypeDef       *Instance;                      /*!< OPAMP instance's registers base address */
122   OPAMP_InitTypeDef   Init;                           /*!< OPAMP required parameters               */
123   HAL_StatusTypeDef   Status;                         /*!< OPAMP peripheral status                 */
124   HAL_LockTypeDef     Lock;                           /*!< Locking object                          */
125   __IO HAL_OPAMP_StateTypeDef  State;                 /*!< OPAMP communication state               */
126 
127 #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
128   void (* MspInitCallback)(struct __OPAMP_HandleTypeDef *hopamp);
129   void (* MspDeInitCallback)(struct __OPAMP_HandleTypeDef *hopamp);
130 #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
131 
132 } OPAMP_HandleTypeDef;
133 
134 /**
135   * @brief HAl_OPAMP_TrimmingValueTypeDef definition
136   */
137 
138 typedef  uint32_t HAL_OPAMP_TrimmingValueTypeDef;
139 
140 #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
141 /**
142   * @brief  HAL OPAMP Callback ID enumeration definition
143   */
144 typedef enum
145 {
146   HAL_OPAMP_MSP_INIT_CB_ID                     = 0x01U,  /*!< OPAMP MspInit Callback ID   */
147   HAL_OPAMP_MSP_DEINIT_CB_ID                   = 0x02U,  /*!< OPAMP MspDeInit Callback ID */
148   HAL_OPAMP_ALL_CB_ID                          = 0x03U   /*!< OPAMP All ID                */
149 
150 } HAL_OPAMP_CallbackIDTypeDef;
151 
152 /**
153   * @brief  HAL OPAMP Callback pointer definition
154   */
155 typedef void (*pOPAMP_CallbackTypeDef)(OPAMP_HandleTypeDef *hopamp);
156 #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
157 
158 /**
159   * @}
160   */
161 
162 /* Exported constants --------------------------------------------------------*/
163 
164 /** @defgroup OPAMP_Exported_Constants OPAMP Exported Constants
165   * @{
166   */
167 
168 /** @defgroup OPAMP_Mode OPAMP Mode
169   * @{
170   */
171 #define OPAMP_STANDALONE_MODE            0x00000000U                /*!< standalone mode */
172 #define OPAMP_PGA_MODE                   OPAMP_CSR_OPAMODE_1        /*!< PGA mode        */
173 #define OPAMP_FOLLOWER_MODE              OPAMP_CSR_OPAMODE          /*!< follower mode   */
174 
175 /**
176   * @}
177   */
178 
179 /** @defgroup OPAMP_NonInvertingInput OPAMP Non Inverting Input
180   * @{
181   */
182 
183 #define OPAMP_NONINVERTINGINPUT_IO0         0x00000000U             /*!< OPAMP non-inverting input connected to dedicated IO pin       */
184 #define OPAMP_NONINVERTINGINPUT_DAC_CH      OPAMP_CSR_VP_SEL        /*!< OPAMP non-inverting input connected internally to DAC channel */
185 /**
186   * @}
187   */
188 
189 /** @defgroup OPAMP_InvertingInput OPAMP Inverting Input
190   * @{
191   */
192 #define OPAMP_INVERTINGINPUT_IO0              0x00000000U            /*!< OPAMP inverting input connected to dedicated IO pin low-leakage                         */
193 #define OPAMP_INVERTINGINPUT_CONNECT_NO       OPAMP_CSR_VM_SEL_1     /*!< OPAMP inverting input not connected externally (PGA mode only)                          */
194 /**
195   * @}
196   */
197 
198 /** @defgroup OPAMP_PgaGain OPAMP Pga Gain
199   * @{
200   */
201 
202 #define OPAMP_PGA_GAIN_2                0x00000000U                                   /*!< PGA gain =  2 */
203 #define OPAMP_PGA_GAIN_4                OPAMP_CSR_PGA_GAIN_0                          /*!< PGA gain =  4 */
204 #define OPAMP_PGA_GAIN_8                OPAMP_CSR_PGA_GAIN_1                          /*!< PGA gain =  8 */
205 #define OPAMP_PGA_GAIN_16              (OPAMP_CSR_PGA_GAIN_0 | OPAMP_CSR_PGA_GAIN_1)  /*!< PGA gain = 16 */
206 
207 /**
208   * @}
209   */
210 
211 /** @defgroup OPAMP_PowerMode OPAMP PowerMode
212   * @{
213   */
214 #define OPAMP_POWERMODE_NORMALPOWER_NORMALSPEED        0x00000000U                         /*!< OPAMP power mode normal speed normal    */
215 #define OPAMP_POWERMODE_LOWPOWER_NORMALSPEED           OPAMP_CSR_OPALPM                    /*!< OPAMP power mode low-power speed normal */
216 
217 /**
218   * @}
219   */
220 
221 /** @defgroup OPAMP_PowerSupplyRange OPAMP PowerSupplyRange
222   * @{
223   */
224 #define OPAMP_POWERSUPPLY_LOW          0x00000000U            /*!< Power supply range low (VDDA lower than 2.4V)   */
225 #define OPAMP_POWERSUPPLY_HIGH         OPAMP_CSR_OPARANGE     /*!< Power supply range high (VDDA higher than 2.4V) */
226 
227 /**
228   * @}
229   */
230 
231 /** @defgroup OPAMP_UserTrimming OPAMP User Trimming
232   * @{
233   */
234 #define OPAMP_TRIMMING_FACTORY        0x00000000U            /*!< Factory trimming */
235 #define OPAMP_TRIMMING_USER           OPAMP_CSR_USERTRIM     /*!< User trimming    */
236 
237 /**
238   * @}
239   */
240 
241 /** @defgroup OPAMP_FactoryTrimming OPAMP Factory Trimming
242   * @{
243   */
244 #define OPAMP_FACTORYTRIMMING_DUMMY    0xFFFFFFFFU           /*!< Dummy value if trimming value could not be retrieved */
245 #define OPAMP_FACTORYTRIMMING_N        0U                    /*!< Offset trimming N                                    */
246 #define OPAMP_FACTORYTRIMMING_P        1U                    /*!< Offset trimming P                                    */
247 
248 /**
249   * @}
250   */
251 
252 /**
253   * @}
254  */
255 
256 /* Private constants ---------------------------------------------------------*/
257 /** @defgroup OPAMP_Private_Constants OPAMP Private Constants
258   * @brief   OPAMP Private constants and defines
259   * @{
260   */
261 
262 /* NONINVERTING bit position in OTR & LPOTR */
263 #define OPAMP_INPUT_NONINVERTING              ((uint32_t) 8)  /*!< Non inverting input */
264 
265 /* Offset trimming time: during calibration, minimum time needed between two  */
266 /* steps to have 1 mV accuracy.                                               */
267 /* Refer to datasheet, electrical characteristics: parameter tOFFTRIM Typ=1ms.*/
268 /* Unit: ms.                                                                  */
269 #define OPAMP_TRIMMING_DELAY                  ((uint32_t) 1)
270 
271 /**
272   * @}
273   */
274 
275 /* Exported macros -----------------------------------------------------------*/
276 /** @defgroup OPAMP_Exported_Macros OPAMP Exported Macros
277   * @{
278   */
279 
280 /** @brief Reset OPAMP handle state.
281   * @param  __HANDLE__: OPAMP handle.
282   * @retval None
283   */
284 #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
285 #define __HAL_OPAMP_RESET_HANDLE_STATE(__HANDLE__)           do {                                             \
286                                                                   (__HANDLE__)->State = HAL_OPAMP_STATE_RESET; \
287                                                                   (__HANDLE__)->MspInitCallback = NULL;        \
288                                                                   (__HANDLE__)->MspDeInitCallback = NULL;      \
289                                                                 } while(0)
290 #else
291 #define __HAL_OPAMP_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_OPAMP_STATE_RESET)
292 #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
293 
294 
295 /**
296   * @}
297   */
298 
299 /* Private macro -------------------------------------------------------------*/
300 
301 /** @defgroup OPAMP_Private_Macros OPAMP Private Macros
302   * @{
303   */
304 
305 #define IS_OPAMP_FUNCTIONAL_NORMALMODE(INPUT) (((INPUT) == OPAMP_STANDALONE_MODE) || \
306                                                ((INPUT) == OPAMP_PGA_MODE)        || \
307                                                ((INPUT) == OPAMP_FOLLOWER_MODE))
308 
309 #define IS_OPAMP_INVERTING_INPUT_STANDALONE(INPUT) (((INPUT) == OPAMP_INVERTINGINPUT_IO0))
310 
311 
312 #define IS_OPAMP_NONINVERTING_INPUT(INPUT) (((INPUT) == OPAMP_NONINVERTINGINPUT_IO0) || \
313                                             ((INPUT) == OPAMP_NONINVERTINGINPUT_DAC_CH))
314 
315 #define IS_OPAMP_INVERTING_INPUT_PGA(INPUT) (((INPUT) == OPAMP_INVERTINGINPUT_IO0) || \
316                                              ((INPUT) == OPAMP_INVERTINGINPUT_CONNECT_NO))
317 
318 #define IS_OPAMP_PGA_GAIN(GAIN) (((GAIN) == OPAMP_PGA_GAIN_2) || \
319                                  ((GAIN) == OPAMP_PGA_GAIN_4) || \
320                                  ((GAIN) == OPAMP_PGA_GAIN_8) || \
321                                  ((GAIN) == OPAMP_PGA_GAIN_16))
322 
323 #define IS_OPAMP_POWERMODE(POWERMODE) (((POWERMODE) == OPAMP_POWERMODE_NORMALPOWER_NORMALSPEED) || \
324                                        ((POWERMODE) == OPAMP_POWERMODE_LOWPOWER_NORMALSPEED))
325 
326 #define IS_OPAMP_TRIMMING(TRIMMING) (((TRIMMING) == OPAMP_TRIMMING_FACTORY) || \
327                                      ((TRIMMING) == OPAMP_TRIMMING_USER))
328 
329 
330 #define IS_OPAMP_TRIMMINGVALUE(TRIMMINGVALUE) ((TRIMMINGVALUE) <= 31U)
331 
332 #define IS_OPAMP_FACTORYTRIMMING(TRIMMING) (((TRIMMING) == OPAMP_FACTORYTRIMMING_N) || \
333                                             ((TRIMMING) == OPAMP_FACTORYTRIMMING_P))
334 
335 /**
336   * @}
337   */
338 
339 /* Include OPAMP HAL Extended module */
340 #include "stm32u0xx_hal_opamp_ex.h"
341 
342 /* Exported functions --------------------------------------------------------*/
343 /** @addtogroup OPAMP_Exported_Functions
344   * @{
345   */
346 
347 /** @addtogroup OPAMP_Exported_Functions_Group1
348   * @{
349   */
350 /* Initialization/de-initialization functions  **********************************/
351 HAL_StatusTypeDef HAL_OPAMP_Init(OPAMP_HandleTypeDef *hopamp);
352 HAL_StatusTypeDef HAL_OPAMP_DeInit(OPAMP_HandleTypeDef *hopamp);
353 void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef *hopamp);
354 void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef *hopamp);
355 /**
356   * @}
357   */
358 
359 /** @addtogroup OPAMP_Exported_Functions_Group2
360   * @{
361   */
362 
363 /* I/O operation functions  *****************************************************/
364 HAL_StatusTypeDef HAL_OPAMP_Start(OPAMP_HandleTypeDef *hopamp);
365 HAL_StatusTypeDef HAL_OPAMP_Stop(OPAMP_HandleTypeDef *hopamp);
366 HAL_StatusTypeDef HAL_OPAMP_SelfCalibrate(OPAMP_HandleTypeDef *hopamp);
367 
368 /**
369   * @}
370   */
371 
372 /** @addtogroup OPAMP_Exported_Functions_Group3
373   * @{
374   */
375 
376 /* Peripheral Control functions  ************************************************/
377 HAL_StatusTypeDef HAL_OPAMP_Lock(OPAMP_HandleTypeDef *hopamp);
378 HAL_OPAMP_TrimmingValueTypeDef HAL_OPAMP_GetTrimOffset(const OPAMP_HandleTypeDef *hopamp, uint32_t trimmingoffset);
379 
380 /**
381   * @}
382   */
383 
384 /** @addtogroup OPAMP_Exported_Functions_Group4
385   * @{
386   */
387 
388 /* Peripheral State functions  **************************************************/
389 HAL_OPAMP_StateTypeDef HAL_OPAMP_GetState(const OPAMP_HandleTypeDef *hopamp);
390 
391 /**
392   * @}
393   */
394 
395 /** @addtogroup OPAMP_Exported_Functions_Group5
396   * @{
397   */
398 
399 /* Peripheral Callback functions  **************************************************/
400 #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
401 HAL_StatusTypeDef HAL_OPAMP_RegisterCallback(OPAMP_HandleTypeDef *hopamp, HAL_OPAMP_CallbackIDTypeDef CallbackID,
402                                              pOPAMP_CallbackTypeDef pCallback);
403 HAL_StatusTypeDef HAL_OPAMP_UnRegisterCallback(OPAMP_HandleTypeDef *hopamp, HAL_OPAMP_CallbackIDTypeDef CallbackID);
404 #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
405 
406 /**
407   * @}
408   */
409 
410 /**
411   * @}
412   */
413 
414 /**
415   * @}
416   */
417 
418 /**
419   * @}
420   */
421 
422 #ifdef __cplusplus
423 }
424 #endif
425 
426 #endif /* STM32U0xx_HAL_OPAMP_H */
427 
428