/**************************************************************************//**
 * @file     main.c
 * @version  V1.00
 * $Revision: 1 $
 * $Date: 14/08/01 10:00p $
 * @brief    Access SPI flash through SPI interface.
 *
 * @note
 * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
 *
 ******************************************************************************/
#include <stdio.h>
#include <string.h>
#include "N572.h"
#include "massstorage.h"


#define CONFIG_BASE      0x00300000
#define DATA_FLASH_BASE  0x00008000

void SYS_Init(void)
{
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/
    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Enable External XTL32K */
    CLK_EnableXtalRC(CLK_PWRCTL_XTL32K_EN_Msk);
	
	/* Set RAM wait state */
	SYS_SET_RAM_WAIT_STATE(SYS_IPRST0_RAMWS_ZERO_WAIT);
	
    /* Set flash memory wait state */
	SYS_SET_CPU_WAIT_STATE(SYS_IPRST0_CPUWS_1HCLK_WAIT);
	
	/* Enable PLL1 & PLL2 */
	CLK_SetPLL1Mode(PLL_NORMAL_MODE);
	CLK_SetPLL2Mode(PLL_NORMAL_MODE);
	
	/* Set PLL2 clock source & frequency */
	CLK_SetPLL2( PLL2_SRC_PLL1_FOUT, NULL );
	
	/* Procedures to configure the HCLK from PLL2 */
	CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL2_FOUT,CLK_CLKDIV_HCLK(1));

    /* Enable IP clock */
    CLK_EnableModuleClock(USBD_MODULE);

    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
    SystemCoreClockUpdate();

	/* Reset IP module */
	SYS_ResetModule(USBD_RST);
	
    /* Lock protected registers */
    SYS_LockReg();
}

int main()
{
    /* Init System, IP clock and multi-function I/O */
    SYS_Init();

    printf("\n+------------------------------------------------------------------------+\n");
    printf("|              N572F065 USBD Sample with MassStorage                     |\n");
    printf("+------------------------------------------------------------------------+\n");
	
    SYS_UnlockReg();
    /* Enable FMC ISP function */
    FMC_Open();

    printf("NnMicro USB MassStorage Start!\n");
	
    USBD_Open(&gsInfo, MSC_ClassRequest, NULL);

    /* Endpoint configuration */
    MSC_Init();
    USBD_Start();
    NVIC_EnableIRQ(USBD_IRQn);

    while(1)
    {
        MSC_ProcessCmd();
    }
}
