Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32F3 / Drivers / STM32F3xx_HAL_Driver / Src / stm32f3xx_hal_opamp.c
blob3a69f7fb975dc006f4eef89d355c256f28f2c577
1 /**
2 ******************************************************************************
3 * @file stm32f3xx_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,...OPAMP4)
8 * peripheral:
9 * + OPAMP Configuration
10 * + OPAMP calibration
11 * Thanks to
12 * + Initialization/de-initialization functions
13 * + I/O operation functions
14 * + Peripheral Control functions
15 * + Peripheral State functions
17 @verbatim
18 ================================================================================
19 ##### OPAMP Peripheral Features #####
20 ================================================================================
22 [..] The device integrates up to 4 operational amplifiers OPAMP1, OPAMP2,
23 OPAMP3 and OPAMP4:
25 (#) The OPAMP(s) provides several exclusive running modes.
26 (++) Standalone mode
27 (++) Programmable Gain Amplifier (PGA) mode (Resistor feedback output)
28 (++) Follower mode
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 (90U% of VDDA for NMOS transistors, 10U% 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
43 results.
44 The user may store the calibration results for larger
45 (ex monitoring the trimming as a function of temperature
46 for instance)
47 (++) for STM32F3 devices having 2 or 4 OPAMPs
48 HAL_OPAMPEx_SelfCalibrateAll
49 runs calibration of 2 or 4 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 between inverting
54 and non-inverting input.
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 CC6 provides the alternate switching tempo between defaults
58 (primary) and secondary inputs.
60 (#) Running mode: Standalone mode
61 (++) Gain is set externally (gain depends on external loads).
62 (++) Follower mode also possible externally by connecting the inverting input to
63 the output.
65 (#) Running mode: Follower mode
66 (++) No Inverting Input is connected.
68 (#) Running mode: Programmable Gain Amplifier (PGA) mode
69 (Resistor feedback output)
70 (++) The OPAMP(s) output(s) can be internally connected to resistor feedback
71 output.
72 (++) OPAMP gain is either 2U, 4U, 8 or 16.
75 ##### How to use this driver #####
76 ================================================================================
77 [..]
78 *** Calibration ***
79 ============================================
80 [..]
81 To run the opamp calibration self calibration:
83 (#) Start calibration using HAL_OPAMP_SelfCalibrate.
84 Store the calibration results.
86 *** Running mode ***
87 ============================================
88 [..]
89 To use the opamp, perform the following steps:
91 (#) Fill in the HAL_OPAMP_MspInit() to
92 (++) Configure the opamp input AND output in analog mode using
93 HAL_GPIO_Init() to map the opamp output to the GPIO pin.
95 (#) Configure the opamp using HAL_OPAMP_Init() function:
96 (++) Select the mode
97 (++) Select the inverting input
98 (++) Select the non-inverting input
99 (++) Select if the Timer controlled Mux mode is enabled/disabled
100 (++) If the Timer controlled Mux mode is enabled, select the secondary inverting input
101 (++) If the Timer controlled Mux mode is enabled, Select the secondary non-inverting input
102 (++) If PGA mode is enabled, Select if inverting input is connected.
103 (++) Select either factory or user defined trimming mode.
104 (++) If the user defined trimming mode is enabled, select PMOS & NMOS trimming values
105 (typ. settings returned by HAL_OPAMP_SelfCalibrate function).
107 (#) Enable the opamp using HAL_OPAMP_Start() function.
109 (#) Disable the opamp using HAL_OPAMP_Stop() function.
111 (#) Lock the opamp in running mode using HAL_OPAMP_Lock() function. From then The configuration
112 can be modified
113 (++) After HW reset
114 (++) OR thanks to HAL_OPAMP_MspDeInit called (user defined) from HAL_OPAMP_DeInit.
116 *** Running mode: change of configuration while OPAMP ON ***
117 ============================================
118 [..]
119 To Re-configure OPAMP when OPAMP is ON (change on the fly)
120 (#) If needed, Fill in the HAL_OPAMP_MspInit()
121 (++) This is the case for instance if you wish to use new OPAMP I/O
123 (#) Configure the opamp using HAL_OPAMP_Init() function:
124 (++) As in configure case, selects first the parameters you wish to modify.
126 @endverbatim
127 ******************************************************************************
128 * @attention
130 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
132 * Redistribution and use in source and binary forms, with or without modification,
133 * are permitted provided that the following conditions are met:
134 * 1. Redistributions of source code must retain the above copyright notice,
135 * this list of conditions and the following disclaimer.
136 * 2. Redistributions in binary form must reproduce the above copyright notice,
137 * this list of conditions and the following disclaimer in the documentation
138 * and/or other materials provided with the distribution.
139 * 3. Neither the name of STMicroelectronics nor the names of its contributors
140 * may be used to endorse or promote products derived from this software
141 * without specific prior written permission.
143 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
144 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
145 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
146 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
147 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
148 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
149 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
150 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
151 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
152 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
154 ******************************************************************************
158 Additional Tables:
159 The OPAMPs non inverting input (both default and secondary) can be
160 selected among the list shown by table below.
162 The OPAMPs non inverting input (both default and secondary) can be
163 selected among the list shown by table below.
165 Table 1. OPAMPs inverting/non-inverting inputs for the STM32F3 devices:
166 +--------------------------------------------------------------+
167 | | | OPAMP1 | OPAMP2 | OPAMP3 | OPAMP4 |
168 |-----------------|--------|--------|--------|--------|--------|
169 | | No conn| X | X | X | X |
170 | Inverting Input | VM0 | PC5 | PC5 | PB10 | PB10 |
171 | (1) | VM1 | PA3 | PA5 | PB2 | PD8 |
172 |-----------------|--------|--------|--------|--------|--------|
173 | | VP0 | PA1 | PA7 | PB0 | PB13 |
174 | Non Inverting | VP1 | PA7 | PD14 | PB13 | PD11 |
175 | Input | VP2 | PA3 | PB0 | PA1 | PA4 |
176 | | VP3 | PA5 | PB14 | PA5 | PB11 |
177 +--------------------------------------------------------------+
178 (1): NA in follower mode.
180 Table 2. OPAMPs outputs for the STM32F3 devices:
181 +--------------------------------------------------------------+
182 | | | OPAMP1 | OPAMP2 | OPAMP3 | OPAMP4 |
183 |-----------------|--------|--------|--------|--------|--------|
184 | Output | | PA2 | PA6 | PB1 | PB12 |
185 |-----------------|--------|--------|--------|--------|--------|
189 /* Includes ------------------------------------------------------------------*/
190 #include "stm32f3xx_hal.h"
192 /** @addtogroup STM32F3xx_HAL_Driver
193 * @{
196 #ifdef HAL_OPAMP_MODULE_ENABLED
198 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
199 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
200 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
201 defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
203 /** @defgroup OPAMP OPAMP
204 * @brief OPAMP HAL module driver
205 * @{
208 /* Private typedef -----------------------------------------------------------*/
209 /* Private define ------------------------------------------------------------*/
210 /** @defgroup OPAMP_Private_Define OPAMP Private Define
211 * @{
213 /* CSR register reset value */
214 #define OPAMP_CSR_RESET_VALUE (0x00000000U)
216 * @}
219 /* Private macro -------------------------------------------------------------*/
220 /* Private variables ---------------------------------------------------------*/
221 /* Private function prototypes -----------------------------------------------*/
222 /* Exported functions ---------------------------------------------------------*/
224 /** @defgroup OPAMP_Exported_Functions OPAMP Exported Functions
225 * @{
228 /** @defgroup OPAMP_Exported_Functions_Group1 Initialization and de-initialization functions
229 * @brief Initialization and Configuration functions
231 @verbatim
232 ===============================================================================
233 ##### Initialization and de-initialization functions #####
234 ===============================================================================
235 [..] This section provides functions allowing to:
237 @endverbatim
238 * @{
242 * @brief Initializes the OPAMP according to the specified
243 * parameters in the OPAMP_InitTypeDef and create the associated handle.
244 * @note If the selected opamp is locked, initialization can't be performed.
245 * To unlock the configuration, perform a system reset.
246 * @param hopamp OPAMP handle
247 * @retval HAL status
249 HAL_StatusTypeDef HAL_OPAMP_Init(OPAMP_HandleTypeDef *hopamp)
252 HAL_StatusTypeDef status = HAL_OK;
254 /* Check the OPAMP handle allocation and lock status */
255 /* Init not allowed if calibration is ongoing */
256 if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED) \
257 || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY))
259 return HAL_ERROR;
261 else
264 /* Check the parameter */
265 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
267 /* Set OPAMP parameters */
268 assert_param(IS_OPAMP_FUNCTIONAL_NORMALMODE(hopamp->Init.Mode));
269 assert_param(IS_OPAMP_NONINVERTING_INPUT(hopamp->Init.NonInvertingInput));
270 if ((hopamp->Init.Mode) == OPAMP_STANDALONE_MODE)
272 assert_param(IS_OPAMP_INVERTING_INPUT(hopamp->Init.InvertingInput));
275 assert_param(IS_OPAMP_TIMERCONTROLLED_MUXMODE(hopamp->Init.TimerControlledMuxmode));
277 if ((hopamp->Init.TimerControlledMuxmode) == OPAMP_TIMERCONTROLLEDMUXMODE_ENABLE)
279 assert_param(IS_OPAMP_SEC_NONINVERTINGINPUT(hopamp->Init.NonInvertingInputSecondary));
280 if ((hopamp->Init.Mode) == OPAMP_STANDALONE_MODE)
282 assert_param(IS_OPAMP_SEC_INVERTINGINPUT(hopamp->Init.InvertingInputSecondary));
286 if ((hopamp->Init.Mode) == OPAMP_PGA_MODE)
288 assert_param(IS_OPAMP_PGACONNECT(hopamp->Init.PgaConnect));
289 assert_param(IS_OPAMP_PGA_GAIN(hopamp->Init.PgaGain));
292 assert_param(IS_OPAMP_TRIMMING(hopamp->Init.UserTrimming));
293 if ((hopamp->Init.UserTrimming) == OPAMP_TRIMMING_USER)
295 assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueP));
296 assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueN));
299 /* Init SYSCFG and the low level hardware to access opamp */
300 __HAL_RCC_SYSCFG_CLK_ENABLE();
302 if(hopamp->State == HAL_OPAMP_STATE_RESET)
304 /* Allocate lock resource and initialize it */
305 hopamp->Lock = HAL_UNLOCKED;
308 /* Call MSP init function */
309 HAL_OPAMP_MspInit(hopamp);
311 /* Set OPAMP parameters */
312 /* Set bits according to hopamp->hopamp->Init.Mode value */
313 /* Set bits according to hopamp->hopamp->Init.InvertingInput value */
314 /* Set bits according to hopamp->hopamp->Init.NonInvertingInput value */
315 /* Set bits according to hopamp->hopamp->Init.TimerControlledMuxmode value */
316 /* Set bits according to hopamp->hopamp->Init.InvertingInputSecondary value */
317 /* Set bits according to hopamp->hopamp->Init.NonInvertingInputSecondary value */
318 /* Set bits according to hopamp->hopamp->Init.PgaConnect value */
319 /* Set bits according to hopamp->hopamp->Init.PgaGain value */
320 /* Set bits according to hopamp->hopamp->Init.UserTrimming value */
321 /* Set bits according to hopamp->hopamp->Init.TrimmingValueP value */
322 /* Set bits according to hopamp->hopamp->Init.TrimmingValueN value */
325 /* check if OPAMP_PGA_MODE & in Follower mode */
326 /* - InvertingInput */
327 /* - InvertingInputSecondary */
328 /* are Not Applicable */
330 if ((hopamp->Init.Mode == OPAMP_PGA_MODE) || (hopamp->Init.Mode == OPAMP_FOLLOWER_MODE))
332 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_UPDATE_PARAMETERS_INIT_MASK, \
333 hopamp->Init.Mode | \
334 hopamp->Init.NonInvertingInput | \
335 hopamp->Init.TimerControlledMuxmode | \
336 hopamp->Init.NonInvertingInputSecondary | \
337 hopamp->Init.PgaConnect | \
338 hopamp->Init.PgaGain | \
339 hopamp->Init.UserTrimming | \
340 (hopamp->Init.TrimmingValueP << OPAMP_INPUT_NONINVERTING) | \
341 (hopamp->Init.TrimmingValueN << OPAMP_INPUT_INVERTING));
344 else /* OPAMP_STANDALONE_MODE */
346 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_UPDATE_PARAMETERS_INIT_MASK, \
347 hopamp->Init.Mode | \
348 hopamp->Init.InvertingInput | \
349 hopamp->Init.NonInvertingInput | \
350 hopamp->Init.TimerControlledMuxmode | \
351 hopamp->Init.InvertingInputSecondary | \
352 hopamp->Init.NonInvertingInputSecondary | \
353 hopamp->Init.PgaConnect | \
354 hopamp->Init.PgaGain | \
355 hopamp->Init.UserTrimming | \
356 (hopamp->Init.TrimmingValueP << OPAMP_INPUT_NONINVERTING) | \
357 (hopamp->Init.TrimmingValueN << OPAMP_INPUT_INVERTING));
360 /* Update the OPAMP state*/
361 if (hopamp->State == HAL_OPAMP_STATE_RESET)
363 /* From RESET state to READY State */
364 hopamp->State = HAL_OPAMP_STATE_READY;
366 /* else: remain in READY or BUSY state (no update) */
368 return status;
374 * @brief DeInitializes the OPAMP peripheral
375 * @note Deinitialization can't be performed if the OPAMP configuration is locked.
376 * To unlock the configuration, perform a system reset.
377 * @param hopamp OPAMP handle
378 * @retval HAL status
380 HAL_StatusTypeDef HAL_OPAMP_DeInit(OPAMP_HandleTypeDef *hopamp)
382 HAL_StatusTypeDef status = HAL_OK;
384 /* Check the OPAMP handle allocation */
385 /* DeInit not allowed if calibration is ongoing */
386 if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY))
388 status = HAL_ERROR;
390 else
392 /* Check the parameter */
393 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
395 /* Set OPAMP_CSR register to reset value */
396 WRITE_REG(hopamp->Instance->CSR, OPAMP_CSR_RESET_VALUE);
398 /* DeInit the low level hardware: GPIO, CLOCK and NVIC */
399 /* When OPAMP is locked, unlocking can be achieved thanks to */
400 /* __HAL_RCC_SYSCFG_CLK_DISABLE() call within HAL_OPAMP_MspDeInit */
401 /* Note that __HAL_RCC_SYSCFG_CLK_DISABLE() also disables comparator */
402 HAL_OPAMP_MspDeInit(hopamp);
404 if (OPAMP_CSR_RESET_VALUE == hopamp->Instance->CSR)
406 /* Update the OPAMP state */
407 hopamp->State = HAL_OPAMP_STATE_RESET;
409 else /* RESET STATE */
411 /* DeInit not complete */
412 /* It can be the case if OPAMP was formerly locked */
413 status = HAL_ERROR;
415 /* The OPAMP state is NOT updated */
418 /* Process unlocked */
419 __HAL_UNLOCK(hopamp);
422 return status;
426 * @brief Initializes the OPAMP MSP.
427 * @param hopamp OPAMP handle
428 * @retval None
430 __weak void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef *hopamp)
432 /* Prevent unused argument(s) compilation warning */
433 UNUSED(hopamp);
435 /* NOTE : This function should not be modified, when the callback is needed,
436 the HAL_OPAMP_MspInit could be implemented in the user file
439 /* Example */
443 * @brief DeInitializes OPAMP MSP.
444 * @param hopamp OPAMP handle
445 * @retval None
447 __weak void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef *hopamp)
449 /* Prevent unused argument(s) compilation warning */
450 UNUSED(hopamp);
452 /* NOTE : This function should not be modified, when the callback is needed,
453 the HAL_OPAMP_MspDeInit could be implemented in the user file
459 * @}
463 /** @defgroup OPAMP_Exported_Functions_Group2 Input and Output operation functions
464 * @brief Data transfers functions
466 @verbatim
467 ===============================================================================
468 ##### IO operation functions #####
469 ===============================================================================
470 [..]
471 This subsection provides a set of functions allowing to manage the OPAMP data
472 transfers.
474 @endverbatim
475 * @{
479 * @brief Start the opamp
480 * @param hopamp OPAMP handle
481 * @retval HAL status
484 HAL_StatusTypeDef HAL_OPAMP_Start(OPAMP_HandleTypeDef *hopamp)
486 HAL_StatusTypeDef status = HAL_OK;
488 /* Check the OPAMP handle allocation */
489 /* Check if OPAMP locked */
490 if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED))
493 status = HAL_ERROR;
495 else
497 /* Check the parameter */
498 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
500 if(hopamp->State == HAL_OPAMP_STATE_READY)
502 /* Enable the selected opamp */
503 SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
505 /* Update the OPAMP state*/
506 /* From HAL_OPAMP_STATE_READY to HAL_OPAMP_STATE_BUSY */
507 hopamp->State = HAL_OPAMP_STATE_BUSY;
509 else
511 status = HAL_ERROR;
516 return status;
520 * @brief Stop the opamp
521 * @param hopamp OPAMP handle
522 * @retval HAL status
524 HAL_StatusTypeDef HAL_OPAMP_Stop(OPAMP_HandleTypeDef *hopamp)
526 HAL_StatusTypeDef status = HAL_OK;
528 /* Check the OPAMP handle allocation */
529 /* Check if OPAMP locked */
530 /* Check if OPAMP calibration ongoing */
531 if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED) \
532 || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY))
534 status = HAL_ERROR;
536 else
538 /* Check the parameter */
539 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
541 if(hopamp->State == HAL_OPAMP_STATE_BUSY)
543 /* Disable the selected opamp */
544 CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
546 /* Update the OPAMP state*/
547 /* From HAL_OPAMP_STATE_BUSY to HAL_OPAMP_STATE_READY*/
548 hopamp->State = HAL_OPAMP_STATE_READY;
550 else
552 status = HAL_ERROR;
555 return status;
559 * @brief Run the self calibration of one OPAMP
560 * @param hopamp handle
561 * @retval Updated offset trimming values (PMOS & NMOS), user trimming is enabled
562 * @retval HAL status
563 * @note Calibration runs about 25 ms.
566 HAL_StatusTypeDef HAL_OPAMP_SelfCalibrate(OPAMP_HandleTypeDef *hopamp)
569 HAL_StatusTypeDef status = HAL_OK;
571 uint32_t trimmingvaluen = 0U;
572 uint32_t trimmingvaluep = 0U;
573 uint32_t delta;
575 /* Check the OPAMP handle allocation */
576 /* Check if OPAMP locked */
577 if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED))
579 status = HAL_ERROR;
581 else
584 /* Check if OPAMP in calibration mode and calibration not yet enable */
585 if(hopamp->State == HAL_OPAMP_STATE_READY)
587 /* Check the parameter */
588 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
590 /* Set Calibration mode */
591 /* Non-inverting input connected to calibration reference voltage. */
592 SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_FORCEVP);
594 /* user trimming values are used for offset calibration */
595 SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_USERTRIM);
597 /* Enable calibration */
598 SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_CALON);
600 /* 1st calibration - N */
601 /* Select 90U% VREF */
602 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_90VDDA);
604 /* Enable the selected opamp */
605 SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
607 /* Init trimming counter */
608 /* Medium value */
609 trimmingvaluen = 16U;
610 delta = 8U;
612 while (delta != 0U)
614 /* Set candidate trimming */
615 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen<<OPAMP_INPUT_INVERTING);
617 /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
618 /* Offset trim time: during calibration, minimum time needed between */
619 /* two steps to have 1 mV accuracy */
620 HAL_Delay(2U);
622 if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET)
624 /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */
625 trimmingvaluen += delta;
627 else
629 /* OPAMP_CSR_OUTCAL is LOW try lower trimming */
630 trimmingvaluen -= delta;
633 delta >>= 1U;
636 /* Still need to check if righ calibration is current value or un step below */
637 /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0 */
638 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen<<OPAMP_INPUT_INVERTING);
640 /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
641 /* Offset trim time: during calibration, minimum time needed between */
642 /* two steps to have 1 mV accuracy */
643 HAL_Delay(2U);
645 if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET)
647 /* OPAMP_CSR_OUTCAL is actually one value more */
648 trimmingvaluen++;
649 /* Set right trimming */
650 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen<<OPAMP_INPUT_INVERTING);
653 /* 2nd calibration - P */
654 /* Select 10U% VREF */
655 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_10VDDA);
657 /* Init trimming counter */
658 /* Medium value */
659 trimmingvaluep = 16U;
660 delta = 8U;
662 while (delta != 0U)
664 /* Set candidate trimming */
665 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep<<OPAMP_INPUT_NONINVERTING);
667 /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
668 /* Offset trim time: during calibration, minimum time needed between */
669 /* two steps to have 1 mV accuracy */
670 HAL_Delay(2U);
672 if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET)
674 /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */
675 trimmingvaluep += delta;
677 else
679 trimmingvaluep -= delta;
682 delta >>= 1U;
685 /* Still need to check if righ calibration is current value or un step below */
686 /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0U */
687 /* Set candidate trimming */
688 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep<<OPAMP_INPUT_NONINVERTING);
690 /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
691 /* Offset trim time: during calibration, minimum time needed between */
692 /* two steps to have 1 mV accuracy */
693 HAL_Delay(2U);
695 if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET)
697 /* OPAMP_CSR_OUTCAL is actually one value more */
698 trimmingvaluep++;
699 /* Set right trimming */
700 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep<<OPAMP_INPUT_NONINVERTING);
703 /* Disable calibration */
704 CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_CALON);
706 /* Disable the OPAMP */
707 CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
709 /* Set operating mode */
710 /* Non-inverting input connected to calibration reference voltage. */
711 CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_FORCEVP);
713 /* Self calibration is successful */
714 /* Store calibration(user timming) results in init structure. */
716 /* Write calibration result N */
717 hopamp->Init.TrimmingValueN = trimmingvaluen;
719 /* Write calibration result P */
720 hopamp->Init.TrimmingValueP = trimmingvaluep;
722 /* Select user timming mode */
723 /* And updated with calibrated settings */
724 hopamp->Init.UserTrimming = OPAMP_TRIMMING_USER;
725 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep<<OPAMP_INPUT_NONINVERTING);
726 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen<<OPAMP_INPUT_INVERTING);
729 else
731 /* OPAMP can not be calibrated from this mode */
732 status = HAL_ERROR;
735 return status;
739 * @}
742 /** @defgroup OPAMP_Exported_Functions_Group3 Peripheral Control functions
743 * @brief management functions
745 @verbatim
746 ===============================================================================
747 ##### Peripheral Control functions #####
748 ===============================================================================
749 [..]
750 This subsection provides a set of functions allowing to control the OPAMP data
751 transfers.
755 @endverbatim
756 * @{
760 * @brief Lock the selected opamp configuration.
761 * @param hopamp OPAMP handle
762 * @retval HAL status
764 HAL_StatusTypeDef HAL_OPAMP_Lock(OPAMP_HandleTypeDef *hopamp)
766 HAL_StatusTypeDef status = HAL_OK;
768 /* Check the OPAMP handle allocation */
769 /* Check if OPAMP locked */
770 /* OPAMP can be locked when enabled and running in normal mode */
771 /* It is meaningless otherwise */
772 if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_RESET) \
773 || (hopamp->State == HAL_OPAMP_STATE_READY) \
774 || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)\
775 || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED))
778 status = HAL_ERROR;
781 else
783 /* Check the parameter */
784 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
786 /* Lock OPAMP */
787 SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_LOCK);
789 /* OPAMP state changed to locked */
790 hopamp->State = HAL_OPAMP_STATE_BUSYLOCKED;
792 return status;
796 * @}
799 /** @defgroup OPAMP_Exported_Functions_Group4 Peripheral State functions
800 * @brief Peripheral State functions
802 @verbatim
803 ===============================================================================
804 ##### Peripheral State functions #####
805 ===============================================================================
806 [..]
807 This subsection permit to get in run-time the status of the peripheral
808 and the data flow.
810 @endverbatim
811 * @{
815 * @brief Return the OPAMP state
816 * @param hopamp OPAMP handle
817 * @retval HAL state
819 HAL_OPAMP_StateTypeDef HAL_OPAMP_GetState(OPAMP_HandleTypeDef *hopamp)
821 /* Check the OPAMP handle allocation */
822 if(hopamp == NULL)
824 return HAL_OPAMP_STATE_RESET;
827 /* Check the parameter */
828 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
830 return hopamp->State;
834 * @brief Return the OPAMP factory trimming value
835 * @param hopamp OPAMP handle
836 * @param trimmingoffset Trimming offset (P or N)
837 * @retval Trimming value (P or N): range: 0->31
838 * or OPAMP_FACTORYTRIMMING_DUMMY if trimming value is not available
841 OPAMP_TrimmingValueTypeDef HAL_OPAMP_GetTrimOffset (OPAMP_HandleTypeDef *hopamp, uint32_t trimmingoffset)
843 uint32_t oldusertrimming = 0U;
844 OPAMP_TrimmingValueTypeDef oldtrimmingvaluep = 0U, oldtrimmingvaluen = 0U, trimmingvalue = 0U;
846 /* Check the OPAMP handle allocation */
847 /* Value can be retrieved in HAL_OPAMP_STATE_READY state */
848 if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_RESET) \
849 || (hopamp->State == HAL_OPAMP_STATE_BUSY) \
850 || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)\
851 || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED))
853 return OPAMP_FACTORYTRIMMING_DUMMY;
855 else
857 /* Check the parameter */
858 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
859 assert_param(IS_OPAMP_FACTORYTRIMMING(trimmingoffset));
861 /* Check the trimming mode */
862 if ((READ_BIT(hopamp->Instance->CSR,OPAMP_CSR_USERTRIM)) != RESET)
864 /* User trimming is used */
865 oldusertrimming = OPAMP_TRIMMING_USER;
866 /* Store the TrimmingValueP & TrimmingValueN */
867 oldtrimmingvaluep = (hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETP) >> OPAMP_INPUT_NONINVERTING;
868 oldtrimmingvaluen = (hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETN) >> OPAMP_INPUT_INVERTING;
871 /* Set factory timming mode */
872 CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_USERTRIM);
874 /* Get factory trimming */
875 if (trimmingoffset == OPAMP_FACTORYTRIMMING_P)
877 /* Return TrimOffsetP */
878 trimmingvalue = ((hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETP) >> OPAMP_INPUT_NONINVERTING);
880 else
882 /* Return TrimOffsetN */
883 trimmingvalue = ((hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETN) >> OPAMP_INPUT_INVERTING);
886 /* Restore user trimming configuration if it was formerly set */
887 /* Check if user trimming was used */
888 if (oldusertrimming == OPAMP_TRIMMING_USER)
890 /* Restore user trimming */
891 SET_BIT(hopamp->Instance->CSR,OPAMP_CSR_USERTRIM);
892 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, oldtrimmingvaluep<<OPAMP_INPUT_NONINVERTING);
893 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, oldtrimmingvaluen<<OPAMP_INPUT_INVERTING);
896 return trimmingvalue;
899 * @}
903 * @}
907 * @}
910 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
911 /* STM32F302xC || STM32F303xC || STM32F358xx || */
912 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
913 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
915 #endif /* HAL_OPAMP_MODULE_ENABLED */
917 * @}
922 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/