summaryrefslogtreecommitdiff
path: root/bsp/hc32f4a0/Libraries/HC32F4A0_StdPeriph_Driver/src/hc32f4a0_adc.c
blob: 07b74cb870bc4f7f7fb746eabaa2abc2685c77c7 (plain)
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
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
/**
 *******************************************************************************
 * @file  hc32f4a0_adc.c
 * @brief This file provides firmware functions to manage the Analog-to-Digital
 *        Converter(ADC).
 @verbatim
   Change Logs:
   Date             Author          Notes
   2020-06-12       Wuze            First version
   2020-07-02       Wuze            1. API ADC_SH_ChannelCmd() refine.
                                    2. Some other optimizations.
   2020-08-10       Wuze            ADC_SeqClrStatus(): ADC_SEQ_FLAG_NESTED can be cleared.
   2020-12-15       Wuze            ADC_ComTriggerCmd() refine.
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by HDSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 *
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32f4a0_adc.h"
#include "hc32f4a0_utility.h"

/**
 * @addtogroup HC32F4A0_DDL_Driver
 * @{
 */

/**
 * @defgroup DDL_ADC ADC
 * @brief Analog-to-Digital Converter Driver Library
 * @{
 */

#if (DDL_ADC_ENABLE == DDL_ON)

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
/**
 * @defgroup ADC_Local_Macros ADC Local Macros
 * @{
 */

/**
 * @defgroup ADC_Unit_Base_Address ADC Unit Base Address
 * @{
 */
#define ADC1_BASE                           (0x40040000UL)
#define ADC2_BASE                           (0x40040400UL)
#define ADC3_BASE                           (0x40040800UL)
/**
 * @}
 */

/**
 * @defgroup ADC_Configuration_Bit_Mask ADC Configuration Bit Mask
 * @{
 */
#define ADC_TRIG_SRC_MSK                    (ADC_TRGSR_TRGSELA)
#define ADC_AWD_COMB_MODE_MSK               (ADC_AWDCR_AWDCM)
#define ADC_PGA_CMD_MSK                     (ADC_PGACR1_PGACTL)
#define ADC_PGA_GAIN_MSK                    (ADC_PGACR1_PGAGAIN)
#define ADC_SYNC_CFG_MSK                    (ADC_SYNCCR_SYNCMD | ADC_SYNCCR_SYNCDLY)
#define ADC_CH_MAP_PIN_MSK                  (ADC_CHMUXR0_CH00MUX)
#define ADC_SH_CFG_MSK                      (ADC_SHCR_SHSST)
#define ADC_SH_CH_MSK                       (ADC_SHCR_SHSEL)
#define ADC_COM_TRIG_MSK                    (ADC_COM_TRIG1 | ADC_COM_TRIG2)
#define ADC_TRIG_EVENT_MSK                  (AOS_ADC_1_ITRGSELR_TRGSEL)
/**
 * @}
 */

/**
 * @defgroup ADC_PGA_Command ADC PGA Command
 * @{
 */
#define ADC_PGA_DISABLE                     (0x0U)
#define ADC_PGA_ENABLE                      (0xEU)
/**
 * @}
 */

/**
 * @defgroup ADC_Unit_Number ADC Unit Number
 * @{
 */
#define __ADC_BASE                          (ADC1_BASE)
#define __ADC_UNIT_SIZE                     (0x400UL)
#define ADC_IDX(__ADCx__)                   ((uint8_t)(((uint32_t)&(__ADCx__)->STR - __ADC_BASE) / __ADC_UNIT_SIZE))
/**
 * @}
 */

/**
 * @defgroup ADC_Register_Bit_Band ADC Register Bit Band
 * @{
 */
#define __BIT_BAND_BASE                     (0x42000000UL)
#define __PERIP_BASE                        (0x40000000UL)
#define __REG_OFS(regAddr)                  ((regAddr) - __PERIP_BASE)
#define __BIT_BAND_ADDR(regAddr, pos)       ((__REG_OFS(regAddr) << 5U) + ((uint32_t)(pos) << 2U) + __BIT_BAND_BASE)
#define BIT_BAND(regAddr, pos)              (*(__IO uint32_t *)__BIT_BAND_ADDR((regAddr), (pos)))
/**
 * @}
 */

/**
 * @defgroup ADC_Sequence_Configuration_Bit_Field_Offset ADC Sequence Configuration Bit Field Offset
 * @{
 */
#define ADC_OFS_TRGSR(__seq__)              ((uint32_t)(__seq__) * ADC_TRGSR_TRGSELB_POS)
#define ADC_OFS_AWDCR(__num__)              ((uint32_t)(__num__) * ADC_AWDCR_AWD1EN)
/**
 * @}
 */

/**
 * @defgroup ADC_Register_Configuration_Value ADC Register Configuration Value
 * @{
 */
#define ADC_TRGSR_VAL(__seq__, __val__)     ((uint32_t)(__val__) << ADC_OFS_TRGSR(__seq__))
#define ADC_AWDCR_VAL(__num__, __val__)     ((uint32_t)(__val__) << ADC_OFS_AWDCR(__num__))
/**
 * @}
 */

/**
 * @defgroup ADC_AWD_DR ADC AWD DR
 * @{
 */
#define ADC_AWDx_DR(__base__, __awdNum__)   (*(__IO uint32_t *)((uint32_t)(__base__) + ((uint32_t)(__awdNum__) << 3U)))
/**
 * @}
 */

/**
 * @defgroup ADC_AWD_CHSR ADC AWD CHSR
 * @{
 */
#define ADC_AWDx_CHSR(__base__, __awdNum__) (*(__IO uint32_t *)((uint32_t)(__base__) + ((uint32_t)(__awdNum__) << 3U)))
/**
 * @}
 */

/**
 * @defgroup ADC_Check_Parameters_Validity ADC check parameters validity
 * @{
 */
#define IS_ADC_UNIT(x)                                                         \
(   ((x) == M4_ADC1)                        ||                                 \
    ((x) == M4_ADC2)                        ||                                 \
    ((x) == M4_ADC3))

#define IS_ADC_SEQ(x)                                                          \
(   ((x) == ADC_SEQ_A)                      ||                                 \
    ((x) == ADC_SEQ_B))

#define IS_ADC_SCAN_MODE(x)                                                    \
(   ((x) == ADC_MODE_SA_SSHOT)              ||                                 \
    ((x) == ADC_MODE_SA_CONT)               ||                                 \
    ((x) == ADC_MODE_SA_SB_SSHOT)           ||                                 \
    ((x) == ADC_MODE_SA_CONT_SB_SSHOT))

#define IS_ADC_RESOLUTION(x)                                                   \
(   ((x) == ADC_RESOLUTION_8BIT)            ||                                 \
    ((x) == ADC_RESOLUTION_10BIT)           ||                                 \
    ((x) == ADC_RESOLUTION_12BIT))

#define IS_ADC_TRIG_SRC(x)                                                     \
(   ((x) == ADC_TRIG_SRC_ADTRG)             ||                                 \
    ((x) == ADC_TRIG_SRC_EVENT0)            ||                                 \
    ((x) == ADC_TRIG_SRC_EVENT1)            ||                                 \
    ((x) == ADC_TRIG_SRC_EVENT0_EVENT1))

#define IS_ADC_AUTO_CLEAR_CMD(x)                                               \
(   ((x) == ADC_AUTO_CLR_DISABLE)           ||                                 \
    ((x) == ADC_AUTO_CLR_ENABLE))

#define IS_ADC_DATA_ALIGN(x)                                                   \
(   ((x) == ADC_DATA_ALIGN_RIGHT)           ||                                 \
    ((x) == ADC_DATA_ALIGN_LEFT))

#define IS_ADC_SA_RESUME_POS(x)                                                \
(   ((x) == ADC_SA_RESUME_POS_INT_CH)       ||                                 \
    ((x) == ADC_SA_RESUME_POS_FIRST_CH))

#define IS_ADC_SAMPLE_TIME(x)                                                  \
(   ((x) >= 5U))

#define IS_ADC_AVG_CNT(x)                                                      \
(   ((x) == ADC_AVG_CNT_2)                  ||                                 \
    ((x) == ADC_AVG_CNT_4)                  ||                                 \
    ((x) == ADC_AVG_CNT_8)                  ||                                 \
    ((x) == ADC_AVG_CNT_16)                 ||                                 \
    ((x) == ADC_AVG_CNT_32)                 ||                                 \
    ((x) == ADC_AVG_CNT_64)                 ||                                 \
    ((x) == ADC_AVG_CNT_128)                ||                                 \
    ((x) == ADC_AVG_CNT_256))

#define IS_ADC_EXCH_SRC(x)                                                     \
(   ((x) == ADC_EXCH_SRC_ADC_PIN)           ||                                 \
    ((x) == ADC_EXCH_SRC_INTERNAL))

#define IS_AWD_UNIT(x)                                                         \
(   ((x) == ADC_AWD_0)                      ||                                 \
    ((x) == ADC_AWD_1))

#define IS_AWD_CH_NUM(x)                                                       \
(   ((x) <= ADC_CH_NUM_15))

#define IS_AWD_CMP_MODE(x)                                                     \
(   ((x) == ADC_AWD_CMP_OUT_RANGE)          ||                                 \
    ((x) == ADC_AWD_CMP_IN_RANGE))

#define IS_AWD_COMB_MODE(x)                                                    \
(   ((x) == ADC_AWD_COMB_OR)                ||                                 \
    ((x) == ADC_AWD_COMB_AND)               ||                                 \
    ((x) == ADC_AWD_COMB_XOR))

#define IS_PGA_UNIT(x)                                                         \
(   ((x) <= ADC_PGA_4))

#define IS_PGA_GAIN(x)                                                         \
(   ((x) <= ADC_PGA_GAIN_32))

#define IS_PGA_VSS(x)                                                          \
(   ((x) == ADC_PGA_VSS_PGAVSS)             ||                                 \
    ((x) == ADC_PGA_VSS_AVSS))

#define IS_ADC_SYNC_MODE(x)                                                    \
(   ((x) == ADC_SYNC_SSHOT_SEQUENTIAL)      ||                                 \
    ((x) == ADC_SYNC_SSHOT_SIMULTANEOUS)    ||                                 \
    ((x) == ADC_SYNC_CONT_SEQUENTIAL)       ||                                 \
    ((x) == ADC_SYNC_CONT_SIMULTANEOUS))

#define IS_ADC_SYNC_UNIT(x)                                                    \
(   ((x) == ADC_SYNC_ADC1_ADC2)             ||                                 \
    ((x) == ADC_SYNC_ADC1_ADC2_ADC3))

#define IS_ADC_SYNC_TRIG_DLY(x)                                                \
(   ((x) != 0U))

#define IS_ADC_REMAP_PIN_NUM(x)                                                \
(   ((x) <= ADC12_IN15))

#define IS_ADC1_DR_LENGTH(x)                                                   \
(   ((x) >= ADC1_CH_COUNT))

#define IS_ADC2_DR_LENGTH(x)                                                   \
(   ((x) >= ADC2_CH_COUNT))

#define IS_ADC3_DR_LENGTH(x)                                                   \
(   ((x) >= ADC3_CH_COUNT))

#define IS_ADC_1_BIT_MSK(x)                                                    \
(   ((x) != 0U)                             &&                                 \
    (((x) & ((x) - 1U)) == 0U))

#define IS_ADC_BIT_MSK(x, msk)                                                 \
(   ((x) != 0U)                             &&                                 \
    (((x) | (msk)) == (msk)))

/**
 * @}
 */

/**
 * @defgroup ADC_Miscellaneous_Macros ADC Miscellaneous Macros
 * @{
 */
#define ADC_UNIT_COUNT                      (3U)
#define ADC_IND_SPL_TIME_COUNT              (16U)
/**
 * @}
 */

/**
 * @}
 */

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
/**
 * @defgroup ADC_Global_Functions ADC Global Functions
 * @{
 */

/**
 * @brief  Initializes the specified ADC peripheral according to the specified parameters
 *         in the structure stc_adc_init_t.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  pstcInit               Pointer to a stc_adc_init_t structure value that
 *                                      contains the configuration information for the ADC.
 * @retval An en_result_t enumeration value.
 *   @arg  Ok:                          No errors occurred.
 *   @arg  ErrorInvalidParameter:       pstcInit == NULL.
 */
en_result_t ADC_Init(M4_ADC_TypeDef *ADCx, const stc_adc_init_t *pstcInit)
{
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcInit != NULL)
    {
        DDL_ASSERT(IS_ADC_UNIT(ADCx));
        DDL_ASSERT(IS_ADC_SCAN_MODE(pstcInit->u16ScanMode));
        DDL_ASSERT(IS_ADC_RESOLUTION(pstcInit->u16Resolution));
        DDL_ASSERT(IS_ADC_AUTO_CLEAR_CMD(pstcInit->u16AutoClrCmd));
        DDL_ASSERT(IS_ADC_DATA_ALIGN(pstcInit->u16DataAlign));
        DDL_ASSERT(IS_ADC_SA_RESUME_POS(pstcInit->u16SAResumePos));

        /* Configures scan convert mode, resolution, data automatically clear command
           and data alignment. */
        WRITE_REG16(ADCx->CR0,                 \
                    (pstcInit->u16ScanMode   | \
                     pstcInit->u16Resolution | \
                     pstcInit->u16AutoClrCmd | \
                     pstcInit->u16DataAlign));

        /* Configures the channel sequence A restart from. */
        WRITE_REG16(ADCx->CR1, pstcInit->u16SAResumePos);

        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  De-initializes the ADC peripheral. Reset the registers of the specified ADC unit.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @retval None
 */
void ADC_DeInit(M4_ADC_TypeDef *ADCx)
{
    uint8_t i;
    uint32_t u32SSTRAddr;

    DDL_ASSERT(IS_ADC_UNIT(ADCx));

    /* Stop the ADC. */
    WRITE_REG8(ADCx->STR, 0U);

    /* Set the registers to reset value. */
    WRITE_REG16(ADCx->CR0, 0x0U);
    WRITE_REG16(ADCx->CR1, 0x0U);
    WRITE_REG16(ADCx->TRGSR, 0x0U);

    WRITE_REG32(ADCx->CHSELRA, 0x0U);
    WRITE_REG32(ADCx->CHSELRB, 0x0U);
    WRITE_REG32(ADCx->AVCHSELR, 0x0U);
    WRITE_REG8(ADCx->EXCHSELR, 0x0U);

    WRITE_REG16(ADCx->CHMUXR0, 0x3210U);
    WRITE_REG16(ADCx->CHMUXR1, 0x7654U);
    WRITE_REG16(ADCx->CHMUXR2, 0xBA98U);
    WRITE_REG16(ADCx->CHMUXR3, 0xFEDCU);

    WRITE_REG8(ADCx->ICR, 0x03U);
    WRITE_REG8(ADCx->ISCLRR, 0x13U);

    WRITE_REG16(ADCx->AWDCR, 0x0U);
    WRITE_REG8(ADCx->AWDSCLRR, 0x13U);

    WRITE_REG16(ADCx->AWD0DR0, 0x0U);
    WRITE_REG16(ADCx->AWD0DR1, 0xFFFFU);
    WRITE_REG16(ADCx->AWD1DR0, 0x0U);
    WRITE_REG16(ADCx->AWD1DR1, 0xFFFFU);

    WRITE_REG8(ADCx->AWD0CHSR, 0x0U);
    WRITE_REG8(ADCx->AWD1CHSR, 0x0U);

    u32SSTRAddr = (uint32_t)&ADCx->SSTR0;
    for (i=0U; i<ADC_IND_SPL_TIME_COUNT; i++)
    {
        RW_MEM8(u32SSTRAddr) = (uint8_t)0x0BU;
        u32SSTRAddr++;
    }

    if (ADCx == M4_ADC1)
    {
        WRITE_REG16(ADCx->SYNCCR, 0x0C00U);
        WRITE_REG16(ADCx->SHCR, 0x18U);
        WRITE_REG8(ADCx->PGACR1, 0x0U);
        WRITE_REG8(ADCx->PGACR2, 0x0U);
        WRITE_REG8(ADCx->PGACR3, 0x0U);
        WRITE_REG8(ADCx->PGAVSSENR, 0x0U);
    }
    else if (ADCx == M4_ADC2)
    {
        WRITE_REG8(ADCx->PGACR1, 0x0U);
        WRITE_REG8(ADCx->PGAVSSENR, 0x0U);
    }
    else if (ADCx == M4_ADC3)
    {
        WRITE_REG8(ADCx->SSTRL, 0x0BU);
    }
    else
    {
        /* rsvd */
    }
}

/**
 * @brief  Set a default value for the ADC initialization structure.
 * @param  [in]  pstcInit               Pointer to a stc_adc_init_t structure that
 *                                      contains configuration information.
 * @retval An en_result_t enumeration value.
 *   @arg  Ok:                          No errors occurred.
 *   @arg  ErrorInvalidParameter:       pstcInit == NULL.
 */
en_result_t ADC_StructInit(stc_adc_init_t *pstcInit)
{
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcInit != NULL)
    {
        pstcInit->u16ScanMode    = ADC_MODE_SA_SSHOT;
        pstcInit->u16Resolution  = ADC_RESOLUTION_12BIT;
        pstcInit->u16AutoClrCmd  = ADC_AUTO_CLR_DISABLE;
        pstcInit->u16DataAlign   = ADC_DATA_ALIGN_RIGHT;
        pstcInit->u16SAResumePos = ADC_SA_RESUME_POS_INT_CH;

        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Set scan convert mode for the specified ADC unit.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u16Mode                ADC san convert mode.
 *                                      This parameter can be a value of @ref ADC_Scan_Convert_Mode.
 *   @arg  ADC_MODE_SA_SSHOT:           Sequence A single shot.
 *   @arg  ADC_MODE_SA_CONT:            Sequence A continuous.
 *   @arg  ADC_MODE_SA_SB_SSHOT:        Sequence A and B both single shot.
 *   @arg  ADC_MODE_SA_CONT_SB_SSHOT:   Sequence A continuous and sequence B single shot.
 * @retval None
 */
void ADC_SetScanMode(M4_ADC_TypeDef *ADCx, uint16_t u16Mode)
{
    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    DDL_ASSERT(IS_ADC_SCAN_MODE(u16Mode));
    MODIFY_REG16(ADCx->CR0, ADC_CR0_MS, u16Mode);
}

/**
 * @brief  Enable or disable the specified sequence's channels.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8Seq                  The sequence that it's specified channels will be enabled or disabled.
 *                                      This parameter can be a value of @ref ADC_Sequence
 *   @arg  ADC_SEQ_A:                   Sequence A.
 *   @arg  ADC_SEQ_B:                   Sequence B.
 * @param  [in]  u32AdcCh               The ADC channels.
 *                                      This parameter can be values of @ref ADC_Channel
 *   @arg  ADC_CH0 ~ ADC_CH15:          For M4_ADC1 and M4_ADC2.
 *   @arg  ADC_CH0 ~ ADC_CH19:          For M4_ADC3.
 * @param  [in]  pu8SplTime:            Pointer to an uint8_t type array which contains sampling time of the specified channels.
 * @param  [in]  enNewState:            An en_functional_state_t value.
 *   @arg  Enable:                      Enable the specified sequence's specified channels.
 *   @arg  Disable:                     Disable the specified sequence's specified channels.
 * @retval An en_result_t enumeration value.
 *   @arg  Ok:                          No errors occurred.
 *   @arg  ErrorInvalidParameter:       pu8SplTime == NULL.
 * @note   Sequence A and Sequence B CAN NOT include the same channel!
 * @note   ADC_CH16 ~ ADC_CH19 of ADC3 share the same sampling time.
 */
en_result_t ADC_ChannelCmd(M4_ADC_TypeDef *ADCx, uint8_t u8Seq, \
                           uint32_t u32AdcCh, const uint8_t pu8SplTime[], \
                           en_functional_state_t enNewState)
{
    uint8_t i = 0U;
    uint8_t j = 0U;
    uint32_t u32CHSELAddr;
    uint32_t u32SSTRAddr;
    uint32_t au32ChMsk[] = {ADC1_CH_ALL, ADC2_CH_ALL, ADC3_CH_ALL};
    en_result_t enRet = ErrorInvalidParameter;

    if (pu8SplTime != NULL)
    {
        DDL_ASSERT(IS_ADC_UNIT(ADCx));
        DDL_ASSERT(IS_ADC_SEQ(u8Seq));
        DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

        u32CHSELAddr = (uint32_t)&ADCx->CHSELRA + ((uint32_t)u8Seq * 4U);
        u32SSTRAddr  = (uint32_t)&ADCx->SSTR0;
        u32AdcCh    &= au32ChMsk[ADC_IDX(ADCx)];

        if (enNewState == Enable)
        {
            RW_MEM32(u32CHSELAddr) |= u32AdcCh;

            while (u32AdcCh != 0U)
            {
                if ((u32AdcCh & 0x1UL) != 0U)
                {
                    DDL_ASSERT(IS_ADC_SAMPLE_TIME(pu8SplTime[j]));
                    RW_MEM8(u32SSTRAddr+i) = pu8SplTime[j];
                    if (i < ADC_IND_SPL_TIME_COUNT)
                    {
                        j++;
                    }
                }
                u32AdcCh >>= 1U;
                if (i < ADC_IND_SPL_TIME_COUNT)
                {
                    i++;
                }
            }
        }
        else
        {
            RW_MEM32(u32CHSELAddr) &= (uint32_t)(~u32AdcCh);
        }

        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Set average count for the specified ADC unit.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u16AvgCnt              The average count to be set for the specified ADC unit.
 *                                      This parameter can be a value of @ref ADC_Average_Count
 *   @arg  ADC_AVG_CNT_2
 *   @arg  ADC_AVG_CNT_4
 *   @arg  ADC_AVG_CNT_8
 *   @arg  ADC_AVG_CNT_16
 *   @arg  ADC_AVG_CNT_32
 *   @arg  ADC_AVG_CNT_64
 *   @arg  ADC_AVG_CNT_128
 *   @arg  ADC_AVG_CNT_256
 * @retval None
 */
void ADC_AvgChannelConfig(M4_ADC_TypeDef *ADCx, uint16_t u16AvgCnt)
{
    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    DDL_ASSERT(IS_ADC_AVG_CNT(u16AvgCnt));
    MODIFY_REG16(ADCx->CR0, ADC_CR0_AVCNT, u16AvgCnt);
}

/**
 * @brief  Enable or disable the specified ADC unit's average channels.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u32AdcCh               The ADC channels.
 *                                      This parameter can be values of @ref ADC_Channel
 *   @arg  ADC_CH0 ~ ADC_CH15:          For M4_ADC1 and M4_ADC2.
 *   @arg  ADC_CH0 ~ ADC_CH19:          For M4_ADC3.
 * @param  [in]  enNewState:            An en_functional_state_t value.
 *   @arg  Enable:                      Enable the specified ADC unit's average channels.
 *   @arg  Disable:                     Disable the specified ADC unit's average channels.
 * @retval None
 */
void ADC_AvgChannelCmd(M4_ADC_TypeDef *ADCx, uint32_t u32AdcCh, en_functional_state_t enNewState)
{
    uint32_t u32SACh;
    uint32_t u32SBCh;

    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    u32SACh = ADCx->CHSELRA;
    u32SBCh = ADCx->CHSELRB;
    u32AdcCh &= (u32SACh | u32SBCh);
    if (enNewState == Enable)
    {
        SET_REG32_BIT(ADCx->AVCHSELR, u32AdcCh);
    }
    else
    {
        CLEAR_REG32_BIT(ADCx->AVCHSELR, u32AdcCh);
    }
}

/**
 * @brief  Set analog input source for the specified ADC unit's extend channel.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8ExChSrc              The analog input source for the extend channel.
 *                                      This parameter can be a value of @ref ADC_Extend_Channel_Source
 *   @arg  ADC_EXCH_SRC_ADC_PIN:        The analog input source of extend channel is ADC analog input pin,
 *                                      ADC12_IN15 of ADC1/ADC2 and ADC3_IN15 of ADC3.
 *   @arg  ADC_EXCH_SRC_INTERNAL:       The analog input source of extend channels is internal reference voltage or
 *                                      the partial voltage of VBAT.
 * @retval None
 */
void ADC_SetExChannelSrc(M4_ADC_TypeDef *ADCx, uint8_t u8ExChSrc)
{
    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    DDL_ASSERT(IS_ADC_EXCH_SRC(u8ExChSrc));
    WRITE_REG8(ADCx->EXCHSELR, u8ExChSrc);
}

/**
 * @brief  Set a default value for the trigger source configuration structure.
 * @param  [in]  pstcCfg                Pointer to a stc_adc_trig_cfg_t structure that
 *                                      contains configuration information.
 * @retval An en_result_t enumeration value.
 *   @arg  Ok:                          No errors occurred.
 *   @arg  ErrorInvalidParameter:       pstcCfg == NULL.
 */
en_result_t ADC_TrigSrcStructInit(stc_adc_trig_cfg_t *pstcCfg)
{
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcCfg != NULL)
    {
        pstcCfg->u16TrigSrc = ADC_TRIG_SRC_ADTRG;
        pstcCfg->enEvent0   = EVT_MAX;
        pstcCfg->enEvent1   = EVT_MAX;

        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Configures the trigger source for the specified sequence of the specified ADC unit.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8Seq                  The sequence to be configured.
 *                                      This parameter can be a value of @ref ADC_Sequence
 *   @arg  ADC_SEQ_A:                   Sequence A.
 *   @arg  ADC_SEQ_B:                   Sequence B.
 * @param  [in]  pstcCfg                Pointer to a stc_adc_trig_cfg_t structure that contains
 *                                      the configuration information of the trigger source.
 * @retval An en_result_t enumeration value.
 *   @arg  Ok:                          No errors occurred.
 *   @arg  ErrorInvalidParameter:       pstcCfg == NULL.
 * @note   ADC must be stopped while calling this function.
 * @note   The trigger source CANNOT be an event that generated by the sequence itself.
 */
en_result_t ADC_TrigSrcConfig(M4_ADC_TypeDef *ADCx, uint8_t u8Seq, const stc_adc_trig_cfg_t *pstcCfg)
{
    uint32_t u32AdcIdx;
    uint32_t u32ITRGSELRAddr;
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcCfg != NULL)
    {
        DDL_ASSERT(IS_ADC_UNIT(ADCx));
        DDL_ASSERT(IS_ADC_SEQ(u8Seq));
        DDL_ASSERT(IS_ADC_TRIG_SRC(pstcCfg->u16TrigSrc));

        u32AdcIdx = ADC_IDX(ADCx);
        u32ITRGSELRAddr = (uint32_t)&M4_AOS->ADC_1_ITRGSELR0 + u32AdcIdx * 8U;
        if ((pstcCfg->u16TrigSrc & ADC_TRIG_SRC_EVENT0) != 0U)
        {
            MODIFY_REG32(RW_MEM32(u32ITRGSELRAddr), ADC_TRIG_EVENT_MSK, pstcCfg->enEvent0);
        }

        if ((pstcCfg->u16TrigSrc & ADC_TRIG_SRC_EVENT1) != 0U)
        {
            u32ITRGSELRAddr += 4U;
            MODIFY_REG32(RW_MEM32(u32ITRGSELRAddr), ADC_TRIG_EVENT_MSK, pstcCfg->enEvent1);
        }

        MODIFY_REG16(ADCx->TRGSR, \
                     ADC_TRGSR_VAL(u8Seq, ADC_TRIG_SRC_MSK), \
                     ADC_TRGSR_VAL(u8Seq, pstcCfg->u16TrigSrc));
        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Enable or disable the trigger source of the specified sequence.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8Seq                  The sequence to be configured.
 *                                      This parameter can be a value of @ref ADC_Sequence
 *   @arg  ADC_SEQ_A:                   Sequence A.
 *   @arg  ADC_SEQ_B:                   Sequence B.
 * @param  [in]  enNewState             An en_functional_state_t enumeration value.
 *   @arg  Enable:                      Enable the trigger source of the specified sequence.
 *   @arg  Disable:                     Disable the trigger source of the specified sequence.
 * @retval None
 * @note   ADC must be stopped while calling this function.
 */
void ADC_TrigSrcCmd(M4_ADC_TypeDef *ADCx, uint8_t u8Seq, en_functional_state_t enNewState)
{
    uint32_t u32Addr;
    uint8_t au8CmdPos[] = {ADC_TRGSR_TRGENA_POS, ADC_TRGSR_TRGENB_POS};

    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    DDL_ASSERT(IS_ADC_SEQ(u8Seq));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    u32Addr = (uint32_t)&ADCx->TRGSR;
    BIT_BAND(u32Addr, au8CmdPos[u8Seq]) = (uint32_t)enNewState;
}

/**
 * @brief  Enable or disable common trigger event to start ADC.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u16TrigSrc             ADC trigger source type.
 *                                      This parameter can be a value of @ref ADC_Trigger_Source_Type except ADC_TRIG_SRC_ADTRG.
 *   @arg  ADC_TRIG_SRC_EVENT0:         The trigger source is a internal event from other peripheral. \
 *                                      Only one event can be configured to trigger ADC.
 *   @arg  ADC_TRIG_SRC_EVENT1:         The trigger source is a internal event from other peripheral. \
 *                                      Only one event can be configured to trigger ADC.
 *   @arg  ADC_TRIG_SRC_EVENT0_EVENT1:  The trigger source are two internal events from other peripheral(s). \
 *                                      Two events can be configured to trigger ADC and one of which can trigger the ADC.
 * @param  [in]  u32ComTrig             Common trigger event enable bit mask.
 *                                      This parameter can be a value of @ref ADC_Common_Trigger_Sel
 *   @arg  ADC_COM_TRIG1:               Common trigger 1.
 *   @arg  ADC_COM_TRIG2:               Common trigger 2.
 * @param  [in]  enNewState             An en_functional_state_t enumeration type value.
 *   @arg Enable:                       Enable the specified common trigger.
 *   @arg Disable:                      Disable the specified common trigger.
 * @retval None
 */
void ADC_ComTriggerCmd(M4_ADC_TypeDef *ADCx, uint16_t u16TrigSrc, \
                       uint32_t u32ComTrig, en_functional_state_t enNewState)
{
    uint32_t u32AdcIdx;
    uint32_t u32ITRGSELRAddr;

    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    DDL_ASSERT(IS_ADC_BIT_MSK(u32ComTrig, ADC_COM_TRIG_MSK));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    u32AdcIdx = ADC_IDX(ADCx);
    u32ITRGSELRAddr = (uint32_t)&M4_AOS->ADC_1_ITRGSELR0 + u32AdcIdx * 8U;

    if ((u16TrigSrc & ADC_TRIG_SRC_EVENT0) != 0U)
    {
        if (enNewState == Enable)
        {
            SET_REG32_BIT(RW_MEM32(u32ITRGSELRAddr), u32ComTrig);
        }
        else
        {
            CLEAR_REG32_BIT(RW_MEM32(u32ITRGSELRAddr), u32ComTrig);
        }
    }

    if ((u16TrigSrc & ADC_TRIG_SRC_EVENT1) != 0U)
    {
        u32ITRGSELRAddr += 4UL;
        if (enNewState == Enable)
        {
            SET_REG32_BIT(RW_MEM32(u32ITRGSELRAddr), u32ComTrig);
        }
        else
        {
            CLEAR_REG32_BIT(RW_MEM32(u32ITRGSELRAddr), u32ComTrig);
        }
    }
}

/**
 * @brief  Enable or disable the interrupt of the specified ADC unit's specified sequence.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8Seq                  The sequence to be configured.
 *                                      This parameter can be a value of @ref ADC_Sequence
 *   @arg  ADC_SEQ_A:                   Sequence A.
 *   @arg  ADC_SEQ_B:                   Sequence B.
 * @param  [in]  enNewState             An en_functional_state_t enumeration value.
 *   @arg  Enable:                      Enable the interrupt of the specified sequence.
 *   @arg  Disable:                     Disable the interrupt of the specified sequence.
 * @retval None
 */
void ADC_SeqIntCmd(M4_ADC_TypeDef *ADCx, uint8_t u8Seq, en_functional_state_t enNewState)
{
    uint32_t u32Addr;

    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    DDL_ASSERT(IS_ADC_SEQ(u8Seq));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    u32Addr = (uint32_t)&ADCx->ICR;
    BIT_BAND(u32Addr, u8Seq) = (uint32_t)enNewState;
}

/**
 * @brief  Get the status flag of the specified ADC unit's specified sequence.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8Flag                 Status flag type.
 *                                      This parameter can be valueS of @ref ADC_Sequence_Status_Flag
 *   @arg  ADC_SEQ_FLAG_EOCA:           Status flag of the end of conversion of sequence A.
 *   @arg  ADC_SEQ_FLAG_EOCB:           Status flag of the end of conversion of sequence B.
 *   @arg  ADC_SEQ_FLAG_NESTED:         Status flag of sequence A was interrupted by sequence B.
 * @retval An en_flag_status_t enumeration type value.
 *   @arg  Set:                         At least one of the specified flags is set.
 *   @arg  Reset:                       The specified flags are not set.
 */
en_flag_status_t ADC_SeqGetStatus(const M4_ADC_TypeDef *ADCx, uint8_t u8Flag)
{
    en_flag_status_t enRet = Reset;

    DDL_ASSERT(IS_ADC_UNIT(ADCx));

    if (READ_REG8_BIT(ADCx->ISR, (u8Flag & (uint8_t)ADC_SEQ_FLAG_ALL)) != 0U)
    {
        enRet = Set;
    }

    return enRet;
}

/**
 * @brief  Clear the status flag of the specified ADC unit's specified sequence.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8Flag                 Status flag type.
 *                                      This parameter can be values of @ref ADC_Sequence_Status_Flag
 *   @arg  ADC_SEQ_FLAG_EOCA:           Status flag of the end of conversion of sequence A.
 *   @arg  ADC_SEQ_FLAG_EOCB:           Status flag of the end of conversion of sequence B.
 *   @arg  ADC_SEQ_FLAG_NESTED:         Status flag of sequence A was interrupted by sequence B.
 * @retval None
 */
void ADC_SeqClrStatus(M4_ADC_TypeDef *ADCx, uint8_t u8Flag)
{
    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    SET_REG8_BIT(ADCx->ISCLRR, (u8Flag & (uint8_t)ADC_SEQ_FLAG_ALL));
}

/**
 * @brief  Configures analog watchdog(AWD).
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8AWDx                 AWD unit that is going to be configured.
 *                                      This parameter can be a value of @ref ADC_AWD_Unit
 *   @arg  ADC_AWD_0:                   AWD unit 0 of the specified ADC.
 *   @arg  ADC_AWD_1:                   AWD unit 1 of the specified ADC.
 * @param  [in]  pstcCfg                Pointer to a stc_adc_awd_cfg_t structure value that
 *                                      contains the configuration information for the AWD.
 * @retval An en_result_t enumeration value.
 *   @arg  Ok:                          No errors occurred.
 *   @arg  ErrorInvalidParameter:       pstcCfg == NULL.
 */
en_result_t ADC_AWD_Config(M4_ADC_TypeDef *ADCx, uint8_t u8AWDx, const stc_adc_awd_cfg_t *pstcCfg)
{
    uint32_t u32AwdCr;
    uint32_t u32AwdDr0;
    uint32_t u32AwdDr1;
    uint32_t u32AwdChsr;
    uint32_t u32Pos;
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcCfg != NULL)
    {
        DDL_ASSERT(IS_ADC_UNIT(ADCx));
        DDL_ASSERT(IS_AWD_UNIT(u8AWDx));
        DDL_ASSERT(IS_AWD_CH_NUM(pstcCfg->u8ChNum));
        DDL_ASSERT(IS_AWD_CMP_MODE(pstcCfg->u16CmpMode));

        u32Pos     = ((uint32_t)u8AWDx << 2U) + ADC_AWDCR_AWD0MD_POS;
        u32AwdCr   = (uint32_t)&ADCx->AWDCR;
        u32AwdDr0  = (uint32_t)&ADCx->AWD0DR0;
        u32AwdDr1  = (uint32_t)&ADCx->AWD0DR1;
        u32AwdChsr = (uint32_t)&ADCx->AWD0CHSR;

        BIT_BAND(u32AwdCr, u32Pos) = (uint32_t)pstcCfg->u16CmpMode;
        ADC_AWDx_DR(u32AwdDr0, u8AWDx) = pstcCfg->u16LowerLimit;
        ADC_AWDx_DR(u32AwdDr1, u8AWDx) = pstcCfg->u16UpperLimit;
        ADC_AWDx_CHSR(u32AwdChsr, u8AWDx) = pstcCfg->u8ChNum;

        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Enable or disable combination mode of analog watchdog(AWD).
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u16CombMode            Combination mode of analog watchdog.
 *                                      This parameter can be a value of @ref ADC_AWD_Combination_Mode
 *   @arg  ADC_AWD_COMB_OR:             The status of AWD0 is set or the status of AWD1 is set, the status of combination mode is set.
 *   @arg  ADC_AWD_COMB_AND:            The status of AWD0 is set and the status of AWD1 is set, the status of combination mode is set.
 *   @arg  ADC_AWD_COMB_XOR:            Only one of the status of AWD0 and AWD1 is set, the status of combination mode is set.
 * @param  [in]  enNewState:            An en_functional_state_t value.
 *   @arg  Enable:                      Enable the combination mode of specified ADC unit's AWD.
 *   @arg  Disable:                     Disable the combination mode of specified ADC unit's AWD.
 * @retval None
 */
void ADC_AWD_CombModeCmd(M4_ADC_TypeDef *ADCx, uint16_t u16CombMode, en_functional_state_t enNewState)
{
    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    DDL_ASSERT(IS_AWD_COMB_MODE(u16CombMode));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    CLEAR_REG16_BIT(ADCx->AWDCR, ADC_AWD_COMB_MODE_MSK);
    if (enNewState == Enable)
    {
        SET_REG16_BIT(ADCx->AWDCR, u16CombMode);
    }
}

/**
 * @brief  Enable or disable the specified analog watchdog(AWD).
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8AWDx                 AWD unit that is going to be enabled or disabled.
 *                                      This parameter can be a value of @ref ADC_AWD_Unit
 *   @arg  ADC_AWD_0:                   AWD unit 0 of the specified ADC.
 *   @arg  ADC_AWD_1:                   AWD unit 1 of the specified ADC.
 * @param  [in]  enNewState:            An en_functional_state_t value.
 *   @arg  Enable:                      Enable the specified AWD.
 *   @arg  Disable:                     Disable the specified AWD.
 * @retval None
 */
void ADC_AWD_Cmd(M4_ADC_TypeDef *ADCx, uint8_t u8AWDx, en_functional_state_t enNewState)
{
    uint32_t u32Addr;
    uint32_t u32Pos;

    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    DDL_ASSERT(IS_AWD_UNIT(u8AWDx));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    u32Pos  = ((uint32_t)u8AWDx << 2U) + ADC_AWDCR_AWD0EN_POS;
    u32Addr = (uint32_t)&ADCx->AWDCR;
    BIT_BAND(u32Addr, u32Pos) = (uint32_t)enNewState;
}

/**
 * @brief  Enable or disable the specified analog watchdog's interrupt.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8AWDx                 AWD unit.
 *                                      This parameter can be a value of @ref ADC_AWD_Unit
 *   @arg  ADC_AWD_0:                   AWD unit 0 of the specified ADC.
 *   @arg  ADC_AWD_1:                   AWD unit 1 of the specified ADC.
 * @param  [in]  enNewState:            An en_functional_state_t value.
 *   @arg  Enable:                      Enable the specified AWD's interrupt.
 *   @arg  Disable:                     Disable the specified AWD's interrupt.
 * @retval None
 */
void ADC_AWD_IntCmd(M4_ADC_TypeDef *ADCx, uint8_t u8AWDx, en_functional_state_t enNewState)
{
    uint32_t u32Addr;
    uint32_t u32Pos;

    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    DDL_ASSERT(IS_AWD_UNIT(u8AWDx));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    u32Pos  = ((uint32_t)u8AWDx << 2U) + ADC_AWDCR_AWD0IEN_POS;
    u32Addr = (uint32_t)&ADCx->AWDCR;
    BIT_BAND(u32Addr, u32Pos) = (uint32_t)enNewState;
}

/**
 * @brief  Get the analog watchdog's flag status of the specified ADC unit.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8Flag                 AWD status flag type.
 *                                      This parameter can be values of @ref ADC_AWD_Status_Flag
 *   @arg  ADC_AWD_FLAG_AWD0:           Flag of AWD0.
 *   @arg  ADC_AWD_FLAG_AWD1:           Flag of AWD1.
 *   @arg  ADC_AWD_FLAG_COMB:           Flag of combination mode.
 * @retval An en_flag_status_t enumeration type value.
 *   @arg  Set:                         At least one of the specified flags is set.
 *   @arg  Reset:                       The specified flags are not set.
 */
en_flag_status_t ADC_AWD_GetStatus(const M4_ADC_TypeDef *ADCx, uint8_t u8Flag)
{
    en_flag_status_t enRet = Reset;

    DDL_ASSERT(IS_ADC_UNIT(ADCx));

    if (READ_REG8_BIT(ADCx->AWDSR, (u8Flag & (uint8_t)ADC_AWD_FLAG_ALL)) != 0U)
    {
        enRet = Set;
    }

    return enRet;
}

/**
 * @brief  Clear the analog watchdog's flag status of the specified ADC unit.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8Flag                 AWD status flag type.
 *                                      This parameter can be values of @ref ADC_AWD_Status_Flag
 *   @arg  ADC_AWD_FLAG_AWD0:           Flag of AWD0.
 *   @arg  ADC_AWD_FLAG_AWD1:           Flag of AWD1.
 *   @arg  ADC_AWD_FLAG_COMB:           Flag of combination mode.
 * @retval None
 */
void ADC_AWD_ClrStatus(M4_ADC_TypeDef *ADCx, uint8_t u8Flag)
{
    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    u8Flag &= (uint8_t)ADC_AWD_FLAG_ALL;
    SET_REG8_BIT(ADCx->AWDSCLRR, u8Flag);
}

/**
 * @brief  Configures the specified programmable gain amplifier(PGA).
 * @param  [in]  u8PGAx                 The PGA unit that is going to be configured.
 *                                      This parameter can be a value of @ref ADC_PGA_Unit
 *   @arg  ADC_PGA_1:                   PGA1, belongs to ADC1. Input pin is ADC123_IN0.
 *   @arg  ADC_PGA_2:                   PGA2, belongs to ADC1. Input pin is ADC123_IN1.
 *   @arg  ADC_PGA_3:                   PGA3, belongs to ADC1. Input pin is ADC123_IN2.
 *   @arg  ADC_PGA_4:                   PGA4, belongs to ADC2. Input pin is ADC12_IN7.
 * @param  [in]  u8GainFactor           Gain factor for the specified PGA.
 *                                      This parameter can be a value of @ref ADC_PGA_Gain_Factor
 *   @arg  ADC_PGA_GAIN_2:              PGA gain factor is 2.
 *   @arg  ADC_PGA_GAIN_2P133:          PGA gain factor is 2.133.
 *   @arg  ADC_PGA_GAIN_2P286:          PGA gain factor is 2.286.
 *   @arg  ADC_PGA_GAIN_2P667:          PGA gain factor is 2.667.
 *   @arg  ADC_PGA_GAIN_2P909:          PGA gain factor is 2.909.
 *   @arg  ADC_PGA_GAIN_3P2:            PGA gain factor is 3.2.
 *   @arg  ADC_PGA_GAIN_3P556:          PGA gain factor is 2.556.
 *   @arg  ADC_PGA_GAIN_4:              PGA gain factor is 4.
 *   @arg  ADC_PGA_GAIN_4P571:          PGA gain factor is 4.571.
 *   @arg  ADC_PGA_GAIN_5P333:          PGA gain factor is 5.333.
 *   @arg  ADC_PGA_GAIN_6P4:            PGA gain factor is 6.4.
 *   @arg  ADC_PGA_GAIN_8:              PGA gain factor is 8.
 *   @arg  ADC_PGA_GAIN_10P667:         PGA gain factor is 10.667.
 *   @arg  ADC_PGA_GAIN_16:             PGA gain factor is 16.
 *   @arg  ADC_PGA_GAIN_32:             PGA gain factor is 32.
 * @param  [in]  u8PgaVss               VSS for the specified PGA.
 *                                      This parameter can be a value of @ref ADC_PGA_VSS
 *   @arg  ADC_PGA_VSS_PGAVSS:          Use pin PGA123_VSS as the reference GND of PGA1, PGA2 and PGA3.
 *   @arg  ADC_PGA_VSS_AVSS:            Use AVSS as the reference GND of PGA1, PGA2 and PGA3.
 * @retval None
 */
void ADC_PGA_Config(uint8_t u8PGAx, uint8_t u8GainFactor, uint8_t u8PgaVss)
{
    uint32_t u32Addr;
    uint32_t u32PGACRAddr;
    M4_ADC_TypeDef *ADCx;
    uint32_t au32AdcBase[] = {ADC1_BASE, ADC1_BASE, ADC1_BASE, ADC2_BASE};

    DDL_ASSERT(IS_PGA_GAIN(u8GainFactor));
    DDL_ASSERT(IS_PGA_VSS(u8PgaVss));
    DDL_ASSERT(IS_PGA_UNIT(u8PGAx));

    ADCx    = (M4_ADC_TypeDef *)au32AdcBase[u8PGAx];
    u8PGAx %= 3U;
    u32PGACRAddr = (uint32_t)&ADCx->PGACR1 + u8PGAx;
    MODIFY_REG8(RW_MEM8(u32PGACRAddr), ADC_PGA_GAIN_MSK, u8GainFactor);
    u32Addr = (uint32_t)&ADCx->PGAVSSENR;
    BIT_BAND(u32Addr, u8PGAx) = (uint32_t)u8PgaVss;
}

/**
 * @brief  Enable the specified programmable gain amplifier(PGA).
 * @param  [in]  u8PGAx                 The PGA unit which is going to be enabled or disabled.
 *                                      This parameter can be a value of @ref ADC_PGA_Unit
 *   @arg  ADC_PGA_1:                   PGA1, belongs to ADC1. Input pin is ADC123_IN0.
 *   @arg  ADC_PGA_2:                   PGA2, belongs to ADC1. Input pin is ADC123_IN1.
 *   @arg  ADC_PGA_3:                   PGA3, belongs to ADC1. Input pin is ADC123_IN2.
 *   @arg  ADC_PGA_4:                   PGA4, belongs to ADC2. Input pin is ADC12_IN7.
 * @param  [in]  enNewState:            An en_functional_state_t value.
 *   @arg  Enable:                      Enable the specified PGA.
 *   @arg  Disable:                     Disable the specified PGA.
 * @retval None
 */
void ADC_PGA_Cmd(uint8_t u8PGAx, en_functional_state_t enNewState)
{
    uint32_t u32PGACRAddr;
    M4_ADC_TypeDef *ADCx;
    uint32_t au32AdcBase[] = {ADC1_BASE, ADC1_BASE, ADC1_BASE, ADC2_BASE};
    uint8_t au8Cmd[] = {ADC_PGA_DISABLE, ADC_PGA_ENABLE};

    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));
    DDL_ASSERT(IS_PGA_UNIT(u8PGAx));

    ADCx    = (M4_ADC_TypeDef *)au32AdcBase[u8PGAx];
    u8PGAx %= 3U;
    u32PGACRAddr = (uint32_t)&ADCx->PGACR1 + u8PGAx;
    MODIFY_REG8(RW_MEM8(u32PGACRAddr), ADC_PGA_CMD_MSK, au8Cmd[(uint8_t)enNewState]);
}

/**
 * @brief  Configures synchronous mode.
 * @param  [in]  u16SyncUnit            Specify the ADC units which work synchronously.
 *                                      This parameter can be a value of @ref ADC_Synchronous_Unit
 *   @arg  ADC_SYNC_ADC1_ADC2:          ADC1 and ADC2 work synchronously.
 *   @arg  ADC_SYNC_ADC1_ADC2_ADC3:     ADC1, ADC2 and ADC3 work synchronously.
 * @param  [in]  u16SyncMode            Synchronous mode.
 *                                      This parameter can be a value of @ref ADC_Synchronous_Mode
 *   @arg  ADC_SYNC_SSHOT_SEQUENTIAL:   Single shot trigger, sequentially trigger. \
 *                                      When the trigger condition occurs, ADC1 starts before ADC2, \
 *                                      and ADC1 and ADC2 only perform one sample conversion.
 *   @arg  ADC_SYNC_SSHOT_SIMULTANEOUS: Single shot trigger, simultaneously trigger. \
 *                                      When the trigger condition occurs, ADC1 and ADC2 start at the same time, \
 *                                      and ADC1 and ADC2 only perform one sample conversion.
 *   @arg  ADC_SYNC_CONT_SEQUENTIAL:    Continuously trigger, sequentially trigger. \
 *                                      When the trigger condition occurs, ADC1 starts before ADC2, \
 *                                      and ADC1 and ADC2 will continue to sample conversion until stopped by software.
 *   @arg  ADC_SYNC_CONT_SIMULTANEOUS:  Continuously trigger, simultaneously trigger. \
 *                                      When the trigger condition occurs, ADC1 and ADC2 start at the same time, \
 *                                      and ADC1 and ADC2 will continue to sample conversion until stopped by software.
 * @param  [in]  u8TrigDelay            Trigger delay time(PCLK2 cycle), range is [1, 255].
 * @retval None
 */
void ADC_SYNC_Config(uint16_t u16SyncUnit, uint16_t u16SyncMode, uint8_t u8TrigDelay)
{
    DDL_ASSERT(IS_ADC_SYNC_UNIT(u16SyncUnit));
    DDL_ASSERT(IS_ADC_SYNC_MODE(u16SyncMode));
    DDL_ASSERT(IS_ADC_SYNC_TRIG_DLY(u8TrigDelay));

    u16SyncMode |= ((uint16_t)((uint32_t)u8TrigDelay << ADC_SYNCCR_SYNCDLY_POS)) | u16SyncUnit;
    MODIFY_REG16(M4_ADC1->SYNCCR, ADC_SYNC_CFG_MSK, u16SyncMode);
}

/**
 * @brief  Enable or disable synchronous mode.
 * @param  [in]  enNewState:            An en_functional_state_t value.
 *   @arg  Enable:                      Enable synchronous mode.
 *   @arg  Disable:                     Disable synchronous mode.
 * @retval None
 */
void ADC_SYNC_Cmd(en_functional_state_t enNewState)
{
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));
    WRITE_REG32(bM4_ADC1->SYNCCR_b.SYNCEN, enNewState);
}

/**
 * @brief  Configures sample-hold. Specifies the sample time of sample-hold.
 * @param  [in]  u8SplTime              Sample time(ADCLK cycles) for sample-hold. \
 *                                      It should be more than 0.4 microseconds.
 * @retval None
 */
void ADC_SH_Config(uint8_t u8SplTime)
{
    MODIFY_REG16(M4_ADC1->SHCR, ADC_SH_CFG_MSK, u8SplTime);
}

/**
 * @brief  Enable or disable sample-hold channels.
 * @param  [in]  u32AdcCh               ADC channels.
 *                                      This parameter be values of the following that from @ref ADC_Channel
 *   @arg  ADC_CH0:                     Channel 0 of ADC1.
 *   @arg  ADC_CH1:                     Channel 1 of ADC1.
 *   @arg  ADC_CH2:                     Channel 2 of ADC1.
 * @param  [in]  enNewState:            An en_functional_state_t value.
 *   @arg  Enable:                      Enable the specified sample-hold channels.
 *   @arg  Disable:                     Disable the specified sample-hold channels.
 * @retval None
 */
void ADC_SH_ChannelCmd(uint32_t u32AdcCh, en_functional_state_t enNewState)
{
    DDL_ASSERT(IS_ADC_BIT_MSK(u32AdcCh, ADC_CH0|ADC_CH1|ADC_CH2));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    u32AdcCh <<= ADC_SHCR_SHSEL_POS;
    if (enNewState == Enable)
    {
        SET_REG16_BIT(M4_ADC1->SHCR, u32AdcCh);
    }
    else
    {
        CLEAR_REG16_BIT(M4_ADC1->SHCR, u32AdcCh);
    }
}

/**
 * @brief  Remap the correspondence between ADC channels and analog input pins.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u32AdcCh               This parameter be values of the following that from @ref ADC_Channel
 *   @arg  ADC_CH0 ~ ADC_CH15           For all of ADC units.
 * @param  [in]  u8AdcPinNum            This parameter be a value of @ref ADC_Pin_Number
 *   @arg  ADC123_xxx:                  Pin number belongs to all of ADC units.
 *   @arg  ADC12_xxx:                   Pin number belongs to ADC1 and ADC2.
 *   @arg  ADC3_xxx:                    Pin number belongs to ADC3.
 * @note   The pin number is from 0 to 15 can be remaped.
 * @retval None
 */
void ADC_ChannelRemap(M4_ADC_TypeDef *ADCx, uint32_t u32AdcCh, uint8_t u8AdcPinNum)
{
    uint8_t u8FieldOfs;
    uint32_t u32RegIdx;
    uint32_t u32CHMUXRAddr;
    uint32_t u32ChNum = 0U;

    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    DDL_ASSERT(IS_ADC_REMAP_PIN_NUM(u8AdcPinNum));
    DDL_ASSERT(IS_ADC_BIT_MSK(u32AdcCh, ADC_CH_REMAP_ALL));

    u32CHMUXRAddr = (uint32_t)&ADCx->CHMUXR0;
    while (u32AdcCh != 0U)
    {
        if ((u32AdcCh & 0x1UL) != 0U)
        {
            u32RegIdx  = u32ChNum / 4U;
            u8FieldOfs = (uint8_t)((u32ChNum % 4U) << 2U);
            MODIFY_REG16(RW_MEM16(u32CHMUXRAddr + u32RegIdx * 2U), \
                         ((uint32_t)ADC_CH_MAP_PIN_MSK << u8FieldOfs), \
                         ((uint32_t)u8AdcPinNum << u8FieldOfs));
        }

        u32AdcCh >>= 1U;
        u32ChNum++;
    }
}

/**
 * @brief  Get the number of the analog input pin corresponding to the specified ADC unit's channel.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u32AdcCh               ADC channel.
 *                                      This parameter can be one of the following values of @ref ADC_Channel
 *   @arg  ADC_CH0 ~ ADC_CH15
 * @retval An uint8_t type value of ADC pin number that corresponding to the specified channel number.
 *         This parameter can be one of the following values of @ref ADC_Pin_Number
 *   @arg  ADC123_xxx:                  Pin number belongs to all of ADC units.
 *   @arg  ADC12_xxx:                   Pin number belongs to ADC1 and ADC2.
 *   @arg  ADC3_xxx:                    Pin number belongs to ADC3.
 */
uint8_t ADC_GetChannelPinNum(const M4_ADC_TypeDef *ADCx, uint32_t u32AdcCh)
{
    uint32_t u32RegIdx;
    uint32_t u32CHMUXRAddr;
    uint32_t u32ChNum = 0U;
    uint8_t u8RetPinNum;
    uint8_t u8FieldOfs;

    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    DDL_ASSERT(IS_ADC_BIT_MSK(u32AdcCh, ADC_CH_REMAP_ALL));
    DDL_ASSERT(IS_ADC_1_BIT_MSK(u32AdcCh));

    while ((u32AdcCh & (1UL << u32ChNum)) == 0U)
    {
        u32ChNum++;
    }

    u32RegIdx     = u32ChNum / 4U;
    u8FieldOfs    = (uint8_t)((u32ChNum % 4U) << 2U);
    u32CHMUXRAddr = (uint32_t)&ADCx->CHMUXR0 + (u32RegIdx * 2U);
    u8RetPinNum   = (uint8_t)((RW_MEM16(u32CHMUXRAddr) >> u8FieldOfs) & 0xFU);

    return u8RetPinNum;
}

/**
 * @brief  Basic usage of ADC sampling. Get the ADC values of the pins
 *         corresponding to the channels enabled in sequence A.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [out]  pu16AdcVal            Pointer to an uint16_t type memory which the ADC values to be stored.
 *                                      The location of the value store depends on the parameter u8Length.
 *                                      u8Length >= ADCx_CH_COUNT, all of the ADC data registers will be read:
 *                                      pu16AdcVal[0] = value of Channel 0,
 *                                      pu16AdcVal[1] = value of Channel 1,
 *                                      pu16AdcVal[2] = value of Channel 2,
 *                                                  ...
 *                                      u8Length < ADCx_CH_COUNT(ADC1_CH_COUNT or ADC2_CH_COUNT),
 *                                      only the value of the enabled channels will be read:
 *                                      pu16AdcVal[0] = value of the 1st enabled channel,
 *                                      pu16AdcVal[1] = value of the 2nd enabled channel,
 *                                      pu16AdcVal[2] = value of the 3rd enabled channel,
 * @param  [in]  u8Length               The length of the ADC value to be got.
 * @param  [in]  u32Timeout             Timeout value(millisecond).
 * @retval An en_result_t enumeration value.
 *   @arg  Ok:                          No errors occurred.
 *   @arg  ErrorInvalidParameter:      -pu16AdcVal == NULL.
 *                                     -u8Length == 0.
 *                                     -u32Timeout == 0.
 *                                     -The base address of 'pu16AdcVal' is not 2-byte aligned.
 */
en_result_t ADC_PollingSA(M4_ADC_TypeDef *ADCx, uint16_t pu16AdcVal[], uint8_t u8Length, uint32_t u32Timeout)
{
    uint32_t u32Ch;
    uint32_t u32TimeCnt;
    uint8_t au8DrLen[ADC_UNIT_COUNT] = {ADC1_CH_COUNT, ADC2_CH_COUNT, ADC3_CH_COUNT};
    en_result_t enRet = ErrorInvalidParameter;

    if ((pu16AdcVal != NULL) && \
        (u8Length != 0U) && (u32Timeout != 0U) && \
        IS_ADDRESS_ALIGN_HALFWORD(&pu16AdcVal[0U]))
    {
        DDL_ASSERT(IS_ADC_UNIT(ADCx));

        u32TimeCnt = u32Timeout * (HCLK_VALUE / 10U / 1000U);
        enRet = ErrorTimeout;
        /* Start ADC.*/
        WRITE_REG8(ADCx->STR, ADC_STR_STRT);
        while (u32TimeCnt-- != 0U)
        {
            if (READ_REG8_BIT(ADCx->ISR, ADC_SEQ_FLAG_EOCA) != 0U)
            {
                if (u8Length < au8DrLen[ADC_IDX(ADCx)])
                {
                    u32Ch = ADCx->CHSELRA;
                    (void)ADC_GetChannelData(ADCx, u32Ch, pu16AdcVal, u8Length);
                }
                else
                {
                    (void)ADC_GetAllData(ADCx, pu16AdcVal, u8Length);
                }
                /* Clear EOC flag. */
                SET_REG8_BIT(ADCx->ISCLRR, ADC_ISCLRR_CLREOCAF);
                enRet = Ok;
                break;
            }
        }
        if (enRet != Ok)
        {
            /* Stop ADC if timeout. */
            WRITE_REG8(ADCx->STR, 0U);
        }
    }

    return enRet;
}

/**
 * @brief  Start the specified ADC unit.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @retval None
 */
void ADC_Start(M4_ADC_TypeDef *ADCx)
{
    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    WRITE_REG8(ADCx->STR, ADC_STR_STRT);
}

/**
 * @brief  Stop the specified ADC unit.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @retval None
 */
void ADC_Stop(M4_ADC_TypeDef *ADCx)
{
    DDL_ASSERT(IS_ADC_UNIT(ADCx));
    WRITE_REG8(ADCx->STR, 0U);
}

/**
 * @brief  Get all of the channels' value of the specified ADC unit.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [out]  pu16AdcVal            Pointer to an uint16_t type memory which the ADC values to be stored.
 *                                      pu16AdcVal[0] = value of Channel 0,
 *                                      pu16AdcVal[1] = value of Channel 1,
 *                                      pu16AdcVal[2] = value of Channel 2,
 * @param  [in]  u8Length               The length of the ADC value to be got.
 *                                      It CAN NOT be less than the number of channels of the ADC unit.
 * @retval An en_result_t enumeration value.
 *   @arg  Ok:                          No errors occurred.
 *   @arg  ErrorInvalidParameter:      -pu16AdcVal == NULL.
 *                                     -The base address of 'pu16AdcVal' is not 2-byte aligned.
 */
en_result_t ADC_GetAllData(const M4_ADC_TypeDef *ADCx, uint16_t pu16AdcVal[], uint8_t u8Length)
{
    uint8_t u8AdcIdx;
    uint32_t i;
    uint32_t u32DRAddr;
    uint8_t au8DrLen[ADC_UNIT_COUNT] = {ADC1_CH_COUNT, ADC2_CH_COUNT, ADC3_CH_COUNT};
    en_result_t enRet = ErrorInvalidParameter;

    if ((pu16AdcVal != NULL) && IS_ADDRESS_ALIGN_HALFWORD(&pu16AdcVal[0U]))
    {
        DDL_ASSERT(IS_ADC_UNIT(ADCx));

        u8AdcIdx = ADC_IDX(ADCx);
        DDL_ASSERT(u8Length >= au8DrLen[u8AdcIdx]);

        if (u8Length > au8DrLen[u8AdcIdx])
        {
            u8Length = au8DrLen[u8AdcIdx];
        }
        u32DRAddr = (uint32_t)&ADCx->DR0;
        for (i=0U; i<u8Length; i++)
        {
            pu16AdcVal[i] = RW_MEM16(u32DRAddr);
            u32DRAddr += 2U;
        }
        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Get specified channels' value of the specified ADC unit.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in] u32TargetCh             The channels which's ADC values will be read.
 *                                      This parameter can be a value of @ref ADC_Channel
 *   @arg  ADC_CH0 ~ ADC_CH15:          For M4_ADC1 and M4_ADC2.
 *   @arg  ADC_CH0 ~ ADC_CH19:          For M4_ADC3.
 * @param  [out]  pu16AdcVal            Pointer to an uint16_t type memory which the ADC values to be stored.
 *                                      pu16AdcVal[0] = value of the 1st enabled channel,
 *                                      pu16AdcVal[1] = value of the 2nd enabled channel,
 *                                      pu16AdcVal[2] = value of the 3rd enabled channel,
 * @param  [in]  u8Length               The length of the ADC value to be got.
 * @retval An en_result_t enumeration value.
 *   @arg  Ok:                          No errors occurred.
 *   @arg  ErrorInvalidParameter:      -u32TargetCh = 0.
 *                                     -pu16AdcVal == NULL.
 *                                     -u8Length == 0.
 *                                     -The base address of 'pu16AdcVal' is not 2-byte aligned.
 */
en_result_t ADC_GetChannelData(const M4_ADC_TypeDef *ADCx, uint32_t u32TargetCh, \
                               uint16_t pu16AdcVal[], uint8_t u8Length)
{
    uint8_t  j = 0U;
    uint32_t i = 0U;
    uint32_t u32DRAddr;
    uint32_t au32ChAllMsk[] = {ADC1_CH_ALL, ADC2_CH_ALL, ADC3_CH_ALL};
    en_result_t enRet = ErrorInvalidParameter;

    if ((u32TargetCh != 0U) && (pu16AdcVal != NULL) &&
        IS_ADDRESS_ALIGN_HALFWORD(&pu16AdcVal[0U]) && (u8Length != 0U))
    {
        DDL_ASSERT(IS_ADC_UNIT(ADCx));
        u32TargetCh &= au32ChAllMsk[ADC_IDX(ADCx)];
        u32DRAddr    = (uint32_t)&ADCx->DR0;
        while ((u32TargetCh != 0U) && (u8Length != 0U))
        {
            if ((u32TargetCh & 0x1UL) != 0U)
            {
                pu16AdcVal[j++] = RW_MEM16(u32DRAddr + i * 2U);
                u8Length--;
            }
            u32TargetCh >>= 1U;
            i++;
        }

        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Get a specified channel's value of the specified ADC unit.
 * @param  [in]  ADCx                   Pointer to ADC instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_ADC1:                     ADC unit 1 instance register base.
 *   @arg  M4_ADC2:                     ADC unit 2 instance register base.
 *   @arg  M4_ADC3:                     ADC unit 3 instance register base.
 * @param  [in]  u8ChNum                The index number of ADC channel.
 *                                      The following parameters that from @ref ADC_Channel_Number can be used:
 *   @arg  ADC_CH_NUM_0 ~ ADC_CH_NUM_15: For ADC1 and ADC2.
 *   @arg  ADC_CH_NUM_0 ~ ADC_CH_NUM_19: For ADC3.
 * @retval An uint16_t type value between 0 ~ 4095.
 */
uint16_t ADC_GetValue(const M4_ADC_TypeDef *ADCx, uint8_t u8ChNum)
{
    uint32_t u32DRAddr;
#ifdef __DEBUG
    uint8_t au8DrLen[ADC_UNIT_COUNT] = {ADC1_CH_COUNT, ADC2_CH_COUNT, ADC3_CH_COUNT};
    DDL_ASSERT(u8ChNum < au8DrLen[ADC_IDX(ADCx)]);
#endif
    DDL_ASSERT(IS_ADC_UNIT(ADCx));

    u32DRAddr = (uint32_t)&ADCx->DR0 + ((uint32_t)u8ChNum * 2U);
    return RW_MEM16(u32DRAddr);
}

/**
 * @}
 */

#endif /* DDL_ADC_ENABLE */

/**
 * @}
 */

/**
* @}
*/

/******************************************************************************
 * EOF (not truncated)
 *****************************************************************************/