Nano100AN Series BSP  V3.02.002
The Board Support Package for Nano100AN Series
system_Nano100Series.c
Go to the documentation of this file.
1 /**************************************************************************/
13 #include <stdint.h>
14 #include "Nano100Series.h"
15 
16 
17 /*----------------------------------------------------------------------------
18  Clock Variable definitions
19  *----------------------------------------------------------------------------*/
20 uint32_t SystemCoreClock = __HSI;
21 uint32_t CyclesPerUs = (__HSI / 1000000);
28 uint32_t SysGet_PLLClockFreq(void)
29 {
30  uint32_t u32Freq =0, u32PLLSrc;
31  uint32_t u32NO, u32NR, u32IN_DV, u32PllReg;
32 
33  u32PllReg = CLK->PLLCTL;
34 
35  if (u32PllReg & CLK_PLLCTL_PD_Msk)
36  return 0; /* PLL is in power down mode */
37 
38  if (u32PllReg & CLK_PLLCTL_PLL_SRC_Msk)
39  u32PLLSrc = __HIRC12M;
40  else
41  u32PLLSrc = __HXT;
42 
43  u32NO = (u32PllReg & CLK_PLLCTL_OUT_DV_Msk) ? 2: 1;
44 
45  u32IN_DV = (u32PllReg & CLK_PLLCTL_IN_DV_Msk) >> 8;
46  if (u32IN_DV == 0)
47  u32NR = 2;
48  else if (u32IN_DV == 1)
49  u32NR = 4;
50  else if (u32IN_DV == 2)
51  u32NR = 8;
52  else
53  u32NR = 16;
54 
55  u32Freq = u32PLLSrc * ((u32PllReg & CLK_PLLCTL_FB_DV_Msk) +32) / u32NR / u32NO;
56 
57  return u32Freq;
58 }
59 
60 
66 uint32_t SysGet_HCLKFreq(void)
67 {
68 
69  uint32_t u32Freqout, u32AHBDivider, u32ClkSel;
70 
71  u32ClkSel = CLK->CLKSEL0 & CLK_CLKSEL0_HCLK_S_Msk;
72 
73  if (u32ClkSel == CLK_CLKSEL0_HCLK_S_HXT) /* external HXT crystal clock */
74  {
75  u32Freqout = __HXT;
76  }
77  else if(u32ClkSel == CLK_CLKSEL0_HCLK_S_LXT) /* external LXT crystal clock */
78  {
79  u32Freqout = __LXT;
80  }
81  else if(u32ClkSel == CLK_CLKSEL0_HCLK_S_PLL) /* PLL clock */
82  {
83  u32Freqout = SysGet_PLLClockFreq();
84  }
85  else if(u32ClkSel == CLK_CLKSEL0_HCLK_S_LIRC) /* internal LIRC oscillator clock */
86  {
87  u32Freqout = __LIRC;
88  }
89  else /* internal HIRC oscillator clock */
90  {
91  u32Freqout = __HIRC12M;
92  }
93  u32AHBDivider = (CLK->CLKDIV0 & CLK_CLKDIV0_HCLK_N_Msk) + 1 ;
94 
95  return (u32Freqout/u32AHBDivider);
96 }
97 
98 
107 {
108 
110  CyclesPerUs = (SystemCoreClock + 500000) / 1000000;
111 }
112 
113 #if USE_ASSERT
114 
126 void AssertError(uint8_t * file, uint32_t line)
127 {
128 
129  printf("[%s] line %d : wrong parameters.\r\n", file, line);
130 
131  /* Infinite loop */
132  while(1) ;
133 }
134 #endif
135 
136 /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
#define CLK_CLKSEL0_HCLK_S_PLL
Definition: clk.h:87
#define CLK
Pointer to CLK register structure.
#define __HSI
Nano100 series peripheral access layer header file. This file contains all the peripheral register's ...
#define CLK_CLKSEL0_HCLK_S_LXT
Definition: clk.h:86
#define CLK_PLLCTL_PD_Msk
#define CLK_CLKDIV0_HCLK_N_Msk
void SystemCoreClockUpdate(void)
This function is used to update the variable SystemCoreClock and must be called whenever the core clo...
uint32_t CyclesPerUs
#define __HIRC12M
#define CLK_PLLCTL_OUT_DV_Msk
#define CLK_CLKSEL0_HCLK_S_Msk
#define CLK_PLLCTL_IN_DV_Msk
#define CLK_CLKSEL0_HCLK_S_HXT
Definition: clk.h:85
#define __HXT
#define CLK_PLLCTL_FB_DV_Msk
#define __LXT
uint32_t SysGet_PLLClockFreq(void)
Calculate current PLL clock frequency.
#define CLK_PLLCTL_PLL_SRC_Msk
uint32_t SystemCoreClock
uint32_t SysGet_HCLKFreq(void)
Get current HCLK clock frequency.
#define CLK_CLKSEL0_HCLK_S_LIRC
Definition: clk.h:88
#define __LIRC