2 ******************************************************************************
3 * @file stm32f30x_opamp.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the operational amplifiers (OPAMP1,...OPAMP4) peripheral:
9 * + OPAMP Configuration
14 ==============================================================================
15 ##### OPAMP Peripheral Features #####
16 ==============================================================================
19 The device integrates 4 operational amplifiers OPAMP1, OPAMP2, OPAMP3 and OPAMP4:
21 (+) The OPAMPs non inverting input can be selected among the list shown by
24 (+) The OPAMPs inverting input can be selected among the list shown by
27 (+) The OPAMPs outputs can be internally connected to the inverting input
29 (+) The OPAMPs outputs can be internally connected to resistor feedback
30 output (Programmable Gain Amplifier mode)
32 (+) The OPAMPs outputs can be internally connected to ADC
34 (+) The OPAMPs can be calibrated to compensate the offset compensation
36 (+) Timer-controlled Mux for automatic switch of inverting and
39 OPAMPs inverting/non-inverting inputs:
40 +--------------------------------------------------------------+
41 | | | OPAMP1 | OPAMP2 | OPAMP3 | OPAMP4 |
42 |-----------------|--------|--------|--------|--------|--------|
43 | | PGA | OK | OK | OK | OK |
44 | Inverting Input | Vout | OK | OK | OK | OK |
45 | | IO1 | PC5 | PC5 | PB10 | PB10 |
46 | | IO2 | PA3 | PA5 | PB2 | PD8 |
47 |-----------------|--------|--------|--------|--------|--------|
48 | | IO1 | PA7 | PD14 | PB13 | PD11 |
49 | Non Inverting | IO2 | PA5 | PB14 | PA5 | PB11 |
50 | Input | IO3 | PA3 | PB0 | PA1 | PA4 |
51 | | IO4 | PA1 | PA7 | PB0 | PB13 |
52 +--------------------------------------------------------------+
54 ##### How to use this driver #####
55 ==============================================================================
57 This driver provides functions to configure and program the OPAMP
58 of all STM32F30x devices.
60 To use the OPAMP, perform the following steps:
62 (#) Enable the SYSCFG APB clock to get write access to OPAMP
63 register using RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
65 (#) Configure the OPAMP input in analog mode using GPIO_Init()
67 (#) Configure the OPAMP using OPAMP_Init() function:
68 (++) Select the inverting input
69 (++) Select the non-inverting inverting input
71 (#) Enable the OPAMP using OPAMP_Cmd() function
75 ******************************************************************************
78 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2>
80 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
81 * You may not use this file except in compliance with the License.
82 * You may obtain a copy of the License at:
84 * http://www.st.com/software_license_agreement_liberty_v2
86 * Unless required by applicable law or agreed to in writing, software
87 * distributed under the License is distributed on an "AS IS" BASIS,
88 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
89 * See the License for the specific language governing permissions and
90 * limitations under the License.
92 ******************************************************************************
95 /* Includes ------------------------------------------------------------------*/
96 #include "stm32f30x_opamp.h"
98 /** @addtogroup STM32F30x_StdPeriph_Driver
103 * @brief OPAMP driver modules
107 /* Private typedef -----------------------------------------------------------*/
108 /* Private define ------------------------------------------------------------*/
109 #define OPAMP_CSR_DEFAULT_MASK ((uint32_t)0xFFFFFF93)
110 #define OPAMP_CSR_TIMERMUX_MASK ((uint32_t)0xFFFFF8FF)
111 #define OPAMP_CSR_TRIMMING_MASK ((uint32_t)0x0000001F)
113 /* Private macro -------------------------------------------------------------*/
114 /* Private variables ---------------------------------------------------------*/
115 /* Private function prototypes -----------------------------------------------*/
116 /* Private functions ---------------------------------------------------------*/
118 /** @defgroup OPAMP_Private_Functions
122 /** @defgroup OPAMP_Group1 Initialization and Configuration functions
123 * @brief Initialization and Configuration functions
126 ===============================================================================
127 ##### Initialization and Configuration functions #####
128 ===============================================================================
135 * @brief Deinitializes OPAMP peripheral registers to their default reset values.
136 * @note Deinitialization can't be performed if the OPAMP configuration is locked.
137 * To unlock the configuration, perform a system reset.
138 * @param OPAMP_Selection: the selected OPAMP.
139 * This parameter can be OPAMP_Selection_OPAMPx where x can be 1 to 4
140 * to select the OPAMP peripheral.
144 void OPAMP_DeInit(uint32_t OPAMP_Selection
)
146 /*!< Set OPAMP_CSR register to reset value */
147 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) = ((uint32_t)0x00000000);
151 * @brief Initializes the OPAMP peripheral according to the specified parameters
152 * in OPAMP_InitStruct
153 * @note If the selected OPAMP is locked, initialization can't be performed.
154 * To unlock the configuration, perform a system reset.
155 * @param OPAMP_Selection: the selected OPAMP.
156 * This parameter can be OPAMP_Selection_OPAMPx where x can be 1 to 4
157 * to select the OPAMP peripheral.
158 * @param OPAMP_InitStruct: pointer to an OPAMP_InitTypeDef structure that contains
159 * the configuration information for the specified OPAMP peripheral.
160 * - OPAMP_InvertingInput specifies the inverting input of OPAMP
161 * - OPAMP_NonInvertingInput specifies the non inverting input of OPAMP
164 void OPAMP_Init(uint32_t OPAMP_Selection
, OPAMP_InitTypeDef
* OPAMP_InitStruct
)
168 /* Check the parameters */
169 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
170 assert_param(IS_OPAMP_INVERTING_INPUT(OPAMP_InitStruct
->OPAMP_InvertingInput
));
171 assert_param(IS_OPAMP_NONINVERTING_INPUT(OPAMP_InitStruct
->OPAMP_NonInvertingInput
));
173 /*!< Get the OPAMPx_CSR register value */
174 tmpreg
= *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
);
176 /*!< Clear the inverting and non inverting bits selection bits */
177 tmpreg
&= (uint32_t) (OPAMP_CSR_DEFAULT_MASK
);
179 /*!< Configure OPAMP: inverting and non inverting inputs */
180 tmpreg
|= (uint32_t)(OPAMP_InitStruct
->OPAMP_InvertingInput
| OPAMP_InitStruct
->OPAMP_NonInvertingInput
);
182 /*!< Write to OPAMPx_CSR register */
183 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) = tmpreg
;
187 * @brief Fills each OPAMP_InitStruct member with its default value.
188 * @param OPAMP_InitStruct: pointer to an OPAMP_InitTypeDef structure which will
192 void OPAMP_StructInit(OPAMP_InitTypeDef
* OPAMP_InitStruct
)
194 OPAMP_InitStruct
->OPAMP_NonInvertingInput
= OPAMP_NonInvertingInput_IO1
;
195 OPAMP_InitStruct
->OPAMP_InvertingInput
= OPAMP_InvertingInput_IO1
;
199 * @brief Configure the feedback resistor gain.
200 * @note If the selected OPAMP is locked, gain configuration can't be performed.
201 * To unlock the configuration, perform a system reset.
202 * @param OPAMP_Selection: the selected OPAMP.
203 * This parameter can be OPAMP_Selection_OPAMPx where x can be 1 to 4
204 * to select the OPAMP peripheral.
205 * @param NewState: new state of the OPAMP peripheral.
206 * This parameter can be: ENABLE or DISABLE.
209 void OPAMP_PGAConfig(uint32_t OPAMP_Selection
, uint32_t OPAMP_PGAGain
, uint32_t OPAMP_PGAConnect
)
211 /* Check the parameters */
212 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
213 assert_param(IS_OPAMP_PGAGAIN(OPAMP_PGAGain
));
214 assert_param(IS_OPAMP_PGACONNECT(OPAMP_PGAConnect
));
216 /* Reset the configuration bits */
217 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) &= (uint32_t)(~OPAMP_CSR_PGGAIN
);
219 /* Set the new configuration */
220 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) |= (uint32_t) (OPAMP_PGAGain
| OPAMP_PGAConnect
);
224 * @brief Configure the OPAMP's internal reference.
225 * @note This feature is used when calibration enabled or OPAMP's reference
226 * connected to the non inverting input.
227 * @note If the selected OPAMP is locked, Vref configuration can't be performed.
228 * To unlock the configuration, perform a system reset.
229 * @param OPAMP_Selection: the selected OPAMP.
230 * This parameter can be OPAMP_Selection_OPAMPx where x can be 1 to 4
231 * to select the OPAMP peripheral.
232 * @param OPAMP_Vref: This parameter can be:
233 * OPAMP_Vref_3VDDA: OPMAP Vref = 3.3% VDDA
234 * OPAMP_Vref_10VDDA: OPMAP Vref = 10% VDDA
235 * OPAMP_Vref_50VDDA: OPMAP Vref = 50% VDDA
236 * OPAMP_Vref_90VDDA: OPMAP Vref = 90% VDDA
239 void OPAMP_VrefConfig(uint32_t OPAMP_Selection
, uint32_t OPAMP_Vref
)
243 /* Check the parameters */
244 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
245 assert_param(IS_OPAMP_VREF(OPAMP_Vref
));
247 /*!< Get the OPAMPx_CSR register value */
248 tmpreg
= *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
);
250 /*!< Clear the CALSEL bits */
251 tmpreg
&= (uint32_t) (~OPAMP_CSR_CALSEL
);
253 /*!< Configure OPAMP reference */
254 tmpreg
|= (uint32_t)(OPAMP_Vref
);
256 /*!< Write to OPAMPx_CSR register */
257 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) = tmpreg
;
261 * @brief Connnect the internal reference to the OPAMP's non inverting input.
262 * @note If the selected OPAMP is locked, Vref configuration can't be performed.
263 * To unlock the configuration, perform a system reset.
264 * @param OPAMP_Selection: the selected OPAMP.
265 * This parameter can be OPAMP_Selection_OPAMPx where x can be 1 to 4
266 * to select the OPAMP peripheral.
267 * @param NewState: new state of the OPAMP peripheral.
268 * This parameter can be: ENABLE or DISABLE.
271 void OPAMP_VrefConnectNonInvertingInput(uint32_t OPAMP_Selection
, FunctionalState NewState
)
273 /* Check the parameters */
274 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
275 assert_param(IS_FUNCTIONAL_STATE(NewState
));
277 if (NewState
!= DISABLE
)
279 /* Connnect the internal reference to the OPAMP's non inverting input */
280 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) |= (uint32_t) (OPAMP_CSR_FORCEVP
);
284 /* Disconnnect the internal reference to the OPAMP's non inverting input */
285 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) &= (uint32_t)(~OPAMP_CSR_FORCEVP
);
290 * @brief Enables or disables connecting the OPAMP's internal reference to ADC.
291 * @note If the selected OPAMP is locked, Vref connection can't be performed.
292 * To unlock the configuration, perform a system reset.
293 * @param NewState: new state of the Vrefint output.
294 * This parameter can be: ENABLE or DISABLE.
297 void OPAMP_VrefConnectADCCmd(uint32_t OPAMP_Selection
, FunctionalState NewState
)
299 /* Check the parameters */
300 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
301 assert_param(IS_FUNCTIONAL_STATE(NewState
));
303 if (NewState
!= DISABLE
)
305 /* Enable output internal reference */
306 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) |= (uint32_t) (OPAMP_CSR_TSTREF
);
310 /* Disable output internal reference */
311 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) &= (uint32_t)(~OPAMP_CSR_TSTREF
);
316 * @brief Configure the OPAMP peripheral (secondary inputs) for timer-controlled
317 * mux mode according to the specified parameters in OPAMP_InitStruct.
318 * @note If the selected OPAMP is locked, timer-controlled mux configuration
319 * can't be performed.
320 * To unlock the configuration, perform a system reset.
321 * @param OPAMP_Selection: the selected OPAMP.
322 * This parameter can be OPAMP_Selection_OPAMPx where x can be 1 to 4
323 * to select the OPAMP peripheral.
324 * @param OPAMP_InitStruct: pointer to an OPAMP_InitTypeDef structure that contains
325 * the configuration information for the specified OPAMP peripheral.
326 * - OPAMP_InvertingInput specifies the inverting input of OPAMP
327 * - OPAMP_NonInvertingInput specifies the non inverting input of OPAMP
328 * @note PGA and Vout can't be selected as seconadry inverting input.
331 void OPAMP_TimerControlledMuxConfig(uint32_t OPAMP_Selection
, OPAMP_InitTypeDef
* OPAMP_InitStruct
)
335 /* Check the parameters */
336 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
337 assert_param(IS_OPAMP_SECONDARY_INVINPUT(OPAMP_InitStruct
->OPAMP_InvertingInput
));
338 assert_param(IS_OPAMP_NONINVERTING_INPUT(OPAMP_InitStruct
->OPAMP_NonInvertingInput
));
340 /*!< Get the OPAMPx_CSR register value */
341 tmpreg
= *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
);
343 /*!< Clear the secondary inverting bit, secondary non inverting bit and TCMEN bits */
344 tmpreg
&= (uint32_t) (OPAMP_CSR_TIMERMUX_MASK
);
346 /*!< Configure OPAMP: secondary inverting and non inverting inputs */
347 tmpreg
|= (uint32_t)((uint32_t)(OPAMP_InitStruct
->OPAMP_InvertingInput
<<3) | (uint32_t)(OPAMP_InitStruct
->OPAMP_NonInvertingInput
<<7));
349 /*!< Write to OPAMPx_CSR register */
350 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) = tmpreg
;
354 * @brief Enable or disable the timer-controlled mux mode.
355 * @note If the selected OPAMP is locked, enable/disable can't be performed.
356 * To unlock the configuration, perform a system reset.
357 * @param OPAMP_Selection: the selected OPAMP.
358 * This parameter can be OPAMP_Selection_OPAMPx where x can be 1 to 4
359 * to select the OPAMP peripheral.
360 * @param NewState: new state of the OPAMP peripheral.
361 * This parameter can be: ENABLE or DISABLE.
364 void OPAMP_TimerControlledMuxCmd(uint32_t OPAMP_Selection
, FunctionalState NewState
)
366 /* Check the parameters */
367 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
368 assert_param(IS_FUNCTIONAL_STATE(NewState
));
370 if (NewState
!= DISABLE
)
372 /* Enable the timer-controlled Mux mode */
373 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) |= (uint32_t) (OPAMP_CSR_TCMEN
);
377 /* Disable the timer-controlled Mux mode */
378 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) &= (uint32_t)(~OPAMP_CSR_TCMEN
);
383 * @brief Enable or disable the OPAMP peripheral.
384 * @note If the selected OPAMP is locked, enable/disable can't be performed.
385 * To unlock the configuration, perform a system reset.
386 * @param OPAMP_Selection: the selected OPAMP.
387 * This parameter can be OPAMP_Selection_OPAMPx where x can be 1 to 4
388 * to select the OPAMP peripheral.
389 * @param NewState: new state of the OPAMP peripheral.
390 * This parameter can be: ENABLE or DISABLE.
393 void OPAMP_Cmd(uint32_t OPAMP_Selection
, FunctionalState NewState
)
395 /* Check the parameters */
396 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
397 assert_param(IS_FUNCTIONAL_STATE(NewState
));
399 if (NewState
!= DISABLE
)
401 /* Enable the selected OPAMPx peripheral */
402 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) |= (uint32_t) (OPAMP_CSR_OPAMPxEN
);
406 /* Disable the selected OPAMPx peripheral */
407 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) &= (uint32_t)(~OPAMP_CSR_OPAMPxEN
);
412 * @brief Return the output level (high or low) during calibration of the selected OPAMP.
413 * @param OPAMP_Selection: the selected OPAMP.
414 * This parameter can be OPAMP_Selection_OPAMPx where x can be 1 to 4
415 * to select the OPAMP peripheral.
416 * - OPAMP output is low when the non-inverting input is at a lower
417 * voltage than the inverting input
418 * - OPAMP output is high when the non-inverting input is at a higher
419 * voltage than the inverting input
420 * @note OPAMP ouput level is provided only during calibration phase.
421 * @retval Returns the selected OPAMP output level: low or high.
424 uint32_t OPAMP_GetOutputLevel(uint32_t OPAMP_Selection
)
426 uint32_t opampout
= 0x0;
428 /* Check the parameters */
429 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
431 /* Check if selected OPAMP output is high */
432 if ((*(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) & (OPAMP_CSR_OUTCAL
)) != 0)
434 opampout
= OPAMP_OutputLevel_High
;
438 opampout
= OPAMP_OutputLevel_Low
;
441 /* Return the OPAMP output level */
442 return (uint32_t)(opampout
);
446 * @brief Select the trimming mode.
447 * @param OffsetTrimming: the selected offset trimming mode.
448 * This parameter can be one of the following values:
449 * @arg OPAMP_Trimming_Factory: factory trimming values are used for offset
451 * @arg OPAMP_Trimming_User: user trimming values are used for offset
453 * @note When OffsetTrimming_User is selected, use OPAMP_OffsetTrimConfig()
454 * function or OPAMP_OffsetTrimLowPowerConfig() function to adjust
458 void OPAMP_OffsetTrimModeSelect(uint32_t OPAMP_Selection
, uint32_t OPAMP_Trimming
)
460 /* Check the parameters */
461 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
462 assert_param(IS_OPAMP_TRIMMING(OPAMP_Trimming
));
464 /* Reset USERTRIM bit */
465 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) &= (~(uint32_t) (OPAMP_CSR_USERTRIM
));
467 /* Select trimming mode */
468 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) |= OPAMP_Trimming
;
472 * @brief Configure the trimming value of the OPAMP.
473 * @param OPAMP_Selection: the selected OPAMP.
474 * This parameter can be OPAMP_Selection_OPAMPx where x can be 1 to 4
475 * to select the OPAMP peripheral.
476 * @param OPAMP_Input: the selected OPAMP input.
477 * This parameter can be one of the following values:
478 * @arg OPAMP_Input_Inverting: Inverting input is selected to configure the trimming value
479 * @arg OPAMP_Input_NonInverting: Non inverting input is selected to configure the trimming value
480 * @param OPAMP_TrimValue: the trimming value. This parameter can be any value lower
481 * or equal to 0x0000001F.
484 void OPAMP_OffsetTrimConfig(uint32_t OPAMP_Selection
, uint32_t OPAMP_Input
, uint32_t OPAMP_TrimValue
)
488 /* Check the parameters */
489 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
490 assert_param(IS_OPAMP_INPUT(OPAMP_Input
));
491 assert_param(IS_OPAMP_TRIMMINGVALUE(OPAMP_TrimValue
));
493 /*!< Get the OPAMPx_CSR register value */
494 tmpreg
= *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
);
496 /*!< Clear the trimming bits */
497 tmpreg
&= ((uint32_t)~(OPAMP_CSR_TRIMMING_MASK
<<OPAMP_Input
));
499 /*!< Configure the new trimming value */
500 tmpreg
|= (uint32_t)(OPAMP_TrimValue
<<OPAMP_Input
);
502 /*!< Write to OPAMPx_CSR register */
503 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) = tmpreg
;
507 * @brief Start or stop the calibration of selected OPAMP peripheral.
508 * @note If the selected OPAMP is locked, start/stop can't be performed.
509 * To unlock the configuration, perform a system reset.
510 * @param OPAMP_Selection: the selected OPAMP.
511 * This parameter can be OPAMP_Selection_OPAMPx where x can be 1 to 4
512 * to select the OPAMP peripheral.
513 * @param NewState: new state of the OPAMP peripheral.
514 * This parameter can be: ENABLE or DISABLE.
517 void OPAMP_StartCalibration(uint32_t OPAMP_Selection
, FunctionalState NewState
)
519 /* Check the parameters */
520 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
521 assert_param(IS_FUNCTIONAL_STATE(NewState
));
523 if (NewState
!= DISABLE
)
525 /* Start the OPAMPx calibration */
526 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) |= (uint32_t) (OPAMP_CSR_CALON
);
530 /* Stop the OPAMPx calibration */
531 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) &= (uint32_t)(~OPAMP_CSR_CALON
);
539 /** @defgroup OPAMP_Group2 OPAMP configuration locking function
540 * @brief OPAMP1,...OPAMP4 configuration locking function
541 * OPAMP1,...OPAMP4 configuration can be locked each separately.
542 * Unlocking is performed by system reset.
545 ===============================================================================
546 ##### Configuration Lock function #####
547 ===============================================================================
554 * @brief Lock the selected OPAMP configuration.
555 * @note Locking the configuration means that all control bits are read-only.
556 * To unlock the OPAMP configuration, perform a system reset.
557 * @param OPAMP_Selection: the selected OPAMP.
558 * This parameter can be OPAMP_Selection_OPAMPx where x can be 1 to 4
559 * to select the OPAMP peripheral.
562 void OPAMP_LockConfig(uint32_t OPAMP_Selection
)
564 /* Check the parameter */
565 assert_param(IS_OPAMP_ALL_PERIPH(OPAMP_Selection
));
567 /* Set the lock bit corresponding to selected OPAMP */
568 *(__IO
uint32_t *) (OPAMP_BASE
+ OPAMP_Selection
) |= (uint32_t) (OPAMP_CSR_LOCK
);
575 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/