/******************** (C) COPYRIGHT 2020 SONiX *******************************
* COMPANY:  SONiX
* DATE:     2021/06
* AUTHOR:   SA1
* IC:       
* DESCRIPTION:
*
*   This sample code demonstrates how to use the UART function.
*
*____________________________________________________________________________
* REVISION  Date        User  Description
* 1.0       2021/06/28  SA1   First release
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/
#define __XRAM_SFR_H__
/*_____ I N C L U D E S ____________________________________________________*/
#include <SN8F5754.h>
#include <stdlib.h>
#include "uart.h"
#include "crc.h"
/*_____ D E F I N I T I O N S ______________________________________________*/

/*_____ D E C L A R A T I O N S ____________________________________________*/
typedef struct{
	uint8_t  baryBuf[8];
	uint16_t dbCRC16;
}UartData_st;

UartData_st stUART_Tx;
UartData_st stUART_Rx;
uint16_t	dbCRC16_Calculation;

/*_____ F U N C T I O N S __________________________________________________*/
void GPIO_Initialization(void);
void UART_SettingTxBuf(uint8_t bData, uint8_t bLength);	
/*_____ M A C R O S _________________________________________________________*/
#define	IO_PORT0_OUTPUT_LOW		P0M |= 0xFF;	P0 &= 0x00;
#define	IO_PORT1_OUTPUT_LOW		P1M |= 0xFF;	P1 &= 0x00;
#define	IO_PORT2_OUTPUT_LOW		P2M |= 0xFF;	P2 &= 0x00;
#define	IO_PORT3_OUTPUT_LOW		P3M |= 0xFF;	P3 &= 0x00;

/**
  * @brief  Main program: UART Demo Code. Connecting P12 to P32.
  * @param  : None.
  * @retval : None.
  */
int main(void)
{
  GPIO_Initialization();
	CRC_Initialization(mskCRCM_POLY_CRC16, mskCRCM_RANGE_3FDD);
	EAL = 1;

	/*	UARTn_Init( (1) , (2) , (3) );
			(1) : "Baud rate selection"
			(2)	: "RX mode: write RX Buf address"	
						"TX mode: NULL"
			(3)	: "RX mode: write RX Buf length"	
						"TX mode: NULL"												*/
	UART0_Init(eBAUD_RATE_19200, &stUART_Rx.baryBuf[0], sizeof(stUART_Rx));
	UART1_Init(eBAUD_RATE_19200, NULL, NULL);   							
	
	UART_SettingTxBuf(0xA0, sizeof(stUART_Tx.baryBuf)); 
	UART1_TransmitData(&stUART_Tx.baryBuf[0], sizeof(stUART_Tx));

	while(1)
	{
		/* UART0 RX received Data via U0RX INT*/
		if(UART0_ReceivedDone())
		{
			/* 	(1) Calculate CRC16 result from stUART_Rx.baryBuf 
					(2) Compare RX calculate CRC16 vs. TX.CRC16 */
			dbCRC16_Calculation	= CRC_ExecuteRamByteCalculation(&stUART_Rx.baryBuf[0], sizeof(stUART_Rx.baryBuf)); /* (1)	*/
			if(stUART_Rx.dbCRC16 == dbCRC16_Calculation) 			/* (2) */
			{
				UART_SettingTxBuf(0xB0, sizeof(stUART_Tx.baryBuf));
				UART0_TransmitData(&stUART_Tx.baryBuf[0], sizeof(stUART_Tx));	
			}	
			else 
			{
				while(1); 	/* Compare CRC16 Error*/
			}
		}
	}
}


/**
 *	@Brief		Initialize GPIO status
 *	@param		None
 *	@retval		None
 */
void GPIO_Initialization(void)
{
	/*	Set whole IOs to output low	*/
	IO_PORT0_OUTPUT_LOW
	IO_PORT1_OUTPUT_LOW
	IO_PORT2_OUTPUT_LOW
	IO_PORT3_OUTPUT_LOW
}

/**
 *	@Brief		Setting Tx Buf
 *	@param		bData, bLength
 *	@retval		None
 */
void UART_SettingTxBuf(uint8_t bData, uint8_t bLength)
{
	uint8_t i;
	for(i=0; i< bLength ; i++)
	{
		stUART_Tx.baryBuf[i] = bData +i;
	}
	/* Calculate CRC16	*/
	stUART_Tx.dbCRC16	= CRC_ExecuteRamByteCalculation(&stUART_Tx.baryBuf[0], sizeof(stUART_Tx.baryBuf));	
}
