/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.3
 * @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>
#include<stdlib.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

#define             DatumCount          10

int                 i;
uint32_t            u32RXData, u32TXData;
uint8_t             u8RXData, u8TXData;

uint32_t UART_RX_Word(void)
{
    uint32_t i = 0;
    uint32_t u32Data = 0;

    for (i = 0; i < 4; i++)
    {
        /* Wait until data is available in RBR or the FIFO */
        while (!UART_IsRxNotEmpty(UART));

        /* In receive process, the MSB come first and the LSB comes the last */
        u32Data |= UART_ReadByte(UART) << ((3 - i) * 8);
    }

    return u32Data;
}

uint8_t UART_RX_Byte(void)
{
    uint8_t u8Data = 0;

    /* Wait until data is available in RBR or the FIFO */
    while (!UART_IsRxNotEmpty(UART));

    /* In receive process, the MSB come first and the LSB comes the last */
    u8Data = UART_ReadByte(UART);

    return u8Data;
}

void UART_TX_Byte(uint8_t u8Data)
{

    /* Write the datum to UART Transmit Holding Register */
    UART_WriteByte(UART, u8Data);

    /* Wait for the TX FIFO empty */
    while (!UART_IsTxDone(UART));
}

void UART_TX_Word(uint32_t u32Data)
{
    uint32_t i = 0;

    for (i = 0; i < 4; i++)
    {
        /*
         * Write the datum to UART Transmit Holding Register, which will be sent out,
         * the MSB sent out first.
         */
        UART_WriteByte(UART, (u32Data & (0xFF000000 >> (i * 8))) >> ((3 - i) * 8));

        /* Wait for the TX FIFO empty */
        while (!UART_IsTxDone(UART));
    }
}


/*************************************************************************************************************************
*
* @brief      In this case, UART using polling model receive the datum come from transmitting end, and sent these datum
*             back, the transmitting end receive these back datum and have a double check.
*
*
*************************************************************************************************************************/


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);

    /* Receive the world data comes from the transmitting end, and sent it back */
    for (i = 0; i < DatumCount; i++)
    {
        u32RXData = UART_RX_Word();
        UART_TX_Word(u32RXData);
    }

    /* Receive the byte data comes from the transmitting end, and sent it back */
    for (i = 0; i < DatumCount; i++)
    {
        u8RXData = UART_RX_Byte();
        UART_TX_Byte(u8RXData);
    }

    while (1)
    {

    }
}
