/******************** (C) COPYRIGHT 2018 SONiX *******************************
* COMPANY:		SONiX
* DATE:				2018/02
* AUTHOR:			SA1
* IC:					SN32F760B
*____________________________________________________________________________
*	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.
*	1.1				2018/02/02	SA1			1. Add NotPinOut_GPIO_init to set the status of the GPIO which are NOT pin-out to input pull-up.
*																2. Add CodeOption_SN32F760B.s to modify Code option, please modify with Configuration Wizard, 
*																	 and Strongly recommand to keep CS0 for debugging with SN-LINK.
*____________________________________________________________________________
* 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 <SN32F760B.h>
#include <SN32F700_Def.h>
#include ".\PMU\PMU_drive.h"
#include ".\Utility\Utility.h"


/*_____ D E C L A R A T I O N S ____________________________________________*/
void	NotPinOut_GPIO_init(void);


/*_____ D E F I N I T I O N S ______________________________________________*/
#ifndef	SN32F768B					//Do NOT Remove or Modify!!!
	#error Please install SONiX.SN32F7_DFP.1.4.3.pack or version >= 1.4.3
#endif
#define	PKG		SN32F768B		//User SHALL modify the package on demand (SN32F768B, SN32F767B, SN32F766B, SN32F7661B, SN32F7651B, SN32F7652B) 


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


/*_____ F U N C T I O N S __________________________________________________*/

/*****************************************************************************
* Function		: main
* Description	: Demo code of Sleep mode, use P2.0~P2.5 Rising edge trigger 
*								interrupt to wakeup MCU.
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
int	main (void)
{
	//User can configure System Clock with Configuration Wizard in system_SN32F760B.c
	SystemInit();
	SystemCoreClockUpdate();				//Must call for SN32F760B, Please do NOT remove!!!

	//Note: User can refer to ClockSwitch sample code to switch various HCLK if needed.

	//1. User SHALL define PKG on demand.
	//2. User SHALL set the status of the GPIO which are NOT pin-out to input pull-up.
	NotPinOut_GPIO_init();

	//--------------------------------------------------------------------------
	//User Code starts HERE!!!

	SN_GPIO1->MODE_b.MODE0 = 1;					//Set P1.0 as output

	//================================================================================
	//set P2.0~P2.5 as Wakeup pins (input pull-up, falling edge trigger interrupt)
	SN_GPIO2->MODE = 0;									//Set P0 as Input mode
	SN_GPIO2->CFG = 0;									//Enable P0 Pull-up resistor
	SN_GPIO2->IEV = 0x3F;									//Set P0 as falling edge/Low level interrupt
	SN_GPIO2->IBS = 0;
	SN_GPIO2->IS = 0x0;									//Set P0 as Edge sensitive

	SN_GPIO2->IC = 0x3F;
	SN_GPIO2->IE = 0x3F;
	NVIC_ClearPendingIRQ(P2_IRQn);
	NVIC_EnableIRQ(P2_IRQn);
	//================================================================================

	PMU_Setting(PMU_SLEEP);

	while(1)
	{
		SN_GPIO1->BSET = 0x1;			//Set P1.0
		UT_DelayNms(200);
		SN_GPIO1->BCLR = 0x1;			//Clear P1.0
		UT_DelayNms(200);
	}
}

/*****************************************************************************
* Function		: NotPinOut_GPIO_init
* Description	: Set the status of the GPIO which are NOT pin-out to input pull-up. 
* Input				: None
* Output			: None
* Return			: None
* Note				: 1. User SHALL define PKG on demand.
*****************************************************************************/
void	NotPinOut_GPIO_init(void)
{
	//(SN32F768B, SN32F767B, SN32F766B, SN32F7651B, SN32F7652B) 
#if (PKG == SN32F767B)
	//set P0.8~P0.9, P0.13~P0.15 to input pull-up
	SN_GPIO0->CFG = 0x02A0AAAA;
	//set P1.0~P1.2 to input pull-up
	SN_GPIO1->CFG = 0xAAAAAA80;
	//set P2.12~P2.14 to input pull-up
	SN_GPIO2->CFG = 0x80AAAAAA;
	//set P3.0~P3.4 to input pull-up
	SN_GPIO3->CFG = 0x00AAA800;
#elif (PKG == SN32F766B)
	//set P0.8~P0.9, P0.12~P0.15 to input pull-up
	SN_GPIO0->CFG = 0x00A0AAAA;
	//set P1.0~P1.2 to input pull-up
	SN_GPIO1->CFG = 0xAAAAAA80;
	//set P2.11~P2.14 to input pull-up
	SN_GPIO2->CFG = 0x802AAAAA;
	//set P3.0~P3.4 to input pull-up
	SN_GPIO3->CFG = 0x00AAA800;
#elif (PKG == SN32F7661B)
	//set P0.2~P0.9 to input pull-up
	SN_GPIO0->CFG = 0xAAA0000A;
	//set P1.0~P1.1 to input pull-up
	SN_GPIO1->CFG = 0xAAAAAAA0;
	//set P2.9~P2.15 to input pull-up
	SN_GPIO2->CFG = 0x0002AAAA;
	//set P3.0, P3.10~P3.11 to input pull-up
	SN_GPIO3->CFG = 0x000AAAA8;
#elif (PKG == SN32F7651B)
	//set P0.2~P0.5, P0.8~P0.9, P0.12~P0.15 to input pull-up
	SN_GPIO0->CFG = 0x00A0A00A;
	//set P1.0~P1.4, P1.15 to input pull-up
	SN_GPIO1->CFG = 0x2AAAA800;
	//set P2.6~P2.15 to input pull-up
	SN_GPIO2->CFG = 0x00000AAA;
	//set P3.0~P3.4, P3.10~P3.11 to input pull-up
	SN_GPIO3->CFG = 0x000AA800;
#elif (PKG == SN32F7652B)
	//set P0.0~P0.1, P0.8~P0.15 to input pull-up
	SN_GPIO0->CFG = 0x0000AAA0;
	//set P1.0~P1.4, P1.15 to input pull-up
	SN_GPIO1->CFG = 0x2AAAA800;
	//set P2.6~P2.15 to input pull-up
	SN_GPIO2->CFG = 0x00000AAA;
	//set P3.0~P3.4, P3.10~P3.11 to input pull-up
	SN_GPIO3->CFG = 0x000AA800;
#endif
}

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

