nu_usci_uart.c 22.4 KB
Newer Older
W
Wayne Lin 已提交
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
/**************************************************************************//**
 * @file     usci_uart.c
 * @version  V1.00
 * @brief    M031 series USCI UART (UUART) driver source file
 *
 * SPDX-License-Identifier: Apache-2.0
 * copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/

#include <stdio.h>
#include "M031Series.h"

/** @addtogroup Standard_Driver Standard Driver
  @{
*/

/** @addtogroup USCI_UART_Driver USCI_UART Driver
  @{
*/

/** @addtogroup USCI_UART_EXPORTED_FUNCTIONS USCI_UART Exported Functions
  @{
*/

/**
 *    @brief        Clear USCI_UART specified interrupt flag
 *
 *    @param[in]    uuart   The pointer of the specified USCI_UART module.
 *    @param[in]    u32Mask The combination of all related interrupt sources.
 *                          Each bit corresponds to a interrupt source.
 *                          This parameter decides which interrupt flags will be cleared. It could be the combination of:
 *                          - \ref UUART_ABR_INT_MASK
 *                          - \ref UUART_RLS_INT_MASK
 *                          - \ref UUART_BUF_RXOV_INT_MASK
 *                          - \ref UUART_TXST_INT_MASK
 *                          - \ref UUART_TXEND_INT_MASK
 *                          - \ref UUART_RXST_INT_MASK
 *                          - \ref UUART_RXEND_INT_MASK
 *
 *    @return       None
 *
 *    @details      The function is used to clear USCI_UART related interrupt flags specified by u32Mask parameter.
 */

void UUART_ClearIntFlag(UUART_T* uuart, uint32_t u32Mask)
{

    if(u32Mask & UUART_ABR_INT_MASK)   /* Clear Auto-baud Rate Interrupt */
    {
        uuart->PROTSTS = UUART_PROTSTS_ABRDETIF_Msk;
    }

    if(u32Mask & UUART_RLS_INT_MASK)   /* Clear Receive Line Status Interrupt */
    {
        uuart->PROTSTS = (UUART_PROTSTS_BREAK_Msk | UUART_PROTSTS_FRMERR_Msk | UUART_PROTSTS_PARITYERR_Msk);
    }

    if(u32Mask & UUART_BUF_RXOV_INT_MASK)   /* Clear Receive Buffer Over-run Error Interrupt */
    {
        uuart->BUFSTS = UUART_BUFSTS_RXOVIF_Msk;
    }

    if(u32Mask & UUART_TXST_INT_MASK)   /* Clear Transmit Start Interrupt */
    {
        uuart->PROTSTS = UUART_PROTSTS_TXSTIF_Msk;
    }

    if(u32Mask & UUART_TXEND_INT_MASK)   /* Clear Transmit End Interrupt */
    {
        uuart->PROTSTS = UUART_PROTSTS_TXENDIF_Msk;
    }

    if(u32Mask & UUART_RXST_INT_MASK)   /* Clear Receive Start Interrupt */
    {
        uuart->PROTSTS = UUART_PROTSTS_RXSTIF_Msk;
    }

    if(u32Mask & UUART_RXEND_INT_MASK)   /* Clear Receive End Interrupt */
    {
        uuart->PROTSTS = UUART_PROTSTS_RXENDIF_Msk;
    }

}


/**
 *    @brief        Get USCI_UART specified interrupt flag
 *
 *    @param[in]    uuart   The pointer of the specified USCI_UART module.
 *    @param[in]    u32Mask The combination of all related interrupt sources.
 *                          Each bit corresponds to a interrupt source.
 *                          This parameter decides which interrupt flags will be read. It is combination of:
 *                          - \ref UUART_ABR_INT_MASK
 *                          - \ref UUART_RLS_INT_MASK
 *                          - \ref UUART_BUF_RXOV_INT_MASK
 *                          - \ref UUART_TXST_INT_MASK
 *                          - \ref UUART_TXEND_INT_MASK
 *                          - \ref UUART_RXST_INT_MASK
 *                          - \ref UUART_RXEND_INT_MASK
 *
 *    @return       Interrupt flags of selected sources.
 *
 *    @details      The function is used to get USCI_UART related interrupt flags specified by u32Mask parameter.
 */

uint32_t UUART_GetIntFlag(UUART_T* uuart, uint32_t u32Mask)
{
    uint32_t u32IntFlag = 0ul;
    uint32_t u32Tmp1, u32Tmp2;

    /* Check Auto-baud Rate Interrupt Flag */
    u32Tmp1 = (u32Mask & UUART_ABR_INT_MASK);
    u32Tmp2 = (uuart->PROTSTS & UUART_PROTSTS_ABRDETIF_Msk);
    if(u32Tmp1 && u32Tmp2)
    {
        u32IntFlag |= UUART_ABR_INT_MASK;
    }

    /* Check Receive Line Status Interrupt Flag */
    u32Tmp1 = (u32Mask & UUART_RLS_INT_MASK);
    u32Tmp2 = (uuart->PROTSTS & (UUART_PROTSTS_BREAK_Msk | UUART_PROTSTS_FRMERR_Msk | UUART_PROTSTS_PARITYERR_Msk));
    if(u32Tmp1 && u32Tmp2)
    {
        u32IntFlag |= UUART_RLS_INT_MASK;
    }

    /* Check Receive Buffer Over-run Error Interrupt Flag */
    u32Tmp1 = (u32Mask & UUART_BUF_RXOV_INT_MASK);
    u32Tmp2 = (uuart->BUFSTS & UUART_BUFSTS_RXOVIF_Msk);
    if(u32Tmp1 && u32Tmp2)
    {
        u32IntFlag |= UUART_BUF_RXOV_INT_MASK;
    }

    /* Check Transmit Start Interrupt Flag */
    u32Tmp1 = (u32Mask & UUART_TXST_INT_MASK);
    u32Tmp2 = (uuart->PROTSTS & UUART_PROTSTS_TXSTIF_Msk);
    if(u32Tmp1 && u32Tmp2)
    {
        u32IntFlag |= UUART_TXST_INT_MASK;
    }

    /* Check Transmit End Interrupt Flag */
    u32Tmp1 = (u32Mask & UUART_TXEND_INT_MASK);
    u32Tmp2 = (uuart->PROTSTS & UUART_PROTSTS_TXENDIF_Msk);
    if(u32Tmp1 && u32Tmp2)
    {
        u32IntFlag |= UUART_TXEND_INT_MASK;
    }

    /* Check Receive Start Interrupt Flag */
    u32Tmp1 = (u32Mask & UUART_RXST_INT_MASK);
    u32Tmp2 = (uuart->PROTSTS & UUART_PROTSTS_RXSTIF_Msk);
    if(u32Tmp1 && u32Tmp2)
    {
        u32IntFlag |= UUART_RXST_INT_MASK;
    }

    /* Check Receive End Interrupt Flag */
    u32Tmp1 = (u32Mask & UUART_RXEND_INT_MASK);
    u32Tmp2 = (uuart->PROTSTS & UUART_PROTSTS_RXENDIF_Msk);
    if(u32Tmp1 && u32Tmp2)
    {
        u32IntFlag |= UUART_RXEND_INT_MASK;
    }

    return u32IntFlag;
}


/**
 *  @brief      Disable USCI_UART function mode
 *
 *  @param[in]  uuart The pointer of the specified USCI_UART module.
 *
 *  @return     None
 *
 *  @details    The function is used to disable USCI_UART function mode.
 */
void UUART_Close(UUART_T* uuart)
{
    uuart->CTL = 0UL;
}


/**
 *    @brief        Disable interrupt function.
 *
 *    @param[in]    uuart   The pointer of the specified USCI_UART module.
 *    @param[in]    u32Mask The combination of all related interrupt enable bits.
 *                          Each bit corresponds to a interrupt enable bit.
 *                          This parameter decides which interrupts will be disabled. It is combination of:
 *                          - \ref UUART_ABR_INT_MASK
 *                          - \ref UUART_RLS_INT_MASK
 *                          - \ref UUART_BUF_RXOV_INT_MASK
 *                          - \ref UUART_TXST_INT_MASK
 *                          - \ref UUART_TXEND_INT_MASK
 *                          - \ref UUART_RXST_INT_MASK
 *                          - \ref UUART_RXEND_INT_MASK
 *
 *    @return       None
 *
 *    @details      The function is used to disabled USCI_UART related interrupts specified by u32Mask parameter.
 */
void UUART_DisableInt(UUART_T* uuart, uint32_t u32Mask)
{

    /* Disable Auto-baud rate interrupt flag */
    if((u32Mask & UUART_ABR_INT_MASK) == UUART_ABR_INT_MASK)
    {
        uuart->PROTIEN &= ~UUART_PROTIEN_ABRIEN_Msk;
    }

    /* Disable receive line status interrupt flag */
    if((u32Mask & UUART_RLS_INT_MASK) == UUART_RLS_INT_MASK)
    {
        uuart->PROTIEN &= ~UUART_PROTIEN_RLSIEN_Msk;
    }

    /* Disable RX overrun interrupt flag */
    if((u32Mask & UUART_BUF_RXOV_INT_MASK) == UUART_BUF_RXOV_INT_MASK)
    {
        uuart->BUFCTL &= ~UUART_BUFCTL_RXOVIEN_Msk;
    }

    /* Disable TX start interrupt flag */
    if((u32Mask & UUART_TXST_INT_MASK) == UUART_TXST_INT_MASK)
    {
        uuart->INTEN &= ~UUART_INTEN_TXSTIEN_Msk;
    }

    /* Disable TX end interrupt flag */
    if((u32Mask & UUART_TXEND_INT_MASK) == UUART_TXEND_INT_MASK)
    {
        uuart->INTEN &= ~UUART_INTEN_TXENDIEN_Msk;
    }

    /* Disable RX start interrupt flag */
    if((u32Mask & UUART_RXST_INT_MASK) == UUART_RXST_INT_MASK)
    {
        uuart->INTEN &= ~UUART_INTEN_RXSTIEN_Msk;
    }

    /* Disable RX end interrupt flag */
    if((u32Mask & UUART_RXEND_INT_MASK) == UUART_RXEND_INT_MASK)
    {
        uuart->INTEN &= ~UUART_INTEN_RXENDIEN_Msk;
    }
}


/**
 *    @brief        Enable interrupt function.
 *
 *    @param[in]    uuart       The pointer of the specified USCI_UART module.
 *    @param[in]    u32Mask     The combination of all related interrupt enable bits.
 *                              Each bit corresponds to a interrupt enable bit.
 *                              This parameter decides which interrupts will be enabled. It is combination of:
 *                              - \ref UUART_ABR_INT_MASK
 *                              - \ref UUART_RLS_INT_MASK
 *                              - \ref UUART_BUF_RXOV_INT_MASK
 *                              - \ref UUART_TXST_INT_MASK
 *                              - \ref UUART_TXEND_INT_MASK
 *                              - \ref UUART_RXST_INT_MASK
 *                              - \ref UUART_RXEND_INT_MASK
 *
 *    @return       None
 *
 *    @details      The function is used to enable USCI_UART related interrupts specified by u32Mask parameter.
 */
void UUART_EnableInt(UUART_T*  uuart, uint32_t u32Mask)
{
    /* Enable Auto-baud rate interrupt flag */
    if((u32Mask & UUART_ABR_INT_MASK) == UUART_ABR_INT_MASK)
    {
        uuart->PROTIEN |= UUART_PROTIEN_ABRIEN_Msk;
    }

    /* Enable receive line status interrupt flag */
    if((u32Mask & UUART_RLS_INT_MASK) == UUART_RLS_INT_MASK)
    {
        uuart->PROTIEN |= UUART_PROTIEN_RLSIEN_Msk;
    }

    /* Enable RX overrun interrupt flag */
    if((u32Mask & UUART_BUF_RXOV_INT_MASK) == UUART_BUF_RXOV_INT_MASK)
    {
        uuart->BUFCTL |= UUART_BUFCTL_RXOVIEN_Msk;
    }

    /* Enable TX start interrupt flag */
    if((u32Mask & UUART_TXST_INT_MASK) == UUART_TXST_INT_MASK)
    {
        uuart->INTEN |= UUART_INTEN_TXSTIEN_Msk;
    }

    /* Enable TX end interrupt flag */
    if((u32Mask & UUART_TXEND_INT_MASK) == UUART_TXEND_INT_MASK)
    {
        uuart->INTEN |= UUART_INTEN_TXENDIEN_Msk;
    }

    /* Enable RX start interrupt flag */
    if((u32Mask & UUART_RXST_INT_MASK) == UUART_RXST_INT_MASK)
    {
        uuart->INTEN |= UUART_INTEN_RXSTIEN_Msk;
    }

    /* Enable RX end interrupt flag */
    if((u32Mask & UUART_RXEND_INT_MASK) == UUART_RXEND_INT_MASK)
    {
        uuart->INTEN |= UUART_INTEN_RXENDIEN_Msk;
    }
}


/**
 *    @brief        Open and set USCI_UART function
 *
 *    @param[in]    uuart           The pointer of the specified USCI_UART module.
 *    @param[in]    u32baudrate     The baud rate of USCI_UART module.
 *
 *    @return       Real baud rate of USCI_UART module.
 *
 *    @details      This function use to enable USCI_UART function and set baud-rate.
 */
uint32_t UUART_Open(UUART_T* uuart, uint32_t u32baudrate)
{
    uint32_t u32PCLKFreq, u32PDSCnt, u32DSCnt, u32ClkDiv;
    uint32_t u32Tmp, u32Tmp2, u32Min, u32MinClkDiv, u32MinDSCnt;
    uint32_t u32Div;

    /* Get PCLK frequency */
    if(uuart == UUART0)
    {
        u32PCLKFreq = CLK_GetPCLK0Freq();
    }
    else
    {
        u32PCLKFreq = CLK_GetPCLK1Freq();
    }

    /* Calculate baud rate divider */
    u32Div = u32PCLKFreq / u32baudrate;
    u32Tmp = (u32PCLKFreq / u32Div) - u32baudrate;
    u32Tmp2 = u32baudrate - (u32PCLKFreq / (u32Div + 1ul));

    if(u32Tmp >= u32Tmp2) u32Div = u32Div + 1ul;

    if(u32Div >= 65536ul)
    {

        /* Set the smallest baud rate that USCI_UART can generate */
        u32PDSCnt = 0x4ul;
        u32MinDSCnt = 0x10ul;
        u32MinClkDiv = 0x400ul;

    }
    else
    {

        u32Tmp = 0x400ul * 0x10ul;
        for(u32PDSCnt = 1ul; u32PDSCnt <= 0x04ul; u32PDSCnt++)
        {
            if(u32Div <= (u32Tmp * u32PDSCnt)) break;
        }

        if(u32PDSCnt > 0x4ul) u32PDSCnt = 0x4ul;

        u32Div = u32Div / u32PDSCnt;

        /* Find best solution */
        u32Min = (uint32_t) - 1;
        u32MinDSCnt = 0ul;
        u32MinClkDiv = 0ul;
        u32Tmp = 0ul;

        for(u32DSCnt = 6ul; u32DSCnt <= 0x10ul; u32DSCnt++)   /* DSCNT could be 0x5~0xF */
        {

            u32ClkDiv = u32Div / u32DSCnt;

            if(u32ClkDiv > 0x400ul)
            {
                u32ClkDiv = 0x400ul;
                u32Tmp = u32Div - (u32ClkDiv * u32DSCnt);
                u32Tmp2 = u32Tmp + 1ul;
            }
            else
            {
                u32Tmp = u32Div - (u32ClkDiv * u32DSCnt);
                u32Tmp2 = ((u32ClkDiv + 1ul) * u32DSCnt) - u32Div;
            }

            if(u32Tmp >= u32Tmp2)
            {
                u32ClkDiv = u32ClkDiv + 1ul;
            }
            else u32Tmp2 = u32Tmp;

            if(u32Tmp2 < u32Min)
            {
                u32Min = u32Tmp2;
                u32MinDSCnt = u32DSCnt;
                u32MinClkDiv = u32ClkDiv;

                /* Break when get good results */
                if(u32Min == 0ul)
                {
                    break;
                }
            }
        }

    }

    /* Enable USCI_UART protocol */
    uuart->CTL &= ~UUART_CTL_FUNMODE_Msk;
    uuart->CTL = 2ul << UUART_CTL_FUNMODE_Pos;

    /* Set USCI_UART line configuration */
    uuart->LINECTL = UUART_WORD_LEN_8 | UUART_LINECTL_LSB_Msk;
    uuart->DATIN0 = (2ul << UUART_DATIN0_EDGEDET_Pos);  /* Set falling edge detection */

    /* Set USCI_UART baud rate */
    uuart->BRGEN = ((u32MinClkDiv - 1ul) << UUART_BRGEN_CLKDIV_Pos) |
                   ((u32MinDSCnt - 1ul) << UUART_BRGEN_DSCNT_Pos) |
                   ((u32PDSCnt - 1ul) << UUART_BRGEN_PDSCNT_Pos);

    uuart->PROTCTL |= UUART_PROTCTL_PROTEN_Msk;

    return (u32PCLKFreq / u32PDSCnt / u32MinDSCnt / u32MinClkDiv);
}


/**
 *    @brief        Read USCI_UART data
 *
 *    @param[in]    uuart           The pointer of the specified USCI_UART module.
 *    @param[in]    pu8RxBuf        The buffer to receive the data of receive buffer.
 *    @param[in]    u32ReadBytes    The read bytes number of data.
 *
 *    @return       Receive byte count
 *
 *    @details      The function is used to read Rx data from RX buffer and the data will be stored in pu8RxBuf.
 */
uint32_t UUART_Read(UUART_T* uuart, uint8_t pu8RxBuf[], uint32_t u32ReadBytes)
{
    uint32_t  u32Count, u32delayno;

    for(u32Count = 0ul; u32Count < u32ReadBytes; u32Count++)
    {
        u32delayno = 0ul;

        while(uuart->BUFSTS & UUART_BUFSTS_RXEMPTY_Msk)   /* Check RX empty => failed */
        {
            u32delayno++;
            if(u32delayno >= 0x40000000ul)
            {
                break;
            }
        }

        if(u32delayno >= 0x40000000ul)
        {
            break;
        }

        pu8RxBuf[u32Count] = (uint8_t)uuart->RXDAT;    /* Get Data from USCI RX  */
    }

    return u32Count;

}


/**
 *    @brief        Set USCI_UART line configuration
 *
 *    @param[in]    uuart           The pointer of the specified USCI_UART module.
 *    @param[in]    u32baudrate     The register value of baud rate of USCI_UART module.
 *                                  If u32baudrate = 0, USCI_UART baud rate will not change.
 *    @param[in]    u32data_width   The data length of USCI_UART module.
 *                                  - \ref UUART_WORD_LEN_6
 *                                  - \ref UUART_WORD_LEN_7
 *                                  - \ref UUART_WORD_LEN_8
 *                                  - \ref UUART_WORD_LEN_9
 *    @param[in]    u32parity       The parity setting (none/odd/even) of USCI_UART module.
 *                                  - \ref UUART_PARITY_NONE
 *                                  - \ref UUART_PARITY_ODD
 *                                  - \ref UUART_PARITY_EVEN
 *    @param[in]    u32stop_bits    The stop bit length (1/2 bit) of USCI_UART module.
 *                                  - \ref UUART_STOP_BIT_1
 *                                  - \ref UUART_STOP_BIT_2
 *
 *    @return       Real baud rate of USCI_UART module.
 *
 *    @details      This function use to config USCI_UART line setting.
 */
uint32_t UUART_SetLine_Config(UUART_T* uuart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
{
    uint32_t u32PCLKFreq, u32PDSCnt, u32DSCnt, u32ClkDiv;
    uint32_t u32Tmp, u32Tmp2, u32Min, u32MinClkDiv, u32MinDSCnt;
    uint32_t u32Div;

    /* Get PCLK frequency */
    if(uuart == UUART0)
    {
        u32PCLKFreq = CLK_GetPCLK0Freq();
    }
    else
    {
        u32PCLKFreq = CLK_GetPCLK1Freq();
    }

    if(u32baudrate != 0ul)
    {

        /* Calculate baud rate divider */
        u32Div = u32PCLKFreq / u32baudrate;
        u32Tmp = (u32PCLKFreq / u32Div) - u32baudrate;
        u32Tmp2 = u32baudrate - (u32PCLKFreq / (u32Div + 1ul));

        if(u32Tmp >= u32Tmp2) u32Div = u32Div + 1ul;

        if(u32Div >= 65536ul)
        {

            /* Set the smallest baud rate that USCI_UART can generate */
            u32PDSCnt = 0x4ul;
            u32MinDSCnt = 0x10ul;
            u32MinClkDiv = 0x400ul;

        }
        else
        {

            u32Tmp = 0x400ul * 0x10ul;
            for(u32PDSCnt = 1ul; u32PDSCnt <= 0x04ul; u32PDSCnt++)
            {
                if(u32Div <= (u32Tmp * u32PDSCnt)) break;
            }

            if(u32PDSCnt > 0x4ul) u32PDSCnt = 0x4ul;

            u32Div = u32Div / u32PDSCnt;

            /* Find best solution */
            u32Min = (uint32_t) - 1;
            u32MinDSCnt = 0ul;
            u32MinClkDiv = 0ul;

            for(u32DSCnt = 6ul; u32DSCnt <= 0x10ul; u32DSCnt++)   /* DSCNT could be 0x5~0xF */
            {
                u32ClkDiv = u32Div / u32DSCnt;

                if(u32ClkDiv > 0x400ul)
                {
                    u32ClkDiv = 0x400ul;
                    u32Tmp = u32Div - (u32ClkDiv * u32DSCnt);
                    u32Tmp2 = u32Tmp + 1ul;
                }
                else
                {
                    u32Tmp = u32Div - (u32ClkDiv * u32DSCnt);
                    u32Tmp2 = ((u32ClkDiv + 1ul) * u32DSCnt) - u32Div;
                }

                if(u32Tmp >= u32Tmp2)
                {
                    u32ClkDiv = u32ClkDiv + 1ul;
                }
                else u32Tmp2 = u32Tmp;

                if(u32Tmp2 < u32Min)
                {
                    u32Min = u32Tmp2;
                    u32MinDSCnt = u32DSCnt;
                    u32MinClkDiv = u32ClkDiv;

                    /* Break when get good results */
                    if(u32Min == 0ul)
                    {
                        break;
                    }
                }
            }

        }

        /* Set USCI_UART baud rate */
        uuart->BRGEN = ((u32MinClkDiv - 1ul) << UUART_BRGEN_CLKDIV_Pos) |
                       ((u32MinDSCnt - 1ul) << UUART_BRGEN_DSCNT_Pos) |
                       ((u32PDSCnt - 1ul) << UUART_BRGEN_PDSCNT_Pos);
    }
    else
    {
        u32PDSCnt = ((uuart->BRGEN & UUART_BRGEN_PDSCNT_Msk) >> UUART_BRGEN_PDSCNT_Pos) + 1ul;
        u32MinDSCnt = ((uuart->BRGEN & UUART_BRGEN_DSCNT_Msk) >> UUART_BRGEN_DSCNT_Pos) + 1ul;
        u32MinClkDiv = ((uuart->BRGEN & UUART_BRGEN_CLKDIV_Msk) >> UUART_BRGEN_CLKDIV_Pos) + 1ul;
    }

    /* Set USCI_UART line configuration */
    uuart->LINECTL = (uuart->LINECTL & ~UUART_LINECTL_DWIDTH_Msk) | u32data_width;
    uuart->PROTCTL = (uuart->PROTCTL & ~(UUART_PROTCTL_STICKEN_Msk | UUART_PROTCTL_EVENPARITY_Msk |
                                         UUART_PROTCTL_PARITYEN_Msk)) | u32parity;
    uuart->PROTCTL = (uuart->PROTCTL & ~UUART_PROTCTL_STOPB_Msk) | u32stop_bits;

    return (u32PCLKFreq / u32PDSCnt / u32MinDSCnt / u32MinClkDiv);
}


/**
 *    @brief        Write USCI_UART data
 *
 *    @param[in]    uuart           The pointer of the specified USCI_UART module.
 *    @param[in]    pu8TxBuf        The buffer to send the data to USCI transmission buffer.
 *    @param[out]   u32WriteBytes   The byte number of data.
 *
 *    @return       Transfer byte count
 *
 *    @details      The function is to write data into TX buffer to transmit data by USCI_UART.
 */
uint32_t UUART_Write(UUART_T* uuart, uint8_t pu8TxBuf[], uint32_t u32WriteBytes)
{
    uint32_t  u32Count, u32delayno;

    for(u32Count = 0ul; u32Count != u32WriteBytes; u32Count++)
    {
        u32delayno = 0ul;
        while((uuart->BUFSTS & UUART_BUFSTS_TXEMPTY_Msk) == 0ul)   /* Wait Tx empty */
        {
            u32delayno++;
            if(u32delayno >= 0x40000000ul)
            {
                break;
            }
        }

        if(u32delayno >= 0x40000000ul)
        {
            break;
        }

        uuart->TXDAT = (uint8_t)pu8TxBuf[u32Count];    /* Send USCI_UART Data to buffer */
    }

    return u32Count;
}


/**
 *    @brief        Enable USCI_UART Wake-up Function
 *
 *    @param[in]    uuart           The pointer of the specified USCI_UART module.
 *    @param[in]    u32WakeupMode   The wakeup mode of USCI_UART module.
*                                   - \ref UUART_PROTCTL_DATWKEN_Msk    : Data wake-up Mode
*                                   - \ref UUART_PROTCTL_CTSWKEN_Msk    : nCTS wake-up Mode
 *
 *    @return       None
 *
 *    @details      The function is used to enable Wake-up function of USCI_UART.
 */
void UUART_EnableWakeup(UUART_T* uuart, uint32_t u32WakeupMode)
{
    uuart->PROTCTL |= u32WakeupMode;
    uuart->WKCTL |= UUART_WKCTL_WKEN_Msk;
}


/**
 *    @brief        Disable USCI_UART Wake-up Function
 *
 *    @param[in]    uuart   The pointer of the specified USCI_UART module.
 *
 *    @return       None
 *
 *    @details      The function is used to disable Wake-up function of USCI_UART.
 */
void UUART_DisableWakeup(UUART_T* uuart)
{
    uuart->PROTCTL &= ~(UUART_PROTCTL_DATWKEN_Msk | UUART_PROTCTL_CTSWKEN_Msk);
    uuart->WKCTL &= ~UUART_WKCTL_WKEN_Msk;
}

/**
 *    @brief        Enable USCI_UART auto flow control
 *
 *    @param[in]    uuart   The pointer of the specified USCI_UART module.
 *
 *    @return       None
 *
 *    @details      The function is used to enable USCI_UART auto flow control.
 */
void UUART_EnableFlowCtrl(UUART_T* uuart)
{
    /* Set RTS signal is low level active */
    uuart->LINECTL &= ~UUART_LINECTL_CTLOINV_Msk;

    /* Set CTS signal is low level active */
    uuart->CTLIN0 &= ~UUART_CTLIN0_ININV_Msk;

    /* Enable CTS and RTS auto flow control function */
    uuart->PROTCTL |= UUART_PROTCTL_RTSAUTOEN_Msk | UUART_PROTCTL_CTSAUTOEN_Msk;
}

/**
 *    @brief        Disable USCI_UART auto flow control
 *
 *    @param[in]    uuart    The pointer of the specified USCI_UART module.
 *
 *    @return       None
 *
 *    @details      The function is used to disable USCI_UART auto flow control.
 */
void UUART_DisableFlowCtrl(UUART_T* uuart)
{
    /* Disable CTS and RTS auto flow control function */
    uuart->PROTCTL &= ~(UUART_PROTCTL_RTSAUTOEN_Msk | UUART_PROTCTL_CTSAUTOEN_Msk);
}

/*@}*/ /* end of group USCI_UART_EXPORTED_FUNCTIONS */

/*@}*/ /* end of group USCI_UART_Driver */

/*@}*/ /* end of group Standard_Driver */

/*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/