1 /*!
2     \file    gd32f403_adc.c
3     \brief   ADC driver
4 
5     \version 2017-02-10, V1.0.0, firmware for gd32F403
6     \version 2018-12-25, V2.0.0, firmware for gd32F403
7     \version 2020-09-30, V2.1.0, firmware for GD32F403
8 */
9 
10 /*
11     Copyright (c) 2020, 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 "gd32f403_adc.h"
38 
39 /*!
40     \brief      reset ADC
41     \param[in]  adc_periph: ADCx,x=0,1,2
42                 only one among these parameters can be selected
43     \param[out] none
44     \retval     none
45 */
adc_deinit(uint32_t adc_periph)46 void adc_deinit(uint32_t adc_periph)
47 {
48     switch(adc_periph){
49     case ADC0:
50         rcu_periph_reset_enable(RCU_ADC0RST);
51         rcu_periph_reset_disable(RCU_ADC0RST);
52         break;
53     case ADC1:
54         rcu_periph_reset_enable(RCU_ADC1RST);
55         rcu_periph_reset_disable(RCU_ADC1RST);
56         break;
57     case ADC2:
58         rcu_periph_reset_enable(RCU_ADC2RST);
59         rcu_periph_reset_disable(RCU_ADC2RST);
60         break;
61     default:
62         break;
63     }
64 }
65 
66 /*!
67     \brief      enable ADC interface
68     \param[in]  adc_periph: ADCx,x=0,1,2
69                 only one among these parameters can be selected
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                 only one among these parameters can be selected
84     \param[out] none
85     \retval     none
86 */
adc_disable(uint32_t adc_periph)87 void adc_disable(uint32_t adc_periph)
88 {
89     ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ADCON);
90 }
91 
92 /*!
93     \brief      ADC calibration and reset calibration
94     \param[in]  adc_periph: ADCx,x=0,1,2
95                 only one among these parameters can be selected
96     \param[out] none
97     \retval     none
98 */
adc_calibration_enable(uint32_t adc_periph)99 void adc_calibration_enable(uint32_t adc_periph)
100 {
101     /* reset the selected ADC calibration registers */
102     ADC_CTL1(adc_periph) |= (uint32_t) ADC_CTL1_RSTCLB;
103     /* check the RSTCLB bit state */
104     while((ADC_CTL1(adc_periph) & ADC_CTL1_RSTCLB)){
105     }
106     /* enable ADC calibration process */
107     ADC_CTL1(adc_periph) |= ADC_CTL1_CLB;
108     /* check the CLB bit state */
109     while((ADC_CTL1(adc_periph) & ADC_CTL1_CLB)){
110     }
111 }
112 
113 /*!
114     \brief      enable DMA request
115     \param[in]  adc_periph: ADCx,x=0,1,2
116                 only one among these parameters can be selected
117     \param[out] none
118     \retval     none
119 */
adc_dma_mode_enable(uint32_t adc_periph)120 void adc_dma_mode_enable(uint32_t adc_periph)
121 {
122     ADC_CTL1(adc_periph) |= (uint32_t)(ADC_CTL1_DMA);
123 }
124 
125 /*!
126     \brief      disable DMA request
127     \param[in]  adc_periph: ADCx,x=0,1,2
128                 only one among these parameters can be selected
129     \param[out] none
130     \retval     none
131 */
adc_dma_mode_disable(uint32_t adc_periph)132 void adc_dma_mode_disable(uint32_t adc_periph)
133 {
134     ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DMA);
135 }
136 
137 /*!
138     \brief      enable the temperature sensor and Vrefint channel
139     \param[in]  none
140     \param[out] none
141     \retval     none
142 */
adc_tempsensor_vrefint_enable(void)143 void adc_tempsensor_vrefint_enable(void)
144 {
145     /* enable the temperature sensor and Vrefint channel */
146     ADC_CTL1(ADC0) |= ADC_CTL1_TSVREN;
147 }
148 
149 /*!
150     \brief      disable the temperature sensor and Vrefint channel
151     \param[in]  none
152     \param[out] none
153     \retval     none
154 */
adc_tempsensor_vrefint_disable(void)155 void adc_tempsensor_vrefint_disable(void)
156 {
157     /* disable the temperature sensor and Vrefint channel */
158     ADC_CTL1(ADC0) &= ~ADC_CTL1_TSVREN;
159 }
160 
161 /*!
162     \brief      configure ADC resolution
163     \param[in]  adc_periph: ADCx,x=0,1,2
164                 only one among these parameters can be selected
165     \param[in]  resolution: ADC resolution
166                 only one among these parameters can be selected
167       \arg        ADC_RESOLUTION_12B: 12-bit ADC resolution
168       \arg        ADC_RESOLUTION_10B: 10-bit ADC resolution
169       \arg        ADC_RESOLUTION_8B: 8-bit ADC resolution
170       \arg        ADC_RESOLUTION_6B: 6-bit ADC resolution
171     \param[out] none
172     \retval     none
173 */
adc_resolution_config(uint32_t adc_periph,uint32_t resolution)174 void adc_resolution_config(uint32_t adc_periph , uint32_t resolution)
175 {
176     ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)ADC_OVSAMPCTL_DRES);
177     ADC_OVSAMPCTL(adc_periph) |= (uint32_t)resolution;
178 }
179 
180 /*!
181     \brief      configure ADC discontinuous mode
182     \param[in]  adc_periph: ADCx,x=0,1,2
183                 only one among these parameters can be selected
184     \param[in]  adc_channel_group: select the channel group
185                 only one among these parameters can be selected
186       \arg        ADC_REGULAR_CHANNEL: regular channel group
187       \arg        ADC_INSERTED_CHANNEL: inserted channel group
188       \arg        ADC_CHANNEL_DISCON_DISABLE: disable discontinuous mode of regular & inserted channel
189     \param[in]  length: number of conversions in discontinuous mode,the number can be 1..8
190                         for regular channel ,the number has no effect for inserted channel
191     \param[out] none
192     \retval     none
193 */
adc_discontinuous_mode_config(uint32_t adc_periph,uint8_t adc_channel_group,uint8_t length)194 void adc_discontinuous_mode_config(uint32_t adc_periph, uint8_t adc_channel_group, uint8_t length)
195 {
196     ADC_CTL0(adc_periph) &= ~((uint32_t)( ADC_CTL0_DISRC | ADC_CTL0_DISIC ));
197     switch(adc_channel_group){
198     case ADC_REGULAR_CHANNEL:
199         /* config the number of conversions in discontinuous mode  */
200         ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_DISNUM);
201         ADC_CTL0(adc_periph) |= CTL0_DISNUM(((uint32_t)length - 1U));
202 
203         ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_DISRC;
204         break;
205     case ADC_INSERTED_CHANNEL:
206         ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_DISIC;
207         break;
208     case ADC_CHANNEL_DISCON_DISABLE:
209     default:
210         break;
211     }
212 }
213 
214 /*!
215     \brief      configure the ADC sync mode
216     \param[in]  mode: ADC mode
217                 only one among these parameters can be selected
218       \arg        ADC_MODE_FREE: all the ADCs work independently
219       \arg        ADC_DAUL_REGULAL_PARALLEL_INSERTED_PARALLEL: ADC0 and ADC1 work in combined regular parallel + inserted parallel mode
220       \arg        ADC_DAUL_REGULAL_PARALLEL_INSERTED_ROTATION: ADC0 and ADC1 work in combined regular parallel + trigger rotation mode
221       \arg        ADC_DAUL_INSERTED_PARALLEL_REGULAL_FOLLOWUP_FAST: ADC0 and ADC1 work in combined inserted parallel + follow-up fast mode
222       \arg        ADC_DAUL_INSERTED_PARALLEL_REGULAL_FOLLOWUP_SLOW: ADC0 and ADC1 work in combined inserted parallel + follow-up slow mode
223       \arg        ADC_DAUL_INSERTED_PARALLEL: ADC0 and ADC1 work in inserted parallel mode only
224       \arg        ADC_DAUL_REGULAL_PARALLEL: ADC0 and ADC1 work in regular parallel mode only
225       \arg        ADC_DAUL_REGULAL_FOLLOWUP_FAST: ADC0 and ADC1 work in follow-up fast mode only
226       \arg        ADC_DAUL_REGULAL_FOLLOWUP_SLOW: ADC0 and ADC1 work in follow-up slow mode only
227       \arg        ADC_DAUL_INSERTED_TRRIGGER_ROTATION: ADC0 and ADC1 work in trigger rotation mode only
228     \param[out] none
229     \retval     none
230 */
adc_mode_config(uint32_t mode)231 void adc_mode_config(uint32_t mode)
232 {
233     ADC_CTL0(ADC0) &= ~(ADC_CTL0_SYNCM);
234     ADC_CTL0(ADC0) |= mode;
235 }
236 
237 /*!
238     \brief      enable or disable ADC special function
239     \param[in]  adc_periph: ADCx,x=0,1,2
240                 only one among these parameters can be selected
241     \param[in]  function: the function to config
242                 one or more parameters can be selected below
243       \arg        ADC_SCAN_MODE: scan mode select
244       \arg        ADC_INSERTED_CHANNEL_AUTO: inserted channel group convert automatically
245       \arg        ADC_CONTINUOUS_MODE: continuous mode select
246     \param[in]  newvalue: ENABLE or DISABLE
247     \param[out] none
248     \retval     none
249 */
adc_special_function_config(uint32_t adc_periph,uint32_t function,ControlStatus newvalue)250 void adc_special_function_config(uint32_t adc_periph , uint32_t function , ControlStatus newvalue)
251 {
252     if(newvalue){
253         if(0U != (function & ADC_SCAN_MODE)){
254             ADC_CTL0(adc_periph) |= ADC_SCAN_MODE;
255         }
256         if(0U != (function & ADC_INSERTED_CHANNEL_AUTO)){
257             ADC_CTL0(adc_periph) |= ADC_INSERTED_CHANNEL_AUTO;
258         }
259         if(0U != (function & ADC_CONTINUOUS_MODE)){
260             ADC_CTL1(adc_periph) |= ADC_CONTINUOUS_MODE;
261         }
262     }else{
263         if(0U != (function & ADC_SCAN_MODE)){
264             ADC_CTL0(adc_periph) &= ~ADC_SCAN_MODE;
265         }
266         if(0U != (function & ADC_INSERTED_CHANNEL_AUTO)){
267             ADC_CTL0(adc_periph) &= ~ADC_INSERTED_CHANNEL_AUTO;
268         }
269         if(0U != (function & ADC_CONTINUOUS_MODE)){
270             ADC_CTL1(adc_periph) &= ~ADC_CONTINUOUS_MODE;
271         }
272     }
273 }
274 
275 /*!
276     \brief      configure ADC data alignment
277     \param[in]  adc_periph: ADCx,x=0,1,2
278                 only one among these parameters can be selected
279     \param[in]  data_alignment: data alignment select
280                 only one parameter can be selected
281       \arg        ADC_DATAALIGN_RIGHT: LSB alignment
282       \arg        ADC_DATAALIGN_LEFT: MSB alignment
283     \param[out] none
284     \retval     none
285 */
adc_data_alignment_config(uint32_t adc_periph,uint32_t data_alignment)286 void adc_data_alignment_config(uint32_t adc_periph , uint32_t data_alignment)
287 {
288     if(ADC_DATAALIGN_RIGHT != data_alignment){
289         ADC_CTL1(adc_periph) |= ADC_CTL1_DAL;
290     }else{
291         ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DAL);
292     }
293 }
294 
295 /*!
296     \brief      configure the length of regular channel group or inserted channel group
297     \param[in]  adc_periph: ADCx,x=0,1,2
298                 only one among these parameters can be selected
299     \param[in]  adc_channel_group: select the channel group
300                 only one parameter can be selected
301       \arg        ADC_REGULAR_CHANNEL: regular channel group
302       \arg        ADC_INSERTED_CHANNEL: inserted channel group
303     \param[in]  length: the length of the channel
304                         regular channel 1-16
305                         inserted channel 1-4
306     \param[out] none
307     \retval     none
308 */
adc_channel_length_config(uint32_t adc_periph,uint8_t adc_channel_group,uint32_t length)309 void adc_channel_length_config(uint32_t adc_periph, uint8_t adc_channel_group, uint32_t length)
310 {
311     switch(adc_channel_group){
312     case ADC_REGULAR_CHANNEL:
313         ADC_RSQ0(adc_periph) &= ~((uint32_t)ADC_RSQ0_RL);
314         ADC_RSQ0(adc_periph) |= RSQ0_RL((uint32_t)(length-1U));
315 
316         break;
317     case ADC_INSERTED_CHANNEL:
318         ADC_ISQ(adc_periph) &= ~((uint32_t)ADC_ISQ_IL);
319         ADC_ISQ(adc_periph) |= ISQ_IL((uint32_t)(length-1U));
320 
321         break;
322     default:
323         break;
324     }
325 }
326 
327 /*!
328     \brief      configure ADC regular channel
329     \param[in]  adc_periph: ADCx,x=0,1,2
330                 only one among these parameters can be selected
331     \param[in]  rank: the regular group sequence rank,this parameter must be between 0 to 15
332     \param[in]  adc_channel: the selected ADC channel
333                 only one among these parameters can be selected
334       \arg        ADC_CHANNEL_x(x=0..17)(x=16 and x=17 are only for ADC0): ADC Channelx
335     \param[in]  sample_time: the sample time value
336                 only one parameter can be selected
337       \arg        ADC_SAMPLETIME_1POINT5: 1.5 cycles
338       \arg        ADC_SAMPLETIME_7POINT5: 7.5 cycles
339       \arg        ADC_SAMPLETIME_13POINT5: 13.5 cycles
340       \arg        ADC_SAMPLETIME_28POINT5: 28.5 cycles
341       \arg        ADC_SAMPLETIME_41POINT5: 41.5 cycles
342       \arg        ADC_SAMPLETIME_55POINT5: 55.5 cycles
343       \arg        ADC_SAMPLETIME_71POINT5: 71.5 cycles
344       \arg        ADC_SAMPLETIME_239POINT5: 239.5 cycles
345     \param[out] none
346     \retval     none
347 */
adc_regular_channel_config(uint32_t adc_periph,uint8_t rank,uint8_t adc_channel,uint32_t sample_time)348 void adc_regular_channel_config(uint32_t adc_periph , uint8_t rank , uint8_t adc_channel , uint32_t sample_time)
349 {
350     uint32_t rsq,sampt;
351 
352     /* ADC regular sequence config */
353     if(rank < 6U){
354         rsq = ADC_RSQ2(adc_periph);
355         rsq &=  ~((uint32_t)(ADC_RSQX_RSQN << (5U*rank)));
356         rsq |= ((uint32_t)adc_channel << (5U*rank));
357         ADC_RSQ2(adc_periph) = rsq;
358     }else if(rank < 12U){
359         rsq = ADC_RSQ1(adc_periph);
360         rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (5U*(rank-6U))));
361         rsq |= ((uint32_t)adc_channel << (5U*(rank-6U)));
362         ADC_RSQ1(adc_periph) = rsq;
363     }else if(rank < 16U){
364         rsq = ADC_RSQ0(adc_periph);
365         rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (5U*(rank-12U))));
366         rsq |= ((uint32_t)adc_channel << (5U*(rank-12U)));
367         ADC_RSQ0(adc_periph) = rsq;
368     }else{
369     }
370 
371     /* ADC sampling time config */
372     if(adc_channel < 10U){
373         sampt = ADC_SAMPT1(adc_periph);
374         sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*adc_channel)));
375         sampt |= (uint32_t)(sample_time << (3U*adc_channel));
376         ADC_SAMPT1(adc_periph) = sampt;
377     }else if(adc_channel < 18U){
378         sampt = ADC_SAMPT0(adc_periph);
379         sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*(adc_channel-10U))));
380         sampt |= (uint32_t)(sample_time << (3U*(adc_channel-10U)));
381         ADC_SAMPT0(adc_periph) = sampt;
382     }else{
383     }
384 }
385 
386 /*!
387     \brief      configure ADC inserted channel
388     \param[in]  adc_periph: ADCx,x=0,1,2
389                 only one among these parameters can be selected
390     \param[in]  rank: the inserted group sequencer rank,this parameter must be between 0 to 3
391     \param[in]  adc_channel: the selected ADC channel
392                 only one among these parameters can be selected
393       \arg        ADC_CHANNEL_x(x=0..17)(x=16 and x=17 are only for ADC0): ADC Channelx
394     \param[in]  sample_time: The sample time value
395                 only one parameter can be selected
396       \arg        ADC_SAMPLETIME_1POINT5: 1.5 cycles
397       \arg        ADC_SAMPLETIME_7POINT5: 7.5 cycles
398       \arg        ADC_SAMPLETIME_13POINT5: 13.5 cycles
399       \arg        ADC_SAMPLETIME_28POINT5: 28.5 cycles
400       \arg        ADC_SAMPLETIME_41POINT5: 41.5 cycles
401       \arg        ADC_SAMPLETIME_55POINT5: 55.5 cycles
402       \arg        ADC_SAMPLETIME_71POINT5: 71.5 cycles
403       \arg        ADC_SAMPLETIME_239POINT5: 239.5 cycles
404     \param[out] none
405     \retval     none
406 */
adc_inserted_channel_config(uint32_t adc_periph,uint8_t rank,uint8_t adc_channel,uint32_t sample_time)407 void adc_inserted_channel_config(uint32_t adc_periph , uint8_t rank , uint8_t adc_channel , uint32_t sample_time)
408 {
409     uint8_t inserted_length;
410     uint32_t isq,sampt;
411 
412     inserted_length = (uint8_t)GET_BITS(ADC_ISQ(adc_periph) , 20U , 21U);
413     isq = ADC_ISQ(adc_periph);
414     isq &= ~((uint32_t)(ADC_ISQ_ISQN << (5U * ((3 + rank) - inserted_length))));
415     isq |= ((uint32_t)adc_channel << (5U * ((3 + rank) - inserted_length)));
416     ADC_ISQ(adc_periph) = isq;
417 
418     /* ADC sampling time config */
419     if(adc_channel < 10U){
420         sampt = ADC_SAMPT1(adc_periph);
421         sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*adc_channel)));
422         sampt |= (uint32_t) sample_time << (3U*adc_channel);
423         ADC_SAMPT1(adc_periph) = sampt;
424     }else if(adc_channel < 18U){
425         sampt = ADC_SAMPT0(adc_periph);
426         sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*(adc_channel-10U))));
427         sampt |= ((uint32_t)sample_time << (3U*(adc_channel-10U)));
428         ADC_SAMPT0(adc_periph) = sampt;
429     }else{
430     }
431 }
432 
433 /*!
434     \brief      configure ADC inserted channel offset
435     \param[in]  adc_periph: ADCx,x=0,1,2
436                 only one among these parameters can be selected
437     \param[in]  inserted_channel : insert channel select
438                 only one parameter can be selected
439       \arg        ADC_INSERTED_CHANNEL_0: inserted channel0
440       \arg        ADC_INSERTED_CHANNEL_1: inserted channel1
441       \arg        ADC_INSERTED_CHANNEL_2: inserted channel2
442       \arg        ADC_INSERTED_CHANNEL_3: inserted channel3
443     \param[in]  offset : the offset data
444     \param[out] none
445     \retval     none
446 */
adc_inserted_channel_offset_config(uint32_t adc_periph,uint8_t inserted_channel,uint16_t offset)447 void adc_inserted_channel_offset_config(uint32_t adc_periph , uint8_t inserted_channel , uint16_t offset)
448 {
449     uint8_t inserted_length;
450     uint32_t num = 0U;
451 
452     inserted_length = (uint8_t)GET_BITS(ADC_ISQ(adc_periph) , 20U , 21U);
453     num = 3U - (inserted_length - inserted_channel);
454 
455     if(num <= 3U){
456         /* calculate the offset of the register */
457         num = num * 4U;
458         /* config the offset of the selected channels */
459         REG32((adc_periph) + 0x14U + num) = IOFFX_IOFF((uint32_t)offset);
460     }
461 }
462 
463 /*!
464     \brief      enable ADC external trigger
465     \param[in]  adc_periph: ADCx,x=0,1,2
466                 only one among these parameters can be selected
467     \param[in]  adc_channel_group: select the channel group
468                 one or more parameters can be selected
469       \arg        ADC_REGULAR_CHANNEL: regular channel group
470       \arg        ADC_INSERTED_CHANNEL: inserted channel group
471     \param[in]  newvalue: ENABLE or DISABLE
472     \param[out] none
473     \retval     none
474 */
adc_external_trigger_config(uint32_t adc_periph,uint8_t adc_channel_group,ControlStatus newvalue)475 void adc_external_trigger_config(uint32_t adc_periph, uint8_t adc_channel_group, ControlStatus newvalue)
476 {
477     if(newvalue){
478         if(0U != (adc_channel_group & ADC_REGULAR_CHANNEL)){
479             ADC_CTL1(adc_periph) |= ADC_CTL1_ETERC;
480         }
481         if(0U != (adc_channel_group & ADC_INSERTED_CHANNEL)){
482             ADC_CTL1(adc_periph) |= ADC_CTL1_ETEIC;
483         }
484     }else{
485         if(0U != (adc_channel_group & ADC_REGULAR_CHANNEL)){
486             ADC_CTL1(adc_periph) &= ~ADC_CTL1_ETERC;
487         }
488         if(0U != (adc_channel_group & ADC_INSERTED_CHANNEL)){
489             ADC_CTL1(adc_periph) &= ~ADC_CTL1_ETEIC;
490         }
491     }
492 }
493 
494 /*!
495     \brief      configure ADC external trigger source
496     \param[in]  adc_periph: ADCx,x=0,1,2
497                 only one among these parameters can be selected
498     \param[in]  adc_channel_group: select the channel group
499                 only one parameter can be selected
500       \arg        ADC_REGULAR_CHANNEL: regular channel group
501       \arg        ADC_INSERTED_CHANNEL: inserted channel group
502     \param[in]  external_trigger_source: regular or inserted group trigger source
503                 only one parameter can be selected
504                 for regular channel:
505       \arg        ADC0_1_EXTTRIG_REGULAR_T0_CH0: timer 0 CC0 event select
506       \arg        ADC0_1_EXTTRIG_REGULAR_T0_CH1: timer 0 CC1 event select
507       \arg        ADC0_1_EXTTRIG_REGULAR_T0_CH2: timer 0 CC2 event select
508       \arg        ADC0_1_EXTTRIG_REGULAR_T2_TRGO: timer 2 TRGO event select
509       \arg        ADC0_1_EXTTRIG_REGULAR_T3_CH3: timer 3 CC3 event select
510       \arg        ADC0_1_EXTTRIG_REGULAR_T7_TRGO: timer 7 TRGO event select
511       \arg        ADC0_1_EXTTRIG_REGULAR_EXTI_11 : external interrupt line 11
512       \arg        ADC2_EXTTRIG_REGULAR_T2_CH0: timer 2 CC0 event select
513       \arg        ADC2_EXTTRIG_REGULAR_T0_CH2: timer 0 CC2 event select
514       \arg        ADC2_EXTTRIG_REGULAR_T7_CH0: timer 7 CC0 event select
515       \arg        ADC2_EXTTRIG_REGULAR_T7_TRGO: timer 7 TRGO event select
516       \arg        ADC0_1_2_EXTTRIG_REGULAR_NONE: software trigger
517                 for inserted channel:
518       \arg        ADC0_1_EXTTRIG_INSERTED_T0_TRGO: timer 0 TRGO event select
519       \arg        ADC0_1_EXTTRIG_INSERTED_T0_CH3: timer 0 CC3 event select
520       \arg        ADC0_1_EXTTRIG_INSERTED_T2_CH3: timer 2 CC3 event select
521       \arg        ADC0_1_EXTTRIG_INSERTED_T3_TRGO: timer 3 TRGO event select
522       \arg        ADC0_1_EXTTRIG_INSERTED_EXTI_15: external interrupt line 15
523       \arg        ADC0_1_EXTTRIG_INSERTED_T7_CH3: timer 7 CC3 event select
524       \arg        ADC2_EXTTRIG_INSERTED_T0_TRGO: timer 0 TRGO event select
525       \arg        ADC2_EXTTRIG_INSERTED_T0_CH3: timer 0 CC3 event select
526       \arg        ADC2_EXTTRIG_INSERTED_T3_CH2: timer 3 CC2 event select
527       \arg        ADC2_EXTTRIG_INSERTED_T7_CH1: timer 7 CC1 event select
528       \arg        ADC2_EXTTRIG_INSERTED_T7_CH3: timer 7 CC3 event select
529       \arg        ADC0_1_2_EXTTRIG_INSERTED_NONE: software trigger
530     \param[out] none
531     \retval     none
532 */
adc_external_trigger_source_config(uint32_t adc_periph,uint8_t adc_channel_group,uint32_t external_trigger_source)533 void adc_external_trigger_source_config(uint32_t adc_periph, uint8_t adc_channel_group, uint32_t external_trigger_source)
534 {
535     switch(adc_channel_group){
536     case ADC_REGULAR_CHANNEL:
537         ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETSRC);
538         ADC_CTL1(adc_periph) |= (uint32_t)external_trigger_source;
539         break;
540     case ADC_INSERTED_CHANNEL:
541         ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETSIC);
542         ADC_CTL1(adc_periph) |= (uint32_t)external_trigger_source;
543         break;
544     default:
545         break;
546     }
547 }
548 
549 /*!
550     \brief      enable ADC software trigger
551     \param[in]  adc_periph: ADCx,x=0,1,2
552                 only one among these parameters can be selected
553     \param[in]  adc_channel_group: select the channel group
554                 one or more parameters can be selected
555       \arg        ADC_REGULAR_CHANNEL: regular channel group
556       \arg        ADC_INSERTED_CHANNEL: inserted channel group
557     \param[out] none
558     \retval     none
559 */
adc_software_trigger_enable(uint32_t adc_periph,uint8_t adc_channel_group)560 void adc_software_trigger_enable(uint32_t adc_periph , uint8_t adc_channel_group)
561 {
562     if(0U != (adc_channel_group & ADC_REGULAR_CHANNEL)){
563         ADC_CTL1(adc_periph) |= ADC_CTL1_SWRCST;
564     }
565     if(0U != (adc_channel_group & ADC_INSERTED_CHANNEL)){
566         ADC_CTL1(adc_periph) |= ADC_CTL1_SWICST;
567     }
568 }
569 
570 /*!
571     \brief      read ADC regular group data register
572     \param[in]  adc_periph: ADCx,x=0,1,2
573                 only one among these parameters can be selected
574     \param[in]  none
575     \param[out] none
576     \retval     the conversion value
577 */
adc_regular_data_read(uint32_t adc_periph)578 uint16_t adc_regular_data_read(uint32_t adc_periph)
579 {
580     return (uint16_t)(ADC_RDATA(adc_periph));
581 }
582 
583 /*!
584     \brief      read ADC inserted group data register
585     \param[in]  adc_periph: ADCx,x=0,1,2
586                 only one among these parameters can be selected
587     \param[in]  inserted_channel : insert channel select
588                 only one parameter can be selected
589       \arg        ADC_INSERTED_CHANNEL_0: inserted Channel0
590       \arg        ADC_INSERTED_CHANNEL_1: inserted channel1
591       \arg        ADC_INSERTED_CHANNEL_2: inserted Channel2
592       \arg        ADC_INSERTED_CHANNEL_3: inserted Channel3
593     \param[out] none
594     \retval     the conversion value
595 */
adc_inserted_data_read(uint32_t adc_periph,uint8_t inserted_channel)596 uint16_t adc_inserted_data_read(uint32_t adc_periph , uint8_t inserted_channel)
597 {
598     uint32_t idata;
599     /* read the data of the selected channel */
600     switch(inserted_channel){
601     case ADC_INSERTED_CHANNEL_0:
602         idata = ADC_IDATA0(adc_periph);
603         break;
604     case ADC_INSERTED_CHANNEL_1:
605         idata = ADC_IDATA1(adc_periph);
606         break;
607     case ADC_INSERTED_CHANNEL_2:
608         idata = ADC_IDATA2(adc_periph);
609         break;
610     case ADC_INSERTED_CHANNEL_3:
611         idata = ADC_IDATA3(adc_periph);
612         break;
613     default:
614         idata = 0U;
615         break;
616     }
617     return (uint16_t)idata;
618 }
619 
620 /*!
621     \brief      read the last ADC0 and ADC1 conversion result data in sync mode
622     \param[in]  none
623     \param[out] none
624     \retval     the conversion value
625 */
adc_sync_mode_convert_value_read(void)626 uint32_t adc_sync_mode_convert_value_read(void)
627 {
628     /* return conversion value */
629     return ADC_RDATA(ADC0);
630 }
631 
632 /*!
633     \brief      get the ADC flag bits
634     \param[in]  adc_periph: ADCx,x=0,1,2
635                 only one among these parameters can be selected
636     \param[in]  adc_flag: the adc flag bits
637                 only one parameter can be selected
638       \arg        ADC_FLAG_WDE: analog watchdog event flag
639       \arg        ADC_FLAG_EOC: end of group conversion flag
640       \arg        ADC_FLAG_EOIC: end of inserted group conversion flag
641       \arg        ADC_FLAG_STIC: start flag of inserted channel group
642       \arg        ADC_FLAG_STRC: start flag of regular channel group
643     \param[out] none
644     \retval     FlagStatus: SET or RESET
645 */
adc_flag_get(uint32_t adc_periph,uint32_t adc_flag)646 FlagStatus adc_flag_get(uint32_t adc_periph , uint32_t adc_flag)
647 {
648     FlagStatus reval = RESET;
649     if(ADC_STAT(adc_periph) & adc_flag){
650         reval = SET;
651     }
652     return reval;
653 }
654 
655 /*!
656     \brief      clear the ADC flag bits
657     \param[in]  adc_periph: ADCx,x=0,1,2
658                 only one among these parameters can be selected
659     \param[in]  adc_flag: the adc flag bits
660                 one or more parameters can be selected
661       \arg        ADC_FLAG_WDE: analog watchdog event flag
662       \arg        ADC_FLAG_EOC: end of group conversion flag
663       \arg        ADC_FLAG_EOIC: end of inserted group conversion flag
664       \arg        ADC_FLAG_STIC: start flag of inserted channel group
665       \arg        ADC_FLAG_STRC: start flag of regular channel group
666     \param[out] none
667     \retval     none
668 */
adc_flag_clear(uint32_t adc_periph,uint32_t adc_flag)669 void adc_flag_clear(uint32_t adc_periph , uint32_t adc_flag)
670 {
671     ADC_STAT(adc_periph) &= ~((uint32_t)adc_flag);
672 }
673 
674 /*!
675     \brief      get the ADC interrupt bits
676     \param[in]  adc_periph: ADCx,x=0,1,2
677                 only one among these parameters can be selected
678     \param[in]  adc_interrupt: the adc interrupt bits
679                 only oneparameter can be selected
680       \arg        ADC_INT_FLAG_WDE: analog watchdog interrupt
681       \arg        ADC_INT_FLAG_EOC: end of group conversion interrupt
682       \arg        ADC_INT_FLAG_EOIC: end of inserted group conversion interrupt
683     \param[out] none
684     \retval     FlagStatus: SET or RESET
685 */
adc_interrupt_flag_get(uint32_t adc_periph,uint32_t adc_interrupt)686 FlagStatus adc_interrupt_flag_get(uint32_t adc_periph , uint32_t adc_interrupt)
687 {
688     FlagStatus interrupt_flag = RESET;
689     uint32_t state;
690     /* check the interrupt bits */
691     switch(adc_interrupt){
692     case ADC_INT_FLAG_WDE:
693         state = ADC_STAT(adc_periph) & ADC_STAT_WDE;
694         if((ADC_CTL0(adc_periph) & ADC_CTL0_WDEIE) && state){
695           interrupt_flag = SET;
696         }
697         break;
698     case ADC_INT_FLAG_EOC:
699         state = ADC_STAT(adc_periph) & ADC_STAT_EOC;
700           if((ADC_CTL0(adc_periph) & ADC_CTL0_EOCIE) && state){
701             interrupt_flag = SET;
702           }
703         break;
704     case ADC_INT_FLAG_EOIC:
705         state = ADC_STAT(adc_periph) & ADC_STAT_EOIC;
706         if((ADC_CTL0(adc_periph) & ADC_CTL0_EOICIE) && state){
707             interrupt_flag = SET;
708         }
709         break;
710     default:
711         break;
712     }
713     return interrupt_flag;
714 }
715 
716 /*!
717     \brief      clear the ADC flag
718     \param[in]  adc_periph: ADCx,x=0,1,2
719                 only one among these parameters can be selected
720     \param[in]  adc_interrupt: the adc status flag
721                 one or more parameters can be selected
722       \arg        ADC_INT_FLAG_WDE: analog watchdog interrupt
723       \arg        ADC_INT_FLAG_EOC: end of group conversion interrupt
724       \arg        ADC_INT_FLAG_EOIC: end of inserted group conversion interrupt
725     \param[out] none
726     \retval     none
727 */
adc_interrupt_flag_clear(uint32_t adc_periph,uint32_t adc_interrupt)728 void adc_interrupt_flag_clear(uint32_t adc_periph , uint32_t adc_interrupt)
729 {
730     ADC_STAT(adc_periph) &= ~((uint32_t)adc_interrupt);
731 }
732 
733 /*!
734     \brief      enable ADC interrupt
735     \param[in]  adc_periph: ADCx,x=0,1,2
736                 only one among these parameters can be selected
737     \param[in]  adc_interrupt: the adc interrupt
738                 one or more parameters can be selected
739       \arg        ADC_INT_WDE: analog watchdog interrupt flag
740       \arg        ADC_INT_EOC: end of group conversion interrupt flag
741       \arg        ADC_INT_EOIC: end of inserted group conversion interrupt flag
742     \param[out] none
743     \retval     none
744 */
adc_interrupt_enable(uint32_t adc_periph,uint32_t adc_interrupt)745 void adc_interrupt_enable(uint32_t adc_periph , uint32_t adc_interrupt)
746 {
747     if(RESET != (adc_interrupt & ADC_INT_WDE)){
748         ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_WDEIE;
749     }
750 
751     if(RESET != (adc_interrupt & ADC_INT_EOC)){
752         ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_EOCIE;
753     }
754 
755     if(RESET != (adc_interrupt & ADC_INT_EOIC)){
756         ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_EOICIE;
757     }
758 }
759 
760 /*!
761     \brief      disable ADC interrupt
762     \param[in]  adc_periph: ADCx,x=0,1,2
763                 only one among these parameters can be selected
764     \param[in]  adc_interrupt: the adc interrupt flag
765                 one or more parameters can be selected
766       \arg        ADC_INT_WDE: analog watchdog interrupt flag
767       \arg        ADC_INT_EOC: end of group conversion interrupt flag
768       \arg        ADC_INT_EOIC: end of inserted group conversion interrupt flag
769     \param[out] none
770     \retval     none
771 */
adc_interrupt_disable(uint32_t adc_periph,uint32_t adc_interrupt)772 void adc_interrupt_disable(uint32_t adc_periph, uint32_t adc_interrupt)
773 {
774     if(RESET != (adc_interrupt & ADC_INT_WDE)){
775         ADC_CTL0(adc_periph) &= ~(uint32_t) ADC_CTL0_WDEIE;
776     }
777 
778     if(RESET != (adc_interrupt & ADC_INT_EOC)){
779         ADC_CTL0(adc_periph) &= ~(uint32_t) ADC_CTL0_EOCIE;
780     }
781 
782     if(RESET != (adc_interrupt & ADC_INT_EOIC)){
783         ADC_CTL0(adc_periph) &= ~(uint32_t) ADC_CTL0_EOICIE;
784     }
785 }
786 
787 /*!
788     \brief      configure ADC analog watchdog single channel
789     \param[in]  adc_periph: ADCx,x=0,1,2
790                 only one among these parameters can be selected
791     \param[in]  adc_channel: the selected ADC channel
792                 only one among these parameters can be selected
793       \arg        ADC_CHANNEL_x: ADC Channelx(x=0..17)(x=16 and x=17 are only for ADC0)
794     \param[out] none
795     \retval     none
796 */
adc_watchdog_single_channel_enable(uint32_t adc_periph,uint8_t adc_channel)797 void adc_watchdog_single_channel_enable(uint32_t adc_periph, uint8_t adc_channel)
798 {
799     ADC_CTL0(adc_periph) &= (uint32_t)~(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC | ADC_CTL0_WDCHSEL);
800 
801     ADC_CTL0(adc_periph) |= (uint32_t)adc_channel;
802     ADC_CTL0(adc_periph) |= (uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC);
803 }
804 
805 /*!
806     \brief      configure ADC analog watchdog group channel
807     \param[in]  adc_periph: ADCx,x=0,1,2
808                 only one among these parameters can be selected
809     \param[in]  adc_channel_group: the channel group use analog watchdog
810                 only one parameter can be selected
811       \arg        ADC_REGULAR_CHANNEL: regular channel group
812       \arg        ADC_INSERTED_CHANNEL: inserted channel group
813       \arg        ADC_REGULAR_INSERTED_CHANNEL: both regular and inserted group
814     \param[out] none
815     \retval     none
816 */
adc_watchdog_group_channel_enable(uint32_t adc_periph,uint8_t adc_channel_group)817 void adc_watchdog_group_channel_enable(uint32_t adc_periph, uint8_t adc_channel_group)
818 {
819     ADC_CTL0(adc_periph) &= (uint32_t)~(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC);
820     /* select the group */
821     switch(adc_channel_group){
822     case ADC_REGULAR_CHANNEL:
823         ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_RWDEN;
824         break;
825     case ADC_INSERTED_CHANNEL:
826         ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_IWDEN;
827         break;
828     case ADC_REGULAR_INSERTED_CHANNEL:
829         ADC_CTL0(adc_periph) |= (uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN);
830         break;
831     default:
832         break;
833     }
834 }
835 
836 /*!
837     \brief      disable ADC analog watchdog
838     \param[in]  adc_periph: ADCx,x=0,1,2
839                 only one among these parameters can be selected
840     \param[out] none
841     \retval     none
842 */
adc_watchdog_disable(uint32_t adc_periph)843 void adc_watchdog_disable(uint32_t adc_periph)
844 {
845     ADC_CTL0(adc_periph) &= (uint32_t)~(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC | ADC_CTL0_WDCHSEL);
846 }
847 
848 /*!
849     \brief      configure ADC analog watchdog threshold
850     \param[in]  adc_periph: ADCx,x=0,1,2
851                 only one among these parameters can be selected
852     \param[in]  low_threshold: analog watchdog low threshold,0..4095
853     \param[in]  high_threshold: analog watchdog high threshold,0..4095
854     \param[out] none
855     \retval     none
856 */
adc_watchdog_threshold_config(uint32_t adc_periph,uint16_t low_threshold,uint16_t high_threshold)857 void adc_watchdog_threshold_config(uint32_t adc_periph , uint16_t low_threshold , uint16_t high_threshold)
858 {
859     ADC_WDLT(adc_periph) = (uint32_t)WDLT_WDLT(low_threshold);
860     ADC_WDHT(adc_periph) = (uint32_t)WDHT_WDHT(high_threshold);
861 }
862 
863 /*!
864     \brief      configure ADC oversample mode
865     \param[in]  adc_periph: ADCx,x=0,1,2
866                 only one among these parameters can be selected
867     \param[in]  mode: ADC oversampling mode
868                 only oneparameter can be selected
869       \arg        ADC_OVERSAMPLING_ALL_CONVERT: all oversampled conversions for a channel are done consecutively after a trigger
870       \arg        ADC_OVERSAMPLING_ONE_CONVERT: each oversampled conversion for a channel needs a trigger
871     \param[in]  shift: ADC oversampling shift
872                 only oneparameter can be selected
873       \arg        ADC_OVERSAMPLING_SHIFT_NONE: no oversampling shift
874       \arg        ADC_OVERSAMPLING_SHIFT_1B: 1-bit oversampling shift
875       \arg        ADC_OVERSAMPLING_SHIFT_2B: 2-bit oversampling shift
876       \arg        ADC_OVERSAMPLING_SHIFT_3B: 3-bit oversampling shift
877       \arg        ADC_OVERSAMPLING_SHIFT_4B: 3-bit oversampling shift
878       \arg        ADC_OVERSAMPLING_SHIFT_5B: 5-bit oversampling shift
879       \arg        ADC_OVERSAMPLING_SHIFT_6B: 6-bit oversampling shift
880       \arg        ADC_OVERSAMPLING_SHIFT_7B: 7-bit oversampling shift
881       \arg        ADC_OVERSAMPLING_SHIFT_8B: 8-bit oversampling shift
882     \param[in]  ratio: ADC oversampling ratio
883                 only oneparameter can be selected
884       \arg        ADC_OVERSAMPLING_RATIO_MUL2: oversampling ratio multiple 2
885       \arg        ADC_OVERSAMPLING_RATIO_MUL4: oversampling ratio multiple 4
886       \arg        ADC_OVERSAMPLING_RATIO_MUL8: oversampling ratio multiple 8
887       \arg        ADC_OVERSAMPLING_RATIO_MUL16: oversampling ratio multiple 16
888       \arg        ADC_OVERSAMPLING_RATIO_MUL32: oversampling ratio multiple 32
889       \arg        ADC_OVERSAMPLING_RATIO_MUL64: oversampling ratio multiple 64
890       \arg        ADC_OVERSAMPLING_RATIO_MUL128: oversampling ratio multiple 128
891       \arg        ADC_OVERSAMPLING_RATIO_MUL256: oversampling ratio multiple 256
892     \param[out] none
893     \retval     none
894 */
adc_oversample_mode_config(uint32_t adc_periph,uint32_t mode,uint16_t shift,uint8_t ratio)895 void adc_oversample_mode_config(uint32_t adc_periph, uint32_t mode, uint16_t shift, uint8_t ratio)
896 {
897     if(ADC_OVERSAMPLING_ONE_CONVERT == mode){
898         ADC_OVSAMPCTL(adc_periph) |= (uint32_t)ADC_OVSAMPCTL_TOVS;
899     }else{
900         ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)ADC_OVSAMPCTL_TOVS);
901     }
902     /* config the shift and ratio */
903     ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)(ADC_OVSAMPCTL_OVSR | ADC_OVSAMPCTL_OVSS));
904     ADC_OVSAMPCTL(adc_periph) |= ((uint32_t)shift | (uint32_t)ratio);
905 }
906 
907 /*!
908     \brief      enable ADC oversample mode
909     \param[in]  adc_periph: ADCx,x=0,1,2
910                 only one among these parameters can be selected
911     \param[out] none
912     \retval     none
913 */
adc_oversample_mode_enable(uint32_t adc_periph)914 void adc_oversample_mode_enable(uint32_t adc_periph)
915 {
916     ADC_OVSAMPCTL(adc_periph) |= ADC_OVSAMPCTL_OVSEN;
917 }
918 
919 /*!
920     \brief      disable ADC oversample mode
921     \param[in]  adc_periph: ADCx,x=0,1,2
922                 only one among these parameters can be selected
923     \param[out] none
924     \retval     none
925 */
adc_oversample_mode_disable(uint32_t adc_periph)926 void adc_oversample_mode_disable(uint32_t adc_periph)
927 {
928     ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)ADC_OVSAMPCTL_OVSEN);
929 }
930