mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Mon Sep 28 20:15:09 2015 +0100
Revision:
634:ac7d6880524d
Parent:
632:7687fb9c4f91
Synchronized with git revision 9b7d23d47153c298a6d24de9a415202705889d11

Full URL: https://github.com/mbedmicro/mbed/commit/9b7d23d47153c298a6d24de9a415202705889d11/

Revert "[NUCLEO_F303K8] add support of the STM32F303K8"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 237:f3da66175598 1 /**
mbed_official 237:f3da66175598 2 ******************************************************************************
mbed_official 237:f3da66175598 3 * @file stm32f3xx_hal_opamp.c
mbed_official 237:f3da66175598 4 * @author MCD Application Team
mbed_official 634:ac7d6880524d 5 * @version V1.1.0
mbed_official 634:ac7d6880524d 6 * @date 12-Sept-2014
mbed_official 237:f3da66175598 7 * @brief OPAMP HAL module driver.
mbed_official 237:f3da66175598 8 *
mbed_official 237:f3da66175598 9 * This file provides firmware functions to manage the following
mbed_official 237:f3da66175598 10 * functionalities of the operational amplifiers (OPAMP1,...OPAMP4)
mbed_official 237:f3da66175598 11 * peripheral:
mbed_official 237:f3da66175598 12 * + OPAMP Configuration
mbed_official 237:f3da66175598 13 * + OPAMP calibration
mbed_official 237:f3da66175598 14 *
mbed_official 237:f3da66175598 15 * Thanks to
mbed_official 237:f3da66175598 16 * + Initialization/de-initialization functions
mbed_official 237:f3da66175598 17 * + I/O operation functions
mbed_official 237:f3da66175598 18 * + Peripheral Control functions
mbed_official 237:f3da66175598 19 * + Peripheral State functions
mbed_official 237:f3da66175598 20 *
mbed_official 237:f3da66175598 21 @verbatim
mbed_official 237:f3da66175598 22 ================================================================================
mbed_official 237:f3da66175598 23 ##### OPAMP Peripheral Features #####
mbed_official 237:f3da66175598 24 ================================================================================
mbed_official 237:f3da66175598 25
mbed_official 237:f3da66175598 26 [..] The device integrates up to 4 operational amplifiers OPAMP1, OPAMP2,
mbed_official 237:f3da66175598 27 OPAMP3 and OPAMP4:
mbed_official 237:f3da66175598 28
mbed_official 237:f3da66175598 29 (#) The OPAMP(s) provides several exclusive running modes.
mbed_official 237:f3da66175598 30 (+) Standalone mode
mbed_official 237:f3da66175598 31 (+) Programmable Gain Amplifier (PGA) mode (Resistor feedback output)
mbed_official 237:f3da66175598 32 (+) Follower mode
mbed_official 237:f3da66175598 33
mbed_official 237:f3da66175598 34 (#) The OPAMP(s) provide(s) calibration capabilities.
mbed_official 237:f3da66175598 35 (+) Calibration aims at correcting some offset for running mode.
mbed_official 237:f3da66175598 36 (+) The OPAMP uses either factory calibration settings OR user defined
mbed_official 237:f3da66175598 37 calibration (trimming) settings (i.e. trimming mode).
mbed_official 237:f3da66175598 38 (+) The user defined settings can be figured out using self calibration
mbed_official 375:3d36234a1087 39 handled by HAL_OPAMP_SelfCalibrate, HAL_OPAMPEx_SelfCalibrateAll
mbed_official 237:f3da66175598 40 (+) HAL_OPAMP_SelfCalibrate:
mbed_official 237:f3da66175598 41 (++) Runs automatically the calibration in 2 steps.
mbed_official 237:f3da66175598 42 (90% of VDDA for NMOS transistors, 10% of VDDA for PMOS transistors).
mbed_official 237:f3da66175598 43 (As OPAMP is Rail-to-rail input/output, these 2 steps calibration is
mbed_official 237:f3da66175598 44 appropriate and enough in most cases).
mbed_official 237:f3da66175598 45 (++) Enables the user trimming mode
mbed_official 237:f3da66175598 46 (++) Updates the init structure with trimming values with fresh calibration
mbed_official 237:f3da66175598 47 results.
mbed_official 237:f3da66175598 48 The user may store the calibration results for larger
mbed_official 237:f3da66175598 49 (ex monitoring the trimming as a function of temperature
mbed_official 237:f3da66175598 50 for instance)
mbed_official 237:f3da66175598 51 (++) for STM32F3 devices having 2 or 4 OPAMPs
mbed_official 375:3d36234a1087 52 HAL_OPAMPEx_SelfCalibrateAll
mbed_official 237:f3da66175598 53 runs calibration of 2 or 4 OPAMPs in parallel.
mbed_official 237:f3da66175598 54
mbed_official 237:f3da66175598 55 (#) For any running mode, an additional Timer-controlled Mux (multiplexer)
mbed_official 237:f3da66175598 56 mode can be set on top.
mbed_official 237:f3da66175598 57 (+) Timer-controlled Mux mode allows Automatic switching between inverting
mbed_official 237:f3da66175598 58 and non-inverting input.
mbed_official 237:f3da66175598 59 (+) Hence on top of defaults (primary) inverting and non-inverting inputs,
mbed_official 237:f3da66175598 60 the user shall select secondary inverting and non inverting inputs.
mbed_official 237:f3da66175598 61 (+) TIM1 CC6 provides the alternate switching tempo between defaults
mbed_official 237:f3da66175598 62 (primary) and secondary inputs.
mbed_official 237:f3da66175598 63
mbed_official 237:f3da66175598 64 (#) Running mode: Standalone mode
mbed_official 237:f3da66175598 65 (+) Gain is set externally (gain depends on external loads).
mbed_official 237:f3da66175598 66 (+) Follower mode also possible externally by connecting the inverting input to
mbed_official 237:f3da66175598 67 the output.
mbed_official 237:f3da66175598 68
mbed_official 237:f3da66175598 69 (#) Running mode: Follower mode
mbed_official 237:f3da66175598 70 (+) No Inverting Input is connected.
mbed_official 237:f3da66175598 71
mbed_official 237:f3da66175598 72 (#) Running mode: Programmable Gain Amplifier (PGA) mode
mbed_official 237:f3da66175598 73 (Resistor feedback output)
mbed_official 237:f3da66175598 74 (+) The OPAMP(s) output(s) can be internally connected to resistor feedback
mbed_official 237:f3da66175598 75 output.
mbed_official 237:f3da66175598 76 (+) OPAMP gain is either 2, 4, 8 or 16.
mbed_official 237:f3da66175598 77
mbed_official 237:f3da66175598 78 (#) The OPAMPs non inverting input (both default and secondary) can be
mbed_official 237:f3da66175598 79 selected among the list shown by table below.
mbed_official 237:f3da66175598 80
mbed_official 237:f3da66175598 81 (#) The OPAMPs non inverting input (both default and secondary) can be
mbed_official 237:f3da66175598 82 selected among the list shown by table below.
mbed_official 237:f3da66175598 83
mbed_official 237:f3da66175598 84 [..] Table 1. OPAMPs inverting/non-inverting inputs for the STM32F3 devices:
mbed_official 237:f3da66175598 85
mbed_official 237:f3da66175598 86 +--------------------------------------------------------------+
mbed_official 237:f3da66175598 87 | | | OPAMP1 | OPAMP2 | OPAMP3 | OPAMP4 |
mbed_official 237:f3da66175598 88 |-----------------|--------|--------|--------|--------|--------|
mbed_official 237:f3da66175598 89 | | No conn| X | X | X | X |
mbed_official 237:f3da66175598 90 | Inverting Input | VM0 | PC5 | PC5 | PB10 | PB10 |
mbed_official 237:f3da66175598 91 | (1) | VM1 | PA3 | PA5 | PB2 | PD8 |
mbed_official 237:f3da66175598 92 |-----------------|--------|--------|--------|--------|--------|
mbed_official 237:f3da66175598 93 | | VP0 | PA1 | PA7 | PB0 | PB13 |
mbed_official 237:f3da66175598 94 | Non Inverting | VP1 | PA7 | PD14 | PB13 | PD11 |
mbed_official 237:f3da66175598 95 | Input | VP2 | PA3 | PB0 | PA1 | PA4 |
mbed_official 237:f3da66175598 96 | | VP3 | PA5 | PB14 | PA5 | PB11 |
mbed_official 237:f3da66175598 97 +--------------------------------------------------------------+
mbed_official 237:f3da66175598 98 (1): NA in follower mode.
mbed_official 237:f3da66175598 99
mbed_official 237:f3da66175598 100 [..] Table 2. OPAMPs outputs for the STM32F3 devices:
mbed_official 237:f3da66175598 101
mbed_official 237:f3da66175598 102 +--------------------------------------------------------------+
mbed_official 237:f3da66175598 103 | | | OPAMP1 | OPAMP2 | OPAMP3 | OPAMP4 |
mbed_official 237:f3da66175598 104 |-----------------|--------|--------|--------|--------|--------|
mbed_official 237:f3da66175598 105 | Output | | PA2 | PA6 | PB1 | PB12 |
mbed_official 237:f3da66175598 106 |-----------------|--------|--------|--------|--------|--------|
mbed_official 237:f3da66175598 107
mbed_official 237:f3da66175598 108
mbed_official 237:f3da66175598 109 ##### How to use this driver #####
mbed_official 237:f3da66175598 110 ================================================================================
mbed_official 237:f3da66175598 111 [..]
mbed_official 237:f3da66175598 112
mbed_official 237:f3da66175598 113 *** Calibration ***
mbed_official 237:f3da66175598 114 ============================================
mbed_official 237:f3da66175598 115 To run the opamp calibration self calibration:
mbed_official 237:f3da66175598 116
mbed_official 237:f3da66175598 117 (#) Start calibration using HAL_OPAMP_SelfCalibrate.
mbed_official 237:f3da66175598 118 Store the calibration results.
mbed_official 237:f3da66175598 119
mbed_official 237:f3da66175598 120 *** Running mode ***
mbed_official 237:f3da66175598 121 ============================================
mbed_official 237:f3da66175598 122
mbed_official 237:f3da66175598 123 To use the opamp, perform the following steps:
mbed_official 237:f3da66175598 124
mbed_official 375:3d36234a1087 125 (#) Fill in the HAL_OPAMP_MspInit() to
mbed_official 237:f3da66175598 126 (+) Configure the opamp input AND output in analog mode using
mbed_official 237:f3da66175598 127 HAL_GPIO_Init() to map the opamp output to the GPIO pin.
mbed_official 237:f3da66175598 128
mbed_official 237:f3da66175598 129 (#) Configure the opamp using HAL_OPAMP_Init() function:
mbed_official 237:f3da66175598 130 (+) Select the mode
mbed_official 237:f3da66175598 131 (+) Select the inverting input
mbed_official 237:f3da66175598 132 (+) Select the non-inverting input
mbed_official 237:f3da66175598 133 (+) Select if the Timer controlled Mux mode is enabled/disabled
mbed_official 237:f3da66175598 134 (+) If the Timer controlled Mux mode is enabled, select the secondary inverting input
mbed_official 237:f3da66175598 135 (+) If the Timer controlled Mux mode is enabled, Select the secondary non-inverting input
mbed_official 237:f3da66175598 136 (+) If PGA mode is enabled, Select if inverting input is connected.
mbed_official 237:f3da66175598 137 (+) Select either factory or user defined trimming mode.
mbed_official 237:f3da66175598 138 (+) If the user defined trimming mode is enabled, select PMOS & NMOS trimming values
mbed_official 237:f3da66175598 139 (typ. settings returned by HAL_OPAMP_SelfCalibrate function).
mbed_official 237:f3da66175598 140
mbed_official 237:f3da66175598 141 (#) Enable the opamp using HAL_OPAMP_Start() function.
mbed_official 237:f3da66175598 142
mbed_official 237:f3da66175598 143 (#) Disable the opamp using HAL_OPAMP_Stop() function.
mbed_official 237:f3da66175598 144
mbed_official 237:f3da66175598 145 (#) Lock the opamp in running mode using HAL_OPAMP_Lock() function. From then The configuration
mbed_official 237:f3da66175598 146 can only be modified after HW reset.
mbed_official 237:f3da66175598 147
mbed_official 237:f3da66175598 148 *** Running mode: change of configuration while OPAMP ON ***
mbed_official 237:f3da66175598 149 ============================================
mbed_official 237:f3da66175598 150 To Re-configure OPAMP when OPAMP is ON (change on the fly)
mbed_official 237:f3da66175598 151 (#) If needed, Fill in the HAL_OPAMP_MspInit()
mbed_official 375:3d36234a1087 152 (+) This is the case for instance if you wish to use new OPAMP I/O
mbed_official 237:f3da66175598 153
mbed_official 237:f3da66175598 154 (#) Configure the opamp using HAL_OPAMP_Init() function:
mbed_official 237:f3da66175598 155 (+) As in configure case, selects first the parameters you wish to modify.
mbed_official 237:f3da66175598 156
mbed_official 237:f3da66175598 157 @endverbatim
mbed_official 237:f3da66175598 158 ******************************************************************************
mbed_official 237:f3da66175598 159 * @attention
mbed_official 237:f3da66175598 160 *
mbed_official 634:ac7d6880524d 161 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 237:f3da66175598 162 *
mbed_official 237:f3da66175598 163 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 237:f3da66175598 164 * are permitted provided that the following conditions are met:
mbed_official 237:f3da66175598 165 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 237:f3da66175598 166 * this list of conditions and the following disclaimer.
mbed_official 237:f3da66175598 167 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 237:f3da66175598 168 * this list of conditions and the following disclaimer in the documentation
mbed_official 237:f3da66175598 169 * and/or other materials provided with the distribution.
mbed_official 237:f3da66175598 170 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 237:f3da66175598 171 * may be used to endorse or promote products derived from this software
mbed_official 237:f3da66175598 172 * without specific prior written permission.
mbed_official 237:f3da66175598 173 *
mbed_official 237:f3da66175598 174 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 237:f3da66175598 175 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 237:f3da66175598 176 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 237:f3da66175598 177 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 237:f3da66175598 178 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 237:f3da66175598 179 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 237:f3da66175598 180 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 237:f3da66175598 181 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 237:f3da66175598 182 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 237:f3da66175598 183 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 237:f3da66175598 184 *
mbed_official 237:f3da66175598 185 ******************************************************************************
mbed_official 237:f3da66175598 186 */
mbed_official 237:f3da66175598 187
mbed_official 237:f3da66175598 188 /* Includes ------------------------------------------------------------------*/
mbed_official 237:f3da66175598 189 #include "stm32f3xx_hal.h"
mbed_official 237:f3da66175598 190
mbed_official 237:f3da66175598 191 /** @addtogroup STM32F3xx_HAL_Driver
mbed_official 237:f3da66175598 192 * @{
mbed_official 237:f3da66175598 193 */
mbed_official 237:f3da66175598 194
mbed_official 375:3d36234a1087 195 /** @defgroup OPAMP OPAMP HAL module driver
mbed_official 237:f3da66175598 196 * @brief OPAMP HAL module driver
mbed_official 237:f3da66175598 197 * @{
mbed_official 237:f3da66175598 198 */
mbed_official 237:f3da66175598 199
mbed_official 237:f3da66175598 200 #ifdef HAL_OPAMP_MODULE_ENABLED
mbed_official 237:f3da66175598 201
mbed_official 375:3d36234a1087 202 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
mbed_official 375:3d36234a1087 203 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
mbed_official 375:3d36234a1087 204 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
mbed_official 375:3d36234a1087 205 defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
mbed_official 237:f3da66175598 206
mbed_official 237:f3da66175598 207 /* Private typedef -----------------------------------------------------------*/
mbed_official 237:f3da66175598 208 /* Private define ------------------------------------------------------------*/
mbed_official 375:3d36234a1087 209 /** @defgroup OPAMP_Private_Define OPAMP Private Define
mbed_official 375:3d36234a1087 210 * @{
mbed_official 375:3d36234a1087 211 */
mbed_official 237:f3da66175598 212 /* CSR register reset value */
mbed_official 237:f3da66175598 213 #define OPAMP_CSR_RESET_VALUE ((uint32_t)0x00000000)
mbed_official 375:3d36234a1087 214 /**
mbed_official 375:3d36234a1087 215 * @}
mbed_official 375:3d36234a1087 216 */
mbed_official 237:f3da66175598 217
mbed_official 237:f3da66175598 218 /* Private macro -------------------------------------------------------------*/
mbed_official 237:f3da66175598 219 /* Private variables ---------------------------------------------------------*/
mbed_official 237:f3da66175598 220 /* Private function prototypes -----------------------------------------------*/
mbed_official 375:3d36234a1087 221 /* Exported functions ---------------------------------------------------------*/
mbed_official 237:f3da66175598 222
mbed_official 375:3d36234a1087 223 /** @defgroup OPAMP_Exported_Functions OPAMP Exported Functions
mbed_official 237:f3da66175598 224 * @{
mbed_official 237:f3da66175598 225 */
mbed_official 237:f3da66175598 226
mbed_official 375:3d36234a1087 227 /** @defgroup OPAMP_Exported_Functions_Group1 Initialization and de-initialization functions
mbed_official 237:f3da66175598 228 * @brief Initialization and Configuration functions
mbed_official 237:f3da66175598 229 *
mbed_official 237:f3da66175598 230 @verbatim
mbed_official 237:f3da66175598 231 ===============================================================================
mbed_official 237:f3da66175598 232 ##### Initialization/de-initialization functions #####
mbed_official 237:f3da66175598 233 ===============================================================================
mbed_official 237:f3da66175598 234 [..] This section provides functions allowing to:
mbed_official 237:f3da66175598 235
mbed_official 237:f3da66175598 236 @endverbatim
mbed_official 237:f3da66175598 237 * @{
mbed_official 237:f3da66175598 238 */
mbed_official 237:f3da66175598 239
mbed_official 237:f3da66175598 240 /**
mbed_official 237:f3da66175598 241 * @brief Initializes the OPAMP according to the specified
mbed_official 237:f3da66175598 242 * parameters in the OPAMP_InitTypeDef and create the associated handle.
mbed_official 237:f3da66175598 243 * @note If the selected opamp is locked, initialization can't be performed.
mbed_official 237:f3da66175598 244 * To unlock the configuration, perform a system reset.
mbed_official 237:f3da66175598 245 * @param hopamp: OPAMP handle
mbed_official 237:f3da66175598 246 * @retval HAL status
mbed_official 237:f3da66175598 247 */
mbed_official 237:f3da66175598 248 HAL_StatusTypeDef HAL_OPAMP_Init(OPAMP_HandleTypeDef *hopamp)
mbed_official 237:f3da66175598 249
mbed_official 237:f3da66175598 250 {
mbed_official 237:f3da66175598 251 HAL_StatusTypeDef status = HAL_OK;
mbed_official 237:f3da66175598 252
mbed_official 237:f3da66175598 253 /* Check the OPAMP handle allocation and lock status */
mbed_official 237:f3da66175598 254 /* Init not allowed if calibration is ongoing */
mbed_official 634:ac7d6880524d 255 if((hopamp == HAL_NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED) \
mbed_official 237:f3da66175598 256 || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY))
mbed_official 237:f3da66175598 257 {
mbed_official 237:f3da66175598 258 return HAL_ERROR;
mbed_official 237:f3da66175598 259 }
mbed_official 237:f3da66175598 260 else
mbed_official 237:f3da66175598 261 {
mbed_official 237:f3da66175598 262
mbed_official 237:f3da66175598 263 /* Check the parameter */
mbed_official 237:f3da66175598 264 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
mbed_official 237:f3da66175598 265
mbed_official 237:f3da66175598 266 /* Set OPAMP parameters */
mbed_official 237:f3da66175598 267 assert_param(IS_OPAMP_FUNCTIONAL_NORMALMODE(hopamp->Init.Mode));
mbed_official 237:f3da66175598 268 assert_param(IS_OPAMP_NONINVERTING_INPUT(hopamp->Init.NonInvertingInput));
mbed_official 375:3d36234a1087 269 if ((hopamp->Init.Mode) == OPAMP_STANDALONE_MODE)
mbed_official 237:f3da66175598 270 {
mbed_official 237:f3da66175598 271 assert_param(IS_OPAMP_INVERTING_INPUT(hopamp->Init.InvertingInput));
mbed_official 237:f3da66175598 272 }
mbed_official 237:f3da66175598 273
mbed_official 237:f3da66175598 274 assert_param(IS_OPAMP_TIMERCONTROLLED_MUXMODE(hopamp->Init.TimerControlledMuxmode));
mbed_official 237:f3da66175598 275
mbed_official 237:f3da66175598 276 if ((hopamp->Init.TimerControlledMuxmode) == OPAMP_TIMERCONTROLLEDMUXMODE_ENABLE)
mbed_official 237:f3da66175598 277 {
mbed_official 237:f3da66175598 278 assert_param(IS_OPAMP_SEC_NONINVERTINGINPUT(hopamp->Init.NonInvertingInputSecondary));
mbed_official 375:3d36234a1087 279 if ((hopamp->Init.Mode) == OPAMP_STANDALONE_MODE)
mbed_official 237:f3da66175598 280 {
mbed_official 237:f3da66175598 281 assert_param(IS_OPAMP_SEC_INVERTINGINPUT(hopamp->Init.InvertingInputSecondary));
mbed_official 237:f3da66175598 282 }
mbed_official 237:f3da66175598 283 }
mbed_official 237:f3da66175598 284
mbed_official 237:f3da66175598 285 if ((hopamp->Init.Mode) == OPAMP_PGA_MODE)
mbed_official 237:f3da66175598 286 {
mbed_official 237:f3da66175598 287 assert_param(IS_OPAMP_PGACONNECT(hopamp->Init.PgaConnect));
mbed_official 237:f3da66175598 288 assert_param(IS_OPAMP_PGA_GAIN(hopamp->Init.PgaGain));
mbed_official 237:f3da66175598 289 }
mbed_official 237:f3da66175598 290
mbed_official 237:f3da66175598 291 assert_param(IS_OPAMP_TRIMMING(hopamp->Init.UserTrimming));
mbed_official 237:f3da66175598 292 if ((hopamp->Init.UserTrimming) == OPAMP_TRIMMING_USER)
mbed_official 237:f3da66175598 293 {
mbed_official 237:f3da66175598 294 assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueP));
mbed_official 237:f3da66175598 295 assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueN));
mbed_official 237:f3da66175598 296 }
mbed_official 237:f3da66175598 297
mbed_official 237:f3da66175598 298 /* Init SYSCFG and the low level hardware to access opamp */
mbed_official 237:f3da66175598 299 __SYSCFG_CLK_ENABLE();
mbed_official 237:f3da66175598 300
mbed_official 237:f3da66175598 301 /* Call MSP init function */
mbed_official 237:f3da66175598 302 HAL_OPAMP_MspInit(hopamp);
mbed_official 237:f3da66175598 303
mbed_official 237:f3da66175598 304 /* Set OPAMP parameters */
mbed_official 237:f3da66175598 305 /* Set bits according to hopamp->hopamp->Init.Mode value */
mbed_official 237:f3da66175598 306 /* Set bits according to hopamp->hopamp->Init.InvertingInput value */
mbed_official 237:f3da66175598 307 /* Set bits according to hopamp->hopamp->Init.NonInvertingInput value */
mbed_official 237:f3da66175598 308 /* Set bits according to hopamp->hopamp->Init.TimerControlledMuxmode value */
mbed_official 237:f3da66175598 309 /* Set bits according to hopamp->hopamp->Init.InvertingInputSecondary value */
mbed_official 237:f3da66175598 310 /* Set bits according to hopamp->hopamp->Init.NonInvertingInputSecondary value */
mbed_official 237:f3da66175598 311 /* Set bits according to hopamp->hopamp->Init.PgaConnect value */
mbed_official 237:f3da66175598 312 /* Set bits according to hopamp->hopamp->Init.PgaGain value */
mbed_official 237:f3da66175598 313 /* Set bits according to hopamp->hopamp->Init.UserTrimming value */
mbed_official 237:f3da66175598 314 /* Set bits according to hopamp->hopamp->Init.TrimmingValueP value */
mbed_official 237:f3da66175598 315 /* Set bits according to hopamp->hopamp->Init.TrimmingValueN value */
mbed_official 237:f3da66175598 316
mbed_official 237:f3da66175598 317
mbed_official 237:f3da66175598 318 /* check if OPAMP_PGA_MODE & in Follower mode */
mbed_official 237:f3da66175598 319 /* - InvertingInput */
mbed_official 237:f3da66175598 320 /* - InvertingInputSecondary */
mbed_official 237:f3da66175598 321 /* are Not Applicable */
mbed_official 237:f3da66175598 322
mbed_official 237:f3da66175598 323 if ((hopamp->Init.Mode == OPAMP_PGA_MODE) || (hopamp->Init.Mode == OPAMP_FOLLOWER_MODE))
mbed_official 237:f3da66175598 324 {
mbed_official 237:f3da66175598 325 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_UPDATE_PARAMETERS_INIT_MASK, \
mbed_official 237:f3da66175598 326 hopamp->Init.Mode | \
mbed_official 237:f3da66175598 327 hopamp->Init.NonInvertingInput | \
mbed_official 237:f3da66175598 328 hopamp->Init.TimerControlledMuxmode | \
mbed_official 237:f3da66175598 329 hopamp->Init.NonInvertingInputSecondary | \
mbed_official 237:f3da66175598 330 hopamp->Init.PgaConnect | \
mbed_official 237:f3da66175598 331 hopamp->Init.PgaGain | \
mbed_official 237:f3da66175598 332 hopamp->Init.UserTrimming | \
mbed_official 237:f3da66175598 333 (hopamp->Init.TrimmingValueP << OPAMP_INPUT_NONINVERTING) | \
mbed_official 237:f3da66175598 334 (hopamp->Init.TrimmingValueN << OPAMP_INPUT_INVERTING));
mbed_official 237:f3da66175598 335 }
mbed_official 375:3d36234a1087 336 else /* OPAMP_STANDALONE_MODE */
mbed_official 237:f3da66175598 337 {
mbed_official 237:f3da66175598 338 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_UPDATE_PARAMETERS_INIT_MASK, \
mbed_official 237:f3da66175598 339 hopamp->Init.Mode | \
mbed_official 237:f3da66175598 340 hopamp->Init.InvertingInput | \
mbed_official 237:f3da66175598 341 hopamp->Init.NonInvertingInput | \
mbed_official 237:f3da66175598 342 hopamp->Init.TimerControlledMuxmode | \
mbed_official 237:f3da66175598 343 hopamp->Init.InvertingInputSecondary | \
mbed_official 237:f3da66175598 344 hopamp->Init.NonInvertingInputSecondary | \
mbed_official 237:f3da66175598 345 hopamp->Init.PgaConnect | \
mbed_official 237:f3da66175598 346 hopamp->Init.PgaGain | \
mbed_official 237:f3da66175598 347 hopamp->Init.UserTrimming | \
mbed_official 237:f3da66175598 348 (hopamp->Init.TrimmingValueP << OPAMP_INPUT_NONINVERTING) | \
mbed_official 237:f3da66175598 349 (hopamp->Init.TrimmingValueN << OPAMP_INPUT_INVERTING));
mbed_official 237:f3da66175598 350 }
mbed_official 375:3d36234a1087 351
mbed_official 237:f3da66175598 352 /* Update the OPAMP state*/
mbed_official 237:f3da66175598 353 if (hopamp->State == HAL_OPAMP_STATE_RESET)
mbed_official 237:f3da66175598 354 {
mbed_official 237:f3da66175598 355 /* From RESET state to READY State */
mbed_official 375:3d36234a1087 356 hopamp->State = HAL_OPAMP_STATE_READY;
mbed_official 237:f3da66175598 357 }
mbed_official 237:f3da66175598 358 /* else: remain in READY or BUSY state (no update) */
mbed_official 375:3d36234a1087 359
mbed_official 237:f3da66175598 360 return status;
mbed_official 237:f3da66175598 361 }
mbed_official 237:f3da66175598 362 }
mbed_official 237:f3da66175598 363
mbed_official 375:3d36234a1087 364
mbed_official 237:f3da66175598 365 /**
mbed_official 237:f3da66175598 366 * @brief DeInitializes the OPAMP peripheral
mbed_official 237:f3da66175598 367 * @note Deinitialization can't be performed if the OPAMP configuration is locked.
mbed_official 237:f3da66175598 368 * To unlock the configuration, perform a system reset.
mbed_official 237:f3da66175598 369 * @param hopamp: OPAMP handle
mbed_official 237:f3da66175598 370 * @retval HAL status
mbed_official 237:f3da66175598 371 */
mbed_official 237:f3da66175598 372 HAL_StatusTypeDef HAL_OPAMP_DeInit(OPAMP_HandleTypeDef *hopamp)
mbed_official 237:f3da66175598 373 {
mbed_official 237:f3da66175598 374 HAL_StatusTypeDef status = HAL_OK;
mbed_official 237:f3da66175598 375
mbed_official 237:f3da66175598 376 /* Check the OPAMP handle allocation */
mbed_official 237:f3da66175598 377 /* Check if OPAMP locked */
mbed_official 237:f3da66175598 378 /* DeInit not allowed if calibration is ongoing */
mbed_official 634:ac7d6880524d 379 if((hopamp == HAL_NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED) \
mbed_official 237:f3da66175598 380 || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY))
mbed_official 237:f3da66175598 381 {
mbed_official 237:f3da66175598 382 status = HAL_ERROR;
mbed_official 237:f3da66175598 383 }
mbed_official 237:f3da66175598 384 else
mbed_official 237:f3da66175598 385 {
mbed_official 237:f3da66175598 386 /* Check the parameter */
mbed_official 237:f3da66175598 387 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
mbed_official 237:f3da66175598 388
mbed_official 237:f3da66175598 389 /* Set OPAMP_CSR register to reset value */
mbed_official 237:f3da66175598 390 WRITE_REG(hopamp->Instance->CSR, OPAMP_CSR_RESET_VALUE);
mbed_official 237:f3da66175598 391
mbed_official 237:f3da66175598 392 /* DeInit the low level hardware: GPIO, CLOCK and NVIC */
mbed_official 237:f3da66175598 393 HAL_OPAMP_MspDeInit(hopamp);
mbed_official 237:f3da66175598 394
mbed_official 237:f3da66175598 395 /* Update the OPAMP state*/
mbed_official 237:f3da66175598 396 hopamp->State = HAL_OPAMP_STATE_RESET;
mbed_official 237:f3da66175598 397 }
mbed_official 237:f3da66175598 398 return status;
mbed_official 237:f3da66175598 399 }
mbed_official 237:f3da66175598 400
mbed_official 237:f3da66175598 401 /**
mbed_official 237:f3da66175598 402 * @brief Initializes the OPAMP MSP.
mbed_official 237:f3da66175598 403 * @param hopamp: OPAMP handle
mbed_official 237:f3da66175598 404 * @retval None
mbed_official 237:f3da66175598 405 */
mbed_official 237:f3da66175598 406 __weak void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef *hopamp)
mbed_official 237:f3da66175598 407 {
mbed_official 237:f3da66175598 408 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 237:f3da66175598 409 the HAL_OPAMP_MspInit could be implemented in the user file
mbed_official 237:f3da66175598 410 */
mbed_official 237:f3da66175598 411
mbed_official 237:f3da66175598 412 /* Example */
mbed_official 237:f3da66175598 413 }
mbed_official 237:f3da66175598 414
mbed_official 237:f3da66175598 415 /**
mbed_official 237:f3da66175598 416 * @brief DeInitializes OPAMP MSP.
mbed_official 237:f3da66175598 417 * @param hopamp: OPAMP handle
mbed_official 237:f3da66175598 418 * @retval None
mbed_official 237:f3da66175598 419 */
mbed_official 237:f3da66175598 420 __weak void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef *hopamp)
mbed_official 237:f3da66175598 421 {
mbed_official 237:f3da66175598 422 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 237:f3da66175598 423 the HAL_OPAMP_MspDeInit could be implemented in the user file
mbed_official 237:f3da66175598 424 */
mbed_official 237:f3da66175598 425
mbed_official 237:f3da66175598 426 }
mbed_official 237:f3da66175598 427
mbed_official 237:f3da66175598 428 /**
mbed_official 237:f3da66175598 429 * @}
mbed_official 237:f3da66175598 430 */
mbed_official 237:f3da66175598 431
mbed_official 237:f3da66175598 432
mbed_official 375:3d36234a1087 433 /** @defgroup OPAMP_Exported_Functions_Group2 Input and Output operation functions
mbed_official 237:f3da66175598 434 * @brief Data transfers functions
mbed_official 237:f3da66175598 435 *
mbed_official 237:f3da66175598 436 @verbatim
mbed_official 237:f3da66175598 437 ===============================================================================
mbed_official 237:f3da66175598 438 ##### IO operation functions #####
mbed_official 237:f3da66175598 439 ===============================================================================
mbed_official 237:f3da66175598 440 [..]
mbed_official 237:f3da66175598 441 This subsection provides a set of functions allowing to manage the OPAMP data
mbed_official 237:f3da66175598 442 transfers.
mbed_official 237:f3da66175598 443
mbed_official 237:f3da66175598 444 @endverbatim
mbed_official 237:f3da66175598 445 * @{
mbed_official 237:f3da66175598 446 */
mbed_official 237:f3da66175598 447
mbed_official 237:f3da66175598 448 /**
mbed_official 237:f3da66175598 449 * @brief Start the opamp
mbed_official 237:f3da66175598 450 * @param hopamp: OPAMP handle
mbed_official 237:f3da66175598 451 * @retval HAL status
mbed_official 237:f3da66175598 452 */
mbed_official 237:f3da66175598 453
mbed_official 237:f3da66175598 454 HAL_StatusTypeDef HAL_OPAMP_Start(OPAMP_HandleTypeDef *hopamp)
mbed_official 237:f3da66175598 455 {
mbed_official 237:f3da66175598 456 HAL_StatusTypeDef status = HAL_OK;
mbed_official 237:f3da66175598 457
mbed_official 237:f3da66175598 458 /* Check the OPAMP handle allocation */
mbed_official 237:f3da66175598 459 /* Check if OPAMP locked */
mbed_official 634:ac7d6880524d 460 if((hopamp == HAL_NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED))
mbed_official 237:f3da66175598 461
mbed_official 237:f3da66175598 462 {
mbed_official 237:f3da66175598 463 status = HAL_ERROR;
mbed_official 237:f3da66175598 464 }
mbed_official 237:f3da66175598 465 else
mbed_official 237:f3da66175598 466 {
mbed_official 237:f3da66175598 467 /* Check the parameter */
mbed_official 237:f3da66175598 468 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
mbed_official 237:f3da66175598 469
mbed_official 237:f3da66175598 470 if(hopamp->State == HAL_OPAMP_STATE_READY)
mbed_official 237:f3da66175598 471 {
mbed_official 237:f3da66175598 472 /* Enable the selected opamp */
mbed_official 237:f3da66175598 473 SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
mbed_official 237:f3da66175598 474
mbed_official 237:f3da66175598 475 /* Update the OPAMP state*/
mbed_official 237:f3da66175598 476 /* From HAL_OPAMP_STATE_READY to HAL_OPAMP_STATE_BUSY */
mbed_official 237:f3da66175598 477 hopamp->State = HAL_OPAMP_STATE_BUSY;
mbed_official 237:f3da66175598 478 }
mbed_official 237:f3da66175598 479 else
mbed_official 237:f3da66175598 480 {
mbed_official 237:f3da66175598 481 status = HAL_ERROR;
mbed_official 237:f3da66175598 482 }
mbed_official 237:f3da66175598 483
mbed_official 237:f3da66175598 484
mbed_official 237:f3da66175598 485 }
mbed_official 237:f3da66175598 486 return status;
mbed_official 237:f3da66175598 487 }
mbed_official 237:f3da66175598 488
mbed_official 237:f3da66175598 489 /**
mbed_official 237:f3da66175598 490 * @brief Stop the opamp
mbed_official 237:f3da66175598 491 * @param hopamp: OPAMP handle
mbed_official 237:f3da66175598 492 * @retval HAL status
mbed_official 237:f3da66175598 493 */
mbed_official 237:f3da66175598 494 HAL_StatusTypeDef HAL_OPAMP_Stop(OPAMP_HandleTypeDef *hopamp)
mbed_official 237:f3da66175598 495 {
mbed_official 237:f3da66175598 496 HAL_StatusTypeDef status = HAL_OK;
mbed_official 237:f3da66175598 497
mbed_official 237:f3da66175598 498 /* Check the OPAMP handle allocation */
mbed_official 237:f3da66175598 499 /* Check if OPAMP locked */
mbed_official 237:f3da66175598 500 /* Check if OPAMP calibration ongoing */
mbed_official 634:ac7d6880524d 501 if((hopamp == HAL_NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED) \
mbed_official 237:f3da66175598 502 || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY))
mbed_official 237:f3da66175598 503 {
mbed_official 237:f3da66175598 504 status = HAL_ERROR;
mbed_official 237:f3da66175598 505 }
mbed_official 237:f3da66175598 506 else
mbed_official 237:f3da66175598 507 {
mbed_official 237:f3da66175598 508 /* Check the parameter */
mbed_official 237:f3da66175598 509 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
mbed_official 237:f3da66175598 510
mbed_official 237:f3da66175598 511 if(hopamp->State == HAL_OPAMP_STATE_BUSY)
mbed_official 237:f3da66175598 512 {
mbed_official 237:f3da66175598 513 /* Disable the selected opamp */
mbed_official 237:f3da66175598 514 CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
mbed_official 237:f3da66175598 515
mbed_official 237:f3da66175598 516 /* Update the OPAMP state*/
mbed_official 237:f3da66175598 517 /* From HAL_OPAMP_STATE_BUSY to HAL_OPAMP_STATE_READY*/
mbed_official 237:f3da66175598 518 hopamp->State = HAL_OPAMP_STATE_READY;
mbed_official 237:f3da66175598 519 }
mbed_official 237:f3da66175598 520 else
mbed_official 237:f3da66175598 521 {
mbed_official 237:f3da66175598 522 status = HAL_ERROR;
mbed_official 237:f3da66175598 523 }
mbed_official 237:f3da66175598 524 }
mbed_official 237:f3da66175598 525 return status;
mbed_official 237:f3da66175598 526 }
mbed_official 237:f3da66175598 527
mbed_official 237:f3da66175598 528 /**
mbed_official 237:f3da66175598 529 * @brief Run the self calibration of one OPAMP
mbed_official 237:f3da66175598 530 * @param hopamp handle
mbed_official 237:f3da66175598 531 * @retval Updated offset trimming values (PMOS & NMOS), user trimming is enabled
mbed_official 237:f3da66175598 532 * @retval HAL status
mbed_official 237:f3da66175598 533 * @note Calibration runs about 25 ms.
mbed_official 237:f3da66175598 534 */
mbed_official 237:f3da66175598 535
mbed_official 237:f3da66175598 536 HAL_StatusTypeDef HAL_OPAMP_SelfCalibrate(OPAMP_HandleTypeDef *hopamp)
mbed_official 237:f3da66175598 537 {
mbed_official 237:f3da66175598 538
mbed_official 237:f3da66175598 539 HAL_StatusTypeDef status = HAL_OK;
mbed_official 237:f3da66175598 540
mbed_official 237:f3da66175598 541 uint32_t trimmingvaluen = 0;
mbed_official 237:f3da66175598 542 uint32_t trimmingvaluep = 0;
mbed_official 237:f3da66175598 543 uint32_t delta;
mbed_official 237:f3da66175598 544
mbed_official 237:f3da66175598 545 /* Check the OPAMP handle allocation */
mbed_official 237:f3da66175598 546 /* Check if OPAMP locked */
mbed_official 634:ac7d6880524d 547 if((hopamp == HAL_NULL) || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED))
mbed_official 237:f3da66175598 548 {
mbed_official 237:f3da66175598 549 status = HAL_ERROR;
mbed_official 237:f3da66175598 550 }
mbed_official 237:f3da66175598 551 else
mbed_official 237:f3da66175598 552 {
mbed_official 237:f3da66175598 553
mbed_official 237:f3da66175598 554 /* Check if OPAMP in calibration mode and calibration not yet enable */
mbed_official 237:f3da66175598 555 if(hopamp->State == HAL_OPAMP_STATE_READY)
mbed_official 237:f3da66175598 556 {
mbed_official 237:f3da66175598 557 /* Check the parameter */
mbed_official 237:f3da66175598 558 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
mbed_official 237:f3da66175598 559
mbed_official 237:f3da66175598 560 /* Set Calibration mode */
mbed_official 237:f3da66175598 561 /* Non-inverting input connected to calibration reference voltage. */
mbed_official 237:f3da66175598 562 SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_FORCEVP);
mbed_official 237:f3da66175598 563
mbed_official 237:f3da66175598 564 /* user trimming values are used for offset calibration */
mbed_official 237:f3da66175598 565 SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_USERTRIM);
mbed_official 237:f3da66175598 566
mbed_official 237:f3da66175598 567 /* Enable calibration */
mbed_official 237:f3da66175598 568 SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_CALON);
mbed_official 237:f3da66175598 569
mbed_official 237:f3da66175598 570 /* 1st calibration - N */
mbed_official 237:f3da66175598 571 /* Select 90% VREF */
mbed_official 237:f3da66175598 572 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_90VDDA);
mbed_official 237:f3da66175598 573
mbed_official 237:f3da66175598 574 /* Enable the selected opamp */
mbed_official 237:f3da66175598 575 SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
mbed_official 237:f3da66175598 576
mbed_official 237:f3da66175598 577 /* Init trimming counter */
mbed_official 237:f3da66175598 578 /* Medium value */
mbed_official 237:f3da66175598 579 trimmingvaluen = 16;
mbed_official 237:f3da66175598 580 delta = 8;
mbed_official 237:f3da66175598 581
mbed_official 237:f3da66175598 582 while (delta != 0)
mbed_official 237:f3da66175598 583 {
mbed_official 237:f3da66175598 584 /* Set candidate trimming */
mbed_official 237:f3da66175598 585 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen<<OPAMP_INPUT_INVERTING);
mbed_official 237:f3da66175598 586
mbed_official 237:f3da66175598 587 /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
mbed_official 237:f3da66175598 588 /* Offset trim time: during calibration, minimum time needed between */
mbed_official 237:f3da66175598 589 /* two steps to have 1 mV accuracy */
mbed_official 237:f3da66175598 590 HAL_Delay(2);
mbed_official 237:f3da66175598 591
mbed_official 237:f3da66175598 592 if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET)
mbed_official 237:f3da66175598 593 {
mbed_official 237:f3da66175598 594 /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */
mbed_official 237:f3da66175598 595 trimmingvaluen += delta;
mbed_official 237:f3da66175598 596 }
mbed_official 237:f3da66175598 597 else
mbed_official 237:f3da66175598 598 {
mbed_official 237:f3da66175598 599 /* OPAMP_CSR_OUTCAL is LOW try lower trimming */
mbed_official 237:f3da66175598 600 trimmingvaluen -= delta;
mbed_official 237:f3da66175598 601 }
mbed_official 237:f3da66175598 602
mbed_official 237:f3da66175598 603 delta >>= 1;
mbed_official 237:f3da66175598 604 }
mbed_official 237:f3da66175598 605
mbed_official 237:f3da66175598 606 /* Still need to check if righ calibration is current value or un step below */
mbed_official 237:f3da66175598 607 /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0 */
mbed_official 237:f3da66175598 608 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen<<OPAMP_INPUT_INVERTING);
mbed_official 237:f3da66175598 609
mbed_official 237:f3da66175598 610 /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
mbed_official 237:f3da66175598 611 /* Offset trim time: during calibration, minimum time needed between */
mbed_official 237:f3da66175598 612 /* two steps to have 1 mV accuracy */
mbed_official 237:f3da66175598 613 HAL_Delay(2);
mbed_official 237:f3da66175598 614
mbed_official 237:f3da66175598 615 if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET)
mbed_official 237:f3da66175598 616 {
mbed_official 237:f3da66175598 617 /* OPAMP_CSR_OUTCAL is actually one value more */
mbed_official 237:f3da66175598 618 trimmingvaluen++;
mbed_official 237:f3da66175598 619 /* Set right trimming */
mbed_official 237:f3da66175598 620 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen<<OPAMP_INPUT_INVERTING);
mbed_official 237:f3da66175598 621 }
mbed_official 237:f3da66175598 622
mbed_official 237:f3da66175598 623 /* 2nd calibration - P */
mbed_official 237:f3da66175598 624 /* Select 10% VREF */
mbed_official 237:f3da66175598 625 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_10VDDA);
mbed_official 237:f3da66175598 626
mbed_official 237:f3da66175598 627 /* Init trimming counter */
mbed_official 237:f3da66175598 628 /* Medium value */
mbed_official 237:f3da66175598 629 trimmingvaluep = 16;
mbed_official 237:f3da66175598 630 delta = 8;
mbed_official 237:f3da66175598 631
mbed_official 237:f3da66175598 632 while (delta != 0)
mbed_official 237:f3da66175598 633 {
mbed_official 237:f3da66175598 634 /* Set candidate trimming */
mbed_official 237:f3da66175598 635 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep<<OPAMP_INPUT_NONINVERTING);
mbed_official 237:f3da66175598 636
mbed_official 237:f3da66175598 637 /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
mbed_official 237:f3da66175598 638 /* Offset trim time: during calibration, minimum time needed between */
mbed_official 237:f3da66175598 639 /* two steps to have 1 mV accuracy */
mbed_official 237:f3da66175598 640 HAL_Delay(2);
mbed_official 237:f3da66175598 641
mbed_official 237:f3da66175598 642 if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET)
mbed_official 237:f3da66175598 643 {
mbed_official 237:f3da66175598 644 /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */
mbed_official 237:f3da66175598 645 trimmingvaluep += delta;
mbed_official 237:f3da66175598 646 }
mbed_official 237:f3da66175598 647 else
mbed_official 237:f3da66175598 648 {
mbed_official 237:f3da66175598 649 trimmingvaluep -= delta;
mbed_official 237:f3da66175598 650 }
mbed_official 237:f3da66175598 651
mbed_official 237:f3da66175598 652 delta >>= 1;
mbed_official 237:f3da66175598 653 }
mbed_official 237:f3da66175598 654
mbed_official 237:f3da66175598 655 /* Still need to check if righ calibration is current value or un step below */
mbed_official 237:f3da66175598 656 /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0 */
mbed_official 237:f3da66175598 657 /* Set candidate trimming */
mbed_official 237:f3da66175598 658 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep<<OPAMP_INPUT_NONINVERTING);
mbed_official 237:f3da66175598 659
mbed_official 237:f3da66175598 660 /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
mbed_official 237:f3da66175598 661 /* Offset trim time: during calibration, minimum time needed between */
mbed_official 237:f3da66175598 662 /* two steps to have 1 mV accuracy */
mbed_official 237:f3da66175598 663 HAL_Delay(2);
mbed_official 237:f3da66175598 664
mbed_official 237:f3da66175598 665 if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET)
mbed_official 237:f3da66175598 666 {
mbed_official 237:f3da66175598 667 /* OPAMP_CSR_OUTCAL is actually one value more */
mbed_official 237:f3da66175598 668 trimmingvaluep++;
mbed_official 237:f3da66175598 669 /* Set right trimming */
mbed_official 237:f3da66175598 670 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep<<OPAMP_INPUT_NONINVERTING);
mbed_official 237:f3da66175598 671 }
mbed_official 237:f3da66175598 672
mbed_official 237:f3da66175598 673 /* Disable calibration */
mbed_official 237:f3da66175598 674 CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_CALON);
mbed_official 237:f3da66175598 675
mbed_official 237:f3da66175598 676 /* Disable the OPAMP */
mbed_official 237:f3da66175598 677 CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
mbed_official 375:3d36234a1087 678
mbed_official 237:f3da66175598 679 /* Set normale operating mode */
mbed_official 237:f3da66175598 680 /* Non-inverting input connected to calibration reference voltage. */
mbed_official 237:f3da66175598 681 CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_FORCEVP);
mbed_official 237:f3da66175598 682
mbed_official 237:f3da66175598 683 /* Self calibration is successful */
mbed_official 237:f3da66175598 684 /* Store calibration(user timming) results in init structure. */
mbed_official 237:f3da66175598 685
mbed_official 237:f3da66175598 686 /* Write calibration result N */
mbed_official 237:f3da66175598 687 hopamp->Init.TrimmingValueN = trimmingvaluen;
mbed_official 237:f3da66175598 688
mbed_official 237:f3da66175598 689 /* Write calibration result P */
mbed_official 237:f3da66175598 690 hopamp->Init.TrimmingValueP = trimmingvaluep;
mbed_official 237:f3da66175598 691
mbed_official 237:f3da66175598 692 /* Select user timming mode */
mbed_official 237:f3da66175598 693 /* And updated with calibrated settings */
mbed_official 237:f3da66175598 694 hopamp->Init.UserTrimming = OPAMP_TRIMMING_USER;
mbed_official 237:f3da66175598 695 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep<<OPAMP_INPUT_NONINVERTING);
mbed_official 237:f3da66175598 696 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen<<OPAMP_INPUT_INVERTING);
mbed_official 237:f3da66175598 697 }
mbed_official 237:f3da66175598 698
mbed_official 237:f3da66175598 699 else
mbed_official 237:f3da66175598 700 {
mbed_official 237:f3da66175598 701 /* OPAMP can not be calibrated from this mode */
mbed_official 237:f3da66175598 702 status = HAL_ERROR;
mbed_official 237:f3da66175598 703 }
mbed_official 237:f3da66175598 704 }
mbed_official 237:f3da66175598 705 return status;
mbed_official 237:f3da66175598 706 }
mbed_official 237:f3da66175598 707
mbed_official 237:f3da66175598 708 /**
mbed_official 237:f3da66175598 709 * @}
mbed_official 237:f3da66175598 710 */
mbed_official 237:f3da66175598 711
mbed_official 375:3d36234a1087 712 /** @defgroup OPAMP_Exported_Functions_Group3 Peripheral Control functions
mbed_official 237:f3da66175598 713 * @brief management functions
mbed_official 237:f3da66175598 714 *
mbed_official 237:f3da66175598 715 @verbatim
mbed_official 237:f3da66175598 716 ===============================================================================
mbed_official 375:3d36234a1087 717 ##### Peripheral Control functions #####
mbed_official 237:f3da66175598 718 ===============================================================================
mbed_official 237:f3da66175598 719 [..]
mbed_official 237:f3da66175598 720 This subsection provides a set of functions allowing to control the OPAMP data
mbed_official 237:f3da66175598 721 transfers.
mbed_official 237:f3da66175598 722
mbed_official 237:f3da66175598 723
mbed_official 237:f3da66175598 724
mbed_official 237:f3da66175598 725 @endverbatim
mbed_official 237:f3da66175598 726 * @{
mbed_official 237:f3da66175598 727 */
mbed_official 237:f3da66175598 728
mbed_official 237:f3da66175598 729 /**
mbed_official 237:f3da66175598 730 * @brief Lock the selected opamp configuration.
mbed_official 237:f3da66175598 731 * @param hopamp: OPAMP handle
mbed_official 237:f3da66175598 732 * @retval HAL status
mbed_official 237:f3da66175598 733 */
mbed_official 237:f3da66175598 734 HAL_StatusTypeDef HAL_OPAMP_Lock(OPAMP_HandleTypeDef *hopamp)
mbed_official 237:f3da66175598 735 {
mbed_official 237:f3da66175598 736 HAL_StatusTypeDef status = HAL_OK;
mbed_official 237:f3da66175598 737
mbed_official 237:f3da66175598 738 /* Check the OPAMP handle allocation */
mbed_official 237:f3da66175598 739 /* Check if OPAMP locked */
mbed_official 237:f3da66175598 740 /* OPAMP can be locked when enabled and running in normal mode */
mbed_official 237:f3da66175598 741 /* It is meaningless otherwise */
mbed_official 634:ac7d6880524d 742 if((hopamp == HAL_NULL) || (hopamp->State == HAL_OPAMP_STATE_RESET) \
mbed_official 237:f3da66175598 743 || (hopamp->State == HAL_OPAMP_STATE_READY) \
mbed_official 237:f3da66175598 744 || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)\
mbed_official 237:f3da66175598 745 || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED))
mbed_official 237:f3da66175598 746
mbed_official 237:f3da66175598 747 {
mbed_official 237:f3da66175598 748 status = HAL_ERROR;
mbed_official 237:f3da66175598 749 }
mbed_official 237:f3da66175598 750
mbed_official 237:f3da66175598 751 else
mbed_official 237:f3da66175598 752 {
mbed_official 237:f3da66175598 753 /* Check the parameter */
mbed_official 237:f3da66175598 754 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
mbed_official 237:f3da66175598 755
mbed_official 237:f3da66175598 756 /* Lock OPAMP */
mbed_official 237:f3da66175598 757 SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_LOCK);
mbed_official 237:f3da66175598 758
mbed_official 237:f3da66175598 759 /* OPAMP state changed to locked */
mbed_official 237:f3da66175598 760 hopamp->State = HAL_OPAMP_STATE_BUSYLOCKED;
mbed_official 237:f3da66175598 761 }
mbed_official 237:f3da66175598 762 return status;
mbed_official 237:f3da66175598 763 }
mbed_official 237:f3da66175598 764
mbed_official 237:f3da66175598 765 /**
mbed_official 237:f3da66175598 766 * @}
mbed_official 237:f3da66175598 767 */
mbed_official 237:f3da66175598 768
mbed_official 375:3d36234a1087 769 /** @defgroup OPAMP_Exported_Functions_Group4 Peripheral State functions
mbed_official 237:f3da66175598 770 * @brief Peripheral State functions
mbed_official 237:f3da66175598 771 *
mbed_official 237:f3da66175598 772 @verbatim
mbed_official 237:f3da66175598 773 ===============================================================================
mbed_official 375:3d36234a1087 774 ##### Peripheral State functions #####
mbed_official 237:f3da66175598 775 ===============================================================================
mbed_official 237:f3da66175598 776 [..]
mbed_official 237:f3da66175598 777 This subsection permit to get in run-time the status of the peripheral
mbed_official 237:f3da66175598 778 and the data flow.
mbed_official 237:f3da66175598 779
mbed_official 237:f3da66175598 780 @endverbatim
mbed_official 237:f3da66175598 781 * @{
mbed_official 237:f3da66175598 782 */
mbed_official 237:f3da66175598 783
mbed_official 237:f3da66175598 784 /**
mbed_official 237:f3da66175598 785 * @brief Return the OPAMP state
mbed_official 237:f3da66175598 786 * @param hopamp : OPAMP handle
mbed_official 237:f3da66175598 787 * @retval HAL state
mbed_official 237:f3da66175598 788 */
mbed_official 237:f3da66175598 789 HAL_OPAMP_StateTypeDef HAL_OPAMP_GetState(OPAMP_HandleTypeDef *hopamp)
mbed_official 237:f3da66175598 790 {
mbed_official 237:f3da66175598 791 /* Check the OPAMP handle allocation */
mbed_official 634:ac7d6880524d 792 if(hopamp == HAL_NULL)
mbed_official 237:f3da66175598 793 {
mbed_official 237:f3da66175598 794 return HAL_OPAMP_STATE_RESET;
mbed_official 237:f3da66175598 795 }
mbed_official 237:f3da66175598 796
mbed_official 237:f3da66175598 797 /* Check the parameter */
mbed_official 237:f3da66175598 798 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
mbed_official 237:f3da66175598 799
mbed_official 237:f3da66175598 800 return hopamp->State;
mbed_official 237:f3da66175598 801 }
mbed_official 237:f3da66175598 802
mbed_official 237:f3da66175598 803 /**
mbed_official 237:f3da66175598 804 * @brief Return the OPAMP factory trimming value
mbed_official 237:f3da66175598 805 * @param hopamp : OPAMP handle
mbed_official 237:f3da66175598 806 * @param trimmingoffset : Trimming offset (P or N)
mbed_official 237:f3da66175598 807 * @retval Trimming value (P or N): range: 0->31
mbed_official 237:f3da66175598 808 * or OPAMP_FACTORYTRIMMING_DUMMY if trimming value is not available
mbed_official 237:f3da66175598 809 */
mbed_official 237:f3da66175598 810
mbed_official 237:f3da66175598 811 OPAMP_TrimmingValueTypeDef HAL_OPAMP_GetTrimOffset (OPAMP_HandleTypeDef *hopamp, uint32_t trimmingoffset)
mbed_official 237:f3da66175598 812 {
mbed_official 237:f3da66175598 813 uint32_t oldusertrimming = 0;
mbed_official 237:f3da66175598 814 OPAMP_TrimmingValueTypeDef oldtrimmingvaluep = 0, oldtrimmingvaluen = 0, trimmingvalue = 0;
mbed_official 237:f3da66175598 815
mbed_official 237:f3da66175598 816 /* Check the OPAMP handle allocation */
mbed_official 237:f3da66175598 817 /* Value can be retrieved in HAL_OPAMP_STATE_READY state */
mbed_official 634:ac7d6880524d 818 if((hopamp == HAL_NULL) || (hopamp->State == HAL_OPAMP_STATE_RESET) \
mbed_official 237:f3da66175598 819 || (hopamp->State == HAL_OPAMP_STATE_BUSY) \
mbed_official 237:f3da66175598 820 || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)\
mbed_official 237:f3da66175598 821 || (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED))
mbed_official 237:f3da66175598 822 {
mbed_official 237:f3da66175598 823 return OPAMP_FACTORYTRIMMING_DUMMY;
mbed_official 237:f3da66175598 824 }
mbed_official 237:f3da66175598 825 else
mbed_official 237:f3da66175598 826 {
mbed_official 237:f3da66175598 827 /* Check the parameter */
mbed_official 237:f3da66175598 828 assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
mbed_official 237:f3da66175598 829 assert_param(IS_OPAMP_FACTORYTRIMMING(trimmingoffset));
mbed_official 237:f3da66175598 830
mbed_official 237:f3da66175598 831 /* Check the trimming mode */
mbed_official 237:f3da66175598 832 if ((READ_BIT(hopamp->Instance->CSR,OPAMP_CSR_USERTRIM)) != RESET)
mbed_official 237:f3da66175598 833 {
mbed_official 237:f3da66175598 834 /* User trimming is used */
mbed_official 237:f3da66175598 835 oldusertrimming = OPAMP_TRIMMING_USER;
mbed_official 237:f3da66175598 836 /* Store the TrimmingValueP & TrimmingValueN */
mbed_official 237:f3da66175598 837 oldtrimmingvaluep = (hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETP) >> OPAMP_INPUT_NONINVERTING;
mbed_official 237:f3da66175598 838 oldtrimmingvaluen = (hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETN) >> OPAMP_INPUT_INVERTING;
mbed_official 237:f3da66175598 839 }
mbed_official 237:f3da66175598 840
mbed_official 237:f3da66175598 841 /* Set factory timming mode */
mbed_official 237:f3da66175598 842 CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_USERTRIM);
mbed_official 237:f3da66175598 843
mbed_official 237:f3da66175598 844 /* Get factory trimming */
mbed_official 237:f3da66175598 845 if (trimmingoffset == OPAMP_FACTORYTRIMMING_P)
mbed_official 237:f3da66175598 846 {
mbed_official 237:f3da66175598 847 /* Return TrimOffsetP */
mbed_official 237:f3da66175598 848 trimmingvalue = ((hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETP) >> OPAMP_INPUT_NONINVERTING);
mbed_official 237:f3da66175598 849 }
mbed_official 237:f3da66175598 850 else
mbed_official 237:f3da66175598 851 {
mbed_official 237:f3da66175598 852 /* Return TrimOffsetN */
mbed_official 237:f3da66175598 853 trimmingvalue = ((hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETN) >> OPAMP_INPUT_INVERTING);
mbed_official 237:f3da66175598 854 }
mbed_official 237:f3da66175598 855
mbed_official 237:f3da66175598 856 /* Restore user trimming configuration if it was formerly set */
mbed_official 237:f3da66175598 857 /* Check if user trimming was used */
mbed_official 237:f3da66175598 858 if (oldusertrimming == OPAMP_TRIMMING_USER)
mbed_official 237:f3da66175598 859 {
mbed_official 237:f3da66175598 860 /* Restore user trimming */
mbed_official 237:f3da66175598 861 SET_BIT(hopamp->Instance->CSR,OPAMP_CSR_USERTRIM);
mbed_official 237:f3da66175598 862 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, oldtrimmingvaluep<<OPAMP_INPUT_NONINVERTING);
mbed_official 237:f3da66175598 863 MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, oldtrimmingvaluen<<OPAMP_INPUT_INVERTING);
mbed_official 237:f3da66175598 864 }
mbed_official 237:f3da66175598 865 }
mbed_official 237:f3da66175598 866 return trimmingvalue;
mbed_official 237:f3da66175598 867 }
mbed_official 237:f3da66175598 868 /**
mbed_official 237:f3da66175598 869 * @}
mbed_official 237:f3da66175598 870 */
mbed_official 237:f3da66175598 871
mbed_official 237:f3da66175598 872 /**
mbed_official 237:f3da66175598 873 * @}
mbed_official 237:f3da66175598 874 */
mbed_official 375:3d36234a1087 875 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
mbed_official 375:3d36234a1087 876 /* STM32F302xC || STM32F303xC || STM32F358xx || */
mbed_official 375:3d36234a1087 877 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
mbed_official 375:3d36234a1087 878 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
mbed_official 237:f3da66175598 879
mbed_official 237:f3da66175598 880 #endif /* HAL_OPAMP_MODULE_ENABLED */
mbed_official 237:f3da66175598 881 /**
mbed_official 237:f3da66175598 882 * @}
mbed_official 237:f3da66175598 883 */
mbed_official 237:f3da66175598 884
mbed_official 237:f3da66175598 885 /**
mbed_official 237:f3da66175598 886 * @}
mbed_official 237:f3da66175598 887 */
mbed_official 237:f3da66175598 888
mbed_official 237:f3da66175598 889 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/