/******************** (C) COPYRIGHT 2022 SONiX *******************************
* COMPANY:	SONiX
* DATE:		2022/05/016
* IC:			SN8F5835
*____________________________________________________________________________
* REVISION		Date				Author		Description
* 1.0					2022/05/09	Eden			1. First release
*____________________________________________________________________________
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include <SN8F5762.h>
#include "softuart.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 bSend_count=0,bRead_count=0;
uint8_t bRead_num_count=0,bSend_num_count=0;
uint8_t bRead_buffer=0,bSend_buffer=0;
uint8_t bRead_flag=0,bSend_flag=0;
uint8_t bRead_data,bSend_data;
/*_____ F U N C T I O N S __________________________________________________*/
void TX_run(void);
void RX_run(void); 


/*_____ M A C R O S _________________________________________________________*/

void TIMER1_Initialization(void)

{
	/*
	 *	(1) Set Timer1 Mode0 (13-bit Timer)	And IHRC As Timer Source
	 *	(2) Set	TL1 Overflow Value
	 *	(3)	Set	TH1 Overflow Value
	 *	  
	 */		
		TMOD |= (mskTMOD_T1_MODE1|mskTMOD_T1CT);			/*	(1) */
		TH1 = TH1_VALUE;            									/*	(2)	*/
    TL1 = TL1_VALUE;            									/*	(3)	*/	
		TCON0 |= mskTCON0_T1_DIV1;  								 	
	  IEN0 |= mskIEN0_ET1;  												
		EAL =	1;																			
		TCON |=	mskTCON_TR1;													
													

}
void UART_Initialization(void)
{
		RX_INPUT
		TX_OUTPUT	
}
void ISR_Timer1(void) interrupt ISRTimer1
{		
	
	/*
	 *	(1) Set  Re-loader Timer1
	 *	(2) SelectUART_MODEL	
	 *	  
	 */	
			TCON &=	TIMER_STOP;			//(1)					
			TL1 = TL1_VALUE; 																
			TH1 = TH1_VALUE;																
			TCON |=	(mskTCON_TR1);
			if(UART_Model==0)				//(2)
			{
					TX_run();
			}
			else if(UART_Model==1)
			{
					RX_run();
			}
			else if(UART_Model==2)
			{
					TX_run();
					RX_run();
			}		
			
}

/**
 *	@Brief		TX Send data Test
 *	@param		output
 *	@retval		success return 1  error return 0
 */
uint8_t Send_data(uint8_t output)
{
	if(bSend_flag==0)
	{	
			bSend_buffer=output;
			bSend_flag=1;
			return 1;
	}
	else
	{
			return 0;
	}	
}
void TX_run(void)
{
	/*************TX DATA*************/
	/*	
	 *	(1) Send Start bit
	 *	(2) Send data	(4 cycle run once)
	 *	(3)	Send Stop bit	
	 *	
	 */							
		
	bSend_count++;	
			if(bSend_flag==1)		// (1)
			{
					TX=0;
					bSend_flag=2;
					bSend_count=0;
			}
			if((bSend_flag==2)&&(bSend_count==4))  //4 cycle run once
			{
					bSend_num_count++;
					if(bSend_num_count<=8)
					{
							bSend_count=0;
							if(bSend_buffer&0x01)		//(2)
							{
									TX=1;
							}
							else
							{
									TX=0;
							}
							bSend_buffer>>=1;
									
					}
					else if(bSend_num_count==9)  //(3)
					{
							TX=1;
					}	
					else
					{
							bSend_num_count=0;
							bSend_flag=0;
					}	
			}
}
void RX_run(void)
{
	/*************RX DATA*************/
	/*
	 *	(1) RX Start bit
	 *	(2) Wait Start bit end
	 *	(3)	Read RX data to buffer 	
	 */				
			bRead_count++;
			if((RX==0)&&(bRead_flag==0)) // (1)
			{
					bRead_flag=1;
					bRead_count=0;
			}
			if((bRead_flag==1)&&(bRead_count==3)) // (2)
			{
					bRead_flag=2;
					bRead_count=0;
			}
			if((bRead_flag==2)&&(bRead_count==2)) // (3)
			{
					bRead_num_count++;
					if(bRead_num_count<=8)
					{
							 bRead_buffer>>=1;
								if(RX==1)
								{
										bRead_buffer+=0x80;
								}
					}
					else	
					{
							bRead_flag=0;
							bRead_num_count=0;
							
							/******* Rx_data output check****/
							OB_TEST_PIN ^=  1;       // RX data Send check pin
							bRead_data=bRead_buffer;
							Send_data(bRead_data);
							bSend_data=bRead_data;
							/********************************/
					}		
			}
			else if(bRead_count==4)			//  4 cycle clear once
			{
					bRead_count=0;
			}
}
