1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
|
/*!
* @file apm32f4xx_adc.c
*
* @brief This file contains all the functions for the ADC peripheral
*
* @version V1.0.2
*
* @date 2022-06-23
*
* @attention
*
* Copyright (C) 2021-2022 Geehy Semiconductor
*
* You may not use this file except in compliance with the
* GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
*
* The program is only for reference, which is distributed in the hope
* that it will be usefull and instructional for customers to develop
* their software. Unless required by applicable law or agreed to in
* writing, the program is distributed on an "AS IS" BASIS, WITHOUT
* ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
* See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
* and limitations under the License.
*/
#include "apm32f4xx_adc.h"
#include "apm32f4xx_rcm.h"
/** @addtogroup APM32F4xx_StdPeriphDriver
@{
*/
/** @defgroup ADC_Driver
* @brief ADC driver modules
@{
*/
/** @defgroup ADC_Functions
@{
*/
/*!
* @brief Reset ADC peripheral registers to their default reset values.
*
* @param None
*
* @retval None
*/
void ADC_Reset(void)
{
RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_ADC);
RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_ADC);
}
/*!
* @brief Config the ADC peripheral according to the specified parameters in the adcConfig.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param adcConfig: pointer to a ADC_Config_T structure.
*
* @retval None
*/
void ADC_Config(ADC_T *adc, ADC_Config_T *adcConfig)
{
adc->CTRL1_B.RESSEL = adcConfig->resolution;
adc->CTRL1_B.SCANEN = adcConfig->scanConvMode;
adc->CTRL2_B.REGEXTTRGEN = adcConfig->extTrigEdge;
adc->CTRL2_B.REGEXTTRGSEL = adcConfig->extTrigConv;
adc->CTRL2_B.DALIGNCFG = adcConfig->dataAlign;
adc->CTRL2_B.CONTCEN = adcConfig->continuousConvMode;
adc->REGSEQ1_B.REGSEQLEN = adcConfig->nbrOfChannel - 1;
}
/*!
* @brief Fills each ADC_Config_T member with its default value.
*
* @param adcConfig: pointer to an ADC_Config_T structure.
*
* @retval None
*/
void ADC_ConfigStructInit(ADC_Config_T *adcConfig)
{
adcConfig->resolution = ADC_RESOLUTION_12BIT;
adcConfig->scanConvMode = DISABLE;
adcConfig->continuousConvMode = DISABLE;
adcConfig->extTrigEdge = ADC_EXT_TRIG_EDGE_NONE;
adcConfig->extTrigConv = ADC_EXT_TRIG_CONV_TMR1_CC1;
adcConfig->dataAlign = ADC_DATA_ALIGN_RIGHT;
adcConfig->nbrOfChannel = 1;
}
/*!
* @brief Config the ADCs peripherals according to the specified parameters in
* the adcCommonConfig.
*
* @param adcCommonConfig: pointer to an ADC_CommonConfig_T structure
*
* @retval None
*/
void ADC_CommonConfig(ADC_CommonConfig_T *adcCommonConfig)
{
ADC->CCTRL_B.ADCMSEL = adcCommonConfig->mode;
ADC->CCTRL_B.ADCPRE = adcCommonConfig->prescaler;
ADC->CCTRL_B.DMAMODE = adcCommonConfig->accessMode;
ADC->CCTRL_B.SMPDEL2 = adcCommonConfig->twoSampling;
}
/*!
* @brief Fills each ADC_CommonInitStruct member with its default value.
*
* @param adccommonconfig: pointer to an ADC_CommonInitTypeDef structure.
*
* @retval None
*/
void ADC_CommonConfigStructInit(ADC_CommonConfig_T *adccommonconfig)
{
adccommonconfig->mode = ADC_MODE_INDEPENDENT;
adccommonconfig->prescaler = ADC_PRESCALER_DIV2;
adccommonconfig->accessMode = ADC_ACCESS_MODE_DISABLED;
adccommonconfig->twoSampling = ADC_TWO_SAMPLING_5CYCLES;
}
/*!
* @brief Enables the specified ADC peripheral.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_Enable(ADC_T *adc)
{
adc->CTRL2_B.ADCEN = BIT_SET;
}
/*!
* @brief Disable the specified ADC peripheral.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_Disable(ADC_T *adc)
{
adc->CTRL2_B.ADCEN = BIT_RESET;
}
/*!
* @brief Enable the specified ADC analog watchdog.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param analogWatchdog: The ADC analog watchdog configuration
* This parameter can be one of the following values:
* @arg ADC_ANALOG_WATCHDOG_SINGLE_REG : Analog watchdog on a single regular channel
* @arg ADC_ANALOG_WATCHDOG_SINGLE_INJEC : Analog watchdog on a single injected channel
* @arg ADC_ANALOG_WATCHDOG_SINGLE_REG_INJEC : Analog watchdog on a single regular or injected channel
* @arg ADC_ANALOG_WATCHDOG_ALL_REG : Analog watchdog on all regular channel
* @arg ADC_ANALOG_WATCHDOG_ALL_INJEC : Analog watchdog on all injected channel
* @arg ADC_ANALOG_WATCHDOG_ALL_REG_ALL_INJEC : Analog watchdog on all regular and injected channels
*
* @retval None
*/
void ADC_EnableAnalogWatchdog(ADC_T *adc, ADC_ANALOG_WATCHDOG_T analogWatchdog)
{
adc->CTRL1_B.INJAWDEN = analogWatchdog & 0x01;
adc->CTRL1_B.REGAWDEN = (analogWatchdog >> 1) & 0x01;
adc->CTRL1_B.AWDSGLEN = analogWatchdog >> 4;
}
/*!
* @brief Disable the specified ADC analog watchdog.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_DisableAnalogWatchdog(ADC_T *adc)
{
adc->CTRL1_B.AWDSGLEN = BIT_RESET;
adc->CTRL1_B.INJAWDEN = BIT_RESET;
adc->CTRL1_B.REGAWDEN = BIT_RESET;
}
/*!
* @brief Configures the specified ADC high and low thresholds of the analog watchdog.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param highThreshold: The ADC analog watchdog High threshold value.
* This parameter must be a 12bit value.
*
* @param lowThreshold: The ADC analog watchdog Low threshold value.
* This parameter must be a 12bit value.
* @retval None
*/
void ADC_ConfigAnalogWatchdogThresholds(ADC_T *adc, uint16_t highThreshold, uint16_t lowThreshold)
{
adc->AWDHT = highThreshold;
adc->AWDLT = lowThreshold;
}
/*!
* @brief Configures the specified ADC high and low thresholds of the analog watchdog.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param lowThreshold: The ADC analog watchdog Low threshold value.
* This parameter must be a 12bit value.
*
* @retval None
*/
void ADC_AnalogWatchdogLowThresholds(ADC_T *adc, uint16_t lowThreshold)
{
adc->AWDLT = lowThreshold;
}
/*!
* @brief Configures the specified ADC analog watchdog guarded single channel
*
* @param adc: Select the ADC peripheral
*
* @param channel: Select the ADC channel
* This parameter can be one of the following values:
* @arg ADC_CHANNEL_0: Select ADC Channel 0
* @arg ADC_CHANNEL_1: Select ADC Channel 1
* @arg ADC_CHANNEL_2: Select ADC Channel 2
* @arg ADC_CHANNEL_3: Select ADC Channel 3
* @arg ADC_CHANNEL_4: Select ADC Channel 4
* @arg ADC_CHANNEL_5: Select ADC Channel 5
* @arg ADC_CHANNEL_6: Select ADC Channel 6
* @arg ADC_CHANNEL_7: Select ADC Channel 7
* @arg ADC_CHANNEL_8: Select ADC Channel 8
* @arg ADC_CHANNEL_9: Select ADC Channel 9
* @arg ADC_CHANNEL_10: Select ADC Channel 10
* @arg ADC_CHANNEL_11: Select ADC Channel 11
* @arg ADC_CHANNEL_12: Select ADC Channel 12
* @arg ADC_CHANNEL_13: Select ADC Channel 13
* @arg ADC_CHANNEL_14: Select ADC Channel 14
* @arg ADC_CHANNEL_15: Select ADC Channel 15
* @arg ADC_CHANNEL_16: Select ADC Channel 16
* @arg ADC_CHANNEL_17: Select ADC Channel 17
* @arg ADC_CHANNEL_18: Select ADC Channel 18
*
* @retval None
*
* @note adc can be ADC1, ADC2 or ADC3.
*/
void ADC_ConfigAnalogWatchdogSingleChannel(ADC_T *adc, uint8_t channel)
{
adc->CTRL1_B.AWDCHSEL = channel;
}
/*!
* @brief Enables the temperature sensor and Vrefint channels.
*
* @param None
*
* @retval None
*/
void ADC_EnableTempSensorVrefint(void)
{
ADC->CCTRL_B.TSVREFEN = BIT_SET;
}
/*!
* @brief Disable the temperature sensor and Vrefint channel.
*
* @param None
*
* @retval None
*/
void ADC_DisableTempSensorVrefint(void)
{
ADC->CCTRL_B.TSVREFEN = BIT_RESET;
}
/*!
* @brief Enables the VBAT (Voltage Battery) channel.
*
* @param None
*
* @retval None
*/
void ADC_EnableVbat(void)
{
ADC->CCTRL_B.VBATEN = BIT_SET;
}
/*!
* @brief Disables the VBAT (Voltage Battery) channel.
*
* @param None
*
* @retval None
*/
void ADC_DisableVbat(void)
{
ADC->CCTRL_B.VBATEN = BIT_RESET;
}
/*!
* @brief Configures the specified ADC regular channel.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param channel: the ADC channel to configure.
* This parameter can be one of the following values:
* @arg ADC_CHANNEL_0: ADC channel 0
* @arg ADC_CHANNEL_1: ADC channel 1
* @arg ADC_CHANNEL_2: ADC channel 2
* @arg ADC_CHANNEL_3: ADC channel 3
* @arg ADC_CHANNEL_4: ADC channel 4
* @arg ADC_CHANNEL_5: ADC channel 5
* @arg ADC_CHANNEL_6: ADC channel 6
* @arg ADC_CHANNEL_7: ADC channel 7
* @arg ADC_CHANNEL_8: ADC channel 8
* @arg ADC_CHANNEL_9: ADC channel 9
* @arg ADC_CHANNEL_10: ADC channel 10
* @arg ADC_CHANNEL_11: ADC channel 11
* @arg ADC_CHANNEL_12: ADC channel 12
* @arg ADC_CHANNEL_13: ADC channel 13
* @arg ADC_CHANNEL_14: ADC channel 14
* @arg ADC_CHANNEL_15: ADC channel 15
* @arg ADC_CHANNEL_16: ADC channel 16
* @arg ADC_CHANNEL_17: ADC channel 17
* @arg ADC_CHANNEL_18: ADC Channel 18
*
* @param rank: The rank in the regular group sequencer
* This parameter must be between 1 to 16.
*
* @param sampleTime: the specified ADC channel SampleTime
* The parameter can be one of following values:
* @arg ADC_SAMPLETIME_3CYCLES: Sample time equal to 3 cycles
* @arg ADC_SAMPLETIME_15CYCLES: Sample time equal to 15 cycles
* @arg ADC_SAMPLETIME_28CYCLES: Sample time equal to 28 cycles
* @arg ADC_SAMPLETIME_56CYCLES: Sample time equal to 56 cycles
* @arg ADC_SAMPLETIME_84CYCLES: Sample time equal to 84 cycles
* @arg ADC_SAMPLETIME_112CYCLES: Sample time equal to 112 cycles
* @arg ADC_SAMPLETIME_144CYCLES: Sample time equal to 144 cycles
* @arg ADC_SAMPLETIME_480CYCLES: Sample time equal to 480 cycles
*
* @retval None
*/
void ADC_ConfigRegularChannel(ADC_T *adc, uint8_t channel, uint8_t rank, uint8_t sampleTime)
{
uint32_t temp1 = 0;
uint32_t temp2 = 0;
if (channel > ADC_CHANNEL_9)
{
temp1 = adc->SMPTIM1;
temp2 = (uint32_t)7 << (3 * (channel - 10));
temp1 &= ~temp2;
temp2 = (uint32_t)sampleTime << (3 * (channel - 10));
temp1 |= temp2;
adc->SMPTIM1 = temp1;
}
else
{
temp1 = adc->SMPTIM2;
temp2 = (uint32_t)7 << (3 * channel);
temp1 &= ~temp2;
temp2 = (uint32_t)sampleTime << (3 * channel);
temp1 |= temp2;
adc->SMPTIM2 = temp1;
}
if (rank < 7)
{
temp1 = adc->REGSEQ3;
temp2 = (uint32_t)0x1F << (5 * (rank - 1));
temp1 &= ~temp2;
temp2 = (uint32_t)channel << (5 * (rank - 1));
temp1 |= temp2;
adc->REGSEQ3 = temp1;
}
else if (rank < 13)
{
temp1 = adc->REGSEQ2;
temp2 = (uint32_t)0x1F << (5 * (rank - 7));
temp1 &= ~temp2;
temp2 = (uint32_t)channel << (5 * (rank - 7));
temp1 |= temp2;
adc->REGSEQ2 = temp1;
}
else
{
temp1 = adc->REGSEQ1;
temp2 = (uint32_t)0x1F << (5 * (rank - 13));
temp1 &= ~temp2;
temp2 = (uint32_t)channel << (5 * (rank - 13));
temp1 |= temp2;
adc->REGSEQ1 = temp1;
}
}
/*!
* @brief Enables the selected ADC software start conversion of the regular channels.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_SoftwareStartConv(ADC_T *adc)
{
adc->CTRL2_B.REGCHSC = BIT_SET;
}
/*!
* @brief Reads the selected ADC Software start regular conversion Status.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval The new state of ADC software start conversion (SET or RESET).
*/
uint8_t ADC_ReadSoftwareStartConvStatus(ADC_T *adc)
{
return (uint8_t)adc->CTRL2_B.REGCHSC;
}
/**
* @brief Enables the EOC on each regular channel conversion
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_EnableEOCOnEachChannel(ADC_T *adc)
{
adc->CTRL2_B.EOCSEL = BIT_SET;
}
/**
* @brief Disables the EOC on each regular channel conversion
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_DisableEOCOnEachChannel(ADC_T *adc)
{
adc->CTRL2_B.EOCSEL = BIT_RESET;
}
/*!
* @brief Enables the ADC continuous conversion mode
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_EnableContinuousMode(ADC_T *adc)
{
adc->CTRL2_B.CONTCEN = BIT_SET;
}
/*!
* @brief Disables the ADC continuous conversion mode
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_DisableContinuousMode(ADC_T *adc)
{
adc->CTRL2_B.CONTCEN = BIT_RESET;;
}
/*!
* @brief Configures the discontinuous mode for the selected ADC regular group
* channel.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param number: specifies the discontinuous mode regular channel count value.
* This number must be between 1 and 8.
*
* @retval None
*/
void ADC_ConfigDiscMode(ADC_T *adc, uint8_t number)
{
adc->CTRL1_B.DISCNUMCFG = number - 1;
}
/*!
* @brief Enables the discontinuous mode on regular group channel.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_EnableDiscMode(ADC_T *adc)
{
adc->CTRL1_B.REGDISCEN = BIT_SET;
}
/*!
* @brief Disables the discontinuous mode for injected group channel.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_DisableDiscMode(ADC_T *adc)
{
adc->CTRL1_B.REGDISCEN = BIT_RESET;
}
/*!
* @brief Reads the specified ADC conversion result data.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval The Data conversion value.
*/
uint16_t ADC_ReadConversionValue(ADC_T *adc)
{
return (uint16_t) adc->REGDATA;
}
/*!
* @brief Reads the specified ADC conversion result data in dual mode.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval The Data conversion value.
* - In dual mode:
* Data[15:0] contain the regular data of ADC1.
* Data[31:16] contain the regular data of ADC2.
* - In triple mode:
* Data[15:0] contain alternatively the regular data of ADC1, ADC3 and ADC2.
* Data[31:16] contain alternatively the regular data of ADC2, ADC1 and ADC3.
*/
uint32_t ADC_ReadMultiValue(void)
{
return ADC->CDATA;
}
/*!
* @brief Enables the specified ADC DMA request.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval The Data conversion value.
*/
void ADC_EnableDMA(ADC_T *adc)
{
adc->CTRL2_B.DMAEN = BIT_SET;
}
/*!
* @brief Disables the specified ADC DMA request.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval The Data conversion value.
*/
void ADC_DisableDMA(ADC_T *adc)
{
adc->CTRL2_B.DMAEN = BIT_RESET;
}
/*!
* @brief Enables the ADC DMA request after last transfer (Single-ADC mode)
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval The Data conversion value.
*/
void ADC_EnableDMARequest(ADC_T *adc)
{
adc->CTRL2_B.DMADISSEL = BIT_SET;
}
/*!
* @brief Disables the ADC DMA request after last transfer (Single-ADC mode)
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval The Data conversion value.
*/
void ADC_DisableDMARequest(ADC_T *adc)
{
adc->CTRL2_B.DMADISSEL = BIT_RESET;
}
/*!
* @brief Enables the ADC DMA request after last transfer in multi ADC mode
*
* @param None
*
* @retval None
*/
void ADC_EnableMultiModeDMARequest(void)
{
ADC->CCTRL_B.DMADISSEL = BIT_SET;
}
/*!
* @brief Disables the ADC DMA request after last transfer in multi ADC mode
*
* @param None
*
* @retval None
*/
void ADC_DisableMultiModeDMARequest(void)
{
ADC->CCTRL_B.DMADISSEL = BIT_RESET;
}
/*!
* @brief Configures the specified ADC injected channel.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param channel: the ADC channel to configure.
* This parameter can be one of the following values:
* @arg ADC_CHANNEL_0: ADC channel 0
* @arg ADC_CHANNEL_1: ADC channel 1
* @arg ADC_CHANNEL_2: ADC channel 2
* @arg ADC_CHANNEL_3: ADC channel 3
* @arg ADC_CHANNEL_4: ADC channel 4
* @arg ADC_CHANNEL_5: ADC channel 5
* @arg ADC_CHANNEL_6: ADC channel 6
* @arg ADC_CHANNEL_7: ADC channel 7
* @arg ADC_CHANNEL_8: ADC channel 8
* @arg ADC_CHANNEL_9: ADC channel 9
* @arg ADC_CHANNEL_10: ADC channel 10
* @arg ADC_CHANNEL_11: ADC channel 11
* @arg ADC_CHANNEL_12: ADC channel 12
* @arg ADC_CHANNEL_13: ADC channel 13
* @arg ADC_CHANNEL_14: ADC channel 14
* @arg ADC_CHANNEL_15: ADC channel 15
* @arg ADC_CHANNEL_16: ADC channel 16
* @arg ADC_CHANNEL_17: ADC channel 17
* @arg ADC_CHANNEL_18: ADC Channel 18
*
* @param rank: The rank in the regular group sequencer
* This parameter must be between 1 to 16.
*
* @param sampleTime: the specified ADC channel SampleTime
* The parameter can be one of following values:
* @arg ADC_SAMPLETIME_3CYCLES: Sample time equal to 3 cycles
* @arg ADC_SAMPLETIME_15CYCLES: Sample time equal to 15 cycles
* @arg ADC_SAMPLETIME_28CYCLES: Sample time equal to 28 cycles
* @arg ADC_SAMPLETIME_56CYCLES: Sample time equal to 56 cycles
* @arg ADC_SAMPLETIME_84CYCLES: Sample time equal to 84 cycles
* @arg ADC_SAMPLETIME_112CYCLES: Sample time equal to 112 cycles
* @arg ADC_SAMPLETIME_144CYCLES: Sample time equal to 144 cycles
* @arg ADC_SAMPLETIME_480CYCLES: Sample time equal to 480 cycles
*
* @retval None
*/
void ADC_ConfigInjectedChannel(ADC_T *adc, uint8_t channel, uint8_t rank, uint8_t sampleTime)
{
uint32_t temp1 = 0;
uint32_t temp2 = 0;
if (channel > ADC_CHANNEL_9)
{
temp1 = adc->SMPTIM1;
temp2 = (uint32_t)7 << (3 * (channel - 10));
temp1 &= ~temp2;
temp2 = (uint32_t)sampleTime << (3 * (channel - 10));
temp1 |= temp2;
adc->SMPTIM1 = temp1;
}
else
{
temp1 = adc->SMPTIM2;
temp2 = (uint32_t)7 << (3 * channel);
temp1 &= ~temp2;
temp2 = (uint32_t)sampleTime << (3 * channel);
temp1 |= temp2;
adc->SMPTIM2 = temp1;
}
temp1 = adc->INJSEQ;
temp2 = (uint32_t)0x1F << (5 * ((rank + 3) - (adc->INJSEQ_B.INJSEQLEN + 1)));
temp1 &= ~temp2;
temp2 = (uint32_t)channel << (5 * ((rank + 3) - (adc->INJSEQ_B.INJSEQLEN + 1)));
temp1 |= temp2;
adc->INJSEQ = temp1;
}
/*!
* @brief Configures the sequencer length for injected channels
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param length: The sequencer length.
* This parameter must be a number between 1 to 4.
*
* @retval None
*/
void ADC_ConfigInjectedSequencerLength(ADC_T *adc, uint8_t length)
{
adc->INJSEQ_B.INJSEQLEN = length - 1;
}
/*!
* @brief Configures the specified ADC injected channel conversion value offset.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param channel: Select the ADC injected channel.
* This parameter can be one of the following values:
* @arg ADC_INJEC_CHANNEL_1: select Injected Channel 1
* @arg ADC_INJEC_CHANNEL_2: select Injected Channel 2
* @arg ADC_INJEC_CHANNEL_3: select Injected Channel 3
* @arg ADC_INJEC_CHANNEL_4: select Injected Channel 4
*
* @param offset: The specified ADC injected channel offset.
* This parameter must be a 12bit value.
*
* @retval None
*/
void ADC_ConfigInjectedOffset(ADC_T *adc, ADC_INJEC_CHANNEL_T channel, uint16_t offset)
{
switch ((uint8_t)channel)
{
case 0x01:
adc->INJDOF1_B.INJDOF1 = offset;
break;
case 0x02:
adc->INJDOF2_B.INJDOF2 = offset;
break;
case 0x03:
adc->INJDOF3_B.INJDOF3 = offset;
break;
case 0x04:
adc->INJDOF4_B.INJDOF4 = offset;
break;
default :
break;
}
}
/*!
* @brief Configures the specified ADC external trigger for injected channels conversion
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param extTrigInjecConv: Select the ADC trigger to start injected conversion
* This parameter can be one of the following values:
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR1_CC4: Timer1 capture compare4 selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR1_TRGO: Timer1 TRGO event selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR2_CC1: Timer2 capture compare1 selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR2_TRGO: Timer2 TRGO event selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR3_CC2: Timer3 capture compare2 selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR3_CC4: Timer3 capture compare4 selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR4_CC1: Timer4 capture compare1 selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR4_CC2: Timer4 capture compare2 selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR4_CC3: Timer4 capture compare3 selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR4_TRGO: Timer4 TRGO event selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR5_CC4: Timer5 capture compare4 selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR5_TRGO: Timer5 TRGO event selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR8_CC2: Timer8 capture compare2 selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR8_CC3: Timer8 capture compare3 selected
* @arg ADC_EXT_TRIG_INJEC_CONV_TMR8_CC4: Timer8 capture compare4 selected
* @arg ADC_EXT_TRIG_INJEC_CONV_EINT15: External interrupt line 15 event selected
*
* @retval None
*/
void ADC_ConfigExternalTrigInjectedConv(ADC_T *adc, ADC_EXT_TRIG_INJEC_CONV_T extTrigInjecConv)
{
adc->CTRL2_B.INJGEXTTRGSEL = extTrigInjecConv;
}
/*!
* @brief Configures the ADCx external trigger edge for injected channels conversion.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param extTrigInjecConvEdge: Select the ADC trigger to start injected conversion
* This parameter can be one of the following values:
* @arg ADC_EXT_TRIG_INJEC_EDGE_NONE :external trigger disabled for injected conversion
* @arg ADC_EXT_TRIG_INJEC_EDGE_RISING :detection on rising edge
* @arg ADC_EXT_TRIG_INJEC_EDGE_FALLING :detection on falling edge
* @arg ADC_EXT_TRIG_INJEC_EDGE_RISING_FALLING:detection on both rising and falling edge
*
* @retval None
*/
void ADC_ConfigExternalTrigInjectedConvEdge(ADC_T *adc, ADC_EXT_TRIG_INJEC_EDGE_T extTrigInjecConvEdge)
{
adc->CTRL2_B.INJEXTTRGEN = extTrigInjecConvEdge;
}
/*!
* @brief Enables the selected ADC software start conversion of the injected channels.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_EnableSoftwareStartInjectedConv(ADC_T *adc)
{
adc->CTRL2_B.INJCHSC = BIT_SET;
}
/*!
* @brief Reads the selected ADC Software start injected conversion Status.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
uint8_t ADC_ReadSoftwareStartInjectedConvStatus(ADC_T *adc)
{
return (uint8_t)(adc->CTRL2_B.INJCHSC);
}
/*!
* @brief Enables the selected ADC automatic injected group conversion after regular one.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_EnableAutoInjectedConv(ADC_T *adc)
{
adc->CTRL1_B.INJGACEN = BIT_SET;
}
/*!
* @brief Disables the selected ADC automatic injected group conversion after regular one.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_DisableAutoInjectedConv(ADC_T *adc)
{
adc->CTRL1_B.INJGACEN = BIT_RESET;
}
/*!
* @brief Enable the specified ADC discontinuous mode for injected group.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_EnableInjectedDiscMode(ADC_T *adc)
{
adc->CTRL1_B.INJDISCEN = BIT_SET;
}
/*!
* @brief Disable the specified ADC discontinuous mode for injected group.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @retval None
*/
void ADC_DisableInjectedDiscMode(ADC_T *adc)
{
adc->CTRL1_B.INJDISCEN = BIT_RESET;
}
/*!
* @brief Reads the ADC injected channel conversion result
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param channel: the converted ADC injected channel.
* This parameter can be one of the following values:
* @arg ADC_INJEC_CHANNEL_1: Injected Channel1 selected
* @arg ADC_INJEC_CHANNEL_2: Injected Channel2 selected
* @arg ADC_INJEC_CHANNEL_3: Injected Channel3 selected
* @arg ADC_INJEC_CHANNEL_4: Injected Channel4 selected
*
* @retval The Data conversion value.
*/
uint16_t ADC_ReadInjectedConversionValue(ADC_T *adc, ADC_INJEC_CHANNEL_T channel)
{
switch ((uint8_t)channel)
{
case 0x01:
return (uint16_t)adc->INJDATA1_B.INJDATA;
case 0x02:
return (uint16_t)adc->INJDATA2_B.INJDATA;
case 0x03:
return (uint16_t)adc->INJDATA3_B.INJDATA;
case 0x04:
return (uint16_t)adc->INJDATA4_B.INJDATA;
default :
return 0;
}
}
/*!
* @brief Enable the specified ADC interrupt.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param interrupt: Select the ADC interrupt sources
* This parameter can be any combination of the following values:
* @arg ADC_INT_EOC: End of conversion interrupt mask
* @arg ADC_INT_AWD: Analog watchdog interrupt mask
* @arg ADC_INT_INJEOC: End of injected conversion interrupt mask
* @arg ADC_INT_OVR: Overrun interrupt enable
*
* @retval None
*/
void ADC_EnableInterrupt(ADC_T *adc, uint32_t interrupt)
{
adc->CTRL1 |= interrupt;
}
/*!
* @brief Disables the specified ADC interrupt.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param interrupt: Select the ADC interrupt sources
* This parameter can be any combination of the following values:
* @arg ADC_INT_EOC: End of conversion interrupt mask
* @arg ADC_INT_AWD: Analog watchdog interrupt mask
* @arg ADC_INT_INJEOC: End of injected conversion interrupt mask
* @arg ADC_INT_OVR: Overrun interrupt enable
*
* @retval None
*/
void ADC_DisableInterrupt(ADC_T *adc, uint32_t interrupt)
{
adc->CTRL1 &= (uint32_t)~interrupt;
}
/*!
* @brief Reads the pending ADC flag
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param flag: Select the flag to check
* This parameter can be one of the following values:
* @arg ADC_FLAG_AWD: Analog watchdog flag
* @arg ADC_FLAG_EOC: End of conversion flag
* @arg ADC_FLAG_INJEOC: End of injected group conversion flag
* @arg ADC_FLAG_INJCS: Start of injected group conversion flag
* @arg ADC_FLAG_REGCS: Start of regular group conversion flag
* @arg ADC_FLAG_OVR: Overrun flag
*
* @retval SET or RESET
*/
uint8_t ADC_ReadStatusFlag(ADC_T *adc, ADC_FLAG_T flag)
{
return (adc->STS & flag) ? SET : RESET;
}
/*!
* @brief Clears the pending ADC flag
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param flag: Select the flag to check
* This parameter can be one of the following values:
* @arg ADC_FLAG_AWD: Analog watchdog flag
* @arg ADC_FLAG_EOC: End of conversion flag
* @arg ADC_FLAG_INJEOC: End of injected group conversion flag
* @arg ADC_FLAG_INJCS: Start of injected group conversion flag
* @arg ADC_FLAG_REGCS: Start of regular group conversion flag
* @arg ADC_FLAG_OVR: Overrun flag
*
* @retval None
*/
void ADC_ClearStatusFlag(ADC_T *adc, uint32_t flag)
{
adc->STS = ~(uint32_t)flag;
}
/*!
* @brief Reads the specified ADC Interrupt flag.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param flag: Select the ADC interrupt source.
* This parameter can be one of the following values:
* @arg ADC_INT_FLAG_EOC: End of conversion interrupt flag
* @arg ADC_INT_FLAG_AWD: Analog watchdog interrupt flag
* @arg ADC_INT_FLAG_INJEOC: End of injected conversion interrupt flag
* @arg ADC_INT_FLAG_OVR: Overrun interrupt flag
*
* @retval SET or RESET
*/
uint16_t ADC_ReadIntFlag(ADC_T *adc, ADC_INT_FLAG_T flag)
{
uint32_t itmask = 0;
uint32_t intStatus = 0;
itmask = (uint32_t)1 << (flag >> 8);
intStatus = adc->CTRL1 & itmask;
if (((adc->STS & (uint32_t)(flag & 0x3F)) != (uint32_t)RESET) && intStatus)
{
return SET;
}
else
{
return RESET;
}
}
/*!
* @brief Clears the specified ADC Interrupt pending bits.
*
* @param adc: Select ADCx where x can be 1, 2 or 3.
*
* @param flag: Select the ADC interrupt source.
* This parameter can be any combination of the following values:
* @arg ADC_INT_FLAG_EOC: End of conversion interrupt flag
* @arg ADC_INT_FLAG_AWD: Analog watchdog interrupt flag
* @arg ADC_INT_FLAG_INJEOC: End of injected conversion interrupt flag
* @arg ADC_INT_FLAG_OVR: Overrun interrupt flag
*
* @retval None
*/
void ADC_ClearIntFlag(ADC_T *adc, uint32_t flag)
{
adc->STS = ~(uint32_t)(flag & 0x3F);
}
/**@} end of group ADC_Functions */
/**@} end of group ADC_Driver */
/**@} end of group APM32F4xx_StdPeriphDriver */
|