/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.1
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include<stdio.h>

#if defined(SPC1158)
#include "spc1158.h"
#elif defined(SPD1148)
#include "spd1148.h"
#elif defined(SPD1178)
#include "spd1178.h"
#elif defined(SPD1188)
#include "spd1188.h"
#elif defined(SPD1163)
#include "spd1163.h"
#else
#include "spc1168.h"
#endif

uint8_t             u8data;
uint32_t            gu32_cnt_uart_fcr_buffer;


/*************************************************************************************************************************
*
* @brief      In this case, UART read data using INT model, when there is one byte in the receive FIFO, the RX INT will be
*             triggered, then, the data will be receive in the INT function.
*
*************************************************************************************************************************/


int main()
{
    FLASH_WALLOW();

#if defined(SPC1158)
    FLASH_SetTiming(100000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(100000000UL);
#else
    FLASH_SetTiming(200000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(200000000UL);
#endif

    Delay_Init();

    /*
     * Init the UART
     *
     * 1.Set the GPIO34/35 as UART FUNC
     *
     * 2.Enable the UART CLK
     *
     * 3.Set the rest para
     */
    GPIO_SetPinChannel(GPIO_34, GPIO34_UART_TXD);
    GPIO_SetPinChannel(GPIO_35, GPIO35_UART_RXD);
    CLOCK_EnableModule(UART_MODULE);
    UART_Init(UART, 38400);

    /* Enable the global INT of UART */
    NVIC_EnableIRQ(UART_IRQn);
    
    /* Set the receive fifo INT trigger level as 1 bytes */
    UART_SetFIFOTriggerThreshold(UART, UART_RXFIFO_1BYTE_OR_MORE, UART_TXFIFO_EMPTY);

    /* Enable data reaching INT */
    UART_EnableRxDataAvailableInt(UART);

    /* Enable UART INT to MCU */
    UART_EnableInt(UART);

    while (1)
    {

    }
}

void UART_IRQHandler(void)
{
    /* Read data until the receive FIFO is empty */
    while (UART_GetRxFIFOLevel(UART))
    {
        u8data = UART_ReadByte(UART) - '0';
        printf("INT rece data(0~10): %d\n", u8data);
    }
}
