[add] libstm32l_discovery from ST packages. build directory contains the necessary...
[fw/stlink] / example / libstm32l_discovery / src / stm32l1xx_adc.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_adc.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0\r
6   * @date    31-December-2010\r
7   * @brief   This file provides firmware functions to manage the following \r
8   *          functionalities of the Analog to Digital Convertor (ADC) peripheral:           \r
9   *           - Initialization and Configuration\r
10   *           - Power saving\r
11   *           - Analog Watchdog configuration              \r
12   *           - Temperature Sensor & Vrefint (Voltage Reference internal) management \r
13   *           - Regular Channels Configuration\r
14   *           - Regular Channels DMA Configuration\r
15   *           - Injected channels Configuration      \r
16   *           - Interrupts and flags management       \r
17   *         \r
18   *  @verbatim\r
19   *                               \r
20   *          ===================================================================      \r
21   *                                   How to use this driver\r
22   *          ===================================================================          \r
23   *            - Configure the ADC Prescaler, conversion resolution and data \r
24   *              alignment using the ADC_Init() function.\r
25   *            - Activate the ADC peripheral using ADC_Cmd() function.  \r
26   *\r
27   *          Regular channels group configuration\r
28   *          ====================================    \r
29   *            - To configure the ADC regular channels group features, use \r
30   *              ADC_Init() and ADC_RegularChannelConfig() functions.\r
31   *            - To activate the continuous mode, use the ADC_continuousModeCmd()\r
32   *              function.\r
33   *            - To configurate and activate the Discontinuous mode, use the \r
34   *              ADC_DiscModeChannelCountConfig() and ADC_DiscModeCmd() functions.        \r
35   *            - To read the ADC converted values, use the ADC_GetConversionValue()\r
36   *              function.\r
37   *\r
38   *          DMA for Regular channels group features configuration\r
39   *          ====================================================== \r
40   *           - To enable the DMA mode for regular channels group, use the \r
41   *             ADC_DMACmd() function.\r
42   *           - To enable the generation of DMA requests continuously at the end\r
43   *             of the last DMA transfer, use the ADC_DMARequestAfterLastTransferCmd() \r
44   *             function.    \r
45              \r
46   *          Injected channels group configuration\r
47   *          =====================================    \r
48   *            - To configure the ADC Injected channels group features, use \r
49   *              ADC_InjectedChannelConfig() and  ADC_InjectedSequencerLengthConfig()\r
50   *              functions.\r
51   *            - To activate the continuous mode, use the ADC_continuousModeCmd()\r
52   *              function.\r
53   *            - To activate the Injected Discontinuous mode, use the \r
54   *              ADC_InjectedDiscModeCmd() function.  \r
55   *            - To activate the AutoInjected mode, use the ADC_AutoInjectedConvCmd() \r
56   *              function.        \r
57   *            - To read the ADC converted values, use the ADC_GetInjectedConversionValue() \r
58   *              function.\r
59   *              \r
60   *  @endverbatim\r
61   *         \r
62   ******************************************************************************\r
63   * @attention\r
64   *\r
65   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
66   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
67   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
68   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
69   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
70   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
71   *\r
72   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
73   ******************************************************************************  \r
74   */ \r
75 \r
76 /* Includes ------------------------------------------------------------------*/\r
77 #include "stm32l1xx_adc.h"\r
78 #include "stm32l1xx_rcc.h"\r
79 \r
80 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
81   * @{\r
82   */\r
83 \r
84 /** @defgroup ADC \r
85   * @brief ADC driver modules\r
86   * @{\r
87   */\r
88 \r
89 /* Private typedef -----------------------------------------------------------*/\r
90 /* Private define ------------------------------------------------------------*/\r
91 /* ADC DISCNUM mask */\r
92 #define CR1_DISCNUM_RESET         ((uint32_t)0xFFFF1FFF)\r
93    \r
94 /* ADC AWDCH mask */\r
95 #define CR1_AWDCH_RESET           ((uint32_t)0xFFFFFFE0) \r
96   \r
97 /* ADC Analog watchdog enable mode mask */\r
98 #define CR1_AWDMODE_RESET         ((uint32_t)0xFF3FFDFF)\r
99   \r
100 /* CR1 register Mask */\r
101 #define CR1_CLEAR_MASK            ((uint32_t)0xFCFFFEFF) \r
102    \r
103 /* ADC DELAY mask */            \r
104 #define CR2_DELS_RESET            ((uint32_t)0xFFFFFF0F)\r
105    \r
106 /* ADC JEXTEN mask */\r
107 #define CR2_JEXTEN_RESET          ((uint32_t)0xFFCFFFFF)\r
108   \r
109 /* ADC JEXTSEL mask */\r
110 #define CR2_JEXTSEL_RESET         ((uint32_t)0xFFF0FFFF)\r
111   \r
112 /* CR2 register Mask */\r
113 #define CR2_CLEAR_MASK            ((uint32_t)0xC0FFF7FD)\r
114 \r
115 /* ADC SQx mask */\r
116 #define SQR5_SQ_SET               ((uint32_t)0x0000001F)  \r
117 #define SQR4_SQ_SET               ((uint32_t)0x0000001F)  \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 /* ADC L Mask */\r
123 #define SQR1_L_RESET              ((uint32_t)0xFE0FFFFF) \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 #define SMPR3_SMP_SET             ((uint32_t)0x00000007) \r
136 \r
137 /* ADC JDRx registers offset */\r
138 #define JDR_OFFSET                ((uint8_t)0x30)   \r
139   \r
140 /* ADC CCR register Mask */\r
141 #define CR_CLEAR_MASK             ((uint32_t)0xFFFCFFFF) \r
142 \r
143 /* Private macro -------------------------------------------------------------*/\r
144 /* Private variables ---------------------------------------------------------*/\r
145 /* Private function prototypes -----------------------------------------------*/\r
146 /* Private functions ---------------------------------------------------------*/\r
147 \r
148 /** @defgroup ADC_Private_Functions\r
149   * @{\r
150   */\r
151 \r
152 /** @defgroup ADC_Group1 Initialization and Configuration functions\r
153  *  @brief   Initialization and Configuration functions \r
154  *\r
155 @verbatim    \r
156  ===============================================================================\r
157                       Initialization and Configuration functions\r
158  ===============================================================================  \r
159   This section provides functions allowing to:\r
160    - Initialize and configure the ADC Prescaler\r
161    - ADC Conversion Resolution (12bit..6bit)\r
162    - Scan Conversion Mode (multichannels or one channel) for regular group\r
163    - ADC Continuous Conversion Mode (Continuous or Single conversion) for regular group\r
164    - External trigger Edge and source of regular group, \r
165    - Converted data alignment (left or right)\r
166    - The number of ADC conversions that will be done using the sequencer for regular channel group\r
167    - Enable or disable the ADC peripheral\r
168    \r
169 @endverbatim\r
170   * @{\r
171   */\r
172 \r
173 /**\r
174   * @brief  Deinitializes ADC1 peripheral registers to their default reset values.\r
175   * @param  None\r
176   * @retval None\r
177   */\r
178 void ADC_DeInit(ADC_TypeDef* ADCx)\r
179 {\r
180   /* Check the parameters */\r
181   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
182   \r
183   /* Enable ADC1 reset state */\r
184   RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);\r
185   /* Release ADC1 from reset state */\r
186   RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);\r
187 }\r
188 \r
189 /**\r
190   * @brief  Initializes the ADCx peripheral according to the specified parameters\r
191   *         in the ADC_InitStruct.\r
192   * @note   This function is used to configure the global features of the ADC ( \r
193   *         Resolution and Data Alignment), however, the rest of the configuration\r
194   *         parameters are specific to the regular channels group (scan mode \r
195   *         activation, continuous mode activation, External trigger source and \r
196   *         edge, number of conversion in the regular channels group sequencer).   \r
197   * @param  ADCx: where x can be 1 to select the ADC peripheral.\r
198   * @param  ADC_InitStruct: pointer to an ADC_InitTypeDef structure that contains \r
199   *         the configuration information for the specified ADC peripheral.\r
200   * @retval None\r
201   */\r
202 void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)               \r
203 {\r
204   uint32_t tmpreg1 = 0;\r
205   uint8_t tmpreg2 = 0;\r
206   \r
207   /* Check the parameters */\r
208   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
209   assert_param(IS_ADC_RESOLUTION(ADC_InitStruct->ADC_Resolution)); \r
210   assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode));\r
211   assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode)); \r
212   assert_param(IS_ADC_EXT_TRIG_EDGE(ADC_InitStruct->ADC_ExternalTrigConvEdge)); \r
213   assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv));    \r
214   assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign)); \r
215   assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfConversion));\r
216   \r
217   /*---------------------------- ADCx CR1 Configuration -----------------*/\r
218   /* Get the ADCx CR1 value */\r
219   tmpreg1 = ADCx->CR1;\r
220   /* Clear RES and SCAN bits */ \r
221   tmpreg1 &= CR1_CLEAR_MASK;\r
222   /* Configure ADCx: scan conversion mode and resolution */\r
223   /* Set SCAN bit according to ADC_ScanConvMode value */\r
224   /* Set RES bit according to ADC_Resolution value */ \r
225   tmpreg1 |= (uint32_t)(((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8) | ADC_InitStruct->ADC_Resolution);\r
226   /* Write to ADCx CR1 */\r
227   ADCx->CR1 = tmpreg1;\r
228   \r
229   /*---------------------------- ADCx CR2 Configuration -----------------*/\r
230   /* Get the ADCx CR2 value */\r
231   tmpreg1 = ADCx->CR2;\r
232   /* Clear CONT, ALIGN, EXTEN and EXTSEL bits */\r
233   tmpreg1 &= CR2_CLEAR_MASK;\r
234   /* Configure ADCx: external trigger event and edge, data alignment and continuous conversion mode */\r
235   /* Set ALIGN bit according to ADC_DataAlign value */\r
236   /* Set EXTEN bits according to ADC_ExternalTrigConvEdge value */ \r
237   /* Set EXTSEL bits according to ADC_ExternalTrigConv value */\r
238   /* Set CONT bit according to ADC_ContinuousConvMode value */\r
239   tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ExternalTrigConv | \r
240               ADC_InitStruct->ADC_ExternalTrigConvEdge | ((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1));\r
241   /* Write to ADCx CR2 */\r
242   ADCx->CR2 = tmpreg1;\r
243   \r
244   /*---------------------------- ADCx SQR1 Configuration -----------------*/\r
245   /* Get the ADCx SQR1 value */\r
246   tmpreg1 = ADCx->SQR1;\r
247   /* Clear L bits */\r
248   tmpreg1 &= SQR1_L_RESET;\r
249   /* Configure ADCx: regular channel sequence length */\r
250   /* Set L bits according to ADC_NbrOfConversion value */ \r
251   tmpreg2 |= (uint8_t)(ADC_InitStruct->ADC_NbrOfConversion - (uint8_t)1);\r
252   tmpreg1 |= ((uint32_t)tmpreg2 << 20);\r
253   /* Write to ADCx SQR1 */\r
254   ADCx->SQR1 = tmpreg1;\r
255 }\r
256 \r
257 /**\r
258   * @brief  Fills each ADC_InitStruct member with its default value.\r
259   * @note   This function is used to initialize the global features of the ADC ( \r
260   *         Resolution and Data Alignment), however, the rest of the configuration\r
261   *         parameters are specific to the regular channels group (scan mode \r
262   *         activation, continuous mode activation, External trigger source and \r
263   *         edge, number of conversion in the regular channels group sequencer).   \r
264   * @param  ADC_InitStruct: pointer to an ADC_InitTypeDef structure which will \r
265   *         be initialized.\r
266   * @retval None\r
267   */\r
268 void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)                            \r
269 {\r
270   /* Reset ADC init structure parameters values */\r
271   /* Initialize the ADC_Resolution member */\r
272   ADC_InitStruct->ADC_Resolution = ADC_Resolution_12b;\r
273 \r
274   /* Initialize the ADC_ScanConvMode member */\r
275   ADC_InitStruct->ADC_ScanConvMode = DISABLE;\r
276 \r
277   /* Initialize the ADC_ContinuousConvMode member */\r
278   ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;\r
279 \r
280   /* Initialize the ADC_ExternalTrigConvEdge member */\r
281   ADC_InitStruct->ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;\r
282 \r
283   /* Initialize the ADC_ExternalTrigConv member */\r
284   ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;\r
285 \r
286   /* Initialize the ADC_DataAlign member */\r
287   ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;\r
288 \r
289   /* Initialize the ADC_NbrOfConversion member */\r
290   ADC_InitStruct->ADC_NbrOfConversion = 1;\r
291 }\r
292 \r
293 /**\r
294   * @brief  Initializes the ADCs peripherals according to the specified parameters\r
295   *          in the ADC_CommonInitStruct.\r
296   * @param  ADC_CommonInitStruct: pointer to an ADC_CommonInitTypeDef structure \r
297   *         that contains the configuration information (Prescaler) for ADC1 peripheral.\r
298   * @retval None\r
299   */\r
300 void ADC_CommonInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct)                           \r
301 {\r
302   uint32_t tmpreg = 0;\r
303   \r
304   /* Check the parameters */\r
305   assert_param(IS_ADC_PRESCALER(ADC_CommonInitStruct->ADC_Prescaler));\r
306 \r
307   /*---------------------------- ADC CCR Configuration -----------------*/\r
308   /* Get the ADC CCR value */\r
309   tmpreg = ADC->CCR;\r
310 \r
311   /* Clear ADCPRE bit */ \r
312   tmpreg &= CR_CLEAR_MASK;\r
313   \r
314   /* Configure ADCx: ADC prescaler according to ADC_Prescaler */                \r
315   tmpreg |= (uint32_t)(ADC_CommonInitStruct->ADC_Prescaler);        \r
316                 \r
317   /* Write to ADC CCR */\r
318   ADC->CCR = tmpreg;\r
319 }\r
320 \r
321 /**\r
322   * @brief  Fills each ADC_CommonInitStruct member with its default value.\r
323   * @param  ADC_CommonInitStruct: pointer to an ADC_CommonInitTypeDef structure\r
324   *         which will be initialized.\r
325   * @retval None\r
326   */\r
327 void ADC_CommonStructInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct)                      \r
328 {\r
329   /* Reset ADC init structure parameters values */\r
330   /* Initialize the ADC_Prescaler member */\r
331   ADC_CommonInitStruct->ADC_Prescaler = ADC_Prescaler_Div1;\r
332 \r
333 }\r
334 \r
335 /**\r
336   * @brief  Enables or disables the specified ADC peripheral.\r
337   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
338   * @param  NewState: new state of the ADCx peripheral. \r
339   *         This parameter can be: ENABLE or DISABLE.\r
340   * @retval None\r
341   */\r
342 void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
343 {\r
344   /* Check the parameters */\r
345   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
346   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
347 \r
348   if (NewState != DISABLE)\r
349   {\r
350     /* Set the ADON bit to wake up the ADC from power down mode */\r
351     ADCx->CR2 |= (uint32_t)ADC_CR2_ADON;\r
352   }\r
353   else\r
354   {\r
355     /* Disable the selected ADC peripheral */\r
356     ADCx->CR2 &= (uint32_t)(~ADC_CR2_ADON);\r
357   }\r
358 }\r
359 \r
360 /**\r
361   * @}\r
362   */\r
363 \r
364 /** @defgroup ADC_Group2 Power saving functions\r
365  *  @brief   Power saving functions \r
366  *\r
367 @verbatim   \r
368  ===============================================================================\r
369                               Power saving functions\r
370  ===============================================================================  \r
371 \r
372   This section provides functions allowing to reduce power consumption.\r
373   The two function must be combined to get the maximal benefits:\r
374   When the ADC frequency is higher than the CPU one, it is recommended to  \r
375   1. Insert a freeze delay : \r
376      ==> using ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze);\r
377   2. Enable the power down in Idle and Delay phases :\r
378      ==> using ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE);\r
379 \r
380 @endverbatim\r
381   * @{\r
382   */\r
383 \r
384 /**\r
385   * @brief  Enables or disables the ADC Power Down during Delay and/or Idle phase.\r
386   * @note   ADC power-on and power-off can be managed by hardware to cut the \r
387   *         consumption when the ADC is not converting. \r
388   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
389   * @param  ADC_PowerDown: The ADC power down configuration. \r
390   *         This parameter can be one of the following values:\r
391   *     @arg ADC_PowerDown_Delay:      ADC is powered down during delay phase  \r
392   *     @arg ADC_PowerDown_Idle:       ADC is powered down during Idle phase \r
393   *     @arg ADC_PowerDown_Idle_Delay: ADC is powered down during Delay and Idle phases\r
394   * @note   The ADC can be powered down: \r
395   *         - During the hardware delay insertion (using the ADC_PowerDown_Delay\r
396   *           parameter) \r
397   *           => The ADC is powered up again at the end of the delay. \r
398   *         - During the ADC is waiting for a trigger event ( using the \r
399   *           ADC_PowerDown_Idle parameter) \r
400   *           => The ADC is powered up at the next trigger event.\r
401   *         - During the hardware delay insertion or the ADC is waiting for a \r
402   *           trigger event (using the ADC_PowerDown_Idle_Delay parameter) \r
403   *            => The ADC is powered up only at the end of the delay and at the \r
404   *              next trigger event.     \r
405   * @param  NewState: new state of the ADCx power down. \r
406   *         This parameter can be: ENABLE or DISABLE.\r
407   * @retval None\r
408   */\r
409 void ADC_PowerDownCmd(ADC_TypeDef* ADCx, uint32_t ADC_PowerDown, FunctionalState NewState)\r
410 {\r
411   /* Check the parameters */\r
412   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
413   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
414   assert_param(IS_ADC_POWER_DOWN(ADC_PowerDown));\r
415   \r
416   if (NewState != DISABLE)\r
417   {\r
418     /* Enable the ADC power-down during Delay and/or Idle phase */\r
419     ADCx->CR1 |= ADC_PowerDown;\r
420   }\r
421   else\r
422   {\r
423     /* Disable The ADC power-down during Delay and/or Idle phase */\r
424     ADCx->CR1 &= (uint32_t)~ADC_PowerDown;\r
425   }\r
426 }\r
427 \r
428 /**\r
429   * @brief  Defines the length of the delay which is applied after a conversion \r
430   *         or a sequence of conversion.\r
431   * @note   When the CPU clock is not fast enough to manage the data rate, a \r
432   *         Hardware delay can be introduced between ADC conversions to reduce \r
433   *         this data rate. \r
434   * @note   The Hardware delay is inserted after :\r
435   *         - each regular conversion\r
436   *         - after each sequence of injected conversions\r
437   * @note   No Hardware delay is inserted between conversions of different groups.\r
438   * @note   When the hardware delay is not enough, the Freeze Delay Mode can be \r
439   *         selected and a new conversion can start only if all the previous data \r
440   *         of the same group have been treated:\r
441   *         - for a regular conversion: once the ADC conversion data register has \r
442   *           been read (using ADC_GetConversionValue() function) or if the EOC \r
443   *           Flag has been cleared (using ADC_ClearFlag() function).\r
444   *         - for an injected conversion: when the JEOC bit has been cleared \r
445   *           (using ADC_ClearFlag() function).\r
446   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
447   * @param  ADC_DelayLength: The length of delay which is applied after a \r
448   *         conversion or a sequence of conversion. \r
449   *   This parameter can be one of the following values:\r
450   *     @arg ADC_DelayLength_None: No delay \r
451   *     @arg ADC_DelayLength_Freeze: Delay until the converted data has been read.\r
452   *     @arg ADC_DelayLength_7Cycles: Delay length equal to 7 APB clock cycles\r
453   *     @arg ADC_DelayLength_15Cycles: Delay length equal to 15 APB clock cycles        \r
454   *     @arg ADC_DelayLength_31Cycles: Delay length equal to 31 APB clock cycles        \r
455   *     @arg ADC_DelayLength_63Cycles: Delay length equal to 63 APB clock cycles        \r
456   *     @arg ADC_DelayLength_127Cycles: Delay length equal to 127 APB clock cycles      \r
457   *     @arg ADC_DelayLength_255Cycles: Delay length equal to 255 APB clock cycles      \r
458   * @retval None\r
459   */\r
460 void ADC_DelaySelectionConfig(ADC_TypeDef* ADCx, uint8_t ADC_DelayLength)\r
461 {\r
462   uint32_t tmpreg = 0;\r
463    \r
464   /* Check the parameters */\r
465   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
466   assert_param(IS_ADC_DELAY_LENGTH(ADC_DelayLength));\r
467 \r
468   /* Get the old register value */    \r
469   tmpreg = ADCx->CR2;\r
470   /* Clear the old delay length */\r
471   tmpreg &= CR2_DELS_RESET;\r
472   /* Set the delay length */\r
473   tmpreg |= ADC_DelayLength;\r
474   /* Store the new register value */\r
475   ADCx->CR2 = tmpreg;\r
476 \r
477 }\r
478 \r
479 /**\r
480   * @}\r
481   */\r
482 \r
483 /** @defgroup ADC_Group3 Analog Watchdog configuration functions\r
484  *  @brief   Analog Watchdog configuration functions \r
485  *\r
486 @verbatim   \r
487  ===============================================================================\r
488                     Analog Watchdog configuration functions\r
489  ===============================================================================  \r
490 \r
491   This section provides functions allowing to configure the Analog Watchdog\r
492   (AWD) feature in the ADC.\r
493   \r
494   A typical configuration Analog Watchdog is done following these steps :\r
495    1. the ADC guarded channel(s) is (are) selected using the \r
496       ADC_AnalogWatchdogSingleChannelConfig() function.\r
497    2. The Analog watchdog lower and higher threshold are configured using the  \r
498      ADC_AnalogWatchdogThresholdsConfig() function.\r
499    3. The Analog watchdog is enabled and configured to enable the check, on one\r
500       or more channels, using the  ADC_AnalogWatchdogCmd() function.\r
501 \r
502 @endverbatim\r
503   * @{\r
504   */\r
505   \r
506 /**\r
507   * @brief  Enables or disables the analog watchdog on single/all regular\r
508   *         or injected channels\r
509   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
510   * @param  ADC_AnalogWatchdog: the ADC analog watchdog configuration.\r
511   *   This parameter can be one of the following values:\r
512   *     @arg ADC_AnalogWatchdog_SingleRegEnable: Analog watchdog on a single \r
513   *          regular channel\r
514   *     @arg ADC_AnalogWatchdog_SingleInjecEnable: Analog watchdog on a single \r
515   *          injected channel\r
516   *     @arg ADC_AnalogWatchdog_SingleRegOrInjecEnable: Analog watchdog on a \r
517   *          single regular or injected channel       \r
518   *     @arg ADC_AnalogWatchdog_AllRegEnable: Analog watchdog on all regular \r
519   *          channel\r
520   *     @arg ADC_AnalogWatchdog_AllInjecEnable: Analog watchdog on all injected \r
521   *          channel\r
522   *     @arg ADC_AnalogWatchdog_AllRegAllInjecEnable: Analog watchdog on all \r
523   *           regular and injected channels\r
524   *     @arg ADC_AnalogWatchdog_None: No channel guarded by the analog watchdog\r
525   * @retval None          \r
526   */\r
527 void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog)\r
528 {\r
529   uint32_t tmpreg = 0;\r
530 \r
531   /* Check the parameters */\r
532   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
533   assert_param(IS_ADC_ANALOG_WATCHDOG(ADC_AnalogWatchdog));\r
534 \r
535   /* Get the old register value */\r
536   tmpreg = ADCx->CR1;\r
537   /* Clear AWDEN, JAWDEN and AWDSGL bits */   \r
538   tmpreg &= CR1_AWDMODE_RESET;\r
539   /* Set the analog watchdog enable mode */\r
540   tmpreg |= ADC_AnalogWatchdog;\r
541   /* Store the new register value */\r
542   ADCx->CR1 = tmpreg;\r
543 }\r
544 \r
545 /**\r
546   * @brief  Configures the high and low thresholds of the analog watchdog. \r
547   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
548   * @param  HighThreshold: the ADC analog watchdog High threshold value.\r
549   *         This parameter must be a 12bit value.\r
550   * @param  LowThreshold: the ADC analog watchdog Low threshold value.\r
551   *         This parameter must be a 12bit value.\r
552   * @retval None\r
553   */\r
554 void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,\r
555                                         uint16_t LowThreshold)\r
556 {\r
557   /* Check the parameters */\r
558   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
559   assert_param(IS_ADC_THRESHOLD(HighThreshold));\r
560   assert_param(IS_ADC_THRESHOLD(LowThreshold));\r
561 \r
562   /* Set the ADCx high threshold */\r
563   ADCx->HTR = HighThreshold;\r
564   /* Set the ADCx low threshold */\r
565   ADCx->LTR = LowThreshold;\r
566 }\r
567 \r
568 /**\r
569   * @brief  Configures the analog watchdog guarded single channel\r
570   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
571   * @param  ADC_Channel: the ADC channel to configure for the analog watchdog. \r
572   *   This parameter can be one of the following values:\r
573   *     @arg ADC_Channel_0: ADC Channel0 selected\r
574   *     @arg ADC_Channel_1: ADC Channel1 selected\r
575   *     @arg ADC_Channel_2: ADC Channel2 selected\r
576   *     @arg ADC_Channel_3: ADC Channel3 selected\r
577   *     @arg ADC_Channel_4: ADC Channel4 selected\r
578   *     @arg ADC_Channel_5: ADC Channel5 selected\r
579   *     @arg ADC_Channel_6: ADC Channel6 selected\r
580   *     @arg ADC_Channel_7: ADC Channel7 selected\r
581   *     @arg ADC_Channel_8: ADC Channel8 selected\r
582   *     @arg ADC_Channel_9: ADC Channel9 selected\r
583   *     @arg ADC_Channel_10: ADC Channel10 selected\r
584   *     @arg ADC_Channel_11: ADC Channel11 selected\r
585   *     @arg ADC_Channel_12: ADC Channel12 selected\r
586   *     @arg ADC_Channel_13: ADC Channel13 selected\r
587   *     @arg ADC_Channel_14: ADC Channel14 selected\r
588   *     @arg ADC_Channel_15: ADC Channel15 selected\r
589   *     @arg ADC_Channel_16: ADC Channel16 selected\r
590   *     @arg ADC_Channel_17: ADC Channel17 selected\r
591   *     @arg ADC_Channel_18: ADC Channel18 selected\r
592   *     @arg ADC_Channel_19: ADC Channel19 selected\r
593   *     @arg ADC_Channel_20: ADC Channel20 selected\r
594   *     @arg ADC_Channel_21: ADC Channel21 selected\r
595   *     @arg ADC_Channel_22: ADC Channel22 selected\r
596   *     @arg ADC_Channel_23: ADC Channel23 selected\r
597   *     @arg ADC_Channel_24: ADC Channel24 selected\r
598   *     @arg ADC_Channel_25: ADC Channel25 selected\r
599   * @retval None\r
600   */\r
601 void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel)\r
602 {\r
603   uint32_t tmpreg = 0;\r
604 \r
605   /* Check the parameters */\r
606   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
607   assert_param(IS_ADC_CHANNEL(ADC_Channel));\r
608 \r
609   /* Get the old register value */\r
610   tmpreg = ADCx->CR1;\r
611   /* Clear the Analog watchdog channel select bits */\r
612   tmpreg &= CR1_AWDCH_RESET;\r
613   /* Set the Analog watchdog channel */\r
614   tmpreg |= ADC_Channel;\r
615   /* Store the new register value */\r
616   ADCx->CR1 = tmpreg;\r
617 }\r
618 \r
619 /**\r
620   * @}\r
621   */\r
622 \r
623 /** @defgroup ADC_Group4 Temperature Sensor & Vrefint (Voltage Reference internal) management function\r
624  *  @brief   Temperature Sensor & Vrefint (Voltage Reference internal) management function \r
625  *\r
626 @verbatim   \r
627  ===============================================================================\r
628   Temperature Sensor & Vrefint (Voltage Reference internal) management function\r
629  ===============================================================================  \r
630 \r
631   This section provides a function allowing to enable/ disable the internal \r
632   connections between the ADC and the Temperature Sensor and the Vrefint source.\r
633      \r
634   A typical configuration to get the Temperature sensor and Vrefint channels \r
635   voltages or is done following these steps :\r
636    1. Enable the internal connection of Temperature sensor and Vrefint sources \r
637       with the ADC channels using ADC_TempSensorVrefintCmd() function. \r
638    2. select the ADC_Channel_TempSensor and/or ADC_Channel_Vrefint using \r
639       ADC_RegularChannelConfig() or  ADC_InjectedChannelConfig() functions \r
640    3. Get the voltage values, using ADC_GetConversionValue() or  \r
641       ADC_GetInjectedConversionValue().\r
642  \r
643 @endverbatim\r
644   * @{\r
645   */\r
646   \r
647 /**\r
648   * @brief  Enables or disables the temperature sensor and Vrefint channel.\r
649   * @param  NewState: new state of the temperature sensor and Vref int channels.\r
650   *         This parameter can be: ENABLE or DISABLE.\r
651   * @retval None\r
652   */\r
653 void ADC_TempSensorVrefintCmd(FunctionalState NewState)                \r
654 {\r
655   /* Check the parameters */\r
656   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
657 \r
658   if (NewState != DISABLE)\r
659   {\r
660     /* Enable the temperature sensor and Vrefint channel*/\r
661     ADC->CCR |= (uint32_t)ADC_CCR_TSVREFE;\r
662   }\r
663   else\r
664   {\r
665     /* Disable the temperature sensor and Vrefint channel*/\r
666     ADC->CCR &= (uint32_t)(~ADC_CCR_TSVREFE);\r
667   }\r
668 }\r
669 \r
670 /**\r
671   * @}\r
672   */\r
673 \r
674 /** @defgroup ADC_Group5 Regular Channels Configuration functions\r
675  *  @brief   Regular Channels Configuration functions \r
676  *\r
677 @verbatim   \r
678  ===============================================================================\r
679                   Regular Channels Configuration functions\r
680  ===============================================================================  \r
681 \r
682   This section provides functions allowing to manage the ADC regular channels,\r
683   it is composed of 2 sub sections : \r
684   \r
685   1. Configuration and management functions for regular channels: This subsection \r
686      provides functions allowing to configure the ADC regular channels :    \r
687           - Configure the rank in the regular group sequencer for each channel\r
688           - Configure the sampling time for each channel\r
689           - select the conversion Trigger for regular channels\r
690           - select the desired EOC event behavior configuration\r
691           - Activate the continuous Mode  (*)\r
692           - Activate the Discontinuous Mode \r
693      Please Note that the following features for regular channels are configurated\r
694      using the ADC_Init() function : \r
695           - scan mode activation \r
696           - continuous mode activation (**) \r
697           - External trigger source  \r
698           - External trigger edge \r
699           - number of conversion in the regular channels group sequencer.\r
700      \r
701      @note : (*) and (**) are performing the same configuration\r
702      \r
703   2. Get the conversion data: This subsection provides an important function in \r
704      the ADC peripheral since it returns the converted data of the current \r
705      regular channel. When the Conversion value is read, the EOC Flag is \r
706      automatically cleared.\r
707   \r
708 @endverbatim\r
709   * @{\r
710   */\r
711 \r
712 /**\r
713   * @brief  Configures for the selected ADC regular channel its corresponding\r
714   *         rank in the sequencer and its sampling time.\r
715   * @param  ADCx: where x can be 1 to select the ADC peripheral.\r
716   * @param  ADC_Channel: the ADC channel to configure. \r
717   *   This parameter can be one of the following values:\r
718   *     @arg ADC_Channel_0: ADC Channel0 selected\r
719   *     @arg ADC_Channel_1: ADC Channel1 selected\r
720   *     @arg ADC_Channel_2: ADC Channel2 selected\r
721   *     @arg ADC_Channel_3: ADC Channel3 selected\r
722   *     @arg ADC_Channel_4: ADC Channel4 selected\r
723   *     @arg ADC_Channel_5: ADC Channel5 selected\r
724   *     @arg ADC_Channel_6: ADC Channel6 selected\r
725   *     @arg ADC_Channel_7: ADC Channel7 selected\r
726   *     @arg ADC_Channel_8: ADC Channel8 selected\r
727   *     @arg ADC_Channel_9: ADC Channel9 selected\r
728   *     @arg ADC_Channel_10: ADC Channel10 selected\r
729   *     @arg ADC_Channel_11: ADC Channel11 selected\r
730   *     @arg ADC_Channel_12: ADC Channel12 selected\r
731   *     @arg ADC_Channel_13: ADC Channel13 selected\r
732   *     @arg ADC_Channel_14: ADC Channel14 selected\r
733   *     @arg ADC_Channel_15: ADC Channel15 selected\r
734   *     @arg ADC_Channel_16: ADC Channel16 selected\r
735   *     @arg ADC_Channel_17: ADC Channel17 selected\r
736   *     @arg ADC_Channel_18: ADC Channel18 selected \r
737   *     @arg ADC_Channel_19: ADC Channel19 selected\r
738   *     @arg ADC_Channel_20: ADC Channel20 selected\r
739   *     @arg ADC_Channel_21: ADC Channel21 selected\r
740   *     @arg ADC_Channel_22: ADC Channel22 selected\r
741   *     @arg ADC_Channel_23: ADC Channel23 selected\r
742   *     @arg ADC_Channel_24: ADC Channel24 selected\r
743   *     @arg ADC_Channel_25: ADC Channel25 selected\r
744   * @param  Rank: The rank in the regular group sequencer. This parameter\r
745   *               must be between 1 to 26.\r
746   * @param  ADC_SampleTime: The sample time value to be set for the selected \r
747   *         channel. \r
748   *   This parameter can be one of the following values:\r
749   *     @arg ADC_SampleTime_4Cycles: Sample time equal to 4 cycles  \r
750   *     @arg ADC_SampleTime_9Cycles: Sample time equal to 9 cycles\r
751   *     @arg ADC_SampleTime_16Cycles: Sample time equal to 16 cycles\r
752   *     @arg ADC_SampleTime_24Cycles: Sample time equal to 24 cycles    \r
753   *     @arg ADC_SampleTime_48Cycles: Sample time equal to 48 cycles    \r
754   *     @arg ADC_SampleTime_96Cycles: Sample time equal to 96 cycles    \r
755   *     @arg ADC_SampleTime_192Cycles: Sample time equal to 192 cycles  \r
756   *     @arg ADC_SampleTime_384Cycles: Sample time equal to 384 cycles  \r
757   * @retval None\r
758   */\r
759 void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)\r
760 {\r
761   uint32_t tmpreg1 = 0, tmpreg2 = 0;\r
762 \r
763   /* Check the parameters */\r
764   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
765   assert_param(IS_ADC_CHANNEL(ADC_Channel));\r
766   assert_param(IS_ADC_REGULAR_RANK(Rank));\r
767   assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));\r
768 \r
769   /* if ADC_Channel_20 ... ADC_Channel_25 is selected */\r
770   if (ADC_Channel > ADC_Channel_19)\r
771   {\r
772     /* Get the old register value */\r
773     tmpreg1 = ADCx->SMPR1;\r
774     /* Calculate the mask to clear */\r
775     tmpreg2 = SMPR1_SMP_SET << (3 * (ADC_Channel - 20));\r
776     /* Clear the old sample time */\r
777     tmpreg1 &= ~tmpreg2;\r
778     /* Calculate the mask to set */\r
779     tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 20));\r
780     /* Set the new sample time */\r
781     tmpreg1 |= tmpreg2;\r
782     /* Store the new register value */\r
783     ADCx->SMPR1 = tmpreg1;\r
784   }\r
785   \r
786    /* if ADC_Channel_10 ... ADC_Channel_19 is selected */\r
787   else if (ADC_Channel > ADC_Channel_9)\r
788   {\r
789     /* Get the old register value */\r
790     tmpreg1 = ADCx->SMPR2;\r
791     /* Calculate the mask to clear */\r
792     tmpreg2 = SMPR2_SMP_SET << (3 * (ADC_Channel - 10));\r
793     /* Clear the old sample time */\r
794     tmpreg1 &= ~tmpreg2;\r
795     /* Calculate the mask to set */\r
796     tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));\r
797     /* Set the new sample time */\r
798     tmpreg1 |= tmpreg2;\r
799     /* Store the new register value */\r
800     ADCx->SMPR2 = tmpreg1;\r
801   }\r
802   \r
803   else /* ADC_Channel include in ADC_Channel_[0..9] */\r
804   {\r
805     /* Get the old register value */\r
806     tmpreg1 = ADCx->SMPR3;\r
807     /* Calculate the mask to clear */\r
808     tmpreg2 = SMPR3_SMP_SET << (3 * ADC_Channel);\r
809     /* Clear the old sample time */\r
810     tmpreg1 &= ~tmpreg2;\r
811     /* Calculate the mask to set */\r
812     tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);\r
813     /* Set the new sample time */\r
814     tmpreg1 |= tmpreg2;\r
815     /* Store the new register value */\r
816     ADCx->SMPR3 = tmpreg1;\r
817   }\r
818   /* For Rank 1 to 6 */\r
819   if (Rank < 7)\r
820   {\r
821     /* Get the old register value */\r
822     tmpreg1 = ADCx->SQR5;\r
823     /* Calculate the mask to clear */\r
824     tmpreg2 = SQR5_SQ_SET << (5 * (Rank - 1));\r
825     /* Clear the old SQx bits for the selected rank */\r
826     tmpreg1 &= ~tmpreg2;\r
827     /* Calculate the mask to set */\r
828     tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1));\r
829     /* Set the SQx bits for the selected rank */\r
830     tmpreg1 |= tmpreg2;\r
831     /* Store the new register value */\r
832     ADCx->SQR5 = tmpreg1;\r
833   }\r
834   /* For Rank 7 to 12 */\r
835   else if (Rank < 13)\r
836   {\r
837     /* Get the old register value */\r
838     tmpreg1 = ADCx->SQR4;\r
839     /* Calculate the mask to clear */\r
840     tmpreg2 = SQR4_SQ_SET << (5 * (Rank - 7));\r
841     /* Clear the old SQx bits for the selected rank */\r
842     tmpreg1 &= ~tmpreg2;\r
843     /* Calculate the mask to set */\r
844     tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7));\r
845     /* Set the SQx bits for the selected rank */\r
846     tmpreg1 |= tmpreg2;\r
847     /* Store the new register value */\r
848     ADCx->SQR4 = tmpreg1;\r
849   }  \r
850   /* For Rank 13 to 18 */\r
851   else if (Rank < 19)\r
852   {\r
853     /* Get the old register value */\r
854     tmpreg1 = ADCx->SQR3;\r
855     /* Calculate the mask to clear */\r
856     tmpreg2 = SQR3_SQ_SET << (5 * (Rank - 13));\r
857     /* Clear the old SQx bits for the selected rank */\r
858     tmpreg1 &= ~tmpreg2;\r
859     /* Calculate the mask to set */\r
860     tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13));\r
861     /* Set the SQx bits for the selected rank */\r
862     tmpreg1 |= tmpreg2;\r
863     /* Store the new register value */\r
864     ADCx->SQR3 = tmpreg1;\r
865   }\r
866     \r
867   /* For Rank 19 to 24 */\r
868   else if (Rank < 25)\r
869   {\r
870     /* Get the old register value */\r
871     tmpreg1 = ADCx->SQR2;\r
872     /* Calculate the mask to clear */\r
873     tmpreg2 = SQR2_SQ_SET << (5 * (Rank - 19));\r
874     /* Clear the old SQx bits for the selected rank */\r
875     tmpreg1 &= ~tmpreg2;\r
876     /* Calculate the mask to set */\r
877     tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 19));\r
878     /* Set the SQx bits for the selected rank */\r
879     tmpreg1 |= tmpreg2;\r
880     /* Store the new register value */\r
881     ADCx->SQR2 = tmpreg1;\r
882   }   \r
883   \r
884   /* For Rank 25 to 27 */\r
885   else\r
886   {\r
887     /* Get the old register value */\r
888     tmpreg1 = ADCx->SQR1;\r
889     /* Calculate the mask to clear */\r
890     tmpreg2 = SQR1_SQ_SET << (5 * (Rank - 25));\r
891     /* Clear the old SQx bits for the selected rank */\r
892     tmpreg1 &= ~tmpreg2;\r
893     /* Calculate the mask to set */\r
894     tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 25));\r
895     /* Set the SQx bits for the selected rank */\r
896     tmpreg1 |= tmpreg2;\r
897     /* Store the new register value */\r
898     ADCx->SQR1 = tmpreg1;\r
899   }\r
900 }\r
901 \r
902 /**\r
903   * @brief  Enables the selected ADC software start conversion of the regular channels.\r
904   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
905   * @retval None\r
906   */\r
907 void ADC_SoftwareStartConv(ADC_TypeDef* ADCx)\r
908 {\r
909   /* Check the parameters */\r
910   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
911 \r
912   /* Enable the selected ADC conversion for regular group */\r
913   ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART;\r
914 }\r
915 \r
916 /**\r
917   * @brief  Gets the selected ADC Software start regular conversion Status.\r
918   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
919   * @retval The new state of ADC software start conversion (SET or RESET).\r
920   */\r
921 FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx)\r
922 {\r
923   FlagStatus bitstatus = RESET;\r
924 \r
925   /* Check the parameters */\r
926   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
927 \r
928   /* Check the status of SWSTART bit */\r
929   if ((ADCx->CR2 & ADC_CR2_SWSTART) != (uint32_t)RESET)\r
930   {\r
931     /* SWSTART bit is set */\r
932     bitstatus = SET;\r
933   }\r
934   else\r
935   {\r
936     /* SWSTART bit is reset */\r
937     bitstatus = RESET;\r
938   }\r
939   /* Return the SWSTART bit status */\r
940   return  bitstatus;\r
941 }\r
942 \r
943 /**\r
944   * @brief  Enables or disables the EOC on each regular channel conversion\r
945   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
946   * @param  NewState: new state of the selected ADC EOC flag rising\r
947   *    This parameter can be: ENABLE or DISABLE.\r
948   * @retval None\r
949   */\r
950 void ADC_EOCOnEachRegularChannelCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
951 {\r
952   /* Check the parameters */\r
953   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
954   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
955 \r
956   if (NewState != DISABLE)\r
957   {\r
958     /* Enable the selected ADC EOC rising on each regular channel conversion */\r
959     ADCx->CR2 |= ADC_CR2_EOCS;\r
960   }\r
961   else\r
962   {\r
963     /* Disable the selected ADC EOC rising on each regular channel conversion */\r
964     ADCx->CR2 &= (uint32_t)~ADC_CR2_EOCS;\r
965   }\r
966 }\r
967 \r
968 /**\r
969   * @brief  Enables or disables the ADC continuous conversion mode \r
970   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
971   * @param  NewState: new state of the selected ADC continuous conversion mode\r
972   *         This parameter can be: ENABLE or DISABLE.\r
973   * @retval None\r
974   */\r
975 void ADC_ContinuousModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
976 {\r
977   /* Check the parameters */\r
978   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
979   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
980 \r
981   if (NewState != DISABLE)\r
982   {\r
983     /* Enable the selected ADC continuous conversion mode */\r
984     ADCx->CR2 |= (uint32_t)ADC_CR2_CONT;\r
985   }\r
986   else\r
987   {\r
988     /* Disable the selected ADC continuous conversion mode */\r
989     ADCx->CR2 &= (uint32_t)(~ADC_CR2_CONT);\r
990   }\r
991 }\r
992 \r
993 /**\r
994   * @brief  Configures the discontinuous mode for the selected ADC regular\r
995   *         group channel.\r
996   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
997   * @param  Number: specifies the discontinuous mode regular channel count value. \r
998   *         This number must be between 1 and 8.\r
999   * @retval None\r
1000   */\r
1001 void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, uint8_t Number)\r
1002 {\r
1003   uint32_t tmpreg1 = 0;\r
1004   uint32_t tmpreg2 = 0;\r
1005 \r
1006   /* Check the parameters */\r
1007   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1008   assert_param(IS_ADC_REGULAR_DISC_NUMBER(Number));\r
1009 \r
1010   /* Get the old register value */\r
1011   tmpreg1 = ADCx->CR1;\r
1012   /* Clear the old discontinuous mode channel count */\r
1013   tmpreg1 &= CR1_DISCNUM_RESET;\r
1014   /* Set the discontinuous mode channel count */\r
1015   tmpreg2 = Number - 1;\r
1016   tmpreg1 |= tmpreg2 << 13;\r
1017   /* Store the new register value */\r
1018   ADCx->CR1 = tmpreg1;\r
1019 }\r
1020 \r
1021 /**\r
1022   * @brief  Enables or disables the discontinuous mode on regular group\r
1023   *         channel for the specified ADC\r
1024   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1025   * @param  NewState: new state of the selected ADC discontinuous mode on regular \r
1026   *         group channel. \r
1027   *         This parameter can be: ENABLE or DISABLE.\r
1028   * @retval None\r
1029   */\r
1030 void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
1031 {\r
1032   /* Check the parameters */\r
1033   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1034   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1035 \r
1036   if (NewState != DISABLE)\r
1037   {\r
1038     /* Enable the selected ADC regular discontinuous mode */\r
1039     ADCx->CR1 |= (uint32_t)ADC_CR1_DISCEN;\r
1040   }\r
1041   else\r
1042   {\r
1043     /* Disable the selected ADC regular discontinuous mode */\r
1044     ADCx->CR1 &= (uint32_t)(~ADC_CR1_DISCEN);\r
1045   }\r
1046 }\r
1047 \r
1048 /**\r
1049   * @brief  Returns the last ADCx conversion result data for regular channel.  \r
1050   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1051   * @retval The Data conversion value.\r
1052   */\r
1053 uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)\r
1054 {\r
1055   /* Check the parameters */\r
1056   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1057 \r
1058   /* Return the selected ADC conversion value */\r
1059   return (uint16_t) ADCx->DR;\r
1060 }\r
1061 \r
1062 /**\r
1063   * @}\r
1064   */\r
1065 \r
1066 /** @defgroup ADC_Group6 Regular Channels DMA Configuration functions\r
1067  *  @brief   Regular Channels DMA Configuration functions \r
1068  *\r
1069 @verbatim   \r
1070  ===============================================================================\r
1071                    Regular Channels DMA Configuration functions\r
1072  ===============================================================================  \r
1073 \r
1074   This section provides functions allowing to configure the DMA for ADC regular \r
1075   channels.\r
1076   Since converted regular channel values are stored into a unique data register, \r
1077   it is useful to use DMA for conversion of more than one regular channel. This \r
1078   avoids the loss of the data already stored in the ADC Data register. \r
1079   \r
1080   When the DMA mode is enabled (using the ADC_DMACmd() function), after each\r
1081   conversion of a regular channel, a DMA request is generated.\r
1082   \r
1083   Depending on the "DMA disable selection" configuration (using the \r
1084   ADC_DMARequestAfterLastTransferCmd() function), at the end of the last DMA \r
1085   transfer, two possibilities are allowed:\r
1086   - No new DMA request is issued to the DMA controller (feature DISABLED) \r
1087   - Requests can continue to be generated (feature ENABLED).\r
1088 \r
1089 @endverbatim\r
1090   * @{\r
1091   */\r
1092 \r
1093 /**\r
1094   * @brief  Enables or disables the specified ADC DMA request.\r
1095   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1096   * @param  NewState: new state of the selected ADC DMA transfer.\r
1097   *         This parameter can be: ENABLE or DISABLE.\r
1098   * @retval None\r
1099   */\r
1100 void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
1101 {\r
1102   /* Check the parameters */\r
1103   assert_param(IS_ADC_DMA_PERIPH(ADCx));\r
1104   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1105 \r
1106   if (NewState != DISABLE)\r
1107   {\r
1108     /* Enable the selected ADC DMA request */\r
1109     ADCx->CR2 |= (uint32_t)ADC_CR2_DMA;\r
1110   }\r
1111   else\r
1112   {\r
1113     /* Disable the selected ADC DMA request */\r
1114     ADCx->CR2 &= (uint32_t)(~ADC_CR2_DMA);\r
1115   }\r
1116 }\r
1117 \r
1118 \r
1119 /**\r
1120   * @brief  Enables or disables the ADC DMA request after last transfer (Single-ADC mode)  \r
1121   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1122   * @param  NewState: new state of the selected ADC EOC flag rising\r
1123   *         This parameter can be: ENABLE or DISABLE.\r
1124   * @retval None\r
1125   */\r
1126 void ADC_DMARequestAfterLastTransferCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
1127 {\r
1128   /* Check the parameters */\r
1129   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1130   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1131 \r
1132   if (NewState != DISABLE)\r
1133   {\r
1134     /* Enable the selected ADC DMA request after last transfer */\r
1135     ADCx->CR2 |= ADC_CR2_DDS;\r
1136   }\r
1137   else\r
1138   {\r
1139     /* Disable the selected ADC DMA request after last transfer */\r
1140     ADCx->CR2 &= (uint32_t)~ADC_CR2_DDS;\r
1141   }\r
1142 }\r
1143 \r
1144 /**\r
1145   * @}\r
1146   */\r
1147 \r
1148 /** @defgroup ADC_Group7 Injected channels Configuration functions\r
1149  *  @brief   Injected channels Configuration functions \r
1150  *\r
1151 @verbatim   \r
1152  ===============================================================================\r
1153                      Injected channels Configuration functions\r
1154  ===============================================================================  \r
1155 \r
1156   This section provide functions allowing to configure the ADC Injected channels,\r
1157   it is composed of 2 sub sections : \r
1158     \r
1159   1. Configuration functions for Injected channels: This subsection provides \r
1160      functions allowing to configure the ADC injected channels :    \r
1161     - Configure the rank in the injected group sequencer for each channel\r
1162     - Configure the sampling time for each channel    \r
1163     - Activate the Auto injected Mode  \r
1164     - Activate the Discontinuous Mode \r
1165     - scan mode activation  \r
1166     - External/software trigger source   \r
1167     - External trigger edge \r
1168     - injected channels sequencer.\r
1169     \r
1170    2. Get the Specified Injected channel conversion data: This subsection \r
1171       provides an important function in the ADC peripheral since it returns the \r
1172       converted data of the specific injected channel.\r
1173 \r
1174 @endverbatim\r
1175   * @{\r
1176   */ \r
1177 \r
1178 /**\r
1179   * @brief  Configures for the selected ADC injected channel its corresponding\r
1180   *         rank in the sequencer and its sample time.\r
1181   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1182   * @param  ADC_Channel: the ADC channel to configure. \r
1183   *   This parameter can be one of the following values:\r
1184   *     @arg ADC_Channel_0: ADC Channel0 selected\r
1185   *     @arg ADC_Channel_1: ADC Channel1 selected\r
1186   *     @arg ADC_Channel_2: ADC Channel2 selected\r
1187   *     @arg ADC_Channel_3: ADC Channel3 selected\r
1188   *     @arg ADC_Channel_4: ADC Channel4 selected\r
1189   *     @arg ADC_Channel_5: ADC Channel5 selected\r
1190   *     @arg ADC_Channel_6: ADC Channel6 selected\r
1191   *     @arg ADC_Channel_7: ADC Channel7 selected\r
1192   *     @arg ADC_Channel_8: ADC Channel8 selected\r
1193   *     @arg ADC_Channel_9: ADC Channel9 selected\r
1194   *     @arg ADC_Channel_10: ADC Channel10 selected\r
1195   *     @arg ADC_Channel_11: ADC Channel11 selected\r
1196   *     @arg ADC_Channel_12: ADC Channel12 selected\r
1197   *     @arg ADC_Channel_13: ADC Channel13 selected\r
1198   *     @arg ADC_Channel_14: ADC Channel14 selected\r
1199   *     @arg ADC_Channel_15: ADC Channel15 selected\r
1200   *     @arg ADC_Channel_16: ADC Channel16 selected\r
1201   *     @arg ADC_Channel_17: ADC Channel17 selected\r
1202   *     @arg ADC_Channel_18: ADC Channel18 selected \r
1203   *     @arg ADC_Channel_19: ADC Channel19 selected\r
1204   *     @arg ADC_Channel_20: ADC Channel20 selected\r
1205   *     @arg ADC_Channel_21: ADC Channel21 selected\r
1206   *     @arg ADC_Channel_22: ADC Channel22 selected\r
1207   *     @arg ADC_Channel_23: ADC Channel23 selected\r
1208   *     @arg ADC_Channel_24: ADC Channel24 selected\r
1209   *     @arg ADC_Channel_25: ADC Channel25 selected\r
1210   * @param  Rank: The rank in the injected group sequencer. This parameter\r
1211   *         must be between 1 to 4.\r
1212   * @param  ADC_SampleTime: The sample time value to be set for the selected \r
1213   *         channel. This parameter can be one of the following values:\r
1214   *     @arg ADC_SampleTime_4Cycles: Sample time equal to 4 cycles  \r
1215   *     @arg ADC_SampleTime_9Cycles: Sample time equal to 9 cycles\r
1216   *     @arg ADC_SampleTime_16Cycles: Sample time equal to 16 cycles\r
1217   *     @arg ADC_SampleTime_24Cycles: Sample time equal to 24 cycles    \r
1218   *     @arg ADC_SampleTime_48Cycles: Sample time equal to 48 cycles    \r
1219   *     @arg ADC_SampleTime_96Cycles: Sample time equal to 96 cycles    \r
1220   *     @arg ADC_SampleTime_192Cycles: Sample time equal to 192 cycles  \r
1221   *     @arg ADC_SampleTime_384Cycles: Sample time equal to 384 cycles  \r
1222   * @retval None\r
1223   */\r
1224 void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)\r
1225 {\r
1226   uint32_t tmpreg1 = 0, tmpreg2 = 0, tmpreg3 = 0;\r
1227 \r
1228   /* Check the parameters */\r
1229   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1230   assert_param(IS_ADC_CHANNEL(ADC_Channel));\r
1231   assert_param(IS_ADC_INJECTED_RANK(Rank));\r
1232   assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));\r
1233   \r
1234   /* if ADC_Channel_20 ... ADC_Channel_25 is selected */\r
1235   if (ADC_Channel > ADC_Channel_19)\r
1236   {\r
1237     /* Get the old register value */\r
1238     tmpreg1 = ADCx->SMPR1;\r
1239     /* Calculate the mask to clear */\r
1240     tmpreg2 = SMPR1_SMP_SET << (3 * (ADC_Channel - 20));\r
1241     /* Clear the old sample time */\r
1242     tmpreg1 &= ~tmpreg2;\r
1243     /* Calculate the mask to set */\r
1244     tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 20));\r
1245     /* Set the new sample time */\r
1246     tmpreg1 |= tmpreg2;\r
1247     /* Store the new register value */\r
1248     ADCx->SMPR1 = tmpreg1;\r
1249   }\r
1250   \r
1251    /* if ADC_Channel_10 ... ADC_Channel_19 is selected */\r
1252   else if (ADC_Channel > ADC_Channel_9)\r
1253   {\r
1254     /* Get the old register value */\r
1255     tmpreg1 = ADCx->SMPR2;\r
1256     /* Calculate the mask to clear */\r
1257     tmpreg2 = SMPR2_SMP_SET << (3 * (ADC_Channel - 10));\r
1258     /* Clear the old sample time */\r
1259     tmpreg1 &= ~tmpreg2;\r
1260     /* Calculate the mask to set */\r
1261     tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));\r
1262     /* Set the new sample time */\r
1263     tmpreg1 |= tmpreg2;\r
1264     /* Store the new register value */\r
1265     ADCx->SMPR2 = tmpreg1;\r
1266   }\r
1267   \r
1268   else /* ADC_Channel include in ADC_Channel_[0..9] */\r
1269   {\r
1270     /* Get the old register value */\r
1271     tmpreg1 = ADCx->SMPR3;\r
1272     /* Calculate the mask to clear */\r
1273     tmpreg2 = SMPR3_SMP_SET << (3 * ADC_Channel);\r
1274     /* Clear the old sample time */\r
1275     tmpreg1 &= ~tmpreg2;\r
1276     /* Calculate the mask to set */\r
1277     tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);\r
1278     /* Set the new sample time */\r
1279     tmpreg1 |= tmpreg2;\r
1280     /* Store the new register value */\r
1281     ADCx->SMPR3 = tmpreg1;\r
1282   }\r
1283   \r
1284   /* Rank configuration */\r
1285   /* Get the old register value */\r
1286   tmpreg1 = ADCx->JSQR;\r
1287   /* Get JL value: Number = JL+1 */\r
1288   tmpreg3 =  (tmpreg1 & JSQR_JL_SET)>> 20;\r
1289   /* Calculate the mask to clear: ((Rank-1)+(4- (JL+1))) */ \r
1290   tmpreg2 = JSQR_JSQ_SET << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));\r
1291   /* Clear the old JSQx bits for the selected rank */\r
1292   tmpreg1 &= ~tmpreg2;\r
1293   /* Calculate the mask to set: ((Rank-1)+(4- (JL+1))) */ \r
1294   tmpreg2 = (uint32_t)ADC_Channel << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));\r
1295   /* Set the JSQx bits for the selected rank */\r
1296   tmpreg1 |= tmpreg2;\r
1297   /* Store the new register value */\r
1298   ADCx->JSQR = tmpreg1;\r
1299 }\r
1300 \r
1301 /**\r
1302   * @brief  Configures the sequencer length for injected channels\r
1303   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1304   * @param  Length: The sequencer length. \r
1305   *         This parameter must be a number between 1 to 4.\r
1306   * @retval None\r
1307   */\r
1308 void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length)\r
1309 {\r
1310   uint32_t tmpreg1 = 0;\r
1311   uint32_t tmpreg2 = 0;\r
1312 \r
1313   /* Check the parameters */\r
1314   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1315   assert_param(IS_ADC_INJECTED_LENGTH(Length));\r
1316   \r
1317   /* Get the old register value */\r
1318   tmpreg1 = ADCx->JSQR;\r
1319   /* Clear the old injected sequence length JL bits */\r
1320   tmpreg1 &= JSQR_JL_RESET;\r
1321   /* Set the injected sequence length JL bits */\r
1322   tmpreg2 = Length - 1; \r
1323   tmpreg1 |= tmpreg2 << 20;\r
1324   /* Store the new register value */\r
1325   ADCx->JSQR = tmpreg1;\r
1326 }\r
1327 \r
1328 /**\r
1329   * @brief  Set the injected channels conversion value offset\r
1330   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1331   * @param  ADC_InjectedChannel: the ADC injected channel to set its offset. \r
1332   *   This parameter can be one of the following values:\r
1333   *     @arg ADC_InjectedChannel_1: Injected Channel1 selected\r
1334   *     @arg ADC_InjectedChannel_2: Injected Channel2 selected\r
1335   *     @arg ADC_InjectedChannel_3: Injected Channel3 selected\r
1336   *     @arg ADC_InjectedChannel_4: Injected Channel4 selected\r
1337   * @param  Offset: the offset value for the selected ADC injected channel\r
1338   *         This parameter must be a 12bit value.\r
1339   * @retval None\r
1340   */\r
1341 void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset)\r
1342 {\r
1343   __IO uint32_t tmp = 0;\r
1344   \r
1345   /* Check the parameters */\r
1346   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1347   assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));\r
1348   assert_param(IS_ADC_OFFSET(Offset));  \r
1349   \r
1350   tmp = (uint32_t)ADCx;\r
1351   tmp += ADC_InjectedChannel;\r
1352   \r
1353   /* Set the selected injected channel data offset */\r
1354   *(__IO uint32_t *) tmp = (uint32_t)Offset;\r
1355 }\r
1356 \r
1357 /**\r
1358   * @brief  Configures the ADCx external trigger for injected channels conversion.\r
1359   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1360   * @param  ADC_ExternalTrigInjecConv: specifies the ADC trigger to start injected \r
1361   *    conversion. This parameter can be one of the following values:                    \r
1362   *     @arg ADC_ExternalTrigInjecConv_T9_CC1: Timer9 capture compare1 selected \r
1363   *     @arg ADC_ExternalTrigInjecConv_T9_TRGO: Timer9 TRGO event selected \r
1364   *     @arg ADC_ExternalTrigInjecConv_T2_TRGO: Timer2 TRGO event selected \r
1365   *     @arg ADC_ExternalTrigInjecConv_T2_CC1: Timer2 capture compare1 selected\r
1366   *     @arg ADC_ExternalTrigInjecConv_T3_CC4: Timer3 capture compare4 selected \r
1367   *     @arg ADC_ExternalTrigInjecConv_T4_TRGO: Timer4 TRGO event selected \r
1368   *     @arg ADC_ExternalTrigInjecConv_T4_CC1: Timer4 capture compare1 selected                       \r
1369   *     @arg ADC_ExternalTrigInjecConv_T4_CC2: Timer4 capture compare2 selected \r
1370   *     @arg ADC_ExternalTrigInjecConv_T4_CC3: Timer4 capture compare3 selected                        \r
1371   *     @arg ADC_ExternalTrigInjecConv_T10_CC1: Timer10 capture compare1 selected\r
1372   *     @arg ADC_ExternalTrigInjecConv_T7_TRGO: Timer7 TRGO event selected                         \r
1373   *     @arg ADC_ExternalTrigInjecConv_Ext_IT15: External interrupt line 15 event selected                          \r
1374   * @retval None\r
1375   */\r
1376 void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv)\r
1377 {\r
1378   uint32_t tmpreg = 0;\r
1379 \r
1380   /* Check the parameters */\r
1381   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1382   assert_param(IS_ADC_EXT_INJEC_TRIG(ADC_ExternalTrigInjecConv));\r
1383 \r
1384   /* Get the old register value */\r
1385   tmpreg = ADCx->CR2;\r
1386   /* Clear the old external event selection for injected group */\r
1387   tmpreg &= CR2_JEXTSEL_RESET;\r
1388   /* Set the external event selection for injected group */\r
1389   tmpreg |= ADC_ExternalTrigInjecConv;\r
1390   /* Store the new register value */\r
1391   ADCx->CR2 = tmpreg;\r
1392 }\r
1393 \r
1394 /**\r
1395   * @brief  Configures the ADCx external trigger edge for injected channels conversion.\r
1396   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1397   * @param  ADC_ExternalTrigInjecConvEdge: specifies the ADC external trigger\r
1398   *         edge to start injected conversion. \r
1399   *   This parameter can be one of the following values:\r
1400   *     @arg ADC_ExternalTrigConvEdge_None: external trigger disabled for \r
1401   *          injected conversion\r
1402   *     @arg ADC_ExternalTrigConvEdge_Rising: detection on rising edge\r
1403   *     @arg ADC_ExternalTrigConvEdge_Falling: detection on falling edge\r
1404   *     @arg ADC_External ADC_ExternalTrigConvEdge_RisingFalling: detection on \r
1405   *          both rising and falling edge\r
1406   * @retval None\r
1407   */\r
1408 void ADC_ExternalTrigInjectedConvEdgeConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConvEdge)\r
1409 {\r
1410   uint32_t tmpreg = 0;\r
1411 \r
1412   /* Check the parameters */\r
1413   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1414   assert_param(IS_ADC_EXT_INJEC_TRIG_EDGE(ADC_ExternalTrigInjecConvEdge));\r
1415 \r
1416   /* Get the old register value */\r
1417   tmpreg = ADCx->CR2;\r
1418   /* Clear the old external trigger edge for injected group */\r
1419   tmpreg &= CR2_JEXTEN_RESET;\r
1420   /* Set the new external trigger edge for injected group */\r
1421   tmpreg |= ADC_ExternalTrigInjecConvEdge;\r
1422   /* Store the new register value */\r
1423   ADCx->CR2 = tmpreg;\r
1424 }\r
1425 \r
1426 /**\r
1427   * @brief  Enables the selected ADC software start conversion of the injected \r
1428   *         channels.\r
1429   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1430   * @retval None\r
1431   */\r
1432 void ADC_SoftwareStartInjectedConv(ADC_TypeDef* ADCx)\r
1433 {\r
1434   /* Check the parameters */\r
1435   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1436   /* Enable the selected ADC conversion for injected group */\r
1437   ADCx->CR2 |= (uint32_t)ADC_CR2_JSWSTART;\r
1438 }\r
1439 \r
1440 /**\r
1441   * @brief  Gets the selected ADC Software start injected conversion Status.\r
1442   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1443   * @retval The new state of ADC software start injected conversion (SET or RESET).\r
1444   */\r
1445 FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx)\r
1446 {\r
1447   FlagStatus bitstatus = RESET;\r
1448 \r
1449   /* Check the parameters */\r
1450   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1451 \r
1452   /* Check the status of JSWSTART bit */\r
1453   if ((ADCx->CR2 & ADC_CR2_JSWSTART) != (uint32_t)RESET)\r
1454   {\r
1455     /* JSWSTART bit is set */\r
1456     bitstatus = SET;\r
1457   }\r
1458   else\r
1459   {\r
1460     /* JSWSTART bit is reset */\r
1461     bitstatus = RESET;\r
1462   }\r
1463   /* Return the JSWSTART bit status */\r
1464   return  bitstatus;\r
1465 }\r
1466 \r
1467 /**\r
1468   * @brief  Enables or disables the selected ADC automatic injected group\r
1469   *         conversion after regular one.\r
1470   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1471   * @param  NewState: new state of the selected ADC auto injected\r
1472   *         conversion. \r
1473   *         This parameter can be: ENABLE or DISABLE.\r
1474   * @retval None\r
1475   */\r
1476 void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
1477 {\r
1478   /* Check the parameters */\r
1479   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1480   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1481 \r
1482   if (NewState != DISABLE)\r
1483   {\r
1484     /* Enable the selected ADC automatic injected group conversion */\r
1485     ADCx->CR1 |= (uint32_t)ADC_CR1_JAUTO;\r
1486   }\r
1487   else\r
1488   {\r
1489     /* Disable the selected ADC automatic injected group conversion */\r
1490     ADCx->CR1 &= (uint32_t)(~ADC_CR1_JAUTO);\r
1491   }\r
1492 }\r
1493 \r
1494 /**\r
1495   * @brief  Enables or disables the discontinuous mode for injected group\r
1496   *         channel for the specified ADC\r
1497   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1498   * @param  NewState: new state of the selected ADC discontinuous mode\r
1499   *         on injected group channel. This parameter can be: ENABLE or DISABLE.\r
1500   * @retval None\r
1501   */\r
1502 void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)\r
1503 {\r
1504   /* Check the parameters */\r
1505   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1506   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1507 \r
1508   if (NewState != DISABLE)\r
1509   {\r
1510     /* Enable the selected ADC injected discontinuous mode */\r
1511     ADCx->CR1 |= (uint32_t)ADC_CR1_JDISCEN;\r
1512   }\r
1513   else\r
1514   {\r
1515     /* Disable the selected ADC injected discontinuous mode */\r
1516     ADCx->CR1 &= (uint32_t)(~ADC_CR1_JDISCEN);\r
1517   }\r
1518 }\r
1519 \r
1520 /**\r
1521   * @brief  Returns the ADC injected channel conversion result\r
1522   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1523   * @param  ADC_InjectedChannel: the converted ADC injected channel.\r
1524   *   This parameter can be one of the following values:\r
1525   *     @arg ADC_InjectedChannel_1: Injected Channel1 selected\r
1526   *     @arg ADC_InjectedChannel_2: Injected Channel2 selected\r
1527   *     @arg ADC_InjectedChannel_3: Injected Channel3 selected\r
1528   *     @arg ADC_InjectedChannel_4: Injected Channel4 selected\r
1529   * @retval The Data conversion value.\r
1530   */\r
1531 uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel)\r
1532 {\r
1533   __IO uint32_t tmp = 0;\r
1534   \r
1535   /* Check the parameters */\r
1536   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1537   assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));\r
1538 \r
1539   tmp = (uint32_t)ADCx;\r
1540   tmp += ADC_InjectedChannel + JDR_OFFSET;\r
1541   \r
1542   /* Returns the selected injected channel conversion data value */\r
1543   return (uint16_t) (*(__IO uint32_t*)  tmp); \r
1544 }\r
1545 \r
1546 /**\r
1547   * @}\r
1548   */\r
1549 \r
1550 /** @defgroup ADC_Group8 Interrupts and flags management functions\r
1551  *  @brief   Interrupts and flags management functions\r
1552  *\r
1553 @verbatim   \r
1554  ===============================================================================\r
1555                    Interrupts and flags management functions\r
1556  ===============================================================================  \r
1557 \r
1558   This section provides functions allowing to configure the ADC Interrupts and get \r
1559   the status and clear flags and Interrupts pending bits.\r
1560   \r
1561   The ADC provide 4 Interrupts sources and 9 Flags which can be divided into 3 groups:\r
1562   \r
1563   I. Flags and Interrupts for ADC regular channels\r
1564   =================================================\r
1565   Flags :\r
1566   ---------- \r
1567      1. ADC_FLAG_OVR : Overrun detection when regular converted data are lost\r
1568 \r
1569      2. ADC_FLAG_EOC : Regular channel end of conversion + to indicate (depending \r
1570               on EOCS bit, managed by ADC_EOCOnEachRegularChannelCmd() ) the end of :\r
1571                ==> a regular CHANNEL conversion \r
1572                ==> sequence of regular GROUP conversions .\r
1573 \r
1574      3. ADC_FLAG_STRT: Regular channel start + to indicate when regular CHANNEL \r
1575               conversion starts.\r
1576               \r
1577      4. ADC_FLAG_RCNR: Regular channel not ready+ to indicate if a new regular \r
1578               conversion can be launched\r
1579      \r
1580   Interrupts :\r
1581   ------------\r
1582      1. ADC_IT_OVR \r
1583      2. ADC_IT_EOC \r
1584   \r
1585   \r
1586   II. Flags and Interrupts for ADC Injected channels\r
1587   =================================================\r
1588   Flags :\r
1589   ---------- \r
1590      1. ADC_FLAG_JEOC : Injected channel end of conversion+ to indicate at \r
1591                the end of injected GROUP conversion  \r
1592               \r
1593      2. ADC_FLAG_JSTRT: Injected channel start +  to indicate hardware when \r
1594                injected GROUP conversion starts.\r
1595 \r
1596      3. ADC_FLAG_JCNR: Injected channel not ready + to indicate if a new \r
1597                injected conversion can be launched.\r
1598 \r
1599   Interrupts :\r
1600   ------------\r
1601      1. ADC_IT_JEOC     \r
1602 \r
1603   III. General Flags and Interrupts for the ADC\r
1604   ================================================= \r
1605   Flags :\r
1606   ---------- \r
1607      1. ADC_FLAG_AWD: Analog watchdog + to indicate if the converted voltage \r
1608               crosses the programmed thresholds values.\r
1609               \r
1610      2. ADC_FLAG_ADONS: ADC ON status + to indicate if the ADC is ready to convert.\r
1611  \r
1612   Interrupts :\r
1613   ------------\r
1614      1. ADC_IT_AWD \r
1615 \r
1616 @endverbatim\r
1617   * @{\r
1618   */ \r
1619 \r
1620 /**\r
1621   * @brief  Enables or disables the specified ADC interrupts.\r
1622   * @param  ADCx: where x can be 1 to select the ADC peripheral.\r
1623   * @param  ADC_IT: specifies the ADC interrupt sources to be enabled or disabled. \r
1624   *   This parameter can be one of the following values:\r
1625   *     @arg ADC_IT_EOC: End of conversion interrupt \r
1626   *     @arg ADC_IT_AWD: Analog watchdog interrupt \r
1627   *     @arg ADC_IT_JEOC: End of injected conversion interrupt \r
1628   *     @arg ADC_IT_OVR: overrun interrupt                        \r
1629   * @param  NewState: new state of the specified ADC interrupts.\r
1630   *         This parameter can be: ENABLE or DISABLE.\r
1631   * @retval None\r
1632   */\r
1633 void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState)  \r
1634 {\r
1635   uint32_t itmask = 0;\r
1636 \r
1637   /* Check the parameters */\r
1638   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1639   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1640   assert_param(IS_ADC_IT(ADC_IT)); \r
1641 \r
1642   /* Get the ADC IT index */\r
1643   itmask = (uint8_t)ADC_IT;\r
1644   itmask = (uint32_t)0x01 << itmask;    \r
1645 \r
1646   if (NewState != DISABLE)\r
1647   {\r
1648     /* Enable the selected ADC interrupts */\r
1649     ADCx->CR1 |= itmask;\r
1650   }\r
1651   else\r
1652   {\r
1653     /* Disable the selected ADC interrupts */\r
1654     ADCx->CR1 &= (~(uint32_t)itmask);\r
1655   }\r
1656 }\r
1657 \r
1658 /**\r
1659   * @brief  Checks whether the specified ADC flag is set or not.\r
1660   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1661   * @param  ADC_FLAG: specifies the flag to check. \r
1662   *   This parameter can be one of the following values:\r
1663   *     @arg ADC_FLAG_AWD: Analog watchdog flag\r
1664   *     @arg ADC_FLAG_EOC: End of conversion flag\r
1665   *     @arg ADC_FLAG_JEOC: End of injected group conversion flag\r
1666   *     @arg ADC_FLAG_JSTRT: Start of injected group conversion flag\r
1667   *     @arg ADC_FLAG_STRT: Start of regular group conversion flag\r
1668   *     @arg ADC_FLAG_OVR: Overrun flag   \r
1669   *     @arg ADC_FLAG_ADONS: ADC ON status \r
1670   *     @arg ADC_FLAG_RCNR: Regular channel not ready \r
1671   *     @arg ADC_FLAG_JCNR: Injected channel not ready \r
1672   * @retval The new state of ADC_FLAG (SET or RESET).\r
1673   */\r
1674 FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint16_t ADC_FLAG)\r
1675 {\r
1676   FlagStatus bitstatus = RESET;\r
1677 \r
1678   /* Check the parameters */\r
1679   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1680   assert_param(IS_ADC_GET_FLAG(ADC_FLAG));\r
1681 \r
1682   /* Check the status of the specified ADC flag */\r
1683   if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET)\r
1684   {\r
1685     /* ADC_FLAG is set */\r
1686     bitstatus = SET;\r
1687   }\r
1688   else\r
1689   {\r
1690     /* ADC_FLAG is reset */\r
1691     bitstatus = RESET;\r
1692   }\r
1693   /* Return the ADC_FLAG status */\r
1694   return  bitstatus;\r
1695 }\r
1696 \r
1697 /**\r
1698   * @brief  Clears the ADCx's pending flags.\r
1699   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1700   * @param  ADC_FLAG: specifies the flag to clear. \r
1701   *   This parameter can be any combination of the following values:\r
1702   *     @arg ADC_FLAG_AWD: Analog watchdog flag\r
1703   *     @arg ADC_FLAG_EOC: End of conversion flag\r
1704   *     @arg ADC_FLAG_JEOC: End of injected group conversion flag\r
1705   *     @arg ADC_FLAG_JSTRT: Start of injected group conversion flag\r
1706   *     @arg ADC_FLAG_STRT: Start of regular group conversion flag\r
1707   *     @arg ADC_FLAG_OVR: overrun flag                                              \r
1708   * @retval None\r
1709   */\r
1710 void ADC_ClearFlag(ADC_TypeDef* ADCx, uint16_t ADC_FLAG)\r
1711 {\r
1712   /* Check the parameters */\r
1713   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1714   assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));\r
1715 \r
1716   /* Clear the selected ADC flags */\r
1717   ADCx->SR = ~(uint32_t)ADC_FLAG;\r
1718 }\r
1719 \r
1720 /**\r
1721   * @brief  Checks whether the specified ADC interrupt has occurred or not.\r
1722   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1723   * @param  ADC_IT: specifies the ADC interrupt source to check. \r
1724   *   This parameter can be one of the following values:\r
1725   *     @arg ADC_IT_EOC: End of conversion interrupt \r
1726   *     @arg ADC_IT_AWD: Analog watchdog interrupt \r
1727   *     @arg ADC_IT_JEOC: End of injected conversion interrupt \r
1728   *     @arg ADC_IT_OVR: Overrun interrupt                         \r
1729   * @retval The new state of ADC_IT (SET or RESET).\r
1730   */\r
1731 ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT)\r
1732 {\r
1733   ITStatus bitstatus = RESET;\r
1734   uint32_t itmask = 0, enablestatus = 0;\r
1735 \r
1736   /* Check the parameters */\r
1737   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1738   assert_param(IS_ADC_IT(ADC_IT));\r
1739 \r
1740   /* Get the ADC IT index */\r
1741   itmask = (uint32_t)((uint32_t)ADC_IT >> 8);\r
1742 \r
1743   /* Get the ADC_IT enable bit status */\r
1744   enablestatus = (ADCx->CR1 & ((uint32_t)0x01 << (uint8_t)ADC_IT)); \r
1745 \r
1746   /* Check the status of the specified ADC interrupt */\r
1747   if (((uint32_t)(ADCx->SR & (uint32_t)itmask) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))\r
1748   {                                                         \r
1749     /* ADC_IT is set */\r
1750     bitstatus = SET;\r
1751   }\r
1752   else\r
1753   {\r
1754     /* ADC_IT is reset */\r
1755     bitstatus = RESET;\r
1756   }\r
1757   /* Return the ADC_IT status */\r
1758   return  bitstatus;\r
1759 }\r
1760 \r
1761 /**\r
1762   * @brief  Clears the ADCx\92s interrupt pending bits.\r
1763   * @param  ADCx: where x can be 1 to select the ADC1 peripheral.\r
1764   * @param  ADC_IT: specifies the ADC interrupt pending bit to clear.\r
1765   *   This parameter can be one of the following values:\r
1766   *     @arg ADC_IT_EOC: End of conversion interrupt \r
1767   *     @arg ADC_IT_AWD: Analog watchdog interrupt \r
1768   *     @arg ADC_IT_JEOC: End of injected conversion interrupt \r
1769   *     @arg ADC_IT_OVR: Overrun interrupt                          \r
1770   * @retval None\r
1771   */\r
1772 void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT)\r
1773 {\r
1774   uint8_t itmask = 0;\r
1775 \r
1776   /* Check the parameters */\r
1777   assert_param(IS_ADC_ALL_PERIPH(ADCx));\r
1778   assert_param(IS_ADC_IT(ADC_IT)); \r
1779 \r
1780   /* Get the ADC IT index */\r
1781   itmask = (uint8_t)(ADC_IT >> 8);\r
1782 \r
1783   /* Clear the selected ADC interrupt pending bits */\r
1784   ADCx->SR = ~(uint32_t)itmask;\r
1785 }\r
1786 \r
1787 /**\r
1788   * @}\r
1789   */\r
1790 \r
1791 /**\r
1792   * @}\r
1793   */ \r
1794 \r
1795 /**\r
1796   * @}\r
1797   */ \r
1798 \r
1799 /**\r
1800   * @}\r
1801   */ \r
1802 \r
1803 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r