Files
@ 89a3a8b7b86f
Branch filter:
Location: therm/libraries/STM32L1xx_StdPeriph_Driver/src/stm32l1xx_opamp.c
89a3a8b7b86f
19.8 KiB
text/plain
updated config interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | /**
******************************************************************************
* @file stm32l1xx_opamp.c
* @author MCD Application Team
* @version V1.2.0
* @date 22-February-2013
* @brief This file provides firmware functions to manage the following
* functionalities of the operational amplifiers (opamp) peripheral:
* + Initialization and configuration
* + Calibration management
*
* @verbatim
==============================================================================
##### How to use this driver #####
==============================================================================
[..] The device integrates three independent rail-to-rail operational amplifiers
OPAMP1, OPAMP2 and OPAMP3:
(+) Internal connections to the ADC.
(+) Internal connections to the DAC.
(+) Internal connection to COMP1 (only OPAMP3).
(+) Internal connection for unity gain (voltage follower) configuration.
(+) Calibration capability.
(+) Selectable gain-bandwidth (2MHz in normal mode, 500KHz in low power mode).
[..]
(#) COMP AHB clock must be enabled to get write access
to OPAMP registers using
(#) RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP, ENABLE)
(#) Configure the corresponding GPIO to OPAMPx INP, OPAMPx_INN (if used)
and OPAMPx_OUT in analog mode.
(#) Configure (close/open) the OPAMP switches using OPAMP_SwitchCmd()
(#) Enable the OPAMP peripheral using OPAMP_Cmd()
-@- In order to use OPAMP outputs as ADC inputs, the opamps must be enabled
and the ADC must use the OPAMP output channel number:
(+@) OPAMP1 output is connected to ADC channel 3.
(+@) OPAMP2 output is connected to ADC channel 8.
(+@) OPAMP3 output is connected to ADC channel 13 (SW1 switch must be closed).
* @endverbatim
*
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32l1xx_opamp.h"
/** @addtogroup STM32L1xx_StdPeriph_Driver
* @{
*/
/** @defgroup OPAMP
* @brief OPAMP driver modules
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/** @defgroup OPAMP_Private_Functions
* @{
*/
/** @defgroup OPAMP_Group1 Initialization and configuration
* @brief Initialization and configuration
*
@verbatim
===============================================================================
##### Initialization and configuration #####
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Deinitialize the OPAMPs register to its default reset value.
* @note At startup, OTR and LPOTR registers are set to factory programmed values.
* @param None.
* @retval None.
*/
void OPAMP_DeInit(void)
{
/*!< Set OPAMP_CSR register to reset value */
OPAMP->CSR = 0x00010101;
/*!< Set OPAMP_OTR register to reset value */
OPAMP->OTR = (uint32_t)(* (uint32_t*)FLASH_R_BASE + 0x00000038);
/*!< Set OPAMP_LPOTR register to reset value */
OPAMP->LPOTR = (uint32_t)(* (uint32_t*)FLASH_R_BASE + 0x0000003C);
}
/**
* @brief Close or Open the OPAMP switches.
* @param OPAMP_OPAMPxSwitchy: selects the OPAMPx switch.
* This parameter can be any combinations of the following values:
* @arg OPAMP_OPAMP1Switch3: used to connect internally OPAMP1 output to
* OPAMP1 negative input (internal follower)
* @arg OPAMP_OPAMP1Switch4: used to connect PA2 to OPAMP1 negative input
* @arg OPAMP_OPAMP1Switch5: used to connect PA1 to OPAMP1 positive input
* @arg OPAMP_OPAMP1Switch6: used to connect DAC_OUT1 to OPAMP1 positive input
* @arg OPAMP_OPAMP1SwitchANA: used to meet 1 nA input leakage
* @arg OPAMP_OPAMP2Switch3: used to connect internally OPAMP2 output to
* OPAMP2 negative input (internal follower)
* @arg OPAMP_OPAMP2Switch4: used to connect PA7 to OPAMP2 negative input
* @arg OPAMP_OPAMP2Switch5: used to connect PA6 to OPAMP2 positive input
* @arg OPAMP_OPAMP2Switch6: used to connect DAC_OUT1 to OPAMP2 positive input
* @arg OPAMP_OPAMP2Switch7: used to connect DAC_OUT2 to OPAMP2 positive input
* @arg OPAMP_OPAMP2SwitchANA: used to meet 1 nA input leakage
* @arg OPAMP_OPAMP3Switch3: used to connect internally OPAMP3 output to
* OPAMP3 negative input (internal follower)
* @arg OPAMP_OPAMP3Switch4: used to connect PC2 to OPAMP3 negative input
* @arg OPAMP_OPAMP3Switch5: used to connect PC1 to OPAMP3 positive input
* @arg OPAMP_OPAMP3Switch6: used to connect DAC_OUT1 to OPAMP3 positive input
* @arg OPAMP_OPAMP3SwitchANA: used to meet 1 nA input leakage on negative input
*
* @param NewState: New state of the OPAMP switch.
* This parameter can be:
* ENABLE to close the OPAMP switch
* or DISABLE to open the OPAMP switch
* @note OPAMP_OPAMP2Switch6 and OPAMP_OPAMP2Switch7 mustn't be closed together.
* @retval None
*/
void OPAMP_SwitchCmd(uint32_t OPAMP_OPAMPxSwitchy, FunctionalState NewState)
{
/* Check the parameter */
assert_param(IS_OPAMP_SWITCH(OPAMP_OPAMPxSwitchy));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Close the selected switches */
OPAMP->CSR |= (uint32_t) OPAMP_OPAMPxSwitchy;
}
else
{
/* Open the selected switches */
OPAMP->CSR &= (~(uint32_t)OPAMP_OPAMPxSwitchy);
}
}
/**
* @brief Enable or disable the OPAMP peripheral.
* @param OPAMP_Selection: the selected OPAMP.
* This parameter can be one of the following values:
* @arg OPAMP_Selection_OPAMP1: OPAMP1 is selected
* @arg OPAMP_Selection_OPAMP2: OPAMP2 is selected
* @arg OPAMP_Selection_OPAMP3: OPAMP3 is selected
* @param NewState: new state of the selected OPAMP peripheral.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void OPAMP_Cmd(uint32_t OPAMP_Selection, FunctionalState NewState)
{
/* Check the parameter */
assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected OPAMP */
OPAMP->CSR &= (~(uint32_t) OPAMP_Selection);
}
else
{
/* Disable the selected OPAMP */
OPAMP->CSR |= (uint32_t) OPAMP_Selection;
}
}
/**
* @brief Enable or disable the low power mode for OPAMP peripheral.
* @param OPAMP_Selection: the selected OPAMP.
* This parameter can be one of the following values:
* @arg OPAMP_Selection_OPAMP1: OPAMP1 selected
* @arg OPAMP_Selection_OPAMP2: OPAMP2 selected
* @arg OPAMP_Selection_OPAMP3: OPAMP3 selected
* @param NewState: new low power state of the selected OPAMP peripheral.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void OPAMP_LowPowerCmd(uint32_t OPAMP_Selection, FunctionalState NewState)
{
/* Check the parameter */
assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Set the selected OPAMP in low power mode */
OPAMP->CSR |= (uint32_t) (OPAMP_Selection << 7);
}
else
{
/* Disable the low power mode for the selected OPAMP */
OPAMP->CSR &= (~(uint32_t) (OPAMP_Selection << 7));
}
}
/**
* @brief Select the OPAMP power range.
* @note The OPAMP power range selection must be performed while OPAMPs are powered down.
* @param OPAMP_Range: the selected OPAMP power range.
* This parameter can be one of the following values:
* @arg OPAMP_PowerRange_Low: Low power range is selected (VDDA is lower than 2.4V).
* @arg OPAMP_PowerRange_High: High power range is selected (VDDA is higher than 2.4V).
* @retval None
*/
void OPAMP_PowerRangeSelect(uint32_t OPAMP_PowerRange)
{
/* Check the parameter */
assert_param(IS_OPAMP_RANGE(OPAMP_PowerRange));
/* Reset the OPAMP range bit */
OPAMP->CSR &= (~(uint32_t) (OPAMP_CSR_AOP_RANGE));
/* Select the OPAMP power range */
OPAMP->CSR |= OPAMP_PowerRange;
}
/**
* @}
*/
/** @defgroup OPAMP_Group2 Calibration functions
* @brief Calibration functions
*
@verbatim
===============================================================================
##### Calibration functions #####
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Select the trimming mode.
* @param OffsetTrimming: the selected offset trimming mode.
* This parameter can be one of the following values:
* @arg OffsetTrimming_Factory: factory trimming values are used for offset
* calibration.
* @arg OffsetTrimming_User: user trimming values are used for offset
* calibration.
* @note When OffsetTrimming_User is selected, use OPAMP_OffsetTrimConfig()
* function or OPAMP_OffsetTrimLowPowerConfig() function to adjust
* trimming value.
* @retval None
*/
void OPAMP_OffsetTrimmingModeSelect(uint32_t OPAMP_Trimming)
{
/* Check the parameter */
assert_param(IS_OPAMP_TRIMMING(OPAMP_Trimming));
/* Reset the OPAMP_OTR range bit */
OPAMP->CSR &= (~(uint32_t) (OPAMP_OTR_OT_USER));
/* Select the OPAMP offset trimming */
OPAMP->CSR |= OPAMP_Trimming;
}
/**
* @brief Configure the trimming value of OPAMPs in normal mode.
* @param OPAMP_Selection: the selected OPAMP.
* This parameter can be one of the following values:
* @arg OPAMP_Selection_OPAMP1: OPAMP1 is selected to configure the trimming value.
* @arg OPAMP_Selection_OPAMP2: OPAMP2 is selected to configure the trimming value.
* @arg OPAMP_Selection_OPAMP3: OPAMP3 is selected to configure the trimming value.
* @param OPAMP_Input: the selected OPAMP input.
* This parameter can be one of the following values:
* @arg OPAMP_Input_NMOS: NMOS input is selected to configure the trimming value.
* @arg OPAMP_Input_PMOS: PMOS input is selected to configure the trimming value.
* @param OPAMP_TrimValue: the trimming value. This parameter can be any value lower
* or equal to 0x0000001F.
* @retval None
*/
void OPAMP_OffsetTrimConfig(uint32_t OPAMP_Selection, uint32_t OPAMP_Input, uint32_t OPAMP_TrimValue)
{
uint32_t tmpreg = 0;
/* Check the parameter */
assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection));
assert_param(IS_OPAMP_INPUT(OPAMP_Input));
assert_param(IS_OPAMP_TRIMMINGVALUE(OPAMP_TrimValue));
/* Get the OPAMP_OTR value */
tmpreg = OPAMP->OTR;
if(OPAMP_Selection == OPAMP_Selection_OPAMP1)
{
/* Reset the OPAMP inputs selection */
tmpreg &= (uint32_t)~(OPAMP_CSR_OPA1CAL_L | OPAMP_CSR_OPA1CAL_H);
/* Select the OPAMP input */
tmpreg |= OPAMP_Input;
if(OPAMP_Input == OPAMP_Input_PMOS)
{
/* Reset the trimming value corresponding to OPAMP1 PMOS input */
tmpreg &= (0xFFFFFFE0);
/* Set the new trimming value corresponding to OPAMP1 PMOS input */
tmpreg |= (OPAMP_TrimValue);
}
else
{
/* Reset the trimming value corresponding to OPAMP1 NMOS input */
tmpreg &= (0xFFFFFC1F);
/* Set the new trimming value corresponding to OPAMP1 NMOS input */
tmpreg |= (OPAMP_TrimValue<<5);
}
}
else if (OPAMP_Selection == OPAMP_Selection_OPAMP2)
{
/* Reset the OPAMP inputs selection */
tmpreg &= (uint32_t)~(OPAMP_CSR_OPA2CAL_L | OPAMP_CSR_OPA2CAL_H);
/* Select the OPAMP input */
tmpreg |= (uint32_t)(OPAMP_Input<<8);
if(OPAMP_Input == OPAMP_Input_PMOS)
{
/* Reset the trimming value corresponding to OPAMP2 PMOS input */
tmpreg &= (0xFFFF83FF);
/* Set the new trimming value corresponding to OPAMP2 PMOS input */
tmpreg |= (OPAMP_TrimValue<<10);
}
else
{
/* Reset the trimming value corresponding to OPAMP2 NMOS input */
tmpreg &= (0xFFF07FFF);
/* Set the new trimming value corresponding to OPAMP2 NMOS input */
tmpreg |= (OPAMP_TrimValue<<15);
}
}
else
{
/* Reset the OPAMP inputs selection */
tmpreg &= (uint32_t)~(OPAMP_CSR_OPA3CAL_L | OPAMP_CSR_OPA3CAL_H);
/* Select the OPAMP input */
tmpreg |= (uint32_t)(OPAMP_Input<<16);
if(OPAMP_Input == OPAMP_Input_PMOS)
{
/* Reset the trimming value corresponding to OPAMP3 PMOS input */
tmpreg &= (0xFE0FFFFF);
/* Set the new trimming value corresponding to OPAMP3 PMOS input */
tmpreg |= (OPAMP_TrimValue<<20);
}
else
{
/* Reset the trimming value corresponding to OPAMP3 NMOS input */
tmpreg &= (0xC1FFFFFF);
/* Set the new trimming value corresponding to OPAMP3 NMOS input */
tmpreg |= (OPAMP_TrimValue<<25);
}
}
/* Set the OPAMP_OTR register */
OPAMP->OTR = tmpreg;
}
/**
* @brief Configure the trimming value of OPAMPs in low power mode.
* @param OPAMP_Selection: the selected OPAMP.
* This parameter can be one of the following values:
* @arg OPAMP_Selection_OPAMP1: OPAMP1 is selected to configure the trimming value.
* @arg OPAMP_Selection_OPAMP2: OPAMP2 is selected to configure the trimming value.
* @arg OPAMP_Selection_OPAMP3: OPAMP3 is selected to configure the trimming value.
* @param OPAMP_Input: the selected OPAMP input.
* This parameter can be one of the following values:
* @arg OPAMP_Input_NMOS: NMOS input is selected to configure the trimming value.
* @arg OPAMP_Input_PMOS: PMOS input is selected to configure the trimming value.
* @param OPAMP_TrimValue: the trimming value.
* This parameter can be any value lower or equal to 0x0000001F.
* @retval None
*/
void OPAMP_OffsetTrimLowPowerConfig(uint32_t OPAMP_Selection, uint32_t OPAMP_Input, uint32_t OPAMP_TrimValue)
{
uint32_t tmpreg = 0;
/* Check the parameter */
assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection));
assert_param(IS_OPAMP_INPUT(OPAMP_Input));
assert_param(IS_OPAMP_TRIMMINGVALUE(OPAMP_TrimValue));
/* Get the OPAMP_LPOTR value */
tmpreg = OPAMP->LPOTR;
if(OPAMP_Selection == OPAMP_Selection_OPAMP1)
{
/* Reset the OPAMP inputs selection */
tmpreg &= (uint32_t)~(OPAMP_CSR_OPA1CAL_L | OPAMP_CSR_OPA1CAL_H);
/* Select the OPAMP input */
tmpreg |= OPAMP_Input;
if(OPAMP_Input == OPAMP_Input_PMOS)
{
/* Reset the trimming value corresponding to OPAMP1 PMOS input */
tmpreg &= (0xFFFFFFE0);
/* Set the new trimming value corresponding to OPAMP1 PMOS input */
tmpreg |= (OPAMP_TrimValue);
}
else
{
/* Reset the trimming value corresponding to OPAMP1 NMOS input */
tmpreg &= (0xFFFFFC1F);
/* Set the new trimming value corresponding to OPAMP1 NMOS input */
tmpreg |= (OPAMP_TrimValue<<5);
}
}
else if (OPAMP_Selection == OPAMP_Selection_OPAMP2)
{
/* Reset the OPAMP inputs selection */
tmpreg &= (uint32_t)~(OPAMP_CSR_OPA2CAL_L | OPAMP_CSR_OPA2CAL_H);
/* Select the OPAMP input */
tmpreg |= (uint32_t)(OPAMP_Input<<8);
if(OPAMP_Input == OPAMP_Input_PMOS)
{
/* Reset the trimming value corresponding to OPAMP2 PMOS input */
tmpreg &= (0xFFFF83FF);
/* Set the new trimming value corresponding to OPAMP2 PMOS input */
tmpreg |= (OPAMP_TrimValue<<10);
}
else
{
/* Reset the trimming value corresponding to OPAMP2 NMOS input */
tmpreg &= (0xFFF07FFF);
/* Set the new trimming value corresponding to OPAMP2 NMOS input */
tmpreg |= (OPAMP_TrimValue<<15);
}
}
else
{
/* Reset the OPAMP inputs selection */
tmpreg &= (uint32_t)~(OPAMP_CSR_OPA3CAL_L | OPAMP_CSR_OPA3CAL_H);
/* Select the OPAMP input */
tmpreg |= (uint32_t)(OPAMP_Input<<16);
if(OPAMP_Input == OPAMP_Input_PMOS)
{
/* Reset the trimming value corresponding to OPAMP3 PMOS input */
tmpreg &= (0xFE0FFFFF);
/* Set the new trimming value corresponding to OPAMP3 PMOS input */
tmpreg |= (OPAMP_TrimValue<<20);
}
else
{
/* Reset the trimming value corresponding to OPAMP3 NMOS input */
tmpreg &= (0xC1FFFFFF);
/* Set the new trimming value corresponding to OPAMP3 NMOS input */
tmpreg |= (OPAMP_TrimValue<<25);
}
}
/* Set the OPAMP_LPOTR register */
OPAMP->LPOTR = tmpreg;
}
/**
* @brief Checks whether the specified OPAMP calibration flag is set or not.
* @note User should wait until calibration flag change the value when changing
* the trimming value.
* @param OPAMP_Selection: the selected OPAMP.
* This parameter can be one of the following values:
* @arg OPAMP_Selection_OPAMP1: OPAMP1 is selected.
* @arg OPAMP_Selection_OPAMP2: OPAMP2 is selected.
* @arg OPAMP_Selection_OPAMP3: OPAMP3 is selected.
* @retval The new state of the OPAMP calibration flag (SET or RESET).
*/
FlagStatus OPAMP_GetFlagStatus(uint32_t OPAMP_Selection)
{
FlagStatus bitstatus = RESET;
uint32_t tmpreg = 0;
/* Check the parameter */
assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection));
/* Get the CSR register value */
tmpreg = OPAMP->CSR;
/* Check if OPAMP1 is selected */
if(OPAMP_Selection == OPAMP_Selection_OPAMP1)
{
/* Check OPAMP1 CAL bit status */
if ((tmpreg & OPAMP_CSR_OPA1CALOUT) != (uint32_t)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
}
/* Check if OPAMP2 is selected */
else if(OPAMP_Selection == OPAMP_Selection_OPAMP2)
{
/* Check OPAMP2 CAL bit status */
if ((tmpreg & OPAMP_CSR_OPA2CALOUT) != (uint32_t)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
}
else
{
/* Check OPAMP3 CAL bit status */
if ((tmpreg & OPAMP_CSR_OPA3CALOUT) != (uint32_t)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
}
return bitstatus;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|