1 /**
2   ******************************************************************************
3   * @file    stm32u5xx_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 STM32U5xx_HAL_OPAMP_H
21 #define STM32U5xx_HAL_OPAMP_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32u5xx_hal_def.h"
29 
30 /** @addtogroup STM32U5xx_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_IO1              OPAMP_CSR_VM_SEL_0     /*!< OPAMP inverting input connected to alternative IO pin available on some device packages */
194 #define OPAMP_INVERTINGINPUT_CONNECT_NO       OPAMP_CSR_VM_SEL_1     /*!< OPAMP inverting input not connected externally (PGA mode only)                          */
195 /**
196   * @}
197   */
198 
199 /** @defgroup OPAMP_PgaGain OPAMP Pga Gain
200   * @{
201   */
202 
203 #define OPAMP_PGA_GAIN_2                0x00000000U                                   /*!< PGA gain =  2 */
204 #define OPAMP_PGA_GAIN_4                OPAMP_CSR_PGA_GAIN_0                          /*!< PGA gain =  4 */
205 #define OPAMP_PGA_GAIN_8                OPAMP_CSR_PGA_GAIN_1                          /*!< PGA gain =  8 */
206 #define OPAMP_PGA_GAIN_16              (OPAMP_CSR_PGA_GAIN_0 | OPAMP_CSR_PGA_GAIN_1)  /*!< PGA gain = 16 */
207 
208 /**
209   * @}
210   */
211 
212 /** @defgroup OPAMP_PowerMode OPAMP PowerMode
213   * @{
214   */
215 #define OPAMP_POWERMODE_NORMALPOWER_NORMALSPEED        0x00000000U                         /*!< OPAMP power mode normal speed normal    */
216 #define OPAMP_POWERMODE_LOWPOWER_NORMALSPEED           OPAMP_CSR_OPALPM                    /*!< OPAMP power mode low-power speed normal */
217 #define OPAMP_POWERMODE_NORMALPOWER_HIGHSPEED          OPAMP_CSR_HSM                       /*!< OPAMP power mode normal speed high      */
218 #define OPAMP_POWERMODE_LOWPOWER_HIGHSPEED            (OPAMP_CSR_OPALPM | OPAMP_CSR_HSM)   /*!< OPAMP power mode low-power speed high   */
219 
220 /**
221   * @}
222   */
223 
224 /** @defgroup OPAMP_PowerSupplyRange OPAMP PowerSupplyRange
225   * @{
226   */
227 #define OPAMP_POWERSUPPLY_LOW          0x00000000U            /*!< Power supply range low (VDDA lower than 2.4V)   */
228 #define OPAMP_POWERSUPPLY_HIGH         OPAMP_CSR_OPARANGE     /*!< Power supply range high (VDDA higher than 2.4V) */
229 
230 /**
231   * @}
232   */
233 
234 /** @defgroup OPAMP_UserTrimming OPAMP User Trimming
235   * @{
236   */
237 #define OPAMP_TRIMMING_FACTORY        0x00000000U            /*!< Factory trimming */
238 #define OPAMP_TRIMMING_USER           OPAMP_CSR_USERTRIM     /*!< User trimming    */
239 
240 /**
241   * @}
242   */
243 
244 /** @defgroup OPAMP_FactoryTrimming OPAMP Factory Trimming
245   * @{
246   */
247 #define OPAMP_FACTORYTRIMMING_DUMMY    0xFFFFFFFFU           /*!< Dummy value if trimming value could not be retrieved */
248 #define OPAMP_FACTORYTRIMMING_N        0U                    /*!< Offset trimming N                                    */
249 #define OPAMP_FACTORYTRIMMING_P        1U                    /*!< Offset trimming P                                    */
250 
251 /**
252   * @}
253   */
254 
255 /**
256   * @}
257  */
258 
259 /* Private constants ---------------------------------------------------------*/
260 /** @defgroup OPAMP_Private_Constants OPAMP Private Constants
261   * @brief   OPAMP Private constants and defines
262   * @{
263   */
264 
265 /* NONINVERTING bit position in OTR & LPOTR */
266 #define OPAMP_INPUT_NONINVERTING              ((uint32_t) 8)  /*!< Non inverting input */
267 
268 /* Offset trimming time: during calibration, minimum time needed between two  */
269 /* steps to have 1 mV accuracy.                                               */
270 /* Refer to datasheet, electrical characteristics: parameter tOFFTRIM Typ=1ms.*/
271 /* Unit: ms.                                                                  */
272 #define OPAMP_TRIMMING_DELAY                  ((uint32_t) 1)
273 
274 /**
275   * @}
276   */
277 
278 /* Exported macros -----------------------------------------------------------*/
279 /** @defgroup OPAMP_Exported_Macros OPAMP Exported Macros
280   * @{
281   */
282 
283 /** @brief Reset OPAMP handle state.
284   * @param  __HANDLE__: OPAMP handle.
285   * @retval None
286   */
287 #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
288 #define __HAL_OPAMP_RESET_HANDLE_STATE(__HANDLE__)           do {                                             \
289                                                                   (__HANDLE__)->State = HAL_OPAMP_STATE_RESET; \
290                                                                   (__HANDLE__)->MspInitCallback = NULL;        \
291                                                                   (__HANDLE__)->MspDeInitCallback = NULL;      \
292                                                                 } while(0)
293 #else
294 #define __HAL_OPAMP_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_OPAMP_STATE_RESET)
295 #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
296 
297 
298 /**
299   * @}
300   */
301 
302 /* Private macro -------------------------------------------------------------*/
303 
304 /** @defgroup OPAMP_Private_Macros OPAMP Private Macros
305   * @{
306   */
307 
308 #define IS_OPAMP_FUNCTIONAL_NORMALMODE(INPUT) (((INPUT) == OPAMP_STANDALONE_MODE) || \
309                                                ((INPUT) == OPAMP_PGA_MODE)        || \
310                                                ((INPUT) == OPAMP_FOLLOWER_MODE))
311 
312 #define IS_OPAMP_INVERTING_INPUT_STANDALONE(INPUT) (((INPUT) == OPAMP_INVERTINGINPUT_IO0) || \
313                                                     ((INPUT) == OPAMP_INVERTINGINPUT_IO1))
314 
315 
316 #define IS_OPAMP_NONINVERTING_INPUT(INPUT) (((INPUT) == OPAMP_NONINVERTINGINPUT_IO0) || \
317                                             ((INPUT) == OPAMP_NONINVERTINGINPUT_DAC_CH))
318 
319 #define IS_OPAMP_INVERTING_INPUT_PGA(INPUT) (((INPUT) == OPAMP_INVERTINGINPUT_IO0) || \
320                                              ((INPUT) == OPAMP_INVERTINGINPUT_IO1) || \
321                                              ((INPUT) == OPAMP_INVERTINGINPUT_CONNECT_NO))
322 
323 #define IS_OPAMP_PGA_GAIN(GAIN) (((GAIN) == OPAMP_PGA_GAIN_2) || \
324                                  ((GAIN) == OPAMP_PGA_GAIN_4) || \
325                                  ((GAIN) == OPAMP_PGA_GAIN_8) || \
326                                  ((GAIN) == OPAMP_PGA_GAIN_16))
327 
328 #define IS_OPAMP_POWERMODE(POWERMODE) (((POWERMODE) == OPAMP_POWERMODE_NORMALPOWER_NORMALSPEED) || \
329                                        ((POWERMODE) == OPAMP_POWERMODE_NORMALPOWER_HIGHSPEED)   || \
330                                        ((POWERMODE) == OPAMP_POWERMODE_LOWPOWER_NORMALSPEED)    || \
331                                        ((POWERMODE) == OPAMP_POWERMODE_LOWPOWER_HIGHSPEED))
332 
333 #define IS_OPAMP_TRIMMING(TRIMMING) (((TRIMMING) == OPAMP_TRIMMING_FACTORY) || \
334                                      ((TRIMMING) == OPAMP_TRIMMING_USER))
335 
336 
337 #define IS_OPAMP_TRIMMINGVALUE(TRIMMINGVALUE) ((TRIMMINGVALUE) <= 31U)
338 
339 #define IS_OPAMP_FACTORYTRIMMING(TRIMMING) (((TRIMMING) == OPAMP_FACTORYTRIMMING_N) || \
340                                             ((TRIMMING) == OPAMP_FACTORYTRIMMING_P))
341 
342 /**
343   * @}
344   */
345 
346 /* Include OPAMP HAL Extended module */
347 #include "stm32u5xx_hal_opamp_ex.h"
348 
349 /* Exported functions --------------------------------------------------------*/
350 /** @addtogroup OPAMP_Exported_Functions
351   * @{
352   */
353 
354 /** @addtogroup OPAMP_Exported_Functions_Group1
355   * @{
356   */
357 /* Initialization/de-initialization functions  **********************************/
358 HAL_StatusTypeDef HAL_OPAMP_Init(OPAMP_HandleTypeDef *hopamp);
359 HAL_StatusTypeDef HAL_OPAMP_DeInit(OPAMP_HandleTypeDef *hopamp);
360 void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef *hopamp);
361 void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef *hopamp);
362 /**
363   * @}
364   */
365 
366 /** @addtogroup OPAMP_Exported_Functions_Group2
367   * @{
368   */
369 
370 /* I/O operation functions  *****************************************************/
371 HAL_StatusTypeDef HAL_OPAMP_Start(OPAMP_HandleTypeDef *hopamp);
372 HAL_StatusTypeDef HAL_OPAMP_Stop(OPAMP_HandleTypeDef *hopamp);
373 HAL_StatusTypeDef HAL_OPAMP_SelfCalibrate(OPAMP_HandleTypeDef *hopamp);
374 
375 /**
376   * @}
377   */
378 
379 /** @addtogroup OPAMP_Exported_Functions_Group3
380   * @{
381   */
382 
383 /* Peripheral Control functions  ************************************************/
384 HAL_StatusTypeDef HAL_OPAMP_Lock(OPAMP_HandleTypeDef *hopamp);
385 HAL_OPAMP_TrimmingValueTypeDef HAL_OPAMP_GetTrimOffset(const OPAMP_HandleTypeDef *hopamp, uint32_t trimmingoffset);
386 
387 /**
388   * @}
389   */
390 
391 /** @addtogroup OPAMP_Exported_Functions_Group4
392   * @{
393   */
394 
395 /* Peripheral State functions  **************************************************/
396 HAL_OPAMP_StateTypeDef HAL_OPAMP_GetState(const OPAMP_HandleTypeDef *hopamp);
397 
398 /**
399   * @}
400   */
401 
402 /** @addtogroup OPAMP_Exported_Functions_Group5
403   * @{
404   */
405 
406 /* Peripheral Callback functions  **************************************************/
407 #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
408 HAL_StatusTypeDef HAL_OPAMP_RegisterCallback(OPAMP_HandleTypeDef *hopamp, HAL_OPAMP_CallbackIDTypeDef CallbackID,
409                                              pOPAMP_CallbackTypeDef pCallback);
410 HAL_StatusTypeDef HAL_OPAMP_UnRegisterCallback(OPAMP_HandleTypeDef *hopamp, HAL_OPAMP_CallbackIDTypeDef CallbackID);
411 #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
412 
413 /**
414   * @}
415   */
416 
417 /**
418   * @}
419   */
420 
421 /**
422   * @}
423   */
424 
425 /**
426   * @}
427   */
428 
429 #ifdef __cplusplus
430 }
431 #endif
432 
433 #endif /* STM32U5xx_HAL_OPAMP_H */
434 
435