Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HAL_L476/stm32l4xx_hal_opamp.c@1:d0dfbce63a89, 2017-02-24 (annotated)
- Committer:
- elmot
- Date:
- Fri Feb 24 21:13:56 2017 +0000
- Revision:
- 1:d0dfbce63a89
Ready-to-copy
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| elmot | 1:d0dfbce63a89 | 1 | /** |
| elmot | 1:d0dfbce63a89 | 2 | ****************************************************************************** |
| elmot | 1:d0dfbce63a89 | 3 | * @file stm32l4xx_hal_opamp.c |
| elmot | 1:d0dfbce63a89 | 4 | * @author MCD Application Team |
| elmot | 1:d0dfbce63a89 | 5 | * @version V1.5.1 |
| elmot | 1:d0dfbce63a89 | 6 | * @date 31-May-2016 |
| elmot | 1:d0dfbce63a89 | 7 | * @brief OPAMP HAL module driver. |
| elmot | 1:d0dfbce63a89 | 8 | * This file provides firmware functions to manage the following |
| elmot | 1:d0dfbce63a89 | 9 | * functionalities of the operational amplifier(s) peripheral: |
| elmot | 1:d0dfbce63a89 | 10 | * + OPAMP configuration |
| elmot | 1:d0dfbce63a89 | 11 | * + OPAMP calibration |
| elmot | 1:d0dfbce63a89 | 12 | * Thanks to |
| elmot | 1:d0dfbce63a89 | 13 | * + Initialization and de-initialization functions |
| elmot | 1:d0dfbce63a89 | 14 | * + IO operation functions |
| elmot | 1:d0dfbce63a89 | 15 | * + Peripheral Control functions |
| elmot | 1:d0dfbce63a89 | 16 | * + Peripheral State functions |
| elmot | 1:d0dfbce63a89 | 17 | * |
| elmot | 1:d0dfbce63a89 | 18 | @verbatim |
| elmot | 1:d0dfbce63a89 | 19 | ================================================================================ |
| elmot | 1:d0dfbce63a89 | 20 | ##### OPAMP Peripheral Features ##### |
| elmot | 1:d0dfbce63a89 | 21 | ================================================================================ |
| elmot | 1:d0dfbce63a89 | 22 | |
| elmot | 1:d0dfbce63a89 | 23 | [..] The device integrates 1 or 2 operational amplifiers OPAMP1 & OPAMP2 |
| elmot | 1:d0dfbce63a89 | 24 | |
| elmot | 1:d0dfbce63a89 | 25 | (#) The OPAMP(s) provide(s) several exclusive running modes. |
| elmot | 1:d0dfbce63a89 | 26 | (++) 1 OPAMP: STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx |
| elmot | 1:d0dfbce63a89 | 27 | (++) 2 OPAMP: STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx |
| elmot | 1:d0dfbce63a89 | 28 | |
| elmot | 1:d0dfbce63a89 | 29 | (#) The OPAMP(s) provide(s) several exclusive running modes. |
| elmot | 1:d0dfbce63a89 | 30 | (++) Standalone mode |
| elmot | 1:d0dfbce63a89 | 31 | (++) Programmable Gain Amplifier (PGA) mode (Resistor feedback output) |
| elmot | 1:d0dfbce63a89 | 32 | (++) Follower mode |
| elmot | 1:d0dfbce63a89 | 33 | |
| elmot | 1:d0dfbce63a89 | 34 | (#) All OPAMP (same for all OPAMPs) can operate in |
| elmot | 1:d0dfbce63a89 | 35 | (++) Either Low range (VDDA < 2.4V) power supply |
| elmot | 1:d0dfbce63a89 | 36 | (++) Or High range (VDDA > 2.4V) power supply |
| elmot | 1:d0dfbce63a89 | 37 | |
| elmot | 1:d0dfbce63a89 | 38 | (#) Each OPAMP(s) can be configured in normal and low power mode. |
| elmot | 1:d0dfbce63a89 | 39 | |
| elmot | 1:d0dfbce63a89 | 40 | (#) The OPAMP(s) provide(s) calibration capabilities. |
| elmot | 1:d0dfbce63a89 | 41 | (++) Calibration aims at correcting some offset for running mode. |
| elmot | 1:d0dfbce63a89 | 42 | (++) The OPAMP uses either factory calibration settings OR user defined |
| elmot | 1:d0dfbce63a89 | 43 | calibration (trimming) settings (i.e. trimming mode). |
| elmot | 1:d0dfbce63a89 | 44 | (++) The user defined settings can be figured out using self calibration |
| elmot | 1:d0dfbce63a89 | 45 | handled by HAL_OPAMP_SelfCalibrate, HAL_OPAMPEx_SelfCalibrateAll |
| elmot | 1:d0dfbce63a89 | 46 | (++) HAL_OPAMP_SelfCalibrate: |
| elmot | 1:d0dfbce63a89 | 47 | (+++) Runs automatically the calibration. |
| elmot | 1:d0dfbce63a89 | 48 | (+++) Enables the user trimming mode |
| elmot | 1:d0dfbce63a89 | 49 | (+++) Updates the init structure with trimming values with fresh calibration |
| elmot | 1:d0dfbce63a89 | 50 | results. |
| elmot | 1:d0dfbce63a89 | 51 | The user may store the calibration results for larger |
| elmot | 1:d0dfbce63a89 | 52 | (ex monitoring the trimming as a function of temperature |
| elmot | 1:d0dfbce63a89 | 53 | for instance) |
| elmot | 1:d0dfbce63a89 | 54 | (+++) HAL_OPAMPEx_SelfCalibrateAll |
| elmot | 1:d0dfbce63a89 | 55 | runs calibration of all OPAMPs in parallel to save search time. |
| elmot | 1:d0dfbce63a89 | 56 | |
| elmot | 1:d0dfbce63a89 | 57 | (#) Running mode: Standalone mode |
| elmot | 1:d0dfbce63a89 | 58 | (++) Gain is set externally (gain depends on external loads). |
| elmot | 1:d0dfbce63a89 | 59 | (++) Follower mode also possible externally by connecting the inverting input to |
| elmot | 1:d0dfbce63a89 | 60 | the output. |
| elmot | 1:d0dfbce63a89 | 61 | |
| elmot | 1:d0dfbce63a89 | 62 | (#) Running mode: Follower mode |
| elmot | 1:d0dfbce63a89 | 63 | (++) No Inverting Input is connected. |
| elmot | 1:d0dfbce63a89 | 64 | |
| elmot | 1:d0dfbce63a89 | 65 | (#) Running mode: Programmable Gain Amplifier (PGA) mode |
| elmot | 1:d0dfbce63a89 | 66 | (Resistor feedback output) |
| elmot | 1:d0dfbce63a89 | 67 | (++) The OPAMP(s) output(s) can be internally connected to resistor feedback |
| elmot | 1:d0dfbce63a89 | 68 | output. |
| elmot | 1:d0dfbce63a89 | 69 | (++) OPAMP gain is either 2, 4, 8 or 16. |
| elmot | 1:d0dfbce63a89 | 70 | |
| elmot | 1:d0dfbce63a89 | 71 | (#) The OPAMPs inverting input can be selected according to the Reference Manual |
| elmot | 1:d0dfbce63a89 | 72 | "OPAMP function description" chapter. |
| elmot | 1:d0dfbce63a89 | 73 | |
| elmot | 1:d0dfbce63a89 | 74 | (#) The OPAMPs non inverting input can be selected according to the Reference Manual |
| elmot | 1:d0dfbce63a89 | 75 | "OPAMP function description" chapter. |
| elmot | 1:d0dfbce63a89 | 76 | |
| elmot | 1:d0dfbce63a89 | 77 | |
| elmot | 1:d0dfbce63a89 | 78 | ##### How to use this driver ##### |
| elmot | 1:d0dfbce63a89 | 79 | ================================================================================ |
| elmot | 1:d0dfbce63a89 | 80 | [..] |
| elmot | 1:d0dfbce63a89 | 81 | |
| elmot | 1:d0dfbce63a89 | 82 | *** Power supply range *** |
| elmot | 1:d0dfbce63a89 | 83 | ============================================ |
| elmot | 1:d0dfbce63a89 | 84 | [..] To run in low power mode: |
| elmot | 1:d0dfbce63a89 | 85 | |
| elmot | 1:d0dfbce63a89 | 86 | (#) Configure the OPAMP using HAL_OPAMP_Init() function: |
| elmot | 1:d0dfbce63a89 | 87 | (++) Select OPAMP_POWERSUPPLY_LOW (VDDA lower than 2.4V) |
| elmot | 1:d0dfbce63a89 | 88 | (++) Otherwise select OPAMP_POWERSUPPLY_HIGH (VDDA higher than 2.4V) |
| elmot | 1:d0dfbce63a89 | 89 | |
| elmot | 1:d0dfbce63a89 | 90 | *** Low / normal power mode *** |
| elmot | 1:d0dfbce63a89 | 91 | ============================================ |
| elmot | 1:d0dfbce63a89 | 92 | [..] To run in low power mode: |
| elmot | 1:d0dfbce63a89 | 93 | |
| elmot | 1:d0dfbce63a89 | 94 | (#) Configure the OPAMP using HAL_OPAMP_Init() function: |
| elmot | 1:d0dfbce63a89 | 95 | (++) Select OPAMP_POWERMODE_LOWPOWER |
| elmot | 1:d0dfbce63a89 | 96 | (++) Otherwise select OPAMP_POWERMODE_NORMAL |
| elmot | 1:d0dfbce63a89 | 97 | |
| elmot | 1:d0dfbce63a89 | 98 | *** Calibration *** |
| elmot | 1:d0dfbce63a89 | 99 | ============================================ |
| elmot | 1:d0dfbce63a89 | 100 | [..] To run the OPAMP calibration self calibration: |
| elmot | 1:d0dfbce63a89 | 101 | |
| elmot | 1:d0dfbce63a89 | 102 | (#) Start calibration using HAL_OPAMP_SelfCalibrate. |
| elmot | 1:d0dfbce63a89 | 103 | Store the calibration results. |
| elmot | 1:d0dfbce63a89 | 104 | |
| elmot | 1:d0dfbce63a89 | 105 | *** Running mode *** |
| elmot | 1:d0dfbce63a89 | 106 | ============================================ |
| elmot | 1:d0dfbce63a89 | 107 | |
| elmot | 1:d0dfbce63a89 | 108 | [..] To use the OPAMP, perform the following steps: |
| elmot | 1:d0dfbce63a89 | 109 | |
| elmot | 1:d0dfbce63a89 | 110 | (#) Fill in the HAL_OPAMP_MspInit() to |
| elmot | 1:d0dfbce63a89 | 111 | (++) Enable the OPAMP Peripheral clock using macro __HAL_RCC_OPAMP_CLK_ENABLE() |
| elmot | 1:d0dfbce63a89 | 112 | (++) Configure the OPAMP input AND output in analog mode using |
| elmot | 1:d0dfbce63a89 | 113 | HAL_GPIO_Init() to map the OPAMP output to the GPIO pin. |
| elmot | 1:d0dfbce63a89 | 114 | |
| elmot | 1:d0dfbce63a89 | 115 | (#) Configure the OPAMP using HAL_OPAMP_Init() function: |
| elmot | 1:d0dfbce63a89 | 116 | (++) Select the mode |
| elmot | 1:d0dfbce63a89 | 117 | (++) Select the inverting input |
| elmot | 1:d0dfbce63a89 | 118 | (++) Select the non-inverting input |
| elmot | 1:d0dfbce63a89 | 119 | (++) If PGA mode is enabled, Select if inverting input is connected. |
| elmot | 1:d0dfbce63a89 | 120 | (++) Select either factory or user defined trimming mode. |
| elmot | 1:d0dfbce63a89 | 121 | (++) If the user-defined trimming mode is enabled, select PMOS & NMOS trimming values |
| elmot | 1:d0dfbce63a89 | 122 | (typically values set by HAL_OPAMP_SelfCalibrate function). |
| elmot | 1:d0dfbce63a89 | 123 | |
| elmot | 1:d0dfbce63a89 | 124 | (#) Enable the OPAMP using HAL_OPAMP_Start() function. |
| elmot | 1:d0dfbce63a89 | 125 | |
| elmot | 1:d0dfbce63a89 | 126 | (#) Disable the OPAMP using HAL_OPAMP_Stop() function. |
| elmot | 1:d0dfbce63a89 | 127 | |
| elmot | 1:d0dfbce63a89 | 128 | (#) Lock the OPAMP in running mode using HAL_OPAMP_Lock() function. |
| elmot | 1:d0dfbce63a89 | 129 | Caution: On STM32L4, HAL OPAMP lock is software lock only (not |
| elmot | 1:d0dfbce63a89 | 130 | hardware lock as on some other STM32 devices) |
| elmot | 1:d0dfbce63a89 | 131 | |
| elmot | 1:d0dfbce63a89 | 132 | (#) If needed, unlock the OPAMP using HAL_OPAMPEx_Unlock() function. |
| elmot | 1:d0dfbce63a89 | 133 | |
| elmot | 1:d0dfbce63a89 | 134 | *** Running mode: change of configuration while OPAMP ON *** |
| elmot | 1:d0dfbce63a89 | 135 | ============================================ |
| elmot | 1:d0dfbce63a89 | 136 | [..] To Re-configure OPAMP when OPAMP is ON (change on the fly) |
| elmot | 1:d0dfbce63a89 | 137 | (#) If needed, fill in the HAL_OPAMP_MspInit() |
| elmot | 1:d0dfbce63a89 | 138 | (++) This is the case for instance if you wish to use new OPAMP I/O |
| elmot | 1:d0dfbce63a89 | 139 | |
| elmot | 1:d0dfbce63a89 | 140 | (#) Configure the OPAMP using HAL_OPAMP_Init() function: |
| elmot | 1:d0dfbce63a89 | 141 | (++) As in configure case, select first the parameters you wish to modify. |
| elmot | 1:d0dfbce63a89 | 142 | |
| elmot | 1:d0dfbce63a89 | 143 | (#) Change from low power mode to normal power mode (& vice versa) requires |
| elmot | 1:d0dfbce63a89 | 144 | first HAL_OPAMP_DeInit() (force OPAMP OFF) and then HAL_OPAMP_Init(). |
| elmot | 1:d0dfbce63a89 | 145 | In other words, of OPAMP is ON, HAL_OPAMP_Init can NOT change power mode |
| elmot | 1:d0dfbce63a89 | 146 | alone. |
| elmot | 1:d0dfbce63a89 | 147 | |
| elmot | 1:d0dfbce63a89 | 148 | @endverbatim |
| elmot | 1:d0dfbce63a89 | 149 | ****************************************************************************** |
| elmot | 1:d0dfbce63a89 | 150 | |
| elmot | 1:d0dfbce63a89 | 151 | Table 1. OPAMPs inverting/non-inverting inputs for the STM32L4 devices: |
| elmot | 1:d0dfbce63a89 | 152 | +------------------------------------------------------------------------| |
| elmot | 1:d0dfbce63a89 | 153 | | | | OPAMP1 | OPAMP2 | |
| elmot | 1:d0dfbce63a89 | 154 | |-----------------|---------|----------------------|---------------------| |
| elmot | 1:d0dfbce63a89 | 155 | | Inverting Input | VM_SEL | | | |
| elmot | 1:d0dfbce63a89 | 156 | | | | IO0-> PA1 | IO0-> PA7 | |
| elmot | 1:d0dfbce63a89 | 157 | | | | LOW LEAKAGE IO (2) | LOW LEAKAGE IO (2) | |
| elmot | 1:d0dfbce63a89 | 158 | | | | Not connected | Not connected | |
| elmot | 1:d0dfbce63a89 | 159 | | (1) | | PGA mode only | PGA mode only | |
| elmot | 1:d0dfbce63a89 | 160 | |-----------------|---------|----------------------|---------------------| |
| elmot | 1:d0dfbce63a89 | 161 | | Non Inverting | VP_SEL | | | |
| elmot | 1:d0dfbce63a89 | 162 | | | | IO0-> PA0 (GPIO) | IO0-> PA6 (GPIO) | |
| elmot | 1:d0dfbce63a89 | 163 | | Input | | DAC1_OUT1 internal | DAC1_OUT2 internal | |
| elmot | 1:d0dfbce63a89 | 164 | +------------------------------------------------------------------------| |
| elmot | 1:d0dfbce63a89 | 165 | (1): NA in follower mode. |
| elmot | 1:d0dfbce63a89 | 166 | (2): Available on some package only (ex. BGA132). |
| elmot | 1:d0dfbce63a89 | 167 | |
| elmot | 1:d0dfbce63a89 | 168 | |
| elmot | 1:d0dfbce63a89 | 169 | Table 2. OPAMPs outputs for the STM32L4 devices: |
| elmot | 1:d0dfbce63a89 | 170 | |
| elmot | 1:d0dfbce63a89 | 171 | +------------------------------------------------------------------------- |
| elmot | 1:d0dfbce63a89 | 172 | | | | OPAMP1 | OPAMP2 | |
| elmot | 1:d0dfbce63a89 | 173 | |-----------------|--------|-----------------------|---------------------| |
| elmot | 1:d0dfbce63a89 | 174 | | Output | VOUT | PA3 | PB0 | |
| elmot | 1:d0dfbce63a89 | 175 | | | | & (1) ADC12_IN if | & (1) ADC12_IN if | |
| elmot | 1:d0dfbce63a89 | 176 | | | | connected internally | connected internally| |
| elmot | 1:d0dfbce63a89 | 177 | |-----------------|--------|-----------------------|---------------------| |
| elmot | 1:d0dfbce63a89 | 178 | (1): ADC1 or ADC2 shall select IN15. |
| elmot | 1:d0dfbce63a89 | 179 | |
| elmot | 1:d0dfbce63a89 | 180 | ****************************************************************************** |
| elmot | 1:d0dfbce63a89 | 181 | * @attention |
| elmot | 1:d0dfbce63a89 | 182 | * |
| elmot | 1:d0dfbce63a89 | 183 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
| elmot | 1:d0dfbce63a89 | 184 | * |
| elmot | 1:d0dfbce63a89 | 185 | * Redistribution and use in source and binary forms, with or without modification, |
| elmot | 1:d0dfbce63a89 | 186 | * are permitted provided that the following conditions are met: |
| elmot | 1:d0dfbce63a89 | 187 | * 1. Redistributions of source code must retain the above copyright notice, |
| elmot | 1:d0dfbce63a89 | 188 | * this list of conditions and the following disclaimer. |
| elmot | 1:d0dfbce63a89 | 189 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| elmot | 1:d0dfbce63a89 | 190 | * this list of conditions and the following disclaimer in the documentation |
| elmot | 1:d0dfbce63a89 | 191 | * and/or other materials provided with the distribution. |
| elmot | 1:d0dfbce63a89 | 192 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| elmot | 1:d0dfbce63a89 | 193 | * may be used to endorse or promote products derived from this software |
| elmot | 1:d0dfbce63a89 | 194 | * without specific prior written permission. |
| elmot | 1:d0dfbce63a89 | 195 | * |
| elmot | 1:d0dfbce63a89 | 196 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| elmot | 1:d0dfbce63a89 | 197 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| elmot | 1:d0dfbce63a89 | 198 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| elmot | 1:d0dfbce63a89 | 199 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| elmot | 1:d0dfbce63a89 | 200 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| elmot | 1:d0dfbce63a89 | 201 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| elmot | 1:d0dfbce63a89 | 202 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| elmot | 1:d0dfbce63a89 | 203 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| elmot | 1:d0dfbce63a89 | 204 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| elmot | 1:d0dfbce63a89 | 205 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| elmot | 1:d0dfbce63a89 | 206 | * |
| elmot | 1:d0dfbce63a89 | 207 | ****************************************************************************** |
| elmot | 1:d0dfbce63a89 | 208 | */ |
| elmot | 1:d0dfbce63a89 | 209 | |
| elmot | 1:d0dfbce63a89 | 210 | /* Includes ------------------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 211 | #include "stm32l4xx_hal.h" |
| elmot | 1:d0dfbce63a89 | 212 | |
| elmot | 1:d0dfbce63a89 | 213 | /** @addtogroup STM32L4xx_HAL_Driver |
| elmot | 1:d0dfbce63a89 | 214 | * @{ |
| elmot | 1:d0dfbce63a89 | 215 | */ |
| elmot | 1:d0dfbce63a89 | 216 | |
| elmot | 1:d0dfbce63a89 | 217 | /** @defgroup OPAMP OPAMP |
| elmot | 1:d0dfbce63a89 | 218 | * @brief OPAMP module driver |
| elmot | 1:d0dfbce63a89 | 219 | * @{ |
| elmot | 1:d0dfbce63a89 | 220 | */ |
| elmot | 1:d0dfbce63a89 | 221 | |
| elmot | 1:d0dfbce63a89 | 222 | #ifdef HAL_OPAMP_MODULE_ENABLED |
| elmot | 1:d0dfbce63a89 | 223 | |
| elmot | 1:d0dfbce63a89 | 224 | /* Private types -------------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 225 | /* Private variables ---------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 226 | /* Private constants ---------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 227 | /** @addtogroup OPAMP_Private_Constants |
| elmot | 1:d0dfbce63a89 | 228 | * @{ |
| elmot | 1:d0dfbce63a89 | 229 | */ |
| elmot | 1:d0dfbce63a89 | 230 | |
| elmot | 1:d0dfbce63a89 | 231 | /* CSR register reset value */ |
| elmot | 1:d0dfbce63a89 | 232 | #define OPAMP_CSR_RESET_VALUE ((uint32_t)0x00000000) |
| elmot | 1:d0dfbce63a89 | 233 | |
| elmot | 1:d0dfbce63a89 | 234 | #define OPAMP_CSR_RESET_BITS (OPAMP_CSR_OPAMPxEN | OPAMP_CSR_OPALPM | OPAMP_CSR_OPAMODE \ |
| elmot | 1:d0dfbce63a89 | 235 | | OPAMP_CSR_PGGAIN | OPAMP_CSR_VMSEL | OPAMP_CSR_VPSEL \ |
| elmot | 1:d0dfbce63a89 | 236 | | OPAMP_CSR_CALON | OPAMP_CSR_USERTRIM) |
| elmot | 1:d0dfbce63a89 | 237 | |
| elmot | 1:d0dfbce63a89 | 238 | /* CSR Init masks */ |
| elmot | 1:d0dfbce63a89 | 239 | #define OPAMP_CSR_INIT_MASK_PGA (OPAMP_CSR_OPALPM | OPAMP_CSR_OPAMODE| OPAMP_CSR_PGGAIN \ |
| elmot | 1:d0dfbce63a89 | 240 | | OPAMP_CSR_VMSEL | OPAMP_CSR_VPSEL | OPAMP_CSR_USERTRIM) |
| elmot | 1:d0dfbce63a89 | 241 | |
| elmot | 1:d0dfbce63a89 | 242 | #define OPAMP_CSR_INIT_MASK_FOLLOWER (OPAMP_CSR_OPALPM | OPAMP_CSR_OPAMODE| OPAMP_CSR_VPSEL \ |
| elmot | 1:d0dfbce63a89 | 243 | | OPAMP_CSR_USERTRIM) |
| elmot | 1:d0dfbce63a89 | 244 | |
| elmot | 1:d0dfbce63a89 | 245 | #define OPAMP_CSR_INIT_MASK_STANDALONE (OPAMP_CSR_OPALPM | OPAMP_CSR_OPAMODE| OPAMP_CSR_VPSEL \ |
| elmot | 1:d0dfbce63a89 | 246 | | OPAMP_CSR_VMSEL | OPAMP_CSR_USERTRIM) |
| elmot | 1:d0dfbce63a89 | 247 | |
| elmot | 1:d0dfbce63a89 | 248 | |
| elmot | 1:d0dfbce63a89 | 249 | /** |
| elmot | 1:d0dfbce63a89 | 250 | * @} |
| elmot | 1:d0dfbce63a89 | 251 | */ |
| elmot | 1:d0dfbce63a89 | 252 | |
| elmot | 1:d0dfbce63a89 | 253 | /* Private macros ------------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 254 | /* Private functions ---------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 255 | /* Exported functions --------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 256 | |
| elmot | 1:d0dfbce63a89 | 257 | /** @defgroup OPAMP_Exported_Functions OPAMP Exported Functions |
| elmot | 1:d0dfbce63a89 | 258 | * @{ |
| elmot | 1:d0dfbce63a89 | 259 | */ |
| elmot | 1:d0dfbce63a89 | 260 | |
| elmot | 1:d0dfbce63a89 | 261 | /** @defgroup OPAMP_Exported_Functions_Group1 Initialization and de-initialization functions |
| elmot | 1:d0dfbce63a89 | 262 | * @brief Initialization and Configuration functions |
| elmot | 1:d0dfbce63a89 | 263 | * |
| elmot | 1:d0dfbce63a89 | 264 | @verbatim |
| elmot | 1:d0dfbce63a89 | 265 | ============================================================================== |
| elmot | 1:d0dfbce63a89 | 266 | ##### Initialization and de-initialization functions ##### |
| elmot | 1:d0dfbce63a89 | 267 | ============================================================================== |
| elmot | 1:d0dfbce63a89 | 268 | |
| elmot | 1:d0dfbce63a89 | 269 | @endverbatim |
| elmot | 1:d0dfbce63a89 | 270 | * @{ |
| elmot | 1:d0dfbce63a89 | 271 | */ |
| elmot | 1:d0dfbce63a89 | 272 | |
| elmot | 1:d0dfbce63a89 | 273 | /** |
| elmot | 1:d0dfbce63a89 | 274 | * @brief Initializes the OPAMP according to the specified |
| elmot | 1:d0dfbce63a89 | 275 | * parameters in the OPAMP_InitTypeDef and initialize the associated handle. |
| elmot | 1:d0dfbce63a89 | 276 | * @note If the selected opamp is locked, initialization can't be performed. |
| elmot | 1:d0dfbce63a89 | 277 | * To unlock the configuration, perform a system reset. |
| elmot | 1:d0dfbce63a89 | 278 | * @param hopamp: OPAMP handle |
| elmot | 1:d0dfbce63a89 | 279 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 280 | */ |
| elmot | 1:d0dfbce63a89 | 281 | HAL_StatusTypeDef HAL_OPAMP_Init(OPAMP_HandleTypeDef *hopamp) |
| elmot | 1:d0dfbce63a89 | 282 | { |
| elmot | 1:d0dfbce63a89 | 283 | HAL_StatusTypeDef status = HAL_OK; |
| elmot | 1:d0dfbce63a89 | 284 | uint32_t updateotrlpotr = 0; |
| elmot | 1:d0dfbce63a89 | 285 | |
| elmot | 1:d0dfbce63a89 | 286 | /* Check the OPAMP handle allocation and lock status */ |
| elmot | 1:d0dfbce63a89 | 287 | /* Init not allowed if calibration is ongoing */ |
| elmot | 1:d0dfbce63a89 | 288 | if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED) |
| elmot | 1:d0dfbce63a89 | 289 | || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)) |
| elmot | 1:d0dfbce63a89 | 290 | { |
| elmot | 1:d0dfbce63a89 | 291 | return HAL_ERROR; |
| elmot | 1:d0dfbce63a89 | 292 | } |
| elmot | 1:d0dfbce63a89 | 293 | else |
| elmot | 1:d0dfbce63a89 | 294 | { |
| elmot | 1:d0dfbce63a89 | 295 | /* Check the parameter */ |
| elmot | 1:d0dfbce63a89 | 296 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
| elmot | 1:d0dfbce63a89 | 297 | |
| elmot | 1:d0dfbce63a89 | 298 | /* Set OPAMP parameters */ |
| elmot | 1:d0dfbce63a89 | 299 | assert_param(IS_OPAMP_POWER_SUPPLY_RANGE(hopamp->Init.PowerSupplyRange)); |
| elmot | 1:d0dfbce63a89 | 300 | assert_param(IS_OPAMP_POWERMODE(hopamp->Init.PowerMode)); |
| elmot | 1:d0dfbce63a89 | 301 | assert_param(IS_OPAMP_FUNCTIONAL_NORMALMODE(hopamp->Init.Mode)); |
| elmot | 1:d0dfbce63a89 | 302 | assert_param(IS_OPAMP_NONINVERTING_INPUT(hopamp->Init.NonInvertingInput)); |
| elmot | 1:d0dfbce63a89 | 303 | |
| elmot | 1:d0dfbce63a89 | 304 | if ((hopamp->Init.Mode) == OPAMP_STANDALONE_MODE) |
| elmot | 1:d0dfbce63a89 | 305 | { |
| elmot | 1:d0dfbce63a89 | 306 | assert_param(IS_OPAMP_INVERTING_INPUT_STANDALONE(hopamp->Init.InvertingInput)); |
| elmot | 1:d0dfbce63a89 | 307 | } |
| elmot | 1:d0dfbce63a89 | 308 | |
| elmot | 1:d0dfbce63a89 | 309 | if ((hopamp->Init.Mode) == OPAMP_PGA_MODE) |
| elmot | 1:d0dfbce63a89 | 310 | { |
| elmot | 1:d0dfbce63a89 | 311 | assert_param(IS_OPAMP_INVERTING_INPUT_PGA(hopamp->Init.InvertingInput)); |
| elmot | 1:d0dfbce63a89 | 312 | } |
| elmot | 1:d0dfbce63a89 | 313 | |
| elmot | 1:d0dfbce63a89 | 314 | if ((hopamp->Init.Mode) == OPAMP_PGA_MODE) |
| elmot | 1:d0dfbce63a89 | 315 | { |
| elmot | 1:d0dfbce63a89 | 316 | assert_param(IS_OPAMP_PGA_GAIN(hopamp->Init.PgaGain)); |
| elmot | 1:d0dfbce63a89 | 317 | } |
| elmot | 1:d0dfbce63a89 | 318 | |
| elmot | 1:d0dfbce63a89 | 319 | assert_param(IS_OPAMP_TRIMMING(hopamp->Init.UserTrimming)); |
| elmot | 1:d0dfbce63a89 | 320 | if ((hopamp->Init.UserTrimming) == OPAMP_TRIMMING_USER) |
| elmot | 1:d0dfbce63a89 | 321 | { |
| elmot | 1:d0dfbce63a89 | 322 | if (hopamp->Init.PowerMode == OPAMP_POWERMODE_NORMAL) |
| elmot | 1:d0dfbce63a89 | 323 | { |
| elmot | 1:d0dfbce63a89 | 324 | assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueP)); |
| elmot | 1:d0dfbce63a89 | 325 | assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueN)); |
| elmot | 1:d0dfbce63a89 | 326 | } |
| elmot | 1:d0dfbce63a89 | 327 | else |
| elmot | 1:d0dfbce63a89 | 328 | { |
| elmot | 1:d0dfbce63a89 | 329 | assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValuePLowPower)); |
| elmot | 1:d0dfbce63a89 | 330 | assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueNLowPower)); |
| elmot | 1:d0dfbce63a89 | 331 | } |
| elmot | 1:d0dfbce63a89 | 332 | } |
| elmot | 1:d0dfbce63a89 | 333 | |
| elmot | 1:d0dfbce63a89 | 334 | if(hopamp->State == HAL_OPAMP_STATE_RESET) |
| elmot | 1:d0dfbce63a89 | 335 | { |
| elmot | 1:d0dfbce63a89 | 336 | /* Allocate lock resource and initialize it */ |
| elmot | 1:d0dfbce63a89 | 337 | hopamp->Lock = HAL_UNLOCKED; |
| elmot | 1:d0dfbce63a89 | 338 | } |
| elmot | 1:d0dfbce63a89 | 339 | |
| elmot | 1:d0dfbce63a89 | 340 | /* Call MSP init function */ |
| elmot | 1:d0dfbce63a89 | 341 | HAL_OPAMP_MspInit(hopamp); |
| elmot | 1:d0dfbce63a89 | 342 | |
| elmot | 1:d0dfbce63a89 | 343 | /* Set operating mode */ |
| elmot | 1:d0dfbce63a89 | 344 | CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALON); |
| elmot | 1:d0dfbce63a89 | 345 | |
| elmot | 1:d0dfbce63a89 | 346 | if (hopamp->Init.Mode == OPAMP_PGA_MODE) |
| elmot | 1:d0dfbce63a89 | 347 | { |
| elmot | 1:d0dfbce63a89 | 348 | MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_INIT_MASK_PGA, \ |
| elmot | 1:d0dfbce63a89 | 349 | hopamp->Init.PowerMode | \ |
| elmot | 1:d0dfbce63a89 | 350 | hopamp->Init.Mode | \ |
| elmot | 1:d0dfbce63a89 | 351 | hopamp->Init.PgaGain | \ |
| elmot | 1:d0dfbce63a89 | 352 | hopamp->Init.InvertingInput | \ |
| elmot | 1:d0dfbce63a89 | 353 | hopamp->Init.NonInvertingInput | \ |
| elmot | 1:d0dfbce63a89 | 354 | hopamp->Init.UserTrimming); |
| elmot | 1:d0dfbce63a89 | 355 | } |
| elmot | 1:d0dfbce63a89 | 356 | |
| elmot | 1:d0dfbce63a89 | 357 | if (hopamp->Init.Mode == OPAMP_FOLLOWER_MODE) |
| elmot | 1:d0dfbce63a89 | 358 | { |
| elmot | 1:d0dfbce63a89 | 359 | /* In Follower mode InvertingInput is Not Applicable */ |
| elmot | 1:d0dfbce63a89 | 360 | MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_INIT_MASK_FOLLOWER, \ |
| elmot | 1:d0dfbce63a89 | 361 | hopamp->Init.PowerMode | \ |
| elmot | 1:d0dfbce63a89 | 362 | hopamp->Init.Mode | \ |
| elmot | 1:d0dfbce63a89 | 363 | hopamp->Init.NonInvertingInput | \ |
| elmot | 1:d0dfbce63a89 | 364 | hopamp->Init.UserTrimming); |
| elmot | 1:d0dfbce63a89 | 365 | } |
| elmot | 1:d0dfbce63a89 | 366 | |
| elmot | 1:d0dfbce63a89 | 367 | if (hopamp->Init.Mode == OPAMP_STANDALONE_MODE) |
| elmot | 1:d0dfbce63a89 | 368 | { |
| elmot | 1:d0dfbce63a89 | 369 | MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_INIT_MASK_STANDALONE, \ |
| elmot | 1:d0dfbce63a89 | 370 | hopamp->Init.PowerMode | \ |
| elmot | 1:d0dfbce63a89 | 371 | hopamp->Init.Mode | \ |
| elmot | 1:d0dfbce63a89 | 372 | hopamp->Init.InvertingInput | \ |
| elmot | 1:d0dfbce63a89 | 373 | hopamp->Init.NonInvertingInput | \ |
| elmot | 1:d0dfbce63a89 | 374 | hopamp->Init.UserTrimming); |
| elmot | 1:d0dfbce63a89 | 375 | } |
| elmot | 1:d0dfbce63a89 | 376 | |
| elmot | 1:d0dfbce63a89 | 377 | if (hopamp->Init.UserTrimming == OPAMP_TRIMMING_USER) |
| elmot | 1:d0dfbce63a89 | 378 | { |
| elmot | 1:d0dfbce63a89 | 379 | /* Set power mode and associated calibration parameters */ |
| elmot | 1:d0dfbce63a89 | 380 | if (hopamp->Init.PowerMode != OPAMP_POWERMODE_LOWPOWER) |
| elmot | 1:d0dfbce63a89 | 381 | { |
| elmot | 1:d0dfbce63a89 | 382 | /* OPAMP_POWERMODE_NORMAL */ |
| elmot | 1:d0dfbce63a89 | 383 | /* Set calibration mode (factory or user) and values for */ |
| elmot | 1:d0dfbce63a89 | 384 | /* transistors differential pair high (PMOS) and low (NMOS) for */ |
| elmot | 1:d0dfbce63a89 | 385 | /* normal mode. */ |
| elmot | 1:d0dfbce63a89 | 386 | updateotrlpotr = (((hopamp->Init.TrimmingValueP) << (OPAMP_INPUT_NONINVERTING)) \ |
| elmot | 1:d0dfbce63a89 | 387 | | (hopamp->Init.TrimmingValueN)); |
| elmot | 1:d0dfbce63a89 | 388 | MODIFY_REG(hopamp->Instance->OTR, OPAMP_OTR_TRIMOFFSETN | OPAMP_OTR_TRIMOFFSETP, updateotrlpotr); |
| elmot | 1:d0dfbce63a89 | 389 | } |
| elmot | 1:d0dfbce63a89 | 390 | else |
| elmot | 1:d0dfbce63a89 | 391 | { |
| elmot | 1:d0dfbce63a89 | 392 | /* OPAMP_POWERMODE_LOWPOWER */ |
| elmot | 1:d0dfbce63a89 | 393 | /* transistors differential pair high (PMOS) and low (NMOS) for */ |
| elmot | 1:d0dfbce63a89 | 394 | /* low power mode. */ |
| elmot | 1:d0dfbce63a89 | 395 | updateotrlpotr = (((hopamp->Init.TrimmingValuePLowPower) << (OPAMP_INPUT_NONINVERTING)) \ |
| elmot | 1:d0dfbce63a89 | 396 | | (hopamp->Init.TrimmingValueNLowPower)); |
| elmot | 1:d0dfbce63a89 | 397 | MODIFY_REG(hopamp->Instance->LPOTR, OPAMP_OTR_TRIMOFFSETN | OPAMP_OTR_TRIMOFFSETP, updateotrlpotr); |
| elmot | 1:d0dfbce63a89 | 398 | } |
| elmot | 1:d0dfbce63a89 | 399 | } |
| elmot | 1:d0dfbce63a89 | 400 | |
| elmot | 1:d0dfbce63a89 | 401 | /* Configure the power supply range */ |
| elmot | 1:d0dfbce63a89 | 402 | /* The OPAMP_CSR_OPARANGE is common configuration for all OPAMPs */ |
| elmot | 1:d0dfbce63a89 | 403 | /* bit OPAMP1_CSR_OPARANGE is used for both OPAMPs */ |
| elmot | 1:d0dfbce63a89 | 404 | MODIFY_REG(OPAMP1->CSR, OPAMP1_CSR_OPARANGE, hopamp->Init.PowerSupplyRange); |
| elmot | 1:d0dfbce63a89 | 405 | |
| elmot | 1:d0dfbce63a89 | 406 | /* Update the OPAMP state*/ |
| elmot | 1:d0dfbce63a89 | 407 | if (hopamp->State == HAL_OPAMP_STATE_RESET) |
| elmot | 1:d0dfbce63a89 | 408 | { |
| elmot | 1:d0dfbce63a89 | 409 | /* From RESET state to READY State */ |
| elmot | 1:d0dfbce63a89 | 410 | hopamp->State = HAL_OPAMP_STATE_READY; |
| elmot | 1:d0dfbce63a89 | 411 | } |
| elmot | 1:d0dfbce63a89 | 412 | /* else: remain in READY or BUSY state (no update) */ |
| elmot | 1:d0dfbce63a89 | 413 | return status; |
| elmot | 1:d0dfbce63a89 | 414 | } |
| elmot | 1:d0dfbce63a89 | 415 | } |
| elmot | 1:d0dfbce63a89 | 416 | |
| elmot | 1:d0dfbce63a89 | 417 | /** |
| elmot | 1:d0dfbce63a89 | 418 | * @brief DeInitialize the OPAMP peripheral. |
| elmot | 1:d0dfbce63a89 | 419 | * @note Deinitialization can be performed if the OPAMP configuration is locked. |
| elmot | 1:d0dfbce63a89 | 420 | * (the lock is SW in L4) |
| elmot | 1:d0dfbce63a89 | 421 | * @param hopamp: OPAMP handle |
| elmot | 1:d0dfbce63a89 | 422 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 423 | */ |
| elmot | 1:d0dfbce63a89 | 424 | HAL_StatusTypeDef HAL_OPAMP_DeInit(OPAMP_HandleTypeDef *hopamp) |
| elmot | 1:d0dfbce63a89 | 425 | { |
| elmot | 1:d0dfbce63a89 | 426 | HAL_StatusTypeDef status = HAL_OK; |
| elmot | 1:d0dfbce63a89 | 427 | |
| elmot | 1:d0dfbce63a89 | 428 | /* Check the OPAMP handle allocation */ |
| elmot | 1:d0dfbce63a89 | 429 | /* DeInit not allowed if calibration is ongoing */ |
| elmot | 1:d0dfbce63a89 | 430 | if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)) |
| elmot | 1:d0dfbce63a89 | 431 | { |
| elmot | 1:d0dfbce63a89 | 432 | status = HAL_ERROR; |
| elmot | 1:d0dfbce63a89 | 433 | } |
| elmot | 1:d0dfbce63a89 | 434 | else |
| elmot | 1:d0dfbce63a89 | 435 | { |
| elmot | 1:d0dfbce63a89 | 436 | /* Check the parameter */ |
| elmot | 1:d0dfbce63a89 | 437 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
| elmot | 1:d0dfbce63a89 | 438 | |
| elmot | 1:d0dfbce63a89 | 439 | /* Set OPAMP_CSR register to reset value */ |
| elmot | 1:d0dfbce63a89 | 440 | /* Mind that OPAMP1_CSR_OPARANGE of CSR of OPAMP1 remains unchanged (applies to both OPAMPs) */ |
| elmot | 1:d0dfbce63a89 | 441 | /* OPAMP shall be disabled first separately */ |
| elmot | 1:d0dfbce63a89 | 442 | CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
| elmot | 1:d0dfbce63a89 | 443 | MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_RESET_BITS, OPAMP_CSR_RESET_VALUE); |
| elmot | 1:d0dfbce63a89 | 444 | |
| elmot | 1:d0dfbce63a89 | 445 | /* DeInit the low level hardware: GPIO, CLOCK and NVIC */ |
| elmot | 1:d0dfbce63a89 | 446 | HAL_OPAMP_MspDeInit(hopamp); |
| elmot | 1:d0dfbce63a89 | 447 | |
| elmot | 1:d0dfbce63a89 | 448 | /* Update the OPAMP state*/ |
| elmot | 1:d0dfbce63a89 | 449 | hopamp->State = HAL_OPAMP_STATE_RESET; |
| elmot | 1:d0dfbce63a89 | 450 | } |
| elmot | 1:d0dfbce63a89 | 451 | |
| elmot | 1:d0dfbce63a89 | 452 | /* Process unlocked */ |
| elmot | 1:d0dfbce63a89 | 453 | __HAL_UNLOCK(hopamp); |
| elmot | 1:d0dfbce63a89 | 454 | |
| elmot | 1:d0dfbce63a89 | 455 | return status; |
| elmot | 1:d0dfbce63a89 | 456 | } |
| elmot | 1:d0dfbce63a89 | 457 | |
| elmot | 1:d0dfbce63a89 | 458 | |
| elmot | 1:d0dfbce63a89 | 459 | /** |
| elmot | 1:d0dfbce63a89 | 460 | * @brief Initialize the OPAMP MSP. |
| elmot | 1:d0dfbce63a89 | 461 | * @param hopamp: OPAMP handle |
| elmot | 1:d0dfbce63a89 | 462 | * @retval None |
| elmot | 1:d0dfbce63a89 | 463 | */ |
| elmot | 1:d0dfbce63a89 | 464 | __weak void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef *hopamp) |
| elmot | 1:d0dfbce63a89 | 465 | { |
| elmot | 1:d0dfbce63a89 | 466 | /* Prevent unused argument(s) compilation warning */ |
| elmot | 1:d0dfbce63a89 | 467 | UNUSED(hopamp); |
| elmot | 1:d0dfbce63a89 | 468 | |
| elmot | 1:d0dfbce63a89 | 469 | /* NOTE : This function should not be modified, when the callback is needed, |
| elmot | 1:d0dfbce63a89 | 470 | the function "HAL_OPAMP_MspInit()" must be implemented in the user file. |
| elmot | 1:d0dfbce63a89 | 471 | */ |
| elmot | 1:d0dfbce63a89 | 472 | } |
| elmot | 1:d0dfbce63a89 | 473 | |
| elmot | 1:d0dfbce63a89 | 474 | /** |
| elmot | 1:d0dfbce63a89 | 475 | * @brief DeInitialize OPAMP MSP. |
| elmot | 1:d0dfbce63a89 | 476 | * @param hopamp: OPAMP handle |
| elmot | 1:d0dfbce63a89 | 477 | * @retval None |
| elmot | 1:d0dfbce63a89 | 478 | */ |
| elmot | 1:d0dfbce63a89 | 479 | __weak void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef *hopamp) |
| elmot | 1:d0dfbce63a89 | 480 | { |
| elmot | 1:d0dfbce63a89 | 481 | /* Prevent unused argument(s) compilation warning */ |
| elmot | 1:d0dfbce63a89 | 482 | UNUSED(hopamp); |
| elmot | 1:d0dfbce63a89 | 483 | |
| elmot | 1:d0dfbce63a89 | 484 | /* NOTE : This function should not be modified, when the callback is needed, |
| elmot | 1:d0dfbce63a89 | 485 | the function "HAL_OPAMP_MspDeInit()" must be implemented in the user file. |
| elmot | 1:d0dfbce63a89 | 486 | */ |
| elmot | 1:d0dfbce63a89 | 487 | } |
| elmot | 1:d0dfbce63a89 | 488 | |
| elmot | 1:d0dfbce63a89 | 489 | /** |
| elmot | 1:d0dfbce63a89 | 490 | * @} |
| elmot | 1:d0dfbce63a89 | 491 | */ |
| elmot | 1:d0dfbce63a89 | 492 | |
| elmot | 1:d0dfbce63a89 | 493 | |
| elmot | 1:d0dfbce63a89 | 494 | /** @defgroup OPAMP_Exported_Functions_Group2 IO operation functions |
| elmot | 1:d0dfbce63a89 | 495 | * @brief IO operation functions |
| elmot | 1:d0dfbce63a89 | 496 | * |
| elmot | 1:d0dfbce63a89 | 497 | @verbatim |
| elmot | 1:d0dfbce63a89 | 498 | =============================================================================== |
| elmot | 1:d0dfbce63a89 | 499 | ##### IO operation functions ##### |
| elmot | 1:d0dfbce63a89 | 500 | =============================================================================== |
| elmot | 1:d0dfbce63a89 | 501 | [..] |
| elmot | 1:d0dfbce63a89 | 502 | This subsection provides a set of functions allowing to manage the OPAMP |
| elmot | 1:d0dfbce63a89 | 503 | start, stop and calibration actions. |
| elmot | 1:d0dfbce63a89 | 504 | |
| elmot | 1:d0dfbce63a89 | 505 | @endverbatim |
| elmot | 1:d0dfbce63a89 | 506 | * @{ |
| elmot | 1:d0dfbce63a89 | 507 | */ |
| elmot | 1:d0dfbce63a89 | 508 | |
| elmot | 1:d0dfbce63a89 | 509 | /** |
| elmot | 1:d0dfbce63a89 | 510 | * @brief Start the OPAMP. |
| elmot | 1:d0dfbce63a89 | 511 | * @param hopamp: OPAMP handle |
| elmot | 1:d0dfbce63a89 | 512 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 513 | */ |
| elmot | 1:d0dfbce63a89 | 514 | |
| elmot | 1:d0dfbce63a89 | 515 | HAL_StatusTypeDef HAL_OPAMP_Start(OPAMP_HandleTypeDef *hopamp) |
| elmot | 1:d0dfbce63a89 | 516 | { |
| elmot | 1:d0dfbce63a89 | 517 | HAL_StatusTypeDef status = HAL_OK; |
| elmot | 1:d0dfbce63a89 | 518 | |
| elmot | 1:d0dfbce63a89 | 519 | /* Check the OPAMP handle allocation */ |
| elmot | 1:d0dfbce63a89 | 520 | /* Check if OPAMP locked */ |
| elmot | 1:d0dfbce63a89 | 521 | if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)) |
| elmot | 1:d0dfbce63a89 | 522 | { |
| elmot | 1:d0dfbce63a89 | 523 | status = HAL_ERROR; |
| elmot | 1:d0dfbce63a89 | 524 | } |
| elmot | 1:d0dfbce63a89 | 525 | else |
| elmot | 1:d0dfbce63a89 | 526 | { |
| elmot | 1:d0dfbce63a89 | 527 | /* Check the parameter */ |
| elmot | 1:d0dfbce63a89 | 528 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
| elmot | 1:d0dfbce63a89 | 529 | |
| elmot | 1:d0dfbce63a89 | 530 | if(hopamp->State == HAL_OPAMP_STATE_READY) |
| elmot | 1:d0dfbce63a89 | 531 | { |
| elmot | 1:d0dfbce63a89 | 532 | /* Enable the selected opamp */ |
| elmot | 1:d0dfbce63a89 | 533 | SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
| elmot | 1:d0dfbce63a89 | 534 | |
| elmot | 1:d0dfbce63a89 | 535 | /* Update the OPAMP state*/ |
| elmot | 1:d0dfbce63a89 | 536 | /* From HAL_OPAMP_STATE_READY to HAL_OPAMP_STATE_BUSY */ |
| elmot | 1:d0dfbce63a89 | 537 | hopamp->State = HAL_OPAMP_STATE_BUSY; |
| elmot | 1:d0dfbce63a89 | 538 | } |
| elmot | 1:d0dfbce63a89 | 539 | else |
| elmot | 1:d0dfbce63a89 | 540 | { |
| elmot | 1:d0dfbce63a89 | 541 | status = HAL_ERROR; |
| elmot | 1:d0dfbce63a89 | 542 | } |
| elmot | 1:d0dfbce63a89 | 543 | |
| elmot | 1:d0dfbce63a89 | 544 | } |
| elmot | 1:d0dfbce63a89 | 545 | return status; |
| elmot | 1:d0dfbce63a89 | 546 | } |
| elmot | 1:d0dfbce63a89 | 547 | |
| elmot | 1:d0dfbce63a89 | 548 | /** |
| elmot | 1:d0dfbce63a89 | 549 | * @brief Stop the OPAMP. |
| elmot | 1:d0dfbce63a89 | 550 | * @param hopamp: OPAMP handle |
| elmot | 1:d0dfbce63a89 | 551 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 552 | */ |
| elmot | 1:d0dfbce63a89 | 553 | HAL_StatusTypeDef HAL_OPAMP_Stop(OPAMP_HandleTypeDef *hopamp) |
| elmot | 1:d0dfbce63a89 | 554 | { |
| elmot | 1:d0dfbce63a89 | 555 | HAL_StatusTypeDef status = HAL_OK; |
| elmot | 1:d0dfbce63a89 | 556 | |
| elmot | 1:d0dfbce63a89 | 557 | /* Check the OPAMP handle allocation */ |
| elmot | 1:d0dfbce63a89 | 558 | /* Check if OPAMP locked */ |
| elmot | 1:d0dfbce63a89 | 559 | /* Check if OPAMP calibration ongoing */ |
| elmot | 1:d0dfbce63a89 | 560 | if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED) \ |
| elmot | 1:d0dfbce63a89 | 561 | || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)) |
| elmot | 1:d0dfbce63a89 | 562 | { |
| elmot | 1:d0dfbce63a89 | 563 | status = HAL_ERROR; |
| elmot | 1:d0dfbce63a89 | 564 | } |
| elmot | 1:d0dfbce63a89 | 565 | else |
| elmot | 1:d0dfbce63a89 | 566 | { |
| elmot | 1:d0dfbce63a89 | 567 | /* Check the parameter */ |
| elmot | 1:d0dfbce63a89 | 568 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
| elmot | 1:d0dfbce63a89 | 569 | |
| elmot | 1:d0dfbce63a89 | 570 | if(hopamp->State == HAL_OPAMP_STATE_BUSY) |
| elmot | 1:d0dfbce63a89 | 571 | { |
| elmot | 1:d0dfbce63a89 | 572 | /* Disable the selected opamp */ |
| elmot | 1:d0dfbce63a89 | 573 | CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
| elmot | 1:d0dfbce63a89 | 574 | |
| elmot | 1:d0dfbce63a89 | 575 | /* Update the OPAMP state*/ |
| elmot | 1:d0dfbce63a89 | 576 | /* From HAL_OPAMP_STATE_BUSY to HAL_OPAMP_STATE_READY*/ |
| elmot | 1:d0dfbce63a89 | 577 | hopamp->State = HAL_OPAMP_STATE_READY; |
| elmot | 1:d0dfbce63a89 | 578 | } |
| elmot | 1:d0dfbce63a89 | 579 | else |
| elmot | 1:d0dfbce63a89 | 580 | { |
| elmot | 1:d0dfbce63a89 | 581 | status = HAL_ERROR; |
| elmot | 1:d0dfbce63a89 | 582 | } |
| elmot | 1:d0dfbce63a89 | 583 | } |
| elmot | 1:d0dfbce63a89 | 584 | return status; |
| elmot | 1:d0dfbce63a89 | 585 | } |
| elmot | 1:d0dfbce63a89 | 586 | |
| elmot | 1:d0dfbce63a89 | 587 | /** |
| elmot | 1:d0dfbce63a89 | 588 | * @brief Run the self calibration of one OPAMP. |
| elmot | 1:d0dfbce63a89 | 589 | * @note Calibration is performed in the mode specified in OPAMP init |
| elmot | 1:d0dfbce63a89 | 590 | * structure (mode normal or low-power). To perform calibration for |
| elmot | 1:d0dfbce63a89 | 591 | * both modes, repeat this function twice after OPAMP init structure |
| elmot | 1:d0dfbce63a89 | 592 | * accordingly updated. |
| elmot | 1:d0dfbce63a89 | 593 | * @note Calibration runs about 10 ms. |
| elmot | 1:d0dfbce63a89 | 594 | * @param hopamp handle |
| elmot | 1:d0dfbce63a89 | 595 | * @retval Updated offset trimming values (PMOS & NMOS), user trimming is enabled |
| elmot | 1:d0dfbce63a89 | 596 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 597 | |
| elmot | 1:d0dfbce63a89 | 598 | */ |
| elmot | 1:d0dfbce63a89 | 599 | |
| elmot | 1:d0dfbce63a89 | 600 | HAL_StatusTypeDef HAL_OPAMP_SelfCalibrate(OPAMP_HandleTypeDef *hopamp) |
| elmot | 1:d0dfbce63a89 | 601 | { |
| elmot | 1:d0dfbce63a89 | 602 | |
| elmot | 1:d0dfbce63a89 | 603 | HAL_StatusTypeDef status = HAL_OK; |
| elmot | 1:d0dfbce63a89 | 604 | |
| elmot | 1:d0dfbce63a89 | 605 | uint32_t trimmingvaluen = 0; |
| elmot | 1:d0dfbce63a89 | 606 | uint32_t trimmingvaluep = 0; |
| elmot | 1:d0dfbce63a89 | 607 | uint32_t delta; |
| elmot | 1:d0dfbce63a89 | 608 | uint32_t opampmode; |
| elmot | 1:d0dfbce63a89 | 609 | |
| elmot | 1:d0dfbce63a89 | 610 | __IO uint32_t* tmp_opamp_reg_trimming; /* Selection of register of trimming depending on power mode: OTR or LPOTR */ |
| elmot | 1:d0dfbce63a89 | 611 | |
| elmot | 1:d0dfbce63a89 | 612 | /* Check the OPAMP handle allocation */ |
| elmot | 1:d0dfbce63a89 | 613 | /* Check if OPAMP locked */ |
| elmot | 1:d0dfbce63a89 | 614 | if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)) |
| elmot | 1:d0dfbce63a89 | 615 | { |
| elmot | 1:d0dfbce63a89 | 616 | status = HAL_ERROR; |
| elmot | 1:d0dfbce63a89 | 617 | } |
| elmot | 1:d0dfbce63a89 | 618 | else |
| elmot | 1:d0dfbce63a89 | 619 | { |
| elmot | 1:d0dfbce63a89 | 620 | /* Check if OPAMP in calibration mode and calibration not yet enable */ |
| elmot | 1:d0dfbce63a89 | 621 | if(hopamp->State == HAL_OPAMP_STATE_READY) |
| elmot | 1:d0dfbce63a89 | 622 | { |
| elmot | 1:d0dfbce63a89 | 623 | /* Check the parameter */ |
| elmot | 1:d0dfbce63a89 | 624 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
| elmot | 1:d0dfbce63a89 | 625 | assert_param(IS_OPAMP_POWERMODE(hopamp->Init.PowerMode)); |
| elmot | 1:d0dfbce63a89 | 626 | |
| elmot | 1:d0dfbce63a89 | 627 | /* Save OPAMP mode as in */ |
| elmot | 1:d0dfbce63a89 | 628 | /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx */ |
| elmot | 1:d0dfbce63a89 | 629 | /* the calibration is not working in PGA mode */ |
| elmot | 1:d0dfbce63a89 | 630 | opampmode = READ_BIT(hopamp->Instance->CSR,OPAMP_CSR_OPAMODE); |
| elmot | 1:d0dfbce63a89 | 631 | |
| elmot | 1:d0dfbce63a89 | 632 | /* Use of standalone mode */ |
| elmot | 1:d0dfbce63a89 | 633 | MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_OPAMODE, OPAMP_STANDALONE_MODE); |
| elmot | 1:d0dfbce63a89 | 634 | |
| elmot | 1:d0dfbce63a89 | 635 | /* user trimming values are used for offset calibration */ |
| elmot | 1:d0dfbce63a89 | 636 | SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_USERTRIM); |
| elmot | 1:d0dfbce63a89 | 637 | |
| elmot | 1:d0dfbce63a89 | 638 | /* Select trimming settings depending on power mode */ |
| elmot | 1:d0dfbce63a89 | 639 | if (hopamp->Init.PowerMode == OPAMP_POWERMODE_NORMAL) |
| elmot | 1:d0dfbce63a89 | 640 | { |
| elmot | 1:d0dfbce63a89 | 641 | tmp_opamp_reg_trimming = &hopamp->Instance->OTR; |
| elmot | 1:d0dfbce63a89 | 642 | } |
| elmot | 1:d0dfbce63a89 | 643 | else |
| elmot | 1:d0dfbce63a89 | 644 | { |
| elmot | 1:d0dfbce63a89 | 645 | tmp_opamp_reg_trimming = &hopamp->Instance->LPOTR; |
| elmot | 1:d0dfbce63a89 | 646 | } |
| elmot | 1:d0dfbce63a89 | 647 | |
| elmot | 1:d0dfbce63a89 | 648 | /* Enable calibration */ |
| elmot | 1:d0dfbce63a89 | 649 | SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_CALON); |
| elmot | 1:d0dfbce63a89 | 650 | |
| elmot | 1:d0dfbce63a89 | 651 | /* 1st calibration - N */ |
| elmot | 1:d0dfbce63a89 | 652 | CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_CALSEL); |
| elmot | 1:d0dfbce63a89 | 653 | |
| elmot | 1:d0dfbce63a89 | 654 | /* Enable the selected opamp */ |
| elmot | 1:d0dfbce63a89 | 655 | SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
| elmot | 1:d0dfbce63a89 | 656 | |
| elmot | 1:d0dfbce63a89 | 657 | /* Init trimming counter */ |
| elmot | 1:d0dfbce63a89 | 658 | /* Medium value */ |
| elmot | 1:d0dfbce63a89 | 659 | trimmingvaluen = 16; |
| elmot | 1:d0dfbce63a89 | 660 | delta = 8; |
| elmot | 1:d0dfbce63a89 | 661 | |
| elmot | 1:d0dfbce63a89 | 662 | while (delta != 0) |
| elmot | 1:d0dfbce63a89 | 663 | { |
| elmot | 1:d0dfbce63a89 | 664 | /* Set candidate trimming */ |
| elmot | 1:d0dfbce63a89 | 665 | /* OPAMP_POWERMODE_NORMAL */ |
| elmot | 1:d0dfbce63a89 | 666 | MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OTR_TRIMOFFSETN, trimmingvaluen); |
| elmot | 1:d0dfbce63a89 | 667 | |
| elmot | 1:d0dfbce63a89 | 668 | /* OFFTRIMmax delay 1 ms as per datasheet (electrical characteristics */ |
| elmot | 1:d0dfbce63a89 | 669 | /* Offset trim time: during calibration, minimum time needed between */ |
| elmot | 1:d0dfbce63a89 | 670 | /* two steps to have 1 mV accuracy */ |
| elmot | 1:d0dfbce63a89 | 671 | HAL_Delay(OPAMP_TRIMMING_DELAY); |
| elmot | 1:d0dfbce63a89 | 672 | |
| elmot | 1:d0dfbce63a89 | 673 | if (READ_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALOUT) != RESET) |
| elmot | 1:d0dfbce63a89 | 674 | { |
| elmot | 1:d0dfbce63a89 | 675 | /* OPAMP_CSR_CALOUT is HIGH try higher trimming */ |
| elmot | 1:d0dfbce63a89 | 676 | trimmingvaluen -= delta; |
| elmot | 1:d0dfbce63a89 | 677 | } |
| elmot | 1:d0dfbce63a89 | 678 | else |
| elmot | 1:d0dfbce63a89 | 679 | { |
| elmot | 1:d0dfbce63a89 | 680 | /* OPAMP_CSR_CALOUT is LOW try lower trimming */ |
| elmot | 1:d0dfbce63a89 | 681 | trimmingvaluen += delta; |
| elmot | 1:d0dfbce63a89 | 682 | } |
| elmot | 1:d0dfbce63a89 | 683 | /* Divide range by 2 to continue dichotomy sweep */ |
| elmot | 1:d0dfbce63a89 | 684 | delta >>= 1; |
| elmot | 1:d0dfbce63a89 | 685 | } |
| elmot | 1:d0dfbce63a89 | 686 | |
| elmot | 1:d0dfbce63a89 | 687 | /* Still need to check if right calibration is current value or one step below */ |
| elmot | 1:d0dfbce63a89 | 688 | /* Indeed the first value that causes the OUTCAL bit to change from 0 to 1 */ |
| elmot | 1:d0dfbce63a89 | 689 | /* Set candidate trimming */ |
| elmot | 1:d0dfbce63a89 | 690 | MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OTR_TRIMOFFSETN, trimmingvaluen); |
| elmot | 1:d0dfbce63a89 | 691 | |
| elmot | 1:d0dfbce63a89 | 692 | /* OFFTRIMmax delay 1 ms as per datasheet (electrical characteristics */ |
| elmot | 1:d0dfbce63a89 | 693 | /* Offset trim time: during calibration, minimum time needed between */ |
| elmot | 1:d0dfbce63a89 | 694 | /* two steps to have 1 mV accuracy */ |
| elmot | 1:d0dfbce63a89 | 695 | HAL_Delay(OPAMP_TRIMMING_DELAY); |
| elmot | 1:d0dfbce63a89 | 696 | |
| elmot | 1:d0dfbce63a89 | 697 | if ((READ_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALOUT)) == 0) |
| elmot | 1:d0dfbce63a89 | 698 | { |
| elmot | 1:d0dfbce63a89 | 699 | /* Trimming value is actually one value more */ |
| elmot | 1:d0dfbce63a89 | 700 | trimmingvaluen++; |
| elmot | 1:d0dfbce63a89 | 701 | /* Set right trimming */ |
| elmot | 1:d0dfbce63a89 | 702 | MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OTR_TRIMOFFSETN, trimmingvaluen); |
| elmot | 1:d0dfbce63a89 | 703 | } |
| elmot | 1:d0dfbce63a89 | 704 | |
| elmot | 1:d0dfbce63a89 | 705 | /* 2nd calibration - P */ |
| elmot | 1:d0dfbce63a89 | 706 | SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_CALSEL); |
| elmot | 1:d0dfbce63a89 | 707 | |
| elmot | 1:d0dfbce63a89 | 708 | /* Init trimming counter */ |
| elmot | 1:d0dfbce63a89 | 709 | /* Medium value */ |
| elmot | 1:d0dfbce63a89 | 710 | trimmingvaluep = 16; |
| elmot | 1:d0dfbce63a89 | 711 | delta = 8; |
| elmot | 1:d0dfbce63a89 | 712 | |
| elmot | 1:d0dfbce63a89 | 713 | while (delta != 0) |
| elmot | 1:d0dfbce63a89 | 714 | { |
| elmot | 1:d0dfbce63a89 | 715 | /* Set candidate trimming */ |
| elmot | 1:d0dfbce63a89 | 716 | /* OPAMP_POWERMODE_NORMAL */ |
| elmot | 1:d0dfbce63a89 | 717 | MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OTR_TRIMOFFSETP, (trimmingvaluep<<OPAMP_INPUT_NONINVERTING)); |
| elmot | 1:d0dfbce63a89 | 718 | |
| elmot | 1:d0dfbce63a89 | 719 | /* OFFTRIMmax delay 1 ms as per datasheet (electrical characteristics */ |
| elmot | 1:d0dfbce63a89 | 720 | /* Offset trim time: during calibration, minimum time needed between */ |
| elmot | 1:d0dfbce63a89 | 721 | /* two steps to have 1 mV accuracy */ |
| elmot | 1:d0dfbce63a89 | 722 | HAL_Delay(OPAMP_TRIMMING_DELAY); |
| elmot | 1:d0dfbce63a89 | 723 | |
| elmot | 1:d0dfbce63a89 | 724 | if (READ_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALOUT) != RESET) |
| elmot | 1:d0dfbce63a89 | 725 | { |
| elmot | 1:d0dfbce63a89 | 726 | /* OPAMP_CSR_CALOUT is HIGH try higher trimming */ |
| elmot | 1:d0dfbce63a89 | 727 | trimmingvaluep += delta; |
| elmot | 1:d0dfbce63a89 | 728 | } |
| elmot | 1:d0dfbce63a89 | 729 | else |
| elmot | 1:d0dfbce63a89 | 730 | { |
| elmot | 1:d0dfbce63a89 | 731 | /* OPAMP_CSR_CALOUT is LOW try lower trimming */ |
| elmot | 1:d0dfbce63a89 | 732 | trimmingvaluep -= delta; |
| elmot | 1:d0dfbce63a89 | 733 | } |
| elmot | 1:d0dfbce63a89 | 734 | |
| elmot | 1:d0dfbce63a89 | 735 | /* Divide range by 2 to continue dichotomy sweep */ |
| elmot | 1:d0dfbce63a89 | 736 | delta >>= 1; |
| elmot | 1:d0dfbce63a89 | 737 | } |
| elmot | 1:d0dfbce63a89 | 738 | |
| elmot | 1:d0dfbce63a89 | 739 | /* Still need to check if right calibration is current value or one step below */ |
| elmot | 1:d0dfbce63a89 | 740 | /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0 */ |
| elmot | 1:d0dfbce63a89 | 741 | /* Set candidate trimming */ |
| elmot | 1:d0dfbce63a89 | 742 | MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OTR_TRIMOFFSETP, (trimmingvaluep<<OPAMP_INPUT_NONINVERTING)); |
| elmot | 1:d0dfbce63a89 | 743 | |
| elmot | 1:d0dfbce63a89 | 744 | /* OFFTRIMmax delay 1 ms as per datasheet (electrical characteristics */ |
| elmot | 1:d0dfbce63a89 | 745 | /* Offset trim time: during calibration, minimum time needed between */ |
| elmot | 1:d0dfbce63a89 | 746 | /* two steps to have 1 mV accuracy */ |
| elmot | 1:d0dfbce63a89 | 747 | HAL_Delay(OPAMP_TRIMMING_DELAY); |
| elmot | 1:d0dfbce63a89 | 748 | |
| elmot | 1:d0dfbce63a89 | 749 | if (READ_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALOUT) != RESET) |
| elmot | 1:d0dfbce63a89 | 750 | { |
| elmot | 1:d0dfbce63a89 | 751 | /* Trimming value is actually one value more */ |
| elmot | 1:d0dfbce63a89 | 752 | trimmingvaluep++; |
| elmot | 1:d0dfbce63a89 | 753 | MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OTR_TRIMOFFSETP, (trimmingvaluep<<OPAMP_INPUT_NONINVERTING)); |
| elmot | 1:d0dfbce63a89 | 754 | } |
| elmot | 1:d0dfbce63a89 | 755 | |
| elmot | 1:d0dfbce63a89 | 756 | /* Disable the OPAMP */ |
| elmot | 1:d0dfbce63a89 | 757 | CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
| elmot | 1:d0dfbce63a89 | 758 | |
| elmot | 1:d0dfbce63a89 | 759 | /* Disable calibration & set normal mode (operating mode) */ |
| elmot | 1:d0dfbce63a89 | 760 | CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_CALON); |
| elmot | 1:d0dfbce63a89 | 761 | |
| elmot | 1:d0dfbce63a89 | 762 | /* Self calibration is successful */ |
| elmot | 1:d0dfbce63a89 | 763 | /* Store calibration(user trimming) results in init structure. */ |
| elmot | 1:d0dfbce63a89 | 764 | |
| elmot | 1:d0dfbce63a89 | 765 | /* Set user trimming mode */ |
| elmot | 1:d0dfbce63a89 | 766 | hopamp->Init.UserTrimming = OPAMP_TRIMMING_USER; |
| elmot | 1:d0dfbce63a89 | 767 | |
| elmot | 1:d0dfbce63a89 | 768 | /* Affect calibration parameters depending on mode normal/low power */ |
| elmot | 1:d0dfbce63a89 | 769 | if (hopamp->Init.PowerMode != OPAMP_POWERMODE_LOWPOWER) |
| elmot | 1:d0dfbce63a89 | 770 | { |
| elmot | 1:d0dfbce63a89 | 771 | /* Write calibration result N */ |
| elmot | 1:d0dfbce63a89 | 772 | hopamp->Init.TrimmingValueN = trimmingvaluen; |
| elmot | 1:d0dfbce63a89 | 773 | /* Write calibration result P */ |
| elmot | 1:d0dfbce63a89 | 774 | hopamp->Init.TrimmingValueP = trimmingvaluep; |
| elmot | 1:d0dfbce63a89 | 775 | } |
| elmot | 1:d0dfbce63a89 | 776 | else |
| elmot | 1:d0dfbce63a89 | 777 | { |
| elmot | 1:d0dfbce63a89 | 778 | /* Write calibration result N */ |
| elmot | 1:d0dfbce63a89 | 779 | hopamp->Init.TrimmingValueNLowPower = trimmingvaluen; |
| elmot | 1:d0dfbce63a89 | 780 | /* Write calibration result P */ |
| elmot | 1:d0dfbce63a89 | 781 | hopamp->Init.TrimmingValuePLowPower = trimmingvaluep; |
| elmot | 1:d0dfbce63a89 | 782 | } |
| elmot | 1:d0dfbce63a89 | 783 | |
| elmot | 1:d0dfbce63a89 | 784 | /* Restore OPAMP mode after calibration */ |
| elmot | 1:d0dfbce63a89 | 785 | MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_OPAMODE, opampmode); |
| elmot | 1:d0dfbce63a89 | 786 | } |
| elmot | 1:d0dfbce63a89 | 787 | else |
| elmot | 1:d0dfbce63a89 | 788 | { |
| elmot | 1:d0dfbce63a89 | 789 | /* OPAMP can not be calibrated from this mode */ |
| elmot | 1:d0dfbce63a89 | 790 | status = HAL_ERROR; |
| elmot | 1:d0dfbce63a89 | 791 | } |
| elmot | 1:d0dfbce63a89 | 792 | } |
| elmot | 1:d0dfbce63a89 | 793 | return status; |
| elmot | 1:d0dfbce63a89 | 794 | } |
| elmot | 1:d0dfbce63a89 | 795 | |
| elmot | 1:d0dfbce63a89 | 796 | /** |
| elmot | 1:d0dfbce63a89 | 797 | * @} |
| elmot | 1:d0dfbce63a89 | 798 | */ |
| elmot | 1:d0dfbce63a89 | 799 | |
| elmot | 1:d0dfbce63a89 | 800 | /** @defgroup OPAMP_Exported_Functions_Group3 Peripheral Control functions |
| elmot | 1:d0dfbce63a89 | 801 | * @brief Peripheral Control functions |
| elmot | 1:d0dfbce63a89 | 802 | * |
| elmot | 1:d0dfbce63a89 | 803 | @verbatim |
| elmot | 1:d0dfbce63a89 | 804 | =============================================================================== |
| elmot | 1:d0dfbce63a89 | 805 | ##### Peripheral Control functions ##### |
| elmot | 1:d0dfbce63a89 | 806 | =============================================================================== |
| elmot | 1:d0dfbce63a89 | 807 | [..] |
| elmot | 1:d0dfbce63a89 | 808 | This subsection provides a set of functions allowing to control the OPAMP data |
| elmot | 1:d0dfbce63a89 | 809 | transfers. |
| elmot | 1:d0dfbce63a89 | 810 | |
| elmot | 1:d0dfbce63a89 | 811 | |
| elmot | 1:d0dfbce63a89 | 812 | |
| elmot | 1:d0dfbce63a89 | 813 | @endverbatim |
| elmot | 1:d0dfbce63a89 | 814 | * @{ |
| elmot | 1:d0dfbce63a89 | 815 | */ |
| elmot | 1:d0dfbce63a89 | 816 | |
| elmot | 1:d0dfbce63a89 | 817 | /** |
| elmot | 1:d0dfbce63a89 | 818 | * @brief Lock the selected OPAMP configuration. |
| elmot | 1:d0dfbce63a89 | 819 | * @note On STM32L4, HAL OPAMP lock is software lock only (in |
| elmot | 1:d0dfbce63a89 | 820 | * contrast of hardware lock available on some other STM32 |
| elmot | 1:d0dfbce63a89 | 821 | * devices). |
| elmot | 1:d0dfbce63a89 | 822 | * @param hopamp: OPAMP handle |
| elmot | 1:d0dfbce63a89 | 823 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 824 | */ |
| elmot | 1:d0dfbce63a89 | 825 | HAL_StatusTypeDef HAL_OPAMP_Lock(OPAMP_HandleTypeDef *hopamp) |
| elmot | 1:d0dfbce63a89 | 826 | { |
| elmot | 1:d0dfbce63a89 | 827 | HAL_StatusTypeDef status = HAL_OK; |
| elmot | 1:d0dfbce63a89 | 828 | |
| elmot | 1:d0dfbce63a89 | 829 | /* Check the OPAMP handle allocation */ |
| elmot | 1:d0dfbce63a89 | 830 | /* Check if OPAMP locked */ |
| elmot | 1:d0dfbce63a89 | 831 | /* OPAMP can be locked when enabled and running in normal mode */ |
| elmot | 1:d0dfbce63a89 | 832 | /* It is meaningless otherwise */ |
| elmot | 1:d0dfbce63a89 | 833 | if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_RESET) \ |
| elmot | 1:d0dfbce63a89 | 834 | || (hopamp->State == HAL_OPAMP_STATE_READY) \ |
| elmot | 1:d0dfbce63a89 | 835 | || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)\ |
| elmot | 1:d0dfbce63a89 | 836 | || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)) |
| elmot | 1:d0dfbce63a89 | 837 | |
| elmot | 1:d0dfbce63a89 | 838 | { |
| elmot | 1:d0dfbce63a89 | 839 | status = HAL_ERROR; |
| elmot | 1:d0dfbce63a89 | 840 | } |
| elmot | 1:d0dfbce63a89 | 841 | |
| elmot | 1:d0dfbce63a89 | 842 | else |
| elmot | 1:d0dfbce63a89 | 843 | { |
| elmot | 1:d0dfbce63a89 | 844 | /* Check the parameter */ |
| elmot | 1:d0dfbce63a89 | 845 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
| elmot | 1:d0dfbce63a89 | 846 | |
| elmot | 1:d0dfbce63a89 | 847 | /* OPAMP state changed to locked */ |
| elmot | 1:d0dfbce63a89 | 848 | hopamp->State = HAL_OPAMP_STATE_BUSYLOCKED; |
| elmot | 1:d0dfbce63a89 | 849 | } |
| elmot | 1:d0dfbce63a89 | 850 | return status; |
| elmot | 1:d0dfbce63a89 | 851 | } |
| elmot | 1:d0dfbce63a89 | 852 | |
| elmot | 1:d0dfbce63a89 | 853 | /** |
| elmot | 1:d0dfbce63a89 | 854 | * @brief Return the OPAMP factory trimming value. |
| elmot | 1:d0dfbce63a89 | 855 | * @note On STM32L4 OPAMP, user can retrieve factory trimming if |
| elmot | 1:d0dfbce63a89 | 856 | * OPAMP has never been set to user trimming before. |
| elmot | 1:d0dfbce63a89 | 857 | * Therefore, this function must be called when OPAMP init |
| elmot | 1:d0dfbce63a89 | 858 | * parameter "UserTrimming" is set to trimming factory, |
| elmot | 1:d0dfbce63a89 | 859 | * and before OPAMP calibration (function |
| elmot | 1:d0dfbce63a89 | 860 | * "HAL_OPAMP_SelfCalibrate()"). |
| elmot | 1:d0dfbce63a89 | 861 | * Otherwise, factory trimming value cannot be retrieved and |
| elmot | 1:d0dfbce63a89 | 862 | * error status is returned. |
| elmot | 1:d0dfbce63a89 | 863 | * @param hopamp : OPAMP handle |
| elmot | 1:d0dfbce63a89 | 864 | * @param trimmingoffset : Trimming offset (P or N) |
| elmot | 1:d0dfbce63a89 | 865 | * This parameter must be a value of @ref OPAMP_FactoryTrimming |
| elmot | 1:d0dfbce63a89 | 866 | * @note Calibration parameter retrieved is corresponding to the mode |
| elmot | 1:d0dfbce63a89 | 867 | * specified in OPAMP init structure (mode normal or low-power). |
| elmot | 1:d0dfbce63a89 | 868 | * To retrieve calibration parameters for both modes, repeat this |
| elmot | 1:d0dfbce63a89 | 869 | * function after OPAMP init structure accordingly updated. |
| elmot | 1:d0dfbce63a89 | 870 | * @retval Trimming value (P or N): range: 0->31 |
| elmot | 1:d0dfbce63a89 | 871 | * or OPAMP_FACTORYTRIMMING_DUMMY if trimming value is not available |
| elmot | 1:d0dfbce63a89 | 872 | * |
| elmot | 1:d0dfbce63a89 | 873 | */ |
| elmot | 1:d0dfbce63a89 | 874 | |
| elmot | 1:d0dfbce63a89 | 875 | HAL_OPAMP_TrimmingValueTypeDef HAL_OPAMP_GetTrimOffset (OPAMP_HandleTypeDef *hopamp, uint32_t trimmingoffset) |
| elmot | 1:d0dfbce63a89 | 876 | { |
| elmot | 1:d0dfbce63a89 | 877 | HAL_OPAMP_TrimmingValueTypeDef trimmingvalue; |
| elmot | 1:d0dfbce63a89 | 878 | __IO uint32_t* tmp_opamp_reg_trimming; /* Selection of register of trimming depending on power mode: OTR or LPOTR */ |
| elmot | 1:d0dfbce63a89 | 879 | |
| elmot | 1:d0dfbce63a89 | 880 | /* Check the OPAMP handle allocation */ |
| elmot | 1:d0dfbce63a89 | 881 | /* Value can be retrieved in HAL_OPAMP_STATE_READY state */ |
| elmot | 1:d0dfbce63a89 | 882 | if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_RESET) \ |
| elmot | 1:d0dfbce63a89 | 883 | || (hopamp->State == HAL_OPAMP_STATE_BUSY) \ |
| elmot | 1:d0dfbce63a89 | 884 | || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)\ |
| elmot | 1:d0dfbce63a89 | 885 | || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)) |
| elmot | 1:d0dfbce63a89 | 886 | { |
| elmot | 1:d0dfbce63a89 | 887 | return OPAMP_FACTORYTRIMMING_DUMMY; |
| elmot | 1:d0dfbce63a89 | 888 | } |
| elmot | 1:d0dfbce63a89 | 889 | else |
| elmot | 1:d0dfbce63a89 | 890 | { |
| elmot | 1:d0dfbce63a89 | 891 | /* Check the parameter */ |
| elmot | 1:d0dfbce63a89 | 892 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
| elmot | 1:d0dfbce63a89 | 893 | assert_param(IS_OPAMP_FACTORYTRIMMING(trimmingoffset)); |
| elmot | 1:d0dfbce63a89 | 894 | assert_param(IS_OPAMP_POWERMODE(hopamp->Init.PowerMode)); |
| elmot | 1:d0dfbce63a89 | 895 | |
| elmot | 1:d0dfbce63a89 | 896 | /* Check the trimming mode */ |
| elmot | 1:d0dfbce63a89 | 897 | if (READ_BIT(hopamp->Instance->CSR,OPAMP_CSR_USERTRIM) != RESET) |
| elmot | 1:d0dfbce63a89 | 898 | { |
| elmot | 1:d0dfbce63a89 | 899 | /* This function must called when OPAMP init parameter "UserTrimming" */ |
| elmot | 1:d0dfbce63a89 | 900 | /* is set to trimming factory, and before OPAMP calibration (function */ |
| elmot | 1:d0dfbce63a89 | 901 | /* "HAL_OPAMP_SelfCalibrate()"). */ |
| elmot | 1:d0dfbce63a89 | 902 | /* Otherwise, factory trimming value cannot be retrieved and error */ |
| elmot | 1:d0dfbce63a89 | 903 | /* status is returned. */ |
| elmot | 1:d0dfbce63a89 | 904 | trimmingvalue = OPAMP_FACTORYTRIMMING_DUMMY; |
| elmot | 1:d0dfbce63a89 | 905 | } |
| elmot | 1:d0dfbce63a89 | 906 | else |
| elmot | 1:d0dfbce63a89 | 907 | { |
| elmot | 1:d0dfbce63a89 | 908 | /* Select trimming settings depending on power mode */ |
| elmot | 1:d0dfbce63a89 | 909 | if (hopamp->Init.PowerMode == OPAMP_POWERMODE_NORMAL) |
| elmot | 1:d0dfbce63a89 | 910 | { |
| elmot | 1:d0dfbce63a89 | 911 | tmp_opamp_reg_trimming = &OPAMP->OTR; |
| elmot | 1:d0dfbce63a89 | 912 | } |
| elmot | 1:d0dfbce63a89 | 913 | else |
| elmot | 1:d0dfbce63a89 | 914 | { |
| elmot | 1:d0dfbce63a89 | 915 | tmp_opamp_reg_trimming = &OPAMP->LPOTR; |
| elmot | 1:d0dfbce63a89 | 916 | } |
| elmot | 1:d0dfbce63a89 | 917 | |
| elmot | 1:d0dfbce63a89 | 918 | /* Get factory trimming */ |
| elmot | 1:d0dfbce63a89 | 919 | if (trimmingoffset == OPAMP_FACTORYTRIMMING_P) |
| elmot | 1:d0dfbce63a89 | 920 | { |
| elmot | 1:d0dfbce63a89 | 921 | /* OPAMP_FACTORYTRIMMING_P */ |
| elmot | 1:d0dfbce63a89 | 922 | trimmingvalue = ((*tmp_opamp_reg_trimming) & OPAMP_OTR_TRIMOFFSETP) >> OPAMP_INPUT_NONINVERTING; |
| elmot | 1:d0dfbce63a89 | 923 | } |
| elmot | 1:d0dfbce63a89 | 924 | else |
| elmot | 1:d0dfbce63a89 | 925 | { |
| elmot | 1:d0dfbce63a89 | 926 | /* OPAMP_FACTORYTRIMMING_N */ |
| elmot | 1:d0dfbce63a89 | 927 | trimmingvalue = (*tmp_opamp_reg_trimming) & OPAMP_OTR_TRIMOFFSETN; |
| elmot | 1:d0dfbce63a89 | 928 | } |
| elmot | 1:d0dfbce63a89 | 929 | } |
| elmot | 1:d0dfbce63a89 | 930 | } |
| elmot | 1:d0dfbce63a89 | 931 | return trimmingvalue; |
| elmot | 1:d0dfbce63a89 | 932 | } |
| elmot | 1:d0dfbce63a89 | 933 | |
| elmot | 1:d0dfbce63a89 | 934 | /** |
| elmot | 1:d0dfbce63a89 | 935 | * @} |
| elmot | 1:d0dfbce63a89 | 936 | */ |
| elmot | 1:d0dfbce63a89 | 937 | |
| elmot | 1:d0dfbce63a89 | 938 | |
| elmot | 1:d0dfbce63a89 | 939 | /** @defgroup OPAMP_Exported_Functions_Group4 Peripheral State functions |
| elmot | 1:d0dfbce63a89 | 940 | * @brief Peripheral State functions |
| elmot | 1:d0dfbce63a89 | 941 | * |
| elmot | 1:d0dfbce63a89 | 942 | @verbatim |
| elmot | 1:d0dfbce63a89 | 943 | =============================================================================== |
| elmot | 1:d0dfbce63a89 | 944 | ##### Peripheral State functions ##### |
| elmot | 1:d0dfbce63a89 | 945 | =============================================================================== |
| elmot | 1:d0dfbce63a89 | 946 | [..] |
| elmot | 1:d0dfbce63a89 | 947 | This subsection permits to get in run-time the status of the peripheral. |
| elmot | 1:d0dfbce63a89 | 948 | |
| elmot | 1:d0dfbce63a89 | 949 | @endverbatim |
| elmot | 1:d0dfbce63a89 | 950 | * @{ |
| elmot | 1:d0dfbce63a89 | 951 | */ |
| elmot | 1:d0dfbce63a89 | 952 | |
| elmot | 1:d0dfbce63a89 | 953 | /** |
| elmot | 1:d0dfbce63a89 | 954 | * @brief Return the OPAMP handle state. |
| elmot | 1:d0dfbce63a89 | 955 | * @param hopamp : OPAMP handle |
| elmot | 1:d0dfbce63a89 | 956 | * @retval HAL state |
| elmot | 1:d0dfbce63a89 | 957 | */ |
| elmot | 1:d0dfbce63a89 | 958 | HAL_OPAMP_StateTypeDef HAL_OPAMP_GetState(OPAMP_HandleTypeDef *hopamp) |
| elmot | 1:d0dfbce63a89 | 959 | { |
| elmot | 1:d0dfbce63a89 | 960 | /* Check the OPAMP handle allocation */ |
| elmot | 1:d0dfbce63a89 | 961 | if(hopamp == NULL) |
| elmot | 1:d0dfbce63a89 | 962 | { |
| elmot | 1:d0dfbce63a89 | 963 | return HAL_OPAMP_STATE_RESET; |
| elmot | 1:d0dfbce63a89 | 964 | } |
| elmot | 1:d0dfbce63a89 | 965 | |
| elmot | 1:d0dfbce63a89 | 966 | /* Check the parameter */ |
| elmot | 1:d0dfbce63a89 | 967 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
| elmot | 1:d0dfbce63a89 | 968 | |
| elmot | 1:d0dfbce63a89 | 969 | /* Return OPAMP handle state */ |
| elmot | 1:d0dfbce63a89 | 970 | return hopamp->State; |
| elmot | 1:d0dfbce63a89 | 971 | } |
| elmot | 1:d0dfbce63a89 | 972 | |
| elmot | 1:d0dfbce63a89 | 973 | /** |
| elmot | 1:d0dfbce63a89 | 974 | * @} |
| elmot | 1:d0dfbce63a89 | 975 | */ |
| elmot | 1:d0dfbce63a89 | 976 | |
| elmot | 1:d0dfbce63a89 | 977 | /** |
| elmot | 1:d0dfbce63a89 | 978 | * @} |
| elmot | 1:d0dfbce63a89 | 979 | */ |
| elmot | 1:d0dfbce63a89 | 980 | |
| elmot | 1:d0dfbce63a89 | 981 | /** |
| elmot | 1:d0dfbce63a89 | 982 | * @} |
| elmot | 1:d0dfbce63a89 | 983 | */ |
| elmot | 1:d0dfbce63a89 | 984 | |
| elmot | 1:d0dfbce63a89 | 985 | #endif /* HAL_OPAMP_MODULE_ENABLED */ |
| elmot | 1:d0dfbce63a89 | 986 | /** |
| elmot | 1:d0dfbce63a89 | 987 | * @} |
| elmot | 1:d0dfbce63a89 | 988 | */ |
| elmot | 1:d0dfbce63a89 | 989 | |
| elmot | 1:d0dfbce63a89 | 990 | /** |
| elmot | 1:d0dfbce63a89 | 991 | * @} |
| elmot | 1:d0dfbce63a89 | 992 | */ |
| elmot | 1:d0dfbce63a89 | 993 | |
| elmot | 1:d0dfbce63a89 | 994 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |