HAL_UART.c 26.8 KB
Newer Older
A
aisino2200 已提交
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
/*
  ******************************************************************************
  * @file    HAL_Uart.c
  * @version V1.0.0
  * @date    2020
  * @brief   UART HAL module driver.
  *          This file provides firmware functions to manage the following
  *          functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
  *           @ Initialization and de-initialization functions
  *           @ IO operation functions
  *           @ Peripheral Control functions
  ******************************************************************************
*/
#include "ACM32Fxx_HAL.h"


/* If Use 'UART_MODE_TX_RX_DEBUG', Point to Debug Uart */
UART_TypeDef    *Uart_Debug = NULL;

/* Private function prototypes -----------------------------------------------*/
static void UART_Config_BaudRate(UART_HandleTypeDef *huart);
static HAL_StatusTypeDef HAL_UART_Wait_Tx_Done(UART_HandleTypeDef *huart);
__weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
__weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
__weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart);

/*********************************************************************************
* Function    : HAL_UART_IRQHandler
* Description : Handle UART interrupt request.
* Input       : huart: UART handle.
* Output      :
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
{
    uint32_t  read_bytes_number;

#if (USE_FULL_ASSERT == 1)
    if (!IS_UART_ALL_INSTANCE(huart->Instance))    return;
#endif

    uint32_t isrflags =READ_REG(huart->Instance->RIS);
    uint32_t ieits    =READ_REG(huart->Instance->IE);
	uint32_t errorflags =0x00U;
    errorflags =(isrflags & (uint32_t)(UART_ICR_PEI|UART_ICR_OEI|UART_ICR_FEI|UART_ICR_BEI));

    /* Enable TXI */
    if (huart->Instance->IE & UART_IE_TXI)
    {
        if (huart->Instance->RIS & UART_RIS_TXI)
        {
            /* Clear TXI Status */
            SET_BIT(huart->Instance->ICR , UART_ICR_TXI);

            for(;;)
            {
                if(huart->lu32_TxCount == huart->lu32_TxSize)
                {
                    huart->lu8_TxBusy = false;

                    /* Disable TX interrupt */
                    CLEAR_BIT(huart->Instance->IE, UART_IE_TXI);

                    HAL_UART_TxCpltCallback(huart);
                    break;
                }

                if (READ_BIT(huart->Instance->FR, UART_FR_TXFF))
                {
                    break;
                }

                huart->Instance->DR = huart->lu8_TxData[huart->lu32_TxCount++];
            }
        }
    }

    /* RXI */
    if ((huart->Instance->IE & UART_IE_RXI || huart->Instance->IE& UART_IE_RTI) && errorflags == 0)
	{
		if (huart->Instance->RIS & UART_RIS_RXI)
		{
            read_bytes_number = 0;
			/* Clear RXI Status */
			SET_BIT(huart->Instance->ICR, UART_ICR_RXI);

			/* Receive end */
			while(huart->lu32_RxCount <huart->lu32_RxSize )
			{
				if(!READ_BIT(huart->Instance->FR, UART_FR_RXFE))
				{
					 /* Store Data in buffer */
					 huart->lu8_RxData[huart->lu32_RxCount++] = huart->Instance->DR;
                     read_bytes_number++;
				}
				else
			    {
					break;
			    }
                if (read_bytes_number == huart->lu32_fifo_level_minus1)
                {
                    break;
                }
			}
			if(huart->lu32_RxCount ==huart->lu32_RxSize )
			{
				huart->lu8_RxBusy = false;

				/* Disable RX and RTI interrupt */
				CLEAR_BIT(huart->Instance->IE, (UART_IE_RXI|UART_IE_RTI));

				/* clear error interrupt */
				CLEAR_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI);

				HAL_UART_RxCpltCallback(huart);
		   }
		}
		else if(huart->Instance->RIS & UART_RIS_RTI)
		{
			/*clear RTI Status */
			SET_BIT(huart->Instance->ICR ,UART_ICR_RTI);

			while(!READ_BIT(huart->Instance->FR, UART_FR_RXFE))
			{
				huart->lu8_RxData[huart->lu32_RxCount++] = huart->Instance->DR;
			}

			huart->lu8_RxBusy = false;

			/* Disable RX and RTI interrupt */
			CLEAR_BIT(huart->Instance->IE, (UART_IE_RXI|UART_IE_RTI));

			/* clear error interrupt */
			CLEAR_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI);

			HAL_UART_RxCpltCallback(huart);
		}
	}
	/* if some errors occurred */
	if(errorflags != 0 &&(ieits & (UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI)))
	{
		/* UART parity error interrupt occurred */
	    if (((isrflags & UART_RIS_PEI) != 0) && ((ieits & UART_IE_PEI) != 0))
	    {
	    	/* Clear parity error status */
			SET_BIT(huart->Instance->ICR, UART_ICR_PEI);
	      	huart->ErrorCode |= HAL_UART_ERROR_PE;
	    }

	    /* UART break error interrupt occurred */
	    if (((isrflags & UART_RIS_BEI) != 0) && ((ieits & UART_IE_BEI) != 0))
	    {
	    	SET_BIT(huart->Instance->ICR, UART_RIS_BEI);
	      	huart->ErrorCode |= HAL_UART_ERROR_NE;
	    }

	    /* UART frame error interrupt occurred */
	    if (((isrflags & UART_RIS_FEI) != 0) && ((ieits & UART_IE_FEI) != 0))
	    {
	    	SET_BIT(huart->Instance->ICR, UART_RIS_FEI);
	      	huart->ErrorCode |= HAL_UART_ERROR_FE;
	    }

	    /* UART Over-Run interrupt occurred */
	    if (((isrflags & UART_RIS_OEI) != 0) && ((ieits & UART_IE_OEI) != 0))
	    {
	    	SET_BIT(huart->Instance->ICR, UART_RIS_OEI);
	      	huart->ErrorCode |= HAL_UART_ERROR_ORE;
	    }

		/* clear error interrupt */
		CLEAR_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI);

		HAL_UART_ErrorCallback(huart);
	}
}

/*********************************************************************************
* Function    : HAL_UART_Wait_Tx_Done
* Description : wait Tx FIFO empty
* Input       : huart: UART handle.
* Output      :
**********************************************************************************/
static HAL_StatusTypeDef HAL_UART_Wait_Tx_Done(UART_HandleTypeDef *huart)
{
#if (USE_FULL_ASSERT == 1)
    if (!IS_UART_ALL_INSTANCE(huart->Instance))         return HAL_ERROR;
#endif
	/* wait FIFO empty */
	while(READ_BIT(huart->Instance->FR, UART_FR_BUSY));

	return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_UART_MspInit
* Description : Initialize the UART MSP.
* Input       : huart: UART handle.
* Output      :
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
__weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
    /*
      NOTE: This function should be modified, when the callback is needed,
      the HAL_UART_MspInit can be implemented in the user file.
    */

    /* For Example */
    GPIO_InitTypeDef    GPIO_Uart1;

    if (huart->Instance == UART1)
    {
        /* Enable Clock */
        System_Module_Enable(EN_UART1);
        //System_Module_Enable(EN_GPIOAB);

        /* Initialization GPIO */
        /* A9:Tx  A10:Rx */
        GPIO_Uart1.Pin       = GPIO_PIN_9 | GPIO_PIN_10;
        GPIO_Uart1.Mode      = GPIO_MODE_AF_PP;
        GPIO_Uart1.Pull      = GPIO_PULLUP;
        GPIO_Uart1.Alternate = GPIO_FUNCTION_2;

        HAL_GPIO_Init(GPIOA, &GPIO_Uart1);

        if (huart->Init.HwFlowCtl & UART_HWCONTROL_CTS)
        {
            /* A11:CTS */
            GPIO_Uart1.Pin = GPIO_PIN_11;

            HAL_GPIO_Init(GPIOA, &GPIO_Uart1);
        }

        if (huart->Init.HwFlowCtl & UART_HWCONTROL_RTS)
        {
            /* A12:RTS */
            GPIO_Uart1.Pin = GPIO_PIN_12;

            HAL_GPIO_Init(GPIOA, &GPIO_Uart1);
        }

        /* NVIC Config */
        NVIC_ClearPendingIRQ(UART1_IRQn);
        NVIC_SetPriority(UART1_IRQn, 5);
        NVIC_EnableIRQ(UART1_IRQn);
    }
}

/*********************************************************************************
* Function    : HAL_UART_Init
* Description : Initialize the UART mode according to the specified parameters
*               in the UART_InitTypeDef
* Input       : huart: UART handle.
* Output      :
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
{

#if (USE_FULL_ASSERT == 1)
    if (!IS_UART_ALL_INSTANCE(huart->Instance))                   return HAL_ERROR;
    if (!IS_UART_WORDLENGTH(huart->Init.WordLength))              return HAL_ERROR;
    if (!IS_UART_STOPBITS(huart->Init.StopBits))                  return HAL_ERROR;
    if (!IS_UART_PARITY(huart->Init.Parity))                      return HAL_ERROR;
    if (!IS_UART_MODE(huart->Init.Mode))                          return HAL_ERROR;
    if (!IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl))    return HAL_ERROR;
#endif

    /* Init the low level hardware : GPIO, CLOCK, NVIC */
    HAL_UART_MspInit(huart);

    /* Config BaudRate */
    UART_Config_BaudRate(huart);

    /* Set the UART Communication parameters */
    //huart->Instance->LCRH = huart->Init.WordLength | UART_LCRH_FEN | huart->Init.StopBits | huart->Init.Parity;
    huart->Instance->LCRH = huart->Init.WordLength | huart->Init.StopBits | huart->Init.Parity;
    huart->Instance->CR = huart->Init.HwFlowCtl | huart->Init.Mode | UART_CR_UARTEN;

    if (huart->Init.Mode == UART_MODE_TX_RX_DEBUG)
    {
        Uart_Debug = huart->Instance;
    }
    else if (huart->Init.Mode == UART_MODE_HALF_DUPLEX)
    {
        huart->Instance->CR2 = UART_CR2_TXOE_SEL;
    }
	return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_UART_MspDeInit
* Description : DeInitialize the UART MSP.
* Input       : huart: UART handle.
* Output      :
**********************************************************************************/
__weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
    /*
      NOTE: This function should be modified, when the callback is needed,
      the HAL_UART_MspDeInit can be implemented in the user file.
    */
    if (huart->Instance == UART1)
    {
        /* Disable Clock */
        System_Module_Disable(EN_UART1);

        /* DeInitialization GPIO */
        /* A9:Tx  A10:Rx */
        HAL_GPIO_DeInit(GPIOA,GPIO_PIN_9 | GPIO_PIN_10);

		if (huart->Init.HwFlowCtl & UART_HWCONTROL_CTS)
        {
            /* A11:CTS */
            HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11);
        }

        if (huart->Init.HwFlowCtl & UART_HWCONTROL_RTS)
        {
            /* A12:RTS */
            HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12);
        }

        /* NVIC DeInit */
        NVIC_DisableIRQ(UART1_IRQn);

    }
	else if(huart->Instance == UART2)
	{

	}
}

/*********************************************************************************
* Function    : HAL_UART_Init
* Description : Initialize the UART mode according to the specified parameters
*               in the UART_InitTypeDef
* Input       : huart: UART handle.
* Output      :
**********************************************************************************/
HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
{

#if (USE_FULL_ASSERT == 1)
    if (!IS_UART_ALL_INSTANCE(huart->Instance))         return HAL_ERROR;
#endif

    /* DeInit the low level hardware : GPIO, CLOCK, NVIC */
    HAL_UART_MspDeInit(huart);

	return HAL_OK;

}


/*********************************************************************************
* Function    : HAL_UART_Transmit
* Description : Send an amount of data in blocking mode.
* Input       : huart:        UART handle.
* Input       : fu8_Data:     Pointer to data buffer.
* Input       : fu32_Size:    Amount of data elements to be sent.
* Input       : fu32_Timeout: Timeout duration.
* Output      : HAL status
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size, uint32_t fu32_Timeout)
{
    uint32_t lu32_Timeout;

#if (USE_FULL_ASSERT == 1)
    if (!IS_UART_ALL_INSTANCE(huart->Instance))    return HAL_ERROR;
#endif

    huart->lu32_TxCount = 0;

    while (fu32_Size--)
    {
        huart->Instance->DR = *fu8_Data++;

        huart->lu32_TxCount++;

        /* have no timeout */
        if (fu32_Timeout == 0)
        {
            while (huart->Instance->FR & UART_FR_TXFF);
        }
        else
        {
            lu32_Timeout = fu32_Timeout *256;

            while (huart->Instance->FR & UART_FR_TXFF)
            {
                if (lu32_Timeout-- == 0)
                {
                    return HAL_TIMEOUT;
                }
            }
        }
    }

    while ((huart->Instance->FR & UART_FR_BUSY));
    return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_UART_Receive
* Description : Receive an amount of data in blocking mode.
* Input       : huart:        UART handle.
* Input       : fu8_Data:     Pointer to data buffer.
* Input       : fu32_Size:    Amount of data elements to be receive.
* Input       : fu32_Timeout: Timeout duration.
* Output      : HAL status
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size, uint32_t fu32_Timeout)
{
    uint32_t lu32_Timeout;

#if (USE_FULL_ASSERT == 1)
    if (!IS_UART_ALL_INSTANCE(huart->Instance))    return HAL_ERROR;
#endif

    huart->lu32_RxCount = 0;

    /* Half duplex Use Tx GPIO Receive Data */
    if (huart->Init.Mode == UART_MODE_HALF_DUPLEX)
    {
        huart->Instance->CR2 |= UART_CR2_RX_SEL;
    }

    while (fu32_Size--)
    {
        if (fu32_Timeout == 0)
        {
            while(huart->Instance->FR & UART_FR_RXFE);

            *fu8_Data++ = huart->Instance->DR;

            huart->lu32_RxCount++;
        }
        else
        {
            lu32_Timeout = fu32_Timeout * 256;

            while(huart->Instance->FR & UART_FR_RXFE)
            {
                if (lu32_Timeout-- == 0)
                {
                    /* Clear Half duplex */
                    huart->Instance->CR2 &= ~UART_CR2_RX_SEL;

                    return HAL_TIMEOUT;
                }
            }

            *fu8_Data++ = huart->Instance->DR;

            huart->lu32_RxCount++;
        }
    }

    /* Clear Half duplex */
    huart->Instance->CR2 &= ~UART_CR2_RX_SEL;

    return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_UART_Transmit_IT
* Description : Send an amount of data in interrupt mode.
* Input       : huart:        UART handle.
* Input       : fu8_Data:     Pointer to data buffer.
* Input       : fu32_Size:    Amount of data elements to be receive.
* Output      :
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size)
{
#if (USE_FULL_ASSERT == 1)
    if (!IS_UART_ALL_INSTANCE(huart->Instance))    return HAL_ERROR;
#endif

    if (huart->lu8_TxBusy == true)
    {
        return HAL_BUSY;
    }

    if (fu32_Size == 0 || fu8_Data == NULL)
    {
        return HAL_ERROR;
    }

    huart->lu32_TxSize  = fu32_Size;
    huart->lu32_TxCount = 0;
    huart->lu8_TxData   = fu8_Data;
    huart->lu8_TxBusy   = true;

    /* Clear TXI Status */
    huart->Instance->ICR = UART_ICR_TXI;
    /* FIFO Enable */
    SET_BIT(huart->Instance->LCRH, UART_LCRH_FEN);
	/*FIFO Select*/
	SET_BIT(huart->Instance->IFLS,UART_TX_FIFO_1_2);

    for(;;)
    {
        /*Data Size less than 16Byte */
	    if(fu32_Size == huart->lu32_TxCount)
	    {
	        huart->lu8_TxBusy = false;

			while ((huart->Instance->FR & UART_FR_BUSY)){}

	        HAL_UART_TxCpltCallback(huart);

	        return HAL_OK;
	    }
	    if(READ_BIT(huart->Instance->FR, UART_FR_TXFF))
	    {
	         break;
	    }
	    huart->Instance->DR = huart->lu8_TxData[huart->lu32_TxCount++];
    }

    /* Enable TX interrupt */
    huart->Instance->IE |= UART_IE_TXI;

    return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_UART_Receive_IT
* Description : Receive an amount of data in interrupt mode.
* Input       :
* Output      :
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size)
{
#if (USE_FULL_ASSERT == 1)
    if (!IS_UART_ALL_INSTANCE(huart->Instance))    return HAL_ERROR;
#endif

    if (huart->lu8_RxBusy == true)
    {
        return HAL_BUSY;
    }

    if (fu32_Size == 0 || fu8_Data == NULL)
    {
        return HAL_ERROR;
    }

    huart->lu32_RxSize  = fu32_Size;
    huart->lu32_RxCount = 0;
    huart->lu8_RxData   = fu8_Data;
    huart->lu8_RxBusy   = true;

    /* Clear RXI Status */
    huart->Instance->ICR = UART_ICR_RXI;
    /* FIFO Enable */
    SET_BIT(huart->Instance->LCRH, UART_LCRH_FEN);
	/*FIFO Select*/
	MODIFY_REG(huart->Instance->IFLS, UART_IFLS_RXIFLSEL, UART_RX_FIFO_1_2);
    huart->lu32_fifo_level_minus1 = 8-1;  // 16/2 - 1
	/* Enable the UART Errors interrupt */
	SET_BIT(huart->Instance->IE,UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI);
    /* Enable RX and RTI interrupt */
    SET_BIT(huart->Instance->IE,UART_IE_RXI|UART_IE_RTI);

    return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_UART_Transmit_DMA
* Description : Send an amount of data in DMA mode.
* Input       : huart:        UART handle.
* Input       : fu8_Data:     Pointer to data buffer.
* Input       : fu32_Size:    Amount of data elements to be Send.
* Output      :
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size)
{
#if (USE_FULL_ASSERT == 1)
    if (!IS_UART_ALL_INSTANCE(huart->Instance))    return HAL_ERROR;
#endif

    if (huart->lu8_TxBusy == true)
    {
        return HAL_BUSY;
    }

    if (fu32_Size == 0 || fu8_Data == NULL)
    {
        return HAL_ERROR;
    }

    huart->Instance->DMACR |= UART_DMACR_TXDMAE;

    if (HAL_DMA_Start_IT(huart->HDMA_Tx, (uint32_t)fu8_Data, (uint32_t)(&huart->Instance->DR), fu32_Size))
    {
        return HAL_ERROR;
    }

    return HAL_OK;
}

/*********************************************************************************
<<<<<<< .mine
* Function    : HAL_UART_Receive_DMA
* Description : Receive an amount of data in DMA mode.
* Input       : huart:        UART handle.
* Input       : fu8_Data:     Pointer to data buffer.
* Input       : fu32_Size:    Amount of data elements to be receive.
* Output      :
**********************************************************************************/
HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size)
{
#if (USE_FULL_ASSERT == 1)
    if (!IS_UART_ALL_INSTANCE(huart->Instance))    return HAL_ERROR;
#endif

    if (huart->lu8_RxBusy == true)
    {
        return HAL_BUSY;
    }

    if (fu32_Size == 0 || fu8_Data == NULL)
    {
        return HAL_ERROR;
    }

    huart->Instance->DMACR |= UART_DMACR_RXDMAE;

    if (HAL_DMA_Start_IT(huart->HDMA_Rx, (uint32_t)(&huart->Instance->DR), (uint32_t)fu8_Data, fu32_Size))
    {
        return HAL_ERROR;
    }

    return HAL_OK;
}



/*********************************************************************************
* Function    : HAL_UART_TxCpltCallback
* Description : Tx Transfer completed callbacks.
* Input       :
* Output       :
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
__weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
    /*
      NOTE: This function Should be modified, when the callback is needed,
      the HAL_UART_TxCpltCallback could be implemented in the user file.
    */
}

/*********************************************************************************
* Function    : HAL_UART_RxCpltCallback
* Description : Rx Transfer completed callbacks.
* Input       :
* Output       :
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
__weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    /*
      NOTE: This function Should be modified, when the callback is needed,
      the HAL_UART_RxCpltCallback could be implemented in the user file.
    */
}

/*********************************************************************************
* Function    : HAL_UART_ErrorCallBack
* Description : Recv Error callbacks.
* Input       :
* Output      :
**********************************************************************************/
__weak void HAL_UART_ErrorCallBack(UART_HandleTypeDef *huart)
{
    /*
      NOTE: This function Should be modified, when the callback is needed,
      the HAL_UART_ErrorCallBack could be implemented in the user file.
    */
}

/*********************************************************************************
* Function    : UART_Config_BaudRate
* Description : Config BaudRate
* Input       :
* Output      :
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
static void UART_Config_BaudRate(UART_HandleTypeDef *huart)
{
    uint32_t lu32_PCLK;
    uint32_t lu32_IBAUD, lu32_FBAUD;
    uint64_t lu64_TempValue;

    lu32_PCLK = System_Get_APBClock();

    /* Integral part */
    lu32_IBAUD = lu32_PCLK / (huart->Init.BaudRate * 16);

    /* Fractional part */
    lu64_TempValue = lu32_PCLK % (huart->Init.BaudRate * 16);
    lu64_TempValue = (lu64_TempValue * 1000000) / (huart->Init.BaudRate * 16);
    lu32_FBAUD     = (lu64_TempValue * 64 + 500000) / 1000000;

    if (lu32_FBAUD >= 64)
    {
        huart->Instance->IBRD = lu32_IBAUD + 1;
        huart->Instance->FBRD = 0;
    }
    else
    {
        huart->Instance->IBRD = lu32_IBAUD;
        huart->Instance->FBRD = lu32_FBAUD;
    }
}

/*********************************************************************************
* Function    : HAL_UART_GetState
* Description : Return the uart State
* Input       :
* Output      :
**********************************************************************************/
HAL_StatusTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
{

#if (USE_FULL_ASSERT == 1)
    if (!IS_UART_ALL_INSTANCE(huart->Instance))    return HAL_ERROR;
#endif

	if(huart->lu8_TxBusy || huart->lu8_RxBusy)
	{
		return HAL_BUSY;
	}

	return HAL_OK;
}


/*********************************************************************************
* Function    : HAL_UART_GetError
* Description : Return the uart Error
* Input       :
* Output      :
**********************************************************************************/
uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
{
  	return huart->ErrorCode;
}

/*********************************************************************************
* Function    : HAL_UART_Abort
* Description : Abort ongoing transfers(blocking mode)
* Input       : UART handle
* Output      :
**********************************************************************************/
HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
{
#if (USE_FULL_ASSERT == 1)
	if (!IS_UART_ALL_INSTANCE(huart->Instance))    return HAL_ERROR;
#endif

	/*disble all interrupt*/
	huart->Instance->IE  =0x00;

	/* Disable the UART DMA Tx request if enable */
	if(READ_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE))
	{
		CLEAR_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE);

		/* Abort the UART Tx Channel   */
		if(huart->HDMA_Tx)
		{
			/*Set the UART DMA Abort callback to Null */
			huart->HDMA_Tx->DMA_ITC_Callback =NULL;

			if(HAL_DMA_Abort(huart->HDMA_Tx)!=HAL_OK)
			{
				return HAL_TIMEOUT;
			}
		}
	}

	/* Disable the UART DMA Rx request if enable */
	if(READ_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE))
	{
		CLEAR_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE);

		/* Abort the UART Rx Channel   */
		if(huart->HDMA_Rx)
		{
			/*Set the UART DMA Abort callback to Null */
			huart->HDMA_Rx->DMA_ITC_Callback =NULL;

			if(HAL_DMA_Abort(huart->HDMA_Rx)!=HAL_OK)
			{
				return HAL_TIMEOUT;
			}
		}
	}

	/*Reset Tx and Rx Transfer size*/
	huart->lu32_TxSize = 0;
	huart->lu32_RxSize = 0;

	/* Restore huart->lu8_TxBusy and huart->lu8_RxBusy to Ready */
	huart->lu8_TxBusy = false;
	huart->lu8_RxBusy = false;

	return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_UART_DMAPause
* Description : Pause the DMA Transfer
* Input       : UART handle
* Output      :
**********************************************************************************/
HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
{
#if (USE_FULL_ASSERT == 1)
		if (!IS_UART_ALL_INSTANCE(huart->Instance))    return HAL_ERROR;
#endif

  if(READ_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE))
  {
    /* Disable the UART DMA Tx request */
    CLEAR_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE);
  }

  if (READ_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE))
  {
    /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
    CLEAR_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_FEI);

    /* Disable the UART DMA Rx request */
    CLEAR_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE);
  }

  return HAL_OK;
}


/*********************************************************************************
* Function	  : HAL_UART_DMAResume
* Description : Resume the DMA Transfer
* Input 	  : UART handle
* Output	  :
**********************************************************************************/
HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
{
#if (USE_FULL_ASSERT == 1)
		if (!IS_UART_ALL_INSTANCE(huart->Instance))    return HAL_ERROR;
#endif

  if (huart->lu8_TxBusy == false)
  {
	/* Enable the UART DMA Tx request */
	SET_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE);
  }

  if (huart->lu8_RxBusy == false)
  {
	/* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
	SET_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_FEI);

	/* Enable the UART DMA Rx request */
	SET_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE);
  }

  return HAL_OK;
}

#if 0
/*********************************************************************************
* Function    : fputc
* Description :
* Input       :
* Output      :
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
__weak int fputc(int ch, FILE *f)
{
    if (Uart_Debug == NULL)
    {
        return 0;
    }

    Uart_Debug->DR = ch;

    while ((Uart_Debug->FR & UART_FR_BUSY));

    return ch;
}
#endif