1 /*!
2     \file    gd32vf103_adc.h
3     \brief   definitions for the ADC
4 
5     \version 2020-06-05, V1.0.0, firmware for GD32VF103
6     \version 2020-08-04, V1.1.0, firmware for GD32VF103
7 */
8 
9 /*
10     Copyright (c) 2020, GigaDevice Semiconductor Inc.
11 
12     Redistribution and use in source and binary forms, with or without modification,
13 are permitted provided that the following conditions are met:
14 
15     1. Redistributions of source code must retain the above copyright notice, this
16        list of conditions and the following disclaimer.
17     2. Redistributions in binary form must reproduce the above copyright notice,
18        this list of conditions and the following disclaimer in the documentation
19        and/or other materials provided with the distribution.
20     3. Neither the name of the copyright holder nor the names of its contributors
21        may be used to endorse or promote products derived from this software without
22        specific prior written permission.
23 
24     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGE.
34 */
35 
36 #ifndef GD32VF103_ADC_H
37 #define GD32VF103_ADC_H
38 
39 #include "gd32vf103.h"
40 
41 /* ADC definitions */
42 #define ADC0                            ADC_BASE
43 #define ADC1                            (ADC_BASE + 0x400U)
44 
45 /* registers definitions */
46 #define ADC_STAT(adcx)                  REG32((adcx) + 0x00U)            /*!< ADC status register */
47 #define ADC_CTL0(adcx)                  REG32((adcx) + 0x04U)            /*!< ADC control register 0 */
48 #define ADC_CTL1(adcx)                  REG32((adcx) + 0x08U)            /*!< ADC control register 1 */
49 #define ADC_SAMPT0(adcx)                REG32((adcx) + 0x0CU)            /*!< ADC sampling time register 0 */
50 #define ADC_SAMPT1(adcx)                REG32((adcx) + 0x10U)            /*!< ADC sampling time register 1 */
51 #define ADC_IOFF0(adcx)                 REG32((adcx) + 0x14U)            /*!< ADC inserted channel data offset register 0 */
52 #define ADC_IOFF1(adcx)                 REG32((adcx) + 0x18U)            /*!< ADC inserted channel data offset register 1 */
53 #define ADC_IOFF2(adcx)                 REG32((adcx) + 0x1CU)            /*!< ADC inserted channel data offset register 2 */
54 #define ADC_IOFF3(adcx)                 REG32((adcx) + 0x20U)            /*!< ADC inserted channel data offset register 3 */
55 #define ADC_WDHT(adcx)                  REG32((adcx) + 0x24U)            /*!< ADC watchdog high threshold register */
56 #define ADC_WDLT(adcx)                  REG32((adcx) + 0x28U)            /*!< ADC watchdog low threshold register */
57 #define ADC_RSQ0(adcx)                  REG32((adcx) + 0x2CU)            /*!< ADC regular sequence register 0 */
58 #define ADC_RSQ1(adcx)                  REG32((adcx) + 0x30U)            /*!< ADC regular sequence register 1 */
59 #define ADC_RSQ2(adcx)                  REG32((adcx) + 0x34U)            /*!< ADC regular sequence register 2 */
60 #define ADC_ISQ(adcx)                   REG32((adcx) + 0x38U)            /*!< ADC inserted sequence register */
61 #define ADC_IDATA0(adcx)                REG32((adcx) + 0x3CU)            /*!< ADC inserted data register 0 */
62 #define ADC_IDATA1(adcx)                REG32((adcx) + 0x40U)            /*!< ADC inserted data register 1 */
63 #define ADC_IDATA2(adcx)                REG32((adcx) + 0x44U)            /*!< ADC inserted data register 2 */
64 #define ADC_IDATA3(adcx)                REG32((adcx) + 0x48U)            /*!< ADC inserted data register 3 */
65 #define ADC_RDATA(adcx)                 REG32((adcx) + 0x4CU)            /*!< ADC regular data register */
66 #define ADC_OVSCR(adcx)                 REG32((adcx) + 0x80U)            /*!< ADC oversample control register */
67 
68 /* bits definitions */
69 /* ADC_STAT */
70 #define ADC_STAT_WDE                    BIT(0)                           /*!< analog watchdog event flag */
71 #define ADC_STAT_EOC                    BIT(1)                           /*!< end of conversion */
72 #define ADC_STAT_EOIC                   BIT(2)                           /*!< inserted channel end of conversion */
73 #define ADC_STAT_STIC                   BIT(3)                           /*!< inserted channel start flag */
74 #define ADC_STAT_STRC                   BIT(4)                           /*!< regular channel start flag */
75 
76 /* ADC_CTL0 */
77 #define ADC_CTL0_WDCHSEL                BITS(0,4)                        /*!< analog watchdog channel select bits */
78 #define ADC_CTL0_EOCIE                  BIT(5)                           /*!< interrupt enable for EOC */
79 #define ADC_CTL0_WDEIE                  BIT(6)                           /*!< analog watchdog interrupt enable */
80 #define ADC_CTL0_EOICIE                 BIT(7)                           /*!< interrupt enable for inserted channels */
81 #define ADC_CTL0_SM                     BIT(8)                           /*!< scan mode */
82 #define ADC_CTL0_WDSC                   BIT(9)                           /*!< when in scan mode, analog watchdog is effective on a single channel */
83 #define ADC_CTL0_ICA                    BIT(10)                          /*!< automatic inserted group conversion */
84 #define ADC_CTL0_DISRC                  BIT(11)                          /*!< discontinuous mode on regular channels */
85 #define ADC_CTL0_DISIC                  BIT(12)                          /*!< discontinuous mode on inserted channels */
86 #define ADC_CTL0_DISNUM                 BITS(13,15)                      /*!< discontinuous mode channel count */
87 #define ADC_CTL0_SYNCM                  BITS(16,19)                      /*!< sync mode selection */
88 #define ADC_CTL0_IWDEN                  BIT(22)                          /*!< analog watchdog enable on inserted channels */
89 #define ADC_CTL0_RWDEN                  BIT(23)                          /*!< analog watchdog enable on regular channels */
90 
91 /* ADC_CTL1 */
92 #define ADC_CTL1_ADCON                  BIT(0)                           /*!< ADC converter on */
93 #define ADC_CTL1_CTN                    BIT(1)                           /*!< continuous conversion */
94 #define ADC_CTL1_CLB                    BIT(2)                           /*!< ADC calibration */
95 #define ADC_CTL1_RSTCLB                 BIT(3)                           /*!< reset calibration */
96 #define ADC_CTL1_DMA                    BIT(8)                           /*!< direct memory access mode */
97 #define ADC_CTL1_DAL                    BIT(11)                          /*!< data alignment */
98 #define ADC_CTL1_ETSIC                  BITS(12,14)                      /*!< external trigger select for inserted channel */
99 #define ADC_CTL1_ETEIC                  BIT(15)                          /*!< external trigger enable for inserted channel */
100 #define ADC_CTL1_ETSRC                  BITS(17,19)                      /*!< external trigger select for regular channel */
101 #define ADC_CTL1_ETERC                  BIT(20)                          /*!< external trigger conversion mode for inserted channels */
102 #define ADC_CTL1_SWICST                 BIT(21)                          /*!< start on inserted channel */
103 #define ADC_CTL1_SWRCST                 BIT(22)                          /*!< start on regular channel */
104 #define ADC_CTL1_TSVREN                 BIT(23)                          /*!< channel 16 and 17 enable of ADC0 */
105 
106 /* ADC_SAMPTx x=0..1 */
107 #define ADC_SAMPTX_SPTN                 BITS(0,2)                        /*!< channel n sample time selection */
108 
109 /* ADC_IOFFx x=0..3 */
110 #define ADC_IOFFX_IOFF                  BITS(0,11)                       /*!< data offset for inserted channel x */
111 
112 /* ADC_WDHT */
113 #define ADC_WDHT_WDHT                   BITS(0,11)                       /*!< analog watchdog high threshold */
114 
115 /* ADC_WDLT */
116 #define ADC_WDLT_WDLT                   BITS(0,11)                       /*!< analog watchdog low threshold */
117 
118 /* ADC_RSQx x=0..2 */
119 #define ADC_RSQX_RSQN                   BITS(0,4)                        /*!< nth conversion in regular sequence */
120 #define ADC_RSQ0_RL                     BITS(20,23)                      /*!< regular channel sequence length */
121 
122 /* ADC_ISQ */
123 #define ADC_ISQ_ISQN                    BITS(0,4)                        /*!< nth conversion in inserted sequence */
124 #define ADC_ISQ_IL                      BITS(20,21)                      /*!< inserted sequence length */
125 
126 /* ADC_IDATAx x=0..3*/
127 #define ADC_IDATAX_IDATAN               BITS(0,15)                       /*!< inserted data n */
128 
129 /* ADC_RDATA */
130 #define ADC_RDATA_RDATA                 BITS(0,15)                       /*!< regular data */
131 #define ADC_RDATA_ADC1RDTR              BITS(16,31)                      /*!< ADC1 regular channel data */
132 
133 /* ADC_OVSCR */
134 #define ADC_OVSCR_OVSEN                 BIT(0)                           /*!< oversampling enable */
135 #define ADC_OVSCR_OVSR                  BITS(2,4)                        /*!< oversampling ratio */
136 #define ADC_OVSCR_OVSS                  BITS(5,8)                        /*!< oversampling shift */
137 #define ADC_OVSCR_TOVS                  BIT(9)                           /*!< triggered oversampling */
138 #define ADC_OVSCR_DRES                  BITS(12,13)                      /*!< ADC data resolution */
139 
140 /* constants definitions */
141 /* adc_stat register value */
142 #define ADC_FLAG_WDE                    ADC_STAT_WDE                                 /*!< analog watchdog event flag */
143 #define ADC_FLAG_EOC                    ADC_STAT_EOC                                 /*!< end of conversion */
144 #define ADC_FLAG_EOIC                   ADC_STAT_EOIC                                /*!< inserted channel end of conversion */
145 #define ADC_FLAG_STIC                   ADC_STAT_STIC                                /*!< inserted channel start flag */
146 #define ADC_FLAG_STRC                   ADC_STAT_STRC                                /*!< regular channel start flag */
147 
148 /* adc_ctl0 register value */
149 #define CTL0_DISNUM(regval)             (BITS(13,15) & ((uint32_t)(regval) << 13))   /*!< write value to ADC_CTL0_DISNUM bit field */
150 
151 /* scan mode */
152 #define ADC_SCAN_MODE                   ADC_CTL0_SM                                  /*!< scan mode */
153 
154 /* inserted channel group convert automatically */
155 #define ADC_INSERTED_CHANNEL_AUTO       ADC_CTL0_ICA                                 /*!< inserted channel group convert automatically */
156 
157 /* ADC sync mode */
158 #define CTL0_SYNCM(regval)              (BITS(16,19) & ((uint32_t)(regval) << 16))   /*!< write value to ADC_CTL0_SYNCM bit field */
159 #define ADC_MODE_FREE                                        CTL0_SYNCM(0)           /*!< all the ADCs work independently */
160 #define ADC_DAUL_REGULAL_PARALLEL_INSERTED_PARALLEL          CTL0_SYNCM(1)           /*!< ADC0 and ADC1 work in combined regular parallel + inserted parallel mode */
161 #define ADC_DAUL_REGULAL_PARALLEL_INSERTED_ROTATION          CTL0_SYNCM(2)           /*!< ADC0 and ADC1 work in combined regular parallel + trigger rotation mode */
162 #define ADC_DAUL_INSERTED_PARALLEL_REGULAL_FOLLOWUP_FAST     CTL0_SYNCM(3)           /*!< ADC0 and ADC1 work in combined inserted parallel + follow-up fast mode */
163 #define ADC_DAUL_INSERTED_PARALLEL_REGULAL_FOLLOWUP_SLOW     CTL0_SYNCM(4)           /*!< ADC0 and ADC1 work in combined inserted parallel + follow-up slow mode */
164 #define ADC_DAUL_INSERTED_PARALLEL                           CTL0_SYNCM(5)           /*!< ADC0 and ADC1 work in inserted parallel mode only */
165 #define ADC_DAUL_REGULAL_PARALLEL                            CTL0_SYNCM(6)           /*!< ADC0 and ADC1 work in regular parallel mode only */
166 #define ADC_DAUL_REGULAL_FOLLOWUP_FAST                       CTL0_SYNCM(7)           /*!< ADC0 and ADC1 work in follow-up fast mode only */
167 #define ADC_DAUL_REGULAL_FOLLOWUP_SLOW                       CTL0_SYNCM(8)           /*!< ADC0 and ADC1 work in follow-up slow mode only */
168 #define ADC_DAUL_INSERTED_TRIGGER_ROTATION                   CTL0_SYNCM(9)           /*!< ADC0 and ADC1 work in trigger rotation mode only */
169 
170 /* adc_ctl1 register value */
171 #define ADC_DATAALIGN_RIGHT              ((uint32_t)0x00000000U)                     /*!< LSB alignment */
172 #define ADC_DATAALIGN_LEFT               ADC_CTL1_DAL                                /*!< MSB alignment */
173 
174 /* continuous mode */
175 #define ADC_CONTINUOUS_MODE              ADC_CTL1_CTN                                /*!< continuous mode */
176 
177 /* external trigger select for regular channel */
178 #define CTL1_ETSRC(regval)               (BITS(17,19) & ((uint32_t)(regval) << 17))  /*!< write value to ADC_CTL1_ETSRC bit field */
179 /* for ADC0 and ADC1 regular channel */
180 #define ADC0_1_EXTTRIG_REGULAR_T0_CH0    CTL1_ETSRC(0)                               /*!< TIMER0 CH0 event select */
181 #define ADC0_1_EXTTRIG_REGULAR_T0_CH1    CTL1_ETSRC(1)                               /*!< TIMER0 CH1 event select */
182 #define ADC0_1_EXTTRIG_REGULAR_T0_CH2    CTL1_ETSRC(2)                               /*!< TIMER0 CH2 event select */
183 #define ADC0_1_EXTTRIG_REGULAR_T1_CH1    CTL1_ETSRC(3)                               /*!< TIMER1 CH1 event select */
184 #define ADC0_1_EXTTRIG_REGULAR_T2_TRGO   CTL1_ETSRC(4)                               /*!< TIMER2 TRGO event select */
185 #define ADC0_1_EXTTRIG_REGULAR_T3_CH3    CTL1_ETSRC(5)                               /*!< TIMER3 CH3 event select */
186 #define ADC0_1_EXTTRIG_REGULAR_EXTI_11   CTL1_ETSRC(6)                               /*!< external interrupt line 11 */
187 #define ADC0_1_EXTTRIG_REGULAR_NONE      CTL1_ETSRC(7)                               /*!< software trigger */
188 
189 /* external trigger mode for inserted channel */
190 #define CTL1_ETSIC(regval)               (BITS(12,14) & ((uint32_t)(regval) << 12))  /*!< write value to ADC_CTL1_ETSIC bit field */
191 /* for ADC0 and ADC1 inserted channel */
192 #define ADC0_1_EXTTRIG_INSERTED_T0_TRGO  CTL1_ETSIC(0)                               /*!< TIMER0 TRGO event select */
193 #define ADC0_1_EXTTRIG_INSERTED_T0_CH3   CTL1_ETSIC(1)                               /*!< TIMER0 CH3 event select */
194 #define ADC0_1_EXTTRIG_INSERTED_T1_TRGO  CTL1_ETSIC(2)                               /*!< TIMER1 TRGO event select */
195 #define ADC0_1_EXTTRIG_INSERTED_T1_CH0   CTL1_ETSIC(3)                               /*!< TIMER1 CH0 event select */
196 #define ADC0_1_EXTTRIG_INSERTED_T2_CH3   CTL1_ETSIC(4)                               /*!< TIMER2 CH3 event select */
197 #define ADC0_1_EXTTRIG_INSERTED_T3_TRGO  CTL1_ETSIC(5)                               /*!< TIMER3 TRGO event select */
198 #define ADC0_1_EXTTRIG_INSERTED_EXTI_15  CTL1_ETSIC(6)                               /*!< external interrupt line 15 */
199 #define ADC0_1_EXTTRIG_INSERTED_NONE     CTL1_ETSIC(7)                               /*!< software trigger */
200 
201 /* adc_samptx register value */
202 #define SAMPTX_SPT(regval)               (BITS(0,2) & ((uint32_t)(regval) << 0))     /*!< write value to ADC_SAMPTX_SPT bit field */
203 #define ADC_SAMPLETIME_1POINT5           SAMPTX_SPT(0)                               /*!< 1.5 sampling cycles */
204 #define ADC_SAMPLETIME_7POINT5           SAMPTX_SPT(1)                               /*!< 7.5 sampling cycles */
205 #define ADC_SAMPLETIME_13POINT5          SAMPTX_SPT(2)                               /*!< 13.5 sampling cycles */
206 #define ADC_SAMPLETIME_28POINT5          SAMPTX_SPT(3)                               /*!< 28.5 sampling cycles */
207 #define ADC_SAMPLETIME_41POINT5          SAMPTX_SPT(4)                               /*!< 41.5 sampling cycles */
208 #define ADC_SAMPLETIME_55POINT5          SAMPTX_SPT(5)                               /*!< 55.5 sampling cycles */
209 #define ADC_SAMPLETIME_71POINT5          SAMPTX_SPT(6)                               /*!< 71.5 sampling cycles */
210 #define ADC_SAMPLETIME_239POINT5         SAMPTX_SPT(7)                               /*!< 239.5 sampling cycles */
211 
212 /* adc_ioffx register value */
213 #define IOFFX_IOFF(regval)               (BITS(0,11) & ((uint32_t)(regval) << 0))    /*!< write value to ADC_IOFFX_IOFF bit field */
214 
215 /* adc_wdht register value */
216 #define WDHT_WDHT(regval)                (BITS(0,11) & ((uint32_t)(regval) << 0))    /*!< write value to ADC_WDHT_WDHT bit field */
217 
218 /* adc_wdlt register value */
219 #define WDLT_WDLT(regval)                (BITS(0,11) & ((uint32_t)(regval) << 0))    /*!< write value to ADC_WDLT_WDLT bit field */
220 
221 /* adc_rsqx register value */
222 #define RSQ0_RL(regval)                  (BITS(20,23) & ((uint32_t)(regval) << 20))  /*!< write value to ADC_RSQ0_RL bit field */
223 
224 /* adc_isq register value */
225 #define ISQ_IL(regval)                   (BITS(20,21) & ((uint32_t)(regval) << 20))  /*!< write value to ADC_ISQ_IL bit field */
226 
227 /* ADC channel group definitions */
228 #define ADC_REGULAR_CHANNEL              ((uint8_t)0x01U)                            /*!< adc regular channel group */
229 #define ADC_INSERTED_CHANNEL             ((uint8_t)0x02U)                            /*!< adc inserted channel group */
230 #define ADC_REGULAR_INSERTED_CHANNEL     ((uint8_t)0x03U)                            /*!< both regular and inserted channel group */
231 
232 #define ADC_CHANNEL_DISCON_DISABLE       ((uint8_t)0x04U)                            /*!< disable discontinuous mode of regular & inserted channel */
233 
234 /* ADC inserted channel definitions */
235 #define ADC_INSERTED_CHANNEL_0           ((uint8_t)0x00U)                            /*!< adc inserted channel 0 */
236 #define ADC_INSERTED_CHANNEL_1           ((uint8_t)0x01U)                            /*!< adc inserted channel 1 */
237 #define ADC_INSERTED_CHANNEL_2           ((uint8_t)0x02U)                            /*!< adc inserted channel 2 */
238 #define ADC_INSERTED_CHANNEL_3           ((uint8_t)0x03U)                            /*!< adc inserted channel 3 */
239 
240 /* ADC channel definitions */
241 #define ADC_CHANNEL_0                    ((uint8_t)0x00U)                            /*!< ADC channel 0 */
242 #define ADC_CHANNEL_1                    ((uint8_t)0x01U)                            /*!< ADC channel 1 */
243 #define ADC_CHANNEL_2                    ((uint8_t)0x02U)                            /*!< ADC channel 2 */
244 #define ADC_CHANNEL_3                    ((uint8_t)0x03U)                            /*!< ADC channel 3 */
245 #define ADC_CHANNEL_4                    ((uint8_t)0x04U)                            /*!< ADC channel 4 */
246 #define ADC_CHANNEL_5                    ((uint8_t)0x05U)                            /*!< ADC channel 5 */
247 #define ADC_CHANNEL_6                    ((uint8_t)0x06U)                            /*!< ADC channel 6 */
248 #define ADC_CHANNEL_7                    ((uint8_t)0x07U)                            /*!< ADC channel 7 */
249 #define ADC_CHANNEL_8                    ((uint8_t)0x08U)                            /*!< ADC channel 8 */
250 #define ADC_CHANNEL_9                    ((uint8_t)0x09U)                            /*!< ADC channel 9 */
251 #define ADC_CHANNEL_10                   ((uint8_t)0x0AU)                            /*!< ADC channel 10 */
252 #define ADC_CHANNEL_11                   ((uint8_t)0x0BU)                            /*!< ADC channel 11 */
253 #define ADC_CHANNEL_12                   ((uint8_t)0x0CU)                            /*!< ADC channel 12 */
254 #define ADC_CHANNEL_13                   ((uint8_t)0x0DU)                            /*!< ADC channel 13 */
255 #define ADC_CHANNEL_14                   ((uint8_t)0x0EU)                            /*!< ADC channel 14 */
256 #define ADC_CHANNEL_15                   ((uint8_t)0x0FU)                            /*!< ADC channel 15 */
257 #define ADC_CHANNEL_16                   ((uint8_t)0x10U)                            /*!< ADC channel 16 */
258 #define ADC_CHANNEL_17                   ((uint8_t)0x11U)                            /*!< ADC channel 17 */
259 
260 /* ADC interrupt */
261 #define ADC_INT_WDE                      ADC_STAT_WDE                                /*!< analog watchdog event interrupt */
262 #define ADC_INT_EOC                      ADC_STAT_EOC                                /*!< end of group conversion interrupt */
263 #define ADC_INT_EOIC                     ADC_STAT_EOIC                               /*!< end of inserted group conversion interrupt */
264 
265 /* ADC interrupt flag */
266 #define ADC_INT_FLAG_WDE                 ADC_STAT_WDE                                /*!< analog watchdog event interrupt flag */
267 #define ADC_INT_FLAG_EOC                 ADC_STAT_EOC                                /*!< end of group conversion interrupt flag */
268 #define ADC_INT_FLAG_EOIC                ADC_STAT_EOIC                               /*!< end of inserted group conversion interrupt flag */
269 
270 /* ADC resolution definitions */
271 #define OVSCR_DRES(regval)               (BITS(12,13) & ((uint32_t)(regval) << 12))
272 #define ADC_RESOLUTION_12B               OVSCR_DRES(0)                               /*!< 12-bit ADC resolution */
273 #define ADC_RESOLUTION_10B               OVSCR_DRES(1)                               /*!< 10-bit ADC resolution */
274 #define ADC_RESOLUTION_8B                OVSCR_DRES(2)                               /*!< 8-bit ADC resolution */
275 #define ADC_RESOLUTION_6B                OVSCR_DRES(3)                               /*!< 6-bit ADC resolution */
276 
277 /* ADC oversampling mode */
278 #define ADC_OVERSAMPLING_ALL_CONVERT     0                                           /*!< all oversampled conversions for a channel are done consecutively after a trigger */
279 #define ADC_OVERSAMPLING_ONE_CONVERT     1                                           /*!< each oversampled conversion for a channel needs a trigger */
280 
281 /* ADC oversampling shift */
282 #define OVSCR_OVSS(regval)               (BITS(5,8) & ((uint32_t)(regval) << 5))
283 #define ADC_OVERSAMPLING_SHIFT_NONE      OVSCR_OVSS(0)                               /*!< no oversampling shift */
284 #define ADC_OVERSAMPLING_SHIFT_1B        OVSCR_OVSS(1)                               /*!< 1-bit oversampling shift */
285 #define ADC_OVERSAMPLING_SHIFT_2B        OVSCR_OVSS(2)                               /*!< 2-bit oversampling shift */
286 #define ADC_OVERSAMPLING_SHIFT_3B        OVSCR_OVSS(3)                               /*!< 3-bit oversampling shift */
287 #define ADC_OVERSAMPLING_SHIFT_4B        OVSCR_OVSS(4)                               /*!< 4-bit oversampling shift */
288 #define ADC_OVERSAMPLING_SHIFT_5B        OVSCR_OVSS(5)                               /*!< 5-bit oversampling shift */
289 #define ADC_OVERSAMPLING_SHIFT_6B        OVSCR_OVSS(6)                               /*!< 6-bit oversampling shift */
290 #define ADC_OVERSAMPLING_SHIFT_7B        OVSCR_OVSS(7)                               /*!< 7-bit oversampling shift */
291 #define ADC_OVERSAMPLING_SHIFT_8B        OVSCR_OVSS(8)                               /*!< 8-bit oversampling shift */
292 
293 /* ADC oversampling ratio */
294 #define OVSCR_OVSR(regval)               (BITS(2,4) & ((uint32_t)(regval) << 2))
295 #define ADC_OVERSAMPLING_RATIO_MUL2      OVSCR_OVSR(0)                               /*!< oversampling ratio X2 */
296 #define ADC_OVERSAMPLING_RATIO_MUL4      OVSCR_OVSR(1)                               /*!< oversampling ratio X4 */
297 #define ADC_OVERSAMPLING_RATIO_MUL8      OVSCR_OVSR(2)                               /*!< oversampling ratio X8 */
298 #define ADC_OVERSAMPLING_RATIO_MUL16     OVSCR_OVSR(3)                               /*!< oversampling ratio X16 */
299 #define ADC_OVERSAMPLING_RATIO_MUL32     OVSCR_OVSR(4)                               /*!< oversampling ratio X32 */
300 #define ADC_OVERSAMPLING_RATIO_MUL64     OVSCR_OVSR(5)                               /*!< oversampling ratio X64 */
301 #define ADC_OVERSAMPLING_RATIO_MUL128    OVSCR_OVSR(6)                               /*!< oversampling ratio X128 */
302 #define ADC_OVERSAMPLING_RATIO_MUL256    OVSCR_OVSR(7)                               /*!< oversampling ratio X256 */
303 
304 /* function declarations */
305 /* initialization config */
306 /* reset ADC */
307 void adc_deinit(uint32_t adc_periph);
308 /* configure the ADC sync mode */
309 void adc_mode_config(uint32_t mode);
310 /* enable or disable ADC special function */
311 void adc_special_function_config(uint32_t adc_periph, uint32_t function, ControlStatus newvalue);
312 /* configure ADC data alignment */
313 void adc_data_alignment_config(uint32_t adc_periph, uint32_t data_alignment);
314 /* enable ADC interface */
315 void adc_enable(uint32_t adc_periph);
316 /* disable ADC interface */
317 void adc_disable(uint32_t adc_periph);
318 /* ADC calibration and reset calibration */
319 void adc_calibration_enable(uint32_t adc_periph);
320 /* enable the temperature sensor and Vrefint channel */
321 void adc_tempsensor_vrefint_enable(void);
322 /* disable the temperature sensor and Vrefint channel */
323 void adc_tempsensor_vrefint_disable(void);
324 
325 /* DMA config */
326 /* enable DMA request */
327 void adc_dma_mode_enable(uint32_t adc_periph);
328 /* disable DMA request */
329 void adc_dma_mode_disable(uint32_t adc_periph);
330 
331 /* regular group and inserted group config */
332 /* configure ADC discontinuous mode */
333 void adc_discontinuous_mode_config(uint32_t adc_periph, uint8_t adc_channel_group, uint8_t length);
334 
335 /* configure the length of regular channel group or inserted channel group */
336 void adc_channel_length_config(uint32_t adc_periph, uint8_t adc_channel_group, uint32_t length);
337 /* configure ADC regular channel */
338 void adc_regular_channel_config(uint32_t adc_periph, uint8_t rank, uint8_t adc_channel, uint32_t sample_time);
339 /* configure ADC inserted channel */
340 void adc_inserted_channel_config(uint32_t adc_periph, uint8_t rank, uint8_t adc_channel, uint32_t sample_time);
341 /* configure ADC inserted channel offset */
342 void adc_inserted_channel_offset_config(uint32_t adc_periph, uint8_t inserted_channel, uint16_t offset);
343 
344 /* configure ADC external trigger source */
345 void adc_external_trigger_source_config(uint32_t adc_periph, uint8_t adc_channel_group, uint32_t external_trigger_source);
346 /* configure ADC external trigger */
347 void adc_external_trigger_config(uint32_t adc_periph, uint8_t adc_channel_group, ControlStatus newvalue);
348 /* enable ADC software trigger */
349 void adc_software_trigger_enable(uint32_t adc_periph, uint8_t adc_channel_group);
350 
351 /* get channel data */
352 /* read ADC regular group data register */
353 uint16_t adc_regular_data_read(uint32_t adc_periph);
354 /* read ADC inserted group data register */
355 uint16_t adc_inserted_data_read(uint32_t adc_periph, uint8_t inserted_channel);
356 /* read the last ADC0 and ADC1 conversion result data in sync mode */
357 uint32_t adc_sync_mode_convert_value_read(void);
358 
359 /* watchdog config */
360 /* configure ADC analog watchdog single channel */
361 void adc_watchdog_single_channel_enable(uint32_t adc_periph, uint8_t adc_channel);
362 /* configure ADC analog watchdog group channel */
363 void adc_watchdog_group_channel_enable(uint32_t adc_periph, uint8_t adc_channel_group);
364 /* disable ADC analog watchdog */
365 void adc_watchdog_disable(uint32_t adc_periph);
366 /* configure ADC analog watchdog threshold */
367 void adc_watchdog_threshold_config(uint32_t adc_periph, uint16_t low_threshold, uint16_t high_threshold);
368 
369 /* interrupt & flag functions */
370 /* get the ADC flag bits */
371 FlagStatus adc_flag_get(uint32_t adc_periph, uint32_t adc_flag);
372 /* clear the ADC flag bits */
373 void adc_flag_clear(uint32_t adc_periph, uint32_t adc_flag);
374 /* get the bit state of ADCx software start conversion */
375 FlagStatus adc_regular_software_startconv_flag_get(uint32_t adc_periph);
376 /* get the bit state of ADCx software inserted channel start conversion */
377 FlagStatus adc_inserted_software_startconv_flag_get(uint32_t adc_periph);
378 /* get the ADC interrupt bits */
379 FlagStatus adc_interrupt_flag_get(uint32_t adc_periph, uint32_t adc_interrupt);
380 /* clear the ADC flag */
381 void adc_interrupt_flag_clear(uint32_t adc_periph, uint32_t adc_interrupt);
382 /* enable ADC interrupt */
383 void adc_interrupt_enable(uint32_t adc_periph, uint32_t adc_interrupt);
384 /* disable ADC interrupt */
385 void adc_interrupt_disable(uint32_t adc_periph, uint32_t adc_interrupt);
386 
387 /* ADC resolution & oversample */
388 /* ADC resolution config */
389 void adc_resolution_config(uint32_t adc_periph, uint32_t resolution);
390 /* ADC oversample mode config */
391 void adc_oversample_mode_config(uint32_t adc_periph, uint8_t mode, uint16_t shift, uint8_t ratio);
392 /* enable ADC oversample mode */
393 void adc_oversample_mode_enable(uint32_t adc_periph);
394 /* disable ADC oversample mode */
395 void adc_oversample_mode_disable(uint32_t adc_periph);
396 
397 #endif /* GD32VF103_ADC_H */
398