
/******************** (C) COPYRIGHT 2021 SONiX *******************************
* COMPANY:	SONiX
* DATE:		2022/04/12
* IC:			SN8F5762
*____________________________________________________________________________
* REVISION		Date				Author		Description
* 1.0					2022/04/12	SONiX			1. First release
*____________________________________________________________________________
*****************************************************************************/
/*_____ I N C L U D E S _________________________________*/
#include <SN8F5762.h>
#include "ilrc_calibration.h"
#include "timer.h"
/*_____ F U N C T I O N S _______________________________*/

void ILRC_SetAutoCalibration(void);
void GPIO_Initialization(void);
void TIMER0_Initialization(void);
/*_____ D E F I N I T I O N S ___________________________*/


/*_____ D E C L A R A T I O N S _________________________*/

/*_____ 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: ILRCCalibration Test Demo Function
 *	@param		None
 *	@return		None
 */

void main(void)
{
	/*
	 *	(1) GPIO Initial
	 *	(2) ILRC Auto-Trim	Function
	 *  
	 */
		GPIO_Initialization();     																				/*	(1)	*/	
//		P00 =1;	                                                        //TEST IO
		ILRC_SetAutoCalibration();																				/*	(2)	*/
//		P00 =0;                                                         //TEST IO
	  TIMER0_Initialization();
		while(1);			
}

/**
 *  @Brief    initial GPIO status
 *  @param    None
 *  @return   None
 */
void GPIO_Initialization(void)
{
    IO_PORT0_OUTPUT_LOW
    IO_PORT1_OUTPUT_LOW
    IO_PORT2_OUTPUT_LOW	
}

/**
 *	@Brief		ILRC_Setautocalibration: ILRCCalibration Auto Trim Function
 *	@param		None
 *	@return		None
 */

void ILRC_SetAutoCalibration(void)
{
	/*
   *	Auto-Calibration Command:
	 *	(1) Enable	ILRC Auto-Calibration Function
	 *	(2) Execute ILRC Calibration
	 *  (3) Waiting For ILRC Calibration Done 
	 */
		CLKCAL = mskCLKCAL_CALEN;																						/*	(1)	*/ 
		FRQCMD = mskFRQCMD_EXRCUTED_ILRC_AUTO_TRIM;													/*	(2)	*/
		while(CLKCAL & mskCLKCAL_CALEN);														  			/*	(3)	*/

}
void TIMER0_Initialization(void)
{
	/*
	 *	(1) Set Timer0 Mode2 (8-bit Auto-Reload Timer) And ILRC As Timer0 Source
	 *	(2) Set	TL0 Overflow Value
	 *	(3)	Set	TH0 Overflow Value
	 *	  
	 */		
		TMOD |= (mskTMOD_T0_MODE2 | mskTMOD_T0CT);			/*	(1)	*/
		TH0 = TH0_VALUE;            									  /*	(2)	*/
    TL0 = TH0_VALUE;            								  	/*	(3)	*/	
		TCON0 |= (mskTCON0_T0EXT_losc | mskTCON0_T0_DIV1);  						
	  IEN0 |= mskIEN0_ET0;  									
		EAL =	1;																	
		TCON |=	mskTCON_TR0;	
}
void ISR_Timer0(void) interrupt ISRTimer0	using 3
{
		P01 ^=  1;														/*	Timer0 ISR Test Pin	Toggle	*/ 
}

