/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.0
 * @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;
}

uint32_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 to transmit datum, and the other UART communicate with this
*             UART end will sent these datum back, The TX UART end in this demo will receive the back datum and do a
*             double check.
*
*                                            ------                  -------
*                                           |      |                |       |
*                                           |  TX  |--------------->|  RX   |
*                                           |  RX  |<---------------|  TX   |
*                                           |      |                |       |
*                                           |      |                |       |
*                                            ------                  -------
*                                         UART TX end               UART RX end
*
*
*************************************************************************************************************************/


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

    /* Generate the world datum to be sent and sent them out */
    for (i = 0; i < DatumCount; i++)
    {
        u32TXData = rand() & 0xFFFFFFFF;
        UART_TX_Word(u32TXData);
        u32RXData = UART_RX_Word();
        if (u32TXData != u32RXData)
        {
            printf("UART TX/RX Worlds test FAIL\n");
            return 0;
        }
    }
    printf("UART TX/RX Worlds test PASS\n");

    /* Generate the byte datum to be sent and sent them out */
    for (i = 0; i < DatumCount; i++)
    {
        u8TXData = rand() & 0xFF;
        UART_TX_Byte(u8TXData);
        u8RXData = UART_RX_Byte();
        if (u8TXData != u8RXData)
        {
            printf("UART TX/RX BYTE test FAIL\n");
            return 0;
        }
    }
    printf("UART TX/RX BYTE test PASS\n");

    while (1)
    {

    }
}
