2 ******************************************************************************
3 * @file stm32g4xx_hal_opamp.c
4 * @author MCD Application Team
5 * @brief OPAMP HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the operational amplifiers (OPAMP1,...OPAMP6)
9 * + OPAMP Configuration
12 * + Initialization/de-initialization functions
13 * + I/O operation functions
14 * + Peripheral Control functions
15 * + Peripheral State functions
18 ================================================================================
19 ##### OPAMP Peripheral Features #####
20 ================================================================================
22 [..] The device integrates up to 6 operational amplifiers OPAMP1, OPAMP2,
23 OPAMP3, OPAMP4, OPAMP5 and OPAMP6:
25 (#) The OPAMP(s) provides several exclusive running modes.
27 (++) Programmable Gain Amplifier (PGA) mode (Resistor feedback output)
30 (#) The OPAMP(s) provide(s) calibration capabilities.
31 (++) Calibration aims at correcting some offset for running mode.
32 (++) The OPAMP uses either factory calibration settings OR user defined
33 calibration (trimming) settings (i.e. trimming mode).
34 (++) The user defined settings can be figured out using self calibration
35 handled by HAL_OPAMP_SelfCalibrate, HAL_OPAMPEx_SelfCalibrateAll
36 (++) HAL_OPAMP_SelfCalibrate:
37 (++) Runs automatically the calibration in 2 steps.
38 (90% of VDDA for NMOS transistors, 10% of VDDA for PMOS transistors).
39 (As OPAMP is Rail-to-rail input/output, these 2 steps calibration is
40 appropriate and enough in most cases).
41 (++) Enables the user trimming mode
42 (++) Updates the init structure with trimming values with fresh calibration
44 The user may store the calibration results for larger
45 (ex monitoring the trimming as a function of temperature
47 (++) for STM32G4 devices having 6 OPAMPs
48 HAL_OPAMPEx_SelfCalibrateAll
49 runs calibration of 6 OPAMPs in parallel.
51 (#) For any running mode, an additional Timer-controlled Mux (multiplexer)
52 mode can be set on top.
53 (++) Timer-controlled Mux mode allows Automatic switching of inputs
54 configuration (inverting and non inverting).
55 (++) Hence on top of defaults (primary) inverting and non-inverting inputs,
56 the user shall select secondary inverting and non inverting inputs.
57 (++) TIM1 OC6, TIM8 OC6 and TIM20 OC6 provides the alternate switching
58 tempo between defaults (primary) and secondary inputs.
59 (++) These 3 timers (TIM1, TIM8 and TIM20) can be combined to design a more
60 complex switching scheme. So that any of the selected channel can initiate
61 the configuration switch.
63 (#) Running mode: Standalone mode
64 (++) Gain is set externally (gain depends on external loads).
65 (++) Follower mode also possible externally by connecting the inverting input to
68 (#) Running mode: Follower mode
69 (++) Inverting Input is not connected.
71 (#) Running mode: Programmable Gain Amplifier (PGA) mode
72 (Resistor feedback output)
73 (++) The OPAMP(s) output(s) can be internally connected to resistor feedback
75 (++) The OPAMP inverting input can be "not" connected, signal to amplify is
76 connected to non inverting input and gain is positive (2,4,8,16,32 or 64)
77 (++) The OPAMP inverting input can be connected to VINM0:
78 If signal is applied to non inverting input, gain is positive (2,4,8,16,32 or 64).
79 If signal is applied to inverting input, gain is negative (-1,-3,-7,-15-,31 or -63).
80 In both cases, the other input can be used as bias.
83 ##### How to use this driver #####
84 ================================================================================
87 *** High speed / normal power mode ***
88 ============================================
89 [..] To run in high speed mode:
91 (#) Configure the OPAMP using HAL_OPAMP_Init() function:
92 (++) Select OPAMP_POWERMODE_HIGHSPEED
93 (++) Otherwise select OPAMP_POWERMODE_NORMAL
96 ============================================
97 [..] To run the OPAMP calibration self calibration:
99 (#) Start calibration using HAL_OPAMP_SelfCalibrate.
100 Store the calibration results.
103 ============================================
104 [..] To use the OPAMP, perform the following steps:
106 (#) Fill in the HAL_OPAMP_MspInit() to
107 (++) Configure the OPAMP input AND output in analog mode using
108 HAL_GPIO_Init() to map the OPAMP output to the GPIO pin.
110 (#) Registrate Callbacks
111 (++) The compilation define USE_HAL_OPAMP_REGISTER_CALLBACKS when set to 1
112 allows the user to configure dynamically the driver callbacks.
114 (++) Use Functions @ref HAL_OPAMP_RegisterCallback() to register a user callback,
115 it allows to register following callbacks:
116 (+++) MspInitCallback : OPAMP MspInit.
117 (+++) MspDeInitCallback : OPAMP MspDeInit.
118 This function takes as parameters the HAL peripheral handle, the Callback ID
119 and a pointer to the user callback function.
121 (++) Use function @ref HAL_OPAMP_UnRegisterCallback() to reset a callback to the default
122 weak (surcharged) function. It allows to reset following callbacks:
123 (+++) MspInitCallback : OPAMP MspInit.
124 (+++) MspDeInitCallback : OPAMP MspDeInit.
127 (#) Configure the OPAMP using HAL_OPAMP_Init() function:
129 (++) Select the inverting input
130 (++) Select the non-inverting input
131 (++) Select if the internal ouput should be enabled/disabled (if enabled, regular I/O output is disabled)
132 (++) Select if the Timer controlled Mux is disabled or enabled and controlled by specified timer(s)
133 (++) If the Timer controlled Mux mode is enabled, select the secondary inverting input
134 (++) If the Timer controlled Mux mode is enabled, Select the secondary non-inverting input
135 (++) If PGA mode is enabled, Select if inverting input is connected.
136 (++) If PGA mode is enabled, Select PGA gain to be used.
137 (++) Select either factory or user defined trimming mode.
138 (++) If the user defined trimming mode is enabled, select PMOS & NMOS trimming values
139 (typ. settings returned by HAL_OPAMP_SelfCalibrate function).
141 (#) Enable the OPAMP using HAL_OPAMP_Start() function.
143 (#) Disable the OPAMP using HAL_OPAMP_Stop() function.
145 (#) Lock the OPAMP in running mode using HAL_OPAMP_Lock() & HAL_OPAMP_TimerMuxLock functions.
146 From then the configuration can only be modified
148 (++) OR thanks to HAL_OPAMP_MspDeInit called (user defined) from HAL_OPAMP_DeInit.
150 *** Running mode: change of configuration while OPAMP ON ***
151 ============================================
152 [..] To Re-configure OPAMP when OPAMP is ON (change on the fly)
153 (#) If needed, fill in the HAL_OPAMP_MspInit()
154 (++) This is the case for instance if you wish to use new OPAMP I/O
156 (#) Configure the OPAMP using HAL_OPAMP_Init() function:
157 (++) As in configure case, selects first the parameters you wish to modify.
158 (++) If OPAMP control register is locked, it is not possible to modify any values
159 on the fly (even the timer controlled mux parameters).
160 (++) If OPAMP timer controlled mux mode register is locked, it is possible to modify any values
161 of the control register but none on the timer controlled mux mode one.
163 (#) Change from high speed mode to normal power mode (& vice versa) requires
164 first HAL_OPAMP_DeInit() (force OPAMP OFF) and then HAL_OPAMP_Init().
165 In other words, of OPAMP is ON, HAL_OPAMP_Init can NOT change power mode
169 ******************************************************************************
172 * <h2><center>© Copyright (c) 2018 STMicroelectronics.
173 * All rights reserved.</center></h2>
175 * This software component is licensed by ST under BSD 3-Clause license,
176 * the "License"; You may not use this file except in compliance with the
177 * License. You may obtain a copy of the License at:
178 * opensource.org/licenses/BSD-3-Clause
180 ******************************************************************************
185 The OPAMPs non inverting input (both default and secondary) can be
186 selected among the list shown by table below.
188 The OPAMPs non inverting input (both default and secondary) can be
189 selected among the list shown by table below.
191 Table 1. OPAMPs inverting/non-inverting inputs for the STM32G4 devices:
192 +--------------------------------------------------------------------------------------------+
193 | | | OPAMP1 | OPAMP2 | OPAMP3 | OPAMP4 | OPAMP5 | OPAMP6 |
194 |-----------------|--------|----------|----------|----------|----------|----------|----------|
195 | | No conn| X | X | X | X | X | X |
196 | Inverting Input | VM0 | PA3 | PA5 | PB2 | PB10 | PB15 | PA1 |
197 | (1) | VM1 | PC5 | PC5 | PB10 | PD8 | PA3 | PB1 |
198 |-----------------|--------|----------|----------|----------|----------|----------|----------|
199 | | VP0 | PA1 | PA7 | PB0 | PB13 | PB14 | PB12 |
200 | Non Inverting | VP1 | PA3 | PB14 | PB13 | PD11 | PD12 | PD9 |
201 | Input | VP2 | PA7 | PB0 | PA1 | PB11 | PC3 | PB13 |
202 | | VP3 | DAC3_CH1 | PD14 | DAC3_CH2 | DAC4_CH1 | DAC4_CH2 | DAC3_CH1 |
203 +--------------------------------------------------------------------------------------------+
204 (1): No connection in follower mode.
206 Table 2. OPAMPs outputs for the STM32G4 devices:
207 +--------------------------------------------------------------------------------+
208 | | | OPAMP1 | OPAMP2 | OPAMP3 | OPAMP4 | OPAMP5 | OPAMP6 |
209 |-----------------|--------|--------|--------|--------|--------|--------|--------|
210 | Output | | PA2 | PA6 | PB1 | PB12 | PA8 | PB11 |
211 |-----------------|--------|--------|--------|--------|--------|--------|--------+
212 | Internal output | | ADC1 | ADC2 | ADC2 | ADC5 | ADC5 | ADC4 |
213 | to ADCs | | CH13 | CH16 | CH18 | CH5 | CH3 | CH17 |
214 | | | | | ADC3 | | | |
215 | | | | | CH13 | | | |
216 |-----------------|--------|--------|--------|--------|--------|--------|--------+
220 /* Includes ------------------------------------------------------------------*/
221 #include "stm32g4xx_hal.h"
223 /** @addtogroup STM32G4xx_HAL_Driver
227 #ifdef HAL_OPAMP_MODULE_ENABLED
229 /** @defgroup OPAMP OPAMP
230 * @brief OPAMP HAL module driver
234 /* Private typedef -----------------------------------------------------------*/
235 /* Private define ------------------------------------------------------------*/
236 /** @defgroup OPAMP_Private_Define OPAMP Private Define
239 /* CSR register reset value */
240 #define OPAMP_CSR_RESET_VALUE (0x00000000UL)
241 /* CSR register TRIM value upon reset are factory ones, filter them out from CSR register check */
242 #define OPAMP_CSR_RESET_CHECK_MASK (~(OPAMP_CSR_TRIMOFFSETN | OPAMP_CSR_TRIMOFFSETP))
243 /* CSR init register Mask */
244 #define OPAMP_CSR_UPDATE_PARAMETERS_INIT_MASK (OPAMP_CSR_TRIMOFFSETN | OPAMP_CSR_TRIMOFFSETP \
245 | OPAMP_CSR_HIGHSPEEDEN | OPAMP_CSR_OPAMPINTEN \
246 | OPAMP_CSR_PGGAIN | OPAMP_CSR_VPSEL \
247 | OPAMP_CSR_VMSEL | OPAMP_CSR_FORCEVP)
248 /* TCMR init register Mask */
249 #define OPAMP_TCMR_UPDATE_PARAMETERS_INIT_MASK (OPAMP_TCMR_T20CMEN | OPAMP_TCMR_T8CMEN \
250 | OPAMP_TCMR_T1CMEN | OPAMP_TCMR_VPSSEL \
256 /* Private macro -------------------------------------------------------------*/
257 /* Private variables ---------------------------------------------------------*/
258 /* Private function prototypes -----------------------------------------------*/
259 /* Exported functions ---------------------------------------------------------*/
261 /** @defgroup OPAMP_Exported_Functions OPAMP Exported Functions
265 /** @defgroup OPAMP_Exported_Functions_Group1 Initialization and de-initialization functions
266 * @brief Initialization and Configuration functions
269 ===============================================================================
270 ##### Initialization and de-initialization functions #####
271 ===============================================================================
272 [..] This section provides functions allowing to:
279 * @brief Initializes the OPAMP according to the specified
280 * parameters in the OPAMP_InitTypeDef and initialize the associated handle.
281 * @note If the selected opamp is locked, initialization can't be performed.
282 * To unlock the configuration, perform a system reset.
283 * @param hopamp OPAMP handle
286 HAL_StatusTypeDef
HAL_OPAMP_Init(OPAMP_HandleTypeDef
*hopamp
)
288 HAL_StatusTypeDef status
= HAL_OK
;
290 /* Check the OPAMP handle allocation and lock status */
291 /* Init not allowed if calibration is ongoing */
296 else if (hopamp
->State
== HAL_OPAMP_STATE_BUSYLOCKED
)
300 else if (hopamp
->State
== HAL_OPAMP_STATE_CALIBBUSY
)
307 /* Check the parameter */
308 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp
->Instance
));
310 /* Set OPAMP parameters */
311 assert_param(IS_OPAMP_POWERMODE(hopamp
->Init
.PowerMode
));
312 assert_param(IS_OPAMP_FUNCTIONAL_NORMALMODE(hopamp
->Init
.Mode
));
313 assert_param(IS_OPAMP_NONINVERTING_INPUT(hopamp
->Init
.NonInvertingInput
));
315 #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
316 if (hopamp
->State
== HAL_OPAMP_STATE_RESET
)
318 if (hopamp
->MspInitCallback
== NULL
)
320 hopamp
->MspInitCallback
= HAL_OPAMP_MspInit
;
323 #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
325 if ((hopamp
->Init
.Mode
) == OPAMP_STANDALONE_MODE
)
327 assert_param(IS_OPAMP_INVERTING_INPUT(hopamp
->Init
.InvertingInput
));
329 assert_param(IS_FUNCTIONAL_STATE(hopamp
->Init
.InternalOutput
));
331 assert_param(IS_OPAMP_TIMERCONTROLLED_MUXMODE(hopamp
->Init
.TimerControlledMuxmode
));
333 if ((hopamp
->Init
.TimerControlledMuxmode
) != OPAMP_TIMERCONTROLLEDMUXMODE_DISABLE
)
335 assert_param(IS_OPAMP_SEC_NONINVERTING_INPUT(hopamp
->Init
.NonInvertingInputSecondary
));
336 assert_param(IS_OPAMP_SEC_INVERTING_INPUT(hopamp
->Init
.InvertingInputSecondary
));
339 if ((hopamp
->Init
.Mode
) == OPAMP_PGA_MODE
)
341 assert_param(IS_OPAMP_PGACONNECT(hopamp
->Init
.PgaConnect
));
342 assert_param(IS_OPAMP_PGA_GAIN(hopamp
->Init
.PgaGain
));
345 assert_param(IS_OPAMP_TRIMMING(hopamp
->Init
.UserTrimming
));
346 if ((hopamp
->Init
.UserTrimming
) == OPAMP_TRIMMING_USER
)
348 assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp
->Init
.TrimmingValueP
));
349 assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp
->Init
.TrimmingValueN
));
352 /* Init SYSCFG and the low level hardware to access opamp */
353 __HAL_RCC_SYSCFG_CLK_ENABLE();
355 if (hopamp
->State
== HAL_OPAMP_STATE_RESET
)
357 /* Allocate lock resource and initialize it */
358 hopamp
->Lock
= HAL_UNLOCKED
;
361 #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
362 hopamp
->MspInitCallback(hopamp
);
364 /* Call MSP init function */
365 HAL_OPAMP_MspInit(hopamp
);
366 #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
368 /* Set OPAMP parameters */
369 /* Set bits according to hopamp->hopamp->Init.Mode value */
370 /* Set bits according to hopamp->hopamp->Init.InvertingInput value */
371 /* Set bits according to hopamp->hopamp->Init.NonInvertingInput value */
372 /* Set bits according to hopamp->hopamp->Init.InternalOutput value */
373 /* Set bits according to hopamp->hopamp->Init.TimerControlledMuxmode value */
374 /* Set bits according to hopamp->hopamp->Init.InvertingInputSecondary value */
375 /* Set bits according to hopamp->hopamp->Init.NonInvertingInputSecondary value */
376 /* Set bits according to hopamp->hopamp->Init.PgaConnect value */
377 /* Set bits according to hopamp->hopamp->Init.PgaGain value */
378 /* Set bits according to hopamp->hopamp->Init.UserTrimming value */
379 /* Set bits according to hopamp->hopamp->Init.TrimmingValueP value */
380 /* Set bits according to hopamp->hopamp->Init.TrimmingValueN value */
383 /* check if OPAMP_PGA_MODE & in Follower mode */
384 /* - InvertingInput */
385 /* is Not Applicable */
387 if ((hopamp
->Init
.Mode
== OPAMP_PGA_MODE
) || (hopamp
->Init
.Mode
== OPAMP_FOLLOWER_MODE
))
389 /* Update User Trim config first to be able to modify trimming value afterwards */
390 MODIFY_REG(hopamp
->Instance
->CSR
,
392 hopamp
->Init
.UserTrimming
);
393 MODIFY_REG(hopamp
->Instance
->CSR
,
394 OPAMP_CSR_UPDATE_PARAMETERS_INIT_MASK
,
395 hopamp
->Init
.PowerMode
|
397 hopamp
->Init
.NonInvertingInput
|
398 ((hopamp
->Init
.InternalOutput
== ENABLE
) ? OPAMP_CSR_OPAMPINTEN
: 0UL) |
399 hopamp
->Init
.PgaConnect
|
400 hopamp
->Init
.PgaGain
|
401 (hopamp
->Init
.TrimmingValueP
<< OPAMP_INPUT_NONINVERTING
) |
402 (hopamp
->Init
.TrimmingValueN
<< OPAMP_INPUT_INVERTING
));
404 else /* OPAMP_STANDALONE_MODE */
406 /* Update User Trim config first to be able to modify trimming value afterwards */
407 MODIFY_REG(hopamp
->Instance
->CSR
,
409 hopamp
->Init
.UserTrimming
);
410 MODIFY_REG(hopamp
->Instance
->CSR
,
411 OPAMP_CSR_UPDATE_PARAMETERS_INIT_MASK
,
412 hopamp
->Init
.PowerMode
|
414 hopamp
->Init
.InvertingInput
|
415 hopamp
->Init
.NonInvertingInput
|
416 ((hopamp
->Init
.InternalOutput
== ENABLE
) ? OPAMP_CSR_OPAMPINTEN
: 0UL) |
417 hopamp
->Init
.PgaConnect
|
418 hopamp
->Init
.PgaGain
|
419 (hopamp
->Init
.TrimmingValueP
<< OPAMP_INPUT_NONINVERTING
) |
420 (hopamp
->Init
.TrimmingValueN
<< OPAMP_INPUT_INVERTING
));
423 if ((READ_BIT(hopamp
->Instance
->TCMR
, OPAMP_TCMR_LOCK
)) == 0UL)
425 MODIFY_REG(hopamp
->Instance
->TCMR
,
426 OPAMP_TCMR_UPDATE_PARAMETERS_INIT_MASK
,
427 hopamp
->Init
.TimerControlledMuxmode
|
428 hopamp
->Init
.InvertingInputSecondary
|
429 hopamp
->Init
.NonInvertingInputSecondary
);
432 /* Update the OPAMP state*/
433 if (hopamp
->State
== HAL_OPAMP_STATE_RESET
)
435 /* From RESET state to READY State */
436 hopamp
->State
= HAL_OPAMP_STATE_READY
;
438 /* else: remain in READY or BUSY state (no update) */
446 * @brief DeInitializes the OPAMP peripheral
447 * @note Deinitialization can't be performed if the OPAMP configuration is locked.
448 * To unlock the configuration, perform a system reset.
449 * @param hopamp OPAMP handle
452 HAL_StatusTypeDef
HAL_OPAMP_DeInit(OPAMP_HandleTypeDef
*hopamp
)
454 HAL_StatusTypeDef status
= HAL_OK
;
456 /* Check the OPAMP handle allocation */
457 /* DeInit not allowed if calibration is ongoing */
462 else if (hopamp
->State
== HAL_OPAMP_STATE_CALIBBUSY
)
468 /* Check the parameter */
469 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp
->Instance
));
471 /* Set OPAMP_CSR register to reset value */
472 WRITE_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_RESET_VALUE
);
474 /* DeInit the low level hardware: GPIO, CLOCK and NVIC */
475 /* When OPAMP is locked, unlocking can be achieved thanks to */
476 /* __HAL_RCC_SYSCFG_CLK_DISABLE() call within HAL_OPAMP_MspDeInit */
477 /* Note that __HAL_RCC_SYSCFG_CLK_DISABLE() also disables comparator */
479 #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
480 if (hopamp
->MspDeInitCallback
== NULL
)
482 hopamp
->MspDeInitCallback
= HAL_OPAMP_MspDeInit
;
484 /* DeInit the low level hardware */
485 hopamp
->MspDeInitCallback(hopamp
);
487 HAL_OPAMP_MspDeInit(hopamp
);
488 #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
490 if (OPAMP_CSR_RESET_VALUE
== (hopamp
->Instance
->CSR
& OPAMP_CSR_RESET_CHECK_MASK
))
492 /* Update the OPAMP state */
493 hopamp
->State
= HAL_OPAMP_STATE_RESET
;
495 else /* RESET STATE */
497 /* DeInit not complete */
498 /* It can be the case if OPAMP was formerly locked */
501 /* The OPAMP state is NOT updated */
504 /* Process unlocked */
505 __HAL_UNLOCK(hopamp
);
512 * @brief Initialize the OPAMP MSP.
513 * @param hopamp OPAMP handle
516 __weak
void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef
*hopamp
)
518 /* Prevent unused argument(s) compilation warning */
521 /* NOTE : This function should not be modified, when the callback is needed,
522 the HAL_OPAMP_MspInit could be implemented in the user file
529 * @brief DeInitialize OPAMP MSP.
530 * @param hopamp OPAMP handle
533 __weak
void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef
*hopamp
)
535 /* Prevent unused argument(s) compilation warning */
538 /* NOTE : This function should not be modified, when the callback is needed,
539 the HAL_OPAMP_MspDeInit could be implemented in the user file
549 /** @defgroup OPAMP_Exported_Functions_Group2 Input and Output operation functions
550 * @brief Data transfers functions
553 ===============================================================================
554 ##### IO operation functions #####
555 ===============================================================================
557 This subsection provides a set of functions allowing to manage the OPAMP data
565 * @brief Start the opamp
566 * @param hopamp OPAMP handle
570 HAL_StatusTypeDef
HAL_OPAMP_Start(OPAMP_HandleTypeDef
*hopamp
)
572 HAL_StatusTypeDef status
= HAL_OK
;
574 /* Check the OPAMP handle allocation */
575 /* Check if OPAMP locked */
580 else if (hopamp
->State
== HAL_OPAMP_STATE_BUSYLOCKED
)
586 /* Check the parameter */
587 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp
->Instance
));
589 if (hopamp
->State
== HAL_OPAMP_STATE_READY
)
591 /* Enable the selected opamp */
592 SET_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_OPAMPxEN
);
594 /* Update the OPAMP state*/
595 /* From HAL_OPAMP_STATE_READY to HAL_OPAMP_STATE_BUSY */
596 hopamp
->State
= HAL_OPAMP_STATE_BUSY
;
609 * @brief Stop the opamp
610 * @param hopamp OPAMP handle
613 HAL_StatusTypeDef
HAL_OPAMP_Stop(OPAMP_HandleTypeDef
*hopamp
)
615 HAL_StatusTypeDef status
= HAL_OK
;
617 /* Check the OPAMP handle allocation */
618 /* Check if OPAMP locked */
619 /* Check if OPAMP calibration ongoing */
624 else if (hopamp
->State
== HAL_OPAMP_STATE_BUSYLOCKED
)
628 else if (hopamp
->State
== HAL_OPAMP_STATE_CALIBBUSY
)
634 /* Check the parameter */
635 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp
->Instance
));
637 if (hopamp
->State
== HAL_OPAMP_STATE_BUSY
)
639 /* Disable the selected opamp */
640 CLEAR_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_OPAMPxEN
);
642 /* Update the OPAMP state*/
643 /* From HAL_OPAMP_STATE_BUSY to HAL_OPAMP_STATE_READY*/
644 hopamp
->State
= HAL_OPAMP_STATE_READY
;
655 * @brief Run the self calibration of one OPAMP
656 * @note Calibration is performed in the mode specified in OPAMP init
657 * structure (mode normal or high-speed).
658 * @param hopamp handle
659 * @retval Updated offset trimming values (PMOS & NMOS), user trimming is enabled
661 * @note Calibration runs about 25 ms.
664 HAL_StatusTypeDef
HAL_OPAMP_SelfCalibrate(OPAMP_HandleTypeDef
*hopamp
)
667 HAL_StatusTypeDef status
= HAL_OK
;
669 uint32_t trimmingvaluen
;
670 uint32_t trimmingvaluep
;
673 /* Check the OPAMP handle allocation */
674 /* Check if OPAMP locked */
679 else if (hopamp
->State
== HAL_OPAMP_STATE_BUSYLOCKED
)
686 /* Check if OPAMP in calibration mode and calibration not yet enable */
687 if (hopamp
->State
== HAL_OPAMP_STATE_READY
)
689 /* Check the parameter */
690 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp
->Instance
));
692 /* Set Calibration mode */
693 /* Non-inverting input connected to calibration reference voltage. */
694 SET_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_FORCEVP
);
696 /* user trimming values are used for offset calibration */
697 SET_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_USERTRIM
);
699 /* Enable calibration */
700 SET_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_CALON
);
702 /* 1st calibration - N */
703 /* Select 90% VREF */
704 MODIFY_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_CALSEL
, OPAMP_VREF_90VDDA
);
706 /* Enable the selected opamp */
707 SET_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_OPAMPxEN
);
709 /* Init trimming counter */
711 trimmingvaluen
= 16UL;
716 /* Set candidate trimming */
717 MODIFY_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_TRIMOFFSETN
, trimmingvaluen
<< OPAMP_INPUT_INVERTING
);
719 /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
720 /* Offset trim time: during calibration, minimum time needed between */
721 /* two steps to have 1 mV accuracy */
724 if ((hopamp
->Instance
->CSR
& OPAMP_CSR_OUTCAL
) != 0UL)
726 /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */
727 trimmingvaluen
+= delta
;
731 /* OPAMP_CSR_OUTCAL is LOW try lower trimming */
732 trimmingvaluen
-= delta
;
738 /* Still need to check if righ calibration is current value or un step below */
739 /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0 */
740 MODIFY_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_TRIMOFFSETN
, trimmingvaluen
<< OPAMP_INPUT_INVERTING
);
742 /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
743 /* Offset trim time: during calibration, minimum time needed between */
744 /* two steps to have 1 mV accuracy */
747 if ((hopamp
->Instance
->CSR
& OPAMP_CSR_OUTCAL
) != 0UL)
749 /* OPAMP_CSR_OUTCAL is actually one value more */
751 /* Set right trimming */
752 MODIFY_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_TRIMOFFSETN
, trimmingvaluen
<< OPAMP_INPUT_INVERTING
);
755 /* 2nd calibration - P */
756 /* Select 10% VREF */
757 MODIFY_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_CALSEL
, OPAMP_VREF_10VDDA
);
759 /* Init trimming counter */
761 trimmingvaluep
= 16UL;
766 /* Set candidate trimming */
767 MODIFY_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_TRIMOFFSETP
, trimmingvaluep
<< OPAMP_INPUT_NONINVERTING
);
769 /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
770 /* Offset trim time: during calibration, minimum time needed between */
771 /* two steps to have 1 mV accuracy */
774 if ((hopamp
->Instance
->CSR
& OPAMP_CSR_OUTCAL
) != 0UL)
776 /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */
777 trimmingvaluep
+= delta
;
781 trimmingvaluep
-= delta
;
787 /* Still need to check if righ calibration is current value or un step below */
788 /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0U */
789 /* Set candidate trimming */
790 MODIFY_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_TRIMOFFSETP
, trimmingvaluep
<< OPAMP_INPUT_NONINVERTING
);
792 /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
793 /* Offset trim time: during calibration, minimum time needed between */
794 /* two steps to have 1 mV accuracy */
797 if ((hopamp
->Instance
->CSR
& OPAMP_CSR_OUTCAL
) != 0UL)
799 /* OPAMP_CSR_OUTCAL is actually one value more */
801 /* Set right trimming */
802 MODIFY_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_TRIMOFFSETP
, trimmingvaluep
<< OPAMP_INPUT_NONINVERTING
);
805 /* Disable calibration */
806 CLEAR_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_CALON
);
808 /* Disable the OPAMP */
809 CLEAR_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_OPAMPxEN
);
811 /* Set operating mode */
812 /* Non-inverting input connected to calibration reference voltage. */
813 CLEAR_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_FORCEVP
);
815 /* Self calibration is successful */
816 /* Store calibration(user timming) results in init structure. */
818 /* Write calibration result N */
819 hopamp
->Init
.TrimmingValueN
= trimmingvaluen
;
821 /* Write calibration result P */
822 hopamp
->Init
.TrimmingValueP
= trimmingvaluep
;
824 /* Select user timming mode */
825 /* And updated with calibrated settings */
826 hopamp
->Init
.UserTrimming
= OPAMP_TRIMMING_USER
;
827 MODIFY_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_TRIMOFFSETP
, trimmingvaluep
<< OPAMP_INPUT_NONINVERTING
);
828 MODIFY_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_TRIMOFFSETN
, trimmingvaluen
<< OPAMP_INPUT_INVERTING
);
833 /* OPAMP can not be calibrated from this mode */
844 /** @defgroup OPAMP_Exported_Functions_Group3 Peripheral Control functions
845 * @brief Peripheral Control functions
848 ===============================================================================
849 ##### Peripheral Control functions #####
850 ===============================================================================
852 This subsection provides a set of functions allowing to control the OPAMP data
862 * @brief Lock the selected opamp configuration.
863 * @param hopamp OPAMP handle
866 HAL_StatusTypeDef
HAL_OPAMP_Lock(OPAMP_HandleTypeDef
*hopamp
)
868 HAL_StatusTypeDef status
= HAL_OK
;
870 /* Check the OPAMP handle allocation */
871 /* Check if OPAMP locked */
872 /* OPAMP can be locked when enabled and running in normal mode */
873 /* It is meaningless otherwise */
878 else if (hopamp
->State
!= HAL_OPAMP_STATE_BUSY
)
884 /* Check the parameter */
885 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp
->Instance
));
888 SET_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_LOCK
);
890 /* OPAMP state changed to locked */
891 hopamp
->State
= HAL_OPAMP_STATE_BUSYLOCKED
;
901 * @brief Lock the selected opamp timer controlled mux configuration.
902 * @param hopamp OPAMP handle
905 HAL_StatusTypeDef
HAL_OPAMP_LockTimerMux(OPAMP_HandleTypeDef
*hopamp
)
907 HAL_StatusTypeDef status
= HAL_OK
;
909 /* Check the OPAMP handle allocation */
910 /* Check if OPAMP timer controlled mux is locked */
911 /* OPAMP timer controlled mux can be locked when enabled */
912 /* It is meaningless otherwise */
917 else if (hopamp
->State
== HAL_OPAMP_STATE_RESET
)
921 else if (READ_BIT(hopamp
->Instance
->TCMR
, OPAMP_TCMR_LOCK
) == OPAMP_TCMR_LOCK
)
927 /* Check the parameter */
928 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp
->Instance
));
931 SET_BIT(hopamp
->Instance
->TCMR
, OPAMP_TCMR_LOCK
);
940 /** @defgroup OPAMP_Exported_Functions_Group4 Peripheral State functions
941 * @brief Peripheral State functions
944 ===============================================================================
945 ##### Peripheral State functions #####
946 ===============================================================================
948 This subsection permit to get in run-time the status of the peripheral
956 * @brief Return the OPAMP state
957 * @param hopamp OPAMP handle
960 HAL_OPAMP_StateTypeDef
HAL_OPAMP_GetState(OPAMP_HandleTypeDef
*hopamp
)
962 /* Check the OPAMP handle allocation */
965 return HAL_OPAMP_STATE_RESET
;
968 /* Check the parameter */
969 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp
->Instance
));
971 return hopamp
->State
;
975 * @brief Return the OPAMP factory trimming value
976 * @param hopamp OPAMP handle
977 * @param trimmingoffset Trimming offset (P or N)
978 * @retval Trimming value (P or N): range: 0->31
979 * or OPAMP_FACTORYTRIMMING_DUMMY if trimming value is not available
982 OPAMP_TrimmingValueTypeDef
HAL_OPAMP_GetTrimOffset(OPAMP_HandleTypeDef
*hopamp
, uint32_t trimmingoffset
)
984 uint32_t oldusertrimming
= 0UL;
985 OPAMP_TrimmingValueTypeDef oldtrimmingvaluep
= 0UL, oldtrimmingvaluen
= 0UL, trimmingvalue
;
987 /* Check the OPAMP handle allocation */
988 /* Value can be retrieved in HAL_OPAMP_STATE_READY state */
991 return OPAMP_FACTORYTRIMMING_DUMMY
;
993 else if (hopamp
->State
!= HAL_OPAMP_STATE_READY
)
995 return OPAMP_FACTORYTRIMMING_DUMMY
;
999 /* Check the parameter */
1000 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp
->Instance
));
1001 assert_param(IS_OPAMP_FACTORYTRIMMING(trimmingoffset
));
1003 /* Check the trimming mode */
1004 if ((READ_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_USERTRIM
)) != 0UL)
1006 /* User trimming is used */
1007 oldusertrimming
= OPAMP_TRIMMING_USER
;
1008 /* Store the TrimmingValueP & TrimmingValueN */
1009 oldtrimmingvaluep
= (hopamp
->Instance
->CSR
& OPAMP_CSR_TRIMOFFSETP
) >> OPAMP_INPUT_NONINVERTING
;
1010 oldtrimmingvaluen
= (hopamp
->Instance
->CSR
& OPAMP_CSR_TRIMOFFSETN
) >> OPAMP_INPUT_INVERTING
;
1013 /* Set factory timming mode */
1014 CLEAR_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_USERTRIM
);
1016 /* Get factory trimming */
1017 if (trimmingoffset
== OPAMP_FACTORYTRIMMING_P
)
1019 /* Return TrimOffsetP */
1020 trimmingvalue
= ((hopamp
->Instance
->CSR
& OPAMP_CSR_TRIMOFFSETP
) >> OPAMP_INPUT_NONINVERTING
);
1024 /* Return TrimOffsetN */
1025 trimmingvalue
= ((hopamp
->Instance
->CSR
& OPAMP_CSR_TRIMOFFSETN
) >> OPAMP_INPUT_INVERTING
);
1028 /* Restore user trimming configuration if it was formerly set */
1029 /* Check if user trimming was used */
1030 if (oldusertrimming
== OPAMP_TRIMMING_USER
)
1032 /* Restore user trimming */
1033 SET_BIT(hopamp
->Instance
->CSR
, OPAMP_CSR_USERTRIM
);
1034 MODIFY_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_TRIMOFFSETP
, oldtrimmingvaluep
<< OPAMP_INPUT_NONINVERTING
);
1035 MODIFY_REG(hopamp
->Instance
->CSR
, OPAMP_CSR_TRIMOFFSETN
, oldtrimmingvaluen
<< OPAMP_INPUT_INVERTING
);
1038 return trimmingvalue
;
1044 #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
1046 * @brief Register a User OPAMP Callback
1047 * To be used instead of the weak (surcharged) predefined callback
1048 * @param hopamp : OPAMP handle
1049 * @param CallbackID : ID of the callback to be registered
1050 * This parameter can be one of the following values:
1051 * @arg @ref HAL_OPAMP_MSP_INIT_CB_ID OPAMP MspInit callback ID
1052 * @arg @ref HAL_OPAMP_MSP_DEINIT_CB_ID OPAMP MspDeInit callback ID
1053 * @param pCallback : pointer to the Callback function
1056 HAL_StatusTypeDef
HAL_OPAMP_RegisterCallback(OPAMP_HandleTypeDef
*hopamp
, HAL_OPAMP_CallbackIDTypeDef CallbackId
,
1057 pOPAMP_CallbackTypeDef pCallback
)
1059 HAL_StatusTypeDef status
= HAL_OK
;
1061 if (pCallback
== NULL
)
1066 /* Process locked */
1069 if (hopamp
->State
== HAL_OPAMP_STATE_READY
)
1073 case HAL_OPAMP_MSP_INIT_CB_ID
:
1074 hopamp
->MspInitCallback
= pCallback
;
1076 case HAL_OPAMP_MSP_DEINIT_CB_ID
:
1077 hopamp
->MspDeInitCallback
= pCallback
;
1080 /* update return status */
1085 else if (hopamp
->State
== HAL_OPAMP_STATE_RESET
)
1089 case HAL_OPAMP_MSP_INIT_CB_ID
:
1090 hopamp
->MspInitCallback
= pCallback
;
1092 case HAL_OPAMP_MSP_DEINIT_CB_ID
:
1093 hopamp
->MspDeInitCallback
= pCallback
;
1096 /* update return status */
1103 /* update return status */
1108 __HAL_UNLOCK(hopamp
);
1113 * @brief Unregister a User OPAMP Callback
1114 * OPAMP Callback is redirected to the weak (surcharged) predefined callback
1115 * @param hopamp : OPAMP handle
1116 * @param CallbackID : ID of the callback to be unregistered
1117 * This parameter can be one of the following values:
1118 * @arg @ref HAL_OPAMP_MSP_INIT_CB_ID OPAMP MSP Init Callback ID
1119 * @arg @ref HAL_OPAMP_MSP_DEINIT_CB_ID OPAMP MSP DeInit Callback ID
1120 * @arg @ref HAL_OPAMP_ALL_CB_ID OPAMP All Callbacks
1124 HAL_StatusTypeDef
HAL_OPAMP_UnRegisterCallback(OPAMP_HandleTypeDef
*hopamp
, HAL_OPAMP_CallbackIDTypeDef CallbackId
)
1126 HAL_StatusTypeDef status
= HAL_OK
;
1128 /* Process locked */
1131 if (hopamp
->State
== HAL_OPAMP_STATE_READY
)
1135 case HAL_OPAMP_MSP_INIT_CB_ID
:
1136 hopamp
->MspInitCallback
= HAL_OPAMP_MspInit
;
1138 case HAL_OPAMP_MSP_DEINIT_CB_ID
:
1139 hopamp
->MspDeInitCallback
= HAL_OPAMP_MspDeInit
;
1141 case HAL_OPAMP_ALL_CB_ID
:
1142 hopamp
->MspInitCallback
= HAL_OPAMP_MspInit
;
1143 hopamp
->MspDeInitCallback
= HAL_OPAMP_MspDeInit
;
1146 /* update return status */
1151 else if (hopamp
->State
== HAL_OPAMP_STATE_RESET
)
1155 case HAL_OPAMP_MSP_INIT_CB_ID
:
1156 hopamp
->MspInitCallback
= HAL_OPAMP_MspInit
;
1158 case HAL_OPAMP_MSP_DEINIT_CB_ID
:
1159 hopamp
->MspDeInitCallback
= HAL_OPAMP_MspDeInit
;
1162 /* update return status */
1169 /* update return status */
1174 __HAL_UNLOCK(hopamp
);
1178 #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
1188 #endif /* HAL_OPAMP_MODULE_ENABLED */
1195 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/