/******************** (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 ".\Interface\SysTick\SysTick.h"
#include ".\Interface\CT\CT16.h"
#include ".\Interface\CT\CT16B0.h"


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


/*_____ 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	: 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_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!!!

	SN_GPIO2->MODE = 0x1FF;			//Set P2 as output

	MN_CT16B0_Init();

	SysTick_Init();

	while(1);
}


/*****************************************************************************
* 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 100 ms period  @ HCLK = 12MHz
	SN_CT16B0->MR0 = 12000;
	SN_CT16B0->PRE = 99; // timer0 scale *100
	//Set MR0 match as TC stop, and enable MR0 interrupt
	SN_CT16B0->MCTRL = (mskCT16_MR0RST_EN|mskCT16_MR0IE_EN);

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

	//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		: HardFault_Handler
* Description	: ISR of Hard fault interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
__irq void HardFault_Handler(void)
{
	NVIC_SystemReset();
}

