/******************** (C) COPYRIGHT 2017 SONiX *******************************
* COMPANY:		SONiX
* DATE:				2017/07
* AUTHOR:			SA1
* IC:					SN32F240B
*____________________________________________________________________________
*	REVISION	Date				User		Description
*	1.0				2017/07/07	SA1			1. First version released
*																2. Compatible to CMSIS DFP Architecture in Keil MDK v5.X (http://www.keil.com/dd2/pack/)
*																3. Run HexConvert to generate bin file and show checksum after building.
*
*____________________________________________________________________________
* 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 ".\PMU\PMU_drive.h"
#include ".\Utility\Utility.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 __________________________________________________*/

/*****************************************************************************
* Function		: main
* Description	: Demo code of Deep Sleep mode, use P0.7~P0.9 Rising edge trigger 
*								interrupt to wakeup MCU, and toggle P2.0, P2.1, or P2.2 to indicate
*								the wakeup source is P0.7, P0.8, or P0.9.
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
int	main (void)
{
	//User can configure System Clock with Configuration Wizard in system_SN32F240B.c
	SystemInit();
	SystemCoreClockUpdate();				//Must call for SN32F240B, Please do NOT remove!!!

	//Note: User can refer to ClockSwitch sample code to switch various HCLK if needed.
	
	//--------------------------------------------------------------------------
	//User Code starts HERE!!!

	//Set P2 as Output Low
	SN_GPIO2->MODE = 0xFFFF;
	SN_GPIO2->DATA = 0x0;

	//================================================================================
	//set P0.7~P0.9 as Deep Sleep Wakeup pins (input pull-up, Rising edge trigger interrupt)	
	SN_GPIO0->MODE = 0;									//Set P0 as Input mode
	SN_GPIO0->CFG = 0;									//Enable P0 Pull-up resistor
	SN_GPIO0->IEV = 0x380;									//Set P0 as falling edge/Low level interrupt
	SN_GPIO0->IBS = 0;
	SN_GPIO0->IS = 0x0;									//Set P0 as Edge sensitive

	SN_GPIO0->IC = 0xFFFF;
	SN_GPIO0->IE = 0x380;								//Enable P0.7~P0.9 Interrupt
	UT_DelayNms(100);	
	
	NVIC_ClearPendingIRQ(P0_IRQn);
	NVIC_EnableIRQ(P0_IRQn);
	//================================================================================
	while((SN_GPIO0->DATA & 0x380)!= 0x380);
	PMU_Setting(PMU_DEEP_SLEEP);
	
	while(1)
	{
		SN_GPIO2->DATA = 0xFFFF;
		UT_DelayNms(100);		
		SN_GPIO2->DATA = 0x0;
		UT_DelayNms(100);
	}
}


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

