/*********************************************************
*Copyright (C), 2017, Shanghai Eastsoft Microelectronics Co., Ltd.
 * @文件名:  uart.c
 * @作  者:  AE Team
 * @版  本:  V1.00
 * @日  期:  2022/07/25
 * @描  述:
 * @note
 *          Change Logs:
 *          Date            Author          Notes
 *          25 July 2022    AE Team         change license to Apache-2.0
 *
 * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **********************************************************************************
 */


#include "uart.h"
#include "string.h"


/*********************************************************
函数名: void UART_Pins_Init(void)
描  述: 初始化UART通讯IO
输入值: 无
输出值: 无
返回值: 无
**********************************************************/
void UART_Pins_Init(void)
{
    GPIO_InitSettingType x;
	memset(&x, 0, sizeof(x));

    x.Signal = GPIO_Pin_Signal_Digital;
    x.Dir = GPIO_Direction_Output;
    x.Func = GPIO_Reuse_Func1;
    x.ODE = GPIO_ODE_Output_Disable;
    x.DS = GPIO_DS_Output_Strong;
    x.PUE = GPIO_PUE_Input_Disable;
    x.PDE = GPIO_PDE_Input_Disable;
    GPIO_Init(GPIO_Pin_A23, &x);        /* TXD - PA23 */

    x.Signal = GPIO_Pin_Signal_Digital;
    x.Func = GPIO_Reuse_Func1;
    x.Dir = GPIO_Direction_Input;
    x.PUE = GPIO_PUE_Input_Disable;
    x.PDE = GPIO_PDE_Input_Disable;
    GPIO_Init(GPIO_Pin_A22, &x);        /* RXD - PA22 */
}


/*********************************************************
函数名: void UARTInit(void)
描  述: UART初始化子程序
输入值: 无
输出值: 无
返回值: 无
**********************************************************/
void UARTInit(void)
{
    UART_InitStruType y;
	memset(&y, 0, sizeof(y));
	
    y.UART_ClockSet = UART_Clock_1;         /* 时钟选择：Pclk */
    y.UART_BaudRate = 115200;               /* 波特率:115200 */
    y.UART_StopBits = UART_StopBits_1;      /* 停止位：1 */
    y.UART_TxMode = UART_DataMode_8;        /* 发送数据格式：8位数据 */
    y.UART_TxPolar = UART_Polar_Normal;     /* 发送端口极性：正常 */
    y.UART_RxMode = UART_DataMode_8;        /* 接收数据格式：8位数据 */
    y.UART_RxPolar = UART_Polar_Normal;     /* 接收端口极性：正常 */
    UART_Init(UART0, &y);

    UART_TBIMConfig(UART0, UART_TBIM_Full);                      /* UART0发送缓冲区空中断模式: 全空中断 */
    UART_RBIMConfig(UART0, UART_RBIM_Byte);                      /* UART0接收缓冲区空中断模式: 字节满中断 */

    UART_Pins_Init();
    UART0_TxEnable();
}

/*********************************************************
函数名: void UARTSendData(uint32_t data)
描  述: UART发送一个32位无符号整形数据
输入值: 无
输出值: 无
返回值: 无
**********************************************************/
void UARTSendData(uint32_t data)
{
    uint8_t i;
    uint8_t buf[4];

    for (i = 0; i < 4; i++)
    {
        buf[i] = (uint8_t)((data & (0xFF << ((3 - i) * 8))) >> ((3 - i) * 8));

        while (UART_GetFlagStatus(UART0, UART_FLAG_TB) == RESET);

        UART_SendByte(UART0, buf[i]);
    }
}


/*************************END OF FILE**********************/
