1 /********************************** (C) COPYRIGHT *******************************
2 * File Name : ch32f20x_adc.c
3 * Author : WCH
4 * Version : V1.0.0
5 * Date : 2021/08/08
6 * Description : This file provides all the ADC firmware functions.
7 *******************************************************************************/
8 #include "ch32f20x_adc.h"
9 #include "ch32f20x_rcc.h"
10
11 /* ADC DISCNUM mask */
12 #define CTLR1_DISCNUM_Reset ((uint32_t)0xFFFF1FFF)
13
14 /* ADC DISCEN mask */
15 #define CTLR1_DISCEN_Set ((uint32_t)0x00000800)
16 #define CTLR1_DISCEN_Reset ((uint32_t)0xFFFFF7FF)
17
18 /* ADC JAUTO mask */
19 #define CTLR1_JAUTO_Set ((uint32_t)0x00000400)
20 #define CTLR1_JAUTO_Reset ((uint32_t)0xFFFFFBFF)
21
22 /* ADC JDISCEN mask */
23 #define CTLR1_JDISCEN_Set ((uint32_t)0x00001000)
24 #define CTLR1_JDISCEN_Reset ((uint32_t)0xFFFFEFFF)
25
26 /* ADC AWDCH mask */
27 #define CTLR1_AWDCH_Reset ((uint32_t)0xFFFFFFE0)
28
29 /* ADC Analog watchdog enable mode mask */
30 #define CTLR1_AWDMode_Reset ((uint32_t)0xFF3FFDFF)
31
32 /* CTLR1 register Mask */
33 #define CTLR1_CLEAR_Mask ((uint32_t)0xE0F0FEFF)
34
35 /* ADC ADON mask */
36 #define CTLR2_ADON_Set ((uint32_t)0x00000001)
37 #define CTLR2_ADON_Reset ((uint32_t)0xFFFFFFFE)
38
39 /* ADC DMA mask */
40 #define CTLR2_DMA_Set ((uint32_t)0x00000100)
41 #define CTLR2_DMA_Reset ((uint32_t)0xFFFFFEFF)
42
43 /* ADC RSTCAL mask */
44 #define CTLR2_RSTCAL_Set ((uint32_t)0x00000008)
45
46 /* ADC CAL mask */
47 #define CTLR2_CAL_Set ((uint32_t)0x00000004)
48
49 /* ADC SWSTART mask */
50 #define CTLR2_SWSTART_Set ((uint32_t)0x00400000)
51
52 /* ADC EXTTRIG mask */
53 #define CTLR2_EXTTRIG_Set ((uint32_t)0x00100000)
54 #define CTLR2_EXTTRIG_Reset ((uint32_t)0xFFEFFFFF)
55
56 /* ADC Software start mask */
57 #define CTLR2_EXTTRIG_SWSTART_Set ((uint32_t)0x00500000)
58 #define CTLR2_EXTTRIG_SWSTART_Reset ((uint32_t)0xFFAFFFFF)
59
60 /* ADC JEXTSEL mask */
61 #define CTLR2_JEXTSEL_Reset ((uint32_t)0xFFFF8FFF)
62
63 /* ADC JEXTTRIG mask */
64 #define CTLR2_JEXTTRIG_Set ((uint32_t)0x00008000)
65 #define CTLR2_JEXTTRIG_Reset ((uint32_t)0xFFFF7FFF)
66
67 /* ADC JSWSTART mask */
68 #define CTLR2_JSWSTART_Set ((uint32_t)0x00200000)
69
70 /* ADC injected software start mask */
71 #define CTLR2_JEXTTRIG_JSWSTART_Set ((uint32_t)0x00208000)
72 #define CTLR2_JEXTTRIG_JSWSTART_Reset ((uint32_t)0xFFDF7FFF)
73
74 /* ADC TSPD mask */
75 #define CTLR2_TSVREFE_Set ((uint32_t)0x00800000)
76 #define CTLR2_TSVREFE_Reset ((uint32_t)0xFF7FFFFF)
77
78 /* CTLR2 register Mask */
79 #define CTLR2_CLEAR_Mask ((uint32_t)0xFFF1F7FD)
80
81 /* ADC SQx mask */
82 #define RSQR3_SQ_Set ((uint32_t)0x0000001F)
83 #define RSQR2_SQ_Set ((uint32_t)0x0000001F)
84 #define RSQR1_SQ_Set ((uint32_t)0x0000001F)
85
86 /* RSQR1 register Mask */
87 #define RSQR1_CLEAR_Mask ((uint32_t)0xFF0FFFFF)
88
89 /* ADC JSQx mask */
90 #define ISQR_JSQ_Set ((uint32_t)0x0000001F)
91
92 /* ADC JL mask */
93 #define ISQR_JL_Set ((uint32_t)0x00300000)
94 #define ISQR_JL_Reset ((uint32_t)0xFFCFFFFF)
95
96 /* ADC SMPx mask */
97 #define SAMPTR1_SMP_Set ((uint32_t)0x00000007)
98 #define SAMPTR2_SMP_Set ((uint32_t)0x00000007)
99
100 /* ADC IDATARx registers offset */
101 #define IDATAR_Offset ((uint8_t)0x28)
102
103 /* ADC1 RDATAR register base address */
104 #define RDATAR_ADDRESS ((uint32_t)0x4001244C)
105
106 /*******************************************************************************
107 * Function Name : ADC_DeInit
108 * Description : Deinitializes the ADCx peripheral registers to their default
109 * reset values.
110 * Input : ADCx:
111 * where x can be 1 to select the ADC peripheral.
112 * Return : None
113 *******************************************************************************/
ADC_DeInit(ADC_TypeDef * ADCx)114 void ADC_DeInit(ADC_TypeDef* ADCx)
115 {
116 if (ADCx == ADC1)
117 {
118 RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
119 RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);
120 }
121 else if(ADCx == ADC2)
122 {
123 RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2, ENABLE);
124 RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2, DISABLE);
125 }
126 }
127
128 /*******************************************************************************
129 * Function Name : ADC_Init
130 * Description : Initializes the ADCx peripheral according to the specified
131 * parameters in the ADC_InitStruct.
132 * Input : ADCx:
133 * where x can be 1 to select the ADC peripheral.
134 * ADC_InitStruct: pointer to an ADC_InitTypeDef structure that
135 * contains the configuration information for the specified ADC peripheral.
136 * Return : None
137 *******************************************************************************/
ADC_Init(ADC_TypeDef * ADCx,ADC_InitTypeDef * ADC_InitStruct)138 void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
139 {
140 uint32_t tmpreg1 = 0;
141 uint8_t tmpreg2 = 0;
142
143 tmpreg1 = ADCx->CTLR1;
144 tmpreg1 &= CTLR1_CLEAR_Mask;
145 tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_Mode | (uint32_t) ADC_InitStruct->ADC_OutputBuffer |
146 (uint32_t) ADC_InitStruct->ADC_Pga | ((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8));
147 ADCx->CTLR1 = tmpreg1;
148
149 tmpreg1 = ADCx->CTLR2;
150 tmpreg1 &= CTLR2_CLEAR_Mask;
151 tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ExternalTrigConv |
152 ((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1));
153 ADCx->CTLR2 = tmpreg1;
154
155 tmpreg1 = ADCx->RSQR1;
156 tmpreg1 &= RSQR1_CLEAR_Mask;
157 tmpreg2 |= (uint8_t) (ADC_InitStruct->ADC_NbrOfChannel - (uint8_t)1);
158 tmpreg1 |= (uint32_t)tmpreg2 << 20;
159 ADCx->RSQR1 = tmpreg1;
160 }
161
162 /*******************************************************************************
163 * Function Name : ADC_StructInit
164 * Description : Fills each ADC_InitStruct member with its default value.
165 * Input : ADC_InitStruct : pointer to an ADC_InitTypeDef structure that
166 * contains the configuration information for the specified ADC peripheral.
167 * Return : None
168 *******************************************************************************/
ADC_StructInit(ADC_InitTypeDef * ADC_InitStruct)169 void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
170 {
171 ADC_InitStruct->ADC_Mode = ADC_Mode_Independent;
172 ADC_InitStruct->ADC_ScanConvMode = DISABLE;
173 ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;
174 ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
175 ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;
176 ADC_InitStruct->ADC_NbrOfChannel = 1;
177 }
178
179 /*******************************************************************************
180 * Function Name : ADC_Cmd
181 * Description : Enables or disables the specified ADC peripheral.
182 * Input : ADCx:
183 * where x can be 1 to select the ADC peripheral.
184 * NewState: ENABLE or DISABLE.
185 * Return : None
186 *******************************************************************************/
ADC_Cmd(ADC_TypeDef * ADCx,FunctionalState NewState)187 void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
188 {
189 if (NewState != DISABLE)
190 {
191 ADCx->CTLR2 |= CTLR2_ADON_Set;
192 }
193 else
194 {
195 ADCx->CTLR2 &= CTLR2_ADON_Reset;
196 }
197 }
198
199 /*******************************************************************************
200 * Function Name : ADC_DMACmd
201 * Description : Enables or disables the specified ADC DMA request.
202 * Input : ADCx:
203 * where x can be 1 to select the ADC peripheral.
204 * NewState: ENABLE or DISABLE.
205 * Return : None
206 *******************************************************************************/
ADC_DMACmd(ADC_TypeDef * ADCx,FunctionalState NewState)207 void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)
208 {
209 if (NewState != DISABLE)
210 {
211 ADCx->CTLR2 |= CTLR2_DMA_Set;
212 }
213 else
214 {
215 ADCx->CTLR2 &= CTLR2_DMA_Reset;
216 }
217 }
218
219 /*******************************************************************************
220 * Function Name : ADC_ITConfig
221 * Description : Enables or disables the specified ADC interrupts.
222 * Input : ADCx:
223 * where x can be 1 to select the ADC peripheral.
224 * ADC_IT: specifies the ADC interrupt sources to be enabled or disabled.
225 * ADC_IT_EOC: End of conversion interrupt mask.
226 * ADC_IT_AWD: Analog watchdog interrupt mask.
227 * ADC_IT_JEOC: End of injected conversion interrupt mask.
228 * NewState: ENABLE or DISABLE.
229 * Return : None
230 *******************************************************************************/
ADC_ITConfig(ADC_TypeDef * ADCx,uint16_t ADC_IT,FunctionalState NewState)231 void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState)
232 {
233 uint8_t itmask = 0;
234
235 itmask = (uint8_t)ADC_IT;
236
237 if (NewState != DISABLE)
238 {
239 ADCx->CTLR1 |= itmask;
240 }
241 else
242 {
243 ADCx->CTLR1 &= (~(uint32_t)itmask);
244 }
245 }
246
247 /*******************************************************************************
248 * Function Name : ADC_ResetCalibration
249 * Description : Resets the selected ADC calibration registers.
250 * Input : ADCx:
251 * where x can be 1 to select the ADC peripheral.
252 * Return : None
253 *******************************************************************************/
ADC_ResetCalibration(ADC_TypeDef * ADCx)254 void ADC_ResetCalibration(ADC_TypeDef* ADCx)
255 {
256 ADCx->CTLR2 |= CTLR2_RSTCAL_Set;
257 }
258
259 /*******************************************************************************
260 * Function Name : ADC_GetResetCalibrationStatus
261 * Description : Gets the selected ADC reset calibration registers status.
262 * Input : ADCx:
263 * where x can be 1 to select the ADC peripheral.
264 * Return : FlagStatus: SET or RESET.
265 *******************************************************************************/
ADC_GetResetCalibrationStatus(ADC_TypeDef * ADCx)266 FlagStatus ADC_GetResetCalibrationStatus(ADC_TypeDef* ADCx)
267 {
268 FlagStatus bitstatus = RESET;
269
270 if ((ADCx->CTLR2 & CTLR2_RSTCAL_Set) != (uint32_t)RESET)
271 {
272 bitstatus = SET;
273 }
274 else
275 {
276 bitstatus = RESET;
277 }
278
279 return bitstatus;
280 }
281
282 /*******************************************************************************
283 * Function Name : ADC_StartCalibration
284 * Description : Starts the selected ADC calibration process.
285 * Input : ADCx:
286 * where x can be 1 to select the ADC peripheral.
287 * Return : None
288 *******************************************************************************/
ADC_StartCalibration(ADC_TypeDef * ADCx)289 void ADC_StartCalibration(ADC_TypeDef* ADCx)
290 {
291 ADCx->CTLR2 |= CTLR2_CAL_Set;
292 }
293
294 /*******************************************************************************
295 * Function Name : ADC_GetCalibrationStatus
296 * Description : Gets the selected ADC calibration status.
297 * Input : ADCx:
298 * where x can be 1 to select the ADC peripheral.
299 * Return : FlagStatus: SET or RESET.
300 *******************************************************************************/
ADC_GetCalibrationStatus(ADC_TypeDef * ADCx)301 FlagStatus ADC_GetCalibrationStatus(ADC_TypeDef* ADCx)
302 {
303 FlagStatus bitstatus = RESET;
304
305 if ((ADCx->CTLR2 & CTLR2_CAL_Set) != (uint32_t)RESET)
306 {
307 bitstatus = SET;
308 }
309 else
310 {
311 bitstatus = RESET;
312 }
313
314 return bitstatus;
315 }
316
317 /*******************************************************************************
318 * Function Name : ADC_SoftwareStartConvCmd
319 * Description : Enables or disables the selected ADC software start conversion.
320 * Input : ADCx:
321 * where x can be 1 to select the ADC peripheral.
322 * NewState: ENABLE or DISABLE.
323 * Return : None
324 *******************************************************************************/
ADC_SoftwareStartConvCmd(ADC_TypeDef * ADCx,FunctionalState NewState)325 void ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
326 {
327 if (NewState != DISABLE)
328 {
329 ADCx->CTLR2 |= CTLR2_EXTTRIG_SWSTART_Set;
330 }
331 else
332 {
333 ADCx->CTLR2 &= CTLR2_EXTTRIG_SWSTART_Reset;
334 }
335 }
336
337 /*******************************************************************************
338 * Function Name : ADC_GetSoftwareStartConvStatus
339 * Description : Gets the selected ADC Software start conversion Status.
340 * Input : ADCx:
341 * where x can be 1 to select the ADC peripheral.
342 * Return : FlagStatus: SET or RESET.
343 *******************************************************************************/
ADC_GetSoftwareStartConvStatus(ADC_TypeDef * ADCx)344 FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx)
345 {
346 FlagStatus bitstatus = RESET;
347
348 if ((ADCx->CTLR2 & CTLR2_SWSTART_Set) != (uint32_t)RESET)
349 {
350 bitstatus = SET;
351 }
352 else
353 {
354 bitstatus = RESET;
355 }
356
357 return bitstatus;
358 }
359
360 /*******************************************************************************
361 * Function Name : ADC_DiscModeChannelCountConfig
362 * Description : Configures the discontinuous mode for the selected ADC regular
363 * group channel.
364 * Input : ADCx:
365 * where x can be 1 to select the ADC peripheral.
366 * Number: specifies the discontinuous mode regular channel
367 * count value.
368 * This number must be between 1 and 8.
369 * Return : None
370 *******************************************************************************/
ADC_DiscModeChannelCountConfig(ADC_TypeDef * ADCx,uint8_t Number)371 void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, uint8_t Number)
372 {
373 uint32_t tmpreg1 = 0;
374 uint32_t tmpreg2 = 0;
375
376 tmpreg1 = ADCx->CTLR1;
377 tmpreg1 &= CTLR1_DISCNUM_Reset;
378 tmpreg2 = Number - 1;
379 tmpreg1 |= tmpreg2 << 13;
380 ADCx->CTLR1 = tmpreg1;
381 }
382
383 /*******************************************************************************
384 * Function Name : ADC_DiscModeCmd
385 * Description : Enables or disables the discontinuous mode on regular group
386 * channel for the specified ADC.
387 * Input : ADCx:
388 * where x can be 1 to select the ADC peripheral.
389 * NewState: ENABLE or DISABLE.
390 * Return : None
391 *******************************************************************************/
ADC_DiscModeCmd(ADC_TypeDef * ADCx,FunctionalState NewState)392 void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
393 {
394 if (NewState != DISABLE)
395 {
396 ADCx->CTLR1 |= CTLR1_DISCEN_Set;
397 }
398 else
399 {
400 ADCx->CTLR1 &= CTLR1_DISCEN_Reset;
401 }
402 }
403
404 /*******************************************************************************
405 * Function Name : ADC_RegularChannelConfig
406 * Description : Configures for the selected ADC regular channel its corresponding
407 * rank in the sequencer and its sample time.
408 * Input : ADCx:
409 * where x can be 1 to select the ADC peripheral.
410 * ADC_Channel: the ADC channel to configure.
411 * ADC_Channel_0: ADC Channel0 selected.
412 * ADC_Channel_1: ADC Channel1 selected.
413 * ADC_Channel_2: ADC Channel2 selected.
414 * ADC_Channel_3: ADC Channel3 selected.
415 * ADC_Channel_4: ADC Channel4 selected.
416 * ADC_Channel_5: ADC Channel5 selected.
417 * ADC_Channel_6: ADC Channel6 selected.
418 * ADC_Channel_7: ADC Channel7 selected.
419 * ADC_Channel_8: ADC Channel8 selected.
420 * ADC_Channel_9: ADC Channel9 selected.
421 * ADC_Channel_10: ADC Channel10 selected.
422 * ADC_Channel_11: ADC Channel11 selected.
423 * ADC_Channel_12: ADC Channel12 selected.
424 * ADC_Channel_13: ADC Channel13 selected.
425 * ADC_Channel_14: ADC Channel14 selected.
426 * ADC_Channel_15: ADC Channel15 selected.
427 * ADC_Channel_16: ADC Channel16 selected.
428 * ADC_Channel_17: ADC Channel17 selected.
429 * Rank: The rank in the regular group sequencer.
430 * This parameter must be between 1 to 16.
431 * ADC_SampleTime: The sample time value to be set for the selected channel.
432 * ADC_SampleTime_1Cycles5: Sample time equal to 1.5 cycles.
433 * ADC_SampleTime_7Cycles5: Sample time equal to 7.5 cycles.
434 * ADC_SampleTime_13Cycles5: Sample time equal to 13.5 cycles.
435 * ADC_SampleTime_28Cycles5: Sample time equal to 28.5 cycles.
436 * ADC_SampleTime_41Cycles5: Sample time equal to 41.5 cycles.
437 * ADC_SampleTime_55Cycles5: Sample time equal to 55.5 cycles.
438 * ADC_SampleTime_71Cycles5: Sample time equal to 71.5 cycles.
439 * ADC_SampleTime_239Cycles5: Sample time equal to 239.5 cycles.
440 * Return : None
441 *******************************************************************************/
ADC_RegularChannelConfig(ADC_TypeDef * ADCx,uint8_t ADC_Channel,uint8_t Rank,uint8_t ADC_SampleTime)442 void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
443 {
444 uint32_t tmpreg1 = 0, tmpreg2 = 0;
445
446 if (ADC_Channel > ADC_Channel_9)
447 {
448 tmpreg1 = ADCx->SAMPTR1;
449 tmpreg2 = SAMPTR1_SMP_Set << (3 * (ADC_Channel - 10));
450 tmpreg1 &= ~tmpreg2;
451 tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));
452 tmpreg1 |= tmpreg2;
453 ADCx->SAMPTR1 = tmpreg1;
454 }
455 else
456 {
457 tmpreg1 = ADCx->SAMPTR2;
458 tmpreg2 = SAMPTR2_SMP_Set << (3 * ADC_Channel);
459 tmpreg1 &= ~tmpreg2;
460 tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
461 tmpreg1 |= tmpreg2;
462 ADCx->SAMPTR2 = tmpreg1;
463 }
464
465 if (Rank < 7)
466 {
467 tmpreg1 = ADCx->RSQR3;
468 tmpreg2 = RSQR3_SQ_Set << (5 * (Rank - 1));
469 tmpreg1 &= ~tmpreg2;
470 tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1));
471 tmpreg1 |= tmpreg2;
472 ADCx->RSQR3 = tmpreg1;
473 }
474 else if (Rank < 13)
475 {
476 tmpreg1 = ADCx->RSQR2;
477 tmpreg2 = RSQR2_SQ_Set << (5 * (Rank - 7));
478 tmpreg1 &= ~tmpreg2;
479 tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7));
480 tmpreg1 |= tmpreg2;
481 ADCx->RSQR2 = tmpreg1;
482 }
483 else
484 {
485 tmpreg1 = ADCx->RSQR1;
486 tmpreg2 = RSQR1_SQ_Set << (5 * (Rank - 13));
487 tmpreg1 &= ~tmpreg2;
488 tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13));
489 tmpreg1 |= tmpreg2;
490 ADCx->RSQR1 = tmpreg1;
491 }
492 }
493
494 /*******************************************************************************
495 * Function Name : ADC_ExternalTrigConvCmd
496 * Description : Enables or disables the ADCx conversion through external trigger.
497 * Input : ADCx:
498 * where x can be 1 to select the ADC peripheral.
499 * NewState: ENABLE or DISABLE.
500 * Return : None
501 *******************************************************************************/
ADC_ExternalTrigConvCmd(ADC_TypeDef * ADCx,FunctionalState NewState)502 void ADC_ExternalTrigConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
503 {
504 if (NewState != DISABLE)
505 {
506 ADCx->CTLR2 |= CTLR2_EXTTRIG_Set;
507 }
508 else
509 {
510 ADCx->CTLR2 &= CTLR2_EXTTRIG_Reset;
511 }
512 }
513
514 /*******************************************************************************
515 * Function Name : ADC_GetConversionValue
516 * Description : Returns the last ADCx conversion result data for regular channel.
517 * Input : ADCx:
518 * where x can be 1 to select the ADC peripheral.
519 * Return : ADCx->RDATAR:
520 * The Data conversion value.
521 *******************************************************************************/
ADC_GetConversionValue(ADC_TypeDef * ADCx)522 uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
523 {
524 return (uint16_t) ADCx->RDATAR;
525 }
526
527 /*******************************************************************************
528 * Function Name : ADC_GetDualModeConversionValue
529 * Description : Returns the last ADC1 and ADC2 conversion result data in dual mode.
530 * Input : None
531 * Return : RDATAR_ADDRESS:
532 * The Data conversion value.
533 *******************************************************************************/
ADC_GetDualModeConversionValue(void)534 uint32_t ADC_GetDualModeConversionValue(void)
535 {
536 return (*(__IO uint32_t *) RDATAR_ADDRESS);
537 }
538
539 /*******************************************************************************
540 * Function Name : ADC_AutoInjectedConvCmd
541 * Description : Enables or disables the selected ADC automatic injected group
542 * conversion after regular one.
543 * Input : ADCx:
544 * where x can be 1 to select the ADC peripheral.
545 * NewState: ENABLE or DISABLE.
546 * Return : None
547 *******************************************************************************/
ADC_AutoInjectedConvCmd(ADC_TypeDef * ADCx,FunctionalState NewState)548 void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
549 {
550 if (NewState != DISABLE)
551 {
552 ADCx->CTLR1 |= CTLR1_JAUTO_Set;
553 }
554 else
555 {
556 ADCx->CTLR1 &= CTLR1_JAUTO_Reset;
557 }
558 }
559
560 /*******************************************************************************
561 * Function Name : ADC_InjectedDiscModeCmd
562 * Description : Enables or disables the discontinuous mode for injected group
563 * channel for the specified ADC.
564 * Input : ADCx:
565 * where x can be 1 to select the ADC peripheral.
566 * NewState: ENABLE or DISABLE.
567 * Return : None
568 *******************************************************************************/
ADC_InjectedDiscModeCmd(ADC_TypeDef * ADCx,FunctionalState NewState)569 void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
570 {
571 if (NewState != DISABLE)
572 {
573 ADCx->CTLR1 |= CTLR1_JDISCEN_Set;
574 }
575 else
576 {
577 ADCx->CTLR1 &= CTLR1_JDISCEN_Reset;
578 }
579 }
580
581 /*******************************************************************************
582 * Function Name : ADC_ExternalTrigInjectedConvConfig
583 * Description : Configures the ADCx external trigger for injected channels conversion.
584 * Input : ADCx:
585 * where x can be 1 to select the ADC peripheral.
586 * ADC_ExternalTrigInjecConv: specifies the ADC trigger to start
587 * injected conversion.
588 * ADC_ExternalTrigInjecConv_T1_TRGO: Timer1 TRGO event selected.
589 * ADC_ExternalTrigInjecConv_T1_CC4: Timer1 capture compare4 selected.
590 * ADC_ExternalTrigInjecConv_T2_TRGO: Timer2 TRGO event selected.
591 * ADC_ExternalTrigInjecConv_T2_CC1: Timer2 capture compare1 selected.
592 * ADC_ExternalTrigInjecConv_T3_CC4: Timer3 capture compare4 selected.
593 * ADC_ExternalTrigInjecConv_T4_TRGO: Timer4 TRGO event selected.
594 * ADC_ExternalTrigInjecConv_Ext_IT15_TIM8_CC4: External interrupt
595 * line 15 event selected.
596 * ADC_ExternalTrigInjecConv_None: Injected conversion started
597 * by software and not by external trigger.
598 * Return : None
599 *******************************************************************************/
ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef * ADCx,uint32_t ADC_ExternalTrigInjecConv)600 void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv)
601 {
602 uint32_t tmpreg = 0;
603
604 tmpreg = ADCx->CTLR2;
605 tmpreg &= CTLR2_JEXTSEL_Reset;
606 tmpreg |= ADC_ExternalTrigInjecConv;
607 ADCx->CTLR2 = tmpreg;
608 }
609
610 /*******************************************************************************
611 * Function Name : ADC_ExternalTrigInjectedConvCmd
612 * Description : Enables or disables the ADCx injected channels conversion through
613 * external trigger.
614 * Input : ADCx:
615 * where x can be 1 to select the ADC peripheral.
616 * NewState: ENABLE or DISABLE.
617 * Return : None
618 *******************************************************************************/
ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef * ADCx,FunctionalState NewState)619 void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
620 {
621 if (NewState != DISABLE)
622 {
623 ADCx->CTLR2 |= CTLR2_JEXTTRIG_Set;
624 }
625 else
626 {
627 ADCx->CTLR2 &= CTLR2_JEXTTRIG_Reset;
628 }
629 }
630
631 /*******************************************************************************
632 * Function Name : ADC_SoftwareStartInjectedConvCmd
633 * Description : Enables or disables the selected ADC start of the injected
634 * channels conversion.
635 * Input : ADCx:
636 * where x can be 1 to select the ADC peripheral.
637 * NewState: ENABLE or DISABLE.
638 * Return : None
639 *******************************************************************************/
ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef * ADCx,FunctionalState NewState)640 void ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
641 {
642 if (NewState != DISABLE)
643 {
644 ADCx->CTLR2 |= CTLR2_JEXTTRIG_JSWSTART_Set;
645 }
646 else
647 {
648 ADCx->CTLR2 &= CTLR2_JEXTTRIG_JSWSTART_Reset;
649 }
650 }
651
652 /*******************************************************************************
653 * Function Name : ADC_GetSoftwareStartInjectedConvCmdStatus
654 * Description : Gets the selected ADC Software start injected conversion Status.
655 * Input : ADCx:
656 * where x can be 1 to select the ADC peripheral.
657 * Return : FlagStatus: SET or RESET.
658 *******************************************************************************/
ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef * ADCx)659 FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx)
660 {
661 FlagStatus bitstatus = RESET;
662
663 if ((ADCx->CTLR2 & CTLR2_JSWSTART_Set) != (uint32_t)RESET)
664 {
665 bitstatus = SET;
666 }
667 else
668 {
669 bitstatus = RESET;
670 }
671
672 return bitstatus;
673 }
674
675 /*******************************************************************************
676 * Function Name : ADC_InjectedChannelConfig
677 * Description : Configures for the selected ADC injected channel its corresponding
678 * rank in the sequencer and its sample time.
679 * Input : ADCx:
680 * where x can be 1 to select the ADC peripheral.
681 * ADC_Channel: the ADC channel to configure.
682 * ADC_Channel_0: ADC Channel0 selected.
683 * ADC_Channel_1: ADC Channel1 selected.
684 * ADC_Channel_2: ADC Channel2 selected.
685 * ADC_Channel_3: ADC Channel3 selected.
686 * ADC_Channel_4: ADC Channel4 selected.
687 * ADC_Channel_5: ADC Channel5 selected.
688 * ADC_Channel_6: ADC Channel6 selected.
689 * ADC_Channel_7: ADC Channel7 selected.
690 * ADC_Channel_8: ADC Channel8 selected.
691 * ADC_Channel_9: ADC Channel9 selected.
692 * ADC_Channel_10: ADC Channel10 selected.
693 * ADC_Channel_11: ADC Channel11 selected.
694 * ADC_Channel_12: ADC Channel12 selected.
695 * ADC_Channel_13: ADC Channel13 selected.
696 * ADC_Channel_14: ADC Channel14 selected.
697 * ADC_Channel_15: ADC Channel15 selected.
698 * ADC_Channel_16: ADC Channel16 selected.
699 * ADC_Channel_17: ADC Channel17 selected.
700 * Rank: The rank in the injected group sequencer.
701 * This parameter must be between 1 to 4.
702 * ADC_SampleTime: The sample time value to be set for the selected channel.
703 * ADC_SampleTime_1Cycles5: Sample time equal to 1.5 cycles.
704 * ADC_SampleTime_7Cycles5: Sample time equal to 7.5 cycles.
705 * ADC_SampleTime_13Cycles5: Sample time equal to 13.5 cycles.
706 * ADC_SampleTime_28Cycles5: Sample time equal to 28.5 cycles.
707 * ADC_SampleTime_41Cycles5: Sample time equal to 41.5 cycles.
708 * ADC_SampleTime_55Cycles5: Sample time equal to 55.5 cycles.
709 * ADC_SampleTime_71Cycles5: Sample time equal to 71.5 cycles.
710 * ADC_SampleTime_239Cycles5: Sample time equal to 239.5 cycles.
711 * Return : None
712 *******************************************************************************/
ADC_InjectedChannelConfig(ADC_TypeDef * ADCx,uint8_t ADC_Channel,uint8_t Rank,uint8_t ADC_SampleTime)713 void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
714 {
715 uint32_t tmpreg1 = 0, tmpreg2 = 0, tmpreg3 = 0;
716
717 if (ADC_Channel > ADC_Channel_9)
718 {
719 tmpreg1 = ADCx->SAMPTR1;
720 tmpreg2 = SAMPTR1_SMP_Set << (3*(ADC_Channel - 10));
721 tmpreg1 &= ~tmpreg2;
722 tmpreg2 = (uint32_t)ADC_SampleTime << (3*(ADC_Channel - 10));
723 tmpreg1 |= tmpreg2;
724 ADCx->SAMPTR1 = tmpreg1;
725 }
726 else
727 {
728 tmpreg1 = ADCx->SAMPTR2;
729 tmpreg2 = SAMPTR2_SMP_Set << (3 * ADC_Channel);
730 tmpreg1 &= ~tmpreg2;
731 tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
732 tmpreg1 |= tmpreg2;
733 ADCx->SAMPTR2 = tmpreg1;
734 }
735
736 tmpreg1 = ADCx->ISQR;
737 tmpreg3 = (tmpreg1 & ISQR_JL_Set)>> 20;
738 tmpreg2 = ISQR_JSQ_Set << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
739 tmpreg1 &= ~tmpreg2;
740 tmpreg2 = (uint32_t)ADC_Channel << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
741 tmpreg1 |= tmpreg2;
742 ADCx->ISQR = tmpreg1;
743 }
744
745 /*******************************************************************************
746 * Function Name : ADC_InjectedSequencerLengthConfig
747 * Description : Configures the sequencer length for injected channels.
748 * Input : ADCx:
749 * where x can be 1 to select the ADC peripheral.
750 * Length: The sequencer length.
751 * This parameter must be a number between 1 to 4.
752 * Return : None
753 *******************************************************************************/
ADC_InjectedSequencerLengthConfig(ADC_TypeDef * ADCx,uint8_t Length)754 void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length)
755 {
756 uint32_t tmpreg1 = 0;
757 uint32_t tmpreg2 = 0;
758
759 tmpreg1 = ADCx->ISQR;
760 tmpreg1 &= ISQR_JL_Reset;
761 tmpreg2 = Length - 1;
762 tmpreg1 |= tmpreg2 << 20;
763 ADCx->ISQR = tmpreg1;
764 }
765
766 /*******************************************************************************
767 * Function Name : ADC_SetInjectedOffset
768 * Description : Set the injected channels conversion value offset.
769 * Input : ADCx:
770 * where x can be 1 to select the ADC peripheral.
771 * ADC_InjectedChannel: the ADC injected channel to set its offset.
772 * ADC_InjectedChannel_1: Injected Channel1 selected.
773 * ADC_InjectedChannel_2: Injected Channel2 selected.
774 * ADC_InjectedChannel_3: Injected Channel3 selected.
775 * ADC_InjectedChannel_4: Injected Channel4 selected.
776 * Offset: the offset value for the selected ADC injected channel.
777 * This parameter must be a 12bit value.
778 * Return : None
779 *******************************************************************************/
ADC_SetInjectedOffset(ADC_TypeDef * ADCx,uint8_t ADC_InjectedChannel,uint16_t Offset)780 void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset)
781 {
782 __IO uint32_t tmp = 0;
783
784 tmp = (uint32_t)ADCx;
785 tmp += ADC_InjectedChannel;
786
787 *(__IO uint32_t *) tmp = (uint32_t)Offset;
788 }
789
790 /*******************************************************************************
791 * Function Name : ADC_GetInjectedConversionValue
792 * Description : Returns the ADC injected channel conversion result.
793 * Input : ADCx:
794 * where x can be 1 to select the ADC peripheral.
795 * ADC_InjectedChannel: the converted ADC injected channel.
796 * ADC_InjectedChannel_1: Injected Channel1 selected.
797 * ADC_InjectedChannel_2: Injected Channel2 selected.
798 * ADC_InjectedChannel_3: Injected Channel3 selected.
799 * ADC_InjectedChannel_4: Injected Channel4 selected.
800 * Return : tmp: The Data conversion value.
801 *******************************************************************************/
ADC_GetInjectedConversionValue(ADC_TypeDef * ADCx,uint8_t ADC_InjectedChannel)802 uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel)
803 {
804 __IO uint32_t tmp = 0;
805
806 tmp = (uint32_t)ADCx;
807 tmp += ADC_InjectedChannel + IDATAR_Offset;
808
809 return (uint16_t) (*(__IO uint32_t*) tmp);
810 }
811
812 /*******************************************************************************
813 * Function Name : ADC_AnalogWatchdogCmd
814 * Description : Enables or disables the analog watchdog on single/all regular
815 * or injected channels.
816 * Input : ADCx:
817 * where x can be 1 to select the ADC peripheral.
818 * ADC_AnalogWatchdog: the ADC analog watchdog configuration.
819 * ADC_AnalogWatchdog_SingleRegEnable: Analog watchdog on a
820 * single regular channel.
821 * ADC_AnalogWatchdog_SingleInjecEnable: Analog watchdog on a
822 * single injected channel.
823 * ADC_AnalogWatchdog_SingleRegOrInjecEnable: Analog watchdog
824 * on a single regular or injected channel.
825 * ADC_AnalogWatchdog_AllRegEnable: Analog watchdog on all
826 * regular channel.
827 * ADC_AnalogWatchdog_AllInjecEnable: Analog watchdog on all
828 * injected channel.
829 * ADC_AnalogWatchdog_AllRegAllInjecEnable: Analog watchdog on
830 * all regular and injected channels.
831 * ADC_AnalogWatchdog_None: No channel guarded by the analog
832 * watchdog.
833 * Return : None
834 *******************************************************************************/
ADC_AnalogWatchdogCmd(ADC_TypeDef * ADCx,uint32_t ADC_AnalogWatchdog)835 void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog)
836 {
837 uint32_t tmpreg = 0;
838
839 tmpreg = ADCx->CTLR1;
840 tmpreg &= CTLR1_AWDMode_Reset;
841 tmpreg |= ADC_AnalogWatchdog;
842 ADCx->CTLR1 = tmpreg;
843 }
844
845 /*******************************************************************************
846 * Function Name : ADC_AnalogWatchdogThresholdsConfig
847 * Description : Configures the high and low thresholds of the analog watchdog.
848 * Input : ADCx:
849 * where x can be 1 to select the ADC peripheral.
850 * HighThreshold: the ADC analog watchdog High threshold value.
851 * This parameter must be a 12bit value.
852 * LowThreshold: the ADC analog watchdog Low threshold value.
853 * This parameter must be a 12bit value.
854 * Return : None
855 *******************************************************************************/
ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef * ADCx,uint16_t HighThreshold,uint16_t LowThreshold)856 void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,
857 uint16_t LowThreshold)
858 {
859 ADCx->WDHTR = HighThreshold;
860 ADCx->WDLTR = LowThreshold;
861 }
862
863 /*******************************************************************************
864 * Function Name : ADC_AnalogWatchdogSingleChannelConfig
865 * Description : Configures the analog watchdog guarded single channel.
866 * Input : ADCx:
867 * where x can be 1 to select the ADC peripheral.
868 * ADC_Channel: the ADC channel to configure for the analog watchdog.
869 * ADC_Channel_0: ADC Channel0 selected.
870 * ADC_Channel_1: ADC Channel1 selected.
871 * ADC_Channel_2: ADC Channel2 selected.
872 * ADC_Channel_3: ADC Channel3 selected.
873 * ADC_Channel_4: ADC Channel4 selected.
874 * ADC_Channel_5: ADC Channel5 selected.
875 * ADC_Channel_6: ADC Channel6 selected.
876 * ADC_Channel_7: ADC Channel7 selected.
877 * ADC_Channel_8: ADC Channel8 selected.
878 * ADC_Channel_9: ADC Channel9 selected.
879 * ADC_Channel_10: ADC Channel10 selected.
880 * ADC_Channel_11: ADC Channel11 selected.
881 * ADC_Channel_12: ADC Channel12 selected.
882 * ADC_Channel_13: ADC Channel13 selected.
883 * ADC_Channel_14: ADC Channel14 selected.
884 * ADC_Channel_15: ADC Channel15 selected.
885 * ADC_Channel_16: ADC Channel16 selected.
886 * ADC_Channel_17: ADC Channel17 selected.
887 * Return : None
888 *******************************************************************************/
ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef * ADCx,uint8_t ADC_Channel)889 void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel)
890 {
891 uint32_t tmpreg = 0;
892
893 tmpreg = ADCx->CTLR1;
894 tmpreg &= CTLR1_AWDCH_Reset;
895 tmpreg |= ADC_Channel;
896 ADCx->CTLR1 = tmpreg;
897 }
898
899 /*******************************************************************************
900 * Function Name : ADC_TempSensorVrefintCmd
901 * Description : Enables or disables the temperature sensor and Vrefint channel.
902 * Input : NewState: ENABLE or DISABLE.
903 * Return : None
904 *******************************************************************************/
ADC_TempSensorVrefintCmd(FunctionalState NewState)905 void ADC_TempSensorVrefintCmd(FunctionalState NewState)
906 {
907 if (NewState != DISABLE)
908 {
909 ADC1->CTLR2 |= CTLR2_TSVREFE_Set;
910 }
911 else
912 {
913 ADC1->CTLR2 &= CTLR2_TSVREFE_Reset;
914 }
915 }
916
917 /*******************************************************************************
918 * Function Name : ADC_GetFlagStatus
919 * Description : Checks whether the specified ADC flag is set or not.
920 * Input : ADCx:
921 * where x can be 1 to select the ADC peripheral.
922 * ADC_FLAG: specifies the flag to check.
923 * ADC_FLAG_AWD: Analog watchdog flag.
924 * ADC_FLAG_EOC: End of conversion flag.
925 * ADC_FLAG_JEOC: End of injected group conversion flag.
926 * ADC_FLAG_JSTRT: Start of injected group conversion flag.
927 * ADC_FLAG_STRT: Start of regular group conversion flag.
928 * Return : FlagStatus: SET or RESET.
929 *******************************************************************************/
ADC_GetFlagStatus(ADC_TypeDef * ADCx,uint8_t ADC_FLAG)930 FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
931 {
932 FlagStatus bitstatus = RESET;
933
934 if ((ADCx->STATR & ADC_FLAG) != (uint8_t)RESET)
935 {
936 bitstatus = SET;
937 }
938 else
939 {
940 bitstatus = RESET;
941 }
942
943 return bitstatus;
944 }
945
946 /*******************************************************************************
947 * Function Name : ADC_ClearFlag
948 * Description : Clears the ADCx's pending flags.
949 * Input : ADCx:
950 * where x can be 1 to select the ADC peripheral.
951 * ADC_FLAG: specifies the flag to clear.
952 * ADC_FLAG_AWD: Analog watchdog flag.
953 * ADC_FLAG_EOC: End of conversion flag.
954 * ADC_FLAG_JEOC: End of injected group conversion flag.
955 * ADC_FLAG_JSTRT: Start of injected group conversion flag.
956 * ADC_FLAG_STRT: Start of regular group conversion flag.
957 * Return : None
958 *******************************************************************************/
ADC_ClearFlag(ADC_TypeDef * ADCx,uint8_t ADC_FLAG)959 void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
960 {
961 ADCx->STATR = ~(uint32_t)ADC_FLAG;
962 }
963
964 /*******************************************************************************
965 * Function Name : ADC_GetITStatus
966 * Description : Checks whether the specified ADC interrupt has occurred or not.
967 * Input : ADCx:
968 * where x can be 1 to select the ADC peripheral.
969 * ADC_IT: specifies the ADC interrupt source to check.
970 * ADC_IT_EOC: End of conversion interrupt mask.
971 * ADC_IT_AWD: Analog watchdog interrupt mask.
972 * ADC_IT_JEOC: End of injected conversion interrupt mask.
973 * Return : ITStatus: SET or RESET.
974 *******************************************************************************/
ADC_GetITStatus(ADC_TypeDef * ADCx,uint16_t ADC_IT)975 ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT)
976 {
977 ITStatus bitstatus = RESET;
978 uint32_t itmask = 0, enablestatus = 0;
979
980 itmask = ADC_IT >> 8;
981 enablestatus = (ADCx->CTLR1 & (uint8_t)ADC_IT) ;
982
983 if (((ADCx->STATR & itmask) != (uint32_t)RESET) && enablestatus)
984 {
985 bitstatus = SET;
986 }
987 else
988 {
989 bitstatus = RESET;
990 }
991
992 return bitstatus;
993 }
994
995 /*******************************************************************************
996 * Function Name : ADC_ClearITPendingBit
997 * Description : Clears the ADCx's interrupt pending bits.
998 * Input : ADCx:
999 * where x can be 1 to select the ADC peripheral.
1000 * ADC_IT: specifies the ADC interrupt pending bit to clear.
1001 * ADC_IT_EOC: End of conversion interrupt mask.
1002 * ADC_IT_AWD: Analog watchdog interrupt mask.
1003 * ADC_IT_JEOC: End of injected conversion interrupt mask.
1004 * Return : None
1005 *******************************************************************************/
ADC_ClearITPendingBit(ADC_TypeDef * ADCx,uint16_t ADC_IT)1006 void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT)
1007 {
1008 uint8_t itmask = 0;
1009
1010 itmask = (uint8_t)(ADC_IT >> 8);
1011 ADCx->STATR = ~(uint32_t)itmask;
1012 }
1013
1014 /*******************************************************************************
1015 * Function Name : TempSensor_Volt_To_Temper
1016 * Description : Internal Temperature Sensor Voltage to temperature.
1017 * Input : Value: Voltage Value(mv).
1018 * Return : Temper: Temperature Value.
1019 *******************************************************************************/
TempSensor_Volt_To_Temper(s32 Value)1020 s32 TempSensor_Volt_To_Temper(s32 Value)
1021 {
1022 s32 Temper, Refer_Volt, Refer_Temper;
1023 s32 k=43;
1024
1025 Refer_Volt = (s32)((*(u32*)0x1FFFF898)&0x0000FFFF);
1026 Refer_Temper = (s32)(((*(u32*)0x1FFFF898)>>16) & 0x0000FFFF);
1027
1028 Temper = Refer_Temper + ((Value-Refer_Volt)*10+(k>>1))/k;
1029
1030 return Temper;
1031 }
1032
1033 /*******************************************************************************
1034 * Function Name : ADC_BufferCmd
1035 * Description : Enables or disables the ADCx buffer.
1036 * Input : ADCx:
1037 * where x can be 1 to select the ADC peripheral.
1038 * NewState: ENABLE or DISABLE.
1039 * Return : None
1040 *******************************************************************************/
ADC_BufferCmd(ADC_TypeDef * ADCx,FunctionalState NewState)1041 void ADC_BufferCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1042 {
1043 if (NewState != DISABLE)
1044 {
1045 ADCx->CTLR1 |= (1<<26);
1046 }
1047 else
1048 {
1049 ADCx->CTLR1 &= ~(1<<26);
1050 }
1051 }
1052
1053 /*******************************************************************************
1054 * Function Name : Get_CalibrationValue
1055 * Description : Get ADCx Calibration Value.
1056 * Input : ADCx:
1057 * where x can be 1 to select the ADC peripheral.
1058 * Return : CalibrationValue
1059 *******************************************************************************/
Get_CalibrationValue(ADC_TypeDef * ADCx)1060 int16_t Get_CalibrationValue(ADC_TypeDef* ADCx)
1061 {
1062 __IO uint8_t i,j;
1063 uint16_t buf[10];
1064 __IO uint16_t t;
1065
1066 for(i=0; i<10; i++){
1067 ADC_ResetCalibration(ADC1);
1068 while(ADC_GetResetCalibrationStatus(ADC1));
1069 ADC_StartCalibration(ADC1);
1070 while(ADC_GetCalibrationStatus(ADC1));
1071 buf[i] = ADCx->RDATAR;
1072 }
1073
1074 for(i=0; i<9; i++){
1075 for(j=0; j<9-i; j++){
1076 if(buf[j]>buf[j+1]){
1077 t=buf[j];
1078 buf[j]=buf[j+1];
1079 buf[j+1]= t;
1080 }
1081 }
1082 }
1083
1084 t=0;
1085 for(i=0; i<6; i++){
1086 t += buf[i+2];
1087 }
1088
1089 t = (t/6)+((t%6)/3);
1090
1091 return (int16_t)((int16_t)t-2048);
1092 }
1093
1094
1095