1 /*!
2     \file    gd32e50x_adc.c
3     \brief   ADC driver
4 
5     \version 2020-03-10, V1.0.0, firmware for GD32E50x
6     \version 2020-08-26, V1.1.0, firmware for GD32E50x
7     \version 2021-03-23, V1.2.0, firmware for GD32E50x
8 */
9 
10 /*
11     Copyright (c) 2021, GigaDevice Semiconductor Inc.
12 
13     Redistribution and use in source and binary forms, with or without modification,
14 are permitted provided that the following conditions are met:
15 
16     1. Redistributions of source code must retain the above copyright notice, this
17        list of conditions and the following disclaimer.
18     2. Redistributions in binary form must reproduce the above copyright notice,
19        this list of conditions and the following disclaimer in the documentation
20        and/or other materials provided with the distribution.
21     3. Neither the name of the copyright holder nor the names of its contributors
22        may be used to endorse or promote products derived from this software without
23        specific prior written permission.
24 
25     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 OF SUCH DAMAGE.
35 */
36 
37 #include "gd32e50x_adc.h"
38 
39 /*!
40     \brief      reset ADC
41     \param[in]  adc_periph: ADCx, x=0,1,2
42     \param[out] none
43     \retval     none
44 */
adc_deinit(uint32_t adc_periph)45 void adc_deinit(uint32_t adc_periph)
46 {
47     switch(adc_periph){
48     case ADC0:
49         rcu_periph_reset_enable(RCU_ADC0RST);
50         rcu_periph_reset_disable(RCU_ADC0RST);
51         break;
52     case ADC1:
53         rcu_periph_reset_enable(RCU_ADC1RST);
54         rcu_periph_reset_disable(RCU_ADC1RST);
55         break;
56 #if (defined(GD32E50X_HD) || defined(GD32E50X_XD)||defined(GD32EPRT))
57     case ADC2:
58         rcu_periph_reset_enable(RCU_ADC2RST);
59         rcu_periph_reset_disable(RCU_ADC2RST);
60         break;
61 #endif
62     default:
63         break;
64     }
65 }
66 
67 /*!
68     \brief      enable ADC interface
69     \param[in]  adc_periph: ADCx, x=0,1,2
70     \param[out] none
71     \retval     none
72 */
adc_enable(uint32_t adc_periph)73 void adc_enable(uint32_t adc_periph)
74 {
75     if(RESET == (ADC_CTL1(adc_periph) & ADC_CTL1_ADCON)){
76         ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_ADCON;
77     }
78 }
79 
80 /*!
81     \brief      disable ADC interface
82     \param[in]  adc_periph: ADCx, x=0,1,2
83     \param[out] none
84     \retval     none
85 */
adc_disable(uint32_t adc_periph)86 void adc_disable(uint32_t adc_periph)
87 {
88     ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ADCON);
89 }
90 
91 /*!
92     \brief      ADC calibration and reset calibration
93     \param[in]  adc_periph: ADCx, x=0,1,2
94     \param[out] none
95     \retval     none
96 */
adc_calibration_enable(uint32_t adc_periph)97 void adc_calibration_enable(uint32_t adc_periph)
98 {
99     /* reset the selected ADC calibration registers */
100     ADC_CTL1(adc_periph) |= (uint32_t) ADC_CTL1_RSTCLB;
101     /* check the RSTCLB bit state */
102     while((ADC_CTL1(adc_periph) & ADC_CTL1_RSTCLB)){
103     }
104     /* enable ADC calibration process */
105     ADC_CTL1(adc_periph) |= ADC_CTL1_CLB;
106     /* check the CLB bit state */
107     while((ADC_CTL1(adc_periph) & ADC_CTL1_CLB)){
108     }
109 }
110 
111 /*!
112     \brief      configure ADC calibration number
113     \param[in]  clb_num: calibration number
114                 only one parameter can be selected which is shown as below:
115       \arg        ADC_CALIBRATION_NUM1: calibrate once
116       \arg        ADC_CALIBRATION_NUM2: calibrate twice
117       \arg        ADC_CALIBRATION_NUM4: calibrate 4 times
118       \arg        ADC_CALIBRATION_NUM8: calibrate 8 times
119       \arg        ADC_CALIBRATION_NUM16: calibrate 16 times
120       \arg        ADC_CALIBRATION_NUM32: calibrate 32 times
121     \param[out] none
122     \retval     none
123 */
adc_calibration_number(uint32_t adc_periph,uint32_t clb_num)124 void adc_calibration_number(uint32_t adc_periph, uint32_t clb_num)
125 {
126     ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_CLBNUM);
127     ADC_CTL1(adc_periph) |= clb_num;
128 }
129 
130 /*!
131     \brief      enable DMA request
132     \param[in]  adc_periph: ADCx, x=0,1,2
133     \param[out] none
134     \retval     none
135 */
adc_dma_mode_enable(uint32_t adc_periph)136 void adc_dma_mode_enable(uint32_t adc_periph)
137 {
138     ADC_CTL1(adc_periph) |= (uint32_t)(ADC_CTL1_DMA);
139 }
140 
141 /*!
142     \brief      disable DMA request
143     \param[in]  adc_periph: ADCx,x=0,1,2
144     \param[out] none
145     \retval     none
146 */
adc_dma_mode_disable(uint32_t adc_periph)147 void adc_dma_mode_disable(uint32_t adc_periph)
148 {
149     ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DMA);
150 }
151 
152 /*!
153     \brief      enable the temperature sensor and vrefint channel
154     \param[in]  none
155     \param[out] none
156     \retval     none
157 */
adc_tempsensor_vrefint_enable(void)158 void adc_tempsensor_vrefint_enable(void)
159 {
160     /* enable the temperature sensor and vrefint channel */
161     ADC_CTL1(ADC0) |= ADC_CTL1_TSVREN;
162 }
163 
164 /*!
165     \brief      disable the temperature sensor and vrefint channel
166     \param[in]  none
167     \param[out] none
168     \retval     none
169 */
adc_tempsensor_vrefint_disable(void)170 void adc_tempsensor_vrefint_disable(void)
171 {
172     /* disable the temperature sensor and vrefint channel */
173     ADC_CTL1(ADC0) &= ~ADC_CTL1_TSVREN;
174 }
175 
176 /*!
177     \brief      configure ADC discontinuous mode
178     \param[in]  adc_periph: ADCx, x=0,1,2
179     \param[in]  adc_channel_group: select the channel group
180                 only one parameter can be selected which is shown as below:
181       \arg        ADC_REGULAR_CHANNEL: regular channel group
182       \arg        ADC_INSERTED_CHANNEL: inserted channel group
183       \arg        ADC_CHANNEL_DISCON_DISABLE: disable discontinuous mode of regular and inserted channel
184     \param[in]  length: number of conversions in discontinuous mode,the number can be 1..8
185                         for regular channel, the number has no effect for inserted channel
186     \param[out] none
187     \retval     none
188 */
adc_discontinuous_mode_config(uint32_t adc_periph,uint8_t adc_channel_group,uint8_t length)189 void adc_discontinuous_mode_config(uint32_t adc_periph, uint8_t adc_channel_group, uint8_t length)
190 {
191     ADC_CTL0(adc_periph) &= ~((uint32_t)( ADC_CTL0_DISRC | ADC_CTL0_DISIC ));
192     switch(adc_channel_group){
193     case ADC_REGULAR_CHANNEL:
194         /* configure the number of conversions in discontinuous mode  */
195         ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_DISNUM);
196         ADC_CTL0(adc_periph) |= CTL0_DISNUM(((uint32_t)length - 1U));
197         ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_DISRC;
198         break;
199     case ADC_INSERTED_CHANNEL:
200         ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_DISIC;
201         break;
202     case ADC_CHANNEL_DISCON_DISABLE:
203     default:
204         break;
205     }
206 }
207 
208 /*!
209     \brief      configure the ADC0 sync mode
210     \param[in]  mode: ADC0 mode
211                 only one parameter can be selected which is shown as below:
212       \arg        ADC_MODE_FREE: all the ADCs work independently
213       \arg        ADC_DAUL_REGULAL_PARALLEL_INSERTED_PARALLEL: ADC0 and ADC1 work in combined regular parallel + inserted parallel mode
214       \arg        ADC_DAUL_REGULAL_PARALLEL_INSERTED_ROTATION: ADC0 and ADC1 work in combined regular parallel + trigger rotation mode
215       \arg        ADC_DAUL_INSERTED_PARALLEL_REGULAL_FOLLOWUP_FAST: ADC0 and ADC1 work in combined inserted parallel + follow-up fast mode
216       \arg        ADC_DAUL_INSERTED_PARALLEL_REGULAL_FOLLOWUP_SLOW: ADC0 and ADC1 work in combined inserted parallel + follow-up slow mode
217       \arg        ADC_DAUL_INSERTED_PARALLEL: ADC0 and ADC1 work in inserted parallel mode only
218       \arg        ADC_DAUL_REGULAL_PARALLEL: ADC0 and ADC1 work in regular parallel mode only
219       \arg        ADC_DAUL_REGULAL_FOLLOWUP_FAST: ADC0 and ADC1 work in follow-up fast mode only
220       \arg        ADC_DAUL_REGULAL_FOLLOWUP_SLOW: ADC0 and ADC1 work in follow-up slow mode only
221       \arg        ADC_DAUL_INSERTED_TRRIGGER_ROTATION: ADC0 and ADC1 work in trigger rotation mode only
222     \param[out] none
223     \retval     none
224 */
adc_mode_config(uint32_t mode)225 void adc_mode_config(uint32_t mode)
226 {
227     ADC_CTL0(ADC0) &= ~((uint32_t)ADC_CTL0_SYNCM);
228     ADC_CTL0(ADC0) |= mode;
229 }
230 
231 /*!
232     \brief      configure ADC special function
233     \param[in]  adc_periph: ADCx, x=0,1,2
234     \param[in]  function: the function to configure
235                 one or more parameters can be selected which is shown as below:
236       \arg        ADC_SCAN_MODE: scan mode select
237       \arg        ADC_INSERTED_CHANNEL_AUTO: inserted channel group convert automatically
238       \arg        ADC_CONTINUOUS_MODE: continuous mode select
239     \param[in]  newvalue: ENABLE or DISABLE
240     \param[out] none
241     \retval     none
242 */
adc_special_function_config(uint32_t adc_periph,uint32_t function,ControlStatus newvalue)243 void adc_special_function_config(uint32_t adc_periph , uint32_t function , ControlStatus newvalue)
244 {
245     if(newvalue){
246         /* enable ADC scan mode */
247         if(RESET != (function & ADC_SCAN_MODE)){
248             ADC_CTL0(adc_periph) |= (uint32_t)ADC_SCAN_MODE;
249         }
250         /* enable ADC inserted channel group convert automatically */
251         if(RESET != (function & ADC_INSERTED_CHANNEL_AUTO)){
252             ADC_CTL0(adc_periph) |= (uint32_t)ADC_INSERTED_CHANNEL_AUTO;
253         }
254         /* enable ADC continuous mode */
255         if(RESET != (function & ADC_CONTINUOUS_MODE)){
256             ADC_CTL1(adc_periph) |= (uint32_t)ADC_CONTINUOUS_MODE;
257         }
258     }else{
259         /* disable ADC scan mode */
260         if(RESET != (function & ADC_SCAN_MODE)){
261             ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_SCAN_MODE);
262         }
263         /* disable ADC inserted channel group convert automatically */
264         if(RESET != (function & ADC_INSERTED_CHANNEL_AUTO)){
265             ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_INSERTED_CHANNEL_AUTO);
266         }
267         /* disable ADC continuous mode */
268         if(RESET != (function & ADC_CONTINUOUS_MODE)){
269             ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CONTINUOUS_MODE);
270         }
271     }
272 }
273 
274 /*!
275     \brief      configure ADC data alignment
276     \param[in]  adc_periph: ADCx, x=0,1,2
277     \param[in]  data_alignment: data alignment select
278                 only one parameter can be selected which is shown as below:
279       \arg        ADC_DATAALIGN_RIGHT: right alignment
280       \arg        ADC_DATAALIGN_LEFT: left alignment
281     \param[out] none
282     \retval     none
283 */
adc_data_alignment_config(uint32_t adc_periph,uint32_t data_alignment)284 void adc_data_alignment_config(uint32_t adc_periph , uint32_t data_alignment)
285 {
286     if(ADC_DATAALIGN_RIGHT != data_alignment){
287         ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_DAL;
288     }else{
289         ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DAL);
290     }
291 }
292 
293 /*!
294     \brief      configure the length of regular channel group or inserted channel group
295     \param[in]  adc_periph: ADCx, x=0,1,2
296     \param[in]  adc_channel_group: select the channel group
297                 only one parameter can be selected which is shown as below:
298       \arg        ADC_REGULAR_CHANNEL: regular channel group
299       \arg        ADC_INSERTED_CHANNEL: inserted channel group
300     \param[in]  length: the length of the channel
301                         regular channel 1-16
302                         inserted channel 1-4
303     \param[out] none
304     \retval     none
305 */
adc_channel_length_config(uint32_t adc_periph,uint8_t adc_channel_group,uint32_t length)306 void adc_channel_length_config(uint32_t adc_periph, uint8_t adc_channel_group, uint32_t length)
307 {
308     switch(adc_channel_group){
309     case ADC_REGULAR_CHANNEL:
310         /* configure the length of regular channel group */
311         ADC_RSQ0(adc_periph) &= ~((uint32_t)ADC_RSQ0_RL);
312         ADC_RSQ0(adc_periph) |= RSQ0_RL((uint32_t)(length-1U));
313         break;
314     case ADC_INSERTED_CHANNEL:
315         /* configure the length of inserted channel group */
316         ADC_ISQ(adc_periph) &= ~((uint32_t)ADC_ISQ_IL);
317         ADC_ISQ(adc_periph) |= ISQ_IL((uint32_t)(length-1U));
318         break;
319     default:
320         break;
321     }
322 }
323 
324 /*!
325     \brief      configure ADC regular channel
326     \param[in]  adc_periph: ADCx, x=0,1,2
327     \param[in]  rank: the regular group sequence rank,this parameter must be between 0 to 15
328     \param[in]  adc_channel: the selected ADC channel
329                 only one parameter can be selected which is shown as below:
330       \arg        ADC_CHANNEL_x(x=0..17)(x=16 and x=17 are only for ADC0): ADC Channelx
331     \param[in]  sample_time: the sample time value
332                 only one parameter can be selected which is shown as below:
333       \arg        ADC_SAMPLETIME_1POINT5: 1.5 cycles
334       \arg        ADC_SAMPLETIME_7POINT5: 7.5 cycles
335       \arg        ADC_SAMPLETIME_13POINT5: 13.5 cycles
336       \arg        ADC_SAMPLETIME_28POINT5: 28.5 cycles
337       \arg        ADC_SAMPLETIME_41POINT5: 41.5 cycles
338       \arg        ADC_SAMPLETIME_55POINT5: 55.5 cycles
339       \arg        ADC_SAMPLETIME_71POINT5: 71.5 cycles
340       \arg        ADC_SAMPLETIME_239POINT5: 239.5 cycles
341     \param[out] none
342     \retval     none
343 */
adc_regular_channel_config(uint32_t adc_periph,uint8_t rank,uint8_t adc_channel,uint32_t sample_time)344 void adc_regular_channel_config(uint32_t adc_periph, uint8_t rank, uint8_t adc_channel, uint32_t sample_time)
345 {
346     uint32_t rsq,sampt;
347 
348     /* configure ADC regular sequence */
349     if(rank < 6U){
350         rsq = ADC_RSQ2(adc_periph);
351         rsq &=  ~((uint32_t)(ADC_RSQX_RSQN << (5U*rank)));
352         rsq |= ((uint32_t)adc_channel << (5U*rank));
353         ADC_RSQ2(adc_periph) = rsq;
354     }else if(rank < 12U){
355         rsq = ADC_RSQ1(adc_periph);
356         rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (5U*(rank-6U))));
357         rsq |= ((uint32_t)adc_channel << (5U*(rank-6U)));
358         ADC_RSQ1(adc_periph) = rsq;
359     }else if(rank < 16U){
360         rsq = ADC_RSQ0(adc_periph);
361         rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (5U*(rank-12U))));
362         rsq |= ((uint32_t)adc_channel << (5U*(rank-12U)));
363         ADC_RSQ0(adc_periph) = rsq;
364     }else{
365     }
366 
367     /* configure ADC sampling time */
368     if(adc_channel < 10U){
369         sampt = ADC_SAMPT1(adc_periph);
370         sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*adc_channel)));
371         sampt |= (uint32_t)(sample_time << (3U*adc_channel));
372         ADC_SAMPT1(adc_periph) = sampt;
373     }else if(adc_channel < 18U){
374         sampt = ADC_SAMPT0(adc_periph);
375         sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*(adc_channel-10U))));
376         sampt |= (uint32_t)(sample_time << (3U*(adc_channel-10U)));
377         ADC_SAMPT0(adc_periph) = sampt;
378     }else{
379         /* illegal parameters */
380     }
381 }
382 
383 /*!
384     \brief      configure ADC inserted channel
385     \param[in]  adc_periph: ADCx, x=0,1,2
386     \param[in]  rank: the inserted group sequencer rank,this parameter must be between 0 to 3
387     \param[in]  adc_channel: the selected ADC channel
388                 only one parameter can be selected which is shown as below:
389       \arg        ADC_CHANNEL_x(x=0..17)(x=16 and x=17 are only for ADC0): ADC Channelx
390     \param[in]  sample_time: The sample time value
391                 only one parameter can be selected which is shown as below:
392       \arg        ADC_SAMPLETIME_1POINT5: 1.5 cycles
393       \arg        ADC_SAMPLETIME_7POINT5: 7.5 cycles
394       \arg        ADC_SAMPLETIME_13POINT5: 13.5 cycles
395       \arg        ADC_SAMPLETIME_28POINT5: 28.5 cycles
396       \arg        ADC_SAMPLETIME_41POINT5: 41.5 cycles
397       \arg        ADC_SAMPLETIME_55POINT5: 55.5 cycles
398       \arg        ADC_SAMPLETIME_71POINT5: 71.5 cycles
399       \arg        ADC_SAMPLETIME_239POINT5: 239.5 cycles
400     \param[out] none
401     \retval     none
402 */
adc_inserted_channel_config(uint32_t adc_periph,uint8_t rank,uint8_t adc_channel,uint32_t sample_time)403 void adc_inserted_channel_config(uint32_t adc_periph, uint8_t rank, uint8_t adc_channel, uint32_t sample_time)
404 {
405     uint8_t inserted_length;
406     uint32_t isq, sampt;
407 
408     /* get inserted channel group length */
409     inserted_length = (uint8_t)GET_BITS(ADC_ISQ(adc_periph), 20U, 21U);
410 
411     /* the channel number is written to these bits to select a channel as the nth conversion in the inserted channel group */
412     isq = ADC_ISQ(adc_periph);
413     isq &= ~((uint32_t)(ADC_ISQ_ISQN << (5U * ((3U + rank) - inserted_length))));
414     isq |= ((uint32_t)adc_channel << (5U * ((3U + rank) - inserted_length)));
415     ADC_ISQ(adc_periph) = isq;
416 
417     /* ADC sampling time config */
418     if(adc_channel < 10U){
419         sampt = ADC_SAMPT1(adc_periph);
420         sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*adc_channel)));
421         sampt |= (uint32_t) sample_time << (3U*adc_channel);
422         ADC_SAMPT1(adc_periph) = sampt;
423     }else if(adc_channel < 18U){
424         sampt = ADC_SAMPT0(adc_periph);
425         sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*(adc_channel-10U))));
426         sampt |= ((uint32_t)sample_time << (3U*(adc_channel-10U)));
427         ADC_SAMPT0(adc_periph) = sampt;
428     }else{
429     }
430 }
431 
432 /*!
433     \brief      configure ADC inserted channel offset
434     \param[in]  adc_periph: ADCx, x=0,1,2
435     \param[in]  inserted_channel : insert channel select
436                 only one parameter can be selected which is shown as below:
437       \arg        ADC_INSERTED_CHANNEL_0: ADC inserted channel0
438       \arg        ADC_INSERTED_CHANNEL_1: ADC inserted channel1
439       \arg        ADC_INSERTED_CHANNEL_2: ADC inserted channel2
440       \arg        ADC_INSERTED_CHANNEL_3: ADC inserted channel3
441     \param[in]  offset : the offset data
442     \param[out] none
443     \retval     none
444 */
adc_inserted_channel_offset_config(uint32_t adc_periph,uint8_t inserted_channel,uint16_t offset)445 void adc_inserted_channel_offset_config(uint32_t adc_periph, uint8_t inserted_channel, uint16_t offset)
446 {
447     uint8_t inserted_length;
448     uint32_t num = 0U;
449 
450     inserted_length = (uint8_t)GET_BITS(ADC_ISQ(adc_periph), 20U, 21U);
451     num = 3U - (inserted_length - inserted_channel);
452 
453     if(num <= 3U){
454         /* calculate the offset of the register */
455         num = num * 4U;
456         /* configure the offset of the selected channels */
457         REG32((adc_periph) + 0x14U + num) = IOFFX_IOFF((uint32_t)offset);
458     }
459 }
460 
461 /*!
462     \brief      configure differential mode for ADC channel
463     \param[in]  adc_periph: ADCx, x=0,1,2
464     \param[in]  adc_channel: the channel use differential mode
465                 one or more parameters can be selected which is shown as below:
466       \arg        ADC_DIFFERENTIAL_MODE_CHANNEL_x(x=0..14), ADC_DIFFERENTIAL_MODE_CHANNEL_ALL: ADC channel for differential mode(just for channel0~channel14)
467     \param[in]  newvalue: ENABLE or DISABLE
468     \param[out] none
469     \retval     none
470 */
adc_channel_differential_mode_config(uint32_t adc_periph,uint32_t adc_channel,ControlStatus newvalue)471 void adc_channel_differential_mode_config(uint32_t adc_periph, uint32_t adc_channel, ControlStatus newvalue)
472 {
473     if(ENABLE == newvalue){
474         ADC_DIFCTL(adc_periph) |= (uint32_t)adc_channel;
475     }else{
476         ADC_DIFCTL(adc_periph) &= ~((uint32_t)adc_channel);
477     }
478 }
479 
480 /*!
481     \brief      configure ADC external trigger
482     \param[in]  adc_periph: ADCx, x=0,1,2
483     \param[in]  adc_channel_group: select the channel group
484                 only one parameter can be selected which is shown as below:
485       \arg        ADC_REGULAR_CHANNEL: regular channel group
486       \arg        ADC_INSERTED_CHANNEL: inserted channel group
487     \param[in]  newvalue: ENABLE or DISABLE
488     \param[out] none
489     \retval     none
490 */
adc_external_trigger_config(uint32_t adc_periph,uint8_t adc_channel_group,ControlStatus newvalue)491 void adc_external_trigger_config(uint32_t adc_periph, uint8_t adc_channel_group, ControlStatus newvalue)
492 {
493     if(newvalue){
494         /* external trigger enable for regular channel */
495         if(RESET != (adc_channel_group & ADC_REGULAR_CHANNEL)){
496             ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_ETERC;
497         }
498         /* external trigger enable for inserted channel */
499         if(RESET != (adc_channel_group & ADC_INSERTED_CHANNEL)){
500             ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_ETEIC;
501         }
502     }else{
503         /* external trigger disable for regular channel */
504         if(RESET != (adc_channel_group & ADC_REGULAR_CHANNEL)){
505             ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETERC);
506         }
507         /* external trigger disable for inserted channel */
508         if(RESET != (adc_channel_group & ADC_INSERTED_CHANNEL)){
509             ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETEIC);
510         }
511     }
512 }
513 
514 /*!
515     \brief      configure ADC external trigger source
516     \param[in]  adc_periph: ADCx, x=0,1,2
517     \param[in]  adc_channel_group: select the channel group
518                 only one parameter can be selected which is shown as below:
519       \arg        ADC_REGULAR_CHANNEL: regular channel group
520       \arg        ADC_INSERTED_CHANNEL: inserted channel group
521     \param[in]  external_trigger_source: regular or inserted group trigger source
522                 only one parameter can be selected which is shown as below:
523                 for regular channel:
524       \arg        ADC0_1_EXTTRIG_REGULAR_T0_CH0: TIMER0 CH0 event select
525       \arg        ADC0_1_EXTTRIG_REGULAR_T0_CH1: TIMER0 CH1 event select
526       \arg        ADC0_1_EXTTRIG_REGULAR_T0_CH2: TIMER0 CH2 event select
527       \arg        ADC0_1_EXTTRIG_REGULAR_T1_CH1: TIMER1 CH1 event select
528       \arg        ADC0_1_EXTTRIG_REGULAR_T2_TRGO: TIMER2 TRGO event select
529       \arg        ADC0_1_EXTTRIG_REGULAR_T3_CH3: TIMER3 CH3 event select
530       \arg        ADC0_1_EXTTRIG_REGULAR_T7_TRGO: TIMER7 TRGO event select
531       \arg        ADC0_1_EXTTRIG_REGULAR_EXTI_11: external interrupt line 11
532       \arg        ADC0_1_EXTTRIG_REGULAR_SHRTIMER_ADCTRG0: SHRTIMER_ADCTRG0 output select(for GD32E50X_HD, GD32E50X_XD and GD32E50X_CL devices)
533       \arg        ADC0_1_EXTTRIG_REGULAR_SHRTIMER_ADCTRG2: SHRTIMER_ADCTRG2 output select(for GD32E50X_HD, GD32E50X_XD and GD32E50X_CL devices)
534       \arg        ADC2_EXTTRIG_REGULAR_T2_CH0: TIMER2 CH0 event select
535       \arg        ADC2_EXTTRIG_REGULAR_T1_CH2: TIMER1 CH2 event select
536       \arg        ADC2_EXTTRIG_REGULAR_T0_CH2: TIMER0 CH2 event select
537       \arg        ADC2_EXTTRIG_REGULAR_T7_CH0: TIMER7 CH0 event select
538       \arg        ADC2_EXTTRIG_REGULAR_T7_TRGO: TIMER7 TRGO event select
539       \arg        ADC2_EXTTRIG_REGULAR_T4_CH0: TIMER4 CH0 event select
540       \arg        ADC2_EXTTRIG_REGULAR_T4_CH2: TIMER4 CH2 event select
541       \arg        ADC0_1_2_EXTTRIG_REGULAR_NONE: software trigger
542                 for inserted channel:
543       \arg        ADC0_1_EXTTRIG_INSERTED_T0_TRGO: TIMER0 TRGO event select
544       \arg        ADC0_1_EXTTRIG_INSERTED_T0_CH3: TIMER0 CH3 event select
545       \arg        ADC0_1_EXTTRIG_INSERTED_T1_TRGO: TIMER1 TRGO event select
546       \arg        ADC0_1_EXTTRIG_INSERTED_T1_CH0: TIMER1 CH0 event select
547       \arg        ADC0_1_EXTTRIG_INSERTED_T2_CH3: TIMER2 CH3 event select
548       \arg        ADC0_1_EXTTRIG_INSERTED_T3_TRGO: TIMER3 TRGO event select
549       \arg        ADC0_1_EXTTRIG_INSERTED_T7_CH3: TIMER7 CH3 event select
550       \arg        ADC0_1_EXTTRIG_INSERTED_EXTI_15: external interrupt line 15
551       \arg        ADC0_1_EXTTRIG_INSERTED_SHRTIMER_ADCTRG1: SHRTIMER_ADCTRG1 output select(for GD32E50X_HD, GD32E50X_XD and GD32E50X_CL devices)
552       \arg        ADC0_1_EXTTRIG_INSERTED_SHRTIMER_ADCTRG3: SHRTIMER_ADCTRG3 output select(for GD32E50X_HD, GD32E50X_XD and GD32E50X_CL devices)
553       \arg        ADC2_EXTTRIG_INSERTED_T0_TRGO: TIMER0 TRGO event select
554       \arg        ADC2_EXTTRIG_INSERTED_T0_CH3: TIMER0 CH3 event select
555       \arg        ADC2_EXTTRIG_INSERTED_T3_CH2: TIMER3 CH2 event select
556       \arg        ADC2_EXTTRIG_INSERTED_T7_CH1: TIMER7 CH1 event select
557       \arg        ADC2_EXTTRIG_INSERTED_T7_CH3: TIMER7 CH3 event select
558       \arg        ADC2_EXTTRIG_INSERTED_T4_TRGO: TIMER4 TRGO event select
559       \arg        ADC2_EXTTRIG_INSERTED_T4_CH3: TIMER4 CH3 event select
560       \arg        ADC0_1_2_EXTTRIG_INSERTED_NONE: software trigger
561     \param[out] none
562     \retval     none
563 */
adc_external_trigger_source_config(uint32_t adc_periph,uint8_t adc_channel_group,uint32_t external_trigger_source)564 void adc_external_trigger_source_config(uint32_t adc_periph, uint8_t adc_channel_group, uint32_t external_trigger_source)
565 {
566     switch(adc_channel_group){
567     case ADC_REGULAR_CHANNEL:
568         /* external trigger select for regular channel */
569         ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETSRC);
570         ADC_CTL1(adc_periph) |= (uint32_t)external_trigger_source;
571         break;
572     case ADC_INSERTED_CHANNEL:
573         /* external trigger select for inserted channel */
574         ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETSIC);
575         ADC_CTL1(adc_periph) |= (uint32_t)external_trigger_source;
576         break;
577     default:
578         break;
579     }
580 }
581 
582 /*!
583     \brief      enable ADC software trigger
584     \param[in]  adc_periph: ADCx, x=0,1,2
585     \param[in]  adc_channel_group: select the channel group
586                 only one parameter can be selected which is shown as below:
587       \arg        ADC_REGULAR_CHANNEL: regular channel group
588       \arg        ADC_INSERTED_CHANNEL: inserted channel group
589     \param[out] none
590     \retval     none
591 */
adc_software_trigger_enable(uint32_t adc_periph,uint8_t adc_channel_group)592 void adc_software_trigger_enable(uint32_t adc_periph, uint8_t adc_channel_group)
593 {
594     /* enable regular group channel software trigger */
595     if(RESET != (adc_channel_group & ADC_REGULAR_CHANNEL)){
596         ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_SWRCST;
597     }
598     /* enable inserted channel group software trigger */
599     if(RESET != (adc_channel_group & ADC_INSERTED_CHANNEL)){
600         ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_SWICST;
601     }
602 }
603 
604 /*!
605     \brief      read ADC regular group data register
606     \param[in]  adc_periph: ADCx, x=0,1,2
607     \param[in]  none
608     \param[out] none
609     \retval     the conversion value: 0~0xFFFF
610 */
adc_regular_data_read(uint32_t adc_periph)611 uint16_t adc_regular_data_read(uint32_t adc_periph)
612 {
613     return (uint16_t)(ADC_RDATA(adc_periph));
614 }
615 
616 /*!
617     \brief      read ADC inserted group data register
618     \param[in]  adc_periph: ADCx, x=0,1,2
619     \param[in]  inserted_channel: inserted channel select
620                 only one parameter can be selected which is shown as below:
621       \arg        ADC_INSERTED_CHANNEL_0: ADC inserted channel0
622       \arg        ADC_INSERTED_CHANNEL_1: ADC inserted channel1
623       \arg        ADC_INSERTED_CHANNEL_2: ADC inserted channel2
624       \arg        ADC_INSERTED_CHANNEL_3: ADC inserted channel3
625     \param[out] none
626     \retval     the conversion value: 0~0xFFFF
627 */
adc_inserted_data_read(uint32_t adc_periph,uint8_t inserted_channel)628 uint16_t adc_inserted_data_read(uint32_t adc_periph , uint8_t inserted_channel)
629 {
630     uint32_t idata;
631     /* read the data of the selected channel */
632     switch(inserted_channel){
633     case ADC_INSERTED_CHANNEL_0:
634         idata = ADC_IDATA0(adc_periph);
635         break;
636     case ADC_INSERTED_CHANNEL_1:
637         idata = ADC_IDATA1(adc_periph);
638         break;
639     case ADC_INSERTED_CHANNEL_2:
640         idata = ADC_IDATA2(adc_periph);
641         break;
642     case ADC_INSERTED_CHANNEL_3:
643         idata = ADC_IDATA3(adc_periph);
644         break;
645     default:
646         idata = 0U;
647         break;
648     }
649     return (uint16_t)idata;
650 }
651 
652 /*!
653     \brief      read the last ADC0 and ADC1 conversion result data in sync mode
654     \param[in]  none
655     \param[out] none
656     \retval     the conversion value: 0~0xFFFFFFFF
657 */
adc_sync_mode_convert_value_read(void)658 uint32_t adc_sync_mode_convert_value_read(void)
659 {
660     /* return conversion value */
661     return ADC_RDATA(ADC0);
662 }
663 
664 /*!
665     \brief      configure ADC analog watchdog 0 single channel
666     \param[in]  adc_periph: ADCx, x=0,1,2
667     \param[in]  adc_channel: the selected ADC channel
668                 only one parameter can be selected which is shown as below:
669       \arg        ADC_CHANNEL_x: ADC Channelx(x=0..17)(x=16 and x=17 are only for ADC0)
670     \param[out] none
671     \retval     none
672 */
adc_watchdog0_single_channel_enable(uint32_t adc_periph,uint8_t adc_channel)673 void adc_watchdog0_single_channel_enable(uint32_t adc_periph, uint8_t adc_channel)
674 {
675     ADC_CTL0(adc_periph) &= (uint32_t)~(ADC_CTL0_RWD0EN | ADC_CTL0_IWD0EN | ADC_CTL0_WD0SC | ADC_CTL0_WD0CHSEL);
676 
677     ADC_CTL0(adc_periph) |= (uint32_t)adc_channel;
678     ADC_CTL0(adc_periph) |= (uint32_t)(ADC_CTL0_RWD0EN | ADC_CTL0_IWD0EN | ADC_CTL0_WD0SC);
679 }
680 
681 /*!
682     \brief      configure ADC analog watchdog 0 group channel
683     \param[in]  adc_periph: ADCx, x=0,1,2
684     \param[in]  adc_channel_group: the channel group use analog watchdog 0
685                 only one parameter can be selected which is shown as below:
686       \arg        ADC_REGULAR_CHANNEL: regular channel group
687       \arg        ADC_INSERTED_CHANNEL: inserted channel group
688       \arg        ADC_REGULAR_INSERTED_CHANNEL: both regular and inserted group
689     \param[out] none
690     \retval     none
691 */
adc_watchdog0_group_channel_enable(uint32_t adc_periph,uint8_t adc_channel_group)692 void adc_watchdog0_group_channel_enable(uint32_t adc_periph, uint8_t adc_channel_group)
693 {
694     ADC_CTL0(adc_periph) &= (uint32_t)~(ADC_CTL0_RWD0EN | ADC_CTL0_IWD0EN | ADC_CTL0_WD0SC);
695     /* select the group */
696     switch(adc_channel_group){
697     case ADC_REGULAR_CHANNEL:
698         ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_RWD0EN;
699         break;
700     case ADC_INSERTED_CHANNEL:
701         ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_IWD0EN;
702         break;
703     case ADC_REGULAR_INSERTED_CHANNEL:
704         ADC_CTL0(adc_periph) |= (uint32_t)(ADC_CTL0_RWD0EN | ADC_CTL0_IWD0EN);
705         break;
706     default:
707         break;
708     }
709 }
710 
711 /*!
712     \brief      disable ADC analog watchdog 0
713     \param[in]  adc_periph: ADCx, x=0,1,2
714     \param[out] none
715     \retval     none
716 */
adc_watchdog0_disable(uint32_t adc_periph)717 void adc_watchdog0_disable(uint32_t adc_periph)
718 {
719     ADC_CTL0(adc_periph) &= (uint32_t)~(ADC_CTL0_RWD0EN | ADC_CTL0_IWD0EN | ADC_CTL0_WD0SC | ADC_CTL0_WD0CHSEL);
720 }
721 
722 /*!
723     \brief      configure ADC analog watchdog 1 channel
724     \param[in]  adc_periph: ADCx, x=0,1,2
725     \param[in]  adc_channel: the channel use analog watchdog 1
726                 one or more parameters can be selected which is shown as below:
727       \arg        ADC_AWD1_2_SELECTION_CHANNEL_x, ADC_AWD1_2_SELECTION_CHANNEL_ALL: ADC channel analog watchdog 1/2 selection
728     \param[in]  newvalue: ENABLE or DISABLE
729     \param[out] none
730     \retval     none
731 */
adc_watchdog1_channel_config(uint32_t adc_periph,uint32_t adc_channel,ControlStatus newvalue)732 void adc_watchdog1_channel_config(uint32_t adc_periph, uint32_t adc_channel, ControlStatus newvalue)
733 {
734     if(ENABLE == newvalue){
735         ADC_WD1SR(adc_periph) |= (uint32_t)adc_channel;
736     }else{
737         ADC_WD1SR(adc_periph) &= ~((uint32_t)adc_channel);
738     }
739 }
740 
741 /*!
742     \brief      configure ADC analog watchdog 2 channel
743     \param[in]  adc_periph: ADCx, x=0,1,2
744     \param[in]  adc_channel: the channel use analog watchdog 2
745                 one or more parameters can be selected which is shown as below:
746       \arg        ADC_AWD1_2_SELECTION_CHANNEL_x, ADC_AWD1_2_SELECTION_CHANNEL_ALL: ADC channel analog watchdog 1/2 selection
747     \param[in]  newvalue: ENABLE or DISABLE
748     \param[out] none
749     \retval     none
750 */
adc_watchdog2_channel_config(uint32_t adc_periph,uint32_t adc_channel,ControlStatus newvalue)751 void adc_watchdog2_channel_config(uint32_t adc_periph, uint32_t adc_channel, ControlStatus newvalue)
752 {
753     if(ENABLE == newvalue){
754         ADC_WD2SR(adc_periph) |= (uint32_t)adc_channel;
755     }else{
756         ADC_WD2SR(adc_periph) &= ~((uint32_t)adc_channel);
757     }
758 }
759 
760 /*!
761     \brief      disable ADC analog watchdog 1
762     \param[in]  adc_periph: ADCx, x=0,1,2
763     \param[out] none
764     \retval     none
765 */
adc_watchdog1_disable(uint32_t adc_periph)766 void adc_watchdog1_disable(uint32_t adc_periph)
767 {
768     ADC_WD1SR(adc_periph) &= (uint32_t)~(ADC_WD1SR_AWD1CS);
769 }
770 
771 /*!
772     \brief      disable ADC analog watchdog 2
773     \param[in]  adc_periph: ADCx, x=0,1,2
774     \param[out] none
775     \retval     none
776 */
adc_watchdog2_disable(uint32_t adc_periph)777 void adc_watchdog2_disable(uint32_t adc_periph)
778 {
779     ADC_WD2SR(adc_periph) &= (uint32_t)~(ADC_WD2SR_AWD2CS);
780 }
781 
782 /*!
783     \brief      configure ADC analog watchdog 0 threshold
784     \param[in]  adc_periph: ADCx, x=0,1,2
785     \param[in]  low_threshold: analog watchdog 0 low threshold, 0..4095
786     \param[in]  high_threshold: analog watchdog 0 high threshold, 0..4095
787     \param[out] none
788     \retval     none
789 */
adc_watchdog0_threshold_config(uint32_t adc_periph,uint16_t low_threshold,uint16_t high_threshold)790 void adc_watchdog0_threshold_config(uint32_t adc_periph, uint16_t low_threshold, uint16_t high_threshold)
791 {
792     ADC_WDLT0(adc_periph) = (uint32_t)WDLT0_WDLT0(low_threshold);
793     ADC_WDHT0(adc_periph) = (uint32_t)WDHT0_WDHT0(high_threshold);
794 }
795 
796 /*!
797     \brief      configure ADC analog watchdog 1 threshold
798     \param[in]  adc_periph: ADCx, x=0,1,2
799     \param[in]  low_threshold: analog watchdog 1 low threshold, 0..255
800     \param[in]  high_threshold: analog watchdog 1 high threshold, 0..255
801     \param[out] none
802     \retval     none
803 */
adc_watchdog1_threshold_config(uint32_t adc_periph,uint8_t low_threshold,uint8_t high_threshold)804 void adc_watchdog1_threshold_config(uint32_t adc_periph, uint8_t low_threshold, uint8_t high_threshold)
805 {
806     ADC_WDT1(adc_periph) &= ~((uint32_t)(ADC_WDT1_WDLT1 | ADC_WDT1_WDHT1));
807     /* configure ADC analog watchdog 1 threshold */
808     ADC_WDT1(adc_periph) |= (uint32_t)WDT1_WDLT1(low_threshold);
809     ADC_WDT1(adc_periph) |= (uint32_t)WDT1_WDHT1(high_threshold);
810 }
811 
812 /*!
813     \brief      configure ADC analog watchdog 2 threshold
814     \param[in]  adc_periph: ADCx, x=0,1,2
815     \param[in]  low_threshold: analog watchdog 2 low threshold, 0..255
816     \param[in]  high_threshold: analog watchdog 2 high threshold, 0..255
817     \param[out] none
818     \retval     none
819 */
adc_watchdog2_threshold_config(uint32_t adc_periph,uint8_t low_threshold,uint8_t high_threshold)820 void adc_watchdog2_threshold_config(uint32_t adc_periph, uint8_t low_threshold, uint8_t high_threshold)
821 {
822     ADC_WDT2(adc_periph) &= ~((uint32_t)(ADC_WDT2_WDLT2 | ADC_WDT2_WDHT2));
823     /* configure ADC analog watchdog 2 threshold */
824     ADC_WDT2(adc_periph) |= (uint32_t)WDT2_WDLT2(low_threshold);
825     ADC_WDT2(adc_periph) |= (uint32_t)WDT2_WDHT2(high_threshold);
826 }
827 
828 /*!
829     \brief      configure ADC resolution
830     \param[in]  adc_periph: ADCx, x=0,1,2
831     \param[in]  resolution: ADC resolution
832                 only one parameter can be selected which is shown as below:
833       \arg        ADC_RESOLUTION_12B: 12-bit ADC resolution
834       \arg        ADC_RESOLUTION_10B: 10-bit ADC resolution
835       \arg        ADC_RESOLUTION_8B: 8-bit ADC resolution
836       \arg        ADC_RESOLUTION_6B: 6-bit ADC resolution
837     \param[out] none
838     \retval     none
839 */
adc_resolution_config(uint32_t adc_periph,uint32_t resolution)840 void adc_resolution_config(uint32_t adc_periph, uint32_t resolution)
841 {
842     ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)ADC_OVSAMPCTL_DRES);
843     ADC_OVSAMPCTL(adc_periph) |= (uint32_t)resolution;
844 }
845 
846 /*!
847     \brief      configure ADC oversample mode
848     \param[in]  adc_periph: ADCx, x=0,1,2
849     \param[in]  mode: ADC oversampling mode
850                 only one parameter can be selected which is shown as below:
851       \arg        ADC_OVERSAMPLING_ALL_CONVERT: all oversampled conversions for a channel are done consecutively after a trigger
852       \arg        ADC_OVERSAMPLING_ONE_CONVERT: each oversampled conversion for a channel needs a trigger
853     \param[in]  shift: ADC oversampling shift
854                 only one parameter can be selected which is shown as below:
855       \arg        ADC_OVERSAMPLING_SHIFT_NONE: no oversampling shift
856       \arg        ADC_OVERSAMPLING_SHIFT_1B: 1-bit oversampling shift
857       \arg        ADC_OVERSAMPLING_SHIFT_2B: 2-bit oversampling shift
858       \arg        ADC_OVERSAMPLING_SHIFT_3B: 3-bit oversampling shift
859       \arg        ADC_OVERSAMPLING_SHIFT_4B: 3-bit oversampling shift
860       \arg        ADC_OVERSAMPLING_SHIFT_5B: 5-bit oversampling shift
861       \arg        ADC_OVERSAMPLING_SHIFT_6B: 6-bit oversampling shift
862       \arg        ADC_OVERSAMPLING_SHIFT_7B: 7-bit oversampling shift
863       \arg        ADC_OVERSAMPLING_SHIFT_8B: 8-bit oversampling shift
864     \param[in]  ratio: ADC oversampling ratio
865                 only one parameter can be selected which is shown as below:
866       \arg        ADC_OVERSAMPLING_RATIO_MUL2: oversampling ratio multiple 2
867       \arg        ADC_OVERSAMPLING_RATIO_MUL4: oversampling ratio multiple 4
868       \arg        ADC_OVERSAMPLING_RATIO_MUL8: oversampling ratio multiple 8
869       \arg        ADC_OVERSAMPLING_RATIO_MUL16: oversampling ratio multiple 16
870       \arg        ADC_OVERSAMPLING_RATIO_MUL32: oversampling ratio multiple 32
871       \arg        ADC_OVERSAMPLING_RATIO_MUL64: oversampling ratio multiple 64
872       \arg        ADC_OVERSAMPLING_RATIO_MUL128: oversampling ratio multiple 128
873       \arg        ADC_OVERSAMPLING_RATIO_MUL256: oversampling ratio multiple 256
874     \param[out] none
875     \retval     none
876 */
adc_oversample_mode_config(uint32_t adc_periph,uint32_t mode,uint16_t shift,uint8_t ratio)877 void adc_oversample_mode_config(uint32_t adc_periph, uint32_t mode, uint16_t shift, uint8_t ratio)
878 {
879     /* configure ADC oversampling mode */
880     if(ADC_OVERSAMPLING_ONE_CONVERT == mode){
881         ADC_OVSAMPCTL(adc_periph) |= (uint32_t)ADC_OVSAMPCTL_TOVS;
882     }else{
883         ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)ADC_OVSAMPCTL_TOVS);
884     }
885     /* configure the shift and ratio */
886     ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)(ADC_OVSAMPCTL_OVSR | ADC_OVSAMPCTL_OVSS));
887     ADC_OVSAMPCTL(adc_periph) |= ((uint32_t)shift | (uint32_t)ratio);
888 }
889 
890 /*!
891     \brief      enable ADC oversample mode
892     \param[in]  adc_periph: ADCx, x=0,1,2
893     \param[out] none
894     \retval     none
895 */
adc_oversample_mode_enable(uint32_t adc_periph)896 void adc_oversample_mode_enable(uint32_t adc_periph)
897 {
898     ADC_OVSAMPCTL(adc_periph) |= (uint32_t)ADC_OVSAMPCTL_OVSEN;
899 }
900 
901 /*!
902     \brief      disable ADC oversample mode
903     \param[in]  adc_periph: ADCx, x=0,1,2
904     \param[out] none
905     \retval     none
906 */
adc_oversample_mode_disable(uint32_t adc_periph)907 void adc_oversample_mode_disable(uint32_t adc_periph)
908 {
909     ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)ADC_OVSAMPCTL_OVSEN);
910 }
911 
912 /*!
913     \brief      get the ADC flag
914     \param[in]  adc_periph: ADCx, x=0,1,2
915     \param[in]  flag: the ADC flag bits
916                 only one parameter can be selected which is shown as below:
917       \arg        ADC_FLAG_WDE0: analog watchdog 0 event flag
918       \arg        ADC_FLAG_EOC: end of group conversion flag
919       \arg        ADC_FLAG_EOIC: end of inserted group conversion flag
920       \arg        ADC_FLAG_STIC: start flag of inserted channel group
921       \arg        ADC_FLAG_STRC: start flag of regular channel group
922       \arg        ADC_FLAG_WDE1: analog watchdog 1 event flag
923       \arg        ADC_FLAG_WDE2: analog watchdog 2 event flag
924     \param[out] none
925     \retval     FlagStatus: SET or RESET
926 */
adc_flag_get(uint32_t adc_periph,uint32_t flag)927 FlagStatus adc_flag_get(uint32_t adc_periph, uint32_t flag)
928 {
929     FlagStatus reval = RESET;
930     if(ADC_STAT(adc_periph) & flag){
931         reval = SET;
932     }
933     return reval;
934 }
935 
936 /*!
937     \brief      clear the ADC flag
938     \param[in]  adc_periph: ADCx, x=0,1,2
939     \param[in]  flag: the ADC flag
940                 one or more parameters can be selected which is shown as below:
941       \arg        ADC_FLAG_WDE0: analog watchdog 0 event flag
942       \arg        ADC_FLAG_EOC: end of group conversion flag
943       \arg        ADC_FLAG_EOIC: end of inserted group conversion flag
944       \arg        ADC_FLAG_STIC: start flag of inserted channel group
945       \arg        ADC_FLAG_STRC: start flag of regular channel group
946       \arg        ADC_FLAG_WDE1: analog watchdog 1 event flag
947       \arg        ADC_FLAG_WDE2: analog watchdog 2 event flag
948     \param[out] none
949     \retval     none
950 */
adc_flag_clear(uint32_t adc_periph,uint32_t flag)951 void adc_flag_clear(uint32_t adc_periph , uint32_t flag)
952 {
953     ADC_STAT(adc_periph) &= ~((uint32_t)flag);
954 }
955 
956 /*!
957     \brief      enable ADC interrupt
958     \param[in]  adc_periph: ADCx, x=0,1,2
959     \param[in]  interrupt: the ADC interrupt
960                 one or more parameters can be selected which is shown as below:
961       \arg        ADC_INT_WDE0: analog watchdog 0 interrupt flag
962       \arg        ADC_INT_EOC: end of group conversion interrupt flag
963       \arg        ADC_INT_EOIC: end of inserted group conversion interrupt flag
964       \arg        ADC_INT_WDE1: analog watchdog 1 interrupt flag
965       \arg        ADC_INT_WDE2: analog watchdog 2 interrupt flag
966     \param[out] none
967     \retval     none
968 */
adc_interrupt_enable(uint32_t adc_periph,uint32_t interrupt)969 void adc_interrupt_enable(uint32_t adc_periph , uint32_t interrupt)
970 {
971     switch(interrupt){
972     /* enable analog watchdog 0 interrupt */
973     case ADC_INT_WDE0:
974         ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_WDE0IE;
975         break;
976     /* enable end of group conversion interrupt */
977     case ADC_INT_EOC:
978         ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_EOCIE;
979         break;
980     /* enable end of inserted group conversion interrupt */
981     case ADC_INT_EOIC:
982         ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_EOICIE;
983         break;
984     /* enable analog watchdog 1 interrupt */
985     case ADC_INT_WDE1:
986         ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_WDE1IE;
987         break;
988     /* enable analog watchdog 2 interrupt */
989     case ADC_INT_WDE2:
990         ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_WDE2IE;
991         break;
992     default:
993         break;
994     }
995 }
996 
997 /*!
998     \brief      disable ADC interrupt
999     \param[in]  adc_periph: ADCx, x=0,1,2
1000     \param[in]  interrupt: the ADC interrupt flag
1001                 one or more parameters can be selected which is shown as below:
1002       \arg        ADC_INT_WDE0: analog watchdog 0 interrupt flag
1003       \arg        ADC_INT_EOC: end of group conversion interrupt flag
1004       \arg        ADC_INT_EOIC: end of inserted group conversion interrupt flag
1005       \arg        ADC_INT_WDE1: analog watchdog 1 interrupt flag
1006       \arg        ADC_INT_WDE2: analog watchdog 2 interrupt flag
1007     \param[out] none
1008     \retval     none
1009 */
adc_interrupt_disable(uint32_t adc_periph,uint32_t interrupt)1010 void adc_interrupt_disable(uint32_t adc_periph, uint32_t interrupt)
1011 {
1012     switch(interrupt){
1013     /* disable analog watchdog 0 interrupt */
1014     case ADC_INT_WDE0:
1015         ADC_CTL0(adc_periph) &= ~(uint32_t)ADC_CTL0_WDE0IE;
1016         break;
1017     /* disable end of group conversion interrupt */
1018     case ADC_INT_EOC:
1019         ADC_CTL0(adc_periph) &= ~(uint32_t)ADC_CTL0_EOCIE;
1020         break;
1021     /* disable end of inserted group conversion interrupt */
1022     case ADC_INT_EOIC:
1023         ADC_CTL0(adc_periph) &= ~(uint32_t)ADC_CTL0_EOICIE;
1024         break;
1025     /* disable analog watchdog 1 interrupt */
1026     case ADC_INT_WDE1:
1027         ADC_CTL0(adc_periph) &= ~(uint32_t)ADC_CTL0_WDE1IE;
1028         break;
1029     /* disable analog watchdog 2 interrupt */
1030     case ADC_INT_WDE2:
1031         ADC_CTL0(adc_periph) &= ~(uint32_t)ADC_CTL0_WDE1IE;
1032         break;
1033     default:
1034         break;
1035     }
1036 }
1037 
1038 /*!
1039     \brief      get ADC interrupt flag
1040     \param[in]  adc_periph: ADCx, x=0,1,2
1041     \param[in]  int_flag: the ADC interrupt
1042                 only one parameter can be selected which is shown as below:
1043       \arg        ADC_INT_FLAG_WDE0: analog watchdog 0 interrupt flag
1044       \arg        ADC_INT_FLAG_EOC: end of group conversion interrupt flag
1045       \arg        ADC_INT_FLAG_EOIC: end of inserted group conversion interrupt
1046       \arg        ADC_INT_FLAG_WDE1: analog watchdog 1 interrupt flag
1047       \arg        ADC_INT_FLAG_WDE2: analog watchdog 2 interrupt flag
1048     \param[out] none
1049     \retval     FlagStatus: SET or RESET
1050 */
adc_interrupt_flag_get(uint32_t adc_periph,uint32_t int_flag)1051 FlagStatus adc_interrupt_flag_get(uint32_t adc_periph , uint32_t int_flag)
1052 {
1053     FlagStatus interrupt_flag = RESET;
1054     uint32_t state;
1055     /* check the interrupt bits */
1056     switch(int_flag){
1057     case ADC_INT_FLAG_WDE0:
1058         state = ADC_STAT(adc_periph) & ADC_STAT_WDE0;
1059         if((ADC_CTL0(adc_periph) & ADC_CTL0_WDE0IE) && state){
1060           interrupt_flag = SET;
1061         }
1062         break;
1063     case ADC_INT_FLAG_EOC:
1064         state = ADC_STAT(adc_periph) & ADC_STAT_EOC;
1065           if((ADC_CTL0(adc_periph) & ADC_CTL0_EOCIE) && state){
1066             interrupt_flag = SET;
1067           }
1068         break;
1069     case ADC_INT_FLAG_EOIC:
1070         state = ADC_STAT(adc_periph) & ADC_STAT_EOIC;
1071         if((ADC_CTL0(adc_periph) & ADC_CTL0_EOICIE) && state){
1072             interrupt_flag = SET;
1073         }
1074         break;
1075         case ADC_INT_FLAG_WDE1:
1076         state = ADC_STAT(adc_periph) & ADC_STAT_WDE1;
1077         if((ADC_CTL0(adc_periph) & ADC_CTL0_WDE1IE) && state){
1078           interrupt_flag = SET;
1079         }
1080         break;
1081        case ADC_INT_FLAG_WDE2:
1082         state = ADC_STAT(adc_periph) & ADC_STAT_WDE2;
1083         if((ADC_CTL0(adc_periph) & ADC_CTL0_WDE2IE) && state){
1084           interrupt_flag = SET;
1085         }
1086         break;
1087     default:
1088         break;
1089     }
1090     return interrupt_flag;
1091 }
1092 
1093 /*!
1094     \brief      clear ADC interrupt flag
1095     \param[in]  adc_periph: ADCx, x=0,1,2
1096     \param[in]  int_flag: the ADC interrupt flag
1097                 only one parameter can be selected which is shown as below:
1098       \arg        ADC_INT_FLAG_WDE0: analog watchdog 0 interrupt flag
1099       \arg        ADC_INT_FLAG_EOC: end of group conversion interrupt flag
1100       \arg        ADC_INT_FLAG_EOIC: end of inserted group conversion interrupt flag
1101       \arg        ADC_INT_FLAG_WDE1: analog watchdog 1 interrupt flag
1102       \arg        ADC_INT_FLAG_WDE2: analog watchdog 2 interrupt flag
1103     \param[out] none
1104     \retval     none
1105 */
adc_interrupt_flag_clear(uint32_t adc_periph,uint32_t int_flag)1106 void adc_interrupt_flag_clear(uint32_t adc_periph, uint32_t int_flag)
1107 {
1108     ADC_STAT(adc_periph) &= ~((uint32_t)int_flag);
1109 }
1110