]> begriffs open source - cmsis-freertos/blob - Demo/ARM7_STR75x_GCC/STLibrary/src/75x_adc.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / ARM7_STR75x_GCC / STLibrary / src / 75x_adc.c
1 /******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
2 * File Name          : 75x_adc.c
3 * Author             : MCD Application Team
4 * Date First Issued  : 03/10/2006
5 * Description        : This file provides all the ADC software functions.
6 ********************************************************************************
7 * History:
8 * 07/17/2006 : V1.0
9 * 03/10/2006 : V0.1
10 ********************************************************************************
11 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
13 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 
14 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
15 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 
16 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *******************************************************************************/
18
19 /* Includes ------------------------------------------------------------------*/
20 #include "75x_adc.h"
21 #include "75x_mrcc.h"
22
23 /* Private typedef -----------------------------------------------------------*/
24 /* Private define ------------------------------------------------------------*/
25 /* Private macro -------------------------------------------------------------*/
26 /* Private variables ---------------------------------------------------------*/
27
28 /* Mask for Power Down Mode */
29 #define ADC_PowerDown_Enable  0x8000
30 #define ADC_PowerDown_Disable 0x7FFF
31
32 /* Mask for Watchdog Thresholds Enable */
33 #define ADC_AnalogWatchdog_Enable  0x8000
34 #define ADC_AnalogWatchdog_Disable 0x7FFF
35
36 /* Mask for Injected conversion start */
37 #define ADC_Injec_ConversionStart  0x8000
38
39 /* DMA enable */
40 #define ADC_DMA_ExtEnable_Mask  0x4000
41
42 /* Injected start trigger enable */
43 #define ADC_Injec_ExtTrigger_Enable   0x4000
44
45 /* ADC Masks */
46 #define ADC_DMAFirstEnabledChannel_Mask  0x000F 
47 #define ADC_DataRegisterOffset           0x0050
48 #define ADC_FirstChannel_Mask            0xFFF0
49 #define ADC_ChannelNumber_Mask           0xFC3F
50 #define ADC_Threshold_Mask               0xFC00
51 #define ADC_AnalogWatchdogChannel_Mask   0xC3FF
52 #define ADC_Prescalers_Mask              0x7F18
53 #define ADC_SPEN_Mask                    0x8000
54 #define ADC_FallingEdge_Mask             0xEFFF
55 #define ADC_LowLevel_Mask                0x4000
56 #define ADC_HighLevel_Mask               0xDFFF
57 #define ADC_Calibration_Mask             0x0002
58         
59 /* Private function prototypes -----------------------------------------------*/
60 /* Private functions ---------------------------------------------------------*/
61
62 /*******************************************************************************
63 * Function Name  : ADC_DeInit                                           
64 * Description    : Deinitializes the ADC peripheral registers to their default
65 *                  reset values.
66 * Input          : None.  
67 * Output         : None                                              
68 * Return         : None.                                                
69 *******************************************************************************/
70 void ADC_DeInit(void)
71 {
72   /* Reset the ADC registers values*/
73   MRCC_PeripheralSWResetConfig(MRCC_Peripheral_ADC,ENABLE);
74   MRCC_PeripheralSWResetConfig(MRCC_Peripheral_ADC,DISABLE); 
75 }
76
77 /*******************************************************************************
78 * Function Name  : ADC_Init                                                 
79 * Description    : Initializes the ADC  peripheral according to the specified
80 *                  parameters in the ADC_InitStruct.
81 * Input          : - ADC_InitStruct: pointer to an ADC_InitTypeDef structure that
82                    contains the configuration information for the ADC peripheral.
83 * Output         : None                                                      
84 * Return         : None                                                      
85 *******************************************************************************/
86 void ADC_Init(ADC_InitTypeDef* ADC_InitStruct)
87 {
88   /* Configure the conversion mode */
89   if(ADC_InitStruct->ADC_ConversionMode == ADC_ConversionMode_Scan)
90   {
91     /* Set the scan conversion mode */
92     ADC->CLR2 |= ADC_ConversionMode_Scan;
93   }
94   else
95   {
96     /* Set the one-shot conversion mode */
97     ADC->CLR2 &= ADC_ConversionMode_OneShot;
98   }
99   
100   /* Configure the external start conversion trigger */
101   switch(ADC_InitStruct->ADC_ExtTrigger)
102   {
103     case ADC_ExtTrigger_HighLevel:
104       /* Start conversion on High level of the external trigger (TIM0) */
105       ADC->CLR0 &= ADC_HighLevel_Mask;
106       ADC->CLR0 |= ADC_ExtTrigger_HighLevel;
107       break;
108       
109     case ADC_ExtTrigger_LowLevel:
110       /* Start conversion on low level of the external trigger (TIM0) */
111       ADC->CLR0 &= ADC_ExtTrigger_LowLevel; 
112       ADC->CLR0 |= ADC_LowLevel_Mask;
113       break;
114       
115     case ADC_ExtTrigger_RisingEdge:
116       /* Start conversion on rising edge of the external trigger (TIM0) */
117       ADC->CLR0 |= ADC_ExtTrigger_RisingEdge;
118       break;
119     
120     case ADC_ExtTrigger_FallingEdge:
121       /* Start conversion on falling edge of the external trigger (TIM0) */
122       ADC->CLR0 &= ADC_FallingEdge_Mask;
123       ADC->CLR0 |= ADC_ExtTrigger_FallingEdge;
124       break;
125     
126     case ADC_ExtTrigger_Disable:
127       /* Disable the external trigger and start the conversion by software */
128       ADC->CLR0 &= ADC_ExtTrigger_Disable;
129       break;
130
131     default:
132       break; 
133   }
134
135   /* Configure the auto clock off feature */
136   if (ADC_InitStruct->ADC_AutoClockOff == ADC_AutoClockOff_Enable)
137   {
138     /* Enable the auto clock off feature */
139     ADC->CLR4 |= ADC_AutoClockOff_Enable;
140   }
141   else
142   {
143     /* Disable the auto clock off feature */
144     ADC->CLR4 &= ADC_AutoClockOff_Disable;      
145   }
146   
147   /* Clear conversion prescaler CNVP[2:0], sampling prescaler SMPP[2:0] bits 
148      and Sample prescaler enable SPEN bit */
149   ADC->CLR1 &= ADC_Prescalers_Mask;
150   /* Set conversion prescaler value (sampling and conversion prescalers are equal
151      while SPEN bit is reset */ 
152   ADC->CLR1 |= (ADC_InitStruct->ADC_ConversionPrescaler<<5);
153   
154   /* In case ADC_SamplingPrescaler member is different from the conversion one */
155   if(ADC_InitStruct->ADC_SamplingPrescaler != ADC_InitStruct->ADC_ConversionPrescaler)
156   {
157     /* Set the sampling prescaler value */
158     ADC->CLR1 |= ADC_InitStruct->ADC_SamplingPrescaler;
159     /* Set SPEN bit (sampling and conversion prescalers are different */
160     ADC->CLR1 = (ADC->CLR1 | ADC_SPEN_Mask);    
161   }
162   
163   /* Clear first channel to be converted FCH[3:0] bits */
164   ADC->CLR2 &= ADC_FirstChannel_Mask;
165   /* Set the first channel to be converted */
166   ADC->CLR2 |= ADC_InitStruct->ADC_FirstChannel;
167   /* Clear number of channels to be converted NCH[3:0] bits */
168   ADC->CLR2 &= ADC_ChannelNumber_Mask;  
169   /* Set the number of channels to be converted */
170   ADC->CLR2 |= ((ADC_InitStruct->ADC_ChannelNumber)-1<<6);
171 }
172
173 /*******************************************************************************
174 * Function Name  : ADC_StructInit                                       
175 * Description    : Fills each ADC_InitStruct member with its default value.
176 * Input          : - ADC_InitStruct: pointer to an ADC_InitTypeDef structure
177                      which will be initialized.  
178 * Output         : None 
179 * Return         : None.
180 *******************************************************************************/
181 void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
182 {
183   /* Initialize the ADC_ConversionMode member */
184   ADC_InitStruct->ADC_ConversionMode = ADC_ConversionMode_OneShot;
185   
186   /* Initialize the ADC_ExtTrigger member */
187   ADC_InitStruct->ADC_ExtTrigger = ADC_ExtTrigger_Disable;
188   
189   /* Initialize the ADC_AutoClockOff member */
190   ADC_InitStruct->ADC_AutoClockOff = ADC_AutoClockOff_Disable;
191   
192   /* Initialize the ADC_SamplingPrescaler member */
193   ADC_InitStruct->ADC_SamplingPrescaler = 0;
194   
195   /* Initialize the ADC_ConversionPrescaler member */
196   ADC_InitStruct->ADC_ConversionPrescaler = 0;
197   
198   /* Initialize the ADC_FirstChannel member */
199   ADC_InitStruct->ADC_FirstChannel = ADC_CHANNEL0;
200   
201   /* Initialize the ADC_ChannelNumber member */
202   ADC_InitStruct->ADC_ChannelNumber = 1;
203  }
204
205 /*******************************************************************************
206 * Function Name  : ADC_StartCalibration                                       
207 * Description    : Starts the ADC Calibration. Calibration average enabled/disabled.
208 * Input          : - ADC_CalibAverage: Enables or disables ADC calibration average.
209 *                    This parameter can be one of the following values:
210 *                         - ADC_CalibAverage_Enable:  enable calibration average 
211 *                         - ADC_CalibAverage_Disable: disable calibration average  
212 * Output         : None 
213 * Return         : None                                                       
214 *******************************************************************************/
215 void ADC_StartCalibration(u16 ADC_CalibAverage)
216 {
217   if (ADC_CalibAverage == ADC_CalibAverage_Enable)
218   {
219     /* Enable ADC Calibration Average */
220     ADC->CLR4 &= ADC_CalibAverage_Enable;
221   }
222   else
223   {
224     /* Disable ADC Calibration Average */
225     ADC->CLR4 |= ADC_CalibAverage_Disable;
226   }
227
228   /* Start Calibration */
229   ADC->CLR0 |= ADC_Calibration_ON;
230 }
231
232 /*******************************************************************************
233 * Function Name  : ADC_GetCalibrationStatus
234 * Description    : Get the ADC Calibration Status.
235 * Input          : None
236 * Output         : None 
237 * Return         : The NewState of the ADC calibration (SET or RESET).
238 *******************************************************************************/
239 FlagStatus ADC_GetCalibrationStatus(void)
240 {
241   /* Check the status of the ADC calibration */
242   if((ADC->CLR0 & ADC_Calibration_Mask) != RESET)
243   {
244     /* Return SET if ADC Calibration is on going */
245     return SET;
246   }
247   else
248   {
249     /* Return RESET if ADC Calibration is finished */
250     return RESET;
251   }
252 }
253
254 /*******************************************************************************
255 * Function Name  : ADC_ConversionCmd
256 * Description    : Starts or stops the ADC conversion.
257 * Input          : - ADC_Conversion: specifies the ADC command to apply.
258 *                    This parameter can be one of the following values:
259 *                         - ADC_Conversion_Start: start conversion 
260 *                         - ADC_Conversion_Stop:  stop conversion 
261 * Output         : None
262 * Return         : None
263 *******************************************************************************/
264 void ADC_ConversionCmd (u16 ADC_Conversion)
265 {
266   if (ADC_Conversion == ADC_Conversion_Start)
267   {
268     /* Start the ADC Conversion */
269     ADC->CLR0 |= ADC_Conversion_Start;
270   }
271   else
272   {
273     /* Stop the ADC Conversion */
274     ADC->CLR0 &= ADC_Conversion_Stop;
275   }
276 }
277
278 /*******************************************************************************
279 * Function Name  : ADC_GetSTARTBitStatus
280 * Description    : Gets the ADC START/STOP bit Status.
281 * Input          : None
282 * Output         : None 
283 * Return         : The NewState of the ADC START/STOP bit (SET or RESET).
284 *******************************************************************************/
285 FlagStatus ADC_GetSTARTBitStatus(void)
286 {
287   /* Check the status of the ADC START/STOP bit */
288   if((ADC->CLR0 & ADC_Conversion_Start) != RESET)
289   {
290     /* Return SET if ADC Conversion is started */
291     return SET;
292   }
293   else
294   {
295     /* Return RESET if ADC Conversion is stopped */
296     return RESET;
297   }
298 }
299
300 /*******************************************************************************
301 * Function Name  : ADC_Cmd
302 * Description    : Enables the ADC peripheral or puts it in power down mode.
303 *                  - NewState: new state of the ADC peripheral. 
304 *                    This parameter can be: ENABLE or DISABLE.
305 * Output         : None  
306 * Return         : None.
307 *******************************************************************************/
308 void ADC_Cmd(FunctionalState NewState)
309 {
310   if (NewState == DISABLE)
311   {
312     /* Enable ADC Power Down Mode */
313     ADC->CLR4 |= ADC_PowerDown_Enable;
314   }
315   else
316   {
317     /* Disable ADC Power Down Mode */
318     ADC->CLR4 &= ADC_PowerDown_Disable;
319   }
320 }
321
322 /*******************************************************************************
323 * Function Name  : ADC_AutoClockOffConfig                                
324 * Description    : Enables or disables the Auto clock off feature.
325 *                  - NewState: new state of the Auto clock off feature. This 
326 *                    parameter can be: ENABLE or DISABLE.  
327 * Output         : None   
328 * Return         : None.                                                 
329 *******************************************************************************/
330 void ADC_AutoClockOffConfig(FunctionalState NewState)
331 {
332   if (NewState == ENABLE)
333   {
334     /* Enable ADC Auto Clock Off */
335     ADC->CLR4 |= ADC_AutoClockOff_Enable;
336   }
337   else
338   {
339     /* Disable ADC Auto Clock Off */
340     ADC->CLR4 &= ADC_AutoClockOff_Disable;
341   }
342 }
343
344 /*******************************************************************************
345 * Function Name  : ADC_AnalogWatchdogConfig                                       
346 * Description    : Configures the analog input channel to be used for the selected
347 *                  Analog Watchdog and defines its corresponding High and Low 
348 *                  threshold values.               
349 * Input          : - ADC_AnalogWatchdog: specifies the analog watchdog which will
350 *                    be affected to the desired converted channel. This parameter
351 *                    can be one of the following values: 
352 *                     - ADC_AnalogWatchdog0: select analog watchdog 0
353 *                     - ADC_AnalogWatchdog1: select analog watchdog 1
354 *                     - ADC_AnalogWatchdog2: select analog watchdog 2
355 *                     - ADC_AnalogWatchdog3: select analog watchdog 3
356 *                  - ADC_CHANNEL: specifies the channel linked to the selected 
357 *                    analog watchdog. This parameter can be ADC_CHANNELx where x 
358 *                    can be (0..15)                    
359 *                  - LowThreshold: Low Threshold for the selected Analog watchdog
360 *                  - HighThreshold: High Threshold for the selected Analog watchdog
361 * Output         : None
362 * Return         : None
363 *******************************************************************************/
364 void ADC_AnalogWatchdogConfig(u16 ADC_AnalogWatchdog, u8 ADC_CHANNEL, 
365                               u16 LowThreshold, u16 HighThreshold)
366 {
367   switch (ADC_AnalogWatchdog)
368   {
369     /* Set the selected channel and their corresponding High and Low thresholds */
370     case ADC_AnalogWatchdog0 :
371       ADC->TRA0 = (ADC->TRA0 & ADC_AnalogWatchdogChannel_Mask) | ((u16) ADC_CHANNEL<<10);
372       ADC->TRA0 = (ADC->TRA0 & ADC_Threshold_Mask) |  HighThreshold;
373       ADC->TRB0 = (ADC->TRB0 & ADC_Threshold_Mask) |  LowThreshold;
374       break;
375
376     case ADC_AnalogWatchdog1 :
377       ADC->TRA1 = (ADC->TRA1 & ADC_AnalogWatchdogChannel_Mask) | ((u16) ADC_CHANNEL<<10);
378       ADC->TRA1 = (ADC->TRA1 & ADC_Threshold_Mask) |  HighThreshold;
379       ADC->TRB1 = (ADC->TRB1 & ADC_Threshold_Mask) |  LowThreshold;
380       break;
381
382     case ADC_AnalogWatchdog2 :
383       ADC->TRA2 = (ADC->TRA2 & ADC_AnalogWatchdogChannel_Mask) | ((u16) ADC_CHANNEL<<10);
384       ADC->TRA2 = (ADC->TRA2 & ADC_Threshold_Mask) |  HighThreshold;
385       ADC->TRB2 = (ADC->TRB2 & ADC_Threshold_Mask) |  LowThreshold;
386       break;
387
388     case ADC_AnalogWatchdog3 :
389       ADC->TRA3 = (ADC->TRA3 & ADC_AnalogWatchdogChannel_Mask) | ((u16) ADC_CHANNEL<<10);
390       ADC->TRA3 = (ADC->TRA3 & ADC_Threshold_Mask) |  HighThreshold;
391       ADC->TRB3 = (ADC->TRB3 & ADC_Threshold_Mask) |  LowThreshold;
392       break;
393
394     default:
395       break; 
396   }
397 }
398
399 /*******************************************************************************
400 * Function Name  : ADC_AnalogWatchdogCmd                                   
401 * Description    : Enables or disables the selected analog Watchdog.
402 * Input          : - ADC_AnalogWatchdog: specifies the analog watchdog to be 
403 *                    enabled or disabled. This parameter can be one of the 
404 *                    following values: 
405 *                     - ADC_AnalogWatchdog0: select analog watchdog 0
406 *                     - ADC_AnalogWatchdog1: select analog watchdog 1
407 *                     - ADC_AnalogWatchdog2: select analog watchdog 2
408 *                     - ADC_AnalogWatchdog3: select analog watchdog 3
409 *                  - NewState: new state of the specified analog watchdog.
410 *                    This parameter can be: ENABLE or DISABLE.
411 * Output         : None
412 * Return         : None.                                                   
413 *******************************************************************************/
414 void ADC_AnalogWatchdogCmd(u16 ADC_AnalogWatchdog, FunctionalState NewState)
415 {
416   if (NewState == ENABLE)
417   {
418     /* Enable the selected ADC AnalogWatchdogx */
419     switch (ADC_AnalogWatchdog)
420     {
421       case ADC_AnalogWatchdog0 : 
422         ADC->TRB0 |= ADC_AnalogWatchdog_Enable;
423         break;
424
425       case ADC_AnalogWatchdog1 : 
426         ADC->TRB1 |= ADC_AnalogWatchdog_Enable;  
427         break;
428
429       case ADC_AnalogWatchdog2 : 
430         ADC->TRB2 |= ADC_AnalogWatchdog_Enable;  
431         break;
432
433       case ADC_AnalogWatchdog3 : 
434         ADC->TRB3 |= ADC_AnalogWatchdog_Enable;  
435         break;
436
437       default:
438         break; 
439     }
440   }
441   else
442   {
443     /* Disable the selected ADC AnalogWatchdogx */
444     switch (ADC_AnalogWatchdog)
445     {
446       case ADC_AnalogWatchdog0 : 
447         ADC->TRB0 &= ADC_AnalogWatchdog_Disable;  
448         break;
449
450       case ADC_AnalogWatchdog1 : 
451         ADC->TRB1 &= ADC_AnalogWatchdog_Disable;  
452         break;
453
454       case ADC_AnalogWatchdog2 : 
455         ADC->TRB2 &= ADC_AnalogWatchdog_Disable;  
456         break;
457
458       case ADC_AnalogWatchdog3 : 
459         ADC->TRB3 &= ADC_AnalogWatchdog_Disable;  
460         break;
461
462       default:
463         break; 
464     }
465   }
466 }
467
468 /*******************************************************************************
469 * Function Name  : ADC_GetAnalogWatchdogResult
470 * Description    : Returns the comparison result of the selected analog watchdog.
471 * Input          : - ADC_AnalogWatchdog: specifies the analog watchdog channel 
472 *                    which its comparison result will be returned. This parameter
473 *                    can be one of the following values: 
474 *                     - ADC_AnalogWatchdog0: select analog watchdog 0
475 *                     - ADC_AnalogWatchdog1: select analog watchdog 1
476 *                     - ADC_AnalogWatchdog2: select analog watchdog 2
477 *                     - ADC_AnalogWatchdog3: select analog watchdog 3
478 * Output         : None
479 * Return         : The analog watchdog comparaison result value
480 *******************************************************************************/
481 u16 ADC_GetAnalogWatchdogResult(u16 ADC_AnalogWatchdog)
482 {
483   /* Return the selected ADC AnalogWatchdogx comparaison result */
484   switch(ADC_AnalogWatchdog)
485   {
486     case ADC_AnalogWatchdog0 :
487       return ((ADC->PBR & ADC_AnalogWatchdog)>>4);
488
489     case ADC_AnalogWatchdog1 :
490       return ((ADC->PBR & ADC_AnalogWatchdog)>>6);
491
492     case ADC_AnalogWatchdog2 :
493       return ((ADC->PBR & ADC_AnalogWatchdog)>>8);
494
495     case ADC_AnalogWatchdog3 :
496       return ((ADC->PBR & ADC_AnalogWatchdog)>>10);
497
498     default : return (0xFF);  /* if a wrong value of ADC_AnalogWatchdog is selected */
499   }
500 }
501
502 /*******************************************************************************
503 * Function Name  : ADC_InjectedConversionConfig
504 * Description    : Configures the start trigger level for the injected channels 
505 *                  and the injected analog input channels to be converted. 
506 * Input          : - ADC_Injec_ExtTrigger: specifies the start trigger level.
507 *                    This parameter can be one of the following values:
508 *                         - ADC_Injec_ExtTrigger_Disable : external trigger disabled
509 *                         - ADC_Injec_ExtTrigger_RisingEdge: external trigger
510 *                           configured as rising edge of PWM Timer TRGO signal
511 *                         - ADC_Injec_ExtTrigger_FallingEdge: external trigger 
512 *                           configured as falling edge of PWM Timer TRGO signal 
513 *                  - FirstChannel: specifies the first injected channel to be
514 *                    converted. 
515 *                    This parameter can be ADC_CHANNELx  where x can be (0..15). 
516 *                  - ChannelNumber: specifies the Number of the injected channels 
517 *                    to be converted. This parameter can be a  value from 1 to 16.    
518 * Output         : None
519 * Return         : None
520 *******************************************************************************/
521 void ADC_InjectedConversionConfig(u16 ADC_Injec_ExtTrigger, u8 FirstChannel, u8 ChannelNumber)
522 {
523   /* Configure the external start injected conversion trigger */
524   switch (ADC_Injec_ExtTrigger)
525   {
526     case ADC_Injec_ExtTrigger_Disable  :
527       /* Disable the external trigger and start the injected conversion by software */
528       ADC->CLR3 &= ADC_Injec_ExtTrigger_Disable ;
529       break;
530     case ADC_Injec_ExtTrigger_RisingEdge :
531       /* Start injected conversion on rising edge of the external trigger (PWM) */
532       ADC->CLR3 |= ADC_Injec_ExtTrigger_RisingEdge;
533       break;
534     case ADC_Injec_ExtTrigger_FallingEdge :
535       /* Start injected conversion on falling edge of the external trigger (PWM) */
536       ADC->CLR3 |= ADC_Injec_ExtTrigger_Enable; 
537       ADC->CLR3 &= ADC_Injec_ExtTrigger_FallingEdge;
538       break;
539
540     default:
541       break;
542   }
543   
544   /* Clear first injected channel to be converted JFCH[3:0] bits */
545   ADC->CLR3 &= ADC_FirstChannel_Mask;
546   /* Set the first injected channel to be converted */
547   ADC->CLR3 |= FirstChannel;
548   /* Clear number of injected channels to be converted JNCH[3:0] bits */
549   ADC->CLR3 &= ADC_ChannelNumber_Mask;  
550   /* Set the number of injected channels to be converted */
551   ADC->CLR3 |= ((ChannelNumber-1)<<6);
552 }
553
554 /*******************************************************************************
555 * Function Name  : ADC_StartInjectedConversion
556 * Description    : Starts by software the conversion of the injected input channels.
557 * Input          : None
558 * Output         : None
559 * Return         : None
560 *******************************************************************************/
561 void ADC_StartInjectedConversion(void)
562 {
563   /* Start the injected ADC Conversion */
564   ADC->CLR3 |= ADC_Injec_ConversionStart;
565 }
566
567 /*******************************************************************************
568 * Function Name  : ADC_GetConversionValue
569 * Description    : Reads the conversion result from the appropriate data register.
570 * Input          : - ADC_CHANNEL :specifies the ADC channel which its conversion 
571 *                    value have to be returned. This parameter can be ADC_CHANNELx
572 *                    where x can be (0..15) to select channelx  
573 * Output         : None
574 * Return         : The returned value holds the conversion result of the selected   
575 *                  channel.
576 *******************************************************************************/
577 u16 ADC_GetConversionValue(u8 ADC_CHANNEL)
578 {
579   /* Return the conversion result of the selected channel */
580   return *((u16 *)(ADC_BASE + ((ADC_CHANNEL<<2) + ADC_DataRegisterOffset)));
581 }
582
583 /*******************************************************************************
584 * Function Name  : ADC_ITConfig
585 * Description    : Enables or disables the specified ADC interrupts.
586 * Input          : - ADC_IT: specifies the ADC interrupts to be enabled or disabled.
587 *                    This parameter can be any combination of the following values:
588 *                         - ADC_IT_ECH:  End of chain conversion interrupt
589 *                         - ADC_IT_EOC:  End of channel conversion interrupt
590 *                         - ADC_IT_JECH: Injected end of chain conversion interrupt
591 *                         - ADC_IT_JEOC: Injected end of channel conversion interrupt
592 *                         - ADC_IT_AnalogWatchdog0_LowThreshold:
593 *                                        Analog Watchdog 0 LowThreshold interrupt
594 *                         - ADC_IT_AnalogWatchdog0_HighThreshold:
595 *                                        Analog Watchdog 0 HighThreshold interrupt
596 *                         - ADC_IT_AnalogWatchdog1_LowThreshold:
597 *                                        Analog Watchdog 1 LowThreshold interrupt
598 *                         - ADC_IT_AnalogWatchdog1_HighThreshold:
599 *                                        Analog Watchdog 1 HighThreshold interrupt
600 *                         - ADC_IT_AnalogWatchdog2_LowThreshold:
601 *                                        Analog Watchdog 2 LowThreshold interrupt
602 *                         - ADC_IT_AnalogWatchdog2_HighThreshold:
603 *                                        Analog Watchdog 2 HighThreshold interrupt
604 *                         - ADC_IT_AnalogWatchdog3_LowThreshold:
605 *                                        Analog Watchdog 3 LowThreshold interrupt
606 *                         - ADC_IT_AnalogWatchdog3_HighThreshold:
607 *                                         Analog Watchdog 5 HighThreshold interrupt
608 *                         - ADC_IT_ALL:  All interrupts
609 *                  - NewState: new state of the specified ADC interrupts.
610 *                    This parameter can be: ENABLE or DISABLE.
611 * Output         : None
612 * Return         : None
613 *******************************************************************************/
614 void ADC_ITConfig(u16 ADC_IT, FunctionalState NewState)
615 {
616   if (NewState == ENABLE)
617   {
618     /* Enable the selected ADC interrupts */
619     ADC->IMR |= ADC_IT;
620   }
621   else
622   {
623     /* Disable the selected ADC interrupts */
624     ADC->IMR &= ~ADC_IT;
625   }
626 }
627
628 /*******************************************************************************
629 * Function Name  : ADC_DMAConfig
630 * Description    : Configures the ADC\92s DMA interface.
631 * Input          : - ADC_DMA_CHANNEL: specifies the channels to be enabled or 
632 *                    disabled for DMA transfer. This parameter can be any 
633 *                    combination of ADC_DMA_CHANNELx where x can be (0..15). 
634 *                  - NewState: new state of the specified ADC DMA channels.
635 *                    This parameter can be: ENABLE or DISABLE.
636 * Output         : None
637 * Return         : None
638 *******************************************************************************/
639 void ADC_DMAConfig(u16 ADC_DMA_CHANNEL, FunctionalState NewState)
640 {
641   if(NewState == ENABLE)
642   {
643     /* Enable DMA for the selected channels */
644     ADC->DMAR |= ADC_DMA_CHANNEL ;
645   }
646   else
647   {
648     /* Disable DMA for the selected channels */
649     ADC->DMAR &= ~ADC_DMA_CHANNEL;
650   }
651 }
652
653 /*******************************************************************************
654 * Function Name  : ADC_DMACmd
655 * Description    : Enable or disable the DMA transfer for the ADC.
656 * Input          : - ADC_DMA: specifies the DMA command. This parameter can be 
657 *                    one of the following values:
658 *                         - ADC_DMA_Disable: disable the DMA capability
659 *                         - ADC_DMA_Enable: enabled by setting the global 
660 *                           enable bit
661 *                         - ADC_DMA_ExtTrigger_HighLevel: enabled by detection of
662 *                           high level of TIM2 OC2 signal
663 *                         - ADC_DMA_ExtTrigger_LowLevel: enabled by detection of
664 *                           low level of TIM2 OC2 signal
665 * Output         : None
666 * Return         : None
667 *******************************************************************************/
668 void ADC_DMACmd(u16 ADC_DMA)
669 {
670   /* Configure the DMA external trigger enable */
671   switch (ADC_DMA)
672   {
673     case ADC_DMA_Disable :
674       /* Disable DMA transfer */
675       ADC->DMAE &= ADC_DMA_Disable;
676       break;
677       
678     case ADC_DMA_Enable :
679       /* Enable DMA transfer */
680       ADC->DMAE |= ADC_DMA_Enable;
681       break;
682       
683     case ADC_DMA_ExtTrigger_HighLevel :
684       /* Enable DMA transfer on high level of the external trigger (TIM2) */
685       ADC->DMAE &= ADC_DMA_Disable;
686       ADC->DMAE |= ADC_DMA_ExtTrigger_HighLevel;
687       break;
688       
689     case ADC_DMA_ExtTrigger_LowLevel :
690       /* Enable DMA transfer on low level of the external trigger (TIM2) */
691       ADC->DMAE |= ADC_DMA_ExtEnable_Mask; 
692       ADC->DMAE &= ADC_DMA_ExtTrigger_LowLevel;
693       break;
694
695     default:
696       break;      
697   }  
698 }
699
700 /*******************************************************************************
701 * Function Name  : ADC_GetDMAFirstEnabledChannel
702 * Description    : Gets the first DMA-enabled channel configured at the time that
703 *                  DMA was last globally enabled.
704 * Input          : None
705 * Output         : None
706 * Return         : The first DMA enabled channel
707 *******************************************************************************/
708 u16 ADC_GetDMAFirstEnabledChannel(void)
709 {
710   /* Return the DMA first enabled channel */
711   return (ADC->DMAE & ADC_DMAFirstEnabledChannel_Mask);
712 }
713
714 /*******************************************************************************
715 * Function Name  : ADC_GetFlagStatus
716 * Description    : Checks whether the specified ADC flag is set or not.
717 * Input          : - ADC_FLAG: specifies the ADC flag to check. This parameter 
718 *                    can be one of the following values:
719 *                         - ADC_FLAG_ECH:  End of chain conversion Flag
720 *                         - ADC_FLAG_EOC:  End of channel conversion Flag
721 *                         - ADC_FLAG_JECH: End of injected chain conversion Flag
722 *                         - ADC_FLAG_JEOC: End of injected channel conversion Flag
723 *                         - ADC_FLAG_AnalogWatchdog0_LowThreshold:  
724 *                                          Analog Watchdog 0 LowThreshold Flag        
725 *                         - ADC_FLAG_AnalogWatchdog0_HighThreshold: 
726 *                                          Analog Watchdog 0 HighThreshold Flag         
727 *                         - ADC_FLAG_AnalogWatchdog1_LowThreshold:  
728 *                                          Analog Watchdog 1 LowThreshold Flag          
729 *                         - ADC_FLAG_AnalogWatchdog1_HighThreshold: 
730 *                                          Analog Watchdog 1 HighThreshold Flag         
731 *                         - ADC_FLAG_AnalogWatchdog2_LowThreshold:  
732 *                                          Analog Watchdog 2 LowThreshold Flag          
733 *                         - ADC_FLAG_AnalogWatchdog2_HighThreshold: 
734 *                                          Analog Watchdog 2 HighThreshold Flag         
735 *                         - ADC_FLAG_AnalogWatchdog3_LowThreshold:  
736 *                                          Analog Watchdog 3 LowThreshold Flag          
737 *                         - ADC_FLAG_AnalogWatchdog3_HighThreshold: 
738 *                                          Analog Watchdog 3 HighThreshold Flag 
739 * Output         : None 
740 * Return         : The new state of the ADC_FLAG (SET or RESET).
741 *******************************************************************************/
742 FlagStatus ADC_GetFlagStatus(u16 ADC_FLAG)
743 {
744   /* Check the status of the specified ADC flag */
745   if((ADC->PBR & ADC_FLAG) != RESET)
746   {
747     /* Return SET if ADC_FLAG is set */
748     return SET;
749   }
750   else
751   {
752     /* Return RESET if ADC_FLAG is reset */
753     return RESET;
754   }
755 }
756
757 /*******************************************************************************
758 * Function Name  : ADC_ClearFlag
759 * Description    : Clears the ADC\92s pending flags.  
760 * Input          : - ADC_FLAG: specifies the flag to clear. This parameter can 
761 *                    be any combination of the following values:
762 *                         - ADC_FLAG_ECH:  End of chain conversion flag
763 *                         - ADC_FLAG_EOC:  End of channel conversion flag
764 *                         - ADC_FLAG_JECH: Injected end of chain conversion flag
765 *                         - ADC_FLAG_JEOC: Injected end of channel conversion flag
766 *                         - ADC_FLAG_AnalogWatchdog0_LowThreshold:  
767 *                                         Analog Watchdog 0 LowThreshold flag        
768 *                         - ADC_FLAG_AnalogWatchdog0_HighThreshold: 
769 *                                         Analog Watchdog 0 HighThreshold flag  
770 *                         - ADC_FLAG_AnalogWatchdog1_LowThreshold:  
771 *                                         Analog Watchdog 1 LowThreshold flag   
772 *                         - ADC_FLAG_AnalogWatchdog1_HighThreshold: 
773 *                                         Analog Watchdog 1 HighThreshold flag          
774 *                         - ADC_FLAG_AnalogWatchdog2_LowThreshold:  
775 *                                         Analog Watchdog 2 LowThreshold flag   
776 *                         - ADC_FLAG_AnalogWatchdog2_HighThreshold: 
777 *                                         Analog Watchdog 2 HighThreshold flag  
778 *                         - ADC_FLAG_AnalogWatchdog3_LowThreshold:  
779 *                                         Analog Watchdog 3 LowThreshold flag   
780 *                         - ADC_FLAG_AnalogWatchdog3_HighThreshold: 
781 *                                         Analog Watchdog 3 HighThreshold flag          
782 * Output         : None
783 * Return         : None
784 *******************************************************************************/
785 void ADC_ClearFlag(u16 ADC_FLAG)
786 {
787   /* Clear the selected ADC flag */ 
788   ADC->PBR = ADC_FLAG;
789 }
790
791 /*******************************************************************************
792 * Function Name  : ADC_GetITStatus
793 * Description    : Checks whether the specified ADC interrupt has occured or not.
794 * Input          : - ADC_IT: specifies the ADC interrupt source to check. This 
795 *                    parameter can be one of the following values:
796 *                         - ADC_IT_ECH :End of chain conversion interrupt 
797 *                         - ADC_IT_EOC :End of channel conversion interrupt
798 *                         - ADC_IT_JECH :End of injected chain conversion interrupt
799 *                         - ADC_IT_JEOC :End of injected channel conversion interrupt
800 *                         - ADC_IT_AnalogWatchdog0_LowThreshold:  
801 *                                         Analog Watchdog 0 LowThreshold interrupt
802 *                         - ADC_IT_AnalogWatchdog0_HighThreshold: 
803 *                                         Analog Watchdog 0 HighThreshold interrupt
804 *                         - ADC_IT_AnalogWatchdog1_LowThreshold:  
805 *                                         Analog Watchdog 1 LowThreshold interrupt
806 *                         - ADC_IT_AnalogWatchdog1_HighThreshold: 
807 *                                         Analog Watchdog 1 HighThreshold interrupt
808 *                         - ADC_IT_AnalogWatchdog2_LowThreshold:  
809 *                                         Analog Watchdog 2 LowThreshold interrupt
810 *                         - ADC_IT_AnalogWatchdog2_HighThreshold: 
811 *                                         Analog Watchdog 2 HighThreshold interrupt
812 *                         - ADC_IT_AnalogWatchdog3_LowThreshold:  
813 *                                         Analog Watchdog 3 LowThreshold interrupt
814 *                         - ADC_IT_AnalogWatchdog3_HighThreshold: 
815 *                                         Analog Watchdog 3 HighThreshold interrupt
816 * Output         : None 
817 * Return         : The new state of the ADC_IT (SET or RESET).
818 *******************************************************************************/
819 ITStatus ADC_GetITStatus(u16 ADC_IT)
820 {
821   /* Check the status of the specified ADC interrupt */
822   if((ADC->PBR & ADC_IT) != RESET)
823   {
824     /* Return SET if the ADC interrupt flag is set */
825     return SET;
826   }
827   else
828   {
829     /* Return RESET if the ADC interrupt flag is reset */
830     return RESET;
831   }
832 }
833
834 /*******************************************************************************
835 * Function Name  : ADC_ClearITPendingBit
836 * Description    : Clears the ADC\92s interrupt pending bits.  
837 * Input          : - ADC_IT: specifies the interrupt pending bit to clear. This 
838 *                    parameter can be can be any combination of the following 
839 *                    values:
840 *                         - ADC_IT_ECH:  End of chain conversion interrupt
841 *                         - ADC_IT_EOC:  End of channel conversion interrupt
842 *                         - ADC_IT_JECH: Injected end of chain conversion interrupt
843 *                         - ADC_IT_JEOC: Injected end of channel conversion interrupt
844 *                         - ADC_IT_AnalogWatchdog0_LowThreshold:  
845 *                                         Analog Watchdog 0 LowThreshold interrupt
846 *                         - ADC_IT_AnalogWatchdog0_HighThreshold: 
847 *                                         Analog Watchdog 0 HighThreshold interrupt
848 *                         - ADC_IT_AnalogWatchdog1_LowThreshold:  
849 *                                         Analog Watchdog 1 LowThreshold interrupt
850 *                         - ADC_IT_AnalogWatchdog1_HighThreshold: 
851 *                                         Analog Watchdog 1 HighThreshold interrupt
852 *                         - ADC_IT_AnalogWatchdog2_LowThreshold:  
853 *                                         Analog Watchdog 2 LowThreshold interrupt
854 *                         - ADC_IT_AnalogWatchdog2_HighThreshold: 
855 *                                         Analog Watchdog 2 HighThreshold interrupt
856 *                         - ADC_IT_AnalogWatchdog3_LowThreshold:  
857 *                                         Analog Watchdog 3 LowThreshold interrupt
858 *                         - ADC_IT_AnalogWatchdog3_HighThreshold: 
859 *                                         Analog Watchdog 5 HighThreshold interrupt
860 * Output         : None
861 * Return         : None
862 *******************************************************************************/
863 void ADC_ClearITPendingBit(u16 ADC_IT)
864 {
865   /* Clear the selected ADC interrupts pending bits */
866   ADC->PBR = ADC_IT;
867 }
868
869 /******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/