/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdint.h>
#include  "N572.h"

/*----------------------------------------------------------------------------
  Clock Variable definitions
 *----------------------------------------------------------------------------*/
#if( __CHIP_SERIES__ == __N572F065_SERIES__ )
uint32_t SystemCoreClock = (__HSI); /*!< System Clock Frequency (Core Clock)*/
#endif
#if( __CHIP_SERIES__ == __N572F072_SERIES__ )
uint32_t SystemCoreClock = (__HSI/4); /*!< System Clock Frequency (Core Clock)*/
#endif
uint32_t CyclesPerUs      = (__HSI / 1000000);  /*!< Cycles per micro second            */

/*----------------------------------------------------------------------------
  Clock functions
 *----------------------------------------------------------------------------*/
#if( __CHIP_SERIES__ == __N572F065_SERIES__ )
void SystemCoreClockUpdate (void)            /* Get Core Clock Frequency      */
{
  uint32_t u32ClkSrc;
  uint32_t u32HclkDiv;
  
  switch (CLK->CLKSEL0 & CLK_CLKSEL0_HCLKSEL_Msk)
  {
  	case 0x000:
  		 u32ClkSrc = __XTL_32K;
  		 break;
  	case 0x001:
  		 u32ClkSrc = __XTL_12M_6M;
  		 break;
  	case 0x002:
  		 u32ClkSrc = __PLL1_FOUT;
  		 break;
  	case 0x004:
  		 u32ClkSrc = CLK_GetPLL2ClockFreq();
  		 break;
  	case 0x007:
  		 u32ClkSrc = __RC_24M;
  		 break;
  	default:
  		 u32ClkSrc = __HSI;
  		 break;
  }
  
    u32HclkDiv = (CLK->CLKDIV & CLK_CLKDIV_HCLKDIV_Msk) + 1;

    /* Update System Core Clock */
    SystemCoreClock = u32ClkSrc/u32HclkDiv;
    
    CyclesPerUs = (SystemCoreClock) / 1000000;
}
#endif
#if( __CHIP_SERIES__ == __N572F072_SERIES__ )
void SystemCoreClockUpdate (void)            /* Get Core Clock Frequency      */
{
  uint32_t u32ClkSrc;
  uint32_t u32HclkDiv;
  
  switch (CLK->CLKSEL0 & CLK_CLKSEL0_HCLKSEL_Msk)
  {
  	case 0x000:
  		 u32ClkSrc = __XTL_32K;
  		 break;
  	case 0x002:
  		 u32ClkSrc = __PLL1_FOUT;
  		 break;
  	case 0x004:
  		 u32ClkSrc = CLK_GetPLL2ClockFreq();
  		 break;
  	case 0x007:
  		 u32ClkSrc = __RC_48M;
  		 break;
  	default:
  		 u32ClkSrc = __HSI;
  		 break;
  }
  
    u32HclkDiv = (CLK->CLKDIV & CLK_CLKDIV_HCLKDIV_Msk) + 1;

    /* Update System Core Clock */
    SystemCoreClock = u32ClkSrc/u32HclkDiv;
    
    CyclesPerUs = (SystemCoreClock) / 1000000;
}
#endif
