Rev 61 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 77 | mjames | 1 | /** |
| 2 | ****************************************************************************** |
||
| 3 | * @file stm32l1xx_hal_opamp.c |
||
| 4 | * @author MCD Application Team |
||
| 5 | * @brief OPAMP HAL module driver. |
||
| 6 | * This file provides firmware functions to manage the following |
||
| 7 | * functionalities of the operational amplifier(s) peripheral: |
||
| 8 | * + Initialization and de-initialization functions |
||
| 9 | * + IO operation functions |
||
| 10 | * + Peripheral Control functions |
||
| 11 | * + Peripheral State functions |
||
| 12 | * |
||
| 13 | ****************************************************************************** |
||
| 14 | * @attention |
||
| 15 | * |
||
| 16 | * Copyright (c) 2017 STMicroelectronics. |
||
| 17 | * All rights reserved. |
||
| 18 | * |
||
| 19 | * This software is licensed under terms that can be found in the LICENSE file |
||
| 20 | * in the root directory of this software component. |
||
| 21 | * If no LICENSE file comes with this software, it is provided AS-IS. |
||
| 22 | * |
||
| 23 | ****************************************************************************** |
||
| 24 | @verbatim |
||
| 25 | ================================================================================ |
||
| 26 | ##### OPAMP Peripheral Features ##### |
||
| 27 | ================================================================================ |
||
| 28 | [..] The device integrates up to 3 operational amplifiers OPAMP1, OPAMP2, |
||
| 29 | OPAMP3 (OPAMP3 availability depends on device category) |
||
| 30 | |||
| 31 | (#) The OPAMP(s) provide(s) several exclusive running modes. |
||
| 32 | (++) Standalone mode |
||
| 33 | (++) Follower mode |
||
| 34 | |||
| 35 | (#) All OPAMP (same for all OPAMPs) can operate in |
||
| 36 | (++) Either Low range (VDDA < 2.4V) power supply |
||
| 37 | (++) Or High range (VDDA > 2.4V) power supply |
||
| 38 | |||
| 39 | (#) Each OPAMP(s) can be configured in normal and low power mode. |
||
| 40 | |||
| 41 | (#) The OPAMP(s) provide(s) calibration capabilities. |
||
| 42 | (++) Calibration aims at correcting some offset for running mode. |
||
| 43 | (++) The OPAMP uses either factory calibration settings OR user defined |
||
| 44 | calibration (trimming) settings (i.e. trimming mode). |
||
| 45 | (++) The user defined settings can be figured out using self calibration |
||
| 46 | handled by HAL_OPAMP_SelfCalibrate, HAL_OPAMPEx_SelfCalibrateAll |
||
| 47 | (++) HAL_OPAMP_SelfCalibrate: |
||
| 48 | (+++) Runs automatically the calibration in 2 steps: for transistors |
||
| 49 | differential pair high (PMOS) or low (NMOS) |
||
| 50 | (+++) Enables the user trimming mode |
||
| 51 | (+++) Updates the init structure with trimming values with fresh calibration |
||
| 52 | results. |
||
| 53 | The user may store the calibration results for larger |
||
| 54 | (ex monitoring the trimming as a function of temperature |
||
| 55 | for instance) |
||
| 56 | (+++) For devices having several OPAMPs, HAL_OPAMPEx_SelfCalibrateAll |
||
| 57 | runs calibration of all OPAMPs in parallel to save search time. |
||
| 58 | |||
| 59 | (#) Running mode: Standalone mode |
||
| 60 | (++) Gain is set externally (gain depends on external loads). |
||
| 61 | (++) Follower mode also possible externally by connecting the inverting input to |
||
| 62 | the output. |
||
| 63 | |||
| 64 | (#) Running mode: Follower mode |
||
| 65 | (++) No Inverting Input is connected. |
||
| 66 | (++) The OPAMP(s) output(s) are internally connected to inverting input. |
||
| 67 | |||
| 68 | ##### How to use this driver ##### |
||
| 69 | ================================================================================ |
||
| 70 | [..] |
||
| 71 | |||
| 72 | *** Power supply range *** |
||
| 73 | ============================================ |
||
| 74 | [..] To run in low power mode: |
||
| 75 | |||
| 76 | (#) Configure the OPAMP using HAL_OPAMP_Init() function: |
||
| 77 | (++) Select OPAMP_POWERSUPPLY_LOW (VDDA lower than 2.4V) |
||
| 78 | (++) Otherwise select OPAMP_POWERSUPPLY_HIGH (VDDA higher than 2.4V) |
||
| 79 | |||
| 80 | *** Low / normal power mode *** |
||
| 81 | ============================================ |
||
| 82 | [..] To run in low power mode: |
||
| 83 | |||
| 84 | (#) Configure the OPAMP using HAL_OPAMP_Init() function: |
||
| 85 | (++) Select OPAMP_POWERMODE_LOWPOWER |
||
| 86 | (++) Otherwise select OPAMP_POWERMODE_NORMAL |
||
| 87 | |||
| 88 | *** Calibration *** |
||
| 89 | ============================================ |
||
| 90 | [..] To run the OPAMP calibration self calibration: |
||
| 91 | |||
| 92 | (#) Start calibration using HAL_OPAMP_SelfCalibrate. |
||
| 93 | Store the calibration results. |
||
| 94 | |||
| 95 | *** Running mode *** |
||
| 96 | ============================================ |
||
| 97 | |||
| 98 | [..] To use the OPAMP, perform the following steps: |
||
| 99 | |||
| 100 | (#) Fill in the HAL_OPAMP_MspInit() to |
||
| 101 | (++) Enable the OPAMP Peripheral clock using macro __HAL_RCC_OPAMP_CLK_ENABLE() |
||
| 102 | (++) Configure the OPAMP input AND output in analog mode using |
||
| 103 | HAL_GPIO_Init() to map the OPAMP output to the GPIO pin. |
||
| 104 | |||
| 105 | (#) Registrate Callbacks |
||
| 106 | (++) The compilation define USE_HAL_OPAMP_REGISTER_CALLBACKS when set to 1 |
||
| 107 | allows the user to configure dynamically the driver callbacks. |
||
| 108 | |||
| 109 | (++) Use Functions HAL_OPAMP_RegisterCallback() to register a user callback, |
||
| 110 | it allows to register following callbacks: |
||
| 111 | (+++) MspInitCallback : OPAMP MspInit. |
||
| 112 | (+++) MspDeInitCallback : OPAMP MspFeInit. |
||
| 113 | This function takes as parameters the HAL peripheral handle, the Callback ID |
||
| 114 | and a pointer to the user callback function. |
||
| 115 | |||
| 116 | (++) Use function HAL_OPAMP_UnRegisterCallback() to reset a callback to the default |
||
| 117 | weak (overridden) function. It allows to reset following callbacks: |
||
| 118 | (+++) MspInitCallback : OPAMP MspInit. |
||
| 119 | (+++) MspDeInitCallback : OPAMP MspdeInit. |
||
| 120 | (+++) All Callbacks |
||
| 121 | |||
| 122 | (#) Configure the OPAMP using HAL_OPAMP_Init() function: |
||
| 123 | (++) Select the mode |
||
| 124 | (++) Select the inverting input |
||
| 125 | (++) Select the non-inverting input |
||
| 126 | (++) Select either factory or user defined trimming mode. |
||
| 127 | (++) If the user-defined trimming mode is enabled, select PMOS & NMOS trimming values |
||
| 128 | (typically values set by HAL_OPAMP_SelfCalibrate function). |
||
| 129 | |||
| 130 | (#) Enable the OPAMP using HAL_OPAMP_Start() function. |
||
| 131 | |||
| 132 | (#) Disable the OPAMP using HAL_OPAMP_Stop() function. |
||
| 133 | |||
| 134 | (#) Lock the OPAMP in running mode using HAL_OPAMP_Lock() function. |
||
| 135 | Caution: On STM32L1, HAL OPAMP lock is software lock only (not |
||
| 136 | hardware lock as on some other STM32 devices) |
||
| 137 | |||
| 138 | (#) If needed, unlock the OPAMP using HAL_OPAMPEx_Unlock() function. |
||
| 139 | |||
| 140 | *** Running mode: change of configuration while OPAMP ON *** |
||
| 141 | ============================================ |
||
| 142 | [..] To Re-configure OPAMP when OPAMP is ON (change on the fly) |
||
| 143 | (#) If needed, fill in the HAL_OPAMP_MspInit() |
||
| 144 | (++) This is the case for instance if you wish to use new OPAMP I/O |
||
| 145 | |||
| 146 | (#) Configure the OPAMP using HAL_OPAMP_Init() function: |
||
| 147 | (++) As in configure case, select first the parameters you wish to modify. |
||
| 148 | |||
| 149 | (#) Change from low power mode to normal power mode (& vice versa) requires |
||
| 150 | first HAL_OPAMP_DeInit() (force OPAMP OFF) and then HAL_OPAMP_Init(). |
||
| 151 | In other words, of OPAMP is ON, HAL_OPAMP_Init can NOT change power mode |
||
| 152 | alone. |
||
| 153 | |||
| 154 | @endverbatim |
||
| 155 | ****************************************************************************** |
||
| 156 | */ |
||
| 157 | |||
| 158 | /* |
||
| 159 | Additional remark: |
||
| 160 | The OPAMPs inverting input can be selected among the list shown by table below. |
||
| 161 | The OPAMPs non inverting input can be selected among the list shown by table below. |
||
| 162 | |||
| 163 | Table 1. OPAMPs inverting/non-inverting inputs for STM32L1 devices: |
||
| 164 | +--------------------------------------------------------------------------+ |
||
| 165 | | | HAL param | OPAMP1 | OPAMP2 | OPAMP3(4) | |
||
| 166 | | | name | | | | |
||
| 167 | |----------------|------------|--------------|--------------|--------------| |
||
| 168 | | Inverting | VM0 | PA2 | PA7 | PC2 | |
||
| 169 | | input (1) | VM1 | VINM pin (2) | VINM pin (2) | VINM pin (2) | |
||
| 170 | |----------------|------------|--------------|--------------|--------------| |
||
| 171 | | Non Inverting | VP0 | PA1 | PA6 | PC1 | |
||
| 172 | | input | DAC_CH1 (3)| DAC_CH1 | DAC_CH1 | --- | |
||
| 173 | | | DAC_CH2 (3)| --- | DAC_CH2 | DAC_CH2 | |
||
| 174 | +--------------------------------------------------------------------------+ |
||
| 175 | (1): NA in follower mode. |
||
| 176 | (2): OPAMP input OPAMPx_VINM are dedicated OPAMP pins, their availability |
||
| 177 | depends on device package. |
||
| 178 | (3): DAC channels 1 and 2 are connected internally to OPAMP. Nevertheless, |
||
| 179 | I/O pins connected to DAC can still be used as DAC output (pins PA4 |
||
| 180 | and PA5). |
||
| 181 | (4): OPAMP3 availability depends on device category. |
||
| 182 | |||
| 183 | Table 2. OPAMPs outputs for STM32L1 devices: |
||
| 184 | +--------------------------------------------------------+ |
||
| 185 | | | OPAMP1 | OPAMP2 | OPAMP3(4) | |
||
| 186 | |-----------------|------------|------------|------------| |
||
| 187 | | Output | PA3 | PB0 | PC3 | |
||
| 188 | +--------------------------------------------------------+ |
||
| 189 | (4) : OPAMP3 availability depends on device category |
||
| 190 | */ |
||
| 191 | |||
| 192 | /* Includes ------------------------------------------------------------------*/ |
||
| 193 | #include "stm32l1xx_hal.h" |
||
| 194 | |||
| 195 | /** @addtogroup STM32L1xx_HAL_Driver |
||
| 196 | * @{ |
||
| 197 | */ |
||
| 198 | |||
| 199 | /** @defgroup OPAMP OPAMP |
||
| 200 | * @brief OPAMP module driver |
||
| 201 | * @{ |
||
| 202 | */ |
||
| 203 | |||
| 204 | #ifdef HAL_OPAMP_MODULE_ENABLED |
||
| 205 | |||
| 206 | #if defined (STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined (STM32L151xE) || defined (STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX) || defined (STM32L162xC) || defined (STM32L152xC) || defined (STM32L151xC) |
||
| 207 | |||
| 208 | /* Private typedef -----------------------------------------------------------*/ |
||
| 209 | /* Private define ------------------------------------------------------------*/ |
||
| 210 | /* Private macro -------------------------------------------------------------*/ |
||
| 211 | /* Private variables ---------------------------------------------------------*/ |
||
| 212 | /* Private constants ---------------------------------------------------------*/ |
||
| 213 | /* Private function prototypes -----------------------------------------------*/ |
||
| 214 | /* Private functions ---------------------------------------------------------*/ |
||
| 215 | /* Exported functions --------------------------------------------------------*/ |
||
| 216 | |||
| 217 | /** @defgroup OPAMP_Exported_Functions OPAMP Exported Functions |
||
| 218 | * @{ |
||
| 219 | */ |
||
| 220 | |||
| 221 | /** @defgroup OPAMP_Exported_Functions_Group1 Initialization and de-initialization functions |
||
| 222 | * @brief Initialization and Configuration functions |
||
| 223 | * |
||
| 224 | @verbatim |
||
| 225 | ============================================================================== |
||
| 226 | ##### Initialization and de-initialization functions ##### |
||
| 227 | ============================================================================== |
||
| 228 | [..] This section provides functions allowing to: |
||
| 229 | |||
| 230 | @endverbatim |
||
| 231 | * @{ |
||
| 232 | */ |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @brief Initializes the OPAMP according to the specified |
||
| 236 | * parameters in the OPAMP_InitTypeDef and create the associated handle. |
||
| 237 | * @note If the selected opamp is locked, initialization can't be performed. |
||
| 238 | * To unlock the configuration, perform a system reset. |
||
| 239 | * @param hopamp OPAMP handle |
||
| 240 | * @retval HAL status |
||
| 241 | */ |
||
| 242 | HAL_StatusTypeDef HAL_OPAMP_Init(OPAMP_HandleTypeDef* hopamp) |
||
| 243 | { |
||
| 244 | HAL_StatusTypeDef status = HAL_OK; |
||
| 245 | uint32_t tmp_csr; /* Temporary variable to update register CSR, except bits ANAWSSELx, S7SEL2, OPA_RANGE, OPAxCALOUT */ |
||
| 246 | |||
| 247 | /* Check the OPAMP handle allocation and lock status */ |
||
| 248 | /* Init not allowed if calibration is ongoing */ |
||
| 249 | if(hopamp == NULL) |
||
| 250 | { |
||
| 251 | return HAL_ERROR; |
||
| 252 | } |
||
| 253 | else if(hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED) |
||
| 254 | { |
||
| 255 | return HAL_ERROR; |
||
| 256 | } |
||
| 257 | else if(hopamp->State == HAL_OPAMP_STATE_CALIBBUSY) |
||
| 258 | { |
||
| 259 | return HAL_ERROR; |
||
| 260 | } |
||
| 261 | else |
||
| 262 | { |
||
| 263 | /* Check the parameter */ |
||
| 264 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
||
| 265 | |||
| 266 | /* Set OPAMP parameters */ |
||
| 267 | assert_param(IS_OPAMP_POWER_SUPPLY_RANGE(hopamp->Init.PowerSupplyRange)); |
||
| 268 | assert_param(IS_OPAMP_POWERMODE(hopamp->Init.PowerMode)); |
||
| 269 | assert_param(IS_OPAMP_FUNCTIONAL_NORMALMODE(hopamp->Init.Mode)); |
||
| 270 | assert_param(IS_OPAMP_NONINVERTING_INPUT_CHECK_INSTANCE(hopamp, hopamp->Init.NonInvertingInput)); |
||
| 271 | assert_param(IS_OPAMP_TRIMMING(hopamp->Init.UserTrimming)); |
||
| 272 | |||
| 273 | #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1) |
||
| 274 | if(hopamp->State == HAL_OPAMP_STATE_RESET) |
||
| 275 | { |
||
| 276 | if(hopamp->MspInitCallback == NULL) |
||
| 277 | { |
||
| 278 | hopamp->MspInitCallback = HAL_OPAMP_MspInit; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */ |
||
| 282 | |||
| 283 | if (hopamp->Init.Mode != OPAMP_FOLLOWER_MODE) |
||
| 284 | { |
||
| 285 | assert_param(IS_OPAMP_INVERTING_INPUT(hopamp->Init.InvertingInput)); |
||
| 286 | } |
||
| 287 | |||
| 288 | if (hopamp->Init.UserTrimming == OPAMP_TRIMMING_USER) |
||
| 289 | { |
||
| 290 | if (hopamp->Init.PowerMode == OPAMP_POWERMODE_NORMAL) |
||
| 291 | { |
||
| 292 | assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueP)); |
||
| 293 | assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueN)); |
||
| 294 | } |
||
| 295 | else |
||
| 296 | { |
||
| 297 | assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValuePLowPower)); |
||
| 298 | assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueNLowPower)); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | if(hopamp->State == HAL_OPAMP_STATE_RESET) |
||
| 303 | { |
||
| 304 | /* Allocate lock resource and initialize it */ |
||
| 305 | hopamp->Lock = HAL_UNLOCKED; |
||
| 306 | } |
||
| 307 | |||
| 308 | #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1) |
||
| 309 | hopamp->MspInitCallback(hopamp); |
||
| 310 | #else |
||
| 311 | /* Call MSP init function */ |
||
| 312 | HAL_OPAMP_MspInit(hopamp); |
||
| 313 | #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */ |
||
| 314 | |||
| 315 | /* Set OPAMP parameters */ |
||
| 316 | /* - Set internal switches in function of: */ |
||
| 317 | /* - OPAMP selected mode: standalone or follower. */ |
||
| 318 | /* - Non-inverting input connection */ |
||
| 319 | /* - Inverting input connection */ |
||
| 320 | /* - Set power supply range */ |
||
| 321 | /* - Set power mode and associated calibration parameters */ |
||
| 322 | |||
| 323 | /* Get OPAMP CSR register into temporary variable */ |
||
| 324 | /* Note: OPAMP register CSR is written directly, independently of OPAMP */ |
||
| 325 | /* instance, because all OPAMP settings are dispatched in the same */ |
||
| 326 | /* register. */ |
||
| 327 | /* Settings of bits for each OPAMP instances are managed case by */ |
||
| 328 | /* case using macro (OPAMP_CSR_S3SELX(), OPAMP_CSR_ANAWSELX(), ...) */ |
||
| 329 | tmp_csr = OPAMP->CSR; |
||
| 330 | |||
| 331 | /* Open all switches on non-inverting input, inverting input and output */ |
||
| 332 | /* feedback. */ |
||
| 333 | CLEAR_BIT(tmp_csr, OPAMP_CSR_ALL_SWITCHES(hopamp)); |
||
| 334 | |||
| 335 | /* Set internal switches in function of OPAMP mode selected: standalone */ |
||
| 336 | /* or follower. */ |
||
| 337 | /* If follower mode is selected, feedback switch S3 is closed and */ |
||
| 338 | /* inverting inputs switches are let opened. */ |
||
| 339 | /* If standalone mode is selected, feedback switch S3 is let opened and */ |
||
| 340 | /* the selected inverting inputs switch is closed. */ |
||
| 341 | if (hopamp->Init.Mode == OPAMP_FOLLOWER_MODE) |
||
| 342 | { |
||
| 343 | /* Follower mode: Close switches S3 and SanB */ |
||
| 344 | SET_BIT(tmp_csr, OPAMP_CSR_S3SELX(hopamp)); |
||
| 345 | } |
||
| 346 | else |
||
| 347 | { |
||
| 348 | /* Set internal switches in function of inverting input selected: */ |
||
| 349 | /* Close switch to connect OPAMP inverting input to the selected */ |
||
| 350 | /* input: dedicated IO pin or alternative IO pin available on some */ |
||
| 351 | /* device packages. */ |
||
| 352 | if (hopamp->Init.InvertingInput == OPAMP_INVERTINGINPUT_IO0) |
||
| 353 | { |
||
| 354 | /* Close switch to connect OPAMP non-inverting input to */ |
||
| 355 | /* dedicated IO pin low-leakage. */ |
||
| 356 | SET_BIT(tmp_csr, OPAMP_CSR_S4SELX(hopamp)); |
||
| 357 | } |
||
| 358 | else |
||
| 359 | { |
||
| 360 | /* Close switch to connect OPAMP inverting input to alternative */ |
||
| 361 | /* IO pin available on some device packages. */ |
||
| 362 | SET_BIT(tmp_csr, OPAMP_CSR_ANAWSELX(hopamp)); |
||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | /* Set internal switches in function of non-inverting input selected: */ |
||
| 367 | /* Close switch to connect OPAMP non-inverting input to the selected */ |
||
| 368 | /* input: dedicated IO pin or DAC channel. */ |
||
| 369 | if (hopamp->Init.NonInvertingInput == OPAMP_NONINVERTINGINPUT_IO0) |
||
| 370 | { |
||
| 371 | /* Close switch to connect OPAMP non-inverting input to */ |
||
| 372 | /* dedicated IO pin low-leakage. */ |
||
| 373 | SET_BIT(tmp_csr, OPAMP_CSR_S5SELX(hopamp)); |
||
| 374 | } |
||
| 375 | else if (hopamp->Init.NonInvertingInput == OPAMP_NONINVERTINGINPUT_DAC_CH1) |
||
| 376 | { |
||
| 377 | |||
| 378 | /* Particular case for connection to DAC channel 1: */ |
||
| 379 | /* OPAMP_NONINVERTINGINPUT_DAC_CH1 available on OPAMP1 and OPAMP2 only */ |
||
| 380 | /* (OPAMP3 availability depends on device category). */ |
||
| 381 | if ((hopamp->Instance == OPAMP1) || (hopamp->Instance == OPAMP2)) |
||
| 382 | { |
||
| 383 | /* Close switch to connect OPAMP non-inverting input to */ |
||
| 384 | /* DAC channel 1. */ |
||
| 385 | SET_BIT(tmp_csr, OPAMP_CSR_S6SELX(hopamp)); |
||
| 386 | } |
||
| 387 | else |
||
| 388 | { |
||
| 389 | /* Set HAL status to error if another OPAMP instance as OPAMP1 or */ |
||
| 390 | /* OPAMP2 is intended to be connected to DAC channel 2. */ |
||
| 391 | status = HAL_ERROR; |
||
| 392 | } |
||
| 393 | } |
||
| 394 | else /* if (hopamp->Init.NonInvertingInput == */ |
||
| 395 | /* OPAMP_NONINVERTINGINPUT_DAC_CH2 ) */ |
||
| 396 | { |
||
| 397 | /* Particular case for connection to DAC channel 2: */ |
||
| 398 | /* OPAMP_NONINVERTINGINPUT_DAC_CH2 available on OPAMP2 and OPAMP3 only */ |
||
| 399 | /* (OPAMP3 availability depends on device category). */ |
||
| 400 | if (hopamp->Instance == OPAMP2) |
||
| 401 | { |
||
| 402 | /* Close switch to connect OPAMP non-inverting input to */ |
||
| 403 | /* DAC channel 2. */ |
||
| 404 | SET_BIT(tmp_csr, OPAMP_CSR_S7SEL2); |
||
| 405 | } |
||
| 406 | /* If OPAMP3 is selected (if available) */ |
||
| 407 | else if (hopamp->Instance != OPAMP1) |
||
| 408 | { |
||
| 409 | /* Close switch to connect OPAMP non-inverting input to */ |
||
| 410 | /* DAC channel 2. */ |
||
| 411 | SET_BIT(tmp_csr, OPAMP_CSR_S6SELX(hopamp)); |
||
| 412 | } |
||
| 413 | else |
||
| 414 | { |
||
| 415 | /* Set HAL status to error if another OPAMP instance as OPAMP2 or */ |
||
| 416 | /* OPAMP3 (if available) is intended to be connected to DAC channel 2.*/ |
||
| 417 | status = HAL_ERROR; |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | /* Continue OPAMP configuration if settings of switches are correct */ |
||
| 422 | if (status != HAL_ERROR) |
||
| 423 | { |
||
| 424 | /* Set power mode and associated calibration parameters */ |
||
| 425 | if (hopamp->Init.PowerMode != OPAMP_POWERMODE_LOWPOWER) |
||
| 426 | { |
||
| 427 | /* Set normal mode */ |
||
| 428 | CLEAR_BIT(tmp_csr, OPAMP_CSR_OPAXLPM(hopamp)); |
||
| 429 | |||
| 430 | if (hopamp->Init.UserTrimming == OPAMP_TRIMMING_USER) |
||
| 431 | { |
||
| 432 | /* Set calibration mode (factory or user) and values for */ |
||
| 433 | /* transistors differential pair high (PMOS) and low (NMOS) for */ |
||
| 434 | /* normal mode. */ |
||
| 435 | MODIFY_REG(OPAMP->OTR, OPAMP_OTR_OT_USER | |
||
| 436 | OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_N, OPAMP_TRIM_VALUE_MASK) | |
||
| 437 | OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_P, OPAMP_TRIM_VALUE_MASK) , |
||
| 438 | hopamp->Init.UserTrimming | |
||
| 439 | OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_N, hopamp->Init.TrimmingValueN) | |
||
| 440 | OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_P, hopamp->Init.TrimmingValueP) ); |
||
| 441 | } |
||
| 442 | else |
||
| 443 | { |
||
| 444 | /* Set calibration mode to factory */ |
||
| 445 | CLEAR_BIT(OPAMP->OTR, OPAMP_OTR_OT_USER); |
||
| 446 | } |
||
| 447 | |||
| 448 | } |
||
| 449 | else |
||
| 450 | { |
||
| 451 | /* Set low power mode */ |
||
| 452 | SET_BIT(tmp_csr, OPAMP_CSR_OPAXLPM(hopamp)); |
||
| 453 | |||
| 454 | if (hopamp->Init.UserTrimming == OPAMP_TRIMMING_USER) |
||
| 455 | { |
||
| 456 | /* Set calibration mode to user trimming */ |
||
| 457 | SET_BIT(OPAMP->OTR, OPAMP_OTR_OT_USER); |
||
| 458 | |||
| 459 | /* Set values for transistors differential pair high (PMOS) and low */ |
||
| 460 | /* (NMOS) for low power mode. */ |
||
| 461 | MODIFY_REG(OPAMP->LPOTR, OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_N, OPAMP_TRIM_VALUE_MASK) | |
||
| 462 | OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_P, OPAMP_TRIM_VALUE_MASK) , |
||
| 463 | OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_N, hopamp->Init.TrimmingValueNLowPower) | |
||
| 464 | OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_P, hopamp->Init.TrimmingValuePLowPower) ); |
||
| 465 | } |
||
| 466 | else |
||
| 467 | { |
||
| 468 | /* Set calibration mode to factory trimming */ |
||
| 469 | CLEAR_BIT(OPAMP->OTR, OPAMP_OTR_OT_USER); |
||
| 470 | } |
||
| 471 | |||
| 472 | } |
||
| 473 | |||
| 474 | |||
| 475 | /* Configure the power supply range */ |
||
| 476 | MODIFY_REG(tmp_csr, OPAMP_CSR_AOP_RANGE, |
||
| 477 | hopamp->Init.PowerSupplyRange); |
||
| 478 | |||
| 479 | /* Set OPAMP CSR register from temporary variable */ |
||
| 480 | /* This allows to apply all changes on one time, in case of update on */ |
||
| 481 | /* the fly with OPAMP previously set and running: */ |
||
| 482 | /* - to avoid hazardous transient switches settings (risk of short */ |
||
| 483 | /* circuit) */ |
||
| 484 | /* - to avoid interruption of input signal */ |
||
| 485 | OPAMP->CSR = tmp_csr; |
||
| 486 | |||
| 487 | |||
| 488 | /* Update the OPAMP state */ |
||
| 489 | /* If coming from state reset: Update from state RESET to state READY */ |
||
| 490 | if (hopamp->State == HAL_OPAMP_STATE_RESET) |
||
| 491 | { |
||
| 492 | hopamp->State = HAL_OPAMP_STATE_READY; |
||
| 493 | } |
||
| 494 | /* else: OPAMP state remains READY or BUSY state (no update) */ |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | return status; |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @brief DeInitializes the OPAMP peripheral |
||
| 503 | * @note Deinitialization can be performed if the OPAMP configuration is locked. |
||
| 504 | * (the OPAMP lock is SW in STM32L1) |
||
| 505 | * @param hopamp OPAMP handle |
||
| 506 | * @retval HAL status |
||
| 507 | */ |
||
| 508 | HAL_StatusTypeDef HAL_OPAMP_DeInit(OPAMP_HandleTypeDef* hopamp) |
||
| 509 | { |
||
| 510 | HAL_StatusTypeDef status = HAL_OK; |
||
| 511 | |||
| 512 | /* Check the OPAMP handle allocation */ |
||
| 513 | /* DeInit not allowed if calibration is ongoing */ |
||
| 514 | if(hopamp == NULL) |
||
| 515 | { |
||
| 516 | status = HAL_ERROR; |
||
| 517 | } |
||
| 518 | else if(hopamp->State == HAL_OPAMP_STATE_CALIBBUSY) |
||
| 519 | { |
||
| 520 | status = HAL_ERROR; |
||
| 521 | } |
||
| 522 | else |
||
| 523 | { |
||
| 524 | /* Check the parameter */ |
||
| 525 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
||
| 526 | |||
| 527 | /* Disable the selected opamp */ |
||
| 528 | SET_BIT (OPAMP->CSR, OPAMP_CSR_OPAXPD(hopamp)); |
||
| 529 | |||
| 530 | /* Open all switches on non-inverting input, inverting input and output */ |
||
| 531 | /* feedback. */ |
||
| 532 | /* Note: OPAMP register CSR is written directly, independently of OPAMP */ |
||
| 533 | /* instance, because all OPAMP settings are dispatched in the same */ |
||
| 534 | /* register. */ |
||
| 535 | /* Settings of bits for each OPAMP instances are managed case by */ |
||
| 536 | /* case using macro (OPAMP_CSR_S3SELX(), OPAMP_CSR_ANAWSELX(), ...) */ |
||
| 537 | CLEAR_BIT(OPAMP->CSR, OPAMP_CSR_ALL_SWITCHES(hopamp)); |
||
| 538 | |||
| 539 | /* Note: Registers and bits shared with other OPAMP instances are kept */ |
||
| 540 | /* unchanged, to not impact other OPAMP while operating on the */ |
||
| 541 | /* selected OPAMP. */ |
||
| 542 | /* Unchanged: bit OPAMP_OTR_OT_USER (parameter "UserTrimming") */ |
||
| 543 | /* bit OPAMP_CSR_AOP_RANGE (parameter "PowerSupplyRange")*/ |
||
| 544 | |||
| 545 | #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1) |
||
| 546 | if(hopamp->MspDeInitCallback == NULL) |
||
| 547 | { |
||
| 548 | hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit; |
||
| 549 | } |
||
| 550 | /* DeInit the low level hardware */ |
||
| 551 | hopamp->MspDeInitCallback(hopamp); |
||
| 552 | #else |
||
| 553 | /* DeInit the low level hardware: GPIO, CLOCK and NVIC */ |
||
| 554 | HAL_OPAMP_MspDeInit(hopamp); |
||
| 555 | #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */ |
||
| 556 | |||
| 557 | /* Update the OPAMP state*/ |
||
| 558 | hopamp->State = HAL_OPAMP_STATE_RESET; |
||
| 559 | } |
||
| 560 | |||
| 561 | /* Process unlocked */ |
||
| 562 | __HAL_UNLOCK(hopamp); |
||
| 563 | |||
| 564 | return status; |
||
| 565 | } |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @brief Initialize the OPAMP MSP. |
||
| 569 | * @param hopamp OPAMP handle |
||
| 570 | * @retval None |
||
| 571 | */ |
||
| 572 | __weak void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef* hopamp) |
||
| 573 | { |
||
| 574 | /* Prevent unused argument(s) compilation warning */ |
||
| 575 | UNUSED(hopamp); |
||
| 576 | |||
| 577 | /* NOTE : This function should not be modified, when the callback is needed, |
||
| 578 | the function "HAL_OPAMP_MspInit()" must be implemented in the user file. |
||
| 579 | */ |
||
| 580 | } |
||
| 581 | |||
| 582 | /** |
||
| 583 | * @brief DeInitialize OPAMP MSP. |
||
| 584 | * @param hopamp OPAMP handle |
||
| 585 | * @retval None |
||
| 586 | */ |
||
| 587 | __weak void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef* hopamp) |
||
| 588 | { |
||
| 589 | /* Prevent unused argument(s) compilation warning */ |
||
| 590 | UNUSED(hopamp); |
||
| 591 | |||
| 592 | /* NOTE : This function should not be modified, when the callback is needed, |
||
| 593 | the function "HAL_OPAMP_MspDeInit()" must be implemented in the user file. |
||
| 594 | */ |
||
| 595 | } |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @} |
||
| 599 | */ |
||
| 600 | |||
| 601 | |||
| 602 | /** @defgroup OPAMP_Exported_Functions_Group2 IO operation functions |
||
| 603 | * @brief IO operation functions |
||
| 604 | * |
||
| 605 | @verbatim |
||
| 606 | =============================================================================== |
||
| 607 | ##### IO operation functions ##### |
||
| 608 | =============================================================================== |
||
| 609 | [..] |
||
| 610 | This subsection provides a set of functions allowing to manage the OPAMP |
||
| 611 | start, stop and calibration actions. |
||
| 612 | |||
| 613 | @endverbatim |
||
| 614 | * @{ |
||
| 615 | */ |
||
| 616 | |||
| 617 | /** |
||
| 618 | * @brief Start the OPAMP. |
||
| 619 | * @param hopamp OPAMP handle |
||
| 620 | * @retval HAL status |
||
| 621 | */ |
||
| 622 | |||
| 623 | HAL_StatusTypeDef HAL_OPAMP_Start(OPAMP_HandleTypeDef* hopamp) |
||
| 624 | { |
||
| 625 | HAL_StatusTypeDef status = HAL_OK; |
||
| 626 | |||
| 627 | /* Check the OPAMP handle allocation */ |
||
| 628 | /* Check if OPAMP locked */ |
||
| 629 | if(hopamp == NULL) |
||
| 630 | { |
||
| 631 | status = HAL_ERROR; |
||
| 632 | } |
||
| 633 | else if(hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED) |
||
| 634 | { |
||
| 635 | status = HAL_ERROR; |
||
| 636 | } |
||
| 637 | else |
||
| 638 | { |
||
| 639 | /* Check the parameter */ |
||
| 640 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
||
| 641 | |||
| 642 | if(hopamp->State == HAL_OPAMP_STATE_READY) |
||
| 643 | { |
||
| 644 | /* Enable the selected opamp */ |
||
| 645 | CLEAR_BIT (OPAMP->CSR, OPAMP_CSR_OPAXPD(hopamp)); |
||
| 646 | |||
| 647 | /* Update the OPAMP state */ |
||
| 648 | /* From HAL_OPAMP_STATE_READY to HAL_OPAMP_STATE_BUSY */ |
||
| 649 | hopamp->State = HAL_OPAMP_STATE_BUSY; |
||
| 650 | } |
||
| 651 | else |
||
| 652 | { |
||
| 653 | status = HAL_ERROR; |
||
| 654 | } |
||
| 655 | |||
| 656 | } |
||
| 657 | return status; |
||
| 658 | } |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @brief Stop the OPAMP. |
||
| 662 | * @param hopamp OPAMP handle |
||
| 663 | * @retval HAL status |
||
| 664 | */ |
||
| 665 | HAL_StatusTypeDef HAL_OPAMP_Stop(OPAMP_HandleTypeDef* hopamp) |
||
| 666 | { |
||
| 667 | HAL_StatusTypeDef status = HAL_OK; |
||
| 668 | |||
| 669 | /* Check the OPAMP handle allocation */ |
||
| 670 | /* Check if OPAMP locked */ |
||
| 671 | /* Check if OPAMP calibration ongoing */ |
||
| 672 | if(hopamp == NULL) |
||
| 673 | { |
||
| 674 | status = HAL_ERROR; |
||
| 675 | } |
||
| 676 | else if(hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED) |
||
| 677 | { |
||
| 678 | status = HAL_ERROR; |
||
| 679 | } |
||
| 680 | else if(hopamp->State == HAL_OPAMP_STATE_CALIBBUSY) |
||
| 681 | { |
||
| 682 | status = HAL_ERROR; |
||
| 683 | } |
||
| 684 | else |
||
| 685 | { |
||
| 686 | /* Check the parameter */ |
||
| 687 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
||
| 688 | |||
| 689 | if(hopamp->State == HAL_OPAMP_STATE_BUSY) |
||
| 690 | { |
||
| 691 | /* Disable the selected opamp */ |
||
| 692 | SET_BIT (OPAMP->CSR, OPAMP_CSR_OPAXPD(hopamp)); |
||
| 693 | |||
| 694 | /* Update the OPAMP state*/ |
||
| 695 | /* From HAL_OPAMP_STATE_BUSY to HAL_OPAMP_STATE_READY*/ |
||
| 696 | hopamp->State = HAL_OPAMP_STATE_READY; |
||
| 697 | } |
||
| 698 | else |
||
| 699 | { |
||
| 700 | status = HAL_ERROR; |
||
| 701 | } |
||
| 702 | } |
||
| 703 | return status; |
||
| 704 | } |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @brief Run the self calibration of one OPAMP. |
||
| 708 | * @note Trimming values (PMOS & NMOS) are updated and user trimming is |
||
| 709 | * enabled if calibration is successful. |
||
| 710 | * @note Calibration is performed in the mode specified in OPAMP init |
||
| 711 | * structure (mode normal or low-power). To perform calibration for |
||
| 712 | * both modes, repeat this function twice after OPAMP init structure |
||
| 713 | * accordingly updated. |
||
| 714 | * @note Calibration runs about 10 ms. |
||
| 715 | * @param hopamp handle |
||
| 716 | * @retval Updated offset trimming values (PMOS & NMOS), user trimming is enabled |
||
| 717 | * @retval HAL status |
||
| 718 | */ |
||
| 719 | HAL_StatusTypeDef HAL_OPAMP_SelfCalibrate(OPAMP_HandleTypeDef* hopamp) |
||
| 720 | { |
||
| 721 | HAL_StatusTypeDef status = HAL_OK; |
||
| 722 | |||
| 723 | uint32_t* opamp_trimmingvalue; |
||
| 724 | uint32_t opamp_trimmingvaluen = 0; |
||
| 725 | uint32_t opamp_trimmingvaluep = 0; |
||
| 726 | |||
| 727 | uint32_t trimming_diff_pair; /* Selection of differential transistors pair high or low */ |
||
| 728 | |||
| 729 | __IO uint32_t* tmp_opamp_reg_trimming; /* Selection of register of trimming depending on power mode: OTR or LPOTR */ |
||
| 730 | uint32_t tmp_opamp_otr_otuser; /* Selection of bit OPAMP_OTR_OT_USER depending on trimming register pointed: OTR or LPOTR */ |
||
| 731 | |||
| 732 | uint32_t tmp_Opaxcalout_DefaultSate; /* Bit OPAMP_CSR_OPAXCALOUT default state when trimming value is 00000b. Used to detect the bit toggling */ |
||
| 733 | |||
| 734 | uint32_t tmp_OpaxSwitchesContextBackup; |
||
| 735 | |||
| 736 | uint8_t trimming_diff_pair_iteration_count; /* For calibration loop algorithm: to repeat the calibration loop for both differential transistors pair high and low */ |
||
| 737 | uint8_t delta; /* For calibration loop algorithm: Variable for dichotomy steps value */ |
||
| 738 | uint8_t final_step_check = 0x0U; /* For calibration loop algorithm: Flag for additional check of last trimming step */ |
||
| 739 | |||
| 740 | /* Check the OPAMP handle allocation */ |
||
| 741 | /* Check if OPAMP locked */ |
||
| 742 | if(hopamp == NULL) |
||
| 743 | { |
||
| 744 | status = HAL_ERROR; |
||
| 745 | } |
||
| 746 | else if(hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED) |
||
| 747 | { |
||
| 748 | status = HAL_ERROR; |
||
| 749 | } |
||
| 750 | else |
||
| 751 | { |
||
| 752 | |||
| 753 | /* Check if OPAMP in calibration mode and calibration not yet enable */ |
||
| 754 | if(hopamp->State == HAL_OPAMP_STATE_READY) |
||
| 755 | { |
||
| 756 | /* Check the parameter */ |
||
| 757 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
||
| 758 | assert_param(IS_OPAMP_POWERMODE(hopamp->Init.PowerMode)); |
||
| 759 | |||
| 760 | /* Update OPAMP state */ |
||
| 761 | hopamp->State = HAL_OPAMP_STATE_CALIBBUSY; |
||
| 762 | |||
| 763 | /* Backup of switches configuration to restore it at the end of the */ |
||
| 764 | /* calibration. */ |
||
| 765 | tmp_OpaxSwitchesContextBackup = READ_BIT(OPAMP->CSR, OPAMP_CSR_ALL_SWITCHES(hopamp)); |
||
| 766 | |||
| 767 | /* Open all switches on non-inverting input, inverting input and output */ |
||
| 768 | /* feedback. */ |
||
| 769 | CLEAR_BIT(OPAMP->CSR, OPAMP_CSR_ALL_SWITCHES(hopamp)); |
||
| 770 | |||
| 771 | /* Set calibration mode to user programmed trimming values */ |
||
| 772 | SET_BIT(OPAMP->OTR, OPAMP_OTR_OT_USER); |
||
| 773 | |||
| 774 | |||
| 775 | /* Select trimming settings depending on power mode */ |
||
| 776 | if (hopamp->Init.PowerMode == OPAMP_POWERMODE_NORMAL) |
||
| 777 | { |
||
| 778 | tmp_opamp_otr_otuser = OPAMP_OTR_OT_USER; |
||
| 779 | tmp_opamp_reg_trimming = &OPAMP->OTR; |
||
| 780 | } |
||
| 781 | else |
||
| 782 | { |
||
| 783 | tmp_opamp_otr_otuser = 0x00000000U; |
||
| 784 | tmp_opamp_reg_trimming = &OPAMP->LPOTR; |
||
| 785 | } |
||
| 786 | |||
| 787 | |||
| 788 | /* Enable the selected opamp */ |
||
| 789 | CLEAR_BIT (OPAMP->CSR, OPAMP_CSR_OPAXPD(hopamp)); |
||
| 790 | |||
| 791 | /* Perform trimming for both differential transistors pair high and low */ |
||
| 792 | for (trimming_diff_pair_iteration_count = 0U; trimming_diff_pair_iteration_count <=1U; trimming_diff_pair_iteration_count++) |
||
| 793 | { |
||
| 794 | if (trimming_diff_pair_iteration_count == 0U) |
||
| 795 | { |
||
| 796 | /* Calibration of transistors differential pair high (NMOS) */ |
||
| 797 | trimming_diff_pair = OPAMP_FACTORYTRIMMING_N; |
||
| 798 | opamp_trimmingvalue = &opamp_trimmingvaluen; |
||
| 799 | |||
| 800 | /* Set bit OPAMP_CSR_OPAXCALOUT default state when trimming value */ |
||
| 801 | /* is 00000b. Used to detect the bit toggling during trimming. */ |
||
| 802 | tmp_Opaxcalout_DefaultSate = 0U; |
||
| 803 | |||
| 804 | /* Enable calibration for N differential pair */ |
||
| 805 | MODIFY_REG(OPAMP->CSR, OPAMP_CSR_OPAXCAL_L(hopamp), |
||
| 806 | OPAMP_CSR_OPAXCAL_H(hopamp) ); |
||
| 807 | } |
||
| 808 | else /* (trimming_diff_pair_iteration_count == 1) */ |
||
| 809 | { |
||
| 810 | /* Calibration of transistors differential pair low (PMOS) */ |
||
| 811 | trimming_diff_pair = OPAMP_FACTORYTRIMMING_P; |
||
| 812 | opamp_trimmingvalue = &opamp_trimmingvaluep; |
||
| 813 | |||
| 814 | /* Set bit OPAMP_CSR_OPAXCALOUT default state when trimming value */ |
||
| 815 | /* is 00000b. Used to detect the bit toggling during trimming. */ |
||
| 816 | tmp_Opaxcalout_DefaultSate = OPAMP_CSR_OPAXCALOUT(hopamp); |
||
| 817 | |||
| 818 | /* Enable calibration for P differential pair */ |
||
| 819 | MODIFY_REG(OPAMP->CSR, OPAMP_CSR_OPAXCAL_H(hopamp), |
||
| 820 | OPAMP_CSR_OPAXCAL_L(hopamp) ); |
||
| 821 | } |
||
| 822 | |||
| 823 | |||
| 824 | /* Perform calibration parameter search by dichotomy sweep */ |
||
| 825 | /* - Delta initial value 16: for 5 dichotomy steps: 16 for the */ |
||
| 826 | /* initial range, then successive delta sweeps (8, 4, 2, 1). */ |
||
| 827 | /* can extend the search range to +/- 15 units. */ |
||
| 828 | /* - Trimming initial value 15: search range will go from 0 to 30 */ |
||
| 829 | /* (Trimming value 31 is forbidden). */ |
||
| 830 | /* Note: After dichotomy sweep, the trimming result is determined. */ |
||
| 831 | /* However, the final trimming step is deduced from previous */ |
||
| 832 | /* trimming steps tested but is not effectively tested. */ |
||
| 833 | /* An additional test step (using variable "final_step_check") */ |
||
| 834 | /* allow to Test the final trimming step. */ |
||
| 835 | *opamp_trimmingvalue = 15U; |
||
| 836 | delta = 16U; |
||
| 837 | |||
| 838 | while ((delta != 0U) || (final_step_check == 1U)) |
||
| 839 | { |
||
| 840 | /* Set candidate trimming */ |
||
| 841 | MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OFFSET_TRIM_SET(hopamp, trimming_diff_pair, OPAMP_TRIM_VALUE_MASK) , |
||
| 842 | OPAMP_OFFSET_TRIM_SET(hopamp, trimming_diff_pair, *opamp_trimmingvalue) | tmp_opamp_otr_otuser); |
||
| 843 | |||
| 844 | /* Offset trimming time: during calibration, minimum time needed */ |
||
| 845 | /* between two steps to have 1 mV accuracy. */ |
||
| 846 | HAL_Delay(OPAMP_TRIMMING_DELAY); |
||
| 847 | |||
| 848 | /* Set flag for additional check of last trimming step equal to */ |
||
| 849 | /* dichotomy step before its division by 2 (equivalent to previous */ |
||
| 850 | /* value of dichotomy step). */ |
||
| 851 | final_step_check = delta; |
||
| 852 | |||
| 853 | /* Divide range by 2 to continue dichotomy sweep */ |
||
| 854 | delta >>= 1; |
||
| 855 | |||
| 856 | /* Set trimming values for next iteration in function of trimming */ |
||
| 857 | /* result toggle (versus initial state). */ |
||
| 858 | /* Note: on the last trimming loop, delta is equal to 0 and */ |
||
| 859 | /* therefore has no effect. */ |
||
| 860 | if (READ_BIT(OPAMP->CSR, OPAMP_CSR_OPAXCALOUT(hopamp)) != tmp_Opaxcalout_DefaultSate) |
||
| 861 | { |
||
| 862 | /* If calibration output is has toggled, try lower trimming */ |
||
| 863 | *opamp_trimmingvalue -= delta; |
||
| 864 | } |
||
| 865 | else |
||
| 866 | { |
||
| 867 | /* If calibration output is has not toggled, try higher trimming */ |
||
| 868 | *opamp_trimmingvalue += delta; |
||
| 869 | } |
||
| 870 | |||
| 871 | } |
||
| 872 | |||
| 873 | /* Check trimming result of the selected step and perform final fine */ |
||
| 874 | /* trimming. */ |
||
| 875 | /* - If calibration output is has toggled: the current step is */ |
||
| 876 | /* already optimized. */ |
||
| 877 | /* - If calibration output is has not toggled: the current step can */ |
||
| 878 | /* be optimized by incrementing it of one step. */ |
||
| 879 | if (READ_BIT(OPAMP->CSR, OPAMP_CSR_OPAXCALOUT(hopamp)) == tmp_Opaxcalout_DefaultSate) |
||
| 880 | { |
||
| 881 | *opamp_trimmingvalue += 1U; |
||
| 882 | |||
| 883 | /* Set final fine trimming */ |
||
| 884 | MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OFFSET_TRIM_SET(hopamp, trimming_diff_pair, OPAMP_TRIM_VALUE_MASK) , |
||
| 885 | OPAMP_OFFSET_TRIM_SET(hopamp, trimming_diff_pair, *opamp_trimmingvalue) | tmp_opamp_otr_otuser); |
||
| 886 | } |
||
| 887 | |||
| 888 | } |
||
| 889 | |||
| 890 | |||
| 891 | /* Disable calibration for P and N differential pairs */ |
||
| 892 | /* Disable the selected opamp */ |
||
| 893 | CLEAR_BIT (OPAMP->CSR, (OPAMP_CSR_OPAXCAL_H(hopamp) | |
||
| 894 | OPAMP_CSR_OPAXCAL_L(hopamp) | |
||
| 895 | OPAMP_CSR_OPAXPD(hopamp)) ); |
||
| 896 | |||
| 897 | /* Backup of switches configuration to restore it at the end of the */ |
||
| 898 | /* calibration. */ |
||
| 899 | SET_BIT(OPAMP->CSR, tmp_OpaxSwitchesContextBackup); |
||
| 900 | |||
| 901 | /* Self calibration is successful */ |
||
| 902 | /* Store calibration (user trimming) results in init structure. */ |
||
| 903 | |||
| 904 | /* Set user trimming mode */ |
||
| 905 | hopamp->Init.UserTrimming = OPAMP_TRIMMING_USER; |
||
| 906 | |||
| 907 | /* Check on unsupported value */ |
||
| 908 | if(opamp_trimmingvaluep == 0x1FU) /* 0x1F is not functional */ |
||
| 909 | { |
||
| 910 | opamp_trimmingvaluep = 30U; |
||
| 911 | } |
||
| 912 | |||
| 913 | if(opamp_trimmingvaluen == 0x1FU) /* 0x1F is not functional */ |
||
| 914 | { |
||
| 915 | opamp_trimmingvaluen = 30U; |
||
| 916 | } |
||
| 917 | |||
| 918 | /* Affect calibration parameters depending on mode normal/low power */ |
||
| 919 | if (hopamp->Init.PowerMode != OPAMP_POWERMODE_LOWPOWER) |
||
| 920 | { |
||
| 921 | /* Write calibration result N */ |
||
| 922 | hopamp->Init.TrimmingValueN = opamp_trimmingvaluen; |
||
| 923 | /* Write calibration result P */ |
||
| 924 | hopamp->Init.TrimmingValueP = opamp_trimmingvaluep; |
||
| 925 | } |
||
| 926 | else |
||
| 927 | { |
||
| 928 | /* Write calibration result N */ |
||
| 929 | hopamp->Init.TrimmingValueNLowPower = opamp_trimmingvaluen; |
||
| 930 | /* Write calibration result P */ |
||
| 931 | hopamp->Init.TrimmingValuePLowPower = opamp_trimmingvaluep; |
||
| 932 | } |
||
| 933 | |||
| 934 | /* Update OPAMP state */ |
||
| 935 | hopamp->State = HAL_OPAMP_STATE_READY; |
||
| 936 | |||
| 937 | } |
||
| 938 | |||
| 939 | else |
||
| 940 | { |
||
| 941 | /* OPAMP can not be calibrated from this mode */ |
||
| 942 | status = HAL_ERROR; |
||
| 943 | } |
||
| 944 | } |
||
| 945 | |||
| 946 | return status; |
||
| 947 | |||
| 948 | } |
||
| 949 | |||
| 950 | /** |
||
| 951 | * @} |
||
| 952 | */ |
||
| 953 | |||
| 954 | /** |
||
| 955 | * @} |
||
| 956 | */ |
||
| 957 | |||
| 958 | /** @defgroup OPAMP_Exported_Functions_Group3 Peripheral Control functions |
||
| 959 | * @brief Peripheral Control functions |
||
| 960 | * |
||
| 961 | @verbatim |
||
| 962 | =============================================================================== |
||
| 963 | ##### Peripheral Control functions ##### |
||
| 964 | =============================================================================== |
||
| 965 | [..] |
||
| 966 | This subsection provides a set of functions allowing to control the OPAMP data |
||
| 967 | transfers. |
||
| 968 | |||
| 969 | |||
| 970 | |||
| 971 | @endverbatim |
||
| 972 | * @{ |
||
| 973 | */ |
||
| 974 | |||
| 975 | /** |
||
| 976 | * @brief Lock the selected opamp configuration. |
||
| 977 | * Caution: On STM32L1, HAL OPAMP lock is software lock only |
||
| 978 | * (not hardware lock as available on some other STM32 devices) |
||
| 979 | * @param hopamp OPAMP handle |
||
| 980 | * @retval HAL status |
||
| 981 | */ |
||
| 982 | HAL_StatusTypeDef HAL_OPAMP_Lock(OPAMP_HandleTypeDef* hopamp) |
||
| 983 | { |
||
| 984 | HAL_StatusTypeDef status = HAL_OK; |
||
| 985 | |||
| 986 | /* Check the OPAMP handle allocation */ |
||
| 987 | /* Check if OPAMP locked */ |
||
| 988 | /* OPAMP can be locked when enabled and running in normal mode */ |
||
| 989 | /* It is meaningless otherwise */ |
||
| 990 | if(hopamp == NULL) |
||
| 991 | { |
||
| 992 | status = HAL_ERROR; |
||
| 993 | } |
||
| 994 | else if(hopamp->State == HAL_OPAMP_STATE_BUSY) |
||
| 995 | { |
||
| 996 | /* Check the parameter */ |
||
| 997 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
||
| 998 | |||
| 999 | /* OPAMP state changed to locked */ |
||
| 1000 | hopamp->State = HAL_OPAMP_STATE_BUSYLOCKED; |
||
| 1001 | } |
||
| 1002 | else |
||
| 1003 | { |
||
| 1004 | status = HAL_ERROR; |
||
| 1005 | } |
||
| 1006 | return status; |
||
| 1007 | } |
||
| 1008 | |||
| 1009 | /** |
||
| 1010 | * @brief Return the OPAMP factory trimming value |
||
| 1011 | * Caution: On STM32L1 OPAMP, user can retrieve factory trimming if |
||
| 1012 | * OPAMP has never been set to user trimming before. |
||
| 1013 | * Therefore, this function must be called when OPAMP init |
||
| 1014 | * parameter "UserTrimming" is set to trimming factory, |
||
| 1015 | * and before OPAMP calibration (function |
||
| 1016 | * "HAL_OPAMP_SelfCalibrate()"). |
||
| 1017 | * Otherwise, factory trimming value cannot be retrieved and |
||
| 1018 | * error status is returned. |
||
| 1019 | * @param hopamp OPAMP handle |
||
| 1020 | * @param trimmingoffset Trimming offset (P or N) |
||
| 1021 | * This parameter must be a value of @ref OPAMP_FactoryTrimming |
||
| 1022 | * @note Calibration parameter retrieved is corresponding to the mode |
||
| 1023 | * specified in OPAMP init structure (mode normal or low-power). |
||
| 1024 | * To retrieve calibration parameters for both modes, repeat this |
||
| 1025 | * function after OPAMP init structure accordingly updated. |
||
| 1026 | * @retval Trimming value (P or N) range: 0->31 |
||
| 1027 | * or OPAMP_FACTORYTRIMMING_DUMMY if trimming value is not available |
||
| 1028 | * |
||
| 1029 | */ |
||
| 1030 | HAL_OPAMP_TrimmingValueTypeDef HAL_OPAMP_GetTrimOffset (OPAMP_HandleTypeDef *hopamp, uint32_t trimmingoffset) |
||
| 1031 | { |
||
| 1032 | HAL_OPAMP_TrimmingValueTypeDef trimmingvalue; |
||
| 1033 | __IO uint32_t* tmp_opamp_reg_trimming; /* Selection of register of trimming depending on power mode: OTR or LPOTR */ |
||
| 1034 | |||
| 1035 | /* Check the OPAMP handle allocation */ |
||
| 1036 | /* Value can be retrieved in HAL_OPAMP_STATE_READY state */ |
||
| 1037 | if(hopamp == NULL) |
||
| 1038 | { |
||
| 1039 | return OPAMP_FACTORYTRIMMING_DUMMY; |
||
| 1040 | } |
||
| 1041 | |||
| 1042 | /* Check the OPAMP handle allocation */ |
||
| 1043 | /* Value can be retrieved in HAL_OPAMP_STATE_READY state */ |
||
| 1044 | if(hopamp->State == HAL_OPAMP_STATE_READY) |
||
| 1045 | { |
||
| 1046 | /* Check the parameter */ |
||
| 1047 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
||
| 1048 | assert_param(IS_OPAMP_FACTORYTRIMMING(trimmingoffset)); |
||
| 1049 | assert_param(IS_OPAMP_POWERMODE(hopamp->Init.PowerMode)); |
||
| 1050 | |||
| 1051 | /* Check the trimming mode */ |
||
| 1052 | if (hopamp->Init.UserTrimming == OPAMP_TRIMMING_USER) |
||
| 1053 | { |
||
| 1054 | /* This function must called when OPAMP init parameter "UserTrimming" */ |
||
| 1055 | /* is set to trimming factory, and before OPAMP calibration (function */ |
||
| 1056 | /* "HAL_OPAMP_SelfCalibrate()"). */ |
||
| 1057 | /* Otherwise, factory trimming value cannot be retrieved and error */ |
||
| 1058 | /* status is returned. */ |
||
| 1059 | trimmingvalue = OPAMP_FACTORYTRIMMING_DUMMY; |
||
| 1060 | } |
||
| 1061 | else |
||
| 1062 | { |
||
| 1063 | /* Select trimming settings depending on power mode */ |
||
| 1064 | if (hopamp->Init.PowerMode == OPAMP_POWERMODE_NORMAL) |
||
| 1065 | { |
||
| 1066 | tmp_opamp_reg_trimming = &OPAMP->OTR; |
||
| 1067 | } |
||
| 1068 | else |
||
| 1069 | { |
||
| 1070 | tmp_opamp_reg_trimming = &OPAMP->LPOTR; |
||
| 1071 | } |
||
| 1072 | |||
| 1073 | /* Get factory trimming */ |
||
| 1074 | trimmingvalue = ((*tmp_opamp_reg_trimming >> OPAMP_OFFSET_TRIM_BITSPOSITION(hopamp, trimmingoffset)) & OPAMP_TRIM_VALUE_MASK); |
||
| 1075 | } |
||
| 1076 | } |
||
| 1077 | else |
||
| 1078 | { |
||
| 1079 | return OPAMP_FACTORYTRIMMING_DUMMY; |
||
| 1080 | } |
||
| 1081 | return trimmingvalue; |
||
| 1082 | } |
||
| 1083 | |||
| 1084 | /** |
||
| 1085 | * @} |
||
| 1086 | */ |
||
| 1087 | |||
| 1088 | |||
| 1089 | /** @defgroup OPAMP_Exported_Functions_Group4 Peripheral State functions |
||
| 1090 | * @brief Peripheral State functions |
||
| 1091 | * |
||
| 1092 | @verbatim |
||
| 1093 | =============================================================================== |
||
| 1094 | ##### Peripheral State functions ##### |
||
| 1095 | =============================================================================== |
||
| 1096 | [..] |
||
| 1097 | This subsection permits to get in run-time the status of the peripheral. |
||
| 1098 | |||
| 1099 | @endverbatim |
||
| 1100 | * @{ |
||
| 1101 | */ |
||
| 1102 | |||
| 1103 | /** |
||
| 1104 | * @brief Return the OPAMP handle state. |
||
| 1105 | * @param hopamp OPAMP handle |
||
| 1106 | * @retval HAL state |
||
| 1107 | */ |
||
| 1108 | HAL_OPAMP_StateTypeDef HAL_OPAMP_GetState(OPAMP_HandleTypeDef* hopamp) |
||
| 1109 | { |
||
| 1110 | /* Check the OPAMP handle allocation */ |
||
| 1111 | if(hopamp == NULL) |
||
| 1112 | { |
||
| 1113 | return HAL_OPAMP_STATE_RESET; |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | /* Check the parameter */ |
||
| 1117 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance)); |
||
| 1118 | |||
| 1119 | return hopamp->State; |
||
| 1120 | } |
||
| 1121 | |||
| 1122 | #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1) |
||
| 1123 | /** |
||
| 1124 | * @brief Register a User OPAMP Callback |
||
| 1125 | * To be used instead of the weak (overridden) predefined callback |
||
| 1126 | * @param hopamp OPAMP handle |
||
| 1127 | * @param CallbackID ID of the callback to be registered |
||
| 1128 | * This parameter can be one of the following values: |
||
| 1129 | * @arg @ref HAL_OPAMP_MSPINIT_CB_ID OPAMP MspInit callback ID |
||
| 1130 | * @arg @ref HAL_OPAMP_MSPDEINIT_CB_ID OPAMP MspDeInit callback ID |
||
| 1131 | * @param pCallback pointer to the Callback function |
||
| 1132 | * @retval status |
||
| 1133 | */ |
||
| 1134 | HAL_StatusTypeDef HAL_OPAMP_RegisterCallback (OPAMP_HandleTypeDef *hopamp, HAL_OPAMP_CallbackIDTypeDef CallbackID, pOPAMP_CallbackTypeDef pCallback) |
||
| 1135 | { |
||
| 1136 | HAL_StatusTypeDef status = HAL_OK; |
||
| 1137 | |||
| 1138 | if(pCallback == NULL) |
||
| 1139 | { |
||
| 1140 | return HAL_ERROR; |
||
| 1141 | } |
||
| 1142 | |||
| 1143 | /* Process locked */ |
||
| 1144 | __HAL_LOCK(hopamp); |
||
| 1145 | |||
| 1146 | if(hopamp->State == HAL_OPAMP_STATE_READY) |
||
| 1147 | { |
||
| 1148 | switch (CallbackID) |
||
| 1149 | { |
||
| 1150 | case HAL_OPAMP_MSPINIT_CB_ID : |
||
| 1151 | hopamp->MspInitCallback = pCallback; |
||
| 1152 | break; |
||
| 1153 | case HAL_OPAMP_MSPDEINIT_CB_ID : |
||
| 1154 | hopamp->MspDeInitCallback = pCallback; |
||
| 1155 | break; |
||
| 1156 | default : |
||
| 1157 | /* Update the error code */ |
||
| 1158 | // hopamp->ErrorCode |= HAL_OPAMP_ERROR_INVALID_CALLBACK; |
||
| 1159 | /* update return status */ |
||
| 1160 | status = HAL_ERROR; |
||
| 1161 | break; |
||
| 1162 | } |
||
| 1163 | } |
||
| 1164 | else if (hopamp->State == HAL_OPAMP_STATE_RESET) |
||
| 1165 | { |
||
| 1166 | switch (CallbackID) |
||
| 1167 | { |
||
| 1168 | case HAL_OPAMP_MSPINIT_CB_ID : |
||
| 1169 | hopamp->MspInitCallback = pCallback; |
||
| 1170 | break; |
||
| 1171 | case HAL_OPAMP_MSPDEINIT_CB_ID : |
||
| 1172 | hopamp->MspDeInitCallback = pCallback; |
||
| 1173 | break; |
||
| 1174 | default : |
||
| 1175 | /* Update the error code */ |
||
| 1176 | // hopamp->ErrorCode |= HAL_OPAMP_ERROR_INVALID_CALLBACK; |
||
| 1177 | /* update return status */ |
||
| 1178 | status = HAL_ERROR; |
||
| 1179 | break; |
||
| 1180 | } |
||
| 1181 | } |
||
| 1182 | else |
||
| 1183 | { |
||
| 1184 | /* update return status */ |
||
| 1185 | status = HAL_ERROR; |
||
| 1186 | } |
||
| 1187 | |||
| 1188 | /* Release Lock */ |
||
| 1189 | __HAL_UNLOCK(hopamp); |
||
| 1190 | return status; |
||
| 1191 | } |
||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * @brief Unregister a User OPAMP Callback |
||
| 1195 | * OPAMP Callback is redirected to the weak (overridden) predefined callback |
||
| 1196 | * @param hopamp OPAMP handle |
||
| 1197 | * @param CallbackID ID of the callback to be unregistered |
||
| 1198 | * This parameter can be one of the following values: |
||
| 1199 | * @arg @ref HAL_OPAMP_MSPINIT_CB_ID OPAMP MSP Init Callback ID |
||
| 1200 | * @arg @ref HAL_OPAMP_MSPDEINIT_CB_ID OPAMP MSP DeInit Callback ID |
||
| 1201 | * @arg @ref HAL_OPAMP_ALL_CB_ID OPAMP All Callbacks |
||
| 1202 | * @retval status |
||
| 1203 | */ |
||
| 1204 | |||
| 1205 | HAL_StatusTypeDef HAL_OPAMP_UnRegisterCallback (OPAMP_HandleTypeDef *hopamp, HAL_OPAMP_CallbackIDTypeDef CallbackID) |
||
| 1206 | { |
||
| 1207 | HAL_StatusTypeDef status = HAL_OK; |
||
| 1208 | |||
| 1209 | /* Process locked */ |
||
| 1210 | __HAL_LOCK(hopamp); |
||
| 1211 | |||
| 1212 | if(hopamp->State == HAL_OPAMP_STATE_READY) |
||
| 1213 | { |
||
| 1214 | switch (CallbackID) |
||
| 1215 | { |
||
| 1216 | case HAL_OPAMP_MSPINIT_CB_ID : |
||
| 1217 | hopamp->MspInitCallback = HAL_OPAMP_MspInit; |
||
| 1218 | break; |
||
| 1219 | case HAL_OPAMP_MSPDEINIT_CB_ID : |
||
| 1220 | hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit; |
||
| 1221 | break; |
||
| 1222 | case HAL_OPAMP_ALL_CB_ID : |
||
| 1223 | hopamp->MspInitCallback = HAL_OPAMP_MspInit; |
||
| 1224 | hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit; |
||
| 1225 | break; |
||
| 1226 | default : |
||
| 1227 | /* update return status */ |
||
| 1228 | status = HAL_ERROR; |
||
| 1229 | break; |
||
| 1230 | } |
||
| 1231 | } |
||
| 1232 | else if (hopamp->State == HAL_OPAMP_STATE_RESET) |
||
| 1233 | { |
||
| 1234 | switch (CallbackID) |
||
| 1235 | { |
||
| 1236 | case HAL_OPAMP_MSPINIT_CB_ID : |
||
| 1237 | hopamp->MspInitCallback = HAL_OPAMP_MspInit; |
||
| 1238 | break; |
||
| 1239 | case HAL_OPAMP_MSPDEINIT_CB_ID : |
||
| 1240 | hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit; |
||
| 1241 | break; |
||
| 1242 | default : |
||
| 1243 | /* update return status */ |
||
| 1244 | status = HAL_ERROR; |
||
| 1245 | break; |
||
| 1246 | } |
||
| 1247 | } |
||
| 1248 | else |
||
| 1249 | { |
||
| 1250 | /* update return status */ |
||
| 1251 | status = HAL_ERROR; |
||
| 1252 | } |
||
| 1253 | |||
| 1254 | /* Release Lock */ |
||
| 1255 | __HAL_UNLOCK(hopamp); |
||
| 1256 | return status; |
||
| 1257 | } |
||
| 1258 | |||
| 1259 | #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */ |
||
| 1260 | /** |
||
| 1261 | * @} |
||
| 1262 | */ |
||
| 1263 | |||
| 1264 | /** |
||
| 1265 | * @} |
||
| 1266 | */ |
||
| 1267 | |||
| 1268 | #endif /* STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX || STM32L162xC || STM32L152xC || STM32L151xC */ |
||
| 1269 | |||
| 1270 | #endif /* HAL_OPAMP_MODULE_ENABLED */ |
||
| 1271 | /** |
||
| 1272 | * @} |
||
| 1273 | */ |
||
| 1274 | |||
| 1275 | /** |
||
| 1276 | * @} |
||
| 1277 | */ |
||
| 1278 |