lpc_uart.c 53.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
/**********************************************************************
* $Id$      lpc_uart.c          2011-06-02
*//**
* @file     lpc_uart.c
* @brief    Contains all functions support for UART 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 UART
 * @{
 */
#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc_libcfg.h"
#else
#include "lpc_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */
#ifdef _UART

/* Includes ------------------------------------------------------------------- */
#include "lpc_uart.h"
#include "lpc_clkpwr.h"

/* Private Functions ---------------------------------------------------------- */

static Status uart_set_divisors(UART_ID_Type UartID, uint32_t baudrate);
static LPC_UART_TypeDef *uart_get_pointer(UART_ID_Type UartID);


/*********************************************************************//**
 * @brief       Determines best dividers to get a target clock rate
 * @param[in]   UARTx   Pointer to selected UART peripheral, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @param[in]   baudrate Desired UART baud rate.
 * @return      Error status, could be:
 *              - SUCCESS
 *              - ERROR
 **********************************************************************/
static Status uart_set_divisors(UART_ID_Type UartID, uint32_t baudrate)
{
    Status errorStatus = ERROR;

    uint32_t uClk;
    uint32_t d, m, bestd, bestm, tmp;
    uint64_t best_divisor, divisor;
    uint32_t current_error, best_error;
    uint32_t recalcbaud;

    /* get UART block clock */
    uClk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);

    /* In the Uart IP block, baud rate is calculated using FDR and DLL-DLM registers
    * The formula is :
    * BaudRate= uClk * (mulFracDiv/(mulFracDiv+dividerAddFracDiv) / (16 * (DLL)
    * It involves floating point calculations. That's the reason the formulae are adjusted with
    * Multiply and divide method.*/
    
    /* The value of mulFracDiv and dividerAddFracDiv should comply to the following expressions:
    * 0 < mulFracDiv <= 15, 0 <= dividerAddFracDiv <= 15 */
    best_error = 0xFFFFFFFF; /* Worst case */
    bestd = 0;
    bestm = 0;
    best_divisor = 0;
    
    for (m = 1 ; m <= 15 ;m++)
    {
        for (d = 0 ; d < m ; d++)
        {
            divisor = ((uint64_t)uClk << 28)*m / (baudrate*(m+d));
            current_error = divisor & 0xFFFFFFFF;

            tmp = divisor>>32;

            /* Adjust error */
            if(current_error > ((uint32_t)1<<31))
            {
                current_error = -current_error;
                tmp++;
            }

            /* Out of range */
            if(tmp < 1 || tmp > 65536)
                continue;

            if( current_error < best_error)
            {
                best_error = current_error;
                best_divisor = tmp;
                bestd = d;
                bestm = m;
                
                if(best_error == 0) 
                    break;
            }
        } /* end of inner for loop */

        if (best_error == 0)
            break;
    } /* end of outer for loop  */

    /* can not find best match */
    if(best_divisor == 0) 
        return ERROR;

    recalcbaud = (uClk >> 4) * bestm / (best_divisor * (bestm + bestd));

    /* reuse best_error to evaluate baud error*/
    if(baudrate > recalcbaud) 
        best_error = baudrate - recalcbaud;
    else 
        best_error = recalcbaud -baudrate;

    best_error = best_error * 100 / baudrate;

    if (best_error < UART_ACCEPTED_BAUDRATE_ERROR)
    {
        if (UartID == UART_1)
        {
            LPC_UART1->LCR |= UART_LCR_DLAB_EN;
            
            LPC_UART1->DLM = UART_LOAD_DLM(best_divisor);
            
            LPC_UART1->DLL = UART_LOAD_DLL(best_divisor);
            
            /* Then reset DLAB bit */
            LPC_UART1->LCR &= (~UART_LCR_DLAB_EN) & UART_LCR_BITMASK;
            
            LPC_UART1->FDR = (UART_FDR_MULVAL(bestm)
                                                    | UART_FDR_DIVADDVAL(bestd)) & UART_FDR_BITMASK;
        }
        else if (UartID == UART_4)
        {
            LPC_UART4->LCR |= UART_LCR_DLAB_EN;
            
            LPC_UART4->DLM = UART_LOAD_DLM(best_divisor);
            
            LPC_UART4->DLL = UART_LOAD_DLL(best_divisor);
            
            /* Then reset DLAB bit */
            LPC_UART4->LCR &= (~UART_LCR_DLAB_EN) & UART_LCR_BITMASK;
            
            LPC_UART4->FDR = (UART_FDR_MULVAL(bestm)
                                                    | UART_FDR_DIVADDVAL(bestd)) & UART_FDR_BITMASK;
        }
            
        else
        {
            LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
            UARTx->LCR |= UART_LCR_DLAB_EN;
            
            UARTx->DLM = UART_LOAD_DLM(best_divisor);
            
            UARTx->DLL = UART_LOAD_DLL(best_divisor);
            
            /* Then reset DLAB bit */
            UARTx->LCR &= (~UART_LCR_DLAB_EN) & UART_LCR_BITMASK;
            
            UARTx->FDR = (UART_FDR_MULVAL(bestm) \
                            | UART_FDR_DIVADDVAL(bestd)) & UART_FDR_BITMASK;
        }
        errorStatus = SUCCESS;
    }

    return errorStatus;
}
/*********************************************************************//**
 * @brief       Get the pointer of a given Uart
 * @param[in]   UARTx   Pointer to selected UART peripheral, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @return      LPC_UART0~LPC_UART4
 **********************************************************************/
LPC_UART_TypeDef *uart_get_pointer(UART_ID_Type UartID)
{
    LPC_UART_TypeDef *UARTx = NULL;
    switch(UartID)
    {
        case UART_0:
            UARTx = LPC_UART0;
            break;
        case UART_2:
            UARTx = LPC_UART2;
            break;
        case UART_3:
            UARTx = LPC_UART3;
            break;
        default:
            break;
    }
    return UARTx;
}

/* End of Private Functions ---------------------------------------------------- */


/* Public Functions ----------------------------------------------------------- */
/** @addtogroup UART_Public_Functions
 * @{
 */
/* UART Init/DeInit functions -------------------------------------------------*/
/********************************************************************//**
 * @brief       Initializes the UARTx peripheral according to the specified
 *               parameters in the UART_ConfigStruct.
 * @param[in]   UARTx   UART peripheral selected, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @param[in]   UART_ConfigStruct Pointer to a UART_CFG_Type structure
*                    that contains the configuration information for the
*                    specified UART peripheral.
 * @return      None
 *********************************************************************/
void UART_Init(UART_ID_Type UartID, UART_CFG_Type *UART_ConfigStruct)
{
    uint32_t tmp;
    switch (UartID)
    {
        case UART_0:
        case UART_2:
        case UART_3:
        {
            LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
            if(UartID == UART_0)
                /* Set up clock and power for UART module */
                CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART0, ENABLE);
            else if(UartID == UART_2)
                /* Set up clock and power for UART module */
                CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART2, ENABLE);
            else if(UartID == UART_3)
                /* Set up clock and power for UART module */
                CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART3, ENABLE);;
            /* FIFOs are empty */
            UARTx->FCR = ( UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS);

            // Disable FIFO
            UARTx->FCR = 0;

            // Dummy reading
            while (UARTx->LSR & UART_LSR_RDR)
            {
                tmp = UARTx->RBR;
            }

            UARTx->TER = UART_TER_TXEN;

            // Wait for current transmit complete
            while (!(UARTx->LSR & UART_LSR_THRE));

            // Disable Tx
            UARTx->TER = 0;

            // Disable interrupt
            UARTx->IER = 0;

            // Set LCR to default state
            UARTx->LCR = 0;

            // Set ACR to default state
            UARTx->ACR = 0;

            // Set RS485 control to default state
            UARTx->RS485CTRL = 0;

            // Set RS485 delay timer to default state
            UARTx->RS485DLY = 0;

            // Set RS485 addr match to default state
            UARTx->ADRMATCH = 0;

            // Dummy reading
            tmp = UARTx->LSR;
        }
        break;
        case UART_1:
        {
            /* Set up clock and power for UART module */
            CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART1, ENABLE);

            /* FIFOs are empty */
            LPC_UART1->FCR = ( UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS);

            // Disable FIFO
            LPC_UART1->FCR = 0;

            // Dummy reading
            while (LPC_UART1->LSR & UART_LSR_RDR)
            {
                tmp = LPC_UART1->RBR;
            }

            LPC_UART1->TER = UART_TER_TXEN;

            // Wait for current transmit complete
            while (!(LPC_UART1->LSR & UART_LSR_THRE));

            // Disable Tx
            LPC_UART1->TER = 0;

            // Disable interrupt
            LPC_UART1->IER = 0;

            // Set LCR to default state
            LPC_UART1->LCR = 0;

            // Set ACR to default state
            LPC_UART1->ACR = 0;

            // Set RS485 control to default state
            LPC_UART1->RS485CTRL = 0;

            // Set RS485 delay timer to default state
            LPC_UART1->RS485DLY = 0;

            // Set RS485 addr match to default state
            LPC_UART1->ADRMATCH = 0;

            // Dummy reading
            tmp = LPC_UART1->LSR;

            // Set Modem Control to default state
            LPC_UART1->MCR = 0;

            //Dummy Reading to Clear Status
            tmp = LPC_UART1->MSR;
        }
        break;
        case UART_4:
        {
            /* Set up clock and power for UART module */
            CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART4, ENABLE);

            /* FIFOs are empty */
            LPC_UART4->FCR = ( UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS);

            // Disable FIFO
            LPC_UART4->FCR = 0;

            // Dummy reading
            while (LPC_UART4->LSR & UART_LSR_RDR)
            {
                tmp = LPC_UART4->RBR;
            }

            LPC_UART4->TER = UART4_TER_TXEN;

            // Wait for current transmit complete
            while (!(LPC_UART4->LSR & UART_LSR_THRE));

            // Disable Tx
            LPC_UART4->TER = 0;

            // Disable interrupt
            LPC_UART4->IER = 0;

            // Set LCR to default state
            LPC_UART4->LCR = 0;

            // Set ACR to default state
            LPC_UART4->ACR = 0;

            // Set RS485 control to default state
            LPC_UART4->RS485CTRL = 0;

            // Set RS485 delay timer to default state
            LPC_UART4->RS485DLY = 0;

            // Set RS485 addr match to default state
            LPC_UART4->ADRMATCH = 0;

            // Dummy reading
            tmp = LPC_UART4->LSR;

            // Set IrDA Mode to default state
            LPC_UART4->ICR = 0;
        }
        break;
    }

    // Set Line Control register ----------------------------

    uart_set_divisors(UartID, (UART_ConfigStruct->Baud_rate));

    if (UartID == UART_1)
    {
        tmp = (LPC_UART1->LCR & (UART_LCR_DLAB_EN | UART_LCR_BREAK_EN)) \
                                                    & UART_LCR_BITMASK;
    }
    else if (UartID == UART_4)
    {
        tmp = (LPC_UART4->LCR & (UART_LCR_DLAB_EN | UART_LCR_BREAK_EN)) \
                                                    & UART_LCR_BITMASK;
    }   
    else
    {
        LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
        tmp = (UARTx->LCR & (UART_LCR_DLAB_EN | UART_LCR_BREAK_EN)) & UART_LCR_BITMASK;
    }

    switch (UART_ConfigStruct->Databits)
    {
        case UART_DATABIT_5:
            tmp |= UART_LCR_WLEN5;
            break;

        case UART_DATABIT_6:
            tmp |= UART_LCR_WLEN6;
            break;

        case UART_DATABIT_7:
            tmp |= UART_LCR_WLEN7;
            break;

        case UART_DATABIT_8:

        default:
            tmp |= UART_LCR_WLEN8;
            break;
    }

    if (UART_ConfigStruct->Parity == UART_PARITY_NONE)
    {
        // Do nothing...
    }
    else
    {
        tmp |= UART_LCR_PARITY_EN;
        switch (UART_ConfigStruct->Parity)
        {
            case UART_PARITY_ODD:
                tmp |= UART_LCR_PARITY_ODD;
                break;

            case UART_PARITY_EVEN:
                tmp |= UART_LCR_PARITY_EVEN;
                break;

            case UART_PARITY_SP_1:
                tmp |= UART_LCR_PARITY_F_1;
                break;

            case UART_PARITY_SP_0:
                tmp |= UART_LCR_PARITY_F_0;
                break;

            default:
                break;
        }
    }

    switch (UART_ConfigStruct->Stopbits)
    {
        case UART_STOPBIT_2:
            tmp |= UART_LCR_STOPBIT_SEL;
            break;

        case UART_STOPBIT_1:

        default:
            // Do no thing
            break;
    }


    // Write back to LCR, configure FIFO and Disable Tx
    if (UartID == UART_1)
    {
        LPC_UART1->LCR = (uint8_t)(tmp & UART_LCR_BITMASK);
    }
    else if (UartID == UART_4)
    {
        LPC_UART4->LCR = (uint8_t)(tmp & UART_LCR_BITMASK);
    }   
    else
    {
        LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
        UARTx->LCR = (uint8_t)(tmp & UART_LCR_BITMASK);
    }
}

/*********************************************************************//**
 * @brief       De-initializes the UARTx peripheral registers to their
 *                  default reset values.
 * @param[in]   UartID  UART peripheral selected, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @return      None
 **********************************************************************/
void UART_DeInit(UART_ID_Type UartID)
{
    UART_TxCmd(UartID, DISABLE);
    if (UartID == UART_0)
    {
        /* Set up clock and power for UART module */
        CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART0, DISABLE);
    }

    else if (UartID == UART_1)
    {
        /* Set up clock and power for UART module */
        CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART1, DISABLE);
    }

    else if (UartID == UART_2)
    {
        /* Set up clock and power for UART module */
        CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART2, DISABLE);
    }

    else if (UartID == UART_3)
    {
        /* Set up clock and power for UART module */
        CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART3, DISABLE);
    }
    else if (UartID == UART_4)
    {
        /* Set up clock and power for UART module */
        CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART4, DISABLE);
    }
}

/*****************************************************************************//**
* @brief        Fills each UART_InitStruct member with its default value:
*               - 115200 bps
*               - 8-bit data
*               - 1 Stopbit
*               - None Parity
* @param[in]    UART_InitStruct Pointer to a UART_CFG_Type structure
*                    which will be initialized.
* @return       None
*******************************************************************************/
void UART_ConfigStructInit(UART_CFG_Type *UART_InitStruct)
{
    UART_InitStruct->Baud_rate = 115200;

    UART_InitStruct->Databits = UART_DATABIT_8;

    UART_InitStruct->Parity = UART_PARITY_NONE;

    UART_InitStruct->Stopbits = UART_STOPBIT_1;
}

/* UART Send/Recieve functions -------------------------------------------------*/
/*********************************************************************//**
 * @brief       Transmit a single data through UART peripheral
 * @param[in]   UARTx   UART peripheral selected, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @param[in]   Data    Data to transmit (must be 8-bit long)
 * @return      None
 **********************************************************************/
void UART_SendByte(UART_ID_Type UartID, uint8_t Data)
{
    switch (UartID)
    {
        case UART_0:
            LPC_UART0->THR = Data & UART_THR_MASKBIT;
            break;
        case UART_1:
            LPC_UART1->THR = Data & UART_THR_MASKBIT;
            break;  
        case UART_2:
            LPC_UART2->THR = Data & UART_THR_MASKBIT;
            break;
        case UART_3:
            LPC_UART3->THR = Data & UART_THR_MASKBIT;
            break;
        case UART_4:
            LPC_UART4->THR = Data & UART_THR_MASKBIT;
            break;
    }

}


/*********************************************************************//**
 * @brief       Receive a single data from UART peripheral
 * @param[in]   UARTx   UART peripheral selected, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @return      Data received
 **********************************************************************/
uint8_t UART_ReceiveByte(UART_ID_Type UartID)
{
    switch (UartID)
    {
        case UART_0:
            return (LPC_UART0->RBR & UART_RBR_MASKBIT);
        case UART_1:
            return (LPC_UART1->RBR & UART_RBR_MASKBIT); 
        case UART_2:
            return (LPC_UART2->RBR & UART_RBR_MASKBIT);
        case UART_3:
            return (LPC_UART3->RBR & UART_RBR_MASKBIT);
        case UART_4:
            return (LPC_UART4->RBR & UART_RBR_MASKBIT);
    }
    return 0x00;
}

/*********************************************************************//**
 * @brief       Send a block of data via UART peripheral
 * @param[in]   UARTx   Selected UART peripheral used to send data, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @param[in]   txbuf   Pointer to Transmit buffer
 * @param[in]   buflen  Length of Transmit buffer
 * @param[in]   flag    Flag used in  UART transfer, should be
 *                      NONE_BLOCKING or BLOCKING
 * @return      Number of bytes sent.
 *
 * Note: when using UART in BLOCKING mode, a time-out condition is used
 * via defined symbol UART_BLOCKING_TIMEOUT.
 **********************************************************************/
uint32_t UART_Send(UART_ID_Type UartID, uint8_t *txbuf,
                            uint32_t buflen, TRANSFER_BLOCK_Type flag)
{
    uint32_t bToSend, bSent, timeOut, fifo_cnt;
    uint8_t *pChar = txbuf;
    __IO uint32_t *LSR = NULL;

    switch (UartID)
    {
        case UART_0:
            LSR = (__IO uint32_t *)&LPC_UART0->LSR;
            break;
        case UART_1:
            LSR = (__IO uint32_t *)&LPC_UART1->LSR;
            break;
        case UART_2:
            LSR = (__IO uint32_t *)&LPC_UART2->LSR;
            break;
        case UART_3:
            LSR = (__IO uint32_t *)&LPC_UART3->LSR;
            break;
        case UART_4:
            LSR = (__IO uint32_t *)&LPC_UART4->LSR;
            break;
    }

    bToSend = buflen;

    // blocking mode
    if (flag == BLOCKING)
    {
        bSent = 0;
        while (bToSend)
        {
            timeOut = UART_BLOCKING_TIMEOUT;

            // Wait for THR empty with timeout
            while (!(*LSR & UART_LSR_THRE))
            {
                if (timeOut == 0)
                    break;

                timeOut--;
            }

            // Time out!
            if(timeOut == 0)
                break;

            fifo_cnt = UART_TX_FIFO_SIZE;

            while (fifo_cnt && bToSend)
            {
                UART_SendByte(UartID, (*pChar++));

                fifo_cnt--;

                bToSend--;

                bSent++;
            }
        }
    }

    // None blocking mode
    else
    {
        bSent = 0;
        while (bToSend)
        {
            if (bToSend == 0)
                break;

            if (!(*LSR & UART_LSR_THRE))
            {
                break;
            }

            fifo_cnt = UART_TX_FIFO_SIZE;

            while (fifo_cnt && bToSend)
            {
                UART_SendByte(UartID, (*pChar++));

                bToSend--;

                fifo_cnt--;

                bSent++;
            }
        }
    }

    return bSent;
}

/*********************************************************************//**
 * @brief       Receive a block of data via UART peripheral
 * @param[in]   UARTx   Selected UART peripheral used to send data,
 *              should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @param[out]  rxbuf   Pointer to Received buffer
 * @param[in]   buflen  Length of Received buffer
 * @param[in]   flag    Flag mode, should be NONE_BLOCKING or BLOCKING

 * @return      Number of bytes received
 *
 * Note: when using UART in BLOCKING mode, a time-out condition is used
 * via defined symbol UART_BLOCKING_TIMEOUT.
 **********************************************************************/
uint32_t UART_Receive(UART_ID_Type UartID, uint8_t *rxbuf,
                                uint32_t buflen, TRANSFER_BLOCK_Type flag)
{
    uint32_t bToRecv, bRecv, timeOut;
    uint8_t *pChar = rxbuf;
    __IO uint32_t *LSR = NULL;

    switch (UartID)
    {
        case UART_0:
            LSR = (__IO uint32_t *)&LPC_UART0->LSR;
            break;
        case UART_1:
            LSR = (__IO uint32_t *)&LPC_UART1->LSR;
            break;
        case UART_2:
            LSR = (__IO uint32_t *)&LPC_UART2->LSR;
            break;
        case UART_3:
            LSR = (__IO uint32_t *)&LPC_UART3->LSR;
            break;
        case UART_4:
            LSR = (__IO uint32_t *)&LPC_UART4->LSR;
            break;
    }
    
    bToRecv = buflen;

    // Blocking mode
    if (flag == BLOCKING)
    {
        bRecv = 0;
        while (bToRecv)
        {
            timeOut = UART_BLOCKING_TIMEOUT;
            while (!(*LSR & UART_LSR_RDR))
            {
                if (timeOut == 0)
                    break;

                timeOut--;
            }

            // Time out!
            if(timeOut == 0)
                break;

            // Get data from the buffer
            (*pChar++) = UART_ReceiveByte(UartID);

            bToRecv--;

            bRecv++;
        }
    }
    // None blocking mode
    else
    {
        bRecv = 0;
        while (bToRecv)
        {
            if (!(*LSR & UART_LSR_RDR))
            {
                break;
            }
            else
            {
                (*pChar++) = UART_ReceiveByte(UartID);

                bRecv++;

                bToRecv--;
            }
        }
    }

    return bRecv;
}

/*********************************************************************//**
 * @brief       Force BREAK character on UART line, output pin UARTx TXD is
                forced to logic 0.
 * @param[in]   UARTx   UART peripheral selected, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @return      None
 **********************************************************************/
void UART_ForceBreak(UART_ID_Type UartID)
{
    switch (UartID)
    {
        case UART_0:
            LPC_UART0->LCR |= UART_LCR_BREAK_EN;
            break;
        case UART_1:
            LPC_UART1->LCR |= UART_LCR_BREAK_EN;
            break;
        case UART_2:
            LPC_UART2->LCR |= UART_LCR_BREAK_EN;
            break;
        case UART_3:
            LPC_UART3->LCR |= UART_LCR_BREAK_EN;
            break;
        case UART_4:
            LPC_UART4->LCR |= UART_LCR_BREAK_EN;
            break;
    }
}


/********************************************************************//**
 * @brief       Enable or disable specified UART interrupt.
 * @param[in]   UARTx   UART peripheral selected, should be
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @param[in]   UARTIntCfg  Specifies the interrupt flag,
 *              should be one of the following:
                - UART_INTCFG_RBR   :  RBR Interrupt enable
                - UART_INTCFG_THRE  :  THR Interrupt enable
                - UART_INTCFG_RLS   :  RX line status interrupt enable
                - UART1_INTCFG_MS   :  Modem status interrupt enable (UART1 only)
                - UART1_INTCFG_CTS  :  CTS1 signal transition interrupt enable (UART1 only)
                - UART_INTCFG_ABEO  :  Enables the end of auto-baud interrupt
                - UART_INTCFG_ABTO  :  Enables the auto-baud time-out interrupt
 * @param[in]   NewState New state of specified UART interrupt type,
 *              should be:
 *              - ENALBE: Enable this UART interrupt type.
*               - DISALBE: Disable this UART interrupt type.
 * @return      None
 *********************************************************************/
void UART_IntConfig(UART_ID_Type UartID, UART_INT_Type UARTIntCfg, FunctionalState NewState)
{
    uint32_t tmp;
    __IO uint32_t *IER = NULL;
    uint32_t IERMask = 0;

    switch (UartID)
    {
        case UART_0:
            IER = &LPC_UART0->IER;
            IERMask = UART_IER_BITMASK;
            break;
        case UART_1:
            IER = &LPC_UART1->IER;
            IERMask = UART1_IER_BITMASK;
            break;
        case UART_2:
            IER = &LPC_UART2->IER;
            IERMask = UART_IER_BITMASK;
            break;
        case UART_3:
            IER = &LPC_UART3->IER;
            IERMask = UART_IER_BITMASK;
            break;
        case UART_4:
            IER = &LPC_UART4->IER;
            IERMask = UART_IER_BITMASK;
            break;
    }


    switch(UARTIntCfg)
    {
        case UART_INTCFG_RBR:
            tmp = UART_IER_RBRINT_EN;
            break;

        case UART_INTCFG_THRE:
            tmp = UART_IER_THREINT_EN;
            break;

        case UART_INTCFG_RLS:
            tmp = UART_IER_RLSINT_EN;
            break;

        case UART_INTCFG_MS:
            tmp = UART1_IER_MSINT_EN;
            break;

        case UART_INTCFG_CTS:
            tmp = UART1_IER_CTSINT_EN;
            break;

        case UART_INTCFG_ABEO:
            tmp = UART_IER_ABEOINT_EN;
            break;

        case UART_INTCFG_ABTO:
            tmp = UART_IER_ABTOINT_EN;
            break;
    }

    if (NewState == ENABLE)
    {
        *IER |= tmp& IERMask;
    }
    else
    {
        *IER &= (~tmp) & IERMask;
    }
}


/********************************************************************//**
 * @brief       Get current value of Line Status register in UART peripheral.
 * @param[in]   UARTx   UART peripheral selected, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @return      Current value of Line Status register in UART peripheral.
 * Note:    The return value of this function must be ANDed with each member in
 *          UART_LS_Type enumeration to determine current flag status
 *          corresponding to each Line status type. Because some flags in
 *          Line Status register will be cleared after reading, the next reading
 *          Line Status register could not be correct. So this function used to
 *          read Line status register in one time only, then the return value
 *          used to check all flags.
 *********************************************************************/
uint8_t UART_GetLineStatus(UART_ID_Type UartID)
{
    switch (UartID)
    {
        case UART_0:
            return ((LPC_UART0->LSR) & UART_LSR_BITMASK);
        case UART_1:
            return ((LPC_UART1->LSR) & UART_LSR_BITMASK);
        case UART_2:
            return ((LPC_UART2->LSR) & UART_LSR_BITMASK);
        case UART_3:
            return ((LPC_UART3->LSR) & UART_LSR_BITMASK);
        case UART_4:
            return ((LPC_UART4->LSR) & UART_LSR_BITMASK);
    }
    return 0;
}

/********************************************************************//**
 * @brief       Get Interrupt Identification value
 * @param[in]   UARTx   UART peripheral selected, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @return      Current value of UART UIIR register in UART peripheral.
 *********************************************************************/
uint32_t UART_GetIntId(UART_ID_Type UartID)
{
    switch (UartID)
    {
        case UART_0:
            return ((LPC_UART0->IIR) & UART_IIR_BITMASK);
        case UART_1:
            return ((LPC_UART1->IIR) & UART_IIR_BITMASK);
        case UART_2:
            return ((LPC_UART2->IIR) & UART_IIR_BITMASK);
        case UART_3:
            return ((LPC_UART3->IIR) & UART_IIR_BITMASK);
        case UART_4:
            return ((LPC_UART4->IIR) & UART_IIR_BITMASK);
    }
    return 0;
}

/*********************************************************************//**
 * @brief       Check whether if UART is busy or not
 * @param[in]   UARTx   UART peripheral selected, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @return      RESET if UART is not busy, otherwise return SET.
 **********************************************************************/
FlagStatus UART_CheckBusy(UART_ID_Type UartID)
{
    uint32_t LSR = 0;
    switch (UartID)
    {
        case UART_0:
            LSR = (LPC_UART0)->LSR & UART_LSR_TEMT;
            break;
        case UART_1:
            LSR = (LPC_UART1)->LSR & UART_LSR_TEMT;
            break;
        case UART_2:
            LSR = (LPC_UART2)->LSR & UART_LSR_TEMT;
            break;
        case UART_3:
            LSR = (LPC_UART3)->LSR & UART_LSR_TEMT;
            break;
        case UART_4:
            LSR = (LPC_UART4)->LSR & UART_LSR_TEMT;
            break;
    }
    
    if (LSR & UART_LSR_TEMT)
    {
        return RESET;
    }
    return SET;
}


/*********************************************************************//**
 * @brief       Configure FIFO function on selected UART peripheral
 * @param[in]   UARTx   UART peripheral selected, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @param[in]   FIFOCfg Pointer to a UART_FIFO_CFG_Type Structure that
 *                      contains specified information about FIFO configuration
 * @return      none
 **********************************************************************/
void UART_FIFOConfig(UART_ID_Type UartID, UART_FIFO_CFG_Type *FIFOCfg)
{
    uint8_t tmp = 0;

    tmp |= UART_FCR_FIFO_EN;

    switch (FIFOCfg->FIFO_Level)
    {
        case UART_FIFO_TRGLEV0:
            tmp |= UART_FCR_TRG_LEV0;
            break;

        case UART_FIFO_TRGLEV1:
            tmp |= UART_FCR_TRG_LEV1;
            break;

        case UART_FIFO_TRGLEV2:
            tmp |= UART_FCR_TRG_LEV2;
            break;

        case UART_FIFO_TRGLEV3:

        default:
            tmp |= UART_FCR_TRG_LEV3;
            break;
    }

    if (FIFOCfg->FIFO_ResetTxBuf == ENABLE)
    {
        tmp |= UART_FCR_TX_RS;
    }

    if (FIFOCfg->FIFO_ResetRxBuf == ENABLE)
    {
        tmp |= UART_FCR_RX_RS;
    }

    if (FIFOCfg->FIFO_DMAMode == ENABLE)
    {
        tmp |= UART_FCR_DMAMODE_SEL;
    }


    //write to FIFO control register
    switch (UartID)
    {
        case UART_0:
            LPC_UART0->FCR = tmp & UART_FCR_BITMASK;
            break;
        case UART_1:
            LPC_UART1->FCR = tmp & UART_FCR_BITMASK;
            break;
        case UART_2:
            LPC_UART2->FCR = tmp & UART_FCR_BITMASK;
            break;
        case UART_3:
            LPC_UART3->FCR = tmp & UART_FCR_BITMASK;
            break;
        case UART_4:
            LPC_UART4->FCR = tmp & UART_FCR_BITMASK;
            break;
    }
}

/*****************************************************************************//**
* @brief        Fills each UART_FIFOInitStruct member with its default value:
*               - FIFO_DMAMode = DISABLE
*               - FIFO_Level = UART_FIFO_TRGLEV0
*               - FIFO_ResetRxBuf = ENABLE
*               - FIFO_ResetTxBuf = ENABLE
*               - FIFO_State = ENABLE
* @param[in]    UART_FIFOInitStruct Pointer to a UART_FIFO_CFG_Type structure
*                    which will be initialized.
* @return       None
*******************************************************************************/
void UART_FIFOConfigStructInit(UART_FIFO_CFG_Type *UART_FIFOInitStruct)
{
    UART_FIFOInitStruct->FIFO_DMAMode = DISABLE;

    UART_FIFOInitStruct->FIFO_Level = UART_FIFO_TRGLEV0;

    UART_FIFOInitStruct->FIFO_ResetRxBuf = ENABLE;

    UART_FIFOInitStruct->FIFO_ResetTxBuf = ENABLE;
}


/*********************************************************************//**
 * @brief       Start/Stop Auto Baudrate activity
 * @param[in]   UARTx   UART peripheral selected, should be
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @param[in]   ABConfigStruct  A pointer to UART_AB_CFG_Type structure that
 *                              contains specified information about UART
 *                              auto baudrate configuration
 * @param[in]   NewState New State of Auto baudrate activity, should be:
 *              - ENABLE: Start this activity
 *              - DISABLE: Stop this activity
 * Note:        Auto-baudrate mode enable bit will be cleared once this mode
 *              completed.
 * @return      none
 **********************************************************************/
void UART_ABCmd(UART_ID_Type UartID, UART_AB_CFG_Type *ABConfigStruct,
                            FunctionalState NewState)
{
    uint32_t tmp;

    tmp = 0;
    if (NewState == ENABLE)
    {
        if (ABConfigStruct->ABMode == UART_AUTOBAUD_MODE1)
        {
            tmp |= UART_ACR_MODE;
        }
        if (ABConfigStruct->AutoRestart == ENABLE)
        {
            tmp |= UART_ACR_AUTO_RESTART;
        }
    }

    if (UartID == UART_1)
    {
        if (NewState == ENABLE)
        {
            // Clear DLL and DLM value
            LPC_UART1->LCR |= UART_LCR_DLAB_EN;

            LPC_UART1->DLL = 0;

            LPC_UART1->DLM = 0;

            LPC_UART1->LCR &= ~UART_LCR_DLAB_EN;

            // FDR value must be reset to default value
            LPC_UART1->FDR = 0x10;

            LPC_UART1->ACR = UART_ACR_START | tmp;
        }
        else
        {
            LPC_UART1->ACR = 0;
        }
    }
    else if (UartID == UART_4)
    {
        if (NewState == ENABLE)
        {
            // Clear DLL and DLM value
            LPC_UART4->LCR |= UART_LCR_DLAB_EN;

            LPC_UART4->DLL = 0;

            LPC_UART4->DLM = 0;

            LPC_UART4->LCR &= ~UART_LCR_DLAB_EN;

            // FDR value must be reset to default value
            LPC_UART4->FDR = 0x10;

            LPC_UART4->ACR = UART_ACR_START | tmp;
        }
        else
        {
            LPC_UART4->ACR = 0;
        }
    }
    else
    {
        LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
        if (NewState == ENABLE)
        {
            // Clear DLL and DLM value
            UARTx->LCR |= UART_LCR_DLAB_EN;

            UARTx->DLL = 0;

            UARTx->DLM = 0;

            UARTx->LCR &= ~UART_LCR_DLAB_EN;

            // FDR value must be reset to default value
            UARTx->FDR = 0x10;

            UARTx->ACR = UART_ACR_START | tmp;
        }
        else
        {
            UARTx->ACR = 0;
        }
    }
}

/*********************************************************************//**
 * @brief       Clear Autobaud Interrupt Pending
 * @param[in]   UARTx   UART peripheral selected, should be
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @param[in]   ABIntType   type of auto-baud interrupt, should be:
 *              - UART_AUTOBAUD_INTSTAT_ABEO: End of Auto-baud interrupt
 *              - UART_AUTOBAUD_INTSTAT_ABTO: Auto-baud time out interrupt
 * @return      none
 **********************************************************************/
void UART_ABClearIntPending(UART_ID_Type UartID, UART_ABEO_Type ABIntType)
{
    if (UartID == UART_1)
    {
        LPC_UART1->ACR |= ABIntType;
    }
    else if (UartID == UART_4)
    {
        LPC_UART4->ACR |= ABIntType;
    }
    else
    {   
        LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
        UARTx->ACR |= ABIntType;
    }
}

/*********************************************************************//**
 * @brief       Enable/Disable transmission on UART TxD pin
 * @param[in]   UARTx   UART peripheral selected, should be:
 *              - UART_0: UART0 peripheral
 *              - UART_1: UART1 peripheral
 *              - UART_2: UART2 peripheral
 *              - UART_3: UART3 peripheral
 *              - UART_4: UART4 peripheral
 * @param[in]   NewState New State of Tx transmission function, should be:
 *              - ENABLE: Enable this function
                - DISABLE: Disable this function
 * @return none
 **********************************************************************/
void UART_TxCmd(UART_ID_Type UartID, FunctionalState NewState)
{
    if (NewState == ENABLE)
    {
        if (UartID == UART_1)
        {
            LPC_UART1->TER |= UART_TER_TXEN;
        }
        else if (UartID == UART_4)
        {
            LPC_UART4->TER |= UART4_TER_TXEN;
        }
        else
        {
            LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
            UARTx->TER |= UART_TER_TXEN;
        }
    }
    else
    {
        if (UartID == UART_1)                     
        {
            LPC_UART1->TER &= (~UART_TER_TXEN) & UART_TER_BITMASK;
        }
        else if (UartID == UART_4)
        {
            LPC_UART4->TER &= (~UART4_TER_TXEN) & UART4_TER_BITMASK;
        }
        else
        {
            LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
            UARTx->TER &= (~UART_TER_TXEN) & UART_TER_BITMASK;
        }
    }
}

/* UART IrDA functions ---------------------------------------------------*/
/*********************************************************************//**
 * @brief       Enable or disable inverting serial input function of IrDA
 *              on UART peripheral.
 * @param[in]   UARTx UART peripheral selected, should be LPC_UART4 (only)
 * @param[in]   NewState New state of inverting serial input, should be:
 *              - ENABLE: Enable this function.
 *              - DISABLE: Disable this function.
 * @return none
 **********************************************************************/
void UART_IrDAInvtInputCmd(UART_ID_Type UartID, FunctionalState NewState)
{
    if (UartID != UART_4)
        return;
    
    if (NewState == ENABLE)
    {
        LPC_UART4->ICR |= UART_ICR_IRDAINV;
    }
    else if (NewState == DISABLE)
    {
        LPC_UART4->ICR &= (~UART_ICR_IRDAINV) & UART_ICR_BITMASK;
    }
    
}


/*********************************************************************//**
 * @brief       Enable or disable IrDA function on UART peripheral.
 * @param[in]   UARTx UART peripheral selected, should be LPC_UART4 (only)
 * @param[in]   NewState New state of IrDA function, should be:
 *              - ENABLE: Enable this function.
 *              - DISABLE: Disable this function.
 * @return none
 **********************************************************************/
void UART_IrDACmd(UART_ID_Type UartID, FunctionalState NewState)
{
    if (UartID != UART_4)
        return;
    
    if (NewState == ENABLE)
    {
        LPC_UART4->ICR |= UART_ICR_IRDAEN;
    }
    else
    {
        LPC_UART4->ICR &= (~UART_ICR_IRDAEN) & UART_ICR_BITMASK;
    }
}


/*********************************************************************//**
 * @brief       Configure Pulse divider for IrDA function on UART peripheral.
 * @param[in]   UARTx UART peripheral selected, should be LPC_UART4 (only)
 * @param[in]   PulseDiv Pulse Divider value from Peripheral clock,
 *              should be one of the following:
                - UART_IrDA_PULSEDIV2   : Pulse width = 2 * Tpclk
                - UART_IrDA_PULSEDIV4   : Pulse width = 4 * Tpclk
                - UART_IrDA_PULSEDIV8   : Pulse width = 8 * Tpclk
                - UART_IrDA_PULSEDIV16  : Pulse width = 16 * Tpclk
                - UART_IrDA_PULSEDIV32  : Pulse width = 32 * Tpclk
                - UART_IrDA_PULSEDIV64  : Pulse width = 64 * Tpclk
                - UART_IrDA_PULSEDIV128 : Pulse width = 128 * Tpclk
                - UART_IrDA_PULSEDIV256 : Pulse width = 256 * Tpclk

 * @return none
 **********************************************************************/
void UART_IrDAPulseDivConfig(UART_ID_Type UartID, UART_IrDA_PULSE_Type PulseDiv)
{
    uint32_t tmp, tmp1;

    if (UartID != UART_4)
        return;

    tmp1 = UART_ICR_PULSEDIV(PulseDiv);

    tmp = LPC_UART4->ICR & (~ UART_ICR_PULSEDIV(7));

    tmp |= tmp1 | UART_ICR_FIXPULSE_EN;

    LPC_UART4->ICR = tmp & UART_ICR_BITMASK;
}

/* UART1 FullModem function ---------------------------------------------*/

/*********************************************************************//**
 * @brief       Force pin DTR/RTS corresponding to given state (Full modem mode)
 * @param[in]   UARTx   LPC_UART1 (only)
 * @param[in]   Pin Pin that NewState will be applied to, should be:
 *              - UART1_MODEM_PIN_DTR: DTR pin.
 *              - UART1_MODEM_PIN_RTS: RTS pin.
 * @param[in]   NewState New State of DTR/RTS pin, should be:
 *              - INACTIVE: Force the pin to inactive signal.
                - ACTIVE: Force the pin to active signal.
 * @return none
 **********************************************************************/
void UART_FullModemForcePinState(UART_ID_Type UartID,
                                                    UART_MODEM_PIN_Type Pin,
                                                    UART1_SignalState NewState)
{
    uint8_t tmp = 0;
    if (UartID != UART_1)
        return;
    switch (Pin)
    {
        case UART1_MODEM_PIN_DTR:
            tmp = UART1_MCR_DTR_CTRL;
            break;

        case UART1_MODEM_PIN_RTS:
            tmp = UART1_MCR_RTS_CTRL;
            break;

        default:
            break;
    }

    if (NewState == ACTIVE)
    {
        LPC_UART1->MCR |= tmp;
    }
    else
    {
        LPC_UART1->MCR &= (~tmp) & UART1_MCR_BITMASK;
    }
}


/*********************************************************************//**
 * @brief       Configure Full Modem mode for UART peripheral
 * @param[in]   UARTx   LPC_UART1 (only)
 * @param[in]   Mode Full Modem mode, should be:
 *              - UART1_MODEM_MODE_LOOPBACK: Loop back mode.
 *              - UART1_MODEM_MODE_AUTO_RTS: Auto-RTS mode.
 *              - UART1_MODEM_MODE_AUTO_CTS: Auto-CTS mode.
 * @param[in]   NewState New State of this mode, should be:
 *              - ENABLE: Enable this mode.
                - DISABLE: Disable this mode.
 * @return none
 **********************************************************************/
void UART_FullModemConfigMode(UART_ID_Type UartID, UART_MODEM_MODE_Type Mode,
                                            FunctionalState NewState)
{
    uint8_t tmp;

    if(UartID != UART_1)
        return;
    
    switch(Mode)
    {
        case UART1_MODEM_MODE_LOOPBACK:
            tmp = UART1_MCR_LOOPB_EN;
            break;

        case UART1_MODEM_MODE_AUTO_RTS:
            tmp = UART1_MCR_AUTO_RTS_EN;
            break;

        case UART1_MODEM_MODE_AUTO_CTS:
            tmp = UART1_MCR_AUTO_CTS_EN;
            break;

        default:
            break;
    }

    if (NewState == ENABLE)
    {
        LPC_UART1->MCR |= tmp;
    }
    else
    {
        LPC_UART1->MCR &= (~tmp) & UART1_MCR_BITMASK;
    }
}


/*********************************************************************//**
 * @brief       Get current status of modem status register
 * @param[in]   UARTx   LPC_UART1 (only)
 * @return      Current value of modem status register
 * Note:    The return value of this function must be ANDed with each member
 *          UART_MODEM_STAT_type enumeration to determine current flag status
 *          corresponding to each modem flag status. Because some flags in
 *          modem status register will be cleared after reading, the next reading
 *          modem register could not be correct. So this function used to
 *          read modem status register in one time only, then the return value
 *          used to check all flags.
 **********************************************************************/
uint8_t UART_FullModemGetStatus(UART_ID_Type UartID)
{
    if(UartID != UART_1)
        return  0;
    
    return ((LPC_UART1->MSR) & UART1_MSR_BITMASK);
}


/* UART RS485 functions --------------------------------------------------------------*/
/*********************************************************************//**
 * @brief       Configure UART peripheral in RS485 mode according to the specified
*               parameters in the RS485ConfigStruct.
 * @param[in]   UARTx   LPC_UART0 ~LPC_UART4
 * @param[in]   RS485ConfigStruct Pointer to a UART1_RS485_CTRLCFG_Type structure
*                    that contains the configuration information for specified UART
*                    in RS485 mode.
 * @return      None
 **********************************************************************/
void UART_RS485Config(UART_ID_Type UartID, UART1_RS485_CTRLCFG_Type *RS485ConfigStruct)
{
    uint32_t tmp;
    __IO uint32_t *RS485DLY, *ADRMATCH, *RS485CTRL, *LCR;

    tmp = 0;
    if (UartID == UART_1)
    {
        RS485DLY = (__IO uint32_t *)&LPC_UART1->RS485DLY;
        ADRMATCH = (__IO uint32_t *)&LPC_UART1->ADRMATCH;
        LCR = (__IO uint32_t *)&LPC_UART1->LCR;
        RS485CTRL =  (__IO uint32_t *)&LPC_UART1->RS485CTRL;
    }
    else if (UartID == UART_4)
    {
        RS485DLY = (__IO uint32_t *)&LPC_UART4->RS485DLY;
        ADRMATCH = (__IO uint32_t *)&LPC_UART4->ADRMATCH;
        LCR = (__IO uint32_t *)&LPC_UART4->LCR;
        RS485CTRL =  (__IO uint32_t *)&LPC_UART4->RS485CTRL;
    }
    else
    {
        LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
        RS485DLY = (__IO uint32_t *)&UARTx->RS485DLY;
        ADRMATCH = (__IO uint32_t *)&UARTx->ADRMATCH;
        LCR = (__IO uint32_t *)&UARTx->LCR;
         RS485CTRL =  (__IO uint32_t *)&UARTx->RS485CTRL;
    }
    // If Auto Direction Control is enabled -  This function is used in Master mode
    if (RS485ConfigStruct->AutoDirCtrl_State == ENABLE)
    {
        tmp |= UART_RS485CTRL_DCTRL_EN;

        // Set polar
        if (RS485ConfigStruct->DirCtrlPol_Level == SET)
        {
            tmp |= UART_RS485CTRL_OINV_1;
        }

        // Set pin according to. This condition is only with UART1. The others are used
        // OE pin as default for control the direction of RS485 buffer IC
        if ((RS485ConfigStruct->DirCtrlPin == UART_RS485_DIRCTRL_DTR) &&
             (UartID == UART_1))
        {
            tmp |= UART_RS485CTRL_SEL_DTR;
        }

        // Fill delay time
        *RS485DLY = RS485ConfigStruct->DelayValue & UART_RS485DLY_BITMASK;
    }
     
    // MultiDrop mode is enable
    if (RS485ConfigStruct->NormalMultiDropMode_State == ENABLE)
    {
        tmp |= UART_RS485CTRL_NMM_EN;
    }

    // Auto Address Detect function
    if (RS485ConfigStruct->AutoAddrDetect_State == ENABLE)
    {
        tmp |= UART_RS485CTRL_AADEN;

        // Fill Match Address
        *ADRMATCH = RS485ConfigStruct->MatchAddrValue & UART_RS485ADRMATCH_BITMASK;
    }

    // Receiver is disable
    if (RS485ConfigStruct->Rx_State == DISABLE)
    {
        tmp |= UART_RS485CTRL_RX_DIS;
    }
     
    // write back to RS485 control register
    *RS485CTRL = tmp & UART_RS485CTRL_BITMASK;

    // Enable Parity function and leave parity in stick '0' parity as default
    *LCR |= (UART_LCR_PARITY_F_0 | UART_LCR_PARITY_EN);
}

/*********************************************************************//**
 * @brief       Enable/Disable receiver in RS485 module in UART1
 * @param[in]   UARTx   LPC_UART1 (only)
 * @param[in]   NewState    New State of command, should be:
 *                          - ENABLE: Enable this function.
 *                          - DISABLE: Disable this function.
 * @return      None
 **********************************************************************/
void UART_RS485ReceiverCmd(UART_ID_Type UartID, FunctionalState NewState)
{
    __IO uint32_t *RS485CTRL;
    if (UartID == UART_1)
    {
        RS485CTRL = (__IO uint32_t *)&LPC_UART1->RS485DLY;
    }
    else if (UartID == UART_4)
    {
        RS485CTRL = (__IO uint32_t *)&LPC_UART4->RS485DLY;
    }
    else
    {
        LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
        RS485CTRL = (__IO uint32_t *)&UARTx->RS485DLY;
    }
    if (NewState == ENABLE)
    {
        *RS485CTRL &= ~UART_RS485CTRL_RX_DIS;
    }
    else
    {
        *RS485CTRL |= UART_RS485CTRL_RX_DIS;
    }
}

/*********************************************************************//**
 * @brief       Send data on RS485 bus with specified parity stick value (9-bit mode).
 * @param[in]   UARTx   LPC_UART1 (only)
 * @param[in]   pDatFrm     Pointer to data frame.
 * @param[in]   size        Size of data.
 * @param[in]   ParityStick Parity Stick value, should be 0 or 1.
 * @return      None
 **********************************************************************/
uint32_t UART_RS485Send(UART_ID_Type UartID, uint8_t *pDatFrm,
                                            uint32_t size, uint8_t ParityStick)
{
    uint8_t tmp, save;
    uint32_t cnt;
    __IO uint32_t *LCR, *LSR;
    if (UartID == UART_1)
    {
        LCR = (__IO uint32_t *)&LPC_UART1->LCR;
        LSR = (__IO uint32_t *)&LPC_UART1->LSR;
    }
    else if (UartID == UART_4)
    {
        LCR = (__IO uint32_t *)&LPC_UART4->LCR;
        LSR = (__IO uint32_t *)&LPC_UART4->LSR;
    }
    else
    {
        LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
        LCR = (__IO uint32_t *)&UARTx->LCR;
        LSR = (__IO uint32_t *)&UARTx->LSR;
    }

    if (ParityStick)
    {
        save = tmp = *LCR & UART_LCR_BITMASK;

        tmp &= ~(UART_LCR_PARITY_EVEN);

        *LCR = tmp;

        cnt = UART_Send(UartID, pDatFrm, size, BLOCKING);

        while (!(*LSR & UART_LSR_TEMT));

        *LCR = save;
    }
    else
    {
        cnt = UART_Send(UartID, pDatFrm, size, BLOCKING);

        while (!(*LSR & UART_LSR_TEMT));
    }

    return cnt;
}

/*********************************************************************//**
 * @brief       Send Slave address frames on RS485 bus.
 * @param[in]   UARTx   LPC_UART1 (only)
 * @param[in]   SlvAddr Slave Address.
 * @return      None
 **********************************************************************/
void UART_RS485SendSlvAddr(UART_ID_Type UartID, uint8_t SlvAddr)
{
    UART_RS485Send(UartID, &SlvAddr, 1, 1);
}

/*********************************************************************//**
 * @brief       Send Data frames on RS485 bus.
 * @param[in]   UARTx   LPC_UART1 (only)
 * @param[in]   pData Pointer to data to be sent.
 * @param[in]   size Size of data frame to be sent.
 * @return      None
 **********************************************************************/
uint32_t UART_RS485SendData(UART_ID_Type UartID, uint8_t *pData, uint32_t size)
{
    return (UART_RS485Send(UartID, pData, size, 0));
}

/**
 * @}
 */
#endif /*_UART*/
/**
 * @}
 */
/* --------------------------------- End Of File ------------------------------ */