lpc_can.c 63.9 KB
Newer Older
N
nongxiaoming 已提交
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 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
/**********************************************************************
* $Id$      lpc_can.c           2011-06-02
*//**
* @file     lpc_can.c
* @brief    Contains all functions support for CAN firmware library on
*           LPC
* @version  1.0
* @date     02. June. 2011
* @author   NXP MCU SW Application Team
* 
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, under NXP Semiconductors'
* relevant copyright in the software, without fee, provided that it
* is used in conjunction with NXP Semiconductors microcontrollers.  This
* copyright, permission, and disclaimer notice must appear in all copies of
* this code.
**********************************************************************/

/* Peripheral group ----------------------------------------------------------- */
/** @addtogroup CAN
 * @{
 */
#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc_libcfg.h"
#else
#include "lpc_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */
#ifdef _CAN
 
/* Includes ------------------------------------------------------------------- */
#include "lpc_can.h"
#include "lpc_clkpwr.h"

/* Private Variables ---------------------------------------------------------- */
/** @defgroup CAN_Private_Variables CAN Private Variables
 * @{
 */

FunctionalState FULLCAN_ENABLE;


/* Counts number of filters (CAN message objects) used */
uint16_t CANAF_FullCAN_cnt = 0;
uint16_t CANAF_std_cnt = 0;
uint16_t CANAF_gstd_cnt = 0;
uint16_t CANAF_ext_cnt = 0;
uint16_t CANAF_gext_cnt = 0;

/* End of Private Variables ----------------------------------------------------*/
/**
 * @}
 */

/* Private Variables ---------------------------------------------------------- */
static LPC_CAN_TypeDef* CAN_GetPointer (uint8_t canId);


static void can_SetBaudrate (LPC_CAN_TypeDef *CANx, uint32_t baudrate);

/*********************************************************************//**
 * @brief       Setting CAN baud rate (bps)
 * @param[in]   canId point to LPC_CAN_TypeDef object, should be:
 *              - LPC_CAN1: CAN1 peripheral
 *              - LPC_CAN2: CAN2 peripheral
 * @return      The pointer to CAN peripheral that's expected to use
 ***********************************************************************/
static LPC_CAN_TypeDef* CAN_GetPointer (uint8_t canId)
{
    LPC_CAN_TypeDef* pCan;

    switch (canId)
    {
        case CAN_ID_1:
            pCan = LPC_CAN1;
            break;

        case CAN_ID_2:
            pCan = LPC_CAN2;
            break;

        default:
            pCan = NULL;
            break;
    }

    return pCan;
}


/*********************************************************************//**
 * @brief       Setting CAN baud rate (bps)
 * @param[in]   CANx point to LPC_CAN_TypeDef object, should be:
 *              - LPC_CAN1: CAN1 peripheral
 *              - LPC_CAN2: CAN2 peripheral
 * @param[in]   baudrate: is the baud rate value will be set
 * @return      None
 ***********************************************************************/
static void can_SetBaudrate (LPC_CAN_TypeDef *CANx, uint32_t baudrate)
{
    uint32_t result = 0;
    uint8_t NT, TSEG1, TSEG2;
    uint32_t CANPclk = 0;
    uint32_t BRP;

    CANPclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);

    result = CANPclk / baudrate;

    /* Calculate suitable nominal time value
     * NT (nominal time) = (TSEG1 + TSEG2 + 3)
     * NT <= 24
     * TSEG1 >= 2*TSEG2
     */
    for(NT = 24; NT > 0; NT = NT-2)
    {
        if ((result%NT) == 0)
        {
            BRP = result / NT - 1;

            NT--;

            TSEG2 = (NT/3) - 1;

            TSEG1 = NT -(NT/3) - 1;

            break;
        }
    }

    /* Enter reset mode */
    CANx->MOD = 0x01;

    /* Set bit timing
     * Default: SAM = 0x00;
     *          SJW = 0x03;
     */
    CANx->BTR = (TSEG2 << 20) | (TSEG1 << 16) | (3 << 14) | BRP;

    /* Return to normal operating */
    CANx->MOD = 0;
}
/* End of Private Functions ----------------------------------------------------*/


/* Public Functions ----------------------------------------------------------- */
/** @addtogroup CAN_Public_Functions
 * @{
 */

/********************************************************************//**
 * @brief       Initialize CAN peripheral with given baudrate
 * @param[in]    canId           The Id of the expected CAN component
 *
 * @param[in]   baudrate: the value of CAN baudrate will be set (bps)
 * @return      None
 *********************************************************************/
void CAN_Init(uint8_t canId, uint32_t baudrate)
{
    LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);

    uint16_t i;

    if(canId == CAN_ID_1)
    {
        /* Turn on power and clock for CAN1 */
        CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, ENABLE);
    }
    else if(canId == CAN_ID_2)
    {
        /* Turn on power and clock for CAN2 */
        CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, ENABLE);
    }
    else
    {
        return;
    }

    pCan->MOD = 1; // Enter Reset Mode
    pCan->IER = 0; // Disable All CAN Interrupts
    pCan->GSR = 0;

    /* Request command to release Rx, Tx buffer and clear data overrun */
    //pCan->CMR = CAN_CMR_AT | CAN_CMR_RRB | CAN_CMR_CDO;
    pCan->CMR = (1 << 1) | (1 << 2) | (1 << 3);

    /* Read to clear interrupt pending in interrupt capture register */
    i = pCan->ICR;
    pCan->MOD = 0;// Return Normal operating

    //Reset CANAF value
    LPC_CANAF->AFMR = 0x01;

    //clear ALUT RAM
    for (i = 0; i < 512; i++)
    {
        LPC_CANAF_RAM->mask[i] = 0x00;
    }

    LPC_CANAF->SFF_sa = 0x00;
    LPC_CANAF->SFF_GRP_sa = 0x00;
    LPC_CANAF->EFF_sa = 0x00;
    LPC_CANAF->EFF_GRP_sa = 0x00;
    LPC_CANAF->ENDofTable = 0x00;

    LPC_CANAF->AFMR = 0x00;

    /* Set baudrate */
    can_SetBaudrate (pCan, baudrate);
}

/********************************************************************//**
 * @brief       CAN deInit
 * @param[in]    canId           The Id of the expected CAN component
 *
 * @return      None
 *********************************************************************/
void CAN_DeInit(uint8_t canId)
{
    if(canId == CAN_ID_1)
    {
        /* Turn on power and clock for CAN1 */
        CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, DISABLE);
    }
    else if(canId == CAN_ID_2)
    {
        /* Turn on power and clock for CAN1 */
        CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, DISABLE);
    }

    return;
}

/********************************************************************//**
 * @brief       Setup Acceptance Filter Look-Up Table
 * @param[in]   CANAFx  pointer to LPC_CANAF_TypeDef
 *              Should be: LPC_CANAF
 * @param[in]   AFSection   the pointer to AF_SectionDef structure
 *              It contain information about 5 sections will be install in AFLUT
 * @return      CAN Error   could be:
 *              - CAN_OBJECTS_FULL_ERROR: No more rx or tx objects available
 *              - CAN_AF_ENTRY_ERROR: table error-violation of ascending numerical order
 *              - CAN_OK: ID is added into table successfully
 *********************************************************************/
CAN_ERROR CAN_SetupAFLUT(AF_SectionDef* AFSection)
{
    uint8_t ctrl1,ctrl2;
    uint8_t dis1, dis2;
    uint16_t SID, ID_temp,i, count = 0;
    uint32_t EID, entry, buf;
    uint16_t lowerSID, upperSID;
    uint32_t lowerEID, upperEID;

    LPC_CANAF->AFMR = 0x01;

    /***** setup FullCAN Table *****/
    if(AFSection->FullCAN_Sec == NULL)
    {
        FULLCAN_ENABLE = DISABLE;
    }
    else
    {
        FULLCAN_ENABLE = ENABLE;

        for(i = 0; i < (AFSection->FC_NumEntry); i++)
        {
            if(count + 1 > 64)
            {
                return CAN_OBJECTS_FULL_ERROR;
            }

            ctrl1 = AFSection->FullCAN_Sec->controller;

            SID = AFSection->FullCAN_Sec->id_11;

            dis1 = AFSection->FullCAN_Sec->disable;

            entry = 0x00; //reset entry value

            if((CANAF_FullCAN_cnt & 0x00000001)==0)
            {
                if(count != 0x00)
                {
                    buf = LPC_CANAF_RAM->mask[count-1];
                    ID_temp = (buf & 0xE7FF); //mask controller & identifier bits
                    if(ID_temp > ((ctrl1<<13)|SID))
                    {
                        return CAN_AF_ENTRY_ERROR;
                    }
                }

                entry = (ctrl1<<29)|(dis1<<28)|(SID<<16)|(1<<27);

                LPC_CANAF_RAM->mask[count] &= 0x0000FFFF;

                LPC_CANAF_RAM->mask[count] |= entry;

                CANAF_FullCAN_cnt++;
                if(CANAF_FullCAN_cnt == AFSection->FC_NumEntry) //this is the lastest FullCAN entry
                    count++;
            }
            else
            {
                buf = LPC_CANAF_RAM->mask[count];
                ID_temp = (buf >>16) & 0xE7FF;
                if(ID_temp > ((ctrl1<<13)|SID))
                {
                    return CAN_AF_ENTRY_ERROR;
                }

                entry = (ctrl1 << 13) | (dis1 << 12) | (SID << 0) | (1 << 11);

                LPC_CANAF_RAM->mask[count] &= 0xFFFF0000;

                LPC_CANAF_RAM->mask[count]|= entry;

                count++;

                CANAF_FullCAN_cnt++;
            }

            AFSection->FullCAN_Sec = (FullCAN_Entry *)((uint32_t)(AFSection->FullCAN_Sec)+ sizeof(FullCAN_Entry));
        }
    }

    /***** Setup Explicit Standard Frame Format Section *****/
    if(AFSection->SFF_Sec != NULL)
    {
        for(i=0;i<(AFSection->SFF_NumEntry);i++)
        {
            if(count + 1 > 512)
            {
                return CAN_OBJECTS_FULL_ERROR;
            }

            ctrl1 = AFSection->SFF_Sec->controller;

            SID = AFSection->SFF_Sec->id_11;

            dis1 = AFSection->SFF_Sec->disable;

            entry = 0x00; //reset entry value

            if((CANAF_std_cnt & 0x00000001)==0)
            {
                if(CANAF_std_cnt !=0 )
                {
                    buf = LPC_CANAF_RAM->mask[count-1];
                    ID_temp = (buf & 0xE7FF); //mask controller & identifier bits
                    if(ID_temp > ((ctrl1<<13)|SID))
                    {
                        return CAN_AF_ENTRY_ERROR;
                    }
                }

                entry = (ctrl1<<29)|(dis1<<28)|(SID<<16);

                LPC_CANAF_RAM->mask[count] &= 0x0000FFFF;

                LPC_CANAF_RAM->mask[count] |= entry;

                CANAF_std_cnt++;
                if(CANAF_std_cnt == AFSection->SFF_NumEntry)//if this is the last SFF entry
                    count++;
            }
            else
            {
                buf = LPC_CANAF_RAM->mask[count];
                ID_temp = (buf >>16) & 0xE7FF;
                if(ID_temp > ((ctrl1<<13)|SID))
                {
                    return CAN_AF_ENTRY_ERROR;
                }

                entry = (ctrl1 << 13) | (dis1 << 12) | (SID << 0);

                LPC_CANAF_RAM->mask[count] &= 0xFFFF0000;

                LPC_CANAF_RAM->mask[count] |= entry;

                count++;

                CANAF_std_cnt++;
            }

            AFSection->SFF_Sec = (SFF_Entry *)((uint32_t)(AFSection->SFF_Sec)+ sizeof(SFF_Entry));
        }
    }

    /***** Setup Group of Standard Frame Format Identifier Section *****/
    if(AFSection->SFF_GPR_Sec != NULL)
    {
        for(i=0;i<(AFSection->SFF_GPR_NumEntry);i++)
        {
            if(count + 1 > 512)
            {
                return CAN_OBJECTS_FULL_ERROR;
            }

            ctrl1 = AFSection->SFF_GPR_Sec->controller1;

            ctrl2 = AFSection->SFF_GPR_Sec->controller2;

            dis1 = AFSection->SFF_GPR_Sec->disable1;

            dis2 = AFSection->SFF_GPR_Sec->disable2;

            lowerSID = AFSection->SFF_GPR_Sec->lowerID;

            upperSID = AFSection->SFF_GPR_Sec->upperID;

            entry = 0x00;

            if(CANAF_gstd_cnt!=0)
            {
                buf = LPC_CANAF_RAM->mask[count-1];
                ID_temp = buf & 0xE7FF;
                if((ctrl1 != ctrl2)||(lowerSID > upperSID)||(ID_temp > ((ctrl1<<13)|lowerSID)))
                {
                    return CAN_AF_ENTRY_ERROR;
                }
            }
            entry = (ctrl1 << 29)|(dis1 << 28)|(lowerSID << 16)|  \
                    (ctrl2 << 13)|(dis2 << 12)|(upperSID << 0);
            LPC_CANAF_RAM->mask[count] = entry;

            CANAF_gstd_cnt++;

            count++;

            AFSection->SFF_GPR_Sec = (SFF_GPR_Entry *)((uint32_t)(AFSection->SFF_GPR_Sec)+ sizeof(SFF_GPR_Entry));
        }
    }

    /***** Setup Explicit Extend Frame Format Identifier Section *****/
    if(AFSection->EFF_Sec != NULL)
    {
        for(i=0;i<(AFSection->EFF_NumEntry);i++)
        {
            if(count + 1 > 512)
            {
                return CAN_OBJECTS_FULL_ERROR;
            }

            EID = AFSection->EFF_Sec->ID_29;

            ctrl1 = AFSection->EFF_Sec->controller;

            entry = 0x00; //reset entry value

            entry = (ctrl1 << 29)|(EID << 0);
            if(CANAF_ext_cnt != 0)
            {
                buf = LPC_CANAF_RAM->mask[count-1];
//              EID_temp = buf & 0x0FFFFFFF;
                if(buf > entry)
                {
                    return CAN_AF_ENTRY_ERROR;
                }
            }
            LPC_CANAF_RAM->mask[count] = entry;

            CANAF_ext_cnt ++;

            count++;

            AFSection->EFF_Sec = (EFF_Entry *)((uint32_t)(AFSection->EFF_Sec)+ sizeof(EFF_Entry));
        }
    }

    /***** Setup Group of Extended Frame Format Identifier Section *****/
    if(AFSection->EFF_GPR_Sec != NULL)
    {
        for(i=0;i<(AFSection->EFF_GPR_NumEntry);i++)
        {
            if(count + 2 > 512)
            {
                return CAN_OBJECTS_FULL_ERROR;
            }

            ctrl1 = AFSection->EFF_GPR_Sec->controller1;

            ctrl2 = AFSection->EFF_GPR_Sec->controller2;

            lowerEID = AFSection->EFF_GPR_Sec->lowerEID;

            upperEID = AFSection->EFF_GPR_Sec->upperEID;

            entry = 0x00;

            if(CANAF_gext_cnt != 0)
            {
                buf = LPC_CANAF_RAM->mask[count-1];
//              EID_temp = buf & 0x0FFFFFFF;
                if((ctrl1 != ctrl2) || (lowerEID > upperEID) || (buf > ((ctrl1 << 29)|(lowerEID << 0))))
                {
                    return CAN_AF_ENTRY_ERROR;
                }
            }

            entry = (ctrl1 << 29)|(lowerEID << 0);

            LPC_CANAF_RAM->mask[count++] = entry;

            entry = (ctrl2 << 29)|(upperEID << 0);

            LPC_CANAF_RAM->mask[count++] = entry;

            CANAF_gext_cnt++;

            AFSection->EFF_GPR_Sec = (EFF_GPR_Entry *)((uint32_t)(AFSection->EFF_GPR_Sec)+ sizeof(EFF_GPR_Entry));
        }
    }

    //update address values
    LPC_CANAF->SFF_sa = ((CANAF_FullCAN_cnt + 1)>>1)<<2;

    LPC_CANAF->SFF_GRP_sa = LPC_CANAF->SFF_sa + (((CANAF_std_cnt+1)>>1)<< 2);

    LPC_CANAF->EFF_sa = LPC_CANAF->SFF_GRP_sa + (CANAF_gstd_cnt << 2);

    LPC_CANAF->EFF_GRP_sa = LPC_CANAF->EFF_sa + (CANAF_ext_cnt << 2);

    LPC_CANAF->ENDofTable = LPC_CANAF->EFF_GRP_sa + (CANAF_gext_cnt << 3);

    if(FULLCAN_ENABLE == DISABLE)
    {
        LPC_CANAF->AFMR = 0x00; // Normal mode
    }
    else
    {
        LPC_CANAF->AFMR = 0x04;
    }

    return CAN_OK;
}
/********************************************************************//**
 * @brief       Add Explicit ID into AF Look-Up Table dynamically.
 * @param[in]    canId           The Id of the expected CAN component
 *
 * @param[in]   id: The ID of entry will be added
 * @param[in]   format: is the type of ID Frame Format, should be:
 *              - STD_ID_FORMAT: 11-bit ID value
 *              - EXT_ID_FORMAT: 29-bit ID value
 * @return      CAN Error, could be:
 *              - CAN_OBJECTS_FULL_ERROR: No more rx or tx objects available
 *              - CAN_ID_EXIT_ERROR: ID exited in table
 *              - CAN_OK: ID is added into table successfully
 *********************************************************************/
CAN_ERROR CAN_LoadExplicitEntry(uint8_t canId, uint32_t id, CAN_ID_FORMAT_Type format)
{
    uint32_t buf0 = 0, buf1 = 0;
    int16_t cnt1 = 0, cnt2 = 0, bound1 = 0, total = 0;

    /* Acceptance Filter Memory full - return */
    total =((CANAF_FullCAN_cnt + 1) >> 1) + CANAF_FullCAN_cnt * 3 + ((CANAF_std_cnt + 1) >> 1)  \
                + CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt << 1);

    if (total >= 512)
    {
        //don't have enough space
        return CAN_OBJECTS_FULL_ERROR;
    }

    /* Setup Acceptance Filter Configuration
    Acceptance Filter Mode Register = Off */
    LPC_CANAF->AFMR = 0x00000001;

    /*********** Add Explicit Standard Identifier Frame Format entry *********/
    if(format == STD_ID_FORMAT)
    {
        id &= 0x07FF;

        id |= canId << 13;/* Add controller number */

        /* Move all remaining sections one place up
        if new entry will increase FullCAN list */
        if ((CANAF_std_cnt & 0x0001) == 0)
        {
            cnt1   = ((CANAF_FullCAN_cnt+1)>>1)+((CANAF_std_cnt+1)>>1);

            bound1 = total - cnt1;

            buf0   = LPC_CANAF_RAM->mask[cnt1];

            while(bound1--)
            {
                cnt1++;

                buf1 = LPC_CANAF_RAM->mask[cnt1];

                LPC_CANAF_RAM->mask[cnt1] = buf0;

                buf0 = buf1;
            }
        }

        if (CANAF_std_cnt == 0)
        {
            cnt2 = (CANAF_FullCAN_cnt + 1)>>1;

            /* For entering first ID */
            LPC_CANAF_RAM->mask[cnt2] = 0x0000FFFF | (id << 16);
        }
        else if (CANAF_std_cnt == 1)
        {
            cnt2 = (CANAF_FullCAN_cnt + 1) >> 1;

            /* For entering second ID */
            if (((LPC_CANAF_RAM->mask[cnt2] >> 16)& 0xE7FF) > id)
            {
                LPC_CANAF_RAM->mask[cnt2] = (LPC_CANAF_RAM->mask[cnt2] >> 16) | (id << 16);
            }
            else
            {
                LPC_CANAF_RAM->mask[cnt2] = (LPC_CANAF_RAM->mask[cnt2] & 0xFFFF0000) | id;
            }
        }
        else
        {
            /* Find where to insert new ID */
            cnt1 = (CANAF_FullCAN_cnt+1)>>1;

            cnt2 = CANAF_std_cnt;

            bound1 = ((CANAF_FullCAN_cnt+1)>>1)+((CANAF_std_cnt+1)>>1);

            while (cnt1 < bound1)
            {
                /* Loop through standard existing IDs */
                if (((LPC_CANAF_RAM->mask[cnt1] >> 16) & 0xE7FF) > id)
                {
                    cnt2 = cnt1 * 2;
                    break;
                }

                if ((LPC_CANAF_RAM->mask[cnt1] & 0x0000E7FF) > id)
                {
                    cnt2 = cnt1 * 2 + 1;
                    break;
                }

                cnt1++;
            }

            /* cnt1 = U32 where to insert new ID */
            /* cnt2 = U16 where to insert new ID */

            if (cnt1 == bound1)
            {
                /* Adding ID as last entry */
                /* Even number of IDs exists */
                if ((CANAF_std_cnt & 0x0001) == 0)
                {
                    LPC_CANAF_RAM->mask[cnt1]  = 0x0000FFFF | (id << 16);
                }
                /* Odd  number of IDs exists */
                else
                {
                    LPC_CANAF_RAM->mask[cnt1]  = (LPC_CANAF_RAM->mask[cnt1] & 0xFFFF0000) | id;
                }
            }
            else
            {
                buf0 = LPC_CANAF_RAM->mask[cnt1]; /* Remember current entry */

                if ((cnt2 & 0x0001) == 0)
                {
                    /* Insert new mask to even address*/
                    buf1 = (id << 16) | (buf0 >> 16);
                }
                else
                {
                    /* Insert new mask to odd  address */
                    buf1 = (buf0 & 0xFFFF0000) | id;
                }

                LPC_CANAF_RAM->mask[cnt1] = buf1;/* Insert mask */

                bound1 = ((CANAF_FullCAN_cnt + 1) >> 1) + ((CANAF_std_cnt+1) >> 1) - 1;

                /* Move all remaining standard mask entries one place up */
                while (cnt1 < bound1)
                {
                    cnt1++;

                    buf1  = LPC_CANAF_RAM->mask[cnt1];

                    LPC_CANAF_RAM->mask[cnt1] = (buf1 >> 16) | (buf0 << 16);

                    buf0  = buf1;
                }

                if ((CANAF_std_cnt & 0x0001) == 0)
                {
                    /* Even number of IDs exists */
                    LPC_CANAF_RAM->mask[cnt1+1] = (buf0 <<16) |(0x0000FFFF);
                }
            }
        }

        CANAF_std_cnt++;

        //update address values
        LPC_CANAF->SFF_GRP_sa += 0x04 ;

        LPC_CANAF->EFF_sa     += 0x04 ;

        LPC_CANAF->EFF_GRP_sa += 0x04;

        LPC_CANAF->ENDofTable += 0x04;
    }

    /*********** Add Explicit Extended Identifier Frame Format entry *********/
    else
    {
        /* Add controller number */
        id |= canId << 29;

        cnt1 = ((CANAF_FullCAN_cnt+1) >> 1) + (((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt);

        cnt2 = 0;

        while (cnt2 < CANAF_ext_cnt)
        {
            /* Loop through extended existing masks*/
            if (LPC_CANAF_RAM->mask[cnt1] > id)
            {
                break;
            }

            cnt1++;/* cnt1 = U32 where to insert new mask */

            cnt2++;
        }

        buf0 = LPC_CANAF_RAM->mask[cnt1];  /* Remember current entry */

        LPC_CANAF_RAM->mask[cnt1] = id;    /* Insert mask */

        CANAF_ext_cnt++;

        bound1 = total;

        /* Move all remaining extended mask entries one place up*/
        while (cnt2 < bound1)
        {
            cnt1++;

            cnt2++;

            buf1 = LPC_CANAF_RAM->mask[cnt1];

            LPC_CANAF_RAM->mask[cnt1] = buf0;

            buf0 = buf1;
        }

        /* update address values */
        LPC_CANAF->EFF_GRP_sa += 4;

        LPC_CANAF->ENDofTable += 4;
    }

    if(CANAF_FullCAN_cnt == 0) //not use FullCAN mode
    {
        LPC_CANAF->AFMR = 0x00;//not use FullCAN mode
    }
    else
    {
        LPC_CANAF->AFMR = 0x04;
    }

    return CAN_OK;
}

/********************************************************************//**
 * @brief       Load FullCAN entry into AFLUT
 * @param[in]    canId           The Id of the expected CAN component
 *
 * @param[in]   id: identifier of entry that will be added
 * @return      CAN_ERROR, could be:
 *              - CAN_OK: loading is successful
 *              - CAN_ID_EXIT_ERROR: ID exited in FullCAN Section
 *              - CAN_OBJECTS_FULL_ERROR: no more space available
 *********************************************************************/
CAN_ERROR CAN_LoadFullCANEntry (uint8_t canId, uint16_t id)
{
    uint32_t buf0 = 0, buf1 = 0, buf2 = 0;
    uint32_t tmp0 = 0, tmp1 = 0, tmp2 = 0;
    int16_t cnt1 = 0, cnt2 = 0, bound1 = 0, total = 0;

    /* Acceptance Filter Memory full - return */
    total =((CANAF_FullCAN_cnt + 1) >> 1) + CANAF_FullCAN_cnt*3 + ((CANAF_std_cnt + 1) >> 1)  \
                + CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt << 1);

    //don't have enough space for this fullCAN Entry and its Object(3*32 bytes)
    if ((total >= 508) || (CANAF_FullCAN_cnt >= 64))
    {
        return CAN_OBJECTS_FULL_ERROR;
    }

    /* Setup Acceptance Filter Configuration
    Acceptance Filter Mode Register = Off */
    LPC_CANAF->AFMR = 0x00000001;

    /* Add mask for standard identifiers   */
    id &= 0x07FF;

    id |= (canId << 13) | (1 << 11);

    /* Move all remaining sections one place up
    if new entry will increase FullCAN list */
    if (((CANAF_FullCAN_cnt & 0x0001) == 0)&&(total!=0))
    {
        //then remove remaining section
        cnt1   = (CANAF_FullCAN_cnt >> 1);

        bound1 = total;

        buf0   = LPC_CANAF_RAM->mask[cnt1];

        while (bound1--)
        {
            cnt1++;

            buf1 = LPC_CANAF_RAM->mask[cnt1];

            LPC_CANAF_RAM->mask[cnt1] = buf0;

            buf0 = buf1;
        }
    }
    if (CANAF_FullCAN_cnt == 0)
    {
        /* For entering first ID */
        LPC_CANAF_RAM->mask[0] = 0x0000FFFF | (id << 16);
    }
    else if (CANAF_FullCAN_cnt == 1)
    {
        /* For entering second ID */
        if (((LPC_CANAF_RAM->mask[0] >> 16)& 0xE7FF) > id)
        {
            LPC_CANAF_RAM->mask[0] = (LPC_CANAF_RAM->mask[0] >> 16) | (id << 16);
        }
        else
        {
            LPC_CANAF_RAM->mask[0] = (LPC_CANAF_RAM->mask[0] & 0xFFFF0000) | id;
        }
    }
    else
    {
        /* Find where to insert new ID */
        cnt1 = 0;

        cnt2 = CANAF_FullCAN_cnt;

        bound1 = (CANAF_FullCAN_cnt - 1) >> 1;

        while (cnt1 <= bound1)
        {
            /* Loop through standard existing IDs */
            if (((LPC_CANAF_RAM->mask[cnt1] >> 16) & 0xE7FF) > (id & 0xE7FF))
            {
                cnt2 = cnt1 * 2;
                break;
            }


            if ((LPC_CANAF_RAM->mask[cnt1] & 0x0000E7FF) > (id & 0xE7FF))
            {
                cnt2 = cnt1 * 2 + 1;
                break;
            }

            cnt1++;
        }
        /* cnt1 = U32 where to insert new ID */
        /* cnt2 = U16 where to insert new ID */

        if (cnt1 > bound1)
        {
            /* Adding ID as last entry */
            /* Even number of IDs exists */
            if ((CANAF_FullCAN_cnt & 0x0001) == 0)
            {
                LPC_CANAF_RAM->mask[cnt1]  = 0x0000FFFF | (id << 16);
            }
            /* Odd  number of IDs exists */
            else
            {
                LPC_CANAF_RAM->mask[cnt1]  = (LPC_CANAF_RAM->mask[cnt1] & 0xFFFF0000) | id;
            }
        }
        else
        {
            buf0 = LPC_CANAF_RAM->mask[cnt1]; /* Remember current entry */

            if ((cnt2 & 0x0001) == 0)
            {
                /* Insert new mask to even address*/
                buf1 = (id << 16) | (buf0 >> 16);
            }
            else
            {
                /* Insert new mask to odd  address */
                buf1 = (buf0 & 0xFFFF0000) | id;
            }

            LPC_CANAF_RAM->mask[cnt1] = buf1;/* Insert mask */

            bound1 = CANAF_FullCAN_cnt >> 1;

            /* Move all remaining standard mask entries one place up */
            while (cnt1 < bound1)
            {
                cnt1++;

                buf1  = LPC_CANAF_RAM->mask[cnt1];

                LPC_CANAF_RAM->mask[cnt1] = (buf1 >> 16) | (buf0 << 16);

                buf0  = buf1;
            }

            if ((CANAF_FullCAN_cnt & 0x0001) == 0)
            {
                /* Even number of IDs exists */
                LPC_CANAF_RAM->mask[cnt1] = (LPC_CANAF_RAM->mask[cnt1] & 0xFFFF0000)
                                            | (0x0000FFFF);
            }
        }
    }

    //restruct FulCAN Object Section
    bound1 = CANAF_FullCAN_cnt - cnt2;

    cnt1 = total - (CANAF_FullCAN_cnt)*3 + cnt2*3 + 1;

    buf0 = LPC_CANAF_RAM->mask[cnt1];

    buf1 = LPC_CANAF_RAM->mask[cnt1+1];

    buf2 = LPC_CANAF_RAM->mask[cnt1+2];

    LPC_CANAF_RAM->mask[cnt1]=LPC_CANAF_RAM->mask[cnt1+1]= LPC_CANAF_RAM->mask[cnt1+2]=0x00;

    cnt1+=3;

    while(bound1--)
    {
        tmp0 = LPC_CANAF_RAM->mask[cnt1];

        tmp1 = LPC_CANAF_RAM->mask[cnt1+1];

        tmp2 = LPC_CANAF_RAM->mask[cnt1+2];

        LPC_CANAF_RAM->mask[cnt1]= buf0;

        LPC_CANAF_RAM->mask[cnt1+1]= buf1;

        LPC_CANAF_RAM->mask[cnt1+2]= buf2;

        buf0 = tmp0;

        buf1 = tmp1;

        buf2 = tmp2;

        cnt1+=3;
    }

    CANAF_FullCAN_cnt++;

    //update address values
    LPC_CANAF->SFF_sa     += 0x04;

    LPC_CANAF->SFF_GRP_sa += 0x04 ;

    LPC_CANAF->EFF_sa     += 0x04 ;

    LPC_CANAF->EFF_GRP_sa += 0x04;

    LPC_CANAF->ENDofTable += 0x04;

    LPC_CANAF->AFMR = 0x04;

    return CAN_OK;
}

/********************************************************************//**
 * @brief       Load Group entry into AFLUT
 * @param[in]    canId           The Id of the expected CAN component
 *
 * @param[in]   lowerID, upperID: lower and upper identifier of entry
 * @param[in]   format: type of ID format, should be:
 *              - STD_ID_FORMAT: Standard ID format (11-bit value)
 *              - EXT_ID_FORMAT: Extended ID format (29-bit value)
 * @return      CAN_ERROR, could be:
 *              - CAN_OK: loading is successful
 *              - CAN_CONFLICT_ID_ERROR: Conflict ID occurs
 *              - CAN_OBJECTS_FULL_ERROR: no more space available
 *********************************************************************/
CAN_ERROR CAN_LoadGroupEntry(uint8_t canId, uint32_t lowerID,
                                        uint32_t upperID, CAN_ID_FORMAT_Type format)
{
    uint32_t buf0, buf1, entry1, entry2, LID,UID;
    int16_t cnt1, bound1, total;

    if(lowerID > upperID)
        return CAN_CONFLICT_ID_ERROR;

    total =((CANAF_FullCAN_cnt+1) >> 1)+ CANAF_FullCAN_cnt*3 +((CANAF_std_cnt + 1) >> 1)  \
                + CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt<<1);

    /* Setup Acceptance Filter Configuration
    Acceptance Filter Mode Register = Off */
    LPC_CANAF->AFMR = 0x00000001;

    /*********Add Group of Standard Identifier Frame Format************/
    if(format == STD_ID_FORMAT)
    {
        if ((total >= 512))
        {
            //don't have enough space
            return CAN_OBJECTS_FULL_ERROR;
        }

        lowerID &=0x7FF; //mask ID

        upperID &=0x7FF;

        entry1  = (canId << 29) | (lowerID << 16) | (canId << 13)|(upperID << 0);

        cnt1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1);

        //if this is the first Group standard ID entry
        if(CANAF_gstd_cnt == 0)
        {
            LPC_CANAF_RAM->mask[cnt1] = entry1;
        }
        else
        {
            //find the position to add new Group entry
            bound1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt;

            while(cnt1 < bound1)
            {
                //compare controller first
                while((LPC_CANAF_RAM->mask[cnt1] >> 29)< (entry1 >> 29))//increase until meet greater or equal controller
                    cnt1++;
                buf0 = LPC_CANAF_RAM->mask[cnt1];
                if((LPC_CANAF_RAM->mask[cnt1] >> 29)> (entry1 >> 29)) //meet greater controller
                {
                    //add at this position
                    LPC_CANAF_RAM->mask[cnt1] = entry1;
                    break;
                }
                else //meet equal controller
                {
                    LID  = (buf0 >> 16)&0x7FF;
                    UID  = buf0 & 0x7FF;
                    if (upperID <= LID)
                    {
                        //add new entry before this entry
                        LPC_CANAF_RAM->mask[cnt1] = entry1;
                        break;
                    }
                    else if (lowerID >= UID)
                    {
                        //load next entry to compare
                        cnt1 ++;
                    }
                    else
                        return CAN_CONFLICT_ID_ERROR;
                }
            }
            if(cnt1 >= bound1)
            {
                //add new entry at the last position in this list
                buf0 = LPC_CANAF_RAM->mask[cnt1];

                LPC_CANAF_RAM->mask[cnt1] = entry1;
            }

            //remove all remaining entry of this section one place up
            bound1 = total - cnt1;

            while(bound1--)
            {
                cnt1++;

                buf1 = LPC_CANAF_RAM->mask[cnt1];

                LPC_CANAF_RAM->mask[cnt1] = buf0;

                buf0 = buf1;
            }
        }

        CANAF_gstd_cnt++;

        //update address values
        LPC_CANAF->EFF_sa     +=0x04 ;

        LPC_CANAF->EFF_GRP_sa +=0x04;

        LPC_CANAF->ENDofTable +=0x04;
    }


    /*********Add Group of Extended Identifier Frame Format************/
    else
    {
        if ((total >= 511))
        {
            //don't have enough space
            return CAN_OBJECTS_FULL_ERROR;
        }

        lowerID  &= 0x1FFFFFFF; //mask ID

        upperID &= 0x1FFFFFFF;

        entry1   = (canId << 29)|(lowerID << 0);

        entry2   = (canId << 29)|(upperID << 0);

        cnt1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt + CANAF_ext_cnt;

        //if this is the first Group standard ID entry
        if(CANAF_gext_cnt == 0)
        {
            LPC_CANAF_RAM->mask[cnt1] = entry1;

            LPC_CANAF_RAM->mask[cnt1+1] = entry2;
        }
        else
        {
            //find the position to add new Group entry
            bound1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt \
                        + CANAF_ext_cnt + (CANAF_gext_cnt<<1);

            while(cnt1 < bound1)
            {
                while((LPC_CANAF_RAM->mask[cnt1] >>29)< canId) //increase until meet greater or equal controller
                    cnt1++;
                buf0 = LPC_CANAF_RAM->mask[cnt1];

                buf1 = LPC_CANAF_RAM->mask[cnt1+1];
                if((LPC_CANAF_RAM->mask[cnt1] >> 29)> canId) //meet greater controller
                {
                    //add at this position
                    LPC_CANAF_RAM->mask[cnt1] = entry1;
                    LPC_CANAF_RAM->mask[++cnt1] = entry2;
                    break;
                }
                else //meet equal controller
                {
                    LID  = buf0 & 0x1FFFFFFF; //mask ID
                    UID  = buf1 & 0x1FFFFFFF;
                    if (upperID <= LID)
                    {
                        //add new entry before this entry
                        LPC_CANAF_RAM->mask[cnt1] = entry1;
                        LPC_CANAF_RAM->mask[++cnt1] = entry2;
                        break;
                    }
                    else if (lowerID >= UID)
                    {
                        //load next entry to compare
                        cnt1 +=2;
                    }
                    else
                        return CAN_CONFLICT_ID_ERROR;
                }
            }
            if(cnt1 >= bound1)
            {
                //add new entry at the last position in this list
                buf0 = LPC_CANAF_RAM->mask[cnt1];

                buf1 = LPC_CANAF_RAM->mask[cnt1+1];

                LPC_CANAF_RAM->mask[cnt1]   = entry1;

                LPC_CANAF_RAM->mask[++cnt1] = entry2;
            }

            //remove all remaining entry of this section two place up
            bound1 = total - cnt1 + 1;

            cnt1++;

            while(bound1>0)
            {
                entry1 = LPC_CANAF_RAM->mask[cnt1];

                entry2 = LPC_CANAF_RAM->mask[cnt1+1];

                LPC_CANAF_RAM->mask[cnt1]   = buf0;

                LPC_CANAF_RAM->mask[cnt1+1] = buf1;

                buf0 = entry1;

                buf1 = entry2;

                cnt1   +=2;

                bound1 -=2;
            }
        }

        CANAF_gext_cnt++;

        //update address values
        LPC_CANAF->ENDofTable +=0x08;
    }

    LPC_CANAF->AFMR = 0x04;

    return CAN_OK;
}

/********************************************************************//**
 * @brief       Remove AFLUT entry (FullCAN entry and Explicit Standard entry)
 * @param[in]   EntryType: the type of entry that want to remove, should be:
 *              - FULLCAN_ENTRY
 *              - EXPLICIT_STANDARD_ENTRY
 *              - GROUP_STANDARD_ENTRY
 *              - EXPLICIT_EXTEND_ENTRY
 *              - GROUP_EXTEND_ENTRY
 * @param[in]   position: the position of this entry in its section
 * Note: the first position is 0
 * @return      CAN_ERROR, could be:
 *              - CAN_OK: removing is successful
 *              - CAN_ENTRY_NOT_EXIT_ERROR: entry want to remove is not exit
 *********************************************************************/
CAN_ERROR CAN_RemoveEntry(AFLUT_ENTRY_Type EntryType, uint16_t position)
{
    uint16_t cnt, bound, total;
    uint32_t buf0, buf1;

    /* Setup Acceptance Filter Configuration
    Acceptance Filter Mode Register = Off */
    LPC_CANAF->AFMR = 0x00000001;

    total = ((CANAF_FullCAN_cnt + 1) >> 1) + ((CANAF_std_cnt + 1) >> 1) + \
                    + CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt << 1);


    /************** Remove FullCAN Entry *************/
    if(EntryType == FULLCAN_ENTRY)
    {
        if((CANAF_FullCAN_cnt == 0)||(position >= CANAF_FullCAN_cnt))
        {
            return CAN_ENTRY_NOT_EXIT_ERROR;
        }
        else
        {
            cnt = position >> 1;

            buf0 = LPC_CANAF_RAM->mask[cnt];

            bound = (CANAF_FullCAN_cnt - position -1)>>1;

            if((position & 0x0001) == 0) //event position
            {
                while(bound--)
                {
                    //remove all remaining FullCAN entry one place down
                    buf1  = LPC_CANAF_RAM->mask[cnt+1];

                    LPC_CANAF_RAM->mask[cnt] = (buf1 >> 16) | (buf0 << 16);

                    buf0  = buf1;

                    cnt++;
                }
            }
            else //odd position
            {
                while(bound--)
                {
                    //remove all remaining FullCAN entry one place down
                    buf1  = LPC_CANAF_RAM->mask[cnt+1];

                    LPC_CANAF_RAM->mask[cnt] = (buf0 & 0xFFFF0000)|(buf1 >> 16);

                    LPC_CANAF_RAM->mask[cnt+1] = LPC_CANAF_RAM->mask[cnt+1] << 16;

                    buf0  = buf1<<16;

                    cnt++;
                }
            }
            if((CANAF_FullCAN_cnt & 0x0001) == 0)
            {
                if((position & 0x0001)==0)
                    LPC_CANAF_RAM->mask[cnt] = (buf0 << 16) | (0x0000FFFF);
                else
                    LPC_CANAF_RAM->mask[cnt] = buf0 | 0x0000FFFF;
            }
            else
            {
                //remove all remaining section one place down
                cnt = (CANAF_FullCAN_cnt + 1)>>1;

                bound = total + CANAF_FullCAN_cnt * 3;

                while(bound>cnt)
                {
                    LPC_CANAF_RAM->mask[cnt-1] = LPC_CANAF_RAM->mask[cnt];
                    cnt++;
                }

                LPC_CANAF_RAM->mask[cnt-1]=0x00;

                //update address values
                LPC_CANAF->SFF_sa     -= 0x04;

                LPC_CANAF->SFF_GRP_sa -= 0x04 ;

                LPC_CANAF->EFF_sa     -= 0x04 ;

                LPC_CANAF->EFF_GRP_sa -= 0x04;

                LPC_CANAF->ENDofTable -= 0x04;
            }

            CANAF_FullCAN_cnt--;

            //delete its FullCAN Object in the FullCAN Object section
            //remove all remaining FullCAN Object three place down
            cnt = total + position * 3;

            bound = (CANAF_FullCAN_cnt - position + 1) * 3;

            while(bound)
            {
                LPC_CANAF_RAM->mask[cnt]= LPC_CANAF_RAM->mask[cnt+3];;

                LPC_CANAF_RAM->mask[cnt+1]= LPC_CANAF_RAM->mask[cnt+4];

                LPC_CANAF_RAM->mask[cnt+2]= LPC_CANAF_RAM->mask[cnt+5];

                bound -= 3;

                cnt   += 3;
            }
        }
    }

    /************** Remove Explicit Standard ID Entry *************/
    else if(EntryType == EXPLICIT_STANDARD_ENTRY)
    {
        if((CANAF_std_cnt == 0)||(position >= CANAF_std_cnt))
        {
            return CAN_ENTRY_NOT_EXIT_ERROR;
        }
        else
        {
            cnt = ((CANAF_FullCAN_cnt+1) >> 1) + (position >> 1);

            buf0 = LPC_CANAF_RAM->mask[cnt];

            bound = (CANAF_std_cnt - position - 1) >> 1;

            if((position & 0x0001) == 0) //event position
            {
                while(bound--)
                {
                    //remove all remaining FullCAN entry one place down
                    buf1  = LPC_CANAF_RAM->mask[cnt + 1];

                    LPC_CANAF_RAM->mask[cnt] = (buf1 >> 16) | (buf0 << 16);

                    buf0  = buf1;

                    cnt++;
                }
            }
            else //odd position
            {
                while(bound--)
                {
                    //remove all remaining FullCAN entry one place down
                    buf1  = LPC_CANAF_RAM->mask[cnt + 1];

                    LPC_CANAF_RAM->mask[cnt] = (buf0 & 0xFFFF0000) | (buf1 >> 16);

                    LPC_CANAF_RAM->mask[cnt + 1] = LPC_CANAF_RAM->mask[cnt + 1] << 16;

                    buf0  = buf1<<16;

                    cnt++;
                }
            }
            if((CANAF_std_cnt & 0x0001) == 0)
            {
                if((position & 0x0001)==0)
                    LPC_CANAF_RAM->mask[cnt] = (buf0 << 16) | (0x0000FFFF);
                else
                    LPC_CANAF_RAM->mask[cnt] = buf0 | 0x0000FFFF;
            }
            else
            {
                //remove all remaining section one place down
                cnt = ((CANAF_FullCAN_cnt + 1)>>1) + ((CANAF_std_cnt + 1) >> 1);

                bound = total + CANAF_FullCAN_cnt * 3;

                while(bound>cnt)
                {
                    LPC_CANAF_RAM->mask[cnt-1] = LPC_CANAF_RAM->mask[cnt];
                    cnt++;
                }

                LPC_CANAF_RAM->mask[cnt-1]=0x00;

                //update address value
                LPC_CANAF->SFF_GRP_sa -= 0x04 ;

                LPC_CANAF->EFF_sa     -= 0x04 ;

                LPC_CANAF->EFF_GRP_sa -= 0x04;

                LPC_CANAF->ENDofTable -= 0x04;
            }

            CANAF_std_cnt--;
        }
    }

    /************** Remove Group of Standard ID Entry *************/
    else if(EntryType == GROUP_STANDARD_ENTRY)
    {
        if((CANAF_gstd_cnt == 0)||(position >= CANAF_gstd_cnt))
        {
            return CAN_ENTRY_NOT_EXIT_ERROR;
        }
        else
        {
            cnt = ((CANAF_FullCAN_cnt + 1) >> 1) + ((CANAF_std_cnt + 1) >> 1)+ position + 1;

            bound = total + CANAF_FullCAN_cnt * 3;

            while (cnt < bound)
            {
                LPC_CANAF_RAM->mask[cnt - 1] = LPC_CANAF_RAM->mask[cnt];
                cnt++;
            }
            LPC_CANAF_RAM->mask[cnt - 1]=0x00;
        }

        CANAF_gstd_cnt--;

        //update address value
        LPC_CANAF->EFF_sa     -= 0x04;

        LPC_CANAF->EFF_GRP_sa -= 0x04;

        LPC_CANAF->ENDofTable -= 0x04;
    }

    /************** Remove Explicit Extended ID Entry *************/
    else if(EntryType == EXPLICIT_EXTEND_ENTRY)
    {
        if((CANAF_ext_cnt == 0)||(position >= CANAF_ext_cnt))
        {
            return CAN_ENTRY_NOT_EXIT_ERROR;
        }
        else
        {
            cnt = ((CANAF_FullCAN_cnt + 1) >> 1) + ((CANAF_std_cnt + 1) >> 1)+ CANAF_gstd_cnt + position + 1;

            bound = total + CANAF_FullCAN_cnt * 3;

            while (cnt<bound)
            {
                LPC_CANAF_RAM->mask[cnt - 1] = LPC_CANAF_RAM->mask[cnt];
                cnt++;
            }
            LPC_CANAF_RAM->mask[cnt - 1]=0x00;
        }

        CANAF_ext_cnt--;

        LPC_CANAF->EFF_GRP_sa -= 0x04;

        LPC_CANAF->ENDofTable -= 0x04;
    }

    /************** Remove Group of Extended ID Entry *************/
    else
    {
        if((CANAF_gext_cnt == 0)||(position >= CANAF_gext_cnt))
        {
            return CAN_ENTRY_NOT_EXIT_ERROR;
        }
        else
        {
            cnt = total - (CANAF_gext_cnt << 1) + (position << 1);

            bound = total + CANAF_FullCAN_cnt * 3;

            while (cnt<bound)
            {
                //remove all remaining entry two place up
                LPC_CANAF_RAM->mask[cnt] = LPC_CANAF_RAM->mask[cnt + 2];

                LPC_CANAF_RAM->mask[cnt + 1] = LPC_CANAF_RAM->mask[cnt + 3];

                cnt += 2;
            }
        }

        CANAF_gext_cnt--;

        LPC_CANAF->ENDofTable -= 0x08;
    }

    LPC_CANAF->AFMR = 0x04;

    return CAN_OK;
}

/********************************************************************//**
 * @brief       Send message data
 * @param[in]    canId           The Id of the expected CAN component
 *
 * @param[in]   CAN_Msg point to the CAN_MSG_Type Structure, it contains message
 *              information such as: ID, DLC, RTR, ID Format
 * @return      Status:
 *              - SUCCESS: send message successfully
 *              - ERROR: send message unsuccessfully
 *********************************************************************/
Status CAN_SendMsg (uint8_t canId, CAN_MSG_Type *CAN_Msg)
{
    LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);

    uint32_t data;

    //Check status of Transmit Buffer 1
    if (pCan->SR & (1 << 2))
    {
        /* Transmit Channel 1 is available */
        /* Write frame informations and frame data into its CANxTFI1,
         * CANxTID1, CANxTDA1, CANxTDB1 register */
        pCan->TFI1 &= ~ 0x000F0000;

        pCan->TFI1 |= (CAN_Msg->len) << 16;

        if(CAN_Msg->type == REMOTE_FRAME)
        {
            pCan->TFI1 |= (1 << 30); //set bit RTR
        }
        else
        {
            pCan->TFI1 &= ~(1 << 30);
        }

        if(CAN_Msg->format == EXT_ID_FORMAT)
        {
            pCan->TFI1 |= (((uint32_t)1) << 31); //set bit FF
        }
        else
        {
            pCan->TFI1 &= ~(((uint32_t)1) << 31);
        }

        /* Write CAN ID*/
        pCan->TID1 = CAN_Msg->id;

        /*Write first 4 data bytes*/
        data = (CAN_Msg->dataA[0]) | (((CAN_Msg->dataA[1]))<< 8) | ((CAN_Msg->dataA[2]) << 16) | ((CAN_Msg->dataA[3]) << 24);

        pCan->TDA1 = data;

        /*Write second 4 data bytes*/
        data = (CAN_Msg->dataB[0]) | (((CAN_Msg->dataB[1])) << 8)|((CAN_Msg->dataB[2]) << 16)|((CAN_Msg->dataB[3]) << 24);

        pCan->TDB1 = data;

         /*Write transmission request*/
         pCan->CMR = 0x21;

         return SUCCESS;
    }

    //check status of Transmit Buffer 2
    else if((pCan->SR) & (1 << 10))
    {
        /* Transmit Channel 2 is available */
        /* Write frame informations and frame data into its CANxTFI2,
         * CANxTID2, CANxTDA2, CANxTDB2 register */
        pCan->TFI2 &= ~0x000F0000;

        pCan->TFI2 |= (CAN_Msg->len) << 16;

        if(CAN_Msg->type == REMOTE_FRAME)
        {
            pCan->TFI2 |= (1 << 30); //set bit RTR
        }
        else
        {
            pCan->TFI2 &= ~(1 << 30);
        }

        if(CAN_Msg->format == EXT_ID_FORMAT)
        {
            pCan->TFI2 |= (((uint32_t)1) << 31); //set bit FF
        }
        else
        {
            pCan->TFI2 &= ~(((uint32_t)1) << 31);
        }

        /* Write CAN ID*/
        pCan->TID2 = CAN_Msg->id;

        /*Write first 4 data bytes*/
        data = (CAN_Msg->dataA[0]) | (((CAN_Msg->dataA[1])) << 8) | ((CAN_Msg->dataA[2]) << 16)|((CAN_Msg->dataA[3]) << 24);

        pCan->TDA2 = data;

        /*Write second 4 data bytes*/
        data = (CAN_Msg->dataB[0]) | (((CAN_Msg->dataB[1])) << 8) | ((CAN_Msg->dataB[2]) << 16) | ((CAN_Msg->dataB[3]) << 24);

        pCan->TDB2 = data;

        /*Write transmission request*/
        pCan->CMR = 0x41;

        return SUCCESS;
    }

    //check status of Transmit Buffer 3
    else if (pCan->SR & (1<<18))
    {
        /* Transmit Channel 3 is available */
        /* Write frame informations and frame data into its CANxTFI3,
         * CANxTID3, CANxTDA3, CANxTDB3 register */
        pCan->TFI3 &= ~0x000F0000;

        pCan->TFI3 |= (CAN_Msg->len) << 16;

        if(CAN_Msg->type == REMOTE_FRAME)
        {
            pCan->TFI3 |= (1 << 30); //set bit RTR
        }
        else
        {
            pCan->TFI3 &= ~(1 << 30);
        }

        if(CAN_Msg->format == EXT_ID_FORMAT)
        {
            pCan->TFI3 |= (((uint32_t)1) << 31); //set bit FF
        }
        else
        {
            pCan->TFI3 &= ~(((uint32_t)1) << 31);
        }

        /* Write CAN ID*/
        pCan->TID3 = CAN_Msg->id;

        /*Write first 4 data bytes*/
        data = (CAN_Msg->dataA[0]) | (((CAN_Msg->dataA[1])) << 8) | ((CAN_Msg->dataA[2]) << 16) | ((CAN_Msg->dataA[3]) << 24);

        pCan->TDA3 = data;

        /*Write second 4 data bytes*/
        data = (CAN_Msg->dataB[0]) | (((CAN_Msg->dataB[1])) << 8) | ((CAN_Msg->dataB[2]) << 16) | ((CAN_Msg->dataB[3]) << 24);

        pCan->TDB3 = data;

        /*Write transmission request*/
        pCan->CMR = 0x81;

        return SUCCESS;
    }
    else
    {
        return ERROR;
    }
}

/********************************************************************//**
 * @brief       Receive message data
 * @param[in]    canId           The Id of the expected CAN component
 *
 * @param[in]   CAN_Msg point to the CAN_MSG_Type Struct, it will contain received
 *              message information such as: ID, DLC, RTR, ID Format
 * @return      Status:
 *              - SUCCESS: receive message successfully
 *              - ERROR: receive message unsuccessfully
 *********************************************************************/
Status CAN_ReceiveMsg (uint8_t canId, CAN_MSG_Type *CAN_Msg)
{
    LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);

    uint32_t data;

    //check status of Receive Buffer
    if((pCan->SR &0x00000001))
    {
        /* Receive message is available */
        /* Read frame informations */
        CAN_Msg->format = (uint8_t)(((pCan->RFS) & 0x80000000) >> 31);

        CAN_Msg->type = (uint8_t)(((pCan->RFS) & 0x40000000) >> 30);

        CAN_Msg->len = (uint8_t)(((pCan->RFS) & 0x000F0000) >> 16);

        /* Read CAN message identifier */
        CAN_Msg->id = pCan->RID;

        /* Read the data if received message was DATA FRAME */
        if (CAN_Msg->type == DATA_FRAME)
        {
            /* Read first 4 data bytes */
            data = pCan->RDA;

            *((uint8_t *) &CAN_Msg->dataA[0])= data & 0x000000FF;

            *((uint8_t *) &CAN_Msg->dataA[1])= (data & 0x0000FF00) >> 8;;

            *((uint8_t *) &CAN_Msg->dataA[2])= (data & 0x00FF0000) >> 16;

            *((uint8_t *) &CAN_Msg->dataA[3])= (data & 0xFF000000) >> 24;

            /* Read second 4 data bytes */
            data = pCan->RDB;

            *((uint8_t *) &CAN_Msg->dataB[0])= data & 0x000000FF;

            *((uint8_t *) &CAN_Msg->dataB[1])= (data & 0x0000FF00) >> 8;

            *((uint8_t *) &CAN_Msg->dataB[2])= (data & 0x00FF0000) >> 16;

            *((uint8_t *) &CAN_Msg->dataB[3])= (data & 0xFF000000) >> 24;

            /*release receive buffer*/
            pCan->CMR = 0x04;
        }
        else
        {
            /* Received Frame is a Remote Frame, not have data, we just receive
             * message information only */
            pCan->CMR = 0x04; /*release receive buffer*/

            return SUCCESS;
        }
    }
    else
    {
        // no receive message available
        return ERROR;
    }

    return SUCCESS;
}

/********************************************************************//**
 * @brief       Receive FullCAN Object
 * @param[in]   CANAFx: CAN Acceptance Filter register, should be: LPC_CANAF
 * @param[in]   CAN_Msg point to the CAN_MSG_Type Struct, it will contain received
 *              message information such as: ID, DLC, RTR, ID Format
 * @return      CAN_ERROR, could be:
 *              - CAN_FULL_OBJ_NOT_RCV: FullCAN Object is not be received
 *              - CAN_OK: Received FullCAN Object successful
 *
 *********************************************************************/
CAN_ERROR FCAN_ReadObj (CAN_MSG_Type *CAN_Msg)
{
    uint32_t *pSrc, data;
    uint32_t interrut_word, msg_idx, test_bit, head_idx, tail_idx;

    interrut_word = 0;

    if (LPC_CANAF->FCANIC0 != 0)
    {
        interrut_word = LPC_CANAF->FCANIC0;

        head_idx = 0;

        tail_idx = 31;
    }
    else if (LPC_CANAF->FCANIC1 != 0)
    {
        interrut_word = LPC_CANAF->FCANIC1;

        head_idx = 32;

        tail_idx = 63;
    }

    if (interrut_word != 0)
    {
        /* Detect for interrupt pending */
        msg_idx = 0;

        for (msg_idx = head_idx; msg_idx <= tail_idx; msg_idx++)
        {
            test_bit = interrut_word & 0x1;

            interrut_word = interrut_word >> 1;

            if (test_bit)
            {
                pSrc = (uint32_t *) (LPC_CANAF->ENDofTable + LPC_CANAF_RAM_BASE + msg_idx * 12);

                /* Has been finished updating the content */
                if ((*pSrc & 0x03000000L) == 0x03000000L)
                {
                    /*clear semaphore*/
                    *pSrc &= 0xFCFFFFFF;

                    /*Set to DatA*/
                    pSrc++;

                    /* Copy to dest buf */
                    data = *pSrc;

                    *((uint8_t *) &CAN_Msg->dataA[0])= data & 0x000000FF;

                    *((uint8_t *) &CAN_Msg->dataA[1])= (data & 0x0000FF00) >> 8;

                    *((uint8_t *) &CAN_Msg->dataA[2])= (data & 0x00FF0000) >> 16;

                    *((uint8_t *) &CAN_Msg->dataA[3])= (data & 0xFF000000) >> 24;

                    /*Set to DatB*/
                    pSrc++;

                    /* Copy to dest buf */
                    data = *pSrc;

                    *((uint8_t *) &CAN_Msg->dataB[0])= data & 0x000000FF;

                    *((uint8_t *) &CAN_Msg->dataB[1])= (data & 0x0000FF00) >> 8;

                    *((uint8_t *) &CAN_Msg->dataB[2])= (data & 0x00FF0000) >> 16;

                    *((uint8_t *) &CAN_Msg->dataB[3])= (data & 0xFF000000) >> 24;

                    /*Back to Dat1*/
                    pSrc -= 2;

                    CAN_Msg->id = *pSrc & 0x7FF;

                    CAN_Msg->len = (uint8_t) (*pSrc >> 16) & 0x0F;

                    CAN_Msg->format = 0; //FullCAN Object ID always is 11-bit value

                    CAN_Msg->type = (uint8_t)(*pSrc >> 30) &0x01;

                    /*Re-read semaphore*/
                    if ((*pSrc & 0x03000000L) == 0)
                    {
                        return CAN_OK;
                    }
                }
            }
        }
    }

    return CAN_FULL_OBJ_NOT_RCV;
}

/********************************************************************//**
 * @brief       Get CAN Control Status
 * @param[in]    canId           The Id of the expected CAN component
 *
 * @param[in]   arg: type of CAN status to get from CAN status register
 *              Should be:
 *              - CANCTRL_GLOBAL_STS: CAN Global Status
 *              - CANCTRL_INT_CAP: CAN Interrupt and Capture
 *              - CANCTRL_ERR_WRN: CAN Error Warning Limit
 *              - CANCTRL_STS: CAN Control Status
 * @return      Current Control Status that you want to get value
 *********************************************************************/
uint32_t CAN_GetCTRLStatus (uint8_t canId, CAN_CTRL_STS_Type arg)
{
    LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);

    switch (arg)
    {
        case CANCTRL_GLOBAL_STS:
            return pCan->GSR;

        case CANCTRL_INT_CAP:
            return pCan->ICR;

        case CANCTRL_ERR_WRN:
            return pCan->EWL;

        default: // CANCTRL_STS
            return pCan->SR;
    }
}
/********************************************************************//**
 * @brief       Get CAN Central Status
 * @param[in]   CANCRx point to LPC_CANCR_TypeDef, should be: LPC_CANCR
 * @param[in]   arg: type of CAN status to get from CAN Central status register
 *              Should be:
 *              - CANCR_TX_STS: Central CAN Tx Status
 *              - CANCR_RX_STS: Central CAN Rx Status
 *              - CANCR_MS: Central CAN Miscellaneous Status
 * @return      Current Central Status that you want to get value
 *********************************************************************/
uint32_t CAN_GetCRStatus (CAN_CR_STS_Type arg)
{
    switch (arg)
    {
        case CANCR_TX_STS:
            return LPC_CANCR->TxSR;

        case CANCR_RX_STS:
            return LPC_CANCR->RxSR;

        default:    // CANCR_MS
            return LPC_CANCR->MSR;
    }
}
/********************************************************************//**
 * @brief       Enable/Disable CAN Interrupt
 * @param[in]    canId           The Id of the expected CAN component
 *
 * @param[in]   arg: type of CAN interrupt that you want to enable/disable
 *              Should be:
 *              - CANINT_RIE: CAN Receiver Interrupt Enable
 *              - CANINT_TIE1: CAN Transmit Interrupt Enable
 *              - CANINT_EIE: CAN Error Warning Interrupt Enable
 *              - CANINT_DOIE: CAN Data Overrun Interrupt Enable
 *              - CANINT_WUIE: CAN Wake-Up Interrupt Enable
 *              - CANINT_EPIE: CAN Error Passive Interrupt Enable
 *              - CANINT_ALIE: CAN Arbitration Lost Interrupt Enable
 *              - CANINT_BEIE: CAN Bus Error Interrupt Enable
 *              - CANINT_IDIE: CAN ID Ready Interrupt Enable
 *              - CANINT_TIE2: CAN Transmit Interrupt Enable for Buffer2
 *              - CANINT_TIE3: CAN Transmit Interrupt Enable for Buffer3
 *              - CANINT_FCE: FullCAN Interrupt Enable
 * @param[in]   NewState: New state of this function, should be:
 *              - ENABLE
 *              - DISABLE
 * @return      none
 *********************************************************************/
void CAN_IRQCmd (uint8_t canId, CAN_INT_EN_Type arg, FunctionalState NewState)
{
    LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);

    if(NewState == ENABLE)
    {
        if(arg == CANINT_FCE)
        {
            LPC_CANAF->AFMR = 0x01;

            LPC_CANAF->FCANIE = 0x01;

            LPC_CANAF->AFMR = 0x04;
        }
        else
            pCan->IER |= (1 << arg);
    }
    else
    {
        if(arg == CANINT_FCE)
        {
            LPC_CANAF->AFMR = 0x01;

            LPC_CANAF->FCANIE = 0x01;

            LPC_CANAF->AFMR = 0x00;
        }
        else
            pCan->IER &= ~(1 << arg);
    }
}

/********************************************************************//**
 * @brief       Setting Acceptance Filter mode
 * @param[in]   CANAFx point to LPC_CANAF_TypeDef object, should be: LPC_CANAF
 * @param[in]   AFMode: type of AF mode that you want to set, should be:
 *              - CAN_NORMAL: Normal mode
 *              - CAN_ACC_OFF: Acceptance Filter Off Mode
 *              - CAN_ACC_BP: Acceptance Fileter Bypass Mode
 *              - CAN_EFCAN: FullCAN Mode Enhancement
 * @return      none
 *********************************************************************/
void CAN_SetAFMode (CAN_AFMODE_Type AFMode)
{
    switch(AFMode)
    {
        case CAN_NORMAL:
            LPC_CANAF->AFMR = 0x00;
            break;

        case CAN_ACC_OFF:
            LPC_CANAF->AFMR = 0x01;
            break;

        case CAN_ACC_BP:
            LPC_CANAF->AFMR = 0x02;
            break;

        case CAN_EFCAN:
            LPC_CANAF->AFMR = 0x04;
            break;
    }
}

/********************************************************************//**
 * @brief       Enable/Disable CAN Mode
 * @param[in]    canId           The Id of the expected CAN component
 *
 * @param[in]   mode: type of CAN mode that you want to enable/disable, should be:
 *              - CAN_OPERATING_MODE: Normal Operating Mode
 *              - CAN_RESET_MODE: Reset Mode
 *              - CAN_LISTENONLY_MODE: Listen Only Mode
 *              - CAN_SELFTEST_MODE: Self Test Mode
 *              - CAN_TXPRIORITY_MODE: Transmit Priority Mode
 *              - CAN_SLEEP_MODE: Sleep Mode
 *              - CAN_RXPOLARITY_MODE: Receive Polarity Mode
 *              - CAN_TEST_MODE: Test Mode
 * @param[in]   NewState: New State of this function, should be:
 *              - ENABLE
 *              - DISABLE
 * @return      none
 *********************************************************************/
void CAN_ModeConfig(uint8_t canId, CAN_MODE_Type mode, FunctionalState NewState)
{
    LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);

    switch(mode)
    {
        case CAN_OPERATING_MODE:
            pCan->MOD = 0x00;
            break;

        case CAN_RESET_MODE:
            if(NewState == ENABLE)
                pCan->MOD |= CAN_MOD_RM;
            else
                pCan->MOD &= ~CAN_MOD_RM;

            break;

        case CAN_LISTENONLY_MODE:
            pCan->MOD |=CAN_MOD_RM;//Enter Reset mode

            if(NewState == ENABLE)
                pCan->MOD |= CAN_MOD_LOM;
            else
                pCan->MOD &= ~ CAN_MOD_LOM;

            pCan->MOD &= ~ CAN_MOD_RM;//Release Reset mode

            break;

        case CAN_SELFTEST_MODE:
            pCan->MOD |= CAN_MOD_RM;//Enter Reset mode

            if(NewState == ENABLE)
                pCan->MOD |= CAN_MOD_STM;
            else
                pCan->MOD &= ~ CAN_MOD_STM;

            pCan->MOD &= ~ CAN_MOD_RM;//Release Reset mode

            break;

        case CAN_TXPRIORITY_MODE:
            if(NewState == ENABLE)
                pCan->MOD |= CAN_MOD_TPM;
            else
                pCan->MOD &= ~ CAN_MOD_TPM;

            break;

        case CAN_SLEEP_MODE:
            if(NewState == ENABLE)
                pCan->MOD |= CAN_MOD_SM;
            else
                pCan->MOD &= ~ CAN_MOD_SM;

            break;

        case CAN_RXPOLARITY_MODE:
            if(NewState == ENABLE)
                pCan->MOD |= CAN_MOD_RPM;
            else
                pCan->MOD &= ~ CAN_MOD_RPM;

            break;

        case CAN_TEST_MODE:
            if(NewState == ENABLE)
                pCan->MOD |= CAN_MOD_TM;
            else
                pCan->MOD &= ~ CAN_MOD_TM;

            break;
    }
}
/*********************************************************************//**
 * @brief       Set CAN command request
 * @param[in]    canId           The Id of the expected CAN component
 *
 * @param[in]   CMRType command request type, should be:
 *              - CAN_CMR_TR: Transmission request
 *              - CAN_CMR_AT: Abort Transmission request
 *              - CAN_CMR_RRB: Release Receive Buffer request
 *              - CAN_CMR_CDO: Clear Data Overrun request
 *              - CAN_CMR_SRR: Self Reception request
 *              - CAN_CMR_STB1: Select Tx Buffer 1 request
 *              - CAN_CMR_STB2: Select Tx Buffer 2 request
 *              - CAN_CMR_STB3: Select Tx Buffer 3 request
 * @return      CANICR (CAN interrupt and Capture register) value
 **********************************************************************/
void CAN_SetCommand(uint8_t canId, uint32_t CMRType)
{
    LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);

    pCan->CMR |= CMRType;
}

/*********************************************************************//**
 * @brief       Get CAN interrupt status
 * @param[in]    canId           The Id of the expected CAN component
 *
 * @return      CANICR (CAN interrupt and Capture register) value
 **********************************************************************/
uint32_t CAN_IntGetStatus(uint8_t canId)
{
    LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);

    return pCan->ICR;
}

/*********************************************************************//**
 * @brief       Check if FullCAN interrupt enable or not
 * @param[in]   CANAFx point to LPC_CANAF_TypeDef object, should be: LPC_CANAF
 * @return      IntStatus, could be:
 *              - SET: if FullCAN interrupt is enable
 *              - RESET: if FullCAN interrupt is disable
 **********************************************************************/
IntStatus CAN_FullCANIntGetStatus (void)
{
    if (LPC_CANAF->FCANIE)
        return SET;

    return RESET;
}

/*********************************************************************//**
 * @brief       Get value of FullCAN interrupt and capture register
 * @param[in]   CANAFx point to LPC_CANAF_TypeDef object, should be: LPC_CANAF
 * @param[in]   type: FullCAN IC type, should be:
 *              - FULLCAN_IC0: FullCAN Interrupt Capture 0
 *              - FULLCAN_IC1: FullCAN Interrupt Capture 1
 * @return      FCANIC0 or FCANIC1 (FullCAN interrupt and Capture register) value
 **********************************************************************/
uint32_t CAN_FullCANPendGetStatus(FullCAN_IC_Type type)
{
    if (type == FULLCAN_IC0)
        return LPC_CANAF->FCANIC0;

    return LPC_CANAF->FCANIC1;
}
/* End of Public Variables ---------------------------------------------------------- */
/**
 * @}
 */
#endif /*_CAN*/

/**
 * @}
 */

/* --------------------------------- End Of File ------------------------------ */