summaryrefslogtreecommitdiff
path: root/bsp/hc32f4a0/Libraries/HC32F4A0_StdPeriph_Driver/src/hc32f4a0_can.c
blob: d8160d9f4e468d91e4d859dcef242306499e2c54 (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
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
/**
 *******************************************************************************
 * @file  hc32f4a0_can.c
 * @brief This file provides firmware functions to manage the CAN.
 @verbatim
   Change Logs:
   Date             Author          Notes
   2020-06-12       Wuze            First version
   2020-08-10       Wuze            Refined CAN_GetStatusVal()
   2020-12-14       Wuze            1. Fixed a bug of CAN_TTC_TransData(); Refined CAN_SBTConfig()
                                    2. Modified comment of structure stc_can_bt_cfg_t
 @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_can.h"
#include "hc32f4a0_utility.h"

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

/**
 * @defgroup DDL_CAN CAN
 * @brief CAN Driver Library
 * @{
 */

#if (DDL_CAN_ENABLE == DDL_ON)

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

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

/**
 * @defgroup CAN_Configuration_Bit_Mask CAN Configuration Bit Mask
 * @{
 */
#define CAN_LB_MODE_MSK                     (CAN_CFG_STAT_LBMI | CAN_CFG_STAT_LBME)
#define CAN_TRANS_MODE_MSK                  (CAN_CFG_STAT_TPSS | CAN_CFG_STAT_TSSS)
#define CAN_TTC_FLAG_CLR_MSK                (CAN_TTC_FLAG_TTI | CAN_TTC_FLAG_WTI)
/**
 * @}
 */

/**
 * @defgroup CAN_Register_Bit_Band CAN 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 CAN_Check_Parameters_Validity CAN check parameters validity
 * @{
 */
#define IS_CAN_UNIT(x)                                                         \
(   ((x) == M4_CAN1)                            ||                             \
    ((x) == M4_CAN2))

#define IS_CAN_SBT_SEG1(x)                                                     \
(   ((x) >= 2U)                                 &&                             \
    ((x) <= 65U))

#define IS_CAN_SBT_SEG2(x)                                                     \
(   ((x) >= 1U)                                 &&                             \
    ((x) <= 8U))

#define IS_CAN_FD_SBT_SEG2(x)                                                  \
(   ((x) >= 1U)                                 &&                             \
    ((x) <= 32U))

#define IS_CAN_SBT_SJW(x)                                                      \
(   ((x) >= 1U)                                 &&                             \
    ((x) <= 16U))

#define IS_CAN_SBT_PRESC(x)                                                    \
(   ((x) >= 1U)                                 &&                             \
    ((x) <= 256U))

#define IS_CAN_FBT_SEG1(x)                                                     \
(   ((x) >= 2U)                                 &&                             \
    ((x) <= 17U))

#define IS_CAN_FBT_SEG2(x)                                                     \
(   ((x) >= 1U)                                 &&                             \
    ((x) <= 8U))

#define IS_CAN_FBT_SJW(x)                                                      \
(   ((x) >= 1U)                                 &&                             \
    ((x) <= 8U))

#define IS_CAN_FBT_PRESC(x)                                                    \
(   ((x) >= 1U)                                 &&                             \
    ((x) <= 256U))

#define IS_CAN_SBT_PRIO_MODE(x)                                                \
(   ((x) == CAN_STB_PRIO_FIFO)                  ||                             \
    ((x) == CAN_STB_PRIO_ID))

#define IS_CAN_IDE(x)                                                          \
(   ((x) == 0U)                                 ||                             \
    ((x) == 1U))

#define IS_CAN_SELF_ACK_CMD(x)                                                 \
(   ((x) == CAN_SELF_ACK_DISABLE)               ||                             \
    ((x) == CAN_SELF_ACK_ENABLE))

#define IS_CAN_TB_TYPE(x)                                                      \
(   ((x) == CAN_BUF_PTB)                        ||                             \
    ((x) == CAN_BUF_STB))

#define IS_CAN_STB_TX_CTRL(x)                                                  \
(   ((x) == CAN_STB_TRANS_ALL)                  ||                             \
    ((x) == CAN_STB_TRANS_ONE))

#define IS_CAN_WORK_MODE(x)                                                    \
(   ((x) <= CAN_MODE_ELB_SILENT))

#define IS_CAN_TRANS_MODE(x)                                                   \
(   ((x) == CAN_TRANS_PTB_STB_AUTO_RETX)        ||                             \
    ((x) == CAN_TRANS_PTB_SSHOT)                ||                             \
    ((x) == CAN_TRANS_STB_SSHOT)                ||                             \
    ((x) == CAN_TRANS_PTB_STB_SSHOT))

#define IS_CAN_RB_STORE_SEL(x)                                                 \
(   ((x) == CAN_RB_STORE_CORRECT_DATA)          ||                             \
    ((x) == CAN_RB_STORE_ALL_DATA))

#define IS_CAN_RB_OVF_OP(x)                                                    \
(   ((x) == CAN_RB_OVF_SAVE_NEW)                ||                             \
    ((x) == CAN_RB_OVF_DISCARD_NEW))

#define IS_CAN_RBS_FULL_WARN_LIMIT(x)                                          \
(   ((x) >= CAN_RBS_WARN_LIMIT_MIN)             &&                             \
    ((x) <= CAN_RBS_WARN_LIMIT_MAX))

#define IS_CAN_ERR_WARN_LIMIT(x)                                               \
(   ((x) < 16U))

#define IS_CAN_AF_MSK_TYPE(x)                                                  \
(   ((x) == CAN_AF_MSK_STD_EXT)                 ||                             \
    ((x) == CAN_AF_MSK_STD)                     ||                             \
    ((x) == CAN_AF_MSK_EXT))

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

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

#define IS_CAN_FD_MODE(x)                                                      \
(   ((x) == CAN_FD_MODE_BOSCH)                  ||                             \
    ((x) == CAN_FD_MODE_ISO_11898))

#define IS_CAN_FD_SSP(x)                                                       \
(   ((x) < 128U))

#define IS_CAN_TDC_CMD(x)                                                      \
(   ((x) == CAN_FD_TDC_DISABLE)                 ||                             \
    ((x) == CAN_FD_TDC_ENABLE))

#define IS_CAN_TTC_TB_MODE(x)                                                  \
(   ((x) == CAN_TTC_TB_MODE_NORMAL)             ||                             \
    ((x) == CAN_TTC_TB_MODE_PTR))

#define IS_CAN_TTC_TBS(x)                                                      \
(   ((x) <= CAN_TTC_TBS_STB3))

#define IS_CAN_TTC_TRIG_TYPE(x)                                                \
(   ((x) == CAN_TTC_TRIG_IMMED_TRIG)            ||                             \
    ((x) == CAN_TTC_TRIG_TIME_TRIG)             ||                             \
    ((x) == CAN_TTC_TRIG_SSHOT_TRANS_TRIG)      ||                             \
    ((x) == CAN_TTC_TRIG_TRANS_START_TRIG)      ||                             \
    ((x) == CAN_TTC_TRIG_TRANS_STOP_TRIG))

#define IS_CAN_TTC_NTU_PRESC(x)                                                \
(   ((x) == CAN_TTC_NTU_PRESC_1)                ||                             \
    ((x) == CAN_TTC_NTU_PRESC_2)                ||                             \
    ((x) == CAN_TTC_NTU_PRESC_4)                ||                             \
    ((x) == CAN_TTC_NTU_PRESC_8))

#define IS_CAN_TTC_TX_EN_WINDOW(x)                                             \
(   (x) <= 16U)

/**
 * @}
 */

/**
 * @defgroup CAN_Miscellaneous_Macros CAN Miscellaneous Macros
 * @{
 */
#define CAN_RBS_CNT                         (8U)                /*!< Each CAN unit has 8 receive buffer slots. */
#define CAN_RBS_WARN_LIMIT_MIN              (1U)
#define CAN_RBS_WARN_LIMIT_MAX              (CAN_RBS_CNT)
/**
 * @}
 */

/**
 * @}
 */

/*******************************************************************************
 * 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 CAN_Global_Functions CAN Global Functions
 * @{
 */

/**
 * @brief  Initializes the specified CAN peripheral according to the specified parameters
 *         in the stc_can_init_t type structure.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  pstcInit               Pointer to a stc_can_init_t structure value that
 *                                      contains the configuration information for the CAN.
 * @retval An en_result_t enumeration type value.
 *   @arg  Ok:                          No error occurred.
 *   @arg  ErrorInvalidParameter:       pstcInit == NULL
 */
en_result_t CAN_Init(M4_CAN_TypeDef *CANx, const stc_can_init_t *pstcInit)
{
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcInit != NULL)
    {
#ifdef __DEBUG
        DDL_ASSERT(IS_FUNCTIONAL_STATE(pstcInit->enCANFDCmd));
        if (pstcInit->enCANFDCmd == Disable)
        {
            DDL_ASSERT(IS_CAN_SBT_SEG2(pstcInit->stcSBT.u32SEG2));
        }
        else
        {
            DDL_ASSERT(IS_CAN_FD_SBT_SEG2(pstcInit->stcSBT.u32SEG2));
        }
#endif
        /* Software reset. */
        CAN_SWReset(CANx);
        /* Defines slow bit time. */
        (void)CAN_SBTConfig(CANx, &pstcInit->stcSBT);
        /* Specifies STB priority mode. */
        CAN_SetSTBPrioMode(CANx, pstcInit->u8STBPrioMode);
        /* Configures acceptance filters. */
        (void)CAN_AFConfig(CANx, pstcInit->u16AFSel, pstcInit->pstcAFCfg);
        /* Configures CAN-FD if enabled. */
        CAN_FD_Cmd(CANx, Disable);
        if (pstcInit->enCANFDCmd == Enable)
        {
            (void)CAN_FD_Config(CANx, &pstcInit->stcFDCfg);
            CAN_FD_Cmd(CANx, Enable);
        }

        /* CAN bus enters normal communication mode. */
        CAN_EnterNormalComm(CANx);
        /* Specifies work mode. */
        CAN_SetWorkMode(CANx, pstcInit->u8WorkMode);
        /* Specifies transmission mode. */
        CAN_SetTransMode(CANx, pstcInit->u8TransMode);

        CAN_SetRBStoreSel(CANx, pstcInit->u8RBStoreSel);
        CAN_SetRBSWarnLimit(CANx, pstcInit->u8RBSWarnLimit);
        CAN_SetErrWarnLimit(CANx, pstcInit->u8ErrWarnLimit);
        /* Specifies the operation when receiving buffer overflow. */
        CAN_SetRBOvfOp(CANx, pstcInit->u8RBOvfOp);
        /* Enable acceptance filters that configured before. */
        SET_REG16_BIT(CANx->ACFEN, pstcInit->u16AFSel);

        /* Enable or disable self-ACK. */
        DDL_ASSERT(IS_CAN_SELF_ACK_CMD(pstcInit->u8SelfACKCmd));
        MODIFY_REG8(CANx->RCTRL, CAN_RCTRL_SACK, pstcInit->u8SelfACKCmd);

        /* Clear all status flags. */
        CAN_ClrStatus(CANx, CAN_FLAG_ALL);

        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Set a default value for CAN initialization structure. \
 *         Slow bit time configuration:
 *         Based on 40MHz CAN clock, TQ clock is CAN clock divided by 4. \
 *         Bit rate 500Kbps, 1 bit time is 20TQs, sample point is 80%.
 *         CAN-FD bit time configuration:
 *         Based on 40MHz CAN clock, TQ clock is CAN clock divided by 1. \
 *         Bit rate 2Mbps, 1 bit time is 20TQs, primary sample point is 80%, \
 *         secondary sample point is 80%.
 * @param  [in]  pstcInit               Pointer to a stc_can_init_t structure value that
 *                                      contains the configuration information for the CAN unit.
 * @retval An en_result_t enumeration type value.
 *   @arg  Ok:                          No error occurred.
 *   @arg  ErrorInvalidParameter:       pstcInit == NULL.
 */
en_result_t CAN_StructInit(stc_can_init_t *pstcInit)
{
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcInit != NULL)
    {
        pstcInit->u8WorkMode     = CAN_MODE_NORMAL;
        pstcInit->u8TransMode    = CAN_TRANS_PTB_STB_AUTO_RETX;
        pstcInit->u8STBPrioMode  = CAN_STB_PRIO_FIFO;
        pstcInit->u8RBSWarnLimit = 6U;
        pstcInit->u8ErrWarnLimit = 7U;
        pstcInit->u16AFSel       = CAN_AF1;
        pstcInit->u8RBStoreSel   = CAN_RB_STORE_CORRECT_DATA;
        pstcInit->u8RBOvfOp      = CAN_RB_OVF_DISCARD_NEW;
        pstcInit->u8SelfACKCmd   = CAN_SELF_ACK_DISABLE;
        pstcInit->pstcAFCfg      = NULL;
        /*
         * Synchronization Segment(SS): Fixed as 1TQ
         * Propagation Time Segment(PTS) and Phase Buffer Segment 1(PBS1): 15TQs
         * Phase Buffer Segment 2(PBS2): 4TQs
         *
         * Field 'S_SEG_1' in register CAN_SBT contains SS, PTS and PBS1.
         * Field 'S_SEG_2' in register CAN_SBT only contains PBS2.
         * Sample point = (SS + PTS + PBS1) / (SS + PTS + PBS1 + PBS2)
         *              = (1 + 15) / (1 + 15 + 4)
         *              = 80%.
         */
        pstcInit->stcSBT.u32SEG1 = 1U + 15U;
        pstcInit->stcSBT.u32SEG2 = 4U;
        pstcInit->stcSBT.u32SJW  = 2U;
        pstcInit->stcSBT.u32Prescaler = 4U;

        /*
         * Set enCANFDCmd as Enable if CAN-FD is needed.
         *
         * u8TDCCmd: Enalbe(CAN_FD_TDC_ENABLE) or disable(CAN_FD_TDC_DISABLE) transmitter delay compensation.
         * u8TDCSSP: The position(TQs) of secondary sample point.
         *
         * Primary sample point: u32SEG1 / (u32SEG1 + u32SEG2) = 80%
         * Secondary sample point: u8TDCSSP / (u32SEG1 + u32SEG2) = 80%
         */
        pstcInit->enCANFDCmd = Disable;
        (void)CAN_FD_StructInit(&pstcInit->stcFDCfg);

        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  De-initializes the CAN peripheral. Reset the registers of the specified CAN unit.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval None
 */
void CAN_DeInit(M4_CAN_TypeDef *CANx)
{
    uint8_t i;

    DDL_ASSERT(IS_CAN_UNIT(CANx));

    CLEAR_REG8_BIT(CANx->CFG_STAT, CAN_CFG_STAT_RESET);
    for (i=0U; i<2U; i++)
    {
        WRITE_REG8(CANx->CFG_STAT, 0x80U);
        WRITE_REG8(CANx->TCMD, 0x00U);
        WRITE_REG8(CANx->TCTRL, 0x90U);
        WRITE_REG8(CANx->RCTRL, 0x10U);
        WRITE_REG8(CANx->RTIE, 0xFEU);
        WRITE_REG8(CANx->RTIF, 0xFFU);
        WRITE_REG8(CANx->ERRINT, 0xD5U);
        WRITE_REG8(CANx->LIMIT, 0x1BU);
        WRITE_REG32(CANx->SBT, 0x01020203U);
        WRITE_REG32(CANx->FBT, 0x01020203U);
        WRITE_REG8(CANx->EALCAP, 0x00U);
        WRITE_REG8(CANx->TDC, 0x00U);
        WRITE_REG8(CANx->RECNT, 0x00U);
        WRITE_REG8(CANx->TECNT, 0x00U);
        WRITE_REG8(CANx->ACFCTRL, 0x00U);
        WRITE_REG8(CANx->ACFEN, 0x01U);
        WRITE_REG8(CANx->TBSLOT, 0x00U);
        WRITE_REG8(CANx->TTCFG, 0xD8U);
        WRITE_REG16(CANx->TRG_CFG, 0x00U);
        WRITE_REG16(CANx->TT_TRIG, 0x00U);
        WRITE_REG16(CANx->TT_WTRIG, 0x00U);
        SET_REG8_BIT(CANx->CFG_STAT, CAN_CFG_STAT_RESET);
    }
}

/**
 * @brief  Software reset the specified CAN unit. \
           Software reset is a partial reset and CANNOT reset all registers. \
           Some registers need software reset before written.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval None
 */
void CAN_SWReset(M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    SET_REG8_BIT(CANx->CFG_STAT, CAN_CFG_STAT_RESET);
}

/**
 * @brief  Set the CAN node to enter the normal communication mode.
 *         When this state is set, it takes 11 CAN bit time for this node to participate in communication.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval None
 */
void CAN_EnterNormalComm(M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    CLEAR_REG8_BIT(CANx->CFG_STAT, CAN_CFG_STAT_RESET);
}

/**
 * @brief  Specifies work mode for the specified CAN unit.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8WorkMode             Work mode of CAN.
 *                                      This parameter can be a value of @ref CAN_Work_Mode
 *   @arg  CAN_MODE_NORMAL:             Normal work mode.
 *   @arg  CAN_MODE_SILENT:             Silent work mode. Prohibit data transmission.
 *   @arg  CAN_MODE_ILB:                Internal loopback mode, just for self-test while developing.
 *   @arg  CAN_MODE_ELB:                External loopback mode, just for self-test while developing.
 *   @arg  CAN_MODE_ELB_SILENT:         External loppback silent mode, just for self-test while developing. \
 *                                      It is forbidden to respond to received frames and error frames, but data can be transmitted.
 * @retval None
 * @note Call this function when CFG_STAT.RESET is 0.
 */
void CAN_SetWorkMode(M4_CAN_TypeDef *CANx, uint8_t u8WorkMode)
{
    uint8_t u8CFGSTAT = 0U;
    uint8_t u8TCMD    = 0U;

    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_WORK_MODE(u8WorkMode));

    switch (u8WorkMode)
    {
        case CAN_MODE_SILENT:
            u8TCMD    = CAN_TCMD_LOM;
            break;
        case CAN_MODE_ILB:
            u8CFGSTAT = CAN_CFG_STAT_LBMI;
            break;
        case CAN_MODE_ELB:
            u8CFGSTAT = CAN_CFG_STAT_LBME;
            break;
        case CAN_MODE_ELB_SILENT:
            u8TCMD    = CAN_TCMD_LOM;
            u8CFGSTAT = CAN_CFG_STAT_LBME;
            break;
        case CAN_MODE_NORMAL:
        default:
            break;
    }

    MODIFY_REG8(CANx->CFG_STAT, CAN_LB_MODE_MSK, u8CFGSTAT);
    MODIFY_REG8(CANx->TCMD, CAN_TCMD_LOM, u8TCMD);
}

/**
 * @brief  Specifies transmission mode for the specified CAN unit.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8TransMode            PTB/STB transmission mode.
 *                                      This parameter can be a value of @ref CAN_Trans_Mode
 *   @arg  CAN_TRANS_PTB_STB_AUTO_RETX: Both PTB and STB automatically retransmit.
 *   @arg  CAN_TRANS_PTB_SSHOT:         PTB single shot transmit.
 *   @arg  CAN_TRANS_STB_SSHOT:         STB single shot transmit.
 *   @arg  CAN_TRANS_PTB_STB_SSHOT:     STB and PTB both single shot transmit.
 * @retval None
 * @note Call this function when CFG_STAT.RESET is 0.
 */
void CAN_SetTransMode(M4_CAN_TypeDef *CANx, uint8_t u8TransMode)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_TRANS_MODE(u8TransMode));
    MODIFY_REG8(CANx->CFG_STAT, CAN_TRANS_MODE_MSK, u8TransMode);
}

/**
 * @brief  Specifies STB transmission priority mode for the specified CAN unit.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8STBPrioMode          STB transmission priority mode.
 *                                      This parameter can be a value of @ref CAN_STB_Priority_Mode
 *   @arg  CAN_STB_PRIO_FIFO:           Data first in first be transmitted.
 *   @arg  CAN_STB_PRIO_ID :            Data with smallest ID first be transmitted.
 * @retval None
 * @note Whatever the priority mode of STB is, PTB always has the highest priority.
 */
void CAN_SetSTBPrioMode(M4_CAN_TypeDef *CANx, uint8_t u8STBPrioMode)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_SBT_PRIO_MODE(u8STBPrioMode));
    MODIFY_REG8(CANx->TCTRL, CAN_TCTRL_TSMODE, u8STBPrioMode);
}

/**
 * @brief  Specifies the receive buffer store selection for specified CAN unit.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8RBStoreSel           Receive buffer store selection.
 *                                      This parameter can be a value of @ref CAN_RB_Store_Selection
 *   @arg  CAN_RB_STORE_CORRECT_DATA:   Receive buffer stores correct data frames only.
 *   @arg  CAN_RB_STORE_ALL_DATA:       Receive buffer stores all data frames, includes error data.
 * @retval None
 * @note Call this function when CFG_STAT.RESET is 0.
 */
void CAN_SetRBStoreSel(M4_CAN_TypeDef *CANx, uint8_t u8RBStoreSel)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_RB_STORE_SEL(u8RBStoreSel));
    MODIFY_REG8(CANx->RCTRL, CAN_RCTRL_RBALL, u8RBStoreSel);
}

/**
 * @brief  Specifies the operation when receiving buffer overflow.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8RBOvfOperation       Operation when receive buffer overflow.
 *                                      This parameter can be a value of @ref CAN_RB_Overflow_Operation
 *   @arg  CAN_RB_OVF_SAVE_NEW:         Saves the newly received data and the first received data will be overwritten.
 *   @arg  CAN_RB_OVF_DISCARD_NEW:      Discard the newly received data.
 * @retval None
 */
void CAN_SetRBOvfOp(M4_CAN_TypeDef *CANx, uint8_t u8RBOvfOperation)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_RB_OVF_OP(u8RBOvfOperation));
    MODIFY_REG8(CANx->RCTRL, CAN_RCTRL_ROM, u8RBOvfOperation);
}

/**
 * @brief  Enable or disable the specified interrupts of the specified CAN unit.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u32IntType             Interrupt type of CAN. Set this parameter to 0xFFFFFFFF to select all the interrupts of CAN.
 *                                      This parameter can be values of @ref CAN_Interrupt_Type
 *   @arg  CAN_INT_ERR_INT:             Register bit RTIE.EIE. Error interrupt.
 *   @arg  CAN_INT_STB_TRANS_OK:        Register bit RTIE.TSIE. Secondary transmit buffer was transmitted successfully.
 *   @arg  CAN_INT_PTB_TRANS_OK:        Register bit RTIE.TPIE. Primary transmit buffer was transmitted successfully.
 *   @arg  CAN_INT_RB_ALMOST_FULL:      Register bit RTIE.RAFIE. The number of filled RB slot is greater than or equal to the LIMIT.AFWL setting value.
 *   @arg  CAN_INT_RB_FIFO_FULL:        Register bit RTIE.RFIE. The FIFO of receive buffer is full.
 *   @arg  CAN_INT_RX_OVERRUN:          Register bit RTIE.ROIE. Receive buffers are full and there is a further message to be stored.
 *   @arg  CAN_INT_RX:                  Register bit RTIE.RIE. Received a valid data frame or remote frame.
 *   @arg  CAN_INT_BUS_ERR:             Register bit ERRINT.BEIE. Arbitration lost caused bus error
 *   @arg  CAN_INT_ARB_LOST:            Register bit ERRINT.ALIE. Arbitration lost.
 *   @arg  CAN_INT_ERR_PASSIVE:         Register bit ERRINT.EPIE. A change from error-passive to error-active or error-active to error-passive has occurred.
 * @param  [in]  enNewState             An en_functional_state_t enumeration type value.
 *   @arg  Enable:                      Enable the specified interrupts.
 *   @arg  Disable:                     Disable the specified interrupts.
 * @retval None
 */
void CAN_IntCmd(M4_CAN_TypeDef *CANx, uint32_t u32IntType, en_functional_state_t enNewState)
{
    uint8_t u8RTIE;
    uint8_t u8ERRINT;

    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    u32IntType &= CAN_INT_ALL;
    u8RTIE      = (uint8_t)u32IntType;
    u8ERRINT    = (uint8_t)(u32IntType >> 8U);

    if (enNewState == Enable)
    {
        SET_REG8_BIT(CANx->RTIE, u8RTIE);
        SET_REG8_BIT(CANx->ERRINT, u8ERRINT);
    }
    else
    {
        CLEAR_REG8_BIT(CANx->RTIE, u8RTIE);
        CLEAR_REG8_BIT(CANx->ERRINT, u8ERRINT);
    }
}

/**
 * @brief  Configures slow bit timing(SBT).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  pstcCfg                Pointer to a stc_can_bt_cfg_t structure value that
 *                                      contains the configuration information for SBT.
 *   @arg  u32SEG1:                     TQs of segment 1. Contains synchronization segment, \
 *                                      propagation time segment and phase buffer segment 1.
 *   @arg  u32SEG2:                     TQs of segment 2. Phase buffer segment 2.
 *   @arg  u32SJW:                      TQs of synchronization jump width.
 *   @arg  u32Prescaler:                Range [1, 256].
 * @retval An en_result_t enumeration type value.
 *   @arg  Ok:                          No error occurred.
 *   @arg  ErrorInvalidParameter:       pstcCfg == NULL.
 * @note 1. Restrictions: u32SEG1 >= u32SEG2 + 1, u32SEG2 >= u32SJW.
 * @note 2. TQ = u32Prescaler / CANClock.
 * @note 3. Slow bit time = (u32SEG1 + u32SEG2) * TQ.
 * @note 4. Call this function when CFG_STAT.RESET is 1.
 */
en_result_t CAN_SBTConfig(M4_CAN_TypeDef *CANx, const stc_can_bt_cfg_t *pstcCfg)
{
    en_result_t enRet = ErrorInvalidParameter;
#ifdef __DEBUG
    uint32_t u32Addr;
    uint32_t u32BitPos = 0U;
#endif
    if (pstcCfg != NULL)
    {
        DDL_ASSERT(IS_CAN_UNIT(CANx));
        DDL_ASSERT(IS_CAN_SBT_SEG1(pstcCfg->u32SEG1));
        DDL_ASSERT(IS_CAN_SBT_SJW(pstcCfg->u32SJW));
        DDL_ASSERT(IS_CAN_SBT_PRESC(pstcCfg->u32Prescaler));
#ifdef __DEBUG
        if (CANx == M4_CAN2)
        {
            u32BitPos = 1U;
        }
        /* If CAN-FD enabled. */
        u32Addr = (uint32_t)&M4_PERIC->CAN_SYCTLREG;
        if (BIT_BAND(u32Addr, u32BitPos) == (uint32_t)Disable)
        {
            DDL_ASSERT(IS_CAN_SBT_SEG2(pstcCfg->u32SEG2));
        }
        else
        {
            DDL_ASSERT(IS_CAN_FD_SBT_SEG2(pstcCfg->u32SEG2));
        }
#endif
        WRITE_REG32(CANx->SBT, ((pstcCfg->u32SEG1 - 2U) | \
                                ((pstcCfg->u32SEG2 - 1U) << CAN_SBT_S_SEG_2_POS) | \
                                ((pstcCfg->u32SJW - 1U) << CAN_SBT_S_SJW_POS)    | \
                                ((pstcCfg->u32Prescaler - 1U) << CAN_SBT_S_PRESC_POS)));
        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Get arbitration lost position of a frame.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval A uint8_t type value of arbitration lost position.
 */
uint8_t CAN_GetArbLostPos(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return (READ_REG8_BIT(CANx->EALCAP, CAN_EALCAP_ALC));
}

/**
 * @brief  Get error type.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint8_t value of error type. It's can be a value of @ref CAN_Error_Type
 *   @arg  CAN_ERR_NO:                  No error.
 *   @arg  CAN_ERR_BIT:                 Error is bit error.
 *   @arg  CAN_ERR_FORM:                Error is form error.
 *   @arg  CAN_ERR_STUFF:               Error is stuff error.
 *   @arg  CAN_ERR_ACK:                 Error is ACK error.
 *   @arg  CAN_ERR_CRC:                 Error is CRC error.
 *   @arg  CAN_ERR_OTHER:               Error is other error.
 */
uint8_t CAN_GetErrType(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return (READ_REG8_BIT(CANx->EALCAP, CAN_EALCAP_KOER));
}

/**
 * @brief  Set receive buffer slots full warning limit.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in] u8RBSWarnLimit:         Receive buffer slots full warning limit. Rang is [1, 8].
 *                                      Each CAN unit has 8 receive buffer slots. When the number of received frames \
 *                                      reaches the value specified by parameter 'u8RBSWarnLimit', register bit RTIF.RAFIF is set and \
 *                                      the interrupt occurred if it was enabled.
 * @retval None
 */
void CAN_SetRBSWarnLimit(M4_CAN_TypeDef *CANx, uint8_t u8RBSWarnLimit)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_RBS_FULL_WARN_LIMIT(u8RBSWarnLimit));

    u8RBSWarnLimit <<= CAN_LIMIT_AFWL_POS;
    MODIFY_REG8(CANx->LIMIT, CAN_LIMIT_AFWL, u8RBSWarnLimit);
}

/**
 * @brief  Set error warning limit.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8ErrWarnLimit         Programmable error warning limit. Range is [0, 15].
 *                                      Error warning limit = (u8ErrWarnLimit + 1) * 8.
 * @retval None
 */
void CAN_SetErrWarnLimit(M4_CAN_TypeDef *CANx, uint8_t u8ErrWarnLimit)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_ERR_WARN_LIMIT(u8ErrWarnLimit));
    MODIFY_REG8(CANx->LIMIT, CAN_LIMIT_EWL, u8ErrWarnLimit);
}

/**
 * @brief  Get receive error count(REC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint8_t type value of REC.
 */
uint8_t CAN_GetREC(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return (CANx->RECNT);
}

/**
 * @brief  Get transmission error count(TEC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint8_t type value of TEC.
 */
uint8_t CAN_GetTEC(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return (CANx->TECNT);
}

/**
 * @brief  Clear receive error count(REC) and transmission error count(TEC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval None
 * @note Set CFG_STAT.BUSOFF to 1 to clear REC and TEC. ONLY for TEST mode!
 */
void CAN_ClrErrCount(M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    SET_REG8_BIT(CANx->CFG_STAT, CAN_CFG_STAT_BUSOFF);
}

/**
 * @brief  Configures acceptance filter. Set ID and ID mask for the specified acceptance filters.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u16AFSel               Acceptance filter enable bit.
 *                                      This parameter can be values of @ref CAN_AF
 *   @arg CAN_AF1 ~ CAN_AF16
 * @param  [in]  pstcAFCfg              Points to a stc_can_af_cfg_t structure type array which contains ID and ID mask
 *                                      values for the acceptance filters specified by parameter u16AFSel.
 * @retval An en_result_t enumeration type value.
 *   @arg  Ok:                          No error occurred.
 *   @arg  ErrorInvalidParameter:      -u16AFSel == 0U.
 *                                     -pstcAFCfg == NULL.
 * @note Call this function when CFG_STAT.RESET is 1.
 */
en_result_t CAN_AFConfig(M4_CAN_TypeDef *CANx, uint16_t u16AFSel, const stc_can_af_cfg_t pstcAFCfg[])
{
    uint8_t u8AFAddr = 0U;
    uint8_t i = 0U;
    en_result_t enRet = ErrorInvalidParameter;

    if ((u16AFSel != 0U) && (pstcAFCfg != NULL))
    {
        DDL_ASSERT(IS_CAN_UNIT(CANx));

        while (u16AFSel != 0U)
        {
            if ((u16AFSel & (uint16_t)0x1U) != 0U)
            {
                DDL_ASSERT(IS_CAN_AF_MSK_TYPE(pstcAFCfg[i].u32MskType));

                WRITE_REG8(CANx->ACFCTRL, u8AFAddr);
                WRITE_REG32(CANx->ACF, pstcAFCfg[i].u32ID);
                SET_REG8_BIT(CANx->ACFCTRL, CAN_ACFCTRL_SELMASK);
                WRITE_REG32(CANx->ACF, pstcAFCfg[i].u32IDMsk | pstcAFCfg[i].u32MskType);
                i++;
            }

            u16AFSel >>= 1U;
            u8AFAddr++;
        }

        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Enable or disable acceptance filter.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u16AFSel               Acceptance filter enable bit.
 *                                      This parameter can be values of @ref CAN_AF
 *   @arg CAN_AF1 ~ CAN_AF16
 * @param  [in]  enNewState             An en_functional_state_t enumeration type value.
 *   @arg Enable:                       Enable the acceptance filters that specified by parameter u16AFSel.
 *   @arg Disable:                      Disable the acceptance filters that specified by parameter u16AFSel.
 * @retval None
 */
void CAN_AFCmd(M4_CAN_TypeDef *CANx, uint16_t u16AFSel, en_functional_state_t enNewState)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    if (enNewState == Enable)
    {
        SET_REG16_BIT(CANx->ACFEN, u16AFSel);
    }
    else
    {
        CLEAR_REG16_BIT(CANx->ACFEN, u16AFSel);
    }
}

/**
 * @brief  Get the transmit buffer which is being transmitted.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint8_t type value of transmit buffer type. It can be a value of @ref CAN_Transmit_Buffer_Type
 *   @arg  CAN_BUF_PTB:                 The transmit buffer which is being transmitted is PTB.
 *   @arg  CAN_BUF_STB:                 The transmit buffer which is being transmitted is STB.
 *   @arg  0xFFU:                       No transmit buffer is being transmitted.
 */
uint8_t CAN_GetTBType(const M4_CAN_TypeDef *CANx)
{
    uint8_t u8Tmp;
    uint8_t u8Ret;

    u8Tmp = CANx->TCMD;
    if ((u8Tmp & CAN_TCMD_TPE) != 0U)
    {
        u8Ret = CAN_BUF_PTB;
    }
    else if ((u8Tmp & (CAN_TCMD_TSALL | CAN_TCMD_TSONE)) != 0U)
    {
        u8Ret = CAN_BUF_STB;
    }
    else
    {
        u8Ret = 0xFFU;
    }

    return u8Ret;
}

/**
 * @brief  Abort the transmission of the specified transmit buffer that ready but not started.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8TBType               The transmit buffer to be aborted.
 *                                      This parameter can be a value of @ref CAN_Transmit_Buffer_Type
 *   @arg  CAN_BUF_PTB:                 Abort PTB transmission that ready but not started.
 *   @arg  CAN_BUF_STB:                 Abort STB transmission that ready but not started.
 * @retval None
 * @note Call this function when CFG_STAT.RESET is 0.
 */
void CAN_AbortTrans(M4_CAN_TypeDef *CANx, uint8_t u8TBType)
{
    uint8_t au8Abort[] = {CAN_TCMD_TPA, CAN_TCMD_TSA};

    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_TB_TYPE(u8TBType));
    SET_REG8_BIT(CANx->TCMD, au8Abort[u8TBType]);
}

/**
 * @brief  Get the common flag's status.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u32Flag                Status flag.
 *                                      This parameter can be a value of @ref CAN_Common_Status_Flag
 *   @arg  CAN_FLAG_BUS_OFF:            Register bit CFG_STAT.BUSOFF. CAN bus off.
 *   @arg  CAN_FLAG_BUS_TX:             Register bit CFG_STAT.TACTIVE. CAN bus is transmitting.
 *   @arg  CAN_FLAG_BUS_RX:             Register bit CFG_STAT.RACTIVE. CAN bus is receiving.
 *   @arg  CAN_FLAG_RB_OVF:             Register bit RCTRL.ROV. Receive buffer is full and there is a further bit to be stored. At least one data is lost.
 *   @arg  CAN_FLAG_TB_FULL:            Register bit RTIE.TSFF. Transmit buffers are all full: \
 *                                      TTCFG.TTEN == 0 or TCTRL.TTTEM == 0: ALL STB slots are filled. \
 *                                      TTCFG.TTEN == 1 and TCTRL.TTTEM == 1: Transmit buffer that pointed by TBSLOT.TBPTR is filled.
 *   @arg  CAN_FLAG_TRANS_ABORTED:      Register bit RTIF.AIF. Transmit messages requested via TCMD.TPA and TCMD.TSA were successfully canceled.
 *   @arg  CAN_FLAG_ERR_INT:            Register bit RTIF.EIF. The CFG_STAT.BUSOFF bit changes, or the relative relationship between the value of the error counter and the \
 *                                      set value of the ERROR warning limit changes. For example, the value of the error counter changes from less than \
 *                                      the set value to greater than the set value, or from greater than the set value to less than the set value.
 *   @arg  CAN_FLAG_STB_TRANS_OK:       Register bit RTIF.TSIF. STB was transmitted successfully.
 *   @arg  CAN_FLAG_PTB_TRANS_OK:       Register bit RTIF.TPIF. PTB was transmitted successfully.
 *   @arg  CAN_FLAG_RB_ALMOST_FULL:     Register bit RTIF.RAFIF. The number of filled RB slot is greater than or equal to the LIMIT.AFWL setting value.
 *   @arg  CAN_FLAG_RB_FIFO_FULL:       Register bit RTIF.RFIF. The FIFO of receive buffer is full.
 *   @arg  CAN_FLAG_RX_OVERRUN:         Register bit RTIF.ROIF. Receive buffers are all full and there is a further message to be stored.
 *   @arg  CAN_FLAG_RX_OK:              Register bit RTIF.RIF. Received a valid data frame or remote frame.
 *   @arg  CAN_FLAG_BUS_ERR:            Register bit ERRINT.BEIF. Arbitration lost caused bus error.
 *   @arg  CAN_FLAG_ARB_LOST:           Register bit ERRINT.ALIF. Arbitration lost.
 *   @arg  CAN_FLAG_ERR_PASSIVE:        Register bit ERRINT.EPIF. A change from error-passive to error-active or error-active to error-passive has occurred.
 *   @arg  CAN_FLAG_ERR_PASSIVE_NODE:   Register bit ERRINT.EPASS. The node is an error-passive node.
 *   @arg  CAN_FLAG_REACH_WARN_LIMIT:   Register bit ERRINT.EWARN. REC or TEC is greater than or equal to the LIMIT.EWL setting value.
 * @retval An en_flag_status_t enumeration type value.
 *   @arg  Set:                         At least one of the specified status flags is set.
 *   @arg  Reset:                       None of the specified status flags is set.
 */
en_flag_status_t CAN_GetStatus(const M4_CAN_TypeDef *CANx, uint32_t u32Flag)
{
    uint8_t u8CFGSTAT;
    uint8_t u8RCTRL;
    uint8_t u8RTIE;
    uint8_t u8RTIF;
    uint8_t u8ERRINT;
    en_flag_status_t enFlag = Reset;

    DDL_ASSERT(IS_CAN_UNIT(CANx));

    u32Flag  &= CAN_FLAG_ALL;

    u8CFGSTAT = (uint8_t)(u32Flag);
    u8RCTRL   = (uint8_t)(u32Flag & CAN_FLAG_RB_OVF);
    u8RTIE    = (uint8_t)(u32Flag >> 8U);
    u8RTIF    = (uint8_t)(u32Flag >> 16U);
    u8ERRINT  = (uint8_t)(u32Flag >> 24U);

    u8CFGSTAT = READ_REG8_BIT(CANx->CFG_STAT, u8CFGSTAT);
    u8RCTRL   = READ_REG8_BIT(CANx->RCTRL, u8RCTRL);
    u8RTIE    = READ_REG8_BIT(CANx->RTIE, u8RTIE);
    u8RTIF    = READ_REG8_BIT(CANx->RTIF, u8RTIF);
    u8ERRINT  = READ_REG8_BIT(CANx->ERRINT, u8ERRINT);

    if ((u8CFGSTAT != 0U) || (u8RCTRL != 0U) || \
        (u8RTIE != 0U) || (u8RTIF != 0U) || (u8ERRINT != 0U))
    {
        enFlag = Set;
    }

    return enFlag;
}

/**
 * @brief  Clear the common flag's status.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u32Flag                Status flag. Set this parameter to 0xFFFFFFFF to select all sataus flags of CAN.
 *                                      This parameter can be a value of @ref CAN_Common_Status_Flag
 *   @arg  CAN_FLAG_RB_OVF:             Register bit RCTRL.ROV. Receive buffer is full and there is a further bit to be stored. At least one data is lost.
 *   @arg  CAN_FLAG_TRANS_ABORTED:      Register bit RTIF.AIF. Transmit messages requested via TCMD.TPA and TCMD.TSA were successfully canceled.
 *   @arg  CAN_FLAG_ERR_INT:            Register bit RTIF.EIF. The CFG_STAT.BUSOFF bit changes, or the relative relationship between the value of the error counter \
 *                                      and the set value of the ERROR warning limit changes. For example, the value of the error counter changes from less than \
 *                                      the set value to greater than the set value, or from greater than the set value to less than the set value.
 *   @arg  CAN_FLAG_STB_TRANS_OK:       Register bit RTIF.TSIF. STB was transmitted successfully.
 *   @arg  CAN_FLAG_PTB_TRANS_OK:       Register bit RTIF.TPIF. PTB was transmitted successfully.
 *   @arg  CAN_FLAG_RB_ALMOST_FULL:     Register bit RTIF.RAFIF. The number of filled RB slot is greater than or equal to the LIMIT.AFWL setting value.
 *   @arg  CAN_FLAG_RB_FIFO_FULL:       Register bit RTIF.RFIF. The FIFO of receive buffer is full.
 *   @arg  CAN_FLAG_RX_OVERRUN:         Register bit RTIF.ROIF. Receive buffers are all full and there is a further message to be stored.
 *   @arg  CAN_FLAG_RX_OK:              Register bit RTIF.RIF. Received a valid data frame or remote frame.
 *   @arg  CAN_FLAG_BUS_ERR:            Register bit ERRINT.BEIF. Arbitration lost caused bus error.
 *   @arg  CAN_FLAG_ARB_LOST:           Register bit ERRINT.ALIF. Arbitration lost.
 *   @arg  CAN_FLAG_ERR_PASSIVE:        Register bit ERRINT.EPIF. A change from error-passive to error-active or error-active to error-passive has occurred.
 *   @arg  CAN_FLAG_ERR_PASSIVE_NODE:   Register bit ERRINT.EPASS. The node is an error-passive node.
 *   @arg  CAN_FLAG_REACH_WARN_LIMIT:   Register bit ERRINT.EWARN. REC or TEC is greater than or equal to the LIMIT.EWL setting value.
 * @retval None
 */
void CAN_ClrStatus(M4_CAN_TypeDef *CANx, uint32_t u32Flag)
{
    uint8_t u8RTIF;
    uint8_t u8ERRINT;

    DDL_ASSERT(IS_CAN_UNIT(CANx));

    u32Flag &= CAN_FLAG_CLR_MSK;
    u8RTIF   = (uint8_t)(u32Flag >> 16U);
    u8ERRINT = (uint8_t)(u32Flag >> 24U);

    if ((u32Flag & CAN_FLAG_RB_OVF) != 0U)
    {
        SET_REG8_BIT(CANx->RCTRL, CAN_RCTRL_RREL);
    }
    SET_REG8_BIT(CANx->RTIF, u8RTIF);
    SET_REG8_BIT(CANx->ERRINT, u8ERRINT);
}

/**
 * @brief  Get the value of status.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint32_t type value that includes the flowing status flags.
 *   @arg  CAN_FLAG_BUS_OFF:            Register bit CFG_STAT.BUSOFF. CAN bus off.
 *   @arg  CAN_FLAG_BUS_TX:             Register bit CFG_STAT.TACTIVE. CAN bus is transmitting.
 *   @arg  CAN_FLAG_BUS_RX:             Register bit CFG_STAT.RACTIVE. CAN bus is receiving.
 *   @arg  CAN_FLAG_RB_OVF:             Register bit RCTRL.ROV. Receive buffer is full and there is a further bit to be stored. At least one data is lost.
 *   @arg  CAN_FLAG_TB_FULL:            Register bit RTIE.TSFF. Transmit buffers are all full: \
 *                                      TTCFG.TTEN == 0 or TCTRL.TTTEM == 0: ALL STB slots are filled. \
 *                                      TTCFG.TTEN == 1 and TCTRL.TTTEM == 1: Transmit buffer that pointed by TBSLOT.TBPTR is filled.
 *   @arg  CAN_FLAG_TRANS_ABORTED:      Register bit RTIF.AIF. Transmit messages requested via TCMD.TPA and TCMD.TSA were successfully canceled.
 *   @arg  CAN_FLAG_ERR_INT:            Register bit RTIF.EIF. The CFG_STAT.BUSOFF bit changes, or the relative relationship between the value of the error counter and the \
 *                                      set value of the ERROR warning limit changes. For example, the value of the error counter changes from less than \
 *                                      the set value to greater than the set value, or from greater than the set value to less than the set value.
 *   @arg  CAN_FLAG_STB_TRANS_OK:       Register bit RTIF.TSIF. STB was transmitted successfully.
 *   @arg  CAN_FLAG_PTB_TRANS_OK:       Register bit RTIF.TPIF. PTB was transmitted successfully.
 *   @arg  CAN_FLAG_RB_ALMOST_FULL:     Register bit RTIF.RAFIF. The number of filled RB slot is greater than or equal to the LIMIT.AFWL setting value.
 *   @arg  CAN_FLAG_RB_FIFO_FULL:       Register bit RTIF.RFIF. The FIFO of receive buffer is full.
 *   @arg  CAN_FLAG_RX_OVERRUN:         Register bit RTIF.ROIF. Receive buffers are all full and there is a further message to be stored.
 *   @arg  CAN_FLAG_RX_OK:              Register bit RTIF.RIF. Received a valid data frame or remote frame.
 *   @arg  CAN_FLAG_BUS_ERR:            Register bit ERRINT.BEIF. Arbitration lost caused bus error.
 *   @arg  CAN_FLAG_ARB_LOST:           Register bit ERRINT.ALIF. Arbitration lost.
 *   @arg  CAN_FLAG_ERR_PASSIVE:        Register bit ERRINT.EPIF. A change from error-passive to error-active or error-active to error-passive has occurred.
 *   @arg  CAN_FLAG_ERR_PASSIVE_NODE:   Register bit ERRINT.EPASS. The node is an error-passive node.
 *   @arg  CAN_FLAG_REACH_WARN_LIMIT:   Register bit ERRINT.EWARN. REC or TEC is greater than or equal to the LIMIT.EWL setting value.
 */
uint32_t CAN_GetStatusVal(const M4_CAN_TypeDef *CANx)
{
    uint32_t u32RCTRL;
    uint32_t u32RTIE;
    uint32_t u32RTIF;
    uint32_t u32ERRINT;
    uint32_t u32RetVal;

    DDL_ASSERT(IS_CAN_UNIT(CANx));

    u32RetVal  = CANx->CFG_STAT;
    u32RCTRL   = CANx->RCTRL;
    u32RCTRL  &= CAN_FLAG_RB_OVF;
    u32RTIE    = CANx->RTIE;
    u32RTIF    = CANx->RTIF;
    u32ERRINT  = CANx->ERRINT;

    u32RetVal |= (u32RCTRL | (u32RTIE << 8U) | (u32RTIF << 16U) | (u32ERRINT << 24U));
    u32RetVal &= CAN_FLAG_ALL;

    return u32RetVal;
}

/**
 * @brief  Get status(full or empty) of transmit buffer.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint8_t type value of status of transmit buffer. It's can be a value of @ref CAN_TB_Status
 *   @arg  CAN_TB_STAT_EMPTY:           TTCAN is disabled(TTEN == 0): STB is empty.
 *                                      TTCAN is disabled(TTEN == 1) and transmit buffer is specified by TBPTR and TTPTR(TTTBM == 1):
 *                                      PTB and STB are both empty.
 *   @arg  CAN_TB_STAT_LESS_HALF:       TTEN == 0: STB is not less than half full;
 *                                      TTEN == 1 && TTTBM == 1: PTB and STB are neither empty.
 *   @arg  CAN_TB_STAT_MORE_HALF:       TTEN == 0: STB is more than half full;
 *                                      TTEN == 1 && TTTBM == 1: reserved value.
 *   @arg  CAN_TB_STAT_FULL             TTEN == 0: STB is full;
 *                                      TTEN == 1 && TTTBM == 1: PTB and STB are both full.
 */
uint8_t CAN_GetTBFullStatus(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return (READ_REG8_BIT(CANx->TCTRL, CAN_TCTRL_TSSTAT));
}

/**
 * @brief  Get status(full or empty) of receive buffer.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint8_t type value of status of receive buffer. It's can be a value of @ref CAN_RB_Status
 *   @arg  CAN_RB_STAT_EMPTY:           Receive buffer(RB) is empty.
 *   @arg  CAN_RB_STAT_LESS_WARN_LIMIT: RB is not empty, but is less than almost full warning limit.
 *   @arg  CAN_RB_STAT_MORE_WARN_LIMIT: RB is not full, but is more than or equal to almost full warning limit.
 *   @arg  CAN_RB_STAT_FULL:            RB is full.
 */
uint8_t CAN_GetRBFullStatus(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return (READ_REG8_BIT(CANx->TCTRL, CAN_RCTRL_RSTAT));
}

/**
 * @brief  Configures the specified CAN FD according to the specified parameters
 *         in the stc_can_fd_cfg_t type structure.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  pstcCfg                Pointer to a stc_can_fd_cfg_t structure value that
 *                                      contains the configuration information for the CAN FD.
 * @retval An en_result_t enumeration type value.
 *   @arg  Ok:                          No error occurred.
 *   @arg  ErrorInvalidParameter:       pstcCfg == NULL.
 * @note Call this function when CFG_STAT.RESET is 1.
 */
en_result_t CAN_FD_Config(M4_CAN_TypeDef *CANx, const stc_can_fd_cfg_t *pstcCfg)
{
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcCfg != NULL)
    {
        DDL_ASSERT(IS_CAN_UNIT(CANx));
        DDL_ASSERT(IS_CAN_FD_MODE(pstcCfg->u8CANFDMode));
        DDL_ASSERT(IS_CAN_TDC_CMD(pstcCfg->u8TDCCmd));
        DDL_ASSERT(IS_CAN_FD_SSP(pstcCfg->u8TDCSSP));

        DDL_ASSERT(IS_CAN_FBT_SEG1(pstcCfg->stcFBT.u32SEG1));
        DDL_ASSERT(IS_CAN_FBT_SEG2(pstcCfg->stcFBT.u32SEG2));
        DDL_ASSERT(IS_CAN_FBT_SJW(pstcCfg->stcFBT.u32SJW));
        DDL_ASSERT(IS_CAN_FBT_PRESC(pstcCfg->stcFBT.u32Prescaler));

        /* Specifies CAN FD ISO mode. */
        MODIFY_REG8(CANx->TCTRL, CAN_TCTRL_FD_ISO, pstcCfg->u8CANFDMode);

        /*
         * Configures fast bit time.
         * Restrictions: u32SEG1 >= u32SEG2 + 1, u32SEG2 >= u32SJW.
         * TQ = u32Prescaler / CANClock.
         * Fast bit time = (u32SEG1 + u32SEG2) * TQ.
         */
        WRITE_REG32(CANx->FBT, ((pstcCfg->stcFBT.u32SEG1 - 2U) | \
                                ((pstcCfg->stcFBT.u32SEG2 - 1U) << CAN_FBT_F_SEG_2_POS) | \
                                ((pstcCfg->stcFBT.u32SJW - 1U) << CAN_FBT_F_SJW_POS)    | \
                                ((pstcCfg->stcFBT.u32Prescaler - 1U) << CAN_FBT_F_PRESC_POS)));

        /* Specifies the secondary sample point. Number of TQ. */
        MODIFY_REG8(CANx->TDC, CAN_TDC_SSPOFF, pstcCfg->u8TDCSSP);

        /* Enable or disable TDC. */
        MODIFY_REG8(CANx->TDC, CAN_TDC_TDCEN, pstcCfg->u8TDCCmd);
        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Set a default value for the CAN FD configuration structure.
 *         Based on 40MHz CAN clock, TQ clock is CAN clock divided by 1. \
 *         Bit rate 2Mbps, 1 bit time is 20TQs, primary sample point is 80%, \
 *         secondary sample point is 80%.
 * @param  [in]  pstcCfg                Pointer to a stc_can_fd_cfg_t structure value that
 *                                      contains the configuration information for the CAN FD.
 * @retval An en_result_t enumeration type value.
 *   @arg  Ok:                          No error occurred.
 *   @arg  ErrorInvalidParameter:       pstcCfg == NULL.
 */
en_result_t CAN_FD_StructInit(stc_can_fd_cfg_t *pstcCfg)
{
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcCfg != NULL)
    {
        /*
         * u8TDCCmd: Enalbe(CAN_FD_TDC_ENABLE) or disable(CAN_FD_TDC_DISABLE) transmitter delay compensation.
         * u8TDCSSP: The position(TQs) of secondary sample point.
         *
         * Primary sample point: u32SEG1 / (u32SEG1 + u32SEG2) = 80%
         * Secondary sample point: u8TDCSSP / (u32SEG1 + u32SEG2) = 80%
         *
         * u32SEG1:      TQs of segment 1. Contains synchronization segment, \
         *               propagation time segment and phase buffer segment 1.
         * u32SEG2:      TQs of segment 2. Phase buffer segment 2.
         * u32SJW:       TQs of synchronization jump width.
         * u32Prescaler: Range [1, 256].
         */
        pstcCfg->u8CANFDMode = CAN_FD_MODE_BOSCH;
        pstcCfg->u8TDCCmd    = CAN_FD_TDC_ENABLE;
        pstcCfg->u8TDCSSP    = 16U;
        pstcCfg->stcFBT.u32SEG1 = 1U + 15U;
        pstcCfg->stcFBT.u32SEG2 = 4U;
        pstcCfg->stcFBT.u32SJW  = 4U;
        pstcCfg->stcFBT.u32Prescaler = 1U;

        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Enable or disable CAN FD.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  enNewState             An en_functional_state_t enumeration type value.
 *   @arg  Enable:                      Enable CAN FD.
 *   @arg  Disable:                     Disable CAN FD.
 * @retval None
 */
void CAN_FD_Cmd(const M4_CAN_TypeDef *CANx, en_functional_state_t enNewState)
{
    uint32_t u32Addr;
    uint32_t u32BitPos = 0U;

    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    if (CANx == M4_CAN2)
    {
        u32BitPos = 1U;
    }
    u32Addr = (uint32_t)&M4_PERIC->CAN_SYCTLREG;
    BIT_BAND(u32Addr, u32BitPos) = (uint32_t)enNewState;
}

/**
 * @brief  Set a default value for the CAN TTC(time-triggered communication) configuration structure.
 * @param  [in]  pstcCfg                Pointer to a stc_can_ttc_cfg_t structure value that
 *                                      contains the configuration information for the CAN TTC.
 * @retval An en_result_t enumeration type value.
 *   @arg  Ok:                          No error occurred.
 *   @arg  ErrorInvalidParameter:       pstcCfg == NULL.
 */
en_result_t CAN_TTC_StructInit(stc_can_ttc_cfg_t *pstcCfg)
{
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcCfg != NULL)
    {
        pstcCfg->u8TransBufMode   = CAN_TTC_TB_MODE_PTR;
        pstcCfg->u8NTUPrescaler   = CAN_TTC_NTU_PRESC_1;
        pstcCfg->u32RefMsgIDE     = 0U;
        pstcCfg->u32RefMsgID      = 0x0U;
        pstcCfg->u16TrigType      = CAN_TTC_TRIG_SSHOT_TRANS_TRIG;
        pstcCfg->u16TxEnWindow    = 16U;
        pstcCfg->u16TxTrigTime    = 0xFFFFU;
        pstcCfg->u16WatchTrigTime = 0xFFFFU;

        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Configures the specified CAN TTC(time-triggered communication) according to the specified parameters
 *         in the stc_can_ttc_cfg_t type structure.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  pstcCfg                Pointer to a stc_can_ttc_cfg_t structure value that
 *                                      contains the configuration information for the CAN TTC.
 * @retval An en_result_t enumeration type value.
 *   @arg  Ok:                          No error occurred.
 *   @arg  ErrorInvalidParameter:       pstcCfg == NULL.
 */
en_result_t CAN_TTC_Config(M4_CAN_TypeDef *CANx, const stc_can_ttc_cfg_t *pstcCfg)
{
    uint32_t u32RefMsgID;
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcCfg != NULL)
    {
        DDL_ASSERT(IS_CAN_UNIT(CANx));
        DDL_ASSERT(IS_CAN_TTC_TB_MODE(pstcCfg->u8TransBufMode));
        DDL_ASSERT(IS_CAN_TTC_NTU_PRESC(pstcCfg->u8NTUPrescaler));
        DDL_ASSERT(IS_CAN_IDE(pstcCfg->u32RefMsgIDE));
        DDL_ASSERT(IS_CAN_TTC_TRIG_TYPE(pstcCfg->u16TrigType));
        DDL_ASSERT(IS_CAN_TTC_TX_EN_WINDOW(pstcCfg->u16TxEnWindow));

        u32RefMsgID = pstcCfg->u32RefMsgID & ((uint32_t)(~CAN_REF_MSG_REF_IDE));

        /* Specifies transmission buffer mode. */
        MODIFY_REG8(CANx->TCTRL, CAN_TCTRL_TTTBM, pstcCfg->u8TransBufMode);
        /* Specifies Tx_Enable window and trigger type. */
        WRITE_REG16(CANx->TRG_CFG, ((((uint32_t)pstcCfg->u16TxEnWindow-1U) << CAN_TRG_CFG_TEW_POS) | pstcCfg->u16TrigType));
        /* Specifies ID of reference message and its extension bit. */
        WRITE_REG32(CANx->REF_MSG, (((pstcCfg->u32RefMsgIDE << CAN_REF_MSG_REF_IDE_POS) | u32RefMsgID)));
        /* Specifies transmission trigger time. */
        WRITE_REG16(CANx->TT_TRIG, pstcCfg->u16TxTrigTime);
        /* Specifies watch trigger time. */
        WRITE_REG16(CANx->TT_WTRIG, pstcCfg->u16WatchTrigTime);
        /* Specifies NTU prescaler. */
        WRITE_REG8(CANx->TTCFG, pstcCfg->u8NTUPrescaler);

        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Enable or disable time-triggered communication(TTC) of the specified CAN unit.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  enNewState             An en_functional_state_t enumeration type value.
 *   @arg  Enable:                      Enable TTC.
 *   @arg  Disable:                     Disable TTC.
 * @retval None
 * @note Call this function when CFG_STAT.RESET is 0.
 */
void CAN_TTC_Cmd(M4_CAN_TypeDef *CANx, en_functional_state_t enNewState)
{
    uint32_t u32Addr;

    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));
    u32Addr = (uint32_t)&CANx->TTCFG;
    BIT_BAND(u32Addr, CAN_TTCFG_TTEN_POS) = (uint32_t)enNewState;
}

/**
 * @brief  Specifies transmission buffer slot(TBS) which is going to be filled.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8SlotPtr              Transmit-trigger transmit buffer slot.
 *                                      This parameter can be a value of @ref CAN_TTC_TBS_Pointer
 *   @arg  CAN_TTC_TBS_PTB:             TBS pointer points to PTB.
 *   @arg  CAN_TTC_TBS_STB1:            TBS pointer points to STB slot 1.
 *   @arg  CAN_TTC_TBS_STB2:            TBS pointer points to STB slot 2.
 *   @arg  CAN_TTC_TBS_STB3:            TBS pointer points to STB slot 3.
 * @retval None
 */
void CAN_TTC_SetTBSToBeFilled(M4_CAN_TypeDef *CANx, uint8_t u8SlotPtr)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_TTC_TBS(u8SlotPtr));
    MODIFY_REG8(CANx->TBSLOT, CAN_TBSLOT_TBPTR, u8SlotPtr);
}

/**
 * @brief  Marks the transmission buffer slot(TBS) as filled.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval None
 */
void CAN_TTC_SetTBSFilled(M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    SET_REG8_BIT(CANx->TBSLOT, CAN_TBSLOT_TBF);
}

/**
 * @brief  Specifies time-triggered communication(TTC) timer prescaler.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8NTUPrescaler         Time-triggered communication timer prescaler.
 *                                      This parameter can be a value of @ref CAN_TTC_NTU_Prescaler
 *   @arg  CAN_TTC_NTU_PRESC_1:         NTU is SBT bit time * 1.
 *   @arg  CAN_TTC_NTU_PRESC_2:         NTU is SBT bit time * 2.
 *   @arg  CAN_TTC_NTU_PRESC_4:         NTU is SBT bit time * 4.
 *   @arg  CAN_TTC_NTU_PRESC_8:         NTU is SBT bit time * 8.
 * @retval None
 */
void CAN_TTC_SetNTUPrescaler(M4_CAN_TypeDef *CANx, uint8_t u8NTUPrescaler)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_TTC_NTU_PRESC(u8NTUPrescaler));
    MODIFY_REG8(CANx->TTCFG, CAN_TTCFG_T_PRESC, u8NTUPrescaler);
}

/**
 * @brief  Enable or disable the specified interrupts of time-triggered communication(TTC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8IntType              Interrupt type of TTC.
 *                                      This parameter can be values of @ref CAN_TTC_Interrupt_Type
 *   @arg  CAN_TTC_INT_TTI:             Time trigger interrupt.
 *   @arg  CAN_TTC_INT_WTI:             Watch trigger interrupt.
 * @param  [in]  enNewState             An en_functional_state_t enumeration type value.
 *   @arg  Enable:                      Enable the specified interrupts.
 *   @arg  Disable:                     Disable the specified interrupts.
 * @retval None
 */
void CAN_TTC_IntCmd(M4_CAN_TypeDef *CANx, uint8_t u8IntType, en_functional_state_t enNewState)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    u8IntType &= CAN_TTC_INT_ALL;
    if (enNewState == Enable)
    {
        SET_REG8_BIT(CANx->TTCFG, u8IntType);
    }
    else
    {
        CLEAR_REG8_BIT(CANx->TTCFG, u8IntType);
    }
}

/**
 * @brief  Get flag status of time-triggered communication(TTC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8Flag                 Status flag of TTC.
 *                                      This parameter can be values of @ref CAN_TTC_Status_Flag
 *   @arg  CAN_TTC_FLAG_TTI:            Time trigger interrupt flag.
 *   @arg  CAN_TTC_FLAG_TEI:            Trigger error interrupt flag.
 *   @arg  CAN_TTC_FLAG_WTI:            Watch trigger interrupt flag.
 * @retval An en_flag_status_t enumeration type value.
 *   @arg  Set:                         At least one of the specified flags is set.
 *   @arg  Reset:                       None of the specified flags is reset.
 */
en_flag_status_t CAN_TTC_GetStatus(const M4_CAN_TypeDef *CANx, uint8_t u8Flag)
{
    en_flag_status_t enFlag = Reset;

    DDL_ASSERT(IS_CAN_UNIT(CANx));
    if (READ_REG8_BIT(CANx->TTCFG, (u8Flag & (uint8_t)CAN_TTC_FLAG_ALL)) != 0U)
    {
        enFlag = Set;
    }

    return enFlag;
}

/**
 * @brief  Clear flag status of time-triggered communication(TTC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8Flag                 Status flag of TTC.
 *                                      This parameter can be a value of @ref CAN_TTC_Status_Flag except CAN_TTC_FLAG_TEI.
 *   @arg  CAN_TTC_FLAG_TTI:            Time trigger interrupt flag.
 *   @arg  CAN_TTC_FLAG_WTI:            Watch trigger interrupt flag.
 * @retval None
 */
void CAN_TTC_ClrStatus(M4_CAN_TypeDef *CANx, uint8_t u8Flag)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    SET_REG8_BIT(CANx->TTCFG, (u8Flag & CAN_TTC_FLAG_CLR_MSK));
}

/**
 * @brief  Get the value of status of TTC.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint8_t type value that includes the flowing status flags.
 *   @arg  CAN_TTC_FLAG_TTI:            Time trigger interrupt flag.
 *   @arg  CAN_TTC_FLAG_TEI:            Trigger error interrupt flag.
 *   @arg  CAN_TTC_FLAG_WTI:            Watch trigger interrupt flag.
 */
uint8_t CAN_TTC_GetStatusVal(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return READ_REG8_BIT(CANx->TTCFG, CAN_TTC_FLAG_ALL);
}

/**
 * @brief  Specifies reference message ID and IDE for time-triggered communication(TTC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u32ID                  Reference message ID.
 * @retval None
 */
void CAN_TTC_SetRefMsgID(M4_CAN_TypeDef *CANx, uint32_t u32ID)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    u32ID &= (uint32_t)(~CAN_REF_MSG_REF_IDE);
    MODIFY_REG32(CANx->REF_MSG, CAN_REF_MSG_REF_ID, u32ID);
}

/**
 * @brief  Specifiy IDE of TTC(time-triggered communication) reference message.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u32IDE                 IDE bit.
 *   @arg  0:                           Standard ID.
 *   @arg  1:                           Extended ID.
 * @retval None
 */
void CAN_TTC_SetRefMsgIDE(M4_CAN_TypeDef *CANx, uint32_t u32IDE)
{
    uint32_t u32Addr;

    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_IDE(u32IDE));
    u32Addr = (uint32_t)&CANx->REF_MSG;
    BIT_BAND(u32Addr, CAN_REF_MSG_REF_IDE_POS) = u32IDE;
}

/**
 * @brief  Get reference message ID of time-triggered communication(TTC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint32_t value of reference message ID.
 */
uint32_t CAN_TTC_GetRefMsgID(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return (READ_REG32_BIT(CANx->REF_MSG, CAN_REF_MSG_REF_ID));
}

/**
 * @brief  Get reference message ID extension bit of time-triggered communication(TTC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint32_t value of reference message IDE.
 *   @arg  0:                           Standard ID.
 *   @arg  1:                           Extended ID.
 */
uint32_t CAN_TTC_GetRefMsgIDE(const M4_CAN_TypeDef *CANx)
{
    uint32_t u32Addr;

    DDL_ASSERT(IS_CAN_UNIT(CANx));
    u32Addr = (uint32_t)&CANx->REF_MSG;
    return BIT_BAND(u32Addr, CAN_REF_MSG_REF_IDE_POS);
}

/**
 * @brief  Specifies transmit-trigger transmit buffer slot(TBS) for time-triggered communication(TTC) of specified CAN unit.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u8TBSlotPtr            Transmit-trigger transmit buffer slot.
 *                                      This parameter can be a value of @ref CAN_TTC_TBS_Pointer
 *   @arg  CAN_TTC_TBS_PTB:             TBS pointer points to PTB.
 *   @arg  CAN_TTC_TBS_STB_S1:          TBS pointer points to STB slot 1.
 *   @arg  CAN_TTC_TBS_STB_S2:          TBS pointer points to STB slot 2.
 *   @arg  CAN_TTC_TBS_STB_S3:          TBS pointer points to STB slot 3.
 * @retval None
 */
void CAN_TTC_SetTxTrigTBS(M4_CAN_TypeDef *CANx, uint8_t u8TBSlotPtr)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_TTC_TBS(u8TBSlotPtr));
    MODIFY_REG16(CANx->TRG_CFG, CAN_TRG_CFG_TTPTR, u8TBSlotPtr);
}

/**
 * @brief  Get transmit-trigger transmit buffer slot(TBS) of specified CAN unit's time-triggered communication(TTC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint8_t type value of transmit buffer slot pointer. It is can be a value of @ref CAN_TTC_TBS_Pointer
 *   @arg  CAN_TTC_TBS_PTB:             TBS pointer points to PTB.
 *   @arg  CAN_TTC_TBS_STB1:            TBS pointer points to STB slot 1.
 *   @arg  CAN_TTC_TBS_STB2:            TBS pointer points to STB slot 2.
 *   @arg  CAN_TTC_TBS_STB3:            TBS pointer points to STB slot 3.
 */
uint8_t CAN_TTC_GetTxTrigTBS(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return (uint8_t)(READ_REG16_BIT(CANx->TRG_CFG, CAN_TRG_CFG_TTPTR));
}

/**
 * @brief  Specifies trigger type for time-triggered communication(TTC) of specified CAN unit.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u16TrigType            Time-triggered communication trigger type.
 *                                      This parameter can be a value of @ref CAN_TTC_Trigger_Type
 *   @arg  CAN_TTC_TRIG_IMMED_TRIG:          Immediate trigger for immediate transmission.
 *   @arg  CAN_TTC_TRIG_TIME_TRIG:           Time trigger for receive triggers.
 *   @arg  CAN_TTC_TRIG_SSHOT_TRANS_TRIG:    Single shot transmit trigger for exclusive time windows.
 *   @arg  CAN_TTC_TRIG_TRANS_START_TRIG:    Transmit start trigger for merged arbitrating time windows.
 *   @arg  CAN_TTC_TRIG_TRANS_STOP_TRIG:     Transmit stop trigger for merged arbitrating time windows.
 * @retval None
 */
void CAN_TTC_SetTrigType(M4_CAN_TypeDef *CANx, uint16_t u16TrigType)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_TTC_TRIG_TYPE(u16TrigType));
    MODIFY_REG16(CANx->TRG_CFG, CAN_TRG_CFG_TTYPE, u16TrigType);
}

/**
 * @brief  Get trigger type of specified CAN unit's time-triggered communication(TTC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint16_t type value of TTC trigger type.
 *   @arg  CAN_TTC_TRIG_IMMED_TRIG:          Immediate trigger for immediate transmission.
 *   @arg  CAN_TTC_TRIG_TIME_TRIG:           Time trigger for receive triggers.
 *   @arg  CAN_TTC_TRIG_SSHOT_TRANS_TRIG:    Single shot transmit trigger for exclusive time windows.
 *   @arg  CAN_TTC_TRIG_TRANS_START_TRIG:    Transmit start trigger for merged arbitrating time windows.
 *   @arg  CAN_TTC_TRIG_TRANS_STOP_TRIG:     Transmit stop trigger for merged arbitrating time windows.
 */
uint16_t CAN_TTC_GetTrigType(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return READ_REG16_BIT(CANx->TRG_CFG, CAN_TRG_CFG_TTYPE);
}

/**
 * @brief  Specifies transmission enable window for single shot transmit trigger.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u16TxEnableWindow      Number of NTU. Time period within which the transmission of a message may be started.
 * @retval None
 */
void CAN_TTC_SetTxEnableWindow(M4_CAN_TypeDef *CANx, uint16_t u16TxEnableWindow)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    DDL_ASSERT(IS_CAN_TTC_TX_EN_WINDOW(u16TxEnableWindow));
    u16TxEnableWindow -= 1U;
    u16TxEnableWindow  = (uint16_t)((uint32_t)u16TxEnableWindow << CAN_TRG_CFG_TEW_POS);
    MODIFY_REG16(CANx->TRG_CFG, CAN_TRG_CFG_TEW, u16TxEnableWindow);
}

/**
 * @brief  Get transmit enable window of single shot transmit trigger.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint16_t type value of Tx_Enable window.
 */
uint16_t CAN_TTC_GetTxEnableWindow(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return ((CANx->TRG_CFG >> CAN_TRG_CFG_TEW_POS) + 1U);
}

/**
 * @brief  Set transmission trigger time for timer-trigger communication(TTC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u16TxTrigTime          Transmission trigger time. Number of NTU.
 * @retval None
 */
void CAN_TTC_SetTxTrigTime(M4_CAN_TypeDef *CANx, uint16_t u16TxTrigTime)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    WRITE_REG16(CANx->TT_TRIG, u16TxTrigTime);
}

/**
 * @brief  Get watch trigger time of timer-trigger communication(TTC).
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint16_t type value of cycle time.
 */
uint16_t CAN_TTC_GetTxTrigTime(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return (CANx->TT_TRIG);
}

/**
 * @brief  Set cycle time for timer-trigger communication(TTC) watch-trigger.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  u16WatchTrigTime       Watch trigger time. Number of NTU.
 * @retval None
 */
void CAN_TTC_SetWatchTrigTime(M4_CAN_TypeDef *CANx, uint16_t u16WatchTrigTime)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    WRITE_REG16(CANx->TT_WTRIG, u16WatchTrigTime);
}

/**
 * @brief  Get cycle time of timer-trigger communication(TTC) watch-trigger.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @retval An uint16_t type value of cycle time.
 */
uint16_t CAN_TTC_GetWatchTrigTime(const M4_CAN_TypeDef *CANx)
{
    DDL_ASSERT(IS_CAN_UNIT(CANx));
    return (CANx->TT_WTRIG);
}

/**
 * @brief  CAN bus transmits data.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  pstcTx                 Points to a stc_can_tx_t structure type data which stores the frames to be transmitted.
 * @param  [in]  u8TxBufType            CAN transmit buffer type.
 *                                      This parameter can be a value of @ref CAN_Transmit_Buffer_Type
 *   @arg  CAN_BUF_PTB:                 Primary transmit buffer.
 *   @arg  CAN_BUF_STB:                 Secondary transmit buffer.
 * @param  [in]  u8STBTxCtrl            STB transmission control. If transmit via PTB, set u8STBTxCtrl to 0.
 *                                      This parameter can be a value of @ref CAN_STB_Trans_Control
 *   @arg  CAN_STB_TRANS_ALL:           Transmit all of STB slots.
 *   @arg  CAN_STB_TRANS_ONE:           Transmit one STB slot.
 * @param  [in]  u32Timeout             Timeout value. Set it as zero for transmit in interrupt-mode.
 * @retval An en_result_t enumeration type value.
 *   @arg  Ok:                          No error occurred.
 *   @arg  ErrorInvalidParameter:       pstcTx == NULL.
 *   @arg  ErrorBufferFull:             The target transmit buffer is full.
 *   @arg  ErrorOperationInProgress:    The target transmit buffer is being transmitted.
 *   @arg  ErrorAddressAlignment:       Data address is not 4-byte aligned.
 *   @arg  ErrorTimeout:                Transmit timeout.
 *   @arg  Error:                       CAN bus transmission error.
 */
en_result_t CAN_TransData(M4_CAN_TypeDef *CANx, const stc_can_tx_t *pstcTx,
                          uint8_t u8TxBufType, uint8_t u8STBTxCtrl, uint32_t u32Timeout)
{
    uint32_t i;
    uint8_t u8DataSize;
    uint8_t u8WordLen;
    uint32_t u32Addr;
    uint32_t u32TimeCnt;
    uint32_t u32SrcDataAddr;
    uint32_t u32TBAddr;
    uint8_t au8TxEn[2U][2U] = {{CAN_TCMD_TPE, CAN_TCMD_TPE}, \
                               {CAN_TCMD_TSALL, CAN_TCMD_TSONE}};
    uint8_t au8Check[2U] = {CAN_RTIF_TPIF, CAN_RTIF_TSIF};
    uint8_t au8DLC2Size[2U][16U] =
    {
        {0U, 1U, 2U, 3U, 4U, 5U, 6U, 7U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U},
        {0U, 1U, 2U, 3U, 4U, 5U, 6U, 7U, 8U, 12U, 16U, 20U, 24U, 32U, 48U, 64U},
    };
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcTx != NULL)
    {
        DDL_ASSERT(IS_CAN_UNIT(CANx));
        DDL_ASSERT(IS_CAN_TB_TYPE(u8TxBufType));
        DDL_ASSERT(IS_CAN_STB_TX_CTRL(u8STBTxCtrl));

        u32SrcDataAddr = (uint32_t)pstcTx->pu8Data;

        if ((READ_REG8_BIT(CANx->TCTRL, CAN_TB_STAT_FULL) == CAN_TB_STAT_FULL) && \
            (u8TxBufType == CAN_BUF_STB))
        {
            /* All STBs are full. */
            enRet = ErrorBufferFull;
        }
        else if ((READ_REG8_BIT(CANx->TCMD, CAN_TCMD_TPE) != 0U) && \
                 (u8TxBufType == CAN_BUF_PTB))
        {
            /* PTB is being transmitted. */
            enRet = ErrorOperationInProgress;
        }
        else if ((u32SrcDataAddr & 0x03UL) != 0U)
        {
            enRet = ErrorAddressAlignment;
        }
        else
        {
            u8DataSize = au8DLC2Size[pstcTx->FDF][pstcTx->DLC];
            u8WordLen  = u8DataSize / 4U;
            if ((u8DataSize % 4U) != 0U)
            {
                u8WordLen += 1U;
            }

            /* Specifies the transmit buffer, PTB or STB. */
            u32Addr = (uint32_t)&CANx->TCMD;
            BIT_BAND(u32Addr, CAN_TCMD_TBSEL_POS) = (uint32_t)u8TxBufType;

            u32TBAddr = (uint32_t)&CANx->TBUF;
            RW_MEM32(u32TBAddr) = pstcTx->u32ID;
            RW_MEM32(u32TBAddr + 4U) = pstcTx->u32Ctrl;

            i = 0U;
            u32TBAddr += 8U;
            while (i < u8WordLen)
            {
                RW_MEM32(u32TBAddr + i * 4U) = *(uint32_t *)u32SrcDataAddr;
                u32SrcDataAddr += 4U;
                i++;
            }

            if (u8TxBufType == CAN_BUF_STB)
            {
                /* After writes the data in transmit buffer(TB), sets the TSNEXT bit to indicate that the current \
                   STB slot has been filled, so that the hardware will point TB to the next STB slot. */
                SET_REG8_BIT(CANx->TCTRL, CAN_TCTRL_TSNEXT);
            }

            /* Enable transmission. */
            SET_REG8_BIT(CANx->TCMD, au8TxEn[u8TxBufType][u8STBTxCtrl]);

            if (u32Timeout == 0U)
            {
                enRet = Ok;
            }
            else
            {
                /* Check transmission. */
                u32TimeCnt = u32Timeout * (HCLK_VALUE / 10U / 1000U);
                enRet = ErrorTimeout;
                while (u32TimeCnt-- != 0U)
                {
                    if ((CAN_GetStatusVal(CANx) & CAN_FLAG_TX_ERR_MSK) != 0U)
                    {
                        enRet = Error;
                    }
                    else if (READ_REG8_BIT(CANx->RTIF, au8Check[u8TxBufType]) != 0U)
                    {
                        /* Clear the transmit-OK status flag. */
                        CANx->RTIF |= au8Check[u8TxBufType];
                        enRet = Ok;
                    }
                    else
                    {
                        /* rsvd */
                    }
                    if (enRet != ErrorTimeout)
                    {
                        break;
                    }
                }
            }
        }
    }

    return enRet;
}

/**
 * @brief  TTCAN transmits data. For single shot transmit trigger.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [in]  pstcTx                 Points to a stc_can_tx_t structure type data which stores the frames to be transmitted.
 * @param  [in]  u8TBSlot               Transmit buffer pointer.
 *                                      This parameter can be a value of @ref CAN_TTC_TBS_Pointer
 *   @arg  CAN_TTC_TBS_PTB:             Point to PTB.
 *   @arg  CAN_TTC_TBS_STB1:            Point to STB slot 1.
 *   @arg  CAN_TTC_TBS_STB2:            Point to STB slot 2.
 *   @arg  CAN_TTC_TBS_STB3:            Point to STB slot 3.
 * @retval An en_result_t enumeration type value.
 *   @arg  Ok:                          No error occurred.
 *   @arg  ErrorInvalidParameter:       pstcTx == NULL.
 *   @arg  ErrorBufferFull:             The target transmit buffer is full.
 *   @arg  ErrorAddressAlignment:       Data address is not 4-byte aligned.
 */
en_result_t CAN_TTC_TransData(M4_CAN_TypeDef *CANx, const stc_can_tx_t *pstcTx, uint8_t u8TBSlot)
{
    uint32_t i;
    uint8_t u8Tmp;
    uint8_t u8DataSize;
    uint8_t u8WordLen;
    uint32_t u32SrcDataAddr;
    uint32_t u32TBAddr;
    uint8_t au8DLC2Size[2U][16U] =
    {
        {0U, 1U, 2U, 3U, 4U, 5U, 6U, 7U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U},
        {0U, 1U, 2U, 3U, 4U, 5U, 6U, 7U, 8U, 12U, 16U, 20U, 24U, 32U, 48U, 64U},
    };
    en_result_t enRet = ErrorInvalidParameter;

    if (pstcTx != NULL)
    {
        DDL_ASSERT(IS_CAN_UNIT(CANx));

        u32SrcDataAddr = (uint32_t)pstcTx->pu8Data;
        u8Tmp = CANx->TBSLOT;
        WRITE_REG8(CANx->TBSLOT, u8TBSlot);
        if (READ_REG8_BIT(CANx->TCTRL, CAN_TB_STAT_FULL) == CAN_TB_STAT_FULL)
        {
            enRet = ErrorBufferFull;
        }
        else if (READ_REG8_BIT(CANx->RTIE, CAN_RTIE_TSFF) != 0U)
        {
            WRITE_REG8(CANx->TBSLOT, CAN_TBSLOT_TBF|u8Tmp);
            WRITE_REG16(CANx->TT_TRIG, CANx->TT_TRIG);
            enRet = ErrorBufferFull;
        }
        else if ((u32SrcDataAddr & 0x03UL) != 0U)
        {
            enRet = ErrorAddressAlignment;
        }
        else
        {
            u8DataSize = au8DLC2Size[pstcTx->FDF][pstcTx->DLC];
            u8WordLen  = u8DataSize / 4U;
            if ((u8DataSize % 4U) != 0U)
            {
                u8WordLen += 1U;
            }

            u32TBAddr = (uint32_t)&CANx->TBUF;
            MODIFY_REG16(CANx->TRG_CFG, CAN_TRG_CFG_TTPTR, u8TBSlot);

            RW_MEM32(u32TBAddr) = pstcTx->u32ID;
            RW_MEM32(u32TBAddr + 4U) = pstcTx->u32Ctrl;

            i = 0U;
            u32TBAddr += 8U;
            while (i < u8WordLen)
            {
                RW_MEM32(u32TBAddr + i * 4U) = *(uint32_t *)u32SrcDataAddr;
                u32SrcDataAddr += 4U;
                i++;
            }
            /* Set buffer as filled. */
            SET_REG8_BIT(CANx->TBSLOT, CAN_TBSLOT_TBF);

            /* Write MSB of TT_TRIG to transmit. */
            WRITE_REG16(CANx->TT_TRIG, CANx->TT_TRIG);

            enRet = Ok;
        }
    }

    return enRet;
}

/**
 * @brief  CAN bus receives data.
 * @param  [in]  CANx                   Pointer to CAN instance register base.
 *                                      This parameter can be a value of the following:
 *   @arg  M4_CAN1:                     CAN unit 1 instance register base.
 *   @arg  M4_CAN2:                     CAN unit 2 instance register base.
 * @param  [out] pstcRx                 Points to a stc_can_rx_t structure type array which is used to \
 *                                      store the received frames.
 * @param  [out] pu8RxFrameCnt          Address to store the number of frames received.
 *                                      If you do not need it, set it as NULL.\
 * @param  [in] u8RxFrameBufLength      The length of the buffer which is used to store the received frames.
 * @retval An en_result_t enumeration type value.
 *   @arg  Ok:                          No error occurred.
 *   @arg  ErrorInvalidParameter:      -pstcRx == NULL.
 *                                     -u8RxFrameBufLength == 0U.
 */
en_result_t CAN_ReceiveData(M4_CAN_TypeDef *CANx, stc_can_rx_t pstcRx[], uint8_t *pu8RxFrameCnt, uint8_t u8RxFrameBufLength)
{
    uint32_t i;
    uint8_t u8DataSize;
    uint8_t u8WordLen;
    uint8_t u8RxFrameCnt = 0U;
    uint32_t u32DestDataAddr;
    uint32_t u32RBAddr;
    en_result_t enRet;
    uint8_t au8DLC2Size[2U][16U] =
    {
        {0U, 1U, 2U, 3U, 4U, 5U, 6U, 7U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U},
        {0U, 1U, 2U, 3U, 4U, 5U, 6U, 7U, 8U, 12U, 16U, 20U, 24U, 32U, 48U, 64U},
    };

    if ((pstcRx == NULL) || (u8RxFrameBufLength == 0U))
    {
        enRet = ErrorInvalidParameter;
    }
    else
    {
        enRet = Ok;
        while (READ_REG8_BIT(CANx->RCTRL, CAN_RCTRL_RSTAT) != CAN_RB_STAT_EMPTY)
        {
            u32RBAddr = (uint32_t)&CANx->RBUF;
            pstcRx[u8RxFrameCnt].u32ID   = RW_MEM32(u32RBAddr);
            pstcRx[u8RxFrameCnt].u32Ctrl = RW_MEM32(u32RBAddr + 4U);

            u8DataSize = au8DLC2Size[pstcRx[u8RxFrameCnt].FDF][pstcRx[u8RxFrameCnt].DLC];
            u8WordLen  = u8DataSize / 4U;
            if ((u8DataSize % 4U) != 0U)
            {
                u8WordLen += 1U;
            }

            u32DestDataAddr = (uint32_t)pstcRx[u8RxFrameCnt].pu8Data;
            if (!IS_ADDRESS_ALIGN_WORD(u32DestDataAddr))
            {
                enRet = ErrorAddressAlignment;
                break;
            }
            i = 0U;
            u32RBAddr += 8U;
            while (i < u8WordLen)
            {
                *(uint32_t *)u32DestDataAddr = RW_MEM32(u32RBAddr + i * 4U);
                u32DestDataAddr += 4U;
                i++;
            }

            /* Set RB to point to the next RB slot. */
            SET_REG8_BIT(CANx->RCTRL, CAN_RCTRL_RREL);
            u8RxFrameCnt++;
            if (u8RxFrameCnt >= u8RxFrameBufLength)
            {
                break;
            }
        }

        if (pu8RxFrameCnt != NULL)
        {
            *pu8RxFrameCnt = u8RxFrameCnt;
        }
    }

    return enRet;
}

/**
 * @}
 */

#endif /* DDL_CAN_ENABLE */

/**
 * @}
 */

/**
 * @}
 */

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