1 /*!
2  * @file        apm32f0xx_adc.c
3  *
4  * @brief       This file contains all the functions for the ADC peripheral
5  *
6  * @version     V1.0.3
7  *
8  * @date        2022-09-20
9  *
10  * @attention
11  *
12  *  Copyright (C) 2020-2022 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be useful and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 /* Includes */
27 #include "apm32f0xx_adc.h"
28 #include "apm32f0xx_rcm.h"
29 
30 /** @addtogroup APM32F0xx_StdPeriphDriver
31   @{
32 */
33 
34 /** @addtogroup ADC_Driver
35   @{
36 */
37 
38 /** @defgroup ADC_Macros Macros
39   @{
40 */
41 
42 /**@} end of group ADC_Macros */
43 
44 /** @defgroup ADC_Enumerations Enumerations
45   @{
46 */
47 
48 /**@} end of group ADC_Enumerations */
49 
50 /** @defgroup ADC_Structures Structures
51   @{
52 */
53 
54 /**@} end of group ADC_Structures */
55 
56 /** @defgroup ADC_Variables Variables
57   @{
58 */
59 
60 /**@} end of group ADC_Variables */
61 
62 /** @defgroup ADC_Functions Functions
63   @{
64 */
65 
66 /*!
67  * @brief     Set the ADC peripheral registers to their default reset values
68  *
69  * @param     None
70  *
71  * @retval    None
72  */
ADC_Reset(void)73 void ADC_Reset(void)
74 {
75     RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_ADC1);
76     RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_ADC1);
77 }
78 
79 /*!
80  * @brief       Config the ADC peripheral according to the specified parameters in the adcConfig
81  *
82  * @param       adcConfig:  Pointer to a ADC_Config_T structure that
83  *                          contains the configuration information for the ADC peripheral
84  *
85  * @retval      None
86  */
ADC_Config(ADC_Config_T * adcConfig)87 void ADC_Config(ADC_Config_T* adcConfig)
88 {
89     ADC->CFG1_B.DATARESCFG = adcConfig->resolution;
90     ADC->CFG1_B.DALIGCFG   = adcConfig->dataAlign;
91     ADC->CFG1_B.SCANSEQDIR = adcConfig->scanDir;
92     ADC->CFG1_B.CMODESEL   = adcConfig->convMode;
93     ADC->CFG1_B.EXTPOLSEL  = adcConfig->extTrigEdge;
94     ADC->CFG1_B.EXTTRGSEL  = adcConfig->extTrigConv;
95 }
96 
97 /*!
98  * @brief       Fills each adcConfig member with its default value
99  *
100  * @param       adcConfig:  Pointer to a ADC_Config_T structure which will be initialized
101  *
102  * @retval      None
103  */
ADC_ConfigStructInit(ADC_Config_T * adcConfig)104 void ADC_ConfigStructInit(ADC_Config_T* adcConfig)
105 {
106     adcConfig->resolution  = ADC_RESOLUTION_12B;
107     adcConfig->dataAlign   = ADC_DATA_ALIGN_RIGHT;
108     adcConfig->scanDir     = ADC_SCAN_DIR_UPWARD;
109     adcConfig->convMode    = ADC_CONVERSION_SINGLE;
110     adcConfig->extTrigConv = ADC_EXT_TRIG_CONV_TRG0;
111     adcConfig->extTrigEdge = ADC_EXT_TRIG_EDGE_NONE;
112 }
113 
114 /*!
115  * @brief       Enable the ADC peripheral
116  *
117  * @param       None
118  *
119  * @retval      None
120  */
ADC_Enable(void)121 void ADC_Enable(void)
122 {
123     ADC->CTRL_B.ADCEN = BIT_SET;
124 }
125 
126 /*!
127  * @brief       Disable the ADC peripheral
128  *
129  * @param       None
130  *
131  * @retval      None
132  */
ADC_Disable(void)133 void ADC_Disable(void)
134 {
135     ADC->CTRL_B.ADCD = BIT_SET;
136 }
137 
138 /*!
139  * @brief       Configure the ADC to either be clocked by the asynchronous clock
140  *
141  * @param       clockmode: selects the ADC clock mode.
142  *                         The parameter can be one of following values:
143  *                         @arg ADC_CLOCK_MODE_ASYNCLK:       ADC Asynchronous clock mode
144  *                         @arg ADC_CLOCK_MODE_SYNCLKDIV2:    Synchronous clock mode divided by 2
145  *                         @arg ADC_CLOCK_MODE_SYNCLKDIV4:    Synchronous clock mode divided by 4
146  *
147  * @retval      None
148  */
ADC_ClockMode(ADC_CLOCK_MODE_T clockMode)149 void ADC_ClockMode(ADC_CLOCK_MODE_T clockMode)
150 {
151     ADC->CFG2_B.CLKCFG = (uint32_t)clockMode;
152 }
153 
154 /*!
155  * @brief       Enables the jitter when the ADC is clocked by PCLK div2 or div4
156  *
157  * @param       jitter: They are replaced by PCLK div2 or div4
158  *                      The parameter can be one of following values:
159  *                      @arg ADC_JITTER_PCLKDIV2:    ADC clocked by PCLK div2
160  *                      @arg ADC_JITTER_PCLKDIV4:    ADC clocked by PCLK div4
161  *
162  * @retval      None
163  */
ADC_EnableJitter(ADC_JITTER_T jitter)164 void ADC_EnableJitter(ADC_JITTER_T jitter)
165 {
166     ADC->CFG2_B.CLKCFG |= (uint32_t)jitter;
167 }
168 
169 /*!
170  * @brief       Disables the jitter when the ADC is clocked by PCLK div2 or div4
171  *
172  * @param       jitter: They are replaced by PCLK div2 or div4
173  *                      The parameter can be one of following values:
174  *                      @arg ADC_JITTER_PCLKDIV2:    ADC clocked by PCLK div2
175  *                      @arg ADC_JITTER_PCLKDIV4:    ADC clocked by PCLK div4
176  * @retval      None
177  */
ADC_DisableJitter(ADC_JITTER_T jitter)178 void ADC_DisableJitter(ADC_JITTER_T jitter)
179 {
180     ADC->CFG2_B.CLKCFG &= (uint32_t)~jitter;
181 }
182 
183 /*!
184  * @brief       Enables the Auto Power Off mode
185  *
186  * @param       None
187  *
188  * @retval      None
189  */
ADC_EnableAutoPowerOff(void)190 void ADC_EnableAutoPowerOff(void)
191 {
192     ADC->CFG1_B.AOEN = BIT_SET;
193 }
194 
195 /*!
196  * @brief       Disables the Auto Power Off mode
197  *
198  * @param       None
199  *
200  * @retval      None
201  */
ADC_DisableAutoPowerOff(void)202 void ADC_DisableAutoPowerOff(void)
203 {
204     ADC->CFG1_B.AOEN = BIT_RESET;
205 }
206 
207 /*!
208  * @brief       Enables the Auto-delayed conversion mode
209  *
210  * @param       None
211  *
212  * @retval      None
213  */
ADC_EnableWaitMode(void)214 void ADC_EnableWaitMode(void)
215 {
216     ADC->CFG1_B.WAITCEN = BIT_SET;
217 }
218 
219 /*!
220  * @brief       Disables the Auto-delayed conversion mode
221  *
222  * @param       None
223  *
224  * @retval      None
225  */
ADC_DisableWaitMode(void)226 void ADC_DisableWaitMode(void)
227 {
228     ADC->CFG1_B.WAITCEN = BIT_RESET;
229 }
230 
231 /*!
232  * @brief       Enables the analog watchdog
233  *
234  * @param       None
235  *
236  * @retval      None
237  */
ADC_EnableAnalogWatchdog(void)238 void ADC_EnableAnalogWatchdog(void)
239 {
240     ADC->CFG1_B.AWDEN = BIT_SET;
241 }
242 
243 /*!
244  * @brief       Disables the analog watchdog
245  *
246  * @param       None
247  *
248  * @retval      None
249  */
ADC_DisableAnalogWatchdog(void)250 void ADC_DisableAnalogWatchdog(void)
251 {
252     ADC->CFG1_B.AWDEN = BIT_RESET;
253 }
254 
255 /*!
256  * @brief       The analog watchdog low threshold
257  *
258  * @param       None
259  *
260  * @retval      None
261  */
ADC_AnalogWatchdogLowThreshold(uint16_t lowThreshold)262 void ADC_AnalogWatchdogLowThreshold(uint16_t lowThreshold)
263 {
264     ADC->AWDT_B.AWDLT = (uint16_t)lowThreshold;
265 }
266 
267 /*!
268  * @brief       The analog watchdog High threshold
269  *
270  * @param       None
271  *
272  * @retval      None
273  */
ADC_AnalogWatchdogHighThreshold(uint16_t highThreshold)274 void ADC_AnalogWatchdogHighThreshold(uint16_t highThreshold)
275 {
276     ADC->AWDT_B.AWDHT = (uint16_t)highThreshold;
277 }
278 
279 /*!
280  * @brief       Configures the analog watchdog guarded single channel
281  *
282  * @param       channel:    ADC analog watchdog channel selection
283  *                          The parameter can be one of following values:
284  *                          @arg ADC_ANALG_WDT_CHANNEL_0: AWD Channel 0
285  *                          @arg ADC_ANALG_WDT_CHANNEL_1: AWD Channel 1
286  *                          @arg ADC_ANALG_WDT_CHANNEL_2: AWD Channel 2
287  *                          @arg ADC_ANALG_WDT_CHANNEL_3: AWD Channel 3
288  *                          @arg ADC_ANALG_WDT_CHANNEL_4: AWD Channel 4
289  *                          @arg ADC_ANALG_WDT_CHANNEL_5: AWD Channel 5
290  *                          @arg ADC_ANALG_WDT_CHANNEL_6: AWD Channel 6
291  *                          @arg ADC_ANALG_WDT_CHANNEL_7: AWD Channel 7
292  *                          @arg ADC_ANALG_WDT_CHANNEL_8: AWD Channel 8
293  *                          @arg ADC_ANALG_WDT_CHANNEL_9: AWD Channel 9
294  *                          @arg ADC_ANALG_WDT_CHANNEL_10: AWD Channel 10
295  *                          @arg ADC_ANALG_WDT_CHANNEL_11: AWD Channel 11
296  *                          @arg ADC_ANALG_WDT_CHANNEL_12: AWD Channel 12
297  *                          @arg ADC_ANALG_WDT_CHANNEL_13: AWD Channel 13
298  *                          @arg ADC_ANALG_WDT_CHANNEL_14: AWD Channel 14
299  *                          @arg ADC_ANALG_WDT_CHANNEL_15: AWD Channel 15
300  *                          @arg ADC_ANALG_WDT_CHANNEL_16: AWD Channel 16 is TempSensor
301  *                          @arg ADC_ANALG_WDT_CHANNEL_17: AWD Channel 17 is Vrefint
302  *                          @arg ADC_ANALG_WDT_CHANNEL_18: AWD Channel 18 is Vbat, not available for APM32F030 devices
303  *
304  * @retval      None
305  */
ADC_AnalogWatchdogSingleChannel(uint32_t channel)306 void ADC_AnalogWatchdogSingleChannel(uint32_t channel)
307 {
308     ADC->CFG1_B.AWDCHSEL = channel;
309 }
310 
311 /*!
312  * @brief       Enables the Analog Watchdog Single Channel
313  *
314  * @param       None
315  *
316  * @retval      None
317  */
ADC_EnableAnalogWatchdogSingleChannel(void)318 void ADC_EnableAnalogWatchdogSingleChannel(void)
319 {
320     ADC->CFG1_B.AWDCHEN = BIT_SET;
321 }
322 
323 /*!
324  * @brief       Disables the Analog Watchdog Single Channel
325  *
326  * @param       None
327  *
328  * @retval      None
329  */
ADC_DisableAnalogWatchdogSingleChannel(void)330 void ADC_DisableAnalogWatchdogSingleChannel(void)
331 {
332     ADC->CFG1_B.AWDCHEN = BIT_RESET;
333 }
334 
335 /*!
336  * @brief       Enables the temperature sensor channel
337  *
338  * @param       None
339  *
340  * @retval      None
341  */
ADC_EnableTempSensor(void)342 void ADC_EnableTempSensor(void)
343 {
344     ADC->CCFG_B.TSEN = BIT_SET;
345 }
346 
347 /*!
348  * @brief       Disables the temperature sensor channel
349  *
350  * @param       None
351  *
352  * @retval      None
353  */
ADC_DisableTempSensor(void)354 void ADC_DisableTempSensor(void)
355 {
356     ADC->CCFG_B.TSEN = BIT_RESET;
357 }
358 
359 /*!
360  * @brief       Enables the vrefint channel
361  *
362  * @param       None
363  *
364  * @retval      None
365  */
ADC_EnableVrefint(void)366 void ADC_EnableVrefint(void)
367 {
368     ADC->CCFG_B.VREFEN = BIT_SET;
369 }
370 
371 /*!
372  * @brief       Disables the vrefint channel
373  *
374  * @param       None
375  *
376  * @retval      None
377  */
ADC_DisableVrefint(void)378 void ADC_DisableVrefint(void)
379 {
380     ADC->CCFG_B.VREFEN = BIT_RESET;
381 }
382 
383 /*!
384  * @brief       Enables the Vbat channel
385  *
386  * @param       None
387  *
388  * @retval      None
389  *
390  * @note        It's not for APM32F030 devices
391  */
ADC_EnableVbat(void)392 void ADC_EnableVbat(void)
393 {
394     ADC->CCFG_B.VBATEN = BIT_SET;
395 }
396 
397 /*!
398  * @brief       Disables the Vbat channel
399  *
400  * @param       None
401  *
402  * @retval      None
403  *
404  * @note        It's not for APM32F030 devices
405  */
ADC_DisableVbat(void)406 void ADC_DisableVbat(void)
407 {
408     ADC->CCFG_B.VBATEN = BIT_RESET;
409 }
410 
411 /*!
412  * @brief       Configures for the selected ADC channel and its sampling time
413  *
414  * @param       channel:    the ADC channel
415  *                          The parameter can be combination of following values:
416  *                          @arg ADC_CHANNEL_0: channel 0
417  *                          @arg ADC_CHANNEL_1: channel 1
418  *                          @arg ADC_CHANNEL_2: channel 2
419  *                          @arg ADC_CHANNEL_3: channel 3
420  *                          @arg ADC_CHANNEL_4: channel 4
421  *                          @arg ADC_CHANNEL_5: channel 5
422  *                          @arg ADC_CHANNEL_6: channel 6
423  *                          @arg ADC_CHANNEL_7: channel 7
424  *                          @arg ADC_CHANNEL_8: channel 8
425  *                          @arg ADC_CHANNEL_9: channel 9
426  *                          @arg ADC_CHANNEL_10: channel 10
427  *                          @arg ADC_CHANNEL_11: channel 11
428  *                          @arg ADC_CHANNEL_12: channel 12
429  *                          @arg ADC_CHANNEL_13: channel 13
430  *                          @arg ADC_CHANNEL_14: channel 14
431  *                          @arg ADC_CHANNEL_15: channel 15
432  *                          @arg ADC_CHANNEL_16: channel 16 is TempSensor
433  *                          @arg ADC_CHANNEL_17: channel 17 is Vrefint
434  *                          @arg ADC_CHANNEL_18: channel 18 is Vbat, not available for APM32F030 devices
435  * @param       sampleTime: the ADC sampling time
436  *                          The parameter can be one of following values:
437  *                          @arg ADC_SAMPLE_TIME_1_5:   ADC 1.5 clock cycles
438  *                          @arg ADC_SAMPLE_TIME_7_5:   ADC 7.5 clock cycles
439  *                          @arg ADC_SAMPLE_TIME_13_5:  ADC 13.5 clock cycles
440  *                          @arg ADC_SAMPLE_TIME_28_5:  ADC 28.5 clock cycles
441  *                          @arg ADC_SAMPLE_TIME_41_5:  ADC 41.5 clock cycles
442  *                          @arg ADC_SAMPLE_TIME_55_5:  ADC 55.5 clock cycles
443  *                          @arg ADC_SAMPLE_TIME_71_5:  ADC 71.5 clock cycles
444  *                          @arg ADC_SAMPLE_TIME_239_5: ADC 239.5 clock cycles
445  *
446  * @retval      None
447  */
ADC_ConfigChannel(uint32_t channel,uint8_t sampleTime)448 void ADC_ConfigChannel(uint32_t channel, uint8_t sampleTime)
449 {
450     ADC->CHSEL |= (uint32_t)channel;
451 
452     ADC->SMPTIM |= (uint8_t)sampleTime;
453 }
454 
455 /*!
456  * @brief       Enables the continuous mode
457  *
458  * @param       None
459  *
460  * @retval      None
461  */
ADC_EnableContinuousMode(void)462 void ADC_EnableContinuousMode(void)
463 {
464     ADC->CFG1_B.CMODESEL = BIT_SET;
465 }
466 
467 /*!
468  * @brief       Disables the continuous mode
469  *
470  * @param       None
471  *
472  * @retval      None
473  */
ADC_DisableContinuousMode(void)474 void ADC_DisableContinuousMode(void)
475 {
476     ADC->CFG1_B.CMODESEL = BIT_RESET;
477 }
478 
479 /*!
480  * @brief       Enables the discontinuous mode
481  *
482  * @param       None
483  *
484  * @retval      None
485  */
ADC_EnableDiscMode(void)486 void ADC_EnableDiscMode(void)
487 {
488     ADC->CFG1_B.DISCEN = BIT_SET;
489 }
490 
491 /*!
492  * @brief       Disables the discontinuous mode
493  *
494  * @param       None
495  *
496  * @retval      None
497  */
ADC_DisableDiscMode(void)498 void ADC_DisableDiscMode(void)
499 {
500     ADC->CFG1_B.DISCEN = BIT_RESET;
501 }
502 
503 /*!
504  * @brief       Enables the Overrun mode
505  *
506  * @param       None
507  *
508  * @retval      None
509  */
ADC_EnableOverrunMode(void)510 void ADC_EnableOverrunMode(void)
511 {
512     ADC->CFG1_B.OVRMAG = BIT_SET;
513 }
514 
515 /*!
516  * @brief       Disables the Overrun mode
517  *
518  * @param       None
519  *
520  * @retval      None
521  */
ADC_DisableOverrunMode(void)522 void ADC_DisableOverrunMode(void)
523 {
524     ADC->CFG1_B.OVRMAG = BIT_RESET;
525 }
526 
527 /*!
528  * @brief       ADC Read Calibration Factor
529  *
530  * @param       None
531  *
532  * @retval      ADC Calibration factor
533  */
ADC_ReadCalibrationFactor(void)534 uint32_t ADC_ReadCalibrationFactor(void)
535 {
536     uint32_t temp = 0, counter = 0, status = 0;
537 
538     ADC->CTRL_B.CAL = BIT_SET;
539 
540     do
541     {
542         status =  ADC->CTRL_B.CAL;
543         counter++;
544     }
545     while ((counter != CALIBRATION_TIMEOUT) && (status != 0x00));
546 
547     if ((uint32_t)(ADC->CTRL_B.CAL) == RESET)
548     {
549         temp = ADC->DATA;
550     }
551     else
552     {
553         temp = 0x00000000;
554     }
555 
556     return temp;
557 }
558 
559 /*!
560  * @brief       ADC stop conversion command
561  *
562  * @param       None
563  *
564  * @retval      None
565  */
ADC_StopConversion(void)566 void ADC_StopConversion(void)
567 {
568     ADC->CTRL_B.STOPCEN = BIT_SET;
569 }
570 
571 /*!
572  * @brief       ADC start conversion command
573  *
574  * @param       None
575  *
576  * @retval      None
577  */
ADC_StartConversion(void)578 void ADC_StartConversion(void)
579 {
580     ADC->CTRL_B.STARTCEN = BIT_SET;
581 }
582 
583 /*!
584  * @brief       Returns the last ADC conversion result data
585  *
586  * @param       None
587  *
588  * @retval      The Data conversion value
589  */
ADC_ReadConversionValue(void)590 uint16_t ADC_ReadConversionValue(void)
591 {
592     return ((uint16_t)ADC->DATA);
593 }
594 
595 /*!
596  * @brief       Enables the ADC DMA request
597  *
598  * @param       None
599  *
600  * @retval      None
601  */
ADC_EnableDMA(void)602 void ADC_EnableDMA(void)
603 {
604     ADC->CFG1_B.DMAEN = BIT_SET;
605 }
606 
607 /*!
608  * @brief       Disables the ADC DMA request
609  *
610  * @param       None
611  *
612  * @retval      None
613  */
ADC_DisableDMA(void)614 void ADC_DisableDMA(void)
615 {
616     ADC->CFG1_B.DMAEN = BIT_RESET;
617 }
618 
619 /*!
620  * @brief       ADC DMA Request Mode
621  *
622  * @param       DMARequestMode: Direct memery access configuration .
623  *                              The parameter can be one of following values:
624  *                              @arg ADC_DMA_MODE_ONESHOUT:    ADC DMA Mode Select one shot
625  *                              @arg ADC_DMA_MODE_CIRCULAR:    ADC DMA Mode Select circular
626  *
627  * @retval      None
628  */
ADC_DMARequestMode(ADC_DMA_MODE_T DMARequestMode)629 void ADC_DMARequestMode(ADC_DMA_MODE_T DMARequestMode)
630 {
631     ADC->CFG1_B.DMACFG = (uint8_t)DMARequestMode;
632 }
633 
634 /*!
635  * @brief       Enables the specified interrupts
636  *
637  * @param       interrupt:  Specifies the ADC interrupts sources
638  *                          The parameter can be combination of following values:
639  *                          @arg ADC_INT_ADRDY:    ADC ready interrupt
640  *                          @arg ADC_INT_CSMP:     End of sampling interrupt
641  *                          @arg ADC_INT_CC:       End of conversion interrupt
642  *                          @arg ADC_INT_CS:       End of sequence interrupt
643  *                          @arg ADC_INT_OVR:      ADC overrun interrupt
644  *                          @arg ADC_INT_AWD:      Analog watchdog interrupt
645  *
646  * @retval      None
647  */
ADC_EnableInterrupt(uint8_t interrupt)648 void ADC_EnableInterrupt(uint8_t interrupt)
649 {
650     ADC->INT |= (uint32_t)interrupt;
651 }
652 
653 /*!
654  * @brief       Disable the specified interrupts
655  *
656  * @param       interrupt:  Specifies the ADC interrupts sources
657  *                          The parameter can be combination of following values:
658  *                          @arg ADC_INT_ADRDY:    ADC ready interrupt
659  *                          @arg ADC_INT_CSMP:     End of sampling interrupt
660  *                          @arg ADC_INT_CC:       End of conversion interrupt
661  *                          @arg ADC_INT_CS:       End of sequence interrupt
662  *                          @arg ADC_INT_OVR:      ADC overrun interrupt
663  *                          @arg ADC_INT_AWD:      Analog watchdog interrupt
664  *
665  * @retval      None
666  */
ADC_DisableInterrupt(uint8_t interrupt)667 void ADC_DisableInterrupt(uint8_t interrupt)
668 {
669     ADC->INT &= (uint32_t)~interrupt;
670 }
671 
672 /*!
673  * @brief       Checks whether the specified ADC flag is set or not
674  *
675  * @param       flag:   Specifies the flag to check
676  *                      This parameter can be one of the following values:
677  *                      @arg ADC_FLAG_ADCON:    ADC enable flag
678  *                      @arg ADC_FLAG_ADCOFF:   ADC disable flag
679  *                      @arg ADC_FLAG_ADCSTA:   ADC start conversion flag
680  *                      @arg ADC_FLAG_ADCSTOP:  ADC stop conversion flag
681  *                      @arg ADC_FLAG_ADCCAL:   ADC calibration flag
682  *                      @arg ADC_FLAG_ADRDY:    ADC ready flag
683  *                      @arg ADC_FLAG_CSMP:     End of sampling flag
684  *                      @arg ADC_FLAG_CC:       End of conversion flag
685  *                      @arg ADC_FLAG_CS:       End of sequence flag
686  *                      @arg ADC_FLAG_OVR:      ADC overrun flag
687  *                      @arg ADC_FLAG_AWD:      Analog watchdog flag
688  *
689  * @retval      The new state of flag (SET or RESET)
690  */
ADC_ReadStatusFlag(ADC_FLAG_T flag)691 uint8_t ADC_ReadStatusFlag(ADC_FLAG_T flag)
692 {
693     uint32_t status;
694 
695     if ((uint32_t)(flag & 0x01000000))
696     {
697         status = ADC->CTRL & 0xFEFFFFFF;
698     }
699     else
700     {
701         status = ADC->STS;
702     }
703 
704     if ((status & flag) != (uint32_t)RESET)
705     {
706         return SET;
707     }
708 
709     return RESET;
710 }
711 
712 /*!
713  * @brief       Clear the specified ADC flag
714  *
715  * @param       flag:   Specifies the flag to check
716  *                      This parameter can be any combination of the following values:
717  *                      @arg ADC_FLAG_ADRDY:    ADC ready flag
718  *                      @arg ADC_FLAG_CSMP:     End of sampling flag
719  *                      @arg ADC_FLAG_CC:       End of conversion flag
720  *                      @arg ADC_FLAG_CS:       End of sequence flag
721  *                      @arg ADC_FLAG_OVR:      ADC overrun flag
722  *                      @arg ADC_FLAG_AWD:      Analog watchdog flag
723  *
724  * @retval      None
725  */
ADC_ClearStatusFlag(uint32_t flag)726 void ADC_ClearStatusFlag(uint32_t flag)
727 {
728     ADC->STS = (uint32_t)flag;
729 }
730 
731 /*!
732  * @brief       Checks whether the specified interrupt has occurred or not
733  *
734  * @param       flag:   Specifies the ADC interrupt pending bit to check
735  *                      The parameter can be one of following values:
736  *                      @arg ADC_INT_FLAG_ADRDY:    ADC ready interrupt
737  *                      @arg ADC_INT_FLAG_CSMP:     End of sampling interrupt
738  *                      @arg ADC_INT_FLAG_CC:       End of conversion interrupt
739  *                      @arg ADC_INT_FLAG_CS:       End of sequence interrupt
740  *                      @arg ADC_INT_FLAG_OVR:      ADC overrun interrupt
741  *                      @arg ADC_INT_FLAG_AWD:      Analog watchdog interrupt
742  *
743  * @retval      None
744  */
ADC_ReadIntFlag(ADC_INT_FLAG_T flag)745 uint8_t ADC_ReadIntFlag(ADC_INT_FLAG_T flag)
746 {
747     uint8_t intEnable;
748     uint8_t intStatus;
749 
750     intEnable = (uint8_t)(ADC->INT& (uint32_t)flag);
751 
752     intStatus = (uint8_t)(ADC->STS & (uint32_t)(flag & 0xff));
753 
754     if (intEnable && intStatus)
755     {
756         return SET;
757     }
758 
759     return RESET;
760 }
761 
762 /*!
763  * @brief       Clears the specified interrupt pending bits
764  *
765  * @param       flag:   Specifies the ADC interrupt pending bit to check
766  *                      The parameter can be combination of following values:
767  *                      @arg ADC_INT_FLAG_ADRDY:    ADC ready interrupt
768  *                      @arg ADC_INT_FLAG_CSMP:     End of sampling interrupt
769  *                      @arg ADC_INT_FLAG_CC:       End of conversion interrupt
770  *                      @arg ADC_INT_FLAG_CS:       End of sequence interrupt
771  *                      @arg ADC_INT_FLAG_OVR:      ADC overrun interrupt
772  *                      @arg ADC_INT_FLAG_AWD:      Analog watchdog interrupt
773  *
774  * @retval      None
775  */
776 
ADC_ClearIntFlag(uint32_t flag)777 void ADC_ClearIntFlag(uint32_t flag)
778 {
779     ADC->STS = flag;
780 }
781 
782 /**@} end of group ADC_Functions */
783 /**@} end of group ADC_Driver */
784 /**@} end of group APM32F0xx_StdPeriphDriver */
785