/******************** (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.
*****************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include <SN8F5762.h>
#include <stdlib.h>
#include "uart.h"
/*_____ D E F I N I T I O N S ______________________________________________*/

/*_____ D E C L A R A T I O N S ____________________________________________*/
uint8_t baryUSER_TxBuf[] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7};
uint8_t baryUSER_RxBuf[] = {0, 0, 0, 0, 0, 0, 0, 0};
/*_____ F U N C T I O N S __________________________________________________*/
void GPIO_Initialization(void);
	
/*_____ 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;


/**
  * @brief  Main program: UART Demo Code. Connecting P44 to P15.
  * @param  : None.
  * @retval : None.
  */
int main(void)
{
  GPIO_Initialization();
	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, &baryUSER_RxBuf[0], sizeof(baryUSER_RxBuf));		/*	TX + RX	*/
	UART1_Init(eBAUD_RATE_19200, NULL, NULL);   																/*  TX only	*/

	UART1_TransmitData(&baryUSER_TxBuf[0], sizeof(baryUSER_TxBuf));

	while(1)
	{
		if(UART0_ReceivedDone())
		{	
			/* read uesr define RX buffer */
			if((baryUSER_RxBuf[0] == 0xA0) && (baryUSER_RxBuf[7] == 0xA7))
			{
				//P00=1; //TEST IO
		  	//P00=0; //TEST IO
				/* Return Data */
				baryUSER_TxBuf[0] = 0xB0;
				baryUSER_TxBuf[1] = 0xB1;	
				baryUSER_TxBuf[2] = 0xB2;
				baryUSER_TxBuf[3] = 0xB3;	
				baryUSER_TxBuf[4] = 0xB4;
				baryUSER_TxBuf[5] = 0xB5;	
				baryUSER_TxBuf[6] = 0xB6;
				baryUSER_TxBuf[7] = 0xB7;
	
				UART0_TransmitData(&baryUSER_TxBuf[0], sizeof(baryUSER_TxBuf));	
			}	
		}
	}
}

/**
 *	@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

}

