/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V2.5
 *
 * @date        2024-08-08
 *
 * @attention
 *
 *  Copyright (C) 2020-2022 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

#include "include.h"
#include "uart_transfer.h"

/*!
 * @brief       Main program,UART
 *
 * @param       None
 *
 * @retval      None
 *
 */

#define  USER_UART0   0//使用串口0
#define  USER_UART1   0
#define  USER_UART2   1


u8 RxData;

int main(void)
{
    printf("Hello GW3323 UART!\n");
    bsp_sys_init();                     /*System initialization*/
    set_sys_clk(SYS_160M);              //提升主频号160M

    u8 len =10;
    gpio_init_typedef gpio_init_structure;
    gpio_init_structure.gpio_pin = GPIO_PIN_5;
    gpio_init_structure.gpio_dir = GPIO_DIR_INPUT;
    gpio_init_structure.gpio_fen = GPIO_FEN_GPIO;
    gpio_init_structure.gpio_mode = GPIO_MODE_DIGITAL;
    gpio_init_structure.gpio_pupd = GPIO_PUPD_UP;
    gpio_init(GPIOB_REG, &gpio_init_structure);

#if USER_UART0
    printf("USER_UART0");
    uart0_init(115200);///如需测试串口0，打印日志功能会失效
#endif
#if USER_UART1
    printf("USER_UART1");
    uart1_init(115200);
#endif
#if USER_UART2
    printf("USER_UART2");
    uart2_init(115200);
#endif

    RingBuff_Init();    //循环队列初始化

    while (1)
    {
        //接收函数
		if(ringBuff.sendlen>0)
		{
            Read_RingBuff(&RxData);//从环形缓冲区中读取数据
            #if USER_UART0
            uart0_SendByte(RxData);
            #endif
            #if USER_UART1
            uart1_SendByte(RxData);
            #endif
            #if USER_UART2
            uart2_SendByte(RxData);
            #endif
            Send_RingBuffOver();
		}
        //发送函数
        if (gpio_read_bit(GPIOB_REG, GPIO_PIN_5) == RESET)
        {
            delay_ms(20);
            if (gpio_read_bit(GPIOB_REG, GPIO_PIN_5) == RESET)
            {
                len ++;
            #if USER_UART0
                uart0_SendString("\r\nNum1=");
                uart0_SendNumber(len,3);
            #endif
            #if USER_UART1
                uart1_SendString("\r\nNum1=");
                uart1_SendNumber(len,3);
            #endif
            #if USER_UART2
                uart2_SendString("\r\nNum1=");
                uart2_SendNumber(len,3);
            #endif
            }
        }

    }
    return 0;
}
