/******************** (C) COPYRIGHT 2021 SONiX *******************************
* COMPANY:		SONiX
* DATE:				2021/03
* AUTHOR:			SA1
* IC:					SN32F770
*____________________________________________________________________________
*	REVISION	Date				User		Description
*	1.0				2017/07/14	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_SN32F770.s to modify Code option, please modify with Configuration Wizard, 
*																	 and Strongly recommend to keep CS0 for debugging with SN-LINK.
*	1.2				2018/10/05	SA1			1. Remove CodeOption_SN32F770.s, please contact SONiX FAE if needed.
*																2. Update system_SN32F770.c to V1.1.0
*																3. Update LPMODE[3:0] to 0101b when HCLK>24MHz in Flash.h
* 1.3       2021/03/15  SA1     1. Merge SysTick and WDT code.
*                               2. Modify __SYSTICK_CLEAR_CONTER_AND_FLAG in SysTick.h
*                               3. Update WDT_Init, WDT_IRQHandler and enable WDT reset function.
*                               4. Add SysTick_ConfigTime function instead of  __SYSTICK_SET_TIMER_PERIOD(ms) in SysTick.c
*																5. Fix SN_CT16B0->MR0 setting in MN_CT16B0_Init, and update CT16B0_IRQHandler for LED toggle.
*
*____________________________________________________________________________
* 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 <SN32F770.h>
#include <SN32F700_Def.h>
#include ".\Interface\SysTick\SysTick.h"
#include ".\Interface\WDT\WDT.h"
#include ".\Interface\CT\CT16.h"
#include ".\Interface\CT\CT16B0.h"


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


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


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


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

/*****************************************************************************
* Function		: main
* Description	: LED toggles based on SysTick timer and CT16B0 timer. 
* Input			: None
* Output		: None
* Return		: None
* Note			: Connect LEDs to P2.0 and P2.1
*****************************************************************************/
int	main (void)
{
	//User can configure System Clock with Configuration Wizard in system_SN32F770.c
	SystemInit();
	SystemCoreClockUpdate();				//Must call for SN32F770, 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_GPIO2->MODE = 0x1FF;			//Set P2 as output

	MN_CT16B0_Init();

	SysTick_Init();
  
  WDT_Init();									//Set WDT reset overflow time ~ 250ms

	while (1)
	{
		__WDT_FEED_VALUE;
		
		#if SYSTICK_IRQ == POLLING_METHOD
		if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)
		{
			if (++i == 50)
			{
				i = 0;
				SN_GPIO2->DATA ^= 0x2;		//Toggle P2.1
			}
		}
		#endif
	}
}


/*****************************************************************************
* Function		: MN_CT16B0_Init
* Description	: Init CT16B0 timer. 
* Input			: None
* Output		: None
* Return		: None
* Note			: Connect LEDs to P2.0 and P2.1
*****************************************************************************/
void MN_CT16B0_Init(void)
{
	CT16B0_Init();

	//Set MR0 value for 1 ms period  @ HCLK = 12MHz
	SN_CT16B0->MR0 = (PWM_KEY|0x2EE0);

	//Set MR0 match as TC stop, and enable MR0 interrupt
	SN_CT16B0->MCTRL = (PWM_KEY|mskCT16_MR0RST_EN|mskCT16_MR0IE_EN);

	//Set CT16B0 as the up-counting mode.
	SN_CT16B0->TMRCTRL = (mskCT16_CRST|mskCT16_CM_EDGE_UP);

	//Wait until timer reset done.
	while (SN_CT16B0->TMRCTRL & mskCT16_CRST);

	//Let TC start counting.
	SN_CT16B0->TMRCTRL |= mskCT16_CEN_EN;
	
	//Enable CT16B0 NVIC interrupt
	NVIC_ClearPendingIRQ(CT16B0_IRQn);
	CT16B0_NvicEnable();
}

/*****************************************************************************
* 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)
{
#if (PKG == SN32F773)
	//set P0.0, P0.9 to input pull-up
	SN_GPIO0->CFG = 0x0022AAA8;
	//set P2.3, P2.5 to input pull-up
	SN_GPIO2->CFG = 0x0000222A;
#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();
}

