/******************** (C) COPYRIGHT 2018 SONiX *******************************
* COMPANY:		SONiX
* DATE:				2018/10
* AUTHOR:			SA1
* IC:					SN32F100
*____________________________________________________________________________
*	REVISION	Date				User		Description
*	1.0				2013/04/09	SA1			First release
*	2.0				2014/02/27	SA1			1. Update project settings & add user guides of sample codes.
*																2. Each project has its own system_SN32F100.c
*																3. Update CT16Bx & CT32Bx functions.
*	3.0				2015/05/29	SA1			1. Compatible to CMSIS DFP Architecture in Keil MDK v5.X (http://www.keil.com/dd2/pack/)
*																2. Run HexConvert to generate bin file and show checksum after building.
*	4.0				2018/02/07	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_SN32F100.s to modify Code option, please modify with Configuration Wizard, 
*																	 and Strongly recommend to keep CS0 for debugging with SN-LINK.
*	4.1				2018/10/05	SA1			1. Remove CodeOption_SN32F100.s, please contact SONiX FAE if needed.
*
*____________________________________________________________________________
* 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 <SN32F100.h>
#include <SN32F100_Def.h>
#include ".\Interface\SysTick\SysTick.h"
#include ".\Interface\CT\CT32.h"
#include ".\Interface\CT\CT32B0.h"


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


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


/*_____ 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 CT32B0 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_SN32F100.c
	SystemInit();
	SystemCoreClockUpdate();

	//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 = 0x3FF;			//Set P2 as output

	MN_CT32B0_Init();

	SysTick_Init();

	while (1);
}


/*****************************************************************************
* Function		: MN_CT32B0_Init
* Description	: LED toggles based on SysTick timer and CT32B0 timer. 
* Input			: None
* Output		: None
* Return		: None
* Note			: Connect LEDs to P2.0 and P2.1
*****************************************************************************/
void MN_CT32B0_Init(void)
{
	CT32B0_Init();

	//Set MR0 value for 100 ms period  @ HCLK = 12MHz
	SN_CT32B0->MR0 = 0x124F80;

	//Set MR0 match as TC stop, and enable MR0 interrupt
	SN_CT32B0->MCTRL = (mskCT32_MR0RST_EN|mskCT32_MR0IE_EN);
	
	//Set CT32B0 as the up-counting mode.
	SN_CT32B0->TMRCTRL = mskCT32_CRST;

	//Wait until timer reset done.
	while (SN_CT32B0->TMRCTRL & mskCT32_CRST);

	//Let TC start counting.
	SN_CT32B0->TMRCTRL |= mskCT32_CEN_EN;
	
	//Enable CT32B0 NVIC interrupt
	NVIC_ClearPendingIRQ(CT32B0_IRQn);
	CT32B0_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 == SN32F108)
	//set P0.8~P0.11 to input pull-up
	SN_GPIO0->CFG = 0xAA00AAAA;
	//set P1.2~P1.9 to input pull-up
	SN_GPIO1->CFG = 0xAAA0000A;
	//set P2.9~P2.15 to input pull-up
	SN_GPIO2->CFG = 0x0002AAAA;
#elif (PKG == SN32F107)
	//set P0.8~P0.11 to input pull-up
	SN_GPIO0->CFG = 0xAA00AAAA;
	//set P1.2~P1.9 to input pull-up
	SN_GPIO1->CFG = 0xAAA0000A;
	//set P2.2~P2.15 to input pull-up
	SN_GPIO2->CFG = 0x0000000A;
	//set P3.0~P3.1, P3.10~P3.13 to input pull-up
	SN_GPIO3->CFG = 0xA00AAAA0;
#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();
}

