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