1 /**
2 ******************************************************************************
3 * @file stm32f37x_adc.c
4 * @author MCD Application Team
5 * @version V1.0.0
6 * @date 20-September-2012
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Analog to Digital Converter (ADC) peripheral:
9 * + Initialization and Configuration
10 * + Analog Watchdog configuration
11 * + Temperature Sensor, Vrefint (Internal Reference Voltage)
12 * and VBAT (Voltage battery) management
13 * + Regular Channels Configuration
14 * + Regular Channels DMA Configuration
15 * + Injected channels Configuration
16 * + Interrupts and flags management
17 *
18 * @verbatim
19 ================================================================================
20 ##### How to use this driver #####
21 ================================================================================
22 [..]
23 (#) Enable the ADC interface clock using
24 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
25 (#) ADC pins configuration
26 (++) Enable the clock for the ADC GPIOs using the following function:
27 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOx, ENABLE);
28 (++) Configure these ADC pins in analog mode using GPIO_Init();
29 (#) Configure the data alignment using the ADC_Init() function.
30 (#) Activate the ADC peripheral using ADC_Cmd() function.
31
32 *** Regular channels group configuration ***
33 ============================================
34 [..]
35 (+) To configure the ADC regular channels group features, use
36 ADC_Init() and ADC_RegularChannelConfig() functions.
37 (+) To activate the continuous mode, use the ADC_ContinuousModeCmd()
38 function.
39 (+) To configure and activate the Discontinuous mode, use the
40 ADC_DiscModeChannelCountConfig() and ADC_DiscModeCmd() functions.
41 (+) To read the ADC converted values, use the ADC_GetConversionValue()
42 function.
43
44 *** DMA for Regular channels group features configuration ***
45 =============================================================
46 [..]
47 (+) To enable the DMA mode for regular channels group, use the
48 ADC_DMACmd() function.
49
50 *** Injected channels group configuration ***
51 =============================================
52 [..]
53 (+) To configure the ADC Injected channels group features, use
54 ADC_InjectedChannelConfig() function.
55 (+) To activate the Injected Discontinuous mode, use the
56 ADC_InjectedDiscModeCmd() function.
57 (+) To activate the AutoInjected mode, use the ADC_AutoInjectedConvCmd()
58 function.
59 (+) To read the ADC converted values, use the ADC_GetInjectedConversionValue() function.
60
61 * @endverbatim
62 *
63 ******************************************************************************
64 * @attention
65 *
66 * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
67 *
68 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
69 * You may not use this file except in compliance with the License.
70 * You may obtain a copy of the License at:
71 *
72 * http://www.st.com/software_license_agreement_liberty_v2
73 *
74 * Unless required by applicable law or agreed to in writing, software
75 * distributed under the License is distributed on an "AS IS" BASIS,
76 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
77 * See the License for the specific language governing permissions and
78 * limitations under the License.
79 *
80 ******************************************************************************
81 */
82
83 /* Includes ------------------------------------------------------------------*/
84 #include "stm32f37x_adc.h"
85 #include "stm32f37x_rcc.h"
86
87 /** @addtogroup STM32F37x_StdPeriph_Driver
88 * @{
89 */
90
91 /** @defgroup ADC
92 * @brief ADC driver modules
93 * @{
94 */
95
96 /* Private typedef -----------------------------------------------------------*/
97 /* Private define ------------------------------------------------------------*/
98 /* CR2 register Mask */
99 #define ADC_CR2_CLEAR_MASK ((uint32_t)0xFFF1F7FD)
100 /* ADC SQRx mask */
101 #define ADC_SQR_SQ_SET ((uint32_t)0x0000001F)
102 /* ADC JSQRx mask */
103 #define ADC_JSQR_JSQ_SET ((uint32_t)0x0000001F)
104 /* ADC SMPRx mask */
105 #define ADC_SMPR_SMP_SET ((uint32_t)0x00000007)
106 /* ADC JDRx registers offset */
107 #define ADC_JDR_OFFSET ((uint8_t)0x28)
108
109 /* Private macro -------------------------------------------------------------*/
110 /* Private variables ---------------------------------------------------------*/
111 /* Private function prototypes -----------------------------------------------*/
112 /* Private functions ---------------------------------------------------------*/
113
114 /** @defgroup ADC_Private_Functions
115 * @{
116 */
117
118 /** @defgroup ADC_Group1 Initialization and Configuration functions
119 * @brief Initialization and Configuration functions
120 *
121 @verbatim
122 ===============================================================================
123 ##### Initialization and Configuration functions #####
124 ===============================================================================
125 [..] This section provides functions allowing to:
126 (+) Scan Conversion Mode (multichannels or one channel) for regular group
127 (+) ADC Continuous Conversion Mode (Continuous or Single conversion) for
128 regular group
129 (+) External trigger Edge and source of regular group,
130 (+) Converted data alignment (left or right)
131 (+) The number of ADC conversions that will be done using the sequencer
132 for regular channel group
133 (+) Enable or disable the ADC peripheral
134 (+) Start/Reset the calibration
135
136 @endverbatim
137 * @{
138 */
139
140 /**
141 * @brief Deinitializes the ADCx peripheral registers to their default reset values.
142 * @param ADCx: where x can be 1 to select the ADC peripheral.
143 * @retval None
144 */
ADC_DeInit(ADC_TypeDef * ADCx)145 void ADC_DeInit(ADC_TypeDef* ADCx)
146 {
147 /* Check the parameters */
148 assert_param(IS_ADC_ALL_PERIPH(ADCx));
149
150 /* Enable ADC1 reset state */
151 RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
152 /* Release ADC1 from reset state */
153 RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);
154 }
155
156 /**
157 * @brief Initializes the ADCx peripheral according to the specified parameters
158 * in the ADC_InitStruct.
159 * @param ADCx: where x can be 1 to select the ADC peripheral.
160 * @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure that contains
161 * the configuration information for the specified ADC peripheral.
162 * @retval None
163 */
ADC_Init(ADC_TypeDef * ADCx,ADC_InitTypeDef * ADC_InitStruct)164 void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
165 {
166 uint32_t tmpreg1 = 0;
167 uint8_t tmpreg2 = 0;
168 /* Check the parameters */
169 assert_param(IS_ADC_ALL_PERIPH(ADCx));
170 assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode));
171 assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode));
172 assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv));
173 assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign));
174 assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfChannel));
175
176 /*---------------------------- ADCx CR1 Configuration -----------------*/
177 /* Get the ADCx CR1 value */
178 tmpreg1 = ADCx->CR1;
179 /* Clear SCAN bit */
180 tmpreg1 &= (uint32_t)(~ADC_CR1_SCAN);
181 /* Configure ADCx: scan conversion mode */
182 /* Set SCAN bit according to ADC_ScanConvMode value */
183 tmpreg1 |= (uint32_t)((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8);
184 /* Write to ADCx CR1 */
185 ADCx->CR1 = tmpreg1;
186
187 /*---------------------------- ADCx CR2 Configuration -----------------*/
188 /* Get the ADCx CR2 value */
189 tmpreg1 = ADCx->CR2;
190 /* Clear CONT, ALIGN and EXTSEL bits */
191 tmpreg1 &= ADC_CR2_CLEAR_MASK;
192 /* Configure ADCx: external trigger event and continuous conversion mode */
193 /* Set ALIGN bit according to ADC_DataAlign value */
194 /* Set EXTSEL bits according to ADC_ExternalTrigConv value */
195 /* Set CONT bit according to ADC_ContinuousConvMode value */
196 tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ExternalTrigConv |
197 ((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1));
198 /* Write to ADCx CR2 */
199 ADCx->CR2 = tmpreg1;
200
201 /*---------------------------- ADCx SQR1 Configuration -----------------*/
202 /* Get the ADCx SQR1 value */
203 tmpreg1 = ADCx->SQR1;
204 /* Clear L bits */
205 tmpreg1 &= (uint32_t) (~ADC_SQR1_L);
206 /* Configure ADCx: regular channel sequence length */
207 /* Set L bits according to ADC_NbrOfChannel value */
208 tmpreg2 |= (uint8_t) (ADC_InitStruct->ADC_NbrOfChannel - (uint8_t)1);
209 tmpreg1 |= (uint32_t)tmpreg2 << 20;
210 /* Write to ADCx SQR1 */
211 ADCx->SQR1 = tmpreg1;
212 }
213
214 /**
215 * @brief Fills each ADC_InitStruct member with its default value.
216 * @param ADC_InitStruct : pointer to an ADC_InitTypeDef structure which will be initialized.
217 * @retval None
218 */
ADC_StructInit(ADC_InitTypeDef * ADC_InitStruct)219 void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
220 {
221 /* Reset ADC init structure parameters values */
222 /* initialize the ADC_ScanConvMode member */
223 ADC_InitStruct->ADC_ScanConvMode = DISABLE;
224 /* Initialize the ADC_ContinuousConvMode member */
225 ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;
226 /* Initialize the ADC_ExternalTrigConv member */
227 ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T19_TRGO;
228 /* Initialize the ADC_DataAlign member */
229 ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;
230 /* Initialize the ADC_NbrOfChannel member */
231 ADC_InitStruct->ADC_NbrOfChannel = 1;
232 }
233
234 /**
235 * @brief Enables or disables the specified ADC peripheral.
236 * @param ADCx: where x can be 1 to select the ADC peripheral.
237 * @param NewState: new state of the ADCx peripheral.
238 * This parameter can be: ENABLE or DISABLE.
239 * @retval None
240 */
ADC_Cmd(ADC_TypeDef * ADCx,FunctionalState NewState)241 void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
242 {
243 /* Check the parameters */
244 assert_param(IS_ADC_ALL_PERIPH(ADCx));
245 assert_param(IS_FUNCTIONAL_STATE(NewState));
246 if (NewState != DISABLE)
247 {
248 /* Set the ADON bit to wake up the ADC from power down mode */
249 ADCx->CR2 |= ADC_CR2_ADON;
250 }
251 else
252 {
253 /* Disable the selected ADC peripheral */
254 ADCx->CR2 &= (uint32_t)(~ADC_CR2_ADON);
255 }
256 }
257
258 /**
259 * @brief Starts the selected ADC calibration process.
260 * @param ADCx: where x can be 1 to select the ADC peripheral.
261 * @retval None
262 */
ADC_StartCalibration(ADC_TypeDef * ADCx)263 void ADC_StartCalibration(ADC_TypeDef* ADCx)
264 {
265 /* Check the parameters */
266 assert_param(IS_ADC_ALL_PERIPH(ADCx));
267 /* Enable the selected ADC calibration process */
268 ADCx->CR2 |= ADC_CR2_CAL;
269 }
270
271 /**
272 * @brief Resets the selected ADC calibration registers.
273 * @param ADCx: where x can be 1 to select the ADC peripheral.
274 * @retval None
275 */
ADC_ResetCalibration(ADC_TypeDef * ADCx)276 void ADC_ResetCalibration(ADC_TypeDef* ADCx)
277 {
278 /* Check the parameters */
279 assert_param(IS_ADC_ALL_PERIPH(ADCx));
280 /* Resets the selected ADC calibration registers */
281 ADCx->CR2 |= ADC_CR2_RSTCAL;
282 }
283
284 /**
285 * @}
286 */
287
288 /** @defgroup ADC_Group2 Analog Watchdog configuration functions
289 * @brief Analog Watchdog configuration functions
290 *
291 @verbatim
292 ===============================================================================
293 ##### Analog Watchdog configuration functions #####
294 ===============================================================================
295
296 [..] This section provides functions allowing to configure the Analog Watchdog
297 (AWD) feature in the ADC.
298
299 [..] A typical configuration Analog Watchdog is done following these steps :
300 (#) The ADC guarded channel(s) is (are) selected using the
301 ADC_AnalogWatchdogSingleChannelConfig() function.
302 (#) The Analog watchdog lower and higher threshold are configured using
303 the ADC_AnalogWatchdogThresholdsConfig() function.
304 (#) The Analog watchdog is enabled and configured to enable the check,
305 on one or more channels, using the ADC_AnalogWatchdogCmd() function.
306
307 @endverbatim
308 * @{
309 */
310
311 /**
312 * @brief Enables or disables the analog watchdog on single/all regular
313 * or injected channels
314 * @param ADCx: where x can be 1 to select the ADC peripheral.
315 * @param ADC_AnalogWatchdog: the ADC analog watchdog configuration.
316 * This parameter can be one of the following values:
317 * @arg ADC_AnalogWatchdog_SingleRegEnable: Analog watchdog on a single regular channel
318 * @arg ADC_AnalogWatchdog_SingleInjecEnable: Analog watchdog on a single injected channel
319 * @arg ADC_AnalogWatchdog_SingleRegOrInjecEnable: Analog watchdog on a single regular or injected channel
320 * @arg ADC_AnalogWatchdog_AllRegEnable: Analog watchdog on all regular channel
321 * @arg ADC_AnalogWatchdog_AllInjecEnable: Analog watchdog on all injected channel
322 * @arg ADC_AnalogWatchdog_AllRegAllInjecEnable: Analog watchdog on all regular and injected channels
323 * @arg ADC_AnalogWatchdog_None: No channel guarded by the analog watchdog
324 * @retval None
325 */
ADC_AnalogWatchdogCmd(ADC_TypeDef * ADCx,uint32_t ADC_AnalogWatchdog)326 void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog)
327 {
328 uint32_t tmpreg = 0;
329 /* Check the parameters */
330 assert_param(IS_ADC_ALL_PERIPH(ADCx));
331 assert_param(IS_ADC_ANALOG_WATCHDOG(ADC_AnalogWatchdog));
332 /* Get the old register value */
333 tmpreg = ADCx->CR1;
334 /* Clear AWDEN, AWDENJ and AWDSGL bits */
335 tmpreg &= (uint32_t) (~(ADC_CR1_JAWDEN | ADC_CR1_AWDEN | ADC_CR1_AWDSGL));
336 /* Set the analog watchdog enable mode */
337 tmpreg |= ADC_AnalogWatchdog;
338 /* Store the new register value */
339 ADCx->CR1 = tmpreg;
340 }
341
342 /**
343 * @brief Configures the high and low thresholds of the analog watchdog.
344 * @param ADCx: where x can be 1 to select the ADC peripheral.
345 * @param HighThreshold: the ADC analog watchdog High threshold value.
346 * This parameter must be a 12bit value.
347 * @param LowThreshold: the ADC analog watchdog Low threshold value.
348 * This parameter must be a 12bit value.
349 * @retval None
350 */
ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef * ADCx,uint16_t HighThreshold,uint16_t LowThreshold)351 void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,
352 uint16_t LowThreshold)
353 {
354 /* Check the parameters */
355 assert_param(IS_ADC_ALL_PERIPH(ADCx));
356 assert_param(IS_ADC_THRESHOLD(HighThreshold));
357 assert_param(IS_ADC_THRESHOLD(LowThreshold));
358 /* Set the ADCx high threshold */
359 ADCx->HTR = HighThreshold;
360 /* Set the ADCx low threshold */
361 ADCx->LTR = LowThreshold;
362 }
363
364 /**
365 * @brief Configures the analog watchdog guarded single channel
366 * @param ADCx: where x can be 1 to select the ADC peripheral.
367 * @param ADC_Channel: the ADC channel to configure for the analog watchdog.
368 * This parameter can be one of the following values:
369 * @arg ADC_Channel_0: ADC Channel0 selected
370 * @arg ADC_Channel_1: ADC Channel1 selected
371 * @arg ADC_Channel_2: ADC Channel2 selected
372 * @arg ADC_Channel_3: ADC Channel3 selected
373 * @arg ADC_Channel_4: ADC Channel4 selected
374 * @arg ADC_Channel_5: ADC Channel5 selected
375 * @arg ADC_Channel_6: ADC Channel6 selected
376 * @arg ADC_Channel_7: ADC Channel7 selected
377 * @arg ADC_Channel_8: ADC Channel8 selected
378 * @arg ADC_Channel_9: ADC Channel9 selected
379 * @arg ADC_Channel_10: ADC Channel10 selected
380 * @arg ADC_Channel_11: ADC Channel11 selected
381 * @arg ADC_Channel_12: ADC Channel12 selected
382 * @arg ADC_Channel_13: ADC Channel13 selected
383 * @arg ADC_Channel_14: ADC Channel14 selected
384 * @arg ADC_Channel_15: ADC Channel15 selected
385 * @arg ADC_Channel_16: ADC Channel16 selected
386 * @arg ADC_Channel_17: ADC Channel17 selected
387 * @retval None
388 */
ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef * ADCx,uint8_t ADC_Channel)389 void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel)
390 {
391 uint32_t tmpreg = 0;
392 /* Check the parameters */
393 assert_param(IS_ADC_ALL_PERIPH(ADCx));
394 assert_param(IS_ADC_CHANNEL(ADC_Channel));
395 /* Get the old register value */
396 tmpreg = ADCx->CR1;
397 /* Clear the Analog watchdog channel select bits */
398 tmpreg &= (uint32_t)(~ADC_CR1_AWDCH);
399 /* Set the Analog watchdog channel */
400 tmpreg |= ADC_Channel;
401 /* Store the new register value */
402 ADCx->CR1 = tmpreg;
403 }
404
405 /**
406 * @}
407 */
408
409 /** @defgroup ADC_Group3 Temperature Sensor- Vrefint (Internal Reference Voltage) and VBAT management function
410 * @brief Temperature Sensor- Vrefint (Internal Reference Voltage) and VBAT management function
411 *
412 @verbatim
413 ===============================================================================
414 ##### Temperature Sensor, Vrefint and VBAT management function #####
415 ===============================================================================
416
417 [..] This section provides a function allowing to enable/ disable the internal
418 connections between the ADC and the Temperature Sensor, the Vrefint
419 and the VBAT sources.
420
421 [..] A typical configuration to get the Temperature sensor and Vrefint channels
422 voltages is done following these steps :
423 (#) Enable the internal connection of Temperature sensor and Vrefint sources
424 with the ADC channels using ADC_TempSensorVrefintCmd() function.
425 Enable the internal connection of VBAT using SYSCFG_VBATMonitoringCmd(ENABLE);
426 (#) Select the ADC_Channel_TempSensor and/or ADC_Channel_Vrefint and/or
427 ADC_Channel_Vbat using ADC_RegularChannelConfig()
428 or ADC_InjectedChannelConfig() functions
429 (#) Get the voltage values, using ADC_GetConversionValue() or
430 ADC_GetInjectedConversionValue().
431
432 @endverbatim
433 * @{
434 */
435
436 /**
437 * @brief Enables or disables the temperature sensor and Vrefint channel.
438 * @param NewState: new state of the temperature sensor.
439 * This parameter can be: ENABLE or DISABLE.
440 * @retval None
441 */
ADC_TempSensorVrefintCmd(FunctionalState NewState)442 void ADC_TempSensorVrefintCmd(FunctionalState NewState)
443 {
444 /* Check the parameters */
445 assert_param(IS_FUNCTIONAL_STATE(NewState));
446
447 if (NewState != DISABLE)
448 {
449 /* Enable the temperature sensor and Vrefint channel*/
450 ADC1->CR2 |= ADC_CR2_TSVREFE;
451 }
452 else
453 {
454 /* Disable the temperature sensor and Vrefint channel*/
455 ADC1->CR2 &= (uint32_t) (~ADC_CR2_TSVREFE);
456 }
457 }
458
459 /**
460 * @}
461 */
462
463 /** @defgroup ADC_Group4 Regular Channels Configuration functions
464 * @brief Regular Channels Configuration functions
465 *
466 @verbatim
467 ===============================================================================
468 ##### Regular Channels Configuration functions #####
469 ===============================================================================
470
471 [..] This section provides functions allowing to manage the ADC regular channels,
472 it is composed of 2 sub sections :
473
474 (#) Configuration and management functions for regular channels: This subsection
475 provides functions allowing to configure the ADC regular channels :
476 (++) Configure the rank in the regular group sequencer for each channel
477 (++) Configure the sampling time for each channel
478 (++) select the conversion Trigger for regular channels
479 (++) select the desired EOC event behavior configuration
480 (++) Activate the continuous Mode (*)
481 (++) Activate the Discontinuous Mode
482 -@@- Please Note that the following features for regular channels
483 are configured using the ADC_Init() function :
484 (+@@) scan mode activation
485 (+@@) continuous mode activation (**)
486 (+@@) External trigger source
487 (+@@) External trigger edge
488 (+@@) number of conversion in the regular channels group sequencer.
489
490 -@@- (*) and (**) are performing the same configuration
491
492 (#) Get the conversion data: This subsection provides an important function in
493 the ADC peripheral since it returns the converted data of the current
494 regular channel. When the Conversion value is read, the EOC Flag is
495 automatically cleared.
496
497 @endverbatim
498 * @{
499 */
500
501 /**
502 * @brief Configures for the selected ADC regular channel its corresponding
503 * rank in the sequencer and its sample time.
504 * @param ADCx: where x can be 1 to select the ADC peripheral.
505 * @param ADC_Channel: the ADC channel to configure.
506 * This parameter can be one of the following values:
507 * @arg ADC_Channel_0: ADC Channel0 selected
508 * @arg ADC_Channel_1: ADC Channel1 selected
509 * @arg ADC_Channel_2: ADC Channel2 selected
510 * @arg ADC_Channel_3: ADC Channel3 selected
511 * @arg ADC_Channel_4: ADC Channel4 selected
512 * @arg ADC_Channel_5: ADC Channel5 selected
513 * @arg ADC_Channel_6: ADC Channel6 selected
514 * @arg ADC_Channel_7: ADC Channel7 selected
515 * @arg ADC_Channel_8: ADC Channel8 selected
516 * @arg ADC_Channel_9: ADC Channel9 selected
517 * @arg ADC_Channel_10: ADC Channel10 selected
518 * @arg ADC_Channel_11: ADC Channel11 selected
519 * @arg ADC_Channel_12: ADC Channel12 selected
520 * @arg ADC_Channel_13: ADC Channel13 selected
521 * @arg ADC_Channel_14: ADC Channel14 selected
522 * @arg ADC_Channel_15: ADC Channel15 selected
523 * @arg ADC_Channel_16: ADC Channel16 selected
524 * @arg ADC_Channel_17: ADC Channel17 selected
525 * @param Rank: The rank in the regular group sequencer. This parameter must be between 1 to 16.
526 * @param ADC_SampleTime: The sample time value to be set for the selected channel.
527 * This parameter can be one of the following values:
528 * @arg ADC_SampleTime_1Cycles5: Sample time equal to 1.5 cycles
529 * @arg ADC_SampleTime_7Cycles5: Sample time equal to 7.5 cycles
530 * @arg ADC_SampleTime_13Cycles5: Sample time equal to 13.5 cycles
531 * @arg ADC_SampleTime_28Cycles5: Sample time equal to 28.5 cycles
532 * @arg ADC_SampleTime_41Cycles5: Sample time equal to 41.5 cycles
533 * @arg ADC_SampleTime_55Cycles5: Sample time equal to 55.5 cycles
534 * @arg ADC_SampleTime_71Cycles5: Sample time equal to 71.5 cycles
535 * @arg ADC_SampleTime_239Cycles5: Sample time equal to 239.5 cycles
536 * @retval None
537 */
ADC_RegularChannelConfig(ADC_TypeDef * ADCx,uint8_t ADC_Channel,uint8_t Rank,uint8_t ADC_SampleTime)538 void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
539 {
540 uint32_t tmpreg1 = 0, tmpreg2 = 0;
541 /* Check the parameters */
542 assert_param(IS_ADC_ALL_PERIPH(ADCx));
543 assert_param(IS_ADC_CHANNEL(ADC_Channel));
544 assert_param(IS_ADC_REGULAR_RANK(Rank));
545 assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
546 /* if ADC_Channel_10 ... ADC_Channel_17 is selected */
547 if (ADC_Channel > ADC_Channel_9)
548 {
549 /* Get the old register value */
550 tmpreg1 = ADCx->SMPR1;
551 /* Calculate the mask to clear */
552 tmpreg2 = ADC_SMPR_SMP_SET << (3 * (ADC_Channel - 10));
553 /* Clear the old channel sample time */
554 tmpreg1 &= ~tmpreg2;
555 /* Calculate the mask to set */
556 tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));
557 /* Set the new channel sample time */
558 tmpreg1 |= tmpreg2;
559 /* Store the new register value */
560 ADCx->SMPR1 = tmpreg1;
561 }
562 else /* ADC_Channel include in ADC_Channel_[0..9] */
563 {
564 /* Get the old register value */
565 tmpreg1 = ADCx->SMPR2;
566 /* Calculate the mask to clear */
567 tmpreg2 = ADC_SMPR_SMP_SET << (3 * ADC_Channel);
568 /* Clear the old channel sample time */
569 tmpreg1 &= ~tmpreg2;
570 /* Calculate the mask to set */
571 tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
572 /* Set the new channel sample time */
573 tmpreg1 |= tmpreg2;
574 /* Store the new register value */
575 ADCx->SMPR2 = tmpreg1;
576 }
577 /* For Rank 1 to 6 */
578 if (Rank < 7)
579 {
580 /* Get the old register value */
581 tmpreg1 = ADCx->SQR3;
582 /* Calculate the mask to clear */
583 tmpreg2 = ADC_SQR_SQ_SET << (5 * (Rank - 1));
584 /* Clear the old SQx bits for the selected rank */
585 tmpreg1 &= ~tmpreg2;
586 /* Calculate the mask to set */
587 tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1));
588 /* Set the SQx bits for the selected rank */
589 tmpreg1 |= tmpreg2;
590 /* Store the new register value */
591 ADCx->SQR3 = tmpreg1;
592 }
593 /* For Rank 7 to 12 */
594 else if (Rank < 13)
595 {
596 /* Get the old register value */
597 tmpreg1 = ADCx->SQR2;
598 /* Calculate the mask to clear */
599 tmpreg2 = ADC_SQR_SQ_SET << (5 * (Rank - 7));
600 /* Clear the old SQx bits for the selected rank */
601 tmpreg1 &= ~tmpreg2;
602 /* Calculate the mask to set */
603 tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7));
604 /* Set the SQx bits for the selected rank */
605 tmpreg1 |= tmpreg2;
606 /* Store the new register value */
607 ADCx->SQR2 = tmpreg1;
608 }
609 /* For Rank 13 to 16 */
610 else
611 {
612 /* Get the old register value */
613 tmpreg1 = ADCx->SQR1;
614 /* Calculate the mask to clear */
615 tmpreg2 = ADC_SQR_SQ_SET << (5 * (Rank - 13));
616 /* Clear the old SQx bits for the selected rank */
617 tmpreg1 &= ~tmpreg2;
618 /* Calculate the mask to set */
619 tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13));
620 /* Set the SQx bits for the selected rank */
621 tmpreg1 |= tmpreg2;
622 /* Store the new register value */
623 ADCx->SQR1 = tmpreg1;
624 }
625 }
626
627 /**
628 * @brief Enables or disables the ADCx conversion through external trigger.
629 * @param ADCx: where x can be 1 to select the ADC peripheral.
630 * @param NewState: new state of the selected ADC external trigger start of conversion.
631 * This parameter can be: ENABLE or DISABLE.
632 * @retval None
633 */
ADC_ExternalTrigConvCmd(ADC_TypeDef * ADCx,FunctionalState NewState)634 void ADC_ExternalTrigConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
635 {
636 /* Check the parameters */
637 assert_param(IS_ADC_ALL_PERIPH(ADCx));
638 assert_param(IS_FUNCTIONAL_STATE(NewState));
639 if (NewState != DISABLE)
640 {
641 /* Enable the selected ADC conversion on external event */
642 ADCx->CR2 |= ADC_CR2_EXTTRIG;
643 }
644 else
645 {
646 /* Disable the selected ADC conversion on external event */
647 ADCx->CR2 &= (uint32_t) (~ADC_CR2_EXTTRIG);
648 }
649 }
650
651 /**
652 * @brief Enables or disables the selected ADC software start conversion .
653 * @param ADCx: where x can be 1 to select the ADC peripheral.
654 * @retval None
655 */
ADC_SoftwareStartConv(ADC_TypeDef * ADCx)656 void ADC_SoftwareStartConv(ADC_TypeDef* ADCx)
657 {
658 /* Check the parameters */
659 assert_param(IS_ADC_ALL_PERIPH(ADCx));
660
661 /* Enable the selected ADC conversion on external event and start the selected
662 ADC conversion */
663 ADCx->CR2 |= (uint32_t)(ADC_CR2_SWSTART | ADC_CR2_EXTTRIG);
664
665 }
666
667 /**
668 * @brief Gets the selected ADC Software start conversion Status.
669 * @param ADCx: where x can be 1 to select the ADC peripheral.
670 * @retval The new state of ADC software start conversion (SET or RESET).
671 */
ADC_GetSoftwareStartConvStatus(ADC_TypeDef * ADCx)672 FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx)
673 {
674 FlagStatus bitstatus = RESET;
675 /* Check the parameters */
676 assert_param(IS_ADC_ALL_PERIPH(ADCx));
677 /* Check the status of SWSTART bit */
678 if ((ADCx->CR2 & ADC_CR2_SWSTART) != (uint32_t)RESET)
679 {
680 /* SWSTART bit is set */
681 bitstatus = SET;
682 }
683 else
684 {
685 /* SWSTART bit is reset */
686 bitstatus = RESET;
687 }
688 /* Return the SWSTART bit status */
689 return bitstatus;
690 }
691
692 /**
693 * @brief Enables or disables the ADC continuous conversion mode
694 * @param ADCx: where x can be 1 to select the ADC peripheral.
695 * @param NewState: new state of the selected ADC continuous conversion mode
696 * This parameter can be: ENABLE or DISABLE.
697 * @retval None
698 */
ADC_ContinuousModeCmd(ADC_TypeDef * ADCx,FunctionalState NewState)699 void ADC_ContinuousModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
700 {
701 /* Check the parameters */
702 assert_param(IS_ADC_ALL_PERIPH(ADCx));
703 assert_param(IS_FUNCTIONAL_STATE(NewState));
704
705 if (NewState != DISABLE)
706 {
707 /* Enable the selected ADC continuous conversion mode */
708 ADCx->CR2 |= (uint32_t)ADC_CR2_CONT;
709 }
710 else
711 {
712 /* Disable the selected ADC continuous conversion mode */
713 ADCx->CR2 &= (uint32_t)(~ADC_CR2_CONT);
714 }
715 }
716
717 /**
718 * @brief Configures the discontinuous mode for the selected ADC regular
719 * group channel.
720 * @param ADCx: where x can be 1 to select the ADC peripheral.
721 * @param Number: specifies the discontinuous mode regular channel
722 * count value. This number must be between 1 and 8.
723 * @retval None
724 */
ADC_DiscModeChannelCountConfig(ADC_TypeDef * ADCx,uint8_t Number)725 void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, uint8_t Number)
726 {
727 uint32_t tmpreg1 = 0;
728 uint32_t tmpreg2 = 0;
729 /* Check the parameters */
730 assert_param(IS_ADC_ALL_PERIPH(ADCx));
731 assert_param(IS_ADC_REGULAR_DISC_NUMBER(Number));
732 /* Get the old register value */
733 tmpreg1 = ADCx->CR1;
734 /* Clear the old discontinuous mode channel count */
735 tmpreg1 &= (uint32_t)(~ADC_CR1_DISCNUM);
736 /* Set the discontinuous mode channel count */
737 tmpreg2 = Number - 1;
738 tmpreg1 |= tmpreg2 << 13;
739 /* Store the new register value */
740 ADCx->CR1 = tmpreg1;
741 }
742
743 /**
744 * @brief Enables or disables the discontinuous mode on regular group
745 * channel for the specified ADC
746 * @param ADCx: where x can be 1 to select the ADC peripheral.
747 * @param NewState: new state of the selected ADC discontinuous mode
748 * on regular group channel.
749 * This parameter can be: ENABLE or DISABLE.
750 * @retval None
751 */
ADC_DiscModeCmd(ADC_TypeDef * ADCx,FunctionalState NewState)752 void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
753 {
754 /* Check the parameters */
755 assert_param(IS_ADC_ALL_PERIPH(ADCx));
756 assert_param(IS_FUNCTIONAL_STATE(NewState));
757 if (NewState != DISABLE)
758 {
759 /* Enable the selected ADC regular discontinuous mode */
760 ADCx->CR1 |= ADC_CR1_DISCEN;
761 }
762 else
763 {
764 /* Disable the selected ADC regular discontinuous mode */
765 ADCx->CR1 &= (uint32_t)(~ADC_CR1_DISCEN);
766 }
767 }
768
769 /**
770 * @brief Returns the last ADCx conversion result data for regular channel.
771 * @param ADCx: where x can be 1 to select the ADC peripheral.
772 * @retval The Data conversion value.
773 */
ADC_GetConversionValue(ADC_TypeDef * ADCx)774 uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
775 {
776 /* Check the parameters */
777 assert_param(IS_ADC_ALL_PERIPH(ADCx));
778 /* Return the selected ADC conversion value */
779 return (uint16_t) ADCx->DR;
780 }
781
782 /**
783 * @}
784 */
785
786 /** @defgroup ADC_Group5 Regular Channels DMA Configuration functions
787 * @brief Regular Channels DMA Configuration functions
788 *
789 @verbatim
790 ===============================================================================
791 ##### Regular Channels DMA Configuration functions #####
792 ===============================================================================
793
794 [..] This section provides functions allowing to configure the DMA for
795 ADC regular channels. Since converted regular channel values are stored
796 into a unique data register, it is useful to use DMA for conversion of
797 more than one regular channel. This avoids the loss of the data already
798 stored in the ADC Data register.
799 When the DMA mode is enabled (using the ADC_DMACmd() function), after
800 each conversion of a regular channel, a DMA request is generated.
801
802 @endverbatim
803 * @{
804 */
805
806 /**
807 * @brief Enables or disables the specified ADC DMA request.
808 * @param ADCx: where x can be 1 to select the ADC peripheral.
809 * @param NewState: new state of the selected ADC DMA transfer.
810 * This parameter can be: ENABLE or DISABLE.
811 * @retval None
812 */
ADC_DMACmd(ADC_TypeDef * ADCx,FunctionalState NewState)813 void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)
814 {
815 /* Check the parameters */
816 assert_param(IS_ADC_DMA_PERIPH(ADCx));
817 assert_param(IS_FUNCTIONAL_STATE(NewState));
818 if (NewState != DISABLE)
819 {
820 /* Enable the selected ADC DMA request */
821 ADCx->CR2 |= ADC_CR2_DMA;
822 }
823 else
824 {
825 /* Disable the selected ADC DMA request */
826 ADCx->CR2 &= (uint32_t) (~ADC_CR2_DMA);
827 }
828 }
829
830 /**
831 * @}
832 */
833
834 /** @defgroup ADC_Group6 Injected channels Configuration functions
835 * @brief Injected channels Configuration functions
836 *
837 @verbatim
838 ===============================================================================
839 ##### Injected channels Configuration functions #####
840 ===============================================================================
841
842 [..] This section provide functions allowing to configure the ADC Injected
843 channels, it is composed of 2 sub sections :
844 (#) Configuration functions for Injected channels: This subsection provides
845 functions allowing to configure the ADC injected channels :
846 (++) Configure the rank in the injected group sequencer for each channel
847 (++) Configure the sampling time for each channel
848 (++) Activate the Auto injected Mode
849 (++) Activate the Discontinuous Mode
850 (++) Scan mode activation
851 (++) External/software trigger source
852 (++) External trigger edge
853 (++) Injected channels sequencer.
854
855 (#) Get the Specified Injected channel conversion data: This subsection
856 provides an important function in the ADC peripheral since it returns
857 the converted data of the specific injected channel.
858
859 @endverbatim
860 * @{
861 */
862
863 /**
864 * @brief Configures for the selected ADC injected channel its corresponding
865 * rank in the sequencer and its sample time.
866 * @param ADCx: where x can be 1 to select the ADC peripheral.
867 * @param ADC_Channel: the ADC channel to configure.
868 * This parameter can be one of the following values:
869 * @arg ADC_Channel_0: ADC Channel0 selected
870 * @arg ADC_Channel_1: ADC Channel1 selected
871 * @arg ADC_Channel_2: ADC Channel2 selected
872 * @arg ADC_Channel_3: ADC Channel3 selected
873 * @arg ADC_Channel_4: ADC Channel4 selected
874 * @arg ADC_Channel_5: ADC Channel5 selected
875 * @arg ADC_Channel_6: ADC Channel6 selected
876 * @arg ADC_Channel_7: ADC Channel7 selected
877 * @arg ADC_Channel_8: ADC Channel8 selected
878 * @arg ADC_Channel_9: ADC Channel9 selected
879 * @arg ADC_Channel_10: ADC Channel10 selected
880 * @arg ADC_Channel_11: ADC Channel11 selected
881 * @arg ADC_Channel_12: ADC Channel12 selected
882 * @arg ADC_Channel_13: ADC Channel13 selected
883 * @arg ADC_Channel_14: ADC Channel14 selected
884 * @arg ADC_Channel_15: ADC Channel15 selected
885 * @arg ADC_Channel_16: ADC Channel16 selected
886 * @arg ADC_Channel_17: ADC Channel17 selected
887 * @param Rank: The rank in the injected group sequencer. This parameter must be between 1 and 4.
888 * @param ADC_SampleTime: The sample time value to be set for the selected channel.
889 * This parameter can be one of the following values:
890 * @arg ADC_SampleTime_1Cycles5: Sample time equal to 1.5 cycles
891 * @arg ADC_SampleTime_7Cycles5: Sample time equal to 7.5 cycles
892 * @arg ADC_SampleTime_13Cycles5: Sample time equal to 13.5 cycles
893 * @arg ADC_SampleTime_28Cycles5: Sample time equal to 28.5 cycles
894 * @arg ADC_SampleTime_41Cycles5: Sample time equal to 41.5 cycles
895 * @arg ADC_SampleTime_55Cycles5: Sample time equal to 55.5 cycles
896 * @arg ADC_SampleTime_71Cycles5: Sample time equal to 71.5 cycles
897 * @arg ADC_SampleTime_239Cycles5: Sample time equal to 239.5 cycles
898 * @retval None
899 */
ADC_InjectedChannelConfig(ADC_TypeDef * ADCx,uint8_t ADC_Channel,uint8_t Rank,uint8_t ADC_SampleTime)900 void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
901 {
902 uint32_t tmpreg1 = 0, tmpreg2 = 0, tmpreg3 = 0;
903 /* Check the parameters */
904 assert_param(IS_ADC_ALL_PERIPH(ADCx));
905 assert_param(IS_ADC_CHANNEL(ADC_Channel));
906 assert_param(IS_ADC_INJECTED_RANK(Rank));
907 assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
908 /* if ADC_Channel_10 ... ADC_Channel_17 is selected */
909 if (ADC_Channel > ADC_Channel_9)
910 {
911 /* Get the old register value */
912 tmpreg1 = ADCx->SMPR1;
913 /* Calculate the mask to clear */
914 tmpreg2 = ADC_SMPR_SMP_SET << (3*(ADC_Channel - 10));
915 /* Clear the old channel sample time */
916 tmpreg1 &= ~tmpreg2;
917 /* Calculate the mask to set */
918 tmpreg2 = (uint32_t)ADC_SampleTime << (3*(ADC_Channel - 10));
919 /* Set the new channel sample time */
920 tmpreg1 |= tmpreg2;
921 /* Store the new register value */
922 ADCx->SMPR1 = tmpreg1;
923 }
924 else /* ADC_Channel include in ADC_Channel_[0..9] */
925 {
926 /* Get the old register value */
927 tmpreg1 = ADCx->SMPR2;
928 /* Calculate the mask to clear */
929 tmpreg2 = ADC_SMPR_SMP_SET << (3 * ADC_Channel);
930 /* Clear the old channel sample time */
931 tmpreg1 &= ~tmpreg2;
932 /* Calculate the mask to set */
933 tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
934 /* Set the new channel sample time */
935 tmpreg1 |= tmpreg2;
936 /* Store the new register value */
937 ADCx->SMPR2 = tmpreg1;
938 }
939 /* Rank configuration */
940 /* Get the old register value */
941 tmpreg1 = ADCx->JSQR;
942 /* Get JL value: Number = JL+1 */
943 tmpreg3 = (tmpreg1 & ADC_JSQR_JL)>> 20;
944 /* Calculate the mask to clear: ((Rank-1)+(4-JL-1)) */
945 tmpreg2 = ADC_JSQR_JSQ_SET << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
946 /* Clear the old JSQx bits for the selected rank */
947 tmpreg1 &= ~tmpreg2;
948 /* Calculate the mask to set: ((Rank-1)+(4-JL-1)) */
949 tmpreg2 = (uint32_t)ADC_Channel << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
950 /* Set the JSQx bits for the selected rank */
951 tmpreg1 |= tmpreg2;
952 /* Store the new register value */
953 ADCx->JSQR = tmpreg1;
954 }
955
956 /**
957 * @brief Configures the sequencer length for injected channels
958 * @param ADCx: where x can be 1 to select the ADC peripheral.
959 * @param Length: The sequencer length.
960 * This parameter must be a number between 1 to 4.
961 * @retval None
962 */
ADC_InjectedSequencerLengthConfig(ADC_TypeDef * ADCx,uint8_t Length)963 void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length)
964 {
965 uint32_t tmpreg1 = 0;
966 uint32_t tmpreg2 = 0;
967 /* Check the parameters */
968 assert_param(IS_ADC_ALL_PERIPH(ADCx));
969 assert_param(IS_ADC_INJECTED_LENGTH(Length));
970
971 /* Get the old register value */
972 tmpreg1 = ADCx->JSQR;
973 /* Clear the old injected sequence length JL bits */
974 tmpreg1 &= (uint32_t)(~ADC_JSQR_JL);
975 /* Set the injected sequence length JL bits */
976 tmpreg2 = Length - 1;
977 tmpreg1 |= tmpreg2 << 20;
978 /* Store the new register value */
979 ADCx->JSQR = tmpreg1;
980 }
981
982 /**
983 * @brief Set the injected channels conversion value offset
984 * @param ADCx: where x can be 1 to select the ADC peripheral.
985 * @param ADC_InjectedChannel: the ADC injected channel to set its offset.
986 * This parameter can be one of the following values:
987 * @arg ADC_InjectedChannel_1: Injected Channel1 selected
988 * @arg ADC_InjectedChannel_2: Injected Channel2 selected
989 * @arg ADC_InjectedChannel_3: Injected Channel3 selected
990 * @arg ADC_InjectedChannel_4: Injected Channel4 selected
991 * @param ADC_Offset: the offset value for the selected ADC injected channel
992 * This parameter must be a 12bit value.
993 * @retval None
994 */
ADC_SetInjectedOffset(ADC_TypeDef * ADCx,uint8_t ADC_InjectedChannel,uint16_t ADC_Offset)995 void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t ADC_Offset)
996 {
997 __IO uint32_t tmp = 0;
998
999 /* Check the parameters */
1000 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1001 assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
1002 assert_param(IS_ADC_OFFSET(ADC_Offset));
1003
1004 tmp = (uint32_t)ADCx;
1005 tmp += ADC_InjectedChannel;
1006
1007 /* Set the selected injected channel data offset */
1008 *(__IO uint32_t *) tmp = (uint32_t)ADC_Offset;
1009 }
1010
1011 /**
1012 * @brief Configures the ADCx external trigger for injected channels conversion.
1013 * @param ADCx: where x can be 1 to select the ADC peripheral.
1014 * @param ADC_ExternalTrigInjecConv: specifies the ADC trigger to start injected conversion.
1015 * This parameter can be one of the following values:
1016 * @arg ADC_ExternalTrigInjecConv_T1_TRGO: Timer1 TRGO event selected (for ADC1, ADC2 and ADC3)
1017 * @arg ADC_ExternalTrigInjecConv_T1_CC4: Timer1 capture compare4 selected (for ADC1, ADC2 and ADC3)
1018 * @arg ADC_ExternalTrigInjecConv_T2_TRGO: Timer2 TRGO event selected (for ADC1 and ADC2)
1019 * @arg ADC_ExternalTrigInjecConv_T2_CC1: Timer2 capture compare1 selected (for ADC1 and ADC2)
1020 * @arg ADC_ExternalTrigInjecConv_T3_CC4: Timer3 capture compare4 selected (for ADC1 and ADC2)
1021 * @arg ADC_ExternalTrigInjecConv_T4_TRGO: Timer4 TRGO event selected (for ADC1 and ADC2)
1022 * @arg ADC_ExternalTrigInjecConv_Ext_IT15_TIM8_CC4: External interrupt line 15 or Timer8
1023 * capture compare4 event selected (for ADC1 and ADC2)
1024 * @arg ADC_ExternalTrigInjecConv_T4_CC3: Timer4 capture compare3 selected (for ADC3 only)
1025 * @arg ADC_ExternalTrigInjecConv_T8_CC2: Timer8 capture compare2 selected (for ADC3 only)
1026 * @arg ADC_ExternalTrigInjecConv_T8_CC4: Timer8 capture compare4 selected (for ADC3 only)
1027 * @arg ADC_ExternalTrigInjecConv_T5_TRGO: Timer5 TRGO event selected (for ADC3 only)
1028 * @arg ADC_ExternalTrigInjecConv_T5_CC4: Timer5 capture compare4 selected (for ADC3 only)
1029 * @arg ADC_ExternalTrigInjecConv_None: Injected conversion started by software and not
1030 * by external trigger (for ADC1, ADC2 and ADC3)
1031 * @retval None
1032 */
ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef * ADCx,uint32_t ADC_ExternalTrigInjecConv)1033 void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv)
1034 {
1035 uint32_t tmpreg = 0;
1036 /* Check the parameters */
1037 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1038 assert_param(IS_ADC_EXT_INJEC_TRIG(ADC_ExternalTrigInjecConv));
1039 /* Get the old register value */
1040 tmpreg = ADCx->CR2;
1041 /* Clear the old external event selection for injected group */
1042 tmpreg &= (uint32_t) (~ADC_CR2_JEXTSEL);
1043 /* Set the external event selection for injected group */
1044 tmpreg |= ADC_ExternalTrigInjecConv;
1045 /* Store the new register value */
1046 ADCx->CR2 = tmpreg;
1047 }
1048
1049 /**
1050 * @brief Enables or disables the ADCx injected channels conversion through
1051 * external trigger
1052 * @param ADCx: where x can be 1 to select the ADC peripheral.
1053 * @param NewState: new state of the selected ADC external trigger start of
1054 * injected conversion.
1055 * This parameter can be: ENABLE or DISABLE.
1056 * @retval None
1057 */
ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef * ADCx,FunctionalState NewState)1058 void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1059 {
1060 /* Check the parameters */
1061 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1062 assert_param(IS_FUNCTIONAL_STATE(NewState));
1063 if (NewState != DISABLE)
1064 {
1065 /* Enable the selected ADC external event selection for injected group */
1066 ADCx->CR2 |= ADC_CR2_JEXTTRIG;
1067 }
1068 else
1069 {
1070 /* Disable the selected ADC external event selection for injected group */
1071 ADCx->CR2 &= (uint32_t)(~ADC_CR2_JEXTTRIG);
1072 }
1073 }
1074
1075 /**
1076 * @brief Enables or disables the selected ADC start of the injected
1077 * channels conversion.
1078 * @param ADCx: where x can be 1 to select the ADC peripheral.
1079 * @param NewState: new state of the selected ADC software start injected conversion.
1080 * This parameter can be: ENABLE or DISABLE.
1081 * @retval None
1082 */
ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef * ADCx,FunctionalState NewState)1083 void ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1084 {
1085 /* Check the parameters */
1086 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1087 assert_param(IS_FUNCTIONAL_STATE(NewState));
1088 if (NewState != DISABLE)
1089 {
1090 /* Enable the selected ADC conversion for injected group on external event and start the selected
1091 ADC injected conversion */
1092 ADCx->CR2 |= (ADC_CR2_JSWSTART | ADC_CR2_JEXTTRIG);
1093 }
1094 else
1095 {
1096 /* Disable the selected ADC conversion on external event for injected group and stop the selected
1097 ADC injected conversion */
1098 ADCx->CR2 &= (uint32_t) ~(ADC_CR2_JSWSTART | ADC_CR2_JEXTTRIG);
1099 }
1100 }
1101
1102 /**
1103 * @brief Gets the selected ADC Software start injected conversion Status.
1104 * @param ADCx: where x can be 1 to select the ADC peripheral.
1105 * @retval The new state of ADC software start injected conversion (SET or RESET).
1106 */
ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef * ADCx)1107 FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx)
1108 {
1109 FlagStatus bitstatus = RESET;
1110 /* Check the parameters */
1111 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1112 /* Check the status of JSWSTART bit */
1113 if ((ADCx->CR2 & ADC_CR2_JSWSTART) != (uint32_t)RESET)
1114 {
1115 /* JSWSTART bit is set */
1116 bitstatus = SET;
1117 }
1118 else
1119 {
1120 /* JSWSTART bit is reset */
1121 bitstatus = RESET;
1122 }
1123 /* Return the JSWSTART bit status */
1124 return bitstatus;
1125 }
1126
1127 /**
1128 * @brief Enables or disables the selected ADC automatic injected group
1129 * conversion after regular one.
1130 * @param ADCx: where x can be 1 to select the ADC peripheral.
1131 * @param NewState: new state of the selected ADC auto injected conversion
1132 * This parameter can be: ENABLE or DISABLE.
1133 * @retval None
1134 */
ADC_AutoInjectedConvCmd(ADC_TypeDef * ADCx,FunctionalState NewState)1135 void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1136 {
1137 /* Check the parameters */
1138 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1139 assert_param(IS_FUNCTIONAL_STATE(NewState));
1140 if (NewState != DISABLE)
1141 {
1142 /* Enable the selected ADC automatic injected group conversion */
1143 ADCx->CR1 |= ADC_CR1_JAUTO;
1144 }
1145 else
1146 {
1147 /* Disable the selected ADC automatic injected group conversion */
1148 ADCx->CR1 &= (uint32_t)(~ADC_CR1_JAUTO);
1149 }
1150 }
1151
1152 /**
1153 * @brief Enables or disables the discontinuous mode for injected group
1154 * channel for the specified ADC
1155 * @param ADCx: where x can be 1 to select the ADC peripheral.
1156 * @param NewState: new state of the selected ADC discontinuous mode
1157 * on injected group channel.
1158 * This parameter can be: ENABLE or DISABLE.
1159 * @retval None
1160 */
ADC_InjectedDiscModeCmd(ADC_TypeDef * ADCx,FunctionalState NewState)1161 void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1162 {
1163 /* Check the parameters */
1164 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1165 assert_param(IS_FUNCTIONAL_STATE(NewState));
1166 if (NewState != DISABLE)
1167 {
1168 /* Enable the selected ADC injected discontinuous mode */
1169 ADCx->CR1 |= ADC_CR1_JDISCEN;
1170 }
1171 else
1172 {
1173 /* Disable the selected ADC injected discontinuous mode */
1174 ADCx->CR1 &= (uint32_t) (~ADC_CR1_JDISCEN);
1175 }
1176 }
1177
1178 /**
1179 * @brief Returns the ADC injected channel conversion result
1180 * @param ADCx: where x can be 1 to select the ADC peripheral.
1181 * @param ADC_InjectedChannel: the converted ADC injected channel.
1182 * This parameter can be one of the following values:
1183 * @arg ADC_InjectedChannel_1: Injected Channel1 selected
1184 * @arg ADC_InjectedChannel_2: Injected Channel2 selected
1185 * @arg ADC_InjectedChannel_3: Injected Channel3 selected
1186 * @arg ADC_InjectedChannel_4: Injected Channel4 selected
1187 * @retval The Data conversion value.
1188 */
ADC_GetInjectedConversionValue(ADC_TypeDef * ADCx,uint8_t ADC_InjectedChannel)1189 uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel)
1190 {
1191 __IO uint32_t tmp = 0;
1192
1193 /* Check the parameters */
1194 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1195 assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
1196
1197 tmp = (uint32_t)ADCx;
1198 tmp += ADC_InjectedChannel + ADC_JDR_OFFSET;
1199
1200 /* Returns the selected injected channel conversion data value */
1201 return (uint16_t) (*(__IO uint32_t*) tmp);
1202 }
1203
1204 /**
1205 * @}
1206 */
1207
1208 /** @defgroup ADC_Group7 Interrupts and flags management functions
1209 * @brief Interrupts and flags management functions
1210 *
1211 @verbatim
1212 ===============================================================================
1213 ##### Interrupts and flags management functions #####
1214 ===============================================================================
1215
1216 [..] This section provides functions allowing to configure the ADC Interrupts,
1217 get the status and clear flags and Interrupts pending bits.
1218
1219 [..] The ADC provide 4 Interrupts sources and 9 Flags which can be divided
1220 into 3 groups:
1221
1222 *** Flags and Interrupts for ADC regular channels ***
1223 =====================================================
1224 [..]
1225 (+)Flags :
1226 (##) ADC_FLAG_EOC : Regular channel end of conversion to indicate
1227 the end of sequence of regular GROUP conversions
1228 (##) ADC_FLAG_STRT: Regular channel start to indicate when regular
1229 CHANNEL conversion starts.
1230
1231 (+)Interrupts :
1232 (##) ADC_IT_EOC : specifies the interrupt source for Regular channel
1233 end of conversion event.
1234
1235
1236 *** Flags and Interrupts for ADC Injected channels ***
1237 ======================================================
1238 [..]
1239 (+)Flags :
1240 (##) ADC_FLAG_JEOC : Injected channel end of conversion to indicate
1241 at the end of injected GROUP conversion
1242 (##) ADC_FLAG_JSTRT : Injected channel start to indicate when injected
1243 GROUP conversion starts.
1244
1245 (+)Interrupts :
1246 (##) ADC_IT_JEOC : specifies the interrupt source for Injected channel
1247 end of conversion event.
1248
1249 *** General Flags and Interrupts for the ADC ***
1250 ================================================
1251 [..]
1252 (+)Flags :
1253 (##) ADC_FLAG_AWD : Analog watchdog + to indicate if the converted voltage
1254 crosses the programmed thresholds values.
1255 (+)Interrupts :
1256 (##) ADC_IT_AWD : specifies the interrupt source for Analog watchdog event.
1257
1258 [..] The user should identify which mode will be used in his application to
1259 manage the ADC controller events: Polling mode or Interrupt mode.
1260
1261 [..] In the Polling Mode it is advised to use the following functions:
1262 (+) ADC_GetFlagStatus() : to check if flags events occur.
1263 (+) ADC_ClearFlag() : to clear the flags events.
1264
1265 [..] In the Interrupt Mode it is advised to use the following functions:
1266 (+) ADC_ITConfig() : to enable or disable the interrupt source.
1267 (+) ADC_GetITStatus() : to check if Interrupt occurs.
1268 (+) ADC_ClearITPendingBit() : to clear the Interrupt pending Bit
1269 (corresponding Flag).
1270 @endverbatim
1271 * @{
1272 */
1273
1274 /**
1275 * @brief Enables or disables the specified ADC interrupts.
1276 * @param ADCx: where x can be 1 to select the ADC peripheral.
1277 * @param ADC_IT: specifies the ADC interrupt sources to be enabled or disabled.
1278 * This parameter can be any combination of the following values:
1279 * @arg ADC_IT_EOC: End of conversion interrupt mask
1280 * @arg ADC_IT_AWD: Analog watchdog interrupt mask
1281 * @arg ADC_IT_JEOC: End of injected conversion interrupt mask
1282 * @param NewState: new state of the specified ADC interrupts.
1283 * This parameter can be: ENABLE or DISABLE.
1284 * @retval None
1285 */
ADC_ITConfig(ADC_TypeDef * ADCx,uint16_t ADC_IT,FunctionalState NewState)1286 void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState)
1287 {
1288 uint8_t itmask = 0;
1289 /* Check the parameters */
1290 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1291 assert_param(IS_FUNCTIONAL_STATE(NewState));
1292 assert_param(IS_ADC_IT(ADC_IT));
1293 /* Get the ADC IT index */
1294 itmask = (uint8_t)ADC_IT;
1295 if (NewState != DISABLE)
1296 {
1297 /* Enable the selected ADC interrupts */
1298 ADCx->CR1 |= itmask;
1299 }
1300 else
1301 {
1302 /* Disable the selected ADC interrupts */
1303 ADCx->CR1 &= (~(uint32_t)itmask);
1304 }
1305 }
1306
1307 /**
1308 * @brief Checks whether the specified ADC flag is set or not.
1309 * @param ADCx: where x can be 1 to select the ADC peripheral.
1310 * @param ADC_FLAG: specifies the flag to check.
1311 * This parameter can be one of the following values:
1312 * @arg ADC_FLAG_AWD: Analog watchdog flag
1313 * @arg ADC_FLAG_EOC: End of conversion flag
1314 * @arg ADC_FLAG_JEOC: End of injected group conversion flag
1315 * @arg ADC_FLAG_JSTRT: Start of injected group conversion flag
1316 * @arg ADC_FLAG_STRT: Start of regular group conversion flag
1317 * @retval The new state of ADC_FLAG (SET or RESET).
1318 */
ADC_GetFlagStatus(ADC_TypeDef * ADCx,uint8_t ADC_FLAG)1319 FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
1320 {
1321 FlagStatus bitstatus = RESET;
1322 /* Check the parameters */
1323 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1324 assert_param(IS_ADC_GET_FLAG(ADC_FLAG));
1325 /* Check the status of the specified ADC flag */
1326 if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET)
1327 {
1328 /* ADC_FLAG is set */
1329 bitstatus = SET;
1330 }
1331 else
1332 {
1333 /* ADC_FLAG is reset */
1334 bitstatus = RESET;
1335 }
1336 /* Return the ADC_FLAG status */
1337 return bitstatus;
1338 }
1339
1340 /**
1341 * @brief Clears the ADCx's pending flags.
1342 * @param ADCx: where x can be 1 to select the ADC peripheral.
1343 * @param ADC_FLAG: specifies the flag to clear.
1344 * This parameter can be any combination of the following values:
1345 * @arg ADC_FLAG_AWD: Analog watchdog flag
1346 * @arg ADC_FLAG_EOC: End of conversion flag
1347 * @arg ADC_FLAG_JEOC: End of injected group conversion flag
1348 * @arg ADC_FLAG_JSTRT: Start of injected group conversion flag
1349 * @arg ADC_FLAG_STRT: Start of regular group conversion flag
1350 * @retval None
1351 */
ADC_ClearFlag(ADC_TypeDef * ADCx,uint8_t ADC_FLAG)1352 void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
1353 {
1354 /* Check the parameters */
1355 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1356 assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));
1357 /* Clear the selected ADC flags */
1358 ADCx->SR = ~(uint32_t)ADC_FLAG;
1359 }
1360
1361 /**
1362 * @brief Checks whether the specified ADC interrupt has occurred or not.
1363 * @param ADCx: where x can be 1 to select the ADC peripheral.
1364 * @param ADC_IT: specifies the ADC interrupt source to check.
1365 * This parameter can be one of the following values:
1366 * @arg ADC_IT_EOC: End of conversion interrupt mask
1367 * @arg ADC_IT_AWD: Analog watchdog interrupt mask
1368 * @arg ADC_IT_JEOC: End of injected conversion interrupt mask
1369 * @retval The new state of ADC_IT (SET or RESET).
1370 */
ADC_GetITStatus(ADC_TypeDef * ADCx,uint16_t ADC_IT)1371 ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT)
1372 {
1373 ITStatus bitstatus = RESET;
1374 uint32_t itmask = 0, enablestatus = 0;
1375 /* Check the parameters */
1376 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1377 assert_param(IS_ADC_GET_IT(ADC_IT));
1378 /* Get the ADC IT index */
1379 itmask = ADC_IT >> 8;
1380 /* Get the ADC_IT enable bit status */
1381 enablestatus = (ADCx->CR1 & (uint8_t)ADC_IT) ;
1382 /* Check the status of the specified ADC interrupt */
1383 if (((ADCx->SR & itmask) != (uint32_t)RESET) && enablestatus)
1384 {
1385 /* ADC_IT is set */
1386 bitstatus = SET;
1387 }
1388 else
1389 {
1390 /* ADC_IT is reset */
1391 bitstatus = RESET;
1392 }
1393 /* Return the ADC_IT status */
1394 return bitstatus;
1395 }
1396
1397 /**
1398 * @brief Clears the ADCx's interrupt pending bits.
1399 * @param ADCx: where x can be 1 to select the ADC peripheral.
1400 * @param ADC_IT: specifies the ADC interrupt pending bit to clear.
1401 * This parameter can be any combination of the following values:
1402 * @arg ADC_IT_EOC: End of conversion interrupt mask
1403 * @arg ADC_IT_AWD: Analog watchdog interrupt mask
1404 * @arg ADC_IT_JEOC: End of injected conversion interrupt mask
1405 * @retval None
1406 */
ADC_ClearITPendingBit(ADC_TypeDef * ADCx,uint16_t ADC_IT)1407 void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT)
1408 {
1409 uint8_t itmask = 0;
1410 /* Check the parameters */
1411 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1412 assert_param(IS_ADC_IT(ADC_IT));
1413 /* Get the ADC IT index */
1414 itmask = (uint8_t)(ADC_IT >> 8);
1415 /* Clear the selected ADC interrupt pending bits */
1416 ADCx->SR = ~(uint32_t)itmask;
1417 }
1418
1419 /**
1420 * @brief Gets the selected ADC calibration status.
1421 * @param ADCx: where x can be 1 to select the ADC peripheral.
1422 * @retval The new state of ADC calibration (SET or RESET).
1423 */
ADC_GetCalibrationStatus(ADC_TypeDef * ADCx)1424 FlagStatus ADC_GetCalibrationStatus(ADC_TypeDef* ADCx)
1425 {
1426 FlagStatus bitstatus = RESET;
1427 /* Check the parameters */
1428 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1429 /* Check the status of CAL bit */
1430 if ((ADCx->CR2 & ADC_CR2_CAL) != (uint32_t)RESET)
1431 {
1432 /* CAL bit is set: calibration on going */
1433 bitstatus = SET;
1434 }
1435 else
1436 {
1437 /* CAL bit is reset: end of calibration */
1438 bitstatus = RESET;
1439 }
1440 /* Return the CAL bit status */
1441 return bitstatus;
1442 }
1443
1444 /**
1445 * @brief Gets the selected ADC reset calibration registers status.
1446 * @param ADCx: where x can be 1 to select the ADC peripheral.
1447 * @retval The new state of ADC reset calibration registers (SET or RESET).
1448 */
ADC_GetResetCalibrationStatus(ADC_TypeDef * ADCx)1449 FlagStatus ADC_GetResetCalibrationStatus(ADC_TypeDef* ADCx)
1450 {
1451 FlagStatus bitstatus = RESET;
1452 /* Check the parameters */
1453 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1454 /* Check the status of RSTCAL bit */
1455 if ((ADCx->CR2 & ADC_CR2_RSTCAL) != (uint32_t)RESET)
1456 {
1457 /* RSTCAL bit is set */
1458 bitstatus = SET;
1459 }
1460 else
1461 {
1462 /* RSTCAL bit is reset */
1463 bitstatus = RESET;
1464 }
1465 /* Return the RSTCAL bit status */
1466 return bitstatus;
1467 }
1468
1469 /**
1470 * @}
1471 */
1472
1473 /**
1474 * @}
1475 */
1476
1477 /**
1478 * @}
1479 */
1480
1481 /**
1482 * @}
1483 */
1484
1485 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1486