before merging master
[inav.git] / lib / main / AT32F43x / Drivers / AT32F43x_StdPeriph_Driver / src / at32f435_437_adc.c
blobe0a12df55f86bd988eb002fd52f5efbd399bfc72
1 /**
2 **************************************************************************
3 * @file at32f435_437_adc.c
4 * @version v2.1.0
5 * @date 2022-08-16
6 * @brief contains all the functions for the adc firmware library
7 **************************************************************************
8 * Copyright notice & Disclaimer
10 * The software Board Support Package (BSP) that is made available to
11 * download from Artery official website is the copyrighted work of Artery.
12 * Artery authorizes customers to use, copy, and distribute the BSP
13 * software and its related documentation for the purpose of design and
14 * development in conjunction with Artery microcontrollers. Use of the
15 * software is governed by this copyright notice and the following disclaimer.
17 * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
18 * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
19 * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
20 * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
21 * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 **************************************************************************
27 #include "at32f435_437_conf.h"
29 /** @addtogroup AT32F435_437_periph_driver
30 * @{
33 /** @defgroup ADC
34 * @brief ADC driver modules
35 * @{
38 #ifdef ADC_MODULE_ENABLED
40 /** @defgroup ADC_private_functions
41 * @{
44 /**
45 * @brief deinitialize the adc peripheral registers to their default reset values.
46 * @param none
47 * @retval none
49 void adc_reset(void)
51 crm_periph_reset(CRM_ADC_PERIPH_RESET, TRUE);
52 crm_periph_reset(CRM_ADC_PERIPH_RESET, FALSE);
55 /**
56 * @brief enable or disable the specified adc peripheral.
57 * @param adc_x: select the adc peripheral.
58 * this parameter can be one of the following values:
59 * - ADC1, ADC2, ADC3.
60 * @param new_state: new state of a/d converter.
61 * this parameter can be: TRUE or FALSE.
62 * @retval none
64 void adc_enable(adc_type *adc_x, confirm_state new_state)
66 adc_x->ctrl2_bit.adcen = new_state;
69 /**
70 * @brief adc base default para init.
71 * @param sequence_mode: set the state of adc sequence mode.
72 * this parameter can be:TRUE or FALSE
73 * @param repeat_mode: set the state of adc repeat conversion mode.
74 * this parameter can be:TRUE or FALSE
75 * @param data_align: set the state of adc data alignment.
76 * this parameter can be one of the following values:
77 * - ADC_RIGHT_ALIGNMENT
78 * - ADC_LEFT_ALIGNMENT
79 * @param ordinary_channel_length: configure the adc ordinary channel sequence length.
80 * this parameter can be:
81 * - (0x1~0xf)
82 * @retval none
84 void adc_base_default_para_init(adc_base_config_type *adc_base_struct)
86 adc_base_struct->sequence_mode = FALSE;
87 adc_base_struct->repeat_mode = FALSE;
88 adc_base_struct->data_align = ADC_RIGHT_ALIGNMENT;
89 adc_base_struct->ordinary_channel_length = 1;
92 /**
93 * @brief initialize the adc peripheral according to the specified parameters.
94 * @param adc_x: select the adc peripheral.
95 * this parameter can be one of the following values:
96 * - ADC1, ADC2, ADC3.
97 * @param sequence_mode: set the state of adc sequence mode.
98 * this parameter can be:TRUE or FALSE
99 * @param repeat_mode: set the state of adc repeat conversion mode.
100 * this parameter can be:TRUE or FALSE
101 * @param data_align: set the state of adc data alignment.
102 * this parameter can be one of the following values:
103 * - ADC_RIGHT_ALIGNMENT
104 * - ADC_LEFT_ALIGNMENT
105 * @param ordinary_channel_length: configure the adc ordinary channel sequence length.
106 * this parameter can be:
107 * - (0x1~0xf)
108 * @retval none
110 void adc_base_config(adc_type *adc_x, adc_base_config_type *adc_base_struct)
112 adc_x->ctrl1_bit.sqen = adc_base_struct->sequence_mode;
113 adc_x->ctrl2_bit.rpen = adc_base_struct->repeat_mode;
114 adc_x->ctrl2_bit.dtalign = adc_base_struct->data_align;
115 adc_x->osq1_bit.oclen = adc_base_struct->ordinary_channel_length - 1;
119 * @brief adc common default para init.
120 * @param combine_mode: configure the adc combine_mode mode.
121 * this parameter can be one of the following values:
122 * - ADC_INDEPENDENT_MODE - ADC_ORDINARY_SMLT_PREEMPT_SMLT_ONESLAVE_MODE - ADC_ORDINARY_SMLT_PREEMPT_INTERLTRIG_ONESLAVE_MODE
123 * - ADC_PREEMPT_SMLT_ONLY_ONESLAVE_MODE - ADC_ORDINARY_SMLT_ONLY_ONESLAVE_MODE - ADC_ORDINARY_SHIFT_ONLY_ONESLAVE_MODE
124 * - ADC_PREEMPT_INTERLTRIG_ONLY_ONESLAVE_MODE - ADC_ORDINARY_SMLT_PREEMPT_SMLT_TWOSLAVE_MODE - ADC_ORDINARY_SMLT_PREEMPT_INTERLTRIG_TWOSLAVE_MODE
125 * - ADC_PREEMPT_SMLT_ONLY_TWOSLAVE_MODE - ADC_ORDINARY_SMLT_ONLY_TWOSLAVE_MODE - ADC_ORDINARY_SHIFT_ONLY_TWOSLAVE_MODE
126 * - ADC_PREEMPT_INTERLTRIG_ONLY_TWOSLAVE_MODE
127 * @param div: configure the adc division.
128 * this parameter can be one of the following values:
129 * - ADC_HCLK_DIV_2 - ADC_HCLK_DIV_3 - ADC_HCLK_DIV_4 - ADC_HCLK_DIV_5
130 * - ADC_HCLK_DIV_6 - ADC_HCLK_DIV_7 - ADC_HCLK_DIV_8 - ADC_HCLK_DIV_9
131 * - ADC_HCLK_DIV_10 - ADC_HCLK_DIV_11 - ADC_HCLK_DIV_12 - ADC_HCLK_DIV_13
132 * - ADC_HCLK_DIV_14 - ADC_HCLK_DIV_15 - ADC_HCLK_DIV_16 - ADC_HCLK_DIV_17
133 * @param common_dma_mode: configure the adc common dma mode.
134 * this parameter can be one of the following values:
135 * - ADC_COMMON_DMAMODE_DISABLE
136 * - ADC_COMMON_DMAMODE_1 <mode 1 can be used: all ordinary simultaneous mode,all ordinary shifting mode>
137 * - ADC_COMMON_DMAMODE_2 <mode 2 can be used: one slaver ordinary simultaneous mode,all ordinary shifting mode>
138 * - ADC_COMMON_DMAMODE_3 <mode 3 can be used: one slaver ordinary simultaneous mode in 6/8 bit resolution,ordinary shifting mode in 6/8 bit resolution>
139 * - ADC_COMMON_DMAMODE_4 <mode 4 can be used: two slaver ordinary simultaneous mode in 6/8 bit resolution,two slave ordinary shifting mode in 6/8 bit resolution>
140 * - ADC_COMMON_DMAMODE_5 <mode 5 can be used: all two slaver ordinary simultaneous mode,all two slave ordinary shifting mode>
141 * @param common_dma_request_repeat_state: set the adc common dma request repeat state.
142 * this parameter can be:TRUE or FALSE
143 * @param sampling_interval: configure the ordinary shifting mode adjacent adc sampling interval.
144 * this parameter can be one of the following values:
145 * - ADC_SAMPLING_INTERVAL_5CYCLES - ADC_SAMPLING_INTERVAL_6CYCLES - ADC_SAMPLING_INTERVAL_7CYCLES - ADC_SAMPLING_INTERVAL_8CYCLES
146 * - ADC_SAMPLING_INTERVAL_9CYCLES - ADC_SAMPLING_INTERVAL_10CYCLES - ADC_SAMPLING_INTERVAL_11CYCLES - ADC_SAMPLING_INTERVAL_12CYCLES
147 * - ADC_SAMPLING_INTERVAL_13CYCLES - ADC_SAMPLING_INTERVAL_14CYCLES - ADC_SAMPLING_INTERVAL_15CYCLES - ADC_SAMPLING_INTERVAL_16CYCLES
148 * - ADC_SAMPLING_INTERVAL_17CYCLES - ADC_SAMPLING_INTERVAL_18CYCLES - ADC_SAMPLING_INTERVAL_19CYCLES - ADC_SAMPLING_INTERVAL_20CYCLES
149 * @param tempervintrv_state: set the adc temperature sensor and vintrv state.
150 * this parameter can be:TRUE or FALSE
151 * @param vbat_state: set the adc voltage battery state.
152 * this parameter can be:TRUE or FALSE
153 * @retval none
155 void adc_common_default_para_init(adc_common_config_type *adc_common_struct)
157 adc_common_struct->combine_mode = ADC_INDEPENDENT_MODE;
158 adc_common_struct->div = ADC_HCLK_DIV_2;
159 adc_common_struct->common_dma_mode = ADC_COMMON_DMAMODE_DISABLE;
160 adc_common_struct->common_dma_request_repeat_state = FALSE;
161 adc_common_struct->sampling_interval = ADC_SAMPLING_INTERVAL_5CYCLES;
162 adc_common_struct->tempervintrv_state = FALSE;
163 adc_common_struct->vbat_state = FALSE;
167 * @brief adc common default para init.
168 * @param combine_mode: configure the adc combine_mode mode.
169 * this parameter can be one of the following values:
170 * - ADC_INDEPENDENT_MODE - ADC_ORDINARY_SMLT_PREEMPT_SMLT_ONESLAVE_MODE - ADC_ORDINARY_SMLT_PREEMPT_INTERLTRIG_ONESLAVE_MODE
171 * - ADC_PREEMPT_SMLT_ONLY_ONESLAVE_MODE - ADC_ORDINARY_SMLT_ONLY_ONESLAVE_MODE - ADC_ORDINARY_SHIFT_ONLY_ONESLAVE_MODE
172 * - ADC_PREEMPT_INTERLTRIG_ONLY_ONESLAVE_MODE - ADC_ORDINARY_SMLT_PREEMPT_SMLT_TWOSLAVE_MODE - ADC_ORDINARY_SMLT_PREEMPT_INTERLTRIG_TWOSLAVE_MODE
173 * - ADC_PREEMPT_SMLT_ONLY_TWOSLAVE_MODE - ADC_ORDINARY_SMLT_ONLY_TWOSLAVE_MODE - ADC_ORDINARY_SHIFT_ONLY_TWOSLAVE_MODE
174 * - ADC_PREEMPT_INTERLTRIG_ONLY_TWOSLAVE_MODE
175 * @param div: configure the adc division.
176 * this parameter can be one of the following values:
177 * - ADC_HCLK_DIV_2 - ADC_HCLK_DIV_3 - ADC_HCLK_DIV_4 - ADC_HCLK_DIV_5
178 * - ADC_HCLK_DIV_6 - ADC_HCLK_DIV_7 - ADC_HCLK_DIV_8 - ADC_HCLK_DIV_9
179 * - ADC_HCLK_DIV_10 - ADC_HCLK_DIV_11 - ADC_HCLK_DIV_12 - ADC_HCLK_DIV_13
180 * - ADC_HCLK_DIV_14 - ADC_HCLK_DIV_15 - ADC_HCLK_DIV_16 - ADC_HCLK_DIV_17
181 * @param common_dma_mode: configure the adc common dma mode.
182 * this parameter can be one of the following values:
183 * - ADC_COMMON_DMAMODE_DISABLE
184 * - ADC_COMMON_DMAMODE_1 <mode 1 can be used: all ordinary simultaneous mode,all ordinary shifting mode>
185 * - ADC_COMMON_DMAMODE_2 <mode 2 can be used: one slaver ordinary simultaneous mode,all ordinary shifting mode>
186 * - ADC_COMMON_DMAMODE_3 <mode 3 can be used: one slaver ordinary simultaneous mode in 6/8 bit resolution,ordinary shifting mode in 6/8 bit resolution>
187 * - ADC_COMMON_DMAMODE_4 <mode 4 can be used: two slaver ordinary simultaneous mode in 6/8 bit resolution,two slave ordinary shifting mode in 6/8 bit resolution>
188 * - ADC_COMMON_DMAMODE_5 <mode 5 can be used: all two slaver ordinary simultaneous mode,all two slave ordinary shifting mode>
189 * @param common_dma_request_repeat_state: set the adc common dma request repeat state.
190 * this parameter can be:TRUE or FALSE
191 * @param sampling_interval: configure the ordinary shifting mode adjacent adc sampling interval.
192 * this parameter can be one of the following values:
193 * - ADC_SAMPLING_INTERVAL_5CYCLES - ADC_SAMPLING_INTERVAL_6CYCLES - ADC_SAMPLING_INTERVAL_7CYCLES - ADC_SAMPLING_INTERVAL_8CYCLES
194 * - ADC_SAMPLING_INTERVAL_9CYCLES - ADC_SAMPLING_INTERVAL_10CYCLES - ADC_SAMPLING_INTERVAL_11CYCLES - ADC_SAMPLING_INTERVAL_12CYCLES
195 * - ADC_SAMPLING_INTERVAL_13CYCLES - ADC_SAMPLING_INTERVAL_14CYCLES - ADC_SAMPLING_INTERVAL_15CYCLES - ADC_SAMPLING_INTERVAL_16CYCLES
196 * - ADC_SAMPLING_INTERVAL_17CYCLES - ADC_SAMPLING_INTERVAL_18CYCLES - ADC_SAMPLING_INTERVAL_19CYCLES - ADC_SAMPLING_INTERVAL_20CYCLES
197 * @param tempervintrv_state: set the adc temperature sensor and vintrv state.
198 * this parameter can be:TRUE or FALSE
199 * @param vbat_state: set the adc voltage battery state.
200 * this parameter can be:TRUE or FALSE
201 * @retval none
203 void adc_common_config(adc_common_config_type *adc_common_struct)
205 ADCCOM->cctrl_bit.mssel = adc_common_struct->combine_mode;
206 ADCCOM->cctrl_bit.adcdiv = adc_common_struct->div;
207 if(adc_common_struct->common_dma_mode & 0x04)
209 ADCCOM->cctrl_bit.msdmasel_h = TRUE;
211 else
213 ADCCOM->cctrl_bit.msdmasel_h = FALSE;
215 ADCCOM->cctrl_bit.msdmasel_l = adc_common_struct->common_dma_mode &0x03;
216 ADCCOM->cctrl_bit.msdrcen = adc_common_struct->common_dma_request_repeat_state;
217 ADCCOM->cctrl_bit.asisel = adc_common_struct->sampling_interval;
218 ADCCOM->cctrl_bit.itsrven = adc_common_struct->tempervintrv_state;
219 ADCCOM->cctrl_bit.vbaten = adc_common_struct->vbat_state;
223 * @brief set resolution of the specified adc peripheral.
224 * @param adc_x: select the adc peripheral.
225 * this parameter can be one of the following values:
226 * - ADC1, ADC2, ADC3.
227 * @param resolution: set the conversion resolution.
228 * this parameter can be one of the following values:
229 * - ADC_RESOLUTION_12B
230 * - ADC_RESOLUTION_10B
231 * - ADC_RESOLUTION_8B
232 * - ADC_RESOLUTION_6B
233 * @retval none
235 void adc_resolution_set(adc_type *adc_x, adc_resolution_type resolution)
237 adc_x->ctrl1_bit.crsel = resolution;
241 * @brief enable or disable the adc voltage battery.
242 * @param new_state: new state of the adc voltage battery.
243 * this parameter can be: TRUE or FALSE.
244 * @retval none
246 void adc_voltage_battery_enable(confirm_state new_state)
248 ADCCOM->cctrl_bit.vbaten = new_state;
252 * @brief enable or disable the adc dma transfer.
253 * @param adc_x: select the adc peripheral.
254 * this parameter can be one of the following values:
255 * - ADC1, ADC2, ADC3.
256 * @param new_state: new state of the adc dma transfer.
257 * this parameter can be: TRUE or FALSE.
258 * @retval none
260 void adc_dma_mode_enable(adc_type *adc_x, confirm_state new_state)
262 adc_x->ctrl2_bit.ocdmaen = new_state;
266 * @brief enable or disable the adc dma request repeat.
267 * @param adc_x: select the adc peripheral.
268 * this parameter can be one of the following values:
269 * - ADC1, ADC2, ADC3.
270 * @param new_state: the adc dma request repeat state.
271 * this parameter can be: TRUE or FALSE.
272 * @retval none
274 void adc_dma_request_repeat_enable(adc_type *adc_x, confirm_state new_state)
276 adc_x->ctrl2_bit.ocdrcen = new_state;
280 * @brief enable or disable the specified adc interrupts.
281 * @param adc_x: select the adc peripheral.
282 * this parameter can be one of the following values:
283 * - ADC1, ADC2, ADC3.
284 * @param adc_int: specifies the adc interrupt sources to be enabled or disabled.
285 * this parameter can be one of the following values:
286 * - ADC_OCCE_INT
287 * - ADC_VMOR_INT
288 * - ADC_PCCE_INT
289 * - ADC_OCCO_INT
290 * @param new_state: new state of the specified adc interrupts.
291 * this parameter can be: TRUE or FALSE.
292 * @retval none
294 void adc_interrupt_enable(adc_type *adc_x, uint32_t adc_int, confirm_state new_state)
296 if(new_state == TRUE)
298 adc_x->ctrl1 |= adc_int;
300 else if(new_state == FALSE)
302 adc_x->ctrl1 &= ~adc_int;
307 * @brief set calibration value of the specified adc peripheral.
308 * @param adc_x: select the adc peripheral.
309 * this parameter can be one of the following values:
310 * - ADC1, ADC2, ADC3.
311 * @param adc_calibration_value: calibration value of adc.
312 * this parameter can be:
313 * - (0x00~0x7F)
314 * @retval none
316 void adc_calibration_value_set(adc_type *adc_x, uint8_t adc_calibration_value)
318 adc_x->calval = adc_calibration_value;
322 * @brief initialize calibration register of the specified adc peripheral.
323 * @param adc_x: select the adc peripheral.
324 * this parameter can be one of the following values:
325 * - ADC1, ADC2, ADC3.
326 * @retval none
328 void adc_calibration_init(adc_type *adc_x)
330 adc_x->ctrl2_bit.adcalinit = TRUE;
334 * @brief get calibration register's initialize status of the specified adc peripheral.
335 * @param adc_x: select the adc peripheral.
336 * this parameter can be one of the following values:
337 * - ADC1, ADC2, ADC3.
338 * @retval the new state of reset calibration register status(SET or RESET).
340 flag_status adc_calibration_init_status_get(adc_type *adc_x)
342 if(adc_x->ctrl2_bit.adcalinit)
344 return SET;
346 else
348 return RESET;
353 * @brief start calibration process of the specified adc peripheral.
354 * @param adc_x: select the adc peripheral.
355 * this parameter can be one of the following values:
356 * - ADC1, ADC2, ADC3.
357 * @retval none
359 void adc_calibration_start(adc_type *adc_x)
361 adc_x->ctrl2_bit.adcal = TRUE;
365 * @brief get calibration status of the specified adc peripheral.
366 * @param adc_x: select the adc peripheral.
367 * this parameter can be one of the following values:
368 * - ADC1, ADC2, ADC3.
369 * @retval the new state of calibration status(SET or RESET).
371 flag_status adc_calibration_status_get(adc_type *adc_x)
373 if(adc_x->ctrl2_bit.adcal)
375 return SET;
377 else
379 return RESET;
384 * @brief enable or disable the voltage monitoring on single/all ordinary or preempt channels of the specified adc peripheral.
385 * @param adc_x: select the adc peripheral.
386 * this parameter can be one of the following values:
387 * - ADC1, ADC2, ADC3.
388 * @param adc_voltage_monitoring: choose the adc_voltage_monitoring config.
389 * this parameter can be one of the following values:
390 * - ADC_VMONITOR_SINGLE_ORDINARY
391 * - ADC_VMONITOR_SINGLE_PREEMPT
392 * - ADC_VMONITOR_SINGLE_ORDINARY_PREEMPT
393 * - ADC_VMONITOR_ALL_ORDINARY
394 * - ADC_VMONITOR_ALL_PREEMPT
395 * - ADC_VMONITOR_ALL_ORDINARY_PREEMPT
396 * - ADC_VMONITOR_NONE
397 * @retval none
399 void adc_voltage_monitor_enable(adc_type *adc_x, adc_voltage_monitoring_type adc_voltage_monitoring)
401 adc_x->ctrl1_bit.ocvmen = FALSE;
402 adc_x->ctrl1_bit.pcvmen = FALSE;
403 adc_x->ctrl1_bit.vmsgen = FALSE;
404 adc_x->ctrl1 |= adc_voltage_monitoring;
408 * @brief set voltage monitoring's high and low thresholds value of the specified adc peripheral.
409 * @param adc_x: select the adc peripheral.
410 * this parameter can be one of the following values:
411 * - ADC1, ADC2, ADC3.
412 * @param adc_high_threshold: voltage monitoring's high thresholds value.
413 * this parameter can be:
414 * - (0x000~0xFFF)
415 * @param adc_low_threshold: voltage monitoring's low thresholds value.
416 * this parameter can be:
417 * - (0x000~0xFFF)
418 * @retval none
420 void adc_voltage_monitor_threshold_value_set(adc_type *adc_x, uint16_t adc_high_threshold, uint16_t adc_low_threshold)
422 adc_x->vmhb_bit.vmhb = adc_high_threshold;
423 adc_x->vmlb_bit.vmlb = adc_low_threshold;
427 * @brief select the voltage monitoring's channel of the specified adc peripheral.
428 * @param adc_x: select the adc peripheral.
429 * this parameter can be one of the following values:
430 * - ADC1, ADC2, ADC3.
431 * @param adc_channel: select the channel.
432 * this parameter can be one of the following values:
433 * - ADC_CHANNEL_0 - ADC_CHANNEL_1 - ADC_CHANNEL_2 - ADC_CHANNEL_3
434 * - ADC_CHANNEL_4 - ADC_CHANNEL_5 - ADC_CHANNEL_6 - ADC_CHANNEL_7
435 * - ADC_CHANNEL_8 - ADC_CHANNEL_9 - ADC_CHANNEL_10 - ADC_CHANNEL_11
436 * - ADC_CHANNEL_12 - ADC_CHANNEL_13 - ADC_CHANNEL_14 - ADC_CHANNEL_15
437 * - ADC_CHANNEL_16 - ADC_CHANNEL_17 - ADC_CHANNEL_18
438 * @retval none
440 void adc_voltage_monitor_single_channel_select(adc_type *adc_x, adc_channel_select_type adc_channel)
442 adc_x->ctrl1_bit.vmcsel = adc_channel;
446 * @brief set ordinary channel's corresponding rank in the sequencer and sample time of the specified adc peripheral.
447 * @param adc_x: select the adc peripheral.
448 * this parameter can be one of the following values:
449 * - ADC1, ADC2, ADC3.
450 * @param adc_channel: select the channel.
451 * this parameter can be one of the following values:
452 * - ADC_CHANNEL_0 - ADC_CHANNEL_1 - ADC_CHANNEL_2 - ADC_CHANNEL_3
453 * - ADC_CHANNEL_4 - ADC_CHANNEL_5 - ADC_CHANNEL_6 - ADC_CHANNEL_7
454 * - ADC_CHANNEL_8 - ADC_CHANNEL_9 - ADC_CHANNEL_10 - ADC_CHANNEL_11
455 * - ADC_CHANNEL_12 - ADC_CHANNEL_13 - ADC_CHANNEL_14 - ADC_CHANNEL_15
456 * - ADC_CHANNEL_16 - ADC_CHANNEL_17 - ADC_CHANNEL_18
457 * @param adc_sequence: set rank in the ordinary group sequencer.
458 * this parameter must be:
459 * - between 1 to 16
460 * @param adc_sampletime: set the sampletime of adc channel.
461 * this parameter can be one of the following values:
462 * - ADC_SAMPLETIME_2_5
463 * - ADC_SAMPLETIME_6_5
464 * - ADC_SAMPLETIME_12_5
465 * - ADC_SAMPLETIME_24_5
466 * - ADC_SAMPLETIME_47_5
467 * - ADC_SAMPLETIME_92_5
468 * - ADC_SAMPLETIME_247_5
469 * - ADC_SAMPLETIME_640_5
470 * @retval none
472 void adc_ordinary_channel_set(adc_type *adc_x, adc_channel_select_type adc_channel, uint8_t adc_sequence, adc_sampletime_select_type adc_sampletime)
474 switch(adc_channel)
476 case ADC_CHANNEL_0:
477 adc_x->spt2_bit.cspt0 = adc_sampletime;
478 break;
479 case ADC_CHANNEL_1:
480 adc_x->spt2_bit.cspt1 = adc_sampletime;
481 break;
482 case ADC_CHANNEL_2:
483 adc_x->spt2_bit.cspt2 = adc_sampletime;
484 break;
485 case ADC_CHANNEL_3:
486 adc_x->spt2_bit.cspt3 = adc_sampletime;
487 break;
488 case ADC_CHANNEL_4:
489 adc_x->spt2_bit.cspt4 = adc_sampletime;
490 break;
491 case ADC_CHANNEL_5:
492 adc_x->spt2_bit.cspt5 = adc_sampletime;
493 break;
494 case ADC_CHANNEL_6:
495 adc_x->spt2_bit.cspt6 = adc_sampletime;
496 break;
497 case ADC_CHANNEL_7:
498 adc_x->spt2_bit.cspt7 = adc_sampletime;
499 break;
500 case ADC_CHANNEL_8:
501 adc_x->spt2_bit.cspt8 = adc_sampletime;
502 break;
503 case ADC_CHANNEL_9:
504 adc_x->spt2_bit.cspt9 = adc_sampletime;
505 break;
506 case ADC_CHANNEL_10:
507 adc_x->spt1_bit.cspt10 = adc_sampletime;
508 break;
509 case ADC_CHANNEL_11:
510 adc_x->spt1_bit.cspt11 = adc_sampletime;
511 break;
512 case ADC_CHANNEL_12:
513 adc_x->spt1_bit.cspt12 = adc_sampletime;
514 break;
515 case ADC_CHANNEL_13:
516 adc_x->spt1_bit.cspt13 = adc_sampletime;
517 break;
518 case ADC_CHANNEL_14:
519 adc_x->spt1_bit.cspt14 = adc_sampletime;
520 break;
521 case ADC_CHANNEL_15:
522 adc_x->spt1_bit.cspt15 = adc_sampletime;
523 break;
524 case ADC_CHANNEL_16:
525 adc_x->spt1_bit.cspt16 = adc_sampletime;
526 break;
527 case ADC_CHANNEL_17:
528 adc_x->spt1_bit.cspt17 = adc_sampletime;
529 break;
530 case ADC_CHANNEL_18:
531 adc_x->spt1_bit.cspt18 = adc_sampletime;
532 break;
533 default:
534 break;
536 switch(adc_sequence)
538 case 1:
539 adc_x->osq3_bit.osn1 = adc_channel;
540 break;
541 case 2:
542 adc_x->osq3_bit.osn2 = adc_channel;
543 break;
544 case 3:
545 adc_x->osq3_bit.osn3 = adc_channel;
546 break;
547 case 4:
548 adc_x->osq3_bit.osn4 = adc_channel;
549 break;
550 case 5:
551 adc_x->osq3_bit.osn5 = adc_channel;
552 break;
553 case 6:
554 adc_x->osq3_bit.osn6 = adc_channel;
555 break;
556 case 7:
557 adc_x->osq2_bit.osn7 = adc_channel;
558 break;
559 case 8:
560 adc_x->osq2_bit.osn8 = adc_channel;
561 break;
562 case 9:
563 adc_x->osq2_bit.osn9 = adc_channel;
564 break;
565 case 10:
566 adc_x->osq2_bit.osn10 = adc_channel;
567 break;
568 case 11:
569 adc_x->osq2_bit.osn11 = adc_channel;
570 break;
571 case 12:
572 adc_x->osq2_bit.osn12 = adc_channel;
573 break;
574 case 13:
575 adc_x->osq1_bit.osn13 = adc_channel;
576 break;
577 case 14:
578 adc_x->osq1_bit.osn14 = adc_channel;
579 break;
580 case 15:
581 adc_x->osq1_bit.osn15 = adc_channel;
582 break;
583 case 16:
584 adc_x->osq1_bit.osn16 = adc_channel;
585 break;
586 default:
587 break;
592 * @brief set preempt channel lenghth of the specified adc peripheral.
593 * @param adc_x: select the adc peripheral.
594 * this parameter can be one of the following values:
595 * - ADC1, ADC2, ADC3.
596 * @param adc_channel_lenght: set the adc preempt channel lenghth.
597 * this parameter can be:
598 * - (0x1~0x4)
599 * @retval none
601 void adc_preempt_channel_length_set(adc_type *adc_x, uint8_t adc_channel_lenght)
603 adc_x->psq_bit.pclen = adc_channel_lenght - 1;
607 * @brief configure preempt channel's corresponding rank in the sequencer and sample time of the specified adc peripheral.
608 * @param adc_x: select the adc peripheral.
609 * this parameter can be one of the following values:
610 * - ADC1, ADC2, ADC3.
611 * @param adc_channel: select the channel.
612 * this parameter can be one of the following values:
613 * - ADC_CHANNEL_0 - ADC_CHANNEL_1 - ADC_CHANNEL_2 - ADC_CHANNEL_3
614 * - ADC_CHANNEL_4 - ADC_CHANNEL_5 - ADC_CHANNEL_6 - ADC_CHANNEL_7
615 * - ADC_CHANNEL_8 - ADC_CHANNEL_9 - ADC_CHANNEL_10 - ADC_CHANNEL_11
616 * - ADC_CHANNEL_12 - ADC_CHANNEL_13 - ADC_CHANNEL_14 - ADC_CHANNEL_15
617 * - ADC_CHANNEL_16 - ADC_CHANNEL_17 - ADC_CHANNEL_18
618 * @param adc_sequence: set rank in the preempt group sequencer.
619 * this parameter must be:
620 * - between 1 to 4
621 * @param adc_sampletime: config the sampletime of adc channel.
622 * this parameter can be one of the following values:
623 * - ADC_SAMPLETIME_2_5
624 * - ADC_SAMPLETIME_6_5
625 * - ADC_SAMPLETIME_12_5
626 * - ADC_SAMPLETIME_24_5
627 * - ADC_SAMPLETIME_47_5
628 * - ADC_SAMPLETIME_92_5
629 * - ADC_SAMPLETIME_247_5
630 * - ADC_SAMPLETIME_640_5
631 * @retval none
633 void adc_preempt_channel_set(adc_type *adc_x, adc_channel_select_type adc_channel, uint8_t adc_sequence, adc_sampletime_select_type adc_sampletime)
635 uint16_t sequence_index=0;
636 switch(adc_channel)
638 case ADC_CHANNEL_0:
639 adc_x->spt2_bit.cspt0 = adc_sampletime;
640 break;
641 case ADC_CHANNEL_1:
642 adc_x->spt2_bit.cspt1 = adc_sampletime;
643 break;
644 case ADC_CHANNEL_2:
645 adc_x->spt2_bit.cspt2 = adc_sampletime;
646 break;
647 case ADC_CHANNEL_3:
648 adc_x->spt2_bit.cspt3 = adc_sampletime;
649 break;
650 case ADC_CHANNEL_4:
651 adc_x->spt2_bit.cspt4 = adc_sampletime;
652 break;
653 case ADC_CHANNEL_5:
654 adc_x->spt2_bit.cspt5 = adc_sampletime;
655 break;
656 case ADC_CHANNEL_6:
657 adc_x->spt2_bit.cspt6 = adc_sampletime;
658 break;
659 case ADC_CHANNEL_7:
660 adc_x->spt2_bit.cspt7 = adc_sampletime;
661 break;
662 case ADC_CHANNEL_8:
663 adc_x->spt2_bit.cspt8 = adc_sampletime;
664 break;
665 case ADC_CHANNEL_9:
666 adc_x->spt2_bit.cspt9 = adc_sampletime;
667 break;
668 case ADC_CHANNEL_10:
669 adc_x->spt1_bit.cspt10 = adc_sampletime;
670 break;
671 case ADC_CHANNEL_11:
672 adc_x->spt1_bit.cspt11 = adc_sampletime;
673 break;
674 case ADC_CHANNEL_12:
675 adc_x->spt1_bit.cspt12 = adc_sampletime;
676 break;
677 case ADC_CHANNEL_13:
678 adc_x->spt1_bit.cspt13 = adc_sampletime;
679 break;
680 case ADC_CHANNEL_14:
681 adc_x->spt1_bit.cspt14 = adc_sampletime;
682 break;
683 case ADC_CHANNEL_15:
684 adc_x->spt1_bit.cspt15 = adc_sampletime;
685 break;
686 case ADC_CHANNEL_16:
687 adc_x->spt1_bit.cspt16 = adc_sampletime;
688 break;
689 case ADC_CHANNEL_17:
690 adc_x->spt1_bit.cspt17 = adc_sampletime;
691 break;
692 case ADC_CHANNEL_18:
693 adc_x->spt1_bit.cspt18 = adc_sampletime;
694 break;
695 default:
696 break;
698 sequence_index = adc_sequence + 3 - adc_x->psq_bit.pclen;
699 switch(sequence_index)
701 case 1:
702 adc_x->psq_bit.psn1 = adc_channel;
703 break;
704 case 2:
705 adc_x->psq_bit.psn2 = adc_channel;
706 break;
707 case 3:
708 adc_x->psq_bit.psn3 = adc_channel;
709 break;
710 case 4:
711 adc_x->psq_bit.psn4 = adc_channel;
712 break;
713 default:
714 break;
719 * @brief set the ordinary channel's external trigger edge and
720 * set external trigger event of the specified adc peripheral.
721 * @param adc_x: select the adc peripheral.
722 * this parameter can be one of the following values:
723 * - ADC1, ADC2, ADC3.
724 * @param adc_ordinary_trig: select the external trigger event.
725 * this parameter can be one of the following values:
726 * - ADC_ORDINARY_TRIG_TMR1CH1 - ADC_ORDINARY_TRIG_TMR1CH2 - ADC_ORDINARY_TRIG_TMR1CH3 - ADC_ORDINARY_TRIG_TMR2CH2
727 * - ADC_ORDINARY_TRIG_TMR2CH3 - ADC_ORDINARY_TRIG_TMR2CH4 - ADC_ORDINARY_TRIG_TMR2TRGOUT - ADC_ORDINARY_TRIG_TMR3CH1
728 * - ADC_ORDINARY_TRIG_TMR3TRGOUT - ADC_ORDINARY_TRIG_TMR4CH4 - ADC_ORDINARY_TRIG_TMR5CH1 - ADC_ORDINARY_TRIG_TMR5CH2
729 * - ADC_ORDINARY_TRIG_TMR5CH3 - ADC_ORDINARY_TRIG_TMR8CH1 - ADC_ORDINARY_TRIG_TMR8TRGOUT - ADC_ORDINARY_TRIG_EXINT11
730 * - ADC_ORDINARY_TRIG_TMR20TRGOUT - ADC_ORDINARY_TRIG_TMR20TRGOUT2 - ADC_ORDINARY_TRIG_TMR20CH1 - ADC_ORDINARY_TRIG_TMR20CH2
731 * - ADC_ORDINARY_TRIG_TMR20CH3 - ADC_ORDINARY_TRIG_TMR8TRGOUT2 - ADC_ORDINARY_TRIG_TMR1TRGOUT2 - ADC_ORDINARY_TRIG_TMR4TRGOUT
732 * - ADC_ORDINARY_TRIG_TMR6TRGOUT - ADC_ORDINARY_TRIG_TMR3CH4 - ADC_ORDINARY_TRIG_TMR4CH1 - ADC_ORDINARY_TRIG_TMR1TRGOUT
733 * - ADC_ORDINARY_TRIG_TMR2CH1 - ADC_ORDINARY_TRIG_TMR7TRGOUT
734 * @param adc_ordinary_trig_edge: ordinary channel conversion's external_trigger_edge.
735 * this parameter can be one of the following values:
736 * - ADC_ORDINARY_TRIG_EDGE_NONE
737 * - ADC_ORDINARY_TRIG_EDGE_RISING
738 * - ADC_ORDINARY_TRIG_EDGE_FALLING
739 * - ADC_ORDINARY_TRIG_EDGE_RISING_FALLING
740 * @retval none
742 void adc_ordinary_conversion_trigger_set(adc_type *adc_x, adc_ordinary_trig_select_type adc_ordinary_trig, adc_ordinary_trig_edge_type adc_ordinary_trig_edge)
744 if(adc_ordinary_trig > ADC_ORDINARY_TRIG_EXINT11)
746 adc_x->ctrl2_bit.octesel_h = 1;
747 adc_x->ctrl2_bit.octesel_l = adc_ordinary_trig & 0x0F;
749 else
751 adc_x->ctrl2_bit.octesel_h = 0;
752 adc_x->ctrl2_bit.octesel_l = adc_ordinary_trig & 0x0F;
754 adc_x->ctrl2_bit.ocete = adc_ordinary_trig_edge;
758 * @brief enable or disable the preempt channel's external trigger and
759 * set external trigger event of the specified adc peripheral.
760 * @param adc_x: select the adc peripheral.
761 * this parameter can be one of the following values:
762 * - ADC1, ADC2, ADC3.
763 * @param adc_preempt_trig: select the external trigger event.
764 * this parameter can be one of the following values:
765 * - ADC_PREEMPT_TRIG_TMR1CH4 - ADC_PREEMPT_TRIG_TMR1TRGOUT - ADC_PREEMPT_TRIG_TMR2CH1 - ADC_PREEMPT_TRIG_TMR2TRGOUT
766 * - ADC_PREEMPT_TRIG_TMR3CH2 - ADC_PREEMPT_TRIG_TMR3CH4 - ADC_PREEMPT_TRIG_TMR4CH1 - ADC_PREEMPT_TRIG_TMR4CH2
767 * - ADC_PREEMPT_TRIG_TMR4CH3 - ADC_PREEMPT_TRIG_TMR4TRGOUT - ADC_PREEMPT_TRIG_TMR5CH4 - ADC_PREEMPT_TRIG_TMR5TRGOUT
768 * - ADC_PREEMPT_TRIG_TMR8CH2 - ADC_PREEMPT_TRIG_TMR8CH3 - ADC_PREEMPT_TRIG_TMR8CH4 - ADC_PREEMPT_TRIG_EXINT15
769 * - ADC_PREEMPT_TRIG_TMR20TRGOUT - ADC_PREEMPT_TRIG_TMR20TRGOUT2 - ADC_PREEMPT_TRIG_TMR20CH4 - ADC_PREEMPT_TRIG_TMR1TRGOUT2
770 * - ADC_PREEMPT_TRIG_TMR8TRGOUT - ADC_PREEMPT_TRIG_TMR8TRGOUT2 - ADC_PREEMPT_TRIG_TMR3CH3 - ADC_PREEMPT_TRIG_TMR3TRGOUT
771 * - ADC_PREEMPT_TRIG_TMR3CH1 - ADC_PREEMPT_TRIG_TMR6TRGOUT - ADC_PREEMPT_TRIG_TMR4CH4 - ADC_PREEMPT_TRIG_TMR1CH3
772 * - ADC_PREEMPT_TRIG_TMR20CH2 - ADC_PREEMPT_TRIG_TMR7TRGOUT
773 * @param adc_preempt_trig_edge: preempt channel conversion's external_trigger_edge.
774 * this parameter can be one of the following values:
775 * - ADC_PREEMPT_TRIG_EDGE_NONE
776 * - ADC_PREEMPT_TRIG_EDGE_RISING
777 * - ADC_PREEMPT_TRIG_EDGE_FALLING
778 * - ADC_PREEMPT_TRIG_EDGE_RISING_FALLING
779 * @retval none
781 void adc_preempt_conversion_trigger_set(adc_type *adc_x, adc_preempt_trig_select_type adc_preempt_trig, adc_preempt_trig_edge_type adc_preempt_trig_edge)
783 if(adc_preempt_trig > ADC_PREEMPT_TRIG_EXINT15)
785 adc_x->ctrl2_bit.pctesel_h = 1;
786 adc_x->ctrl2_bit.pctesel_l = adc_preempt_trig & 0x0F;
788 else
790 adc_x->ctrl2_bit.pctesel_h = 0;
791 adc_x->ctrl2_bit.pctesel_l = adc_preempt_trig & 0x0F;
793 adc_x->ctrl2_bit.pcete = adc_preempt_trig_edge;
797 * @brief set preempt channel's conversion value offset of the specified adc peripheral.
798 * @param adc_x: select the adc peripheral.
799 * this parameter can be one of the following values:
800 * - ADC1, ADC2, ADC3.
801 * @param adc_preempt_channel: select the preempt channel.
802 * this parameter can be one of the following values:
803 * - ADC_PREEMPT_CHANNEL_1
804 * - ADC_PREEMPT_CHANNEL_2
805 * - ADC_PREEMPT_CHANNEL_3
806 * - ADC_PREEMPT_CHANNEL_4
807 * @param adc_offset_value: set the adc preempt channel's conversion value offset.
808 * this parameter can be:
809 * - (0x000~0xFFF)
810 * @retval none
812 void adc_preempt_offset_value_set(adc_type *adc_x, adc_preempt_channel_type adc_preempt_channel, uint16_t adc_offset_value)
814 switch(adc_preempt_channel)
816 case ADC_PREEMPT_CHANNEL_1:
817 adc_x->pcdto1_bit.pcdto1 = adc_offset_value;
818 break;
819 case ADC_PREEMPT_CHANNEL_2:
820 adc_x->pcdto2_bit.pcdto2 = adc_offset_value;
821 break;
822 case ADC_PREEMPT_CHANNEL_3:
823 adc_x->pcdto3_bit.pcdto3 = adc_offset_value;
824 break;
825 case ADC_PREEMPT_CHANNEL_4:
826 adc_x->pcdto4_bit.pcdto4 = adc_offset_value;
827 break;
828 default:
829 break;
834 * @brief set partitioned mode channel count of the specified adc peripheral.
835 * @param adc_x: select the adc peripheral.
836 * this parameter can be one of the following values:
837 * - ADC1, ADC2, ADC3.
838 * @param adc_channel_count: configure the adc partitioned mode channel count.
839 * this parameter can be:
840 * - (0x1~0x8)
841 * @retval none
843 void adc_ordinary_part_count_set(adc_type *adc_x, uint8_t adc_channel_count)
846 adc_x->ctrl1_bit.ocpcnt = adc_channel_count - 1;
850 * @brief enable or disable the partitioned mode on ordinary channel of the specified adc peripheral.
851 * @param adc_x: select the adc peripheral.
852 * this parameter can be one of the following values:
853 * - ADC1, ADC2, ADC3.
854 * @param new_state: new state of ordinary channel's partitioned mode.
855 * this parameter can be: TRUE or FALSE.
856 * @retval none
858 void adc_ordinary_part_mode_enable(adc_type *adc_x, confirm_state new_state)
860 adc_x->ctrl1_bit.ocpen = new_state;
864 * @brief enable or disable the partitioned mode on preempt channel of the specified adc peripheral.
865 * @param adc_x: select the adc peripheral.
866 * this parameter can be one of the following values:
867 * - ADC1, ADC2, ADC3.
868 * @param new_state: new state of preempt channel's partitioned mode.
869 * this parameter can be: TRUE or FALSE.
870 * @retval none
872 void adc_preempt_part_mode_enable(adc_type *adc_x, confirm_state new_state)
874 adc_x->ctrl1_bit.pcpen = new_state;
878 * @brief enable or disable automatic preempt group conversion of the specified adc peripheral.
879 * @param adc_x: select the adc peripheral.
880 * this parameter can be one of the following values:
881 * - ADC1, ADC2, ADC3.
882 * @param new_state: new state of automatic preempt group conversion.
883 * this parameter can be: TRUE or FALSE.
884 * @retval none
886 void adc_preempt_auto_mode_enable(adc_type *adc_x, confirm_state new_state)
888 adc_x->ctrl1_bit.pcautoen = new_state;
892 * @brief stop the ongoing conversion of the specified adc peripheral.
893 * @param adc_x: select the adc peripheral.
894 * this parameter can be one of the following values:
895 * - ADC1, ADC2, ADC3.
896 * @retval none
898 void adc_conversion_stop(adc_type *adc_x)
900 adc_x->ctrl2_bit.adabrt = TRUE;
904 * @brief get stop conversion's status of the specified adc peripheral.
905 * @param adc_x: select the adc peripheral.
906 * this parameter can be one of the following values:
907 * - ADC1, ADC2, ADC3.
908 * @retval the new state of stop conversion's status(SET or RESET).
910 flag_status adc_conversion_stop_status_get(adc_type *adc_x)
912 if(adc_x->ctrl2_bit.adabrt)
914 return SET;
916 else
918 return RESET;
923 * @brief enable or disable each ordinary channel conversion set occe flag of the specified adc peripheral.
924 * @param adc_x: select the adc peripheral.
925 * this parameter can be one of the following values:
926 * - ADC1, ADC2, ADC3.
927 * @param new_state: new state of each ordinary channel conversion set occe flag.
928 * this parameter can be: TRUE or FALSE.
929 * @retval none
931 void adc_occe_each_conversion_enable(adc_type *adc_x, confirm_state new_state)
933 adc_x->ctrl2_bit.eocsfen = new_state;
937 * @brief enable or disable ordinary software start conversion of the specified adc peripheral.
938 * @param adc_x: select the adc peripheral.
939 * this parameter can be one of the following values:
940 * - ADC1, ADC2, ADC3.
941 * @param new_state: new state of ordinary software start conversion.
942 * this parameter can be: TRUE or FALSE.
943 * @retval none
945 void adc_ordinary_software_trigger_enable(adc_type *adc_x, confirm_state new_state)
947 adc_x->ctrl2_bit.ocswtrg = new_state;
951 * @brief get ordinary software start conversion status of the specified adc peripheral.
952 * @param adc_x: select the adc peripheral.
953 * this parameter can be one of the following values:
954 * - ADC1, ADC2, ADC3.
955 * @retval the new state of ordinary software start conversion status(SET or RESET).
957 flag_status adc_ordinary_software_trigger_status_get(adc_type *adc_x)
959 if(adc_x->ctrl2_bit.ocswtrg)
961 return SET;
963 else
965 return RESET;
970 * @brief enable or disable preempt software start conversion of the specified adc peripheral.
971 * @param adc_x: select the adc peripheral.
972 * this parameter can be one of the following values:
973 * - ADC1, ADC2, ADC3.
974 * @param new_state: new state of preempt software start conversion.
975 * this parameter can be: TRUE or FALSE.
976 * @retval none
978 void adc_preempt_software_trigger_enable(adc_type *adc_x, confirm_state new_state)
980 adc_x->ctrl2_bit.pcswtrg = new_state;
984 * @brief get preempt software start conversion status of the specified adc peripheral.
985 * @param adc_x: select the adc peripheral.
986 * this parameter can be one of the following values:
987 * - ADC1, ADC2, ADC3.
988 * @retval the new state of preempt software start conversion status(SET or RESET).
990 flag_status adc_preempt_software_trigger_status_get(adc_type *adc_x)
992 if(adc_x->ctrl2_bit.pcswtrg)
994 return SET;
996 else
998 return RESET;
1003 * @brief return the last conversion data for ordinary channel of the specified adc peripheral.
1004 * @param adc_x: select the adc peripheral.
1005 * this parameter can be one of the following values:
1006 * - ADC1, ADC2, ADC3.
1007 * @retval the last conversion data for ordinary channel.
1009 uint16_t adc_ordinary_conversion_data_get(adc_type *adc_x)
1011 return (uint16_t)(adc_x->odt_bit.odt);
1015 * @brief return the last ordinary conversion data of combine adc.
1016 * @retval the last conversion data for ordinary channel.
1018 uint32_t adc_combine_ordinary_conversion_data_get(void)
1020 return (uint32_t)(ADCCOM->codt);
1024 * @brief return the conversion data for selection preempt channel of the specified adc peripheral.
1025 * @param adc_x: select the adc peripheral.
1026 * this parameter can be one of the following values:
1027 * - ADC1, ADC2, ADC3.
1028 * @param adc_preempt_channel: select the preempt channel.
1029 * this parameter can be one of the following values:
1030 * - ADC_PREEMPT_CHANNEL_1
1031 * - ADC_PREEMPT_CHANNEL_2
1032 * - ADC_PREEMPT_CHANNEL_3
1033 * - ADC_PREEMPT_CHANNEL_4
1034 * @retval the conversion data for selection preempt channel.
1036 uint16_t adc_preempt_conversion_data_get(adc_type *adc_x, adc_preempt_channel_type adc_preempt_channel)
1038 uint16_t preempt_conv_data_index = 0;
1039 switch(adc_preempt_channel)
1041 case ADC_PREEMPT_CHANNEL_1:
1042 preempt_conv_data_index = (uint16_t)(adc_x->pdt1_bit.pdt1);
1043 break;
1044 case ADC_PREEMPT_CHANNEL_2:
1045 preempt_conv_data_index = (uint16_t)(adc_x->pdt2_bit.pdt2);
1046 break;
1047 case ADC_PREEMPT_CHANNEL_3:
1048 preempt_conv_data_index = (uint16_t)(adc_x->pdt3_bit.pdt3);
1049 break;
1050 case ADC_PREEMPT_CHANNEL_4:
1051 preempt_conv_data_index = (uint16_t)(adc_x->pdt4_bit.pdt4);
1052 break;
1053 default:
1054 break;
1056 return preempt_conv_data_index;
1060 * @brief get flag of the specified adc peripheral.
1061 * @param adc_x: select the adc peripheral.
1062 * this parameter can be one of the following values:
1063 * - ADC1, ADC2, ADC3.
1064 * @param adc_flag: select the adc flag.
1065 * this parameter can be one of the following values:
1066 * - ADC_VMOR_FLAG
1067 * - ADC_OCCE_FLAG
1068 * - ADC_PCCE_FLAG
1069 * - ADC_PCCS_FLAG(no interrupt associated)
1070 * - ADC_OCCS_FLAG(no interrupt associated)
1071 * - ADC_OCCO_FLAG
1072 * - ADC_RDY_FLAG(no interrupt associated)
1073 * @retval the new state of adc flag status(SET or RESET).
1075 flag_status adc_flag_get(adc_type *adc_x, uint8_t adc_flag)
1077 flag_status status = RESET;
1079 if((adc_x->sts & adc_flag) == RESET)
1081 status = RESET;
1083 else
1085 status = SET;
1087 return status;
1091 * @brief clear flag of the specified adc peripheral.
1092 * @param adc_x: select the adc peripheral.
1093 * this parameter can be one of the following values:
1094 * ADC1, ADC2, ADC3.
1095 * @param adc_flag: select the adc flag.
1096 * this parameter can be any combination of the following values:
1097 * - ADC_VMOR_FLAG
1098 * - ADC_OCCE_FLAG(also can clear by reading the adc_x->odt)
1099 * - ADC_PCCE_FLAG
1100 * - ADC_PCCS_FLAG
1101 * - ADC_OCCS_FLAG
1102 * - ADC_OCCO_FLAG
1103 * - note:"ADC_RDY_FLAG" cannot be choose!rdy bit is readonly bit,it means the adc is ready to accept conversion request
1104 * @retval none
1106 void adc_flag_clear(adc_type *adc_x, uint32_t adc_flag)
1108 adc_x->sts = ~adc_flag;
1112 * @brief enable or disable the ordinary oversampling of the specified adc peripheral.
1113 * @param adc_x: select the adc peripheral.
1114 * this parameter can be one of the following values:
1115 * - ADC1, ADC2, ADC3.
1116 * @param new_state: new state of ordinary oversampling.
1117 * this parameter can be: TRUE or FALSE.
1118 * @retval none
1120 void adc_ordinary_oversample_enable(adc_type *adc_x, confirm_state new_state)
1122 adc_x->ovsp_bit.oosen = new_state;
1126 * @brief enable or disable the preempt oversampling of the specified adc peripheral.
1127 * @param adc_x: select the adc peripheral.
1128 * this parameter can be one of the following values:
1129 * - ADC1, ADC2, ADC3.
1130 * @param new_state: new state of preempt oversampling.
1131 * this parameter can be: TRUE or FALSE.
1132 * @retval none
1134 void adc_preempt_oversample_enable(adc_type *adc_x, confirm_state new_state)
1136 adc_x->ovsp_bit.posen = new_state;
1140 * @brief config the oversampling ratio and shift of the specified adc peripheral.
1141 * @param adc_x: select the adc peripheral.
1142 * this parameter can be one of the following values:
1143 * - ADC1, ADC2, ADC3.
1144 * @param adc_oversample_ratio: set the oversample ratio.
1145 * this parameter can be one of the following values:
1146 * - ADC_OVERSAMPLE_RATIO_2
1147 * - ADC_OVERSAMPLE_RATIO_4
1148 * - ADC_OVERSAMPLE_RATIO_8
1149 * - ADC_OVERSAMPLE_RATIO_16
1150 * - ADC_OVERSAMPLE_RATIO_32
1151 * - ADC_OVERSAMPLE_RATIO_64
1152 * - ADC_OVERSAMPLE_RATIO_128
1153 * - ADC_OVERSAMPLE_RATIO_256
1154 * @param adc_oversample_shift: set the oversample shift.
1155 * this parameter can be one of the following values:
1156 * - ADC_OVERSAMPLE_SHIFT_0
1157 * - ADC_OVERSAMPLE_SHIFT_1
1158 * - ADC_OVERSAMPLE_SHIFT_2
1159 * - ADC_OVERSAMPLE_SHIFT_3
1160 * - ADC_OVERSAMPLE_SHIFT_4
1161 * - ADC_OVERSAMPLE_SHIFT_5
1162 * - ADC_OVERSAMPLE_SHIFT_6
1163 * - ADC_OVERSAMPLE_SHIFT_7
1164 * - ADC_OVERSAMPLE_SHIFT_8
1165 * @retval none
1167 void adc_oversample_ratio_shift_set(adc_type *adc_x, adc_oversample_ratio_type adc_oversample_ratio, adc_oversample_shift_type adc_oversample_shift)
1169 adc_x->ovsp_bit.osrsel = adc_oversample_ratio;
1170 adc_x->ovsp_bit.osssel = adc_oversample_shift;
1174 * @brief enable or disable the ordinary oversampling trigger mode of the specified adc peripheral.
1175 * @param adc_x: select the adc peripheral.
1176 * this parameter can be one of the following values:
1177 * - ADC1, ADC2, ADC3.
1178 * @param new_state: new state of ordinary oversampling trigger mode.
1179 * this parameter can be: TRUE or FALSE.
1180 * @retval none
1182 void adc_ordinary_oversample_trig_enable(adc_type *adc_x, confirm_state new_state)
1184 adc_x->ovsp_bit.oostren = new_state;
1188 * @brief set ordinary oversample restart mode of the specified adc peripheral.
1189 * @param adc_x: select the adc peripheral.
1190 * this parameter can be one of the following values:
1191 * - ADC1, ADC2, ADC3.
1192 * @param adc_or_oversample_restart: ordinary oversample restart mode.
1193 * this parameter can be one of the following values:
1194 * - ADC_OVERSAMPLE_CONTINUE
1195 * - ADC_OVERSAMPLE_RESTART
1196 * @retval none
1198 void adc_ordinary_oversample_restart_set(adc_type *adc_x, adc_ordinary_oversample_restart_type adc_ordinary_oversample_restart)
1200 adc_x->ovsp_bit.oosrsel = adc_ordinary_oversample_restart;
1204 * @}
1207 #endif
1210 * @}
1214 * @}