/******************************************************************************
 * @file     main.c
 * @version  V3.00
 * $Revision: 2 $
 * $Date: 14/07/31 10:14a $
 * @brief    This sample code demo semihost function
 *
 * @note
 * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "N572.h"

void SYS_Init(void)
{

	/* Unlock protected registers */
    SYS_UnlockReg();
	
	/* Procedure to configure the wait state of SDRAM */
	SYS_SET_RAM_WAIT_STATE(SYS_IPRST0_RAMWS_ZERO_WAIT);
	/* Set flash access with 1 wait state, if system clock is greater than 24MHz*/
	SYS_SET_CPU_WAIT_STATE(SYS_IPRST0_CPUWS_1HCLK_WAIT);
	/* Enable external 32K oscillator */
	CLK_EnableXtalRC(CLK_PWRCTL_XTL32K_EN_Msk);
	/* Enable PLL1 & PLL2 */
	CLK_SetPLL1Mode(PLL_NORMAL_MODE);
	CLK_SetPLL2Mode(PLL_NORMAL_MODE);
#if defined(__N572F072__) 
	/* Select PLL1 as PLL2 input source, and PLL2 output 48MHz*/
	CLK_SetPLL2(PLL2_SRC_PLL1_FOUT, PLL2_48M);
	CLK_EnableLDO30();	
#endif
	
	/* Switch HCLK clock source to PLL2, system clock output 48MHz */
    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL2_FOUT,CLK_CLKDIV_HCLK(1));

    /* Lock protected registers */
    SYS_LockReg();
}

int main(void)
{
    int8_t item;
			
	/* Lock protected registers */
    if(SYS->REGLCTL == 1) // In end of main function, program issued CPU reset and write-protection will be disabled.
        SYS_LockReg();
			
    /* Init System, IP clock and multi-function I/O */
    SYS_Init(); //In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register.

	printf("\n Start SEMIHOST test: \n");

    while(1)
	{
        item = getchar();
        printf("%c\n",item);
    }
}

/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
