/******************** (C) COPYRIGHT 2017 SONiX *******************************
* COMPANY: SONiX
* DATE:		 2017/07
* AUTHOR:	 SA1
* IC:			 SN32F240B
*____________________________________________________________________________
* REVISION	Date				User		Description
* 1.00			2017/07/07	Tim		1. First release
* 1.01			2017/07/17	Tim		1. Modified the BL jump address
															2. Added the EMC protect
_________________________________________________________________
* 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	"SN32F240B.h"
#include	"SN32F200_Def.h"
#include	".\Utility\Utility.h"
#include	"Usb\usbram.h"
#include	"Usb\usbdesc.h"
#include	"Usb\usbhw.h"
#include	"Usb\usbuser.h"
#include	"Usb\usbepfunc.h"
#include	"UsbHid\hid.h"
#include	"UsbHid\hiduser.h"

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

/*_____ D E F I N I T I O N S ______________________________________________*/

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

/*_____ F U N C T I O N S __________________________________________________*/
void SysTick_Init(void);
void NDT_Init(void);

/*****************************************************************************
* Function		: main
* Description	: USB HID demo code 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
int	main (void)
{
	SystemInit();
	SystemCoreClockUpdate();
	
	//** Initial GPIO
	SN_GPIO0->CFG = 0x00000000;  //** Enable P0 internal pull-up resistor
	SN_GPIO1->CFG = 0x00000000;  //** Enable P1 internal pull-up resistor
	SN_GPIO2->CFG = 0x00000000;  //** Enable P2 internal pull-up resistor
	SN_GPIO3->CFG = 0x00000000;  //** Enable P3 internal pull-up resistor
	
	SN_GPIO0->MODE = 0x00000000; //** P0 Input mode			
	SN_GPIO1->MODE = 0x00000000; //** P1 Input mode
	SN_GPIO2->MODE = 0x00000000; //** P2 Input mode
	SN_GPIO3->MODE = 0x00000000; //** P3 Input mode
	
	//****************USB Setting START******************//
	USB_Init();								/* USB Initialization */
	//*******************USB Setting END*****************//
	
	while (1)
	{	
		if (sUSB_EumeData.wUSB_Status & mskBUSSUSPEND)	//** Check BusSuspend
		{
			USB_Suspend();
		}
		/*#if (USB_LIBRARY_TYPE == USB_MOUSE_TYPE)
		USB_EPnINFunction(USB_EP1,&wUSB_MouseData,4);
		#else
		USB_EPnINFunction(USB_EP2,&wUSB_MouseData,5);
		#endif*/
	}
}


/*****************************************************************************
* Function		 : SysTick_Init
* Description	 : For EFT Protection
* Input	(Global) : None
* Input	(Local)	 : None
* Output (Global): None
* Return (Local) : None
*****************************************************************************/
void	SysTick_Init (void)
{
	SysTick->LOAD = 0x000752FF;		//RELOAD = (system tick clock frequency ? 10 ms)/1000 -1
	SysTick->VAL = 0xFF; //__SYSTICK_CLEAR_COUNTER_AND_FLAG;
	SysTick->CTRL = 0x7;			//Enable SysTick timer and interrupt	
}


/*****************************************************************************
* Function		 : SysTick_Handler
* Description	 : For EFT Protection
* Input	(Global) : None
* Input	(Local)	 : None
* Output (Global): None
* Return (Local) : None
*****************************************************************************/
__irq void SysTick_Handler(void)
{
	if(bNDT_Flag)			//** Check NDT
	{
		if(SN_SYS0->NDTSTS_b.NDT5V_DET == 1)
		{
			dbNDT_Cnt = 0;
		}
		else
		{
		  dbNDT_Cnt++;
			if(dbNDT_Cnt == 90)//** 900ms
			{
				bNDT_Flag = 0;
			}
		}
	}	
}


/*****************************************************************************
* Function		 : NDT_Init
* Description	 : Noise Detect Init
* Input	(Global) : None
* Input	(Local)	 : None
* Output (Global): None
* Return (Local) : None
*****************************************************************************/
void NDT_Init(void)
{
	SN_SYS0->NDTCTRL = 0x2;					//** Enable NDT 5V
	NVIC_EnableIRQ(NDT_IRQn);
	SN_SYS0->ANTIEFT = 0x04;	
}


/*****************************************************************************
* Function		: NDT_IRQHandler
* Description	: ISR of NDT interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
__irq void NDT_IRQHandler(void)
{
	NVIC_ClearPendingIRQ(NDT_IRQn);
	if (SN_SYS0->NDTSTS)				//** Check NDT 5V detected
	{
		bNDT_Flag = 1;
	}
	SN_SYS0->NDTSTS = 0x3;				//** Clear NDT 5V flag
}


/*****************************************************************************
* Function		: HardFault_Handler
* Description	: ISR of Hard fault interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
__irq void HardFault_Handler(void)
{
	NVIC_SystemReset();
}



