/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V1.8.0
 * @date     31-Dec-2021
 *
 * @note
 * Copyright (C) 2021 Spintrol Limited Corporation. 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"spc1168.h"
#include<stdio.h>
#include<stdlib.h>

#define             DatumCount          10

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

uint32_t UART_RX_Word(void) 
{
  volatile 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) 
{
  volatile 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. 
 *
 *             This demo is a couple with another named 'UART_TX_and_CheckRX'.
 *
 *************************************************************************************************************************/


int main()
{
  FLASH_WALLOW();
  FLASH_SetTiming(200000000);
  /* Disable flash write access after flash operation had done */
  FLASH_WDIS();

  CLOCK_InitWithRCO(CLOCK_HCLK_200MHZ);

  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(u8TXData);
  }

  while(1)
  {

  }
}
