]> jspc29.x-matter.uni-frankfurt.de Git - mvd_firmware.git/blob
916a096d4b74fa2d6c60d1e6fa53e27bfc426053
[mvd_firmware.git] /
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f10x_adc.c\r
4   * @author  MCD Application Team\r
5   * @version V3.5.0\r
6   * @date    11-March-2011\r
7   * @brief   This file provides all the ADC firmware functions.\r
8   ******************************************************************************\r
9   * @attention\r
10   *\r
11   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
12   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
13   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
14   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
15   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
16   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
17   *\r
18   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>\r
19   ******************************************************************************\r
20   */\r
21 \r
22 /* Includes ------------------------------------------------------------------*/\r
23 #include "stm32f10x_adc.h"\r
24 #include "stm32f10x_rcc.h"\r
25 \r
26 /** @addtogroup STM32F10x_StdPeriph_Driver\r
27   * @{\r
28   */\r
29 \r
30 /** @defgroup ADC \r
31   * @brief ADC driver modules\r
32   * @{\r
33   */\r
34 \r
35 /** @defgroup ADC_Private_TypesDefinitions\r
36   * @{\r
37   */\r
38 \r
39 /**\r
40   * @}\r
41   */\r
42 \r
43 /** @defgroup ADC_Private_Defines\r
44   * @{\r
45   */\r
46 \r
47 /* ADC DISCNUM mask */\r
48 #define CR1_DISCNUM_Reset           ((uint32_t)0xFFFF1FFF)\r
49 \r
50 /* ADC DISCEN mask */\r
51 #define CR1_DISCEN_Set              ((uint32_t)0x00000800)\r
52 #define CR1_DISCEN_Reset            ((uint32_t)0xFFFFF7FF)\r
53 \r
54 /* ADC JAUTO mask */\r
55 #define CR1_JAUTO_Set               ((uint32_t)0x00000400)\r
56 #define CR1_JAUTO_Reset             ((uint32_t)0xFFFFFBFF)\r
57 \r
58 /* ADC JDISCEN mask */\r
59 #define CR1_JDISCEN_Set             ((uint32_t)0x00001000)\r
60 #define CR1_JDISCEN_Reset           ((uint32_t)0xFFFFEFFF)\r
61 \r
62 /* ADC AWDCH mask */\r
63 #define CR1_AWDCH_Reset             ((uint32_t)0xFFFFFFE0)\r
64 \r
65 /* ADC Analog watchdog enable mode mask */\r
66 #define CR1_AWDMode_Reset           ((uint32_t)0xFF3FFDFF)\r
67 \r
68 /* CR1 register Mask */\r
69 #define CR1_CLEAR_Mask              ((uint32_t)0xFFF0FEFF)\r
70 \r
71 /* ADC ADON mask */\r
72 #define CR2_ADON_Set                ((uint32_t)0x00000001)\r
73 #define CR2_ADON_Reset              ((uint32_t)0xFFFFFFFE)\r
74 \r
75 /* ADC DMA mask */\r
76 #define CR2_DMA_Set                 ((uint32_t)0x00000100)\r
77 #define CR2_DMA_Reset               ((uint32_t)0xFFFFFEFF)\r
78 \r
79 /* ADC RSTCAL mask */\r
80 #define CR2_RSTCAL_Set              ((uint32_t)0x00000008)\r
81 \r
82 /* ADC CAL mask */\r
83 #define CR2_CAL_Set                 ((uint32_t)0x00000004)\r
84 \r
85 /* ADC SWSTART mask */\r
86 #define CR2_SWSTART_Set             ((uint32_t)0x00400000)\r
87 \r
88 /* ADC EXTTRIG mask */\r
89 #define CR2_EXTTRIG_Set             ((uint32_t)0x00100000)\r
90 #define CR2_EXTTRIG_Reset           ((uint32_t)0xFFEFFFFF)\r
91 \r
92 /* ADC Software start mask */\r
93 #define CR2_EXTTRIG_SWSTART_Set     ((uint32_t)0x00500000)\r
94 #define CR2_EXTTRIG_SWSTART_Reset   ((uint32_t)0xFFAFFFFF)\r
95 \r
96 /* ADC JEXTSEL mask */\r
97 #define CR2_JEXTSEL_Reset           ((uint32_t)0xFFFF8FFF)\r
98 \r
99 /* ADC JEXTTRIG mask */\r
100 #define CR2_JEXTTRIG_Set            ((uint32_t)0x00008000)\r
101 #define CR2_JEXTTRIG_Reset          ((uint32_t)0xFFFF7FFF)\r
102 \r
103 /* ADC JSWSTART mask */\r
104 #define CR2_JSWSTART_Set            ((uint32_t)0x00200000)\r
105 \r
106 /* ADC injected software start mask */\r
107 #define CR2_JEXTTRIG_JSWSTART_Set   ((uint32_t)0x00208000)\r
108 #define CR2_JEXTTRIG_JSWSTART_Reset ((uint32_t)0xFFDF7FFF)\r
109 \r
110 /* ADC TSPD mask */\r
111 #define CR2_TSVREFE_Set             ((uint32_t)0x00800000)\r
112 #define CR2_TSVREFE_Reset           ((uint32_t)0xFF7FFFFF)\r
113 \r
114 /* CR2 register Mask */\r
115 #define CR2_CLEAR_Mask              ((uint32_t)0xFFF1F7FD)\r
116 \r
117 /* ADC SQx mask */\r
118 #define SQR3_SQ_Set                 ((uint32_t)0x0000001F)\r
119 #define SQR2_SQ_Set                 ((uint32_t)0x0000001F)\r
120 #define SQR1_SQ_Set                 ((uint32_t)0x0000001F)\r
121 \r
122 /* SQR1 register Mask */\r
123 #define SQR1_CLEAR_Mask             ((uint32_t)0xFF0FFFFF)\r
124 \r
125 /* ADC JSQx mask */\r
126 #define JSQR_JSQ_Set                ((uint32_t)0x0000001F)\r
127 \r
128 /* ADC JL mask */\r
129 #define JSQR_JL_Set                 ((uint32_t)0x00300000)\r
130 #define JSQR_JL_Reset               ((uint32_t)0xFFCFFFFF)\r
131 \r
132 /* ADC SMPx mask */\r
133 #define SMPR1_SMP_Set               ((uint32_t)0x00000007)\r
134 #define SMPR2_SMP_Set               ((uint32_t)0x00000007)\r
135 \r
136 /* ADC JDRx registers offset */\r
137 #define JDR_Offset                  ((uint8_t)0x28)\r
138 \r
139 /* ADC1 DR register base address */\r
140 #define DR_ADDRESS                  ((uint32_t)0x4001244C)\r
141 \r
142 /**\r
143   * @}\r
144   */\r
145 \r
146 /** @defgroup ADC_Private_Macros\r
147   * @{\r
148   */\r
149 \r
150 /**\r
151   * @}\r
152   */\r
153 \r
154 /** @defgroup ADC_Private_Variables\r
155   * @{\r
156   */\r
157 \r
158 /**\r
159   * @}\r
160   */\r
161 \r
162 /** @defgroup ADC_Private_FunctionPrototypes\r
163   * @{\r
164   */\r
165 \r
166 /**\r
167   * @}\r
168   */\r
169 \r
170 /** @defgroup ADC_Private_Functions\r
171   * @{\r
172   */\r
173 \r
174 /**\r
175   * @brief  Deinitializes the ADCx peripheral registers to their default reset values.\r
176   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
177   * @retval None\r
178   */\r
179 void ADC_DeInit(ADC_TypeDef* ADCx)\r
180 {\r
181   /* Check the parameters */\r
182   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
183   \r
184   if (ADCx == ADC1)\r
185   {\r
186     /* Enable ADC1 reset state */\r
187     RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);\r
188     /* Release ADC1 from reset state */\r
189     RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);\r
190   }\r
191   else if (ADCx == ADC2)\r
192   {\r
193     /* Enable ADC2 reset state */\r
194     RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2, ENABLE);\r
195     /* Release ADC2 from reset state */\r
196     RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2, DISABLE);\r
197   }\r
198   else\r
199   {\r
200     if (ADCx == ADC3)\r
201     {\r
202       /* Enable ADC3 reset state */\r
203       RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, ENABLE);\r
204       /* Release ADC3 from reset state */\r
205       RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, DISABLE);\r
206     }\r
207   }\r
208 }\r
209 \r
210 /**\r
211   * @brief  Initializes the ADCx peripheral according to the specified parameters\r
212   *         in the ADC_InitStruct.\r
213   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
214   * @param  ADC_InitStruct: pointer to an ADC_InitTypeDef structure that contains\r
215   *         the configuration information for the specified ADC peripheral.\r
216   * @retval None\r
217   */\r
218 void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)\r
219 {\r
220   uint32_t tmpreg1 = 0;\r
221   uint8_t tmpreg2 = 0;\r
222   /* Check the parameters */\r
223   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
224   assert_param(IS_ADC_MODE(ADC_InitStruct->ADC_Mode));\r
225   assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode));\r
226   assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode));\r
227   assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv));   \r
228   assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign)); \r
229   assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfChannel));\r
230 \r
231   /*---------------------------- ADCx CR1 Configuration -----------------*/\r
232   /* Get the ADCx CR1 value */\r
233   tmpreg1 = ADCx->CR1;\r
234   /* Clear DUALMOD and SCAN bits */\r
235   tmpreg1 &= CR1_CLEAR_Mask;\r
236   /* Configure ADCx: Dual mode and scan conversion mode */\r
237   /* Set DUALMOD bits according to ADC_Mode value */\r
238   /* Set SCAN bit according to ADC_ScanConvMode value */\r
239   tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_Mode | ((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8));\r
240   /* Write to ADCx CR1 */\r
241   ADCx->CR1 = tmpreg1;\r
242 \r
243   /*---------------------------- ADCx CR2 Configuration -----------------*/\r
244   /* Get the ADCx CR2 value */\r
245   tmpreg1 = ADCx->CR2;\r
246   /* Clear CONT, ALIGN and EXTSEL bits */\r
247   tmpreg1 &= CR2_CLEAR_Mask;\r
248   /* Configure ADCx: external trigger event and continuous conversion mode */\r
249   /* Set ALIGN bit according to ADC_DataAlign value */\r
250   /* Set EXTSEL bits according to ADC_ExternalTrigConv value */\r
251   /* Set CONT bit according to ADC_ContinuousConvMode value */\r
252   tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ExternalTrigConv |\r
253             ((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1));\r
254   /* Write to ADCx CR2 */\r
255   ADCx->CR2 = tmpreg1;\r
256 \r
257   /*---------------------------- ADCx SQR1 Configuration -----------------*/\r
258   /* Get the ADCx SQR1 value */\r
259   tmpreg1 = ADCx->SQR1;\r
260   /* Clear L bits */\r
261   tmpreg1 &= SQR1_CLEAR_Mask;\r
262   /* Configure ADCx: regular channel sequence length */\r
263   /* Set L bits according to ADC_NbrOfChannel value */\r
264   tmpreg2 |= (uint8_t) (ADC_InitStruct->ADC_NbrOfChannel - (uint8_t)1);\r
265   tmpreg1 |= (uint32_t)tmpreg2 << 20;\r
266   /* Write to ADCx SQR1 */\r
267   ADCx->SQR1 = tmpreg1;\r
268 }\r
269 \r
270 /**\r
271   * @brief  Fills each ADC_InitStruct member with its default value.\r
272   * @param  ADC_InitStruct : pointer to an ADC_InitTypeDef structure which will be initialized.\r
273   * @retval None\r
274   */\r
275 void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)\r
276 {\r
277   /* Reset ADC init structure parameters values */\r
278   /* Initialize the ADC_Mode member */\r
279   ADC_InitStruct->ADC_Mode = ADC_Mode_Independent;\r
280   /* initialize the ADC_ScanConvMode member */\r
281   ADC_InitStruct->ADC_ScanConvMode = DISABLE;\r
282   /* Initialize the ADC_ContinuousConvMode member */\r
283   ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;\r
284   /* Initialize the ADC_ExternalTrigConv member */\r
285   ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;\r
286   /* Initialize the ADC_DataAlign member */\r
287   ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;\r
288   /* Initialize the ADC_NbrOfChannel member */\r
289   ADC_InitStruct->ADC_NbrOfChannel = 1;\r
290 }\r
291 \r
292 /**\r
293   * @brief  Enables or disables the specified ADC peripheral.\r
294   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
295   * @param  NewState: new state of the ADCx peripheral.\r
296   *   This parameter can be: ENABLE or DISABLE.\r
297   * @retval None\r
298   */\r
299 void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
300 {\r
301   /* Check the parameters */\r
302   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
303   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
304   if (NewState != DISABLE)\r
305   {\r
306     /* Set the ADON bit to wake up the ADC from power down mode */\r
307     ADCx->CR2 |= CR2_ADON_Set;\r
308   }\r
309   else\r
310   {\r
311     /* Disable the selected ADC peripheral */\r
312     ADCx->CR2 &= CR2_ADON_Reset;\r
313   }\r
314 }\r
315 \r
316 /**\r
317   * @brief  Enables or disables the specified ADC DMA request.\r
318   * @param  ADCx: where x can be 1 or 3 to select the ADC peripheral.\r
319   *   Note: ADC2 hasn't a DMA capability.\r
320   * @param  NewState: new state of the selected ADC DMA transfer.\r
321   *   This parameter can be: ENABLE or DISABLE.\r
322   * @retval None\r
323   */\r
324 void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
325 {\r
326   /* Check the parameters */\r
327   assert_param(IS_ADC_DMA_PERIPH(ADCx));\r
328   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
329   if (NewState != DISABLE)\r
330   {\r
331     /* Enable the selected ADC DMA request */\r
332     ADCx->CR2 |= CR2_DMA_Set;\r
333   }\r
334   else\r
335   {\r
336     /* Disable the selected ADC DMA request */\r
337     ADCx->CR2 &= CR2_DMA_Reset;\r
338   }\r
339 }\r
340 \r
341 /**\r
342   * @brief  Enables or disables the specified ADC interrupts.\r
343   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
344   * @param  ADC_IT: specifies the ADC interrupt sources to be enabled or disabled. \r
345   *   This parameter can be any combination of the following values:\r
346   *     @arg ADC_IT_EOC: End of conversion interrupt mask\r
347   *     @arg ADC_IT_AWD: Analog watchdog interrupt mask\r
348   *     @arg ADC_IT_JEOC: End of injected conversion interrupt mask\r
349   * @param  NewState: new state of the specified ADC interrupts.\r
350   *   This parameter can be: ENABLE or DISABLE.\r
351   * @retval None\r
352   */\r
353 void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState)\r
354 {\r
355   uint8_t itmask = 0;\r
356   /* Check the parameters */\r
357   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
358   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
359   assert_param(IS_ADC_IT(ADC_IT));\r
360   /* Get the ADC IT index */\r
361   itmask = (uint8_t)ADC_IT;\r
362   if (NewState != DISABLE)\r
363   {\r
364     /* Enable the selected ADC interrupts */\r
365     ADCx->CR1 |= itmask;\r
366   }\r
367   else\r
368   {\r
369     /* Disable the selected ADC interrupts */\r
370     ADCx->CR1 &= (~(uint32_t)itmask);\r
371   }\r
372 }\r
373 \r
374 /**\r
375   * @brief  Resets the selected ADC calibration registers.\r
376   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
377   * @retval None\r
378   */\r
379 void ADC_ResetCalibration(ADC_TypeDef* ADCx)\r
380 {\r
381   /* Check the parameters */\r
382   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
383   /* Resets the selected ADC calibration registers */  \r
384   ADCx->CR2 |= CR2_RSTCAL_Set;\r
385 }\r
386 \r
387 /**\r
388   * @brief  Gets the selected ADC reset calibration registers status.\r
389   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
390   * @retval The new state of ADC reset calibration registers (SET or RESET).\r
391   */\r
392 FlagStatus ADC_GetResetCalibrationStatus(ADC_TypeDef* ADCx)\r
393 {\r
394   FlagStatus bitstatus = RESET;\r
395   /* Check the parameters */\r
396   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
397   /* Check the status of RSTCAL bit */\r
398   if ((ADCx->CR2 & CR2_RSTCAL_Set) != (uint32_t)RESET)\r
399   {\r
400     /* RSTCAL bit is set */\r
401     bitstatus = SET;\r
402   }\r
403   else\r
404   {\r
405     /* RSTCAL bit is reset */\r
406     bitstatus = RESET;\r
407   }\r
408   /* Return the RSTCAL bit status */\r
409   return  bitstatus;\r
410 }\r
411 \r
412 /**\r
413   * @brief  Starts the selected ADC calibration process.\r
414   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
415   * @retval None\r
416   */\r
417 void ADC_StartCalibration(ADC_TypeDef* ADCx)\r
418 {\r
419   /* Check the parameters */\r
420   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
421   /* Enable the selected ADC calibration process */  \r
422   ADCx->CR2 |= CR2_CAL_Set;\r
423 }\r
424 \r
425 /**\r
426   * @brief  Gets the selected ADC calibration status.\r
427   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
428   * @retval The new state of ADC calibration (SET or RESET).\r
429   */\r
430 FlagStatus ADC_GetCalibrationStatus(ADC_TypeDef* ADCx)\r
431 {\r
432   FlagStatus bitstatus = RESET;\r
433   /* Check the parameters */\r
434   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
435   /* Check the status of CAL bit */\r
436   if ((ADCx->CR2 & CR2_CAL_Set) != (uint32_t)RESET)\r
437   {\r
438     /* CAL bit is set: calibration on going */\r
439     bitstatus = SET;\r
440   }\r
441   else\r
442   {\r
443     /* CAL bit is reset: end of calibration */\r
444     bitstatus = RESET;\r
445   }\r
446   /* Return the CAL bit status */\r
447   return  bitstatus;\r
448 }\r
449 \r
450 /**\r
451   * @brief  Enables or disables the selected ADC software start conversion .\r
452   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
453   * @param  NewState: new state of the selected ADC software start conversion.\r
454   *   This parameter can be: ENABLE or DISABLE.\r
455   * @retval None\r
456   */\r
457 void ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
458 {\r
459   /* Check the parameters */\r
460   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
461   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
462   if (NewState != DISABLE)\r
463   {\r
464     /* Enable the selected ADC conversion on external event and start the selected\r
465        ADC conversion */\r
466     ADCx->CR2 |= CR2_EXTTRIG_SWSTART_Set;\r
467   }\r
468   else\r
469   {\r
470     /* Disable the selected ADC conversion on external event and stop the selected\r
471        ADC conversion */\r
472     ADCx->CR2 &= CR2_EXTTRIG_SWSTART_Reset;\r
473   }\r
474 }\r
475 \r
476 /**\r
477   * @brief  Gets the selected ADC Software start conversion Status.\r
478   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
479   * @retval The new state of ADC software start conversion (SET or RESET).\r
480   */\r
481 FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx)\r
482 {\r
483   FlagStatus bitstatus = RESET;\r
484   /* Check the parameters */\r
485   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
486   /* Check the status of SWSTART bit */\r
487   if ((ADCx->CR2 & CR2_SWSTART_Set) != (uint32_t)RESET)\r
488   {\r
489     /* SWSTART bit is set */\r
490     bitstatus = SET;\r
491   }\r
492   else\r
493   {\r
494     /* SWSTART bit is reset */\r
495     bitstatus = RESET;\r
496   }\r
497   /* Return the SWSTART bit status */\r
498   return  bitstatus;\r
499 }\r
500 \r
501 /**\r
502   * @brief  Configures the discontinuous mode for the selected ADC regular\r
503   *         group channel.\r
504   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
505   * @param  Number: specifies the discontinuous mode regular channel\r
506   *         count value. This number must be between 1 and 8.\r
507   * @retval None\r
508   */\r
509 void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, uint8_t Number)\r
510 {\r
511   uint32_t tmpreg1 = 0;\r
512   uint32_t tmpreg2 = 0;\r
513   /* Check the parameters */\r
514   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
515   assert_param(IS_ADC_REGULAR_DISC_NUMBER(Number));\r
516   /* Get the old register value */\r
517   tmpreg1 = ADCx->CR1;\r
518   /* Clear the old discontinuous mode channel count */\r
519   tmpreg1 &= CR1_DISCNUM_Reset;\r
520   /* Set the discontinuous mode channel count */\r
521   tmpreg2 = Number - 1;\r
522   tmpreg1 |= tmpreg2 << 13;\r
523   /* Store the new register value */\r
524   ADCx->CR1 = tmpreg1;\r
525 }\r
526 \r
527 /**\r
528   * @brief  Enables or disables the discontinuous mode on regular group\r
529   *         channel for the specified ADC\r
530   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
531   * @param  NewState: new state of the selected ADC discontinuous mode\r
532   *         on regular group channel.\r
533   *         This parameter can be: ENABLE or DISABLE.\r
534   * @retval None\r
535   */\r
536 void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
537 {\r
538   /* Check the parameters */\r
539   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
540   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
541   if (NewState != DISABLE)\r
542   {\r
543     /* Enable the selected ADC regular discontinuous mode */\r
544     ADCx->CR1 |= CR1_DISCEN_Set;\r
545   }\r
546   else\r
547   {\r
548     /* Disable the selected ADC regular discontinuous mode */\r
549     ADCx->CR1 &= CR1_DISCEN_Reset;\r
550   }\r
551 }\r
552 \r
553 /**\r
554   * @brief  Configures for the selected ADC regular channel its corresponding\r
555   *         rank in the sequencer and its sample time.\r
556   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
557   * @param  ADC_Channel: the ADC channel to configure. \r
558   *   This parameter can be one of the following values:\r
559   *     @arg ADC_Channel_0: ADC Channel0 selected\r
560   *     @arg ADC_Channel_1: ADC Channel1 selected\r
561   *     @arg ADC_Channel_2: ADC Channel2 selected\r
562   *     @arg ADC_Channel_3: ADC Channel3 selected\r
563   *     @arg ADC_Channel_4: ADC Channel4 selected\r
564   *     @arg ADC_Channel_5: ADC Channel5 selected\r
565   *     @arg ADC_Channel_6: ADC Channel6 selected\r
566   *     @arg ADC_Channel_7: ADC Channel7 selected\r
567   *     @arg ADC_Channel_8: ADC Channel8 selected\r
568   *     @arg ADC_Channel_9: ADC Channel9 selected\r
569   *     @arg ADC_Channel_10: ADC Channel10 selected\r
570   *     @arg ADC_Channel_11: ADC Channel11 selected\r
571   *     @arg ADC_Channel_12: ADC Channel12 selected\r
572   *     @arg ADC_Channel_13: ADC Channel13 selected\r
573   *     @arg ADC_Channel_14: ADC Channel14 selected\r
574   *     @arg ADC_Channel_15: ADC Channel15 selected\r
575   *     @arg ADC_Channel_16: ADC Channel16 selected\r
576   *     @arg ADC_Channel_17: ADC Channel17 selected\r
577   * @param  Rank: The rank in the regular group sequencer. This parameter must be between 1 to 16.\r
578   * @param  ADC_SampleTime: The sample time value to be set for the selected channel. \r
579   *   This parameter can be one of the following values:\r
580   *     @arg ADC_SampleTime_1Cycles5: Sample time equal to 1.5 cycles\r
581   *     @arg ADC_SampleTime_7Cycles5: Sample time equal to 7.5 cycles\r
582   *     @arg ADC_SampleTime_13Cycles5: Sample time equal to 13.5 cycles\r
583   *     @arg ADC_SampleTime_28Cycles5: Sample time equal to 28.5 cycles \r
584   *     @arg ADC_SampleTime_41Cycles5: Sample time equal to 41.5 cycles \r
585   *     @arg ADC_SampleTime_55Cycles5: Sample time equal to 55.5 cycles \r
586   *     @arg ADC_SampleTime_71Cycles5: Sample time equal to 71.5 cycles \r
587   *     @arg ADC_SampleTime_239Cycles5: Sample time equal to 239.5 cycles       \r
588   * @retval None\r
589   */\r
590 void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)\r
591 {\r
592   uint32_t tmpreg1 = 0, tmpreg2 = 0;\r
593   /* Check the parameters */\r
594   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
595   assert_param(IS_ADC_CHANNEL(ADC_Channel));\r
596   assert_param(IS_ADC_REGULAR_RANK(Rank));\r
597   assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));\r
598   /* if ADC_Channel_10 ... ADC_Channel_17 is selected */\r
599   if (ADC_Channel > ADC_Channel_9)\r
600   {\r
601     /* Get the old register value */\r
602     tmpreg1 = ADCx->SMPR1;\r
603     /* Calculate the mask to clear */\r
604     tmpreg2 = SMPR1_SMP_Set << (3 * (ADC_Channel - 10));\r
605     /* Clear the old channel sample time */\r
606     tmpreg1 &= ~tmpreg2;\r
607     /* Calculate the mask to set */\r
608     tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));\r
609     /* Set the new channel sample time */\r
610     tmpreg1 |= tmpreg2;\r
611     /* Store the new register value */\r
612     ADCx->SMPR1 = tmpreg1;\r
613   }\r
614   else /* ADC_Channel include in ADC_Channel_[0..9] */\r
615   {\r
616     /* Get the old register value */\r
617     tmpreg1 = ADCx->SMPR2;\r
618     /* Calculate the mask to clear */\r
619     tmpreg2 = SMPR2_SMP_Set << (3 * ADC_Channel);\r
620     /* Clear the old channel sample time */\r
621     tmpreg1 &= ~tmpreg2;\r
622     /* Calculate the mask to set */\r
623     tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);\r
624     /* Set the new channel sample time */\r
625     tmpreg1 |= tmpreg2;\r
626     /* Store the new register value */\r
627     ADCx->SMPR2 = tmpreg1;\r
628   }\r
629   /* For Rank 1 to 6 */\r
630   if (Rank < 7)\r
631   {\r
632     /* Get the old register value */\r
633     tmpreg1 = ADCx->SQR3;\r
634     /* Calculate the mask to clear */\r
635     tmpreg2 = SQR3_SQ_Set << (5 * (Rank - 1));\r
636     /* Clear the old SQx bits for the selected rank */\r
637     tmpreg1 &= ~tmpreg2;\r
638     /* Calculate the mask to set */\r
639     tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1));\r
640     /* Set the SQx bits for the selected rank */\r
641     tmpreg1 |= tmpreg2;\r
642     /* Store the new register value */\r
643     ADCx->SQR3 = tmpreg1;\r
644   }\r
645   /* For Rank 7 to 12 */\r
646   else if (Rank < 13)\r
647   {\r
648     /* Get the old register value */\r
649     tmpreg1 = ADCx->SQR2;\r
650     /* Calculate the mask to clear */\r
651     tmpreg2 = SQR2_SQ_Set << (5 * (Rank - 7));\r
652     /* Clear the old SQx bits for the selected rank */\r
653     tmpreg1 &= ~tmpreg2;\r
654     /* Calculate the mask to set */\r
655     tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7));\r
656     /* Set the SQx bits for the selected rank */\r
657     tmpreg1 |= tmpreg2;\r
658     /* Store the new register value */\r
659     ADCx->SQR2 = tmpreg1;\r
660   }\r
661   /* For Rank 13 to 16 */\r
662   else\r
663   {\r
664     /* Get the old register value */\r
665     tmpreg1 = ADCx->SQR1;\r
666     /* Calculate the mask to clear */\r
667     tmpreg2 = SQR1_SQ_Set << (5 * (Rank - 13));\r
668     /* Clear the old SQx bits for the selected rank */\r
669     tmpreg1 &= ~tmpreg2;\r
670     /* Calculate the mask to set */\r
671     tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13));\r
672     /* Set the SQx bits for the selected rank */\r
673     tmpreg1 |= tmpreg2;\r
674     /* Store the new register value */\r
675     ADCx->SQR1 = tmpreg1;\r
676   }\r
677 }\r
678 \r
679 /**\r
680   * @brief  Enables or disables the ADCx conversion through external trigger.\r
681   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
682   * @param  NewState: new state of the selected ADC external trigger start of conversion.\r
683   *   This parameter can be: ENABLE or DISABLE.\r
684   * @retval None\r
685   */\r
686 void ADC_ExternalTrigConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
687 {\r
688   /* Check the parameters */\r
689   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
690   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
691   if (NewState != DISABLE)\r
692   {\r
693     /* Enable the selected ADC conversion on external event */\r
694     ADCx->CR2 |= CR2_EXTTRIG_Set;\r
695   }\r
696   else\r
697   {\r
698     /* Disable the selected ADC conversion on external event */\r
699     ADCx->CR2 &= CR2_EXTTRIG_Reset;\r
700   }\r
701 }\r
702 \r
703 /**\r
704   * @brief  Returns the last ADCx conversion result data for regular channel.\r
705   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
706   * @retval The Data conversion value.\r
707   */\r
708 uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)\r
709 {\r
710   /* Check the parameters */\r
711   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
712   /* Return the selected ADC conversion value */\r
713   return (uint16_t) ADCx->DR;\r
714 }\r
715 \r
716 /**\r
717   * @brief  Returns the last ADC1 and ADC2 conversion result data in dual mode.\r
718   * @retval The Data conversion value.\r
719   */\r
720 uint32_t ADC_GetDualModeConversionValue(void)\r
721 {\r
722   /* Return the dual mode conversion value */\r
723   return (*(__IO uint32_t *) DR_ADDRESS);\r
724 }\r
725 \r
726 /**\r
727   * @brief  Enables or disables the selected ADC automatic injected group\r
728   *         conversion after regular one.\r
729   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
730   * @param  NewState: new state of the selected ADC auto injected conversion\r
731   *   This parameter can be: ENABLE or DISABLE.\r
732   * @retval None\r
733   */\r
734 void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
735 {\r
736   /* Check the parameters */\r
737   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
738   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
739   if (NewState != DISABLE)\r
740   {\r
741     /* Enable the selected ADC automatic injected group conversion */\r
742     ADCx->CR1 |= CR1_JAUTO_Set;\r
743   }\r
744   else\r
745   {\r
746     /* Disable the selected ADC automatic injected group conversion */\r
747     ADCx->CR1 &= CR1_JAUTO_Reset;\r
748   }\r
749 }\r
750 \r
751 /**\r
752   * @brief  Enables or disables the discontinuous mode for injected group\r
753   *         channel for the specified ADC\r
754   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
755   * @param  NewState: new state of the selected ADC discontinuous mode\r
756   *         on injected group channel.\r
757   *   This parameter can be: ENABLE or DISABLE.\r
758   * @retval None\r
759   */\r
760 void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
761 {\r
762   /* Check the parameters */\r
763   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
764   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
765   if (NewState != DISABLE)\r
766   {\r
767     /* Enable the selected ADC injected discontinuous mode */\r
768     ADCx->CR1 |= CR1_JDISCEN_Set;\r
769   }\r
770   else\r
771   {\r
772     /* Disable the selected ADC injected discontinuous mode */\r
773     ADCx->CR1 &= CR1_JDISCEN_Reset;\r
774   }\r
775 }\r
776 \r
777 /**\r
778   * @brief  Configures the ADCx external trigger for injected channels conversion.\r
779   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
780   * @param  ADC_ExternalTrigInjecConv: specifies the ADC trigger to start injected conversion. \r
781   *   This parameter can be one of the following values:\r
782   *     @arg ADC_ExternalTrigInjecConv_T1_TRGO: Timer1 TRGO event selected (for ADC1, ADC2 and ADC3)\r
783   *     @arg ADC_ExternalTrigInjecConv_T1_CC4: Timer1 capture compare4 selected (for ADC1, ADC2 and ADC3)\r
784   *     @arg ADC_ExternalTrigInjecConv_T2_TRGO: Timer2 TRGO event selected (for ADC1 and ADC2)\r
785   *     @arg ADC_ExternalTrigInjecConv_T2_CC1: Timer2 capture compare1 selected (for ADC1 and ADC2)\r
786   *     @arg ADC_ExternalTrigInjecConv_T3_CC4: Timer3 capture compare4 selected (for ADC1 and ADC2)\r
787   *     @arg ADC_ExternalTrigInjecConv_T4_TRGO: Timer4 TRGO event selected (for ADC1 and ADC2)\r
788   *     @arg ADC_ExternalTrigInjecConv_Ext_IT15_TIM8_CC4: External interrupt line 15 or Timer8\r
789   *                                                       capture compare4 event selected (for ADC1 and ADC2)                       \r
790   *     @arg ADC_ExternalTrigInjecConv_T4_CC3: Timer4 capture compare3 selected (for ADC3 only)\r
791   *     @arg ADC_ExternalTrigInjecConv_T8_CC2: Timer8 capture compare2 selected (for ADC3 only)                         \r
792   *     @arg ADC_ExternalTrigInjecConv_T8_CC4: Timer8 capture compare4 selected (for ADC3 only)\r
793   *     @arg ADC_ExternalTrigInjecConv_T5_TRGO: Timer5 TRGO event selected (for ADC3 only)                         \r
794   *     @arg ADC_ExternalTrigInjecConv_T5_CC4: Timer5 capture compare4 selected (for ADC3 only)                        \r
795   *     @arg ADC_ExternalTrigInjecConv_None: Injected conversion started by software and not\r
796   *                                          by external trigger (for ADC1, ADC2 and ADC3)\r
797   * @retval None\r
798   */\r
799 void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv)\r
800 {\r
801   uint32_t tmpreg = 0;\r
802   /* Check the parameters */\r
803   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
804   assert_param(IS_ADC_EXT_INJEC_TRIG(ADC_ExternalTrigInjecConv));\r
805   /* Get the old register value */\r
806   tmpreg = ADCx->CR2;\r
807   /* Clear the old external event selection for injected group */\r
808   tmpreg &= CR2_JEXTSEL_Reset;\r
809   /* Set the external event selection for injected group */\r
810   tmpreg |= ADC_ExternalTrigInjecConv;\r
811   /* Store the new register value */\r
812   ADCx->CR2 = tmpreg;\r
813 }\r
814 \r
815 /**\r
816   * @brief  Enables or disables the ADCx injected channels conversion through\r
817   *         external trigger\r
818   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
819   * @param  NewState: new state of the selected ADC external trigger start of\r
820   *         injected conversion.\r
821   *   This parameter can be: ENABLE or DISABLE.\r
822   * @retval None\r
823   */\r
824 void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
825 {\r
826   /* Check the parameters */\r
827   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
828   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
829   if (NewState != DISABLE)\r
830   {\r
831     /* Enable the selected ADC external event selection for injected group */\r
832     ADCx->CR2 |= CR2_JEXTTRIG_Set;\r
833   }\r
834   else\r
835   {\r
836     /* Disable the selected ADC external event selection for injected group */\r
837     ADCx->CR2 &= CR2_JEXTTRIG_Reset;\r
838   }\r
839 }\r
840 \r
841 /**\r
842   * @brief  Enables or disables the selected ADC start of the injected \r
843   *         channels conversion.\r
844   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
845   * @param  NewState: new state of the selected ADC software start injected conversion.\r
846   *   This parameter can be: ENABLE or DISABLE.\r
847   * @retval None\r
848   */\r
849 void ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
850 {\r
851   /* Check the parameters */\r
852   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
853   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
854   if (NewState != DISABLE)\r
855   {\r
856     /* Enable the selected ADC conversion for injected group on external event and start the selected\r
857        ADC injected conversion */\r
858     ADCx->CR2 |= CR2_JEXTTRIG_JSWSTART_Set;\r
859   }\r
860   else\r
861   {\r
862     /* Disable the selected ADC conversion on external event for injected group and stop the selected\r
863        ADC injected conversion */\r
864     ADCx->CR2 &= CR2_JEXTTRIG_JSWSTART_Reset;\r
865   }\r
866 }\r
867 \r
868 /**\r
869   * @brief  Gets the selected ADC Software start injected conversion Status.\r
870   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
871   * @retval The new state of ADC software start injected conversion (SET or RESET).\r
872   */\r
873 FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx)\r
874 {\r
875   FlagStatus bitstatus = RESET;\r
876   /* Check the parameters */\r
877   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
878   /* Check the status of JSWSTART bit */\r
879   if ((ADCx->CR2 & CR2_JSWSTART_Set) != (uint32_t)RESET)\r
880   {\r
881     /* JSWSTART bit is set */\r
882     bitstatus = SET;\r
883   }\r
884   else\r
885   {\r
886     /* JSWSTART bit is reset */\r
887     bitstatus = RESET;\r
888   }\r
889   /* Return the JSWSTART bit status */\r
890   return  bitstatus;\r
891 }\r
892 \r
893 /**\r
894   * @brief  Configures for the selected ADC injected channel its corresponding\r
895   *         rank in the sequencer and its sample time.\r
896   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
897   * @param  ADC_Channel: the ADC channel to configure. \r
898   *   This parameter can be one of the following values:\r
899   *     @arg ADC_Channel_0: ADC Channel0 selected\r
900   *     @arg ADC_Channel_1: ADC Channel1 selected\r
901   *     @arg ADC_Channel_2: ADC Channel2 selected\r
902   *     @arg ADC_Channel_3: ADC Channel3 selected\r
903   *     @arg ADC_Channel_4: ADC Channel4 selected\r
904   *     @arg ADC_Channel_5: ADC Channel5 selected\r
905   *     @arg ADC_Channel_6: ADC Channel6 selected\r
906   *     @arg ADC_Channel_7: ADC Channel7 selected\r
907   *     @arg ADC_Channel_8: ADC Channel8 selected\r
908   *     @arg ADC_Channel_9: ADC Channel9 selected\r
909   *     @arg ADC_Channel_10: ADC Channel10 selected\r
910   *     @arg ADC_Channel_11: ADC Channel11 selected\r
911   *     @arg ADC_Channel_12: ADC Channel12 selected\r
912   *     @arg ADC_Channel_13: ADC Channel13 selected\r
913   *     @arg ADC_Channel_14: ADC Channel14 selected\r
914   *     @arg ADC_Channel_15: ADC Channel15 selected\r
915   *     @arg ADC_Channel_16: ADC Channel16 selected\r
916   *     @arg ADC_Channel_17: ADC Channel17 selected\r
917   * @param  Rank: The rank in the injected group sequencer. This parameter must be between 1 and 4.\r
918   * @param  ADC_SampleTime: The sample time value to be set for the selected channel. \r
919   *   This parameter can be one of the following values:\r
920   *     @arg ADC_SampleTime_1Cycles5: Sample time equal to 1.5 cycles\r
921   *     @arg ADC_SampleTime_7Cycles5: Sample time equal to 7.5 cycles\r
922   *     @arg ADC_SampleTime_13Cycles5: Sample time equal to 13.5 cycles\r
923   *     @arg ADC_SampleTime_28Cycles5: Sample time equal to 28.5 cycles \r
924   *     @arg ADC_SampleTime_41Cycles5: Sample time equal to 41.5 cycles \r
925   *     @arg ADC_SampleTime_55Cycles5: Sample time equal to 55.5 cycles \r
926   *     @arg ADC_SampleTime_71Cycles5: Sample time equal to 71.5 cycles \r
927   *     @arg ADC_SampleTime_239Cycles5: Sample time equal to 239.5 cycles       \r
928   * @retval None\r
929   */\r
930 void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)\r
931 {\r
932   uint32_t tmpreg1 = 0, tmpreg2 = 0, tmpreg3 = 0;\r
933   /* Check the parameters */\r
934   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
935   assert_param(IS_ADC_CHANNEL(ADC_Channel));\r
936   assert_param(IS_ADC_INJECTED_RANK(Rank));\r
937   assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));\r
938   /* if ADC_Channel_10 ... ADC_Channel_17 is selected */\r
939   if (ADC_Channel > ADC_Channel_9)\r
940   {\r
941     /* Get the old register value */\r
942     tmpreg1 = ADCx->SMPR1;\r
943     /* Calculate the mask to clear */\r
944     tmpreg2 = SMPR1_SMP_Set << (3*(ADC_Channel - 10));\r
945     /* Clear the old channel sample time */\r
946     tmpreg1 &= ~tmpreg2;\r
947     /* Calculate the mask to set */\r
948     tmpreg2 = (uint32_t)ADC_SampleTime << (3*(ADC_Channel - 10));\r
949     /* Set the new channel sample time */\r
950     tmpreg1 |= tmpreg2;\r
951     /* Store the new register value */\r
952     ADCx->SMPR1 = tmpreg1;\r
953   }\r
954   else /* ADC_Channel include in ADC_Channel_[0..9] */\r
955   {\r
956     /* Get the old register value */\r
957     tmpreg1 = ADCx->SMPR2;\r
958     /* Calculate the mask to clear */\r
959     tmpreg2 = SMPR2_SMP_Set << (3 * ADC_Channel);\r
960     /* Clear the old channel sample time */\r
961     tmpreg1 &= ~tmpreg2;\r
962     /* Calculate the mask to set */\r
963     tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);\r
964     /* Set the new channel sample time */\r
965     tmpreg1 |= tmpreg2;\r
966     /* Store the new register value */\r
967     ADCx->SMPR2 = tmpreg1;\r
968   }\r
969   /* Rank configuration */\r
970   /* Get the old register value */\r
971   tmpreg1 = ADCx->JSQR;\r
972   /* Get JL value: Number = JL+1 */\r
973   tmpreg3 =  (tmpreg1 & JSQR_JL_Set)>> 20;\r
974   /* Calculate the mask to clear: ((Rank-1)+(4-JL-1)) */\r
975   tmpreg2 = JSQR_JSQ_Set << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));\r
976   /* Clear the old JSQx bits for the selected rank */\r
977   tmpreg1 &= ~tmpreg2;\r
978   /* Calculate the mask to set: ((Rank-1)+(4-JL-1)) */\r
979   tmpreg2 = (uint32_t)ADC_Channel << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));\r
980   /* Set the JSQx bits for the selected rank */\r
981   tmpreg1 |= tmpreg2;\r
982   /* Store the new register value */\r
983   ADCx->JSQR = tmpreg1;\r
984 }\r
985 \r
986 /**\r
987   * @brief  Configures the sequencer length for injected channels\r
988   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
989   * @param  Length: The sequencer length. \r
990   *   This parameter must be a number between 1 to 4.\r
991   * @retval None\r
992   */\r
993 void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length)\r
994 {\r
995   uint32_t tmpreg1 = 0;\r
996   uint32_t tmpreg2 = 0;\r
997   /* Check the parameters */\r
998   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
999   assert_param(IS_ADC_INJECTED_LENGTH(Length));\r
1000   \r
1001   /* Get the old register value */\r
1002   tmpreg1 = ADCx->JSQR;\r
1003   /* Clear the old injected sequnence lenght JL bits */\r
1004   tmpreg1 &= JSQR_JL_Reset;\r
1005   /* Set the injected sequnence lenght JL bits */\r
1006   tmpreg2 = Length - 1; \r
1007   tmpreg1 |= tmpreg2 << 20;\r
1008   /* Store the new register value */\r
1009   ADCx->JSQR = tmpreg1;\r
1010 }\r
1011 \r
1012 /**\r
1013   * @brief  Set the injected channels conversion value offset\r
1014   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
1015   * @param  ADC_InjectedChannel: the ADC injected channel to set its offset. \r
1016   *   This parameter can be one of the following values:\r
1017   *     @arg ADC_InjectedChannel_1: Injected Channel1 selected\r
1018   *     @arg ADC_InjectedChannel_2: Injected Channel2 selected\r
1019   *     @arg ADC_InjectedChannel_3: Injected Channel3 selected\r
1020   *     @arg ADC_InjectedChannel_4: Injected Channel4 selected\r
1021   * @param  Offset: the offset value for the selected ADC injected channel\r
1022   *   This parameter must be a 12bit value.\r
1023   * @retval None\r
1024   */\r
1025 void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset)\r
1026 {\r
1027   __IO uint32_t tmp = 0;\r
1028   \r
1029   /* Check the parameters */\r
1030   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1031   assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));\r
1032   assert_param(IS_ADC_OFFSET(Offset));  \r
1033   \r
1034   tmp = (uint32_t)ADCx;\r
1035   tmp += ADC_InjectedChannel;\r
1036   \r
1037   /* Set the selected injected channel data offset */\r
1038   *(__IO uint32_t *) tmp = (uint32_t)Offset;\r
1039 }\r
1040 \r
1041 /**\r
1042   * @brief  Returns the ADC injected channel conversion result\r
1043   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
1044   * @param  ADC_InjectedChannel: the converted ADC injected channel.\r
1045   *   This parameter can be one of the following values:\r
1046   *     @arg ADC_InjectedChannel_1: Injected Channel1 selected\r
1047   *     @arg ADC_InjectedChannel_2: Injected Channel2 selected\r
1048   *     @arg ADC_InjectedChannel_3: Injected Channel3 selected\r
1049   *     @arg ADC_InjectedChannel_4: Injected Channel4 selected\r
1050   * @retval The Data conversion value.\r
1051   */\r
1052 uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel)\r
1053 {\r
1054   __IO uint32_t tmp = 0;\r
1055   \r
1056   /* Check the parameters */\r
1057   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1058   assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));\r
1059 \r
1060   tmp = (uint32_t)ADCx;\r
1061   tmp += ADC_InjectedChannel + JDR_Offset;\r
1062   \r
1063   /* Returns the selected injected channel conversion data value */\r
1064   return (uint16_t) (*(__IO uint32_t*)  tmp);   \r
1065 }\r
1066 \r
1067 /**\r
1068   * @brief  Enables or disables the analog watchdog on single/all regular\r
1069   *         or injected channels\r
1070   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
1071   * @param  ADC_AnalogWatchdog: the ADC analog watchdog configuration.\r
1072   *   This parameter can be one of the following values:\r
1073   *     @arg ADC_AnalogWatchdog_SingleRegEnable: Analog watchdog on a single regular channel\r
1074   *     @arg ADC_AnalogWatchdog_SingleInjecEnable: Analog watchdog on a single injected channel\r
1075   *     @arg ADC_AnalogWatchdog_SingleRegOrInjecEnable: Analog watchdog on a single regular or injected channel\r
1076   *     @arg ADC_AnalogWatchdog_AllRegEnable: Analog watchdog on  all regular channel\r
1077   *     @arg ADC_AnalogWatchdog_AllInjecEnable: Analog watchdog on  all injected channel\r
1078   *     @arg ADC_AnalogWatchdog_AllRegAllInjecEnable: Analog watchdog on all regular and injected channels\r
1079   *     @arg ADC_AnalogWatchdog_None: No channel guarded by the analog watchdog\r
1080   * @retval None          \r
1081   */\r
1082 void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog)\r
1083 {\r
1084   uint32_t tmpreg = 0;\r
1085   /* Check the parameters */\r
1086   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1087   assert_param(IS_ADC_ANALOG_WATCHDOG(ADC_AnalogWatchdog));\r
1088   /* Get the old register value */\r
1089   tmpreg = ADCx->CR1;\r
1090   /* Clear AWDEN, AWDENJ and AWDSGL bits */\r
1091   tmpreg &= CR1_AWDMode_Reset;\r
1092   /* Set the analog watchdog enable mode */\r
1093   tmpreg |= ADC_AnalogWatchdog;\r
1094   /* Store the new register value */\r
1095   ADCx->CR1 = tmpreg;\r
1096 }\r
1097 \r
1098 /**\r
1099   * @brief  Configures the high and low thresholds of the analog watchdog.\r
1100   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
1101   * @param  HighThreshold: the ADC analog watchdog High threshold value.\r
1102   *   This parameter must be a 12bit value.\r
1103   * @param  LowThreshold: the ADC analog watchdog Low threshold value.\r
1104   *   This parameter must be a 12bit value.\r
1105   * @retval None\r
1106   */\r
1107 void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,\r
1108                                         uint16_t LowThreshold)\r
1109 {\r
1110   /* Check the parameters */\r
1111   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1112   assert_param(IS_ADC_THRESHOLD(HighThreshold));\r
1113   assert_param(IS_ADC_THRESHOLD(LowThreshold));\r
1114   /* Set the ADCx high threshold */\r
1115   ADCx->HTR = HighThreshold;\r
1116   /* Set the ADCx low threshold */\r
1117   ADCx->LTR = LowThreshold;\r
1118 }\r
1119 \r
1120 /**\r
1121   * @brief  Configures the analog watchdog guarded single channel\r
1122   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
1123   * @param  ADC_Channel: the ADC channel to configure for the analog watchdog. \r
1124   *   This parameter can be one of the following values:\r
1125   *     @arg ADC_Channel_0: ADC Channel0 selected\r
1126   *     @arg ADC_Channel_1: ADC Channel1 selected\r
1127   *     @arg ADC_Channel_2: ADC Channel2 selected\r
1128   *     @arg ADC_Channel_3: ADC Channel3 selected\r
1129   *     @arg ADC_Channel_4: ADC Channel4 selected\r
1130   *     @arg ADC_Channel_5: ADC Channel5 selected\r
1131   *     @arg ADC_Channel_6: ADC Channel6 selected\r
1132   *     @arg ADC_Channel_7: ADC Channel7 selected\r
1133   *     @arg ADC_Channel_8: ADC Channel8 selected\r
1134   *     @arg ADC_Channel_9: ADC Channel9 selected\r
1135   *     @arg ADC_Channel_10: ADC Channel10 selected\r
1136   *     @arg ADC_Channel_11: ADC Channel11 selected\r
1137   *     @arg ADC_Channel_12: ADC Channel12 selected\r
1138   *     @arg ADC_Channel_13: ADC Channel13 selected\r
1139   *     @arg ADC_Channel_14: ADC Channel14 selected\r
1140   *     @arg ADC_Channel_15: ADC Channel15 selected\r
1141   *     @arg ADC_Channel_16: ADC Channel16 selected\r
1142   *     @arg ADC_Channel_17: ADC Channel17 selected\r
1143   * @retval None\r
1144   */\r
1145 void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel)\r
1146 {\r
1147   uint32_t tmpreg = 0;\r
1148   /* Check the parameters */\r
1149   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1150   assert_param(IS_ADC_CHANNEL(ADC_Channel));\r
1151   /* Get the old register value */\r
1152   tmpreg = ADCx->CR1;\r
1153   /* Clear the Analog watchdog channel select bits */\r
1154   tmpreg &= CR1_AWDCH_Reset;\r
1155   /* Set the Analog watchdog channel */\r
1156   tmpreg |= ADC_Channel;\r
1157   /* Store the new register value */\r
1158   ADCx->CR1 = tmpreg;\r
1159 }\r
1160 \r
1161 /**\r
1162   * @brief  Enables or disables the temperature sensor and Vrefint channel.\r
1163   * @param  NewState: new state of the temperature sensor.\r
1164   *   This parameter can be: ENABLE or DISABLE.\r
1165   * @retval None\r
1166   */\r
1167 void ADC_TempSensorVrefintCmd(FunctionalState NewState)\r
1168 {\r
1169   /* Check the parameters */\r
1170   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1171   if (NewState != DISABLE)\r
1172   {\r
1173     /* Enable the temperature sensor and Vrefint channel*/\r
1174     ADC1->CR2 |= CR2_TSVREFE_Set;\r
1175   }\r
1176   else\r
1177   {\r
1178     /* Disable the temperature sensor and Vrefint channel*/\r
1179     ADC1->CR2 &= CR2_TSVREFE_Reset;\r
1180   }\r
1181 }\r
1182 \r
1183 /**\r
1184   * @brief  Checks whether the specified ADC flag is set or not.\r
1185   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
1186   * @param  ADC_FLAG: specifies the flag to check. \r
1187   *   This parameter can be one of the following values:\r
1188   *     @arg ADC_FLAG_AWD: Analog watchdog flag\r
1189   *     @arg ADC_FLAG_EOC: End of conversion flag\r
1190   *     @arg ADC_FLAG_JEOC: End of injected group conversion flag\r
1191   *     @arg ADC_FLAG_JSTRT: Start of injected group conversion flag\r
1192   *     @arg ADC_FLAG_STRT: Start of regular group conversion flag\r
1193   * @retval The new state of ADC_FLAG (SET or RESET).\r
1194   */\r
1195 FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)\r
1196 {\r
1197   FlagStatus bitstatus = RESET;\r
1198   /* Check the parameters */\r
1199   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1200   assert_param(IS_ADC_GET_FLAG(ADC_FLAG));\r
1201   /* Check the status of the specified ADC flag */\r
1202   if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET)\r
1203   {\r
1204     /* ADC_FLAG is set */\r
1205     bitstatus = SET;\r
1206   }\r
1207   else\r
1208   {\r
1209     /* ADC_FLAG is reset */\r
1210     bitstatus = RESET;\r
1211   }\r
1212   /* Return the ADC_FLAG status */\r
1213   return  bitstatus;\r
1214 }\r
1215 \r
1216 /**\r
1217   * @brief  Clears the ADCx's pending flags.\r
1218   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
1219   * @param  ADC_FLAG: specifies the flag to clear. \r
1220   *   This parameter can be any combination of the following values:\r
1221   *     @arg ADC_FLAG_AWD: Analog watchdog flag\r
1222   *     @arg ADC_FLAG_EOC: End of conversion flag\r
1223   *     @arg ADC_FLAG_JEOC: End of injected group conversion flag\r
1224   *     @arg ADC_FLAG_JSTRT: Start of injected group conversion flag\r
1225   *     @arg ADC_FLAG_STRT: Start of regular group conversion flag\r
1226   * @retval None\r
1227   */\r
1228 void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)\r
1229 {\r
1230   /* Check the parameters */\r
1231   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1232   assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));\r
1233   /* Clear the selected ADC flags */\r
1234   ADCx->SR = ~(uint32_t)ADC_FLAG;\r
1235 }\r
1236 \r
1237 /**\r
1238   * @brief  Checks whether the specified ADC interrupt has occurred or not.\r
1239   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
1240   * @param  ADC_IT: specifies the ADC interrupt source to check. \r
1241   *   This parameter can be one of the following values:\r
1242   *     @arg ADC_IT_EOC: End of conversion interrupt mask\r
1243   *     @arg ADC_IT_AWD: Analog watchdog interrupt mask\r
1244   *     @arg ADC_IT_JEOC: End of injected conversion interrupt mask\r
1245   * @retval The new state of ADC_IT (SET or RESET).\r
1246   */\r
1247 ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT)\r
1248 {\r
1249   ITStatus bitstatus = RESET;\r
1250   uint32_t itmask = 0, enablestatus = 0;\r
1251   /* Check the parameters */\r
1252   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1253   assert_param(IS_ADC_GET_IT(ADC_IT));\r
1254   /* Get the ADC IT index */\r
1255   itmask = ADC_IT >> 8;\r
1256   /* Get the ADC_IT enable bit status */\r
1257   enablestatus = (ADCx->CR1 & (uint8_t)ADC_IT) ;\r
1258   /* Check the status of the specified ADC interrupt */\r
1259   if (((ADCx->SR & itmask) != (uint32_t)RESET) && enablestatus)\r
1260   {\r
1261     /* ADC_IT is set */\r
1262     bitstatus = SET;\r
1263   }\r
1264   else\r
1265   {\r
1266     /* ADC_IT is reset */\r
1267     bitstatus = RESET;\r
1268   }\r
1269   /* Return the ADC_IT status */\r
1270   return  bitstatus;\r
1271 }\r
1272 \r
1273 /**\r
1274   * @brief  Clears the ADCx's interrupt pending bits.\r
1275   * @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.\r
1276   * @param  ADC_IT: specifies the ADC interrupt pending bit to clear.\r
1277   *   This parameter can be any combination of the following values:\r
1278   *     @arg ADC_IT_EOC: End of conversion interrupt mask\r
1279   *     @arg ADC_IT_AWD: Analog watchdog interrupt mask\r
1280   *     @arg ADC_IT_JEOC: End of injected conversion interrupt mask\r
1281   * @retval None\r
1282   */\r
1283 void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT)\r
1284 {\r
1285   uint8_t itmask = 0;\r
1286   /* Check the parameters */\r
1287   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1288   assert_param(IS_ADC_IT(ADC_IT));\r
1289   /* Get the ADC IT index */\r
1290   itmask = (uint8_t)(ADC_IT >> 8);\r
1291   /* Clear the selected ADC interrupt pending bits */\r
1292   ADCx->SR = ~(uint32_t)itmask;\r
1293 }\r
1294 \r
1295 /**\r
1296   * @}\r
1297   */\r
1298 \r
1299 /**\r
1300   * @}\r
1301   */\r
1302 \r
1303 /**\r
1304   * @}\r
1305   */\r
1306 \r
1307 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/\r