| Nano100BN Series BSP V3.03.002 The Board Support Package for Nano100BN 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)
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) ? 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
106 void SystemCoreClockUpdate (void)
107 {
108
109 SystemCoreClock = SysGet_HCLKFreq();
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. ***/
CLK_CLKSEL0_HCLK_S_PLL
#define CLK_CLKSEL0_HCLK_S_PLL
Definition: clk.h:103
CLK
#define CLK
Pointer to CLK register structure.
Definition: Nano100Series.h:11693
__HSI
#define __HSI
Definition: system_Nano100Series.h:31
Nano100Series.h
Nano100 series peripheral access layer header file. This file contains all the peripheral register's ...
CLK_CLKSEL0_HCLK_S_LXT
#define CLK_CLKSEL0_HCLK_S_LXT
Definition: clk.h:102
CLK_CLKDIV0_HCLK_N_Msk
#define CLK_CLKDIV0_HCLK_N_Msk
Definition: Nano100Series.h:1414
SystemCoreClockUpdate
void SystemCoreClockUpdate(void)
This function is used to update the variable SystemCoreClock and must be called whenever the core clo...
Definition: system_Nano100Series.c:106
CyclesPerUs
uint32_t CyclesPerUs
Definition: system_Nano100Series.c:21
__HIRC12M
#define __HIRC12M
Definition: system_Nano100Series.h:28
CLK_CLKSEL0_HCLK_S_Msk
#define CLK_CLKSEL0_HCLK_S_Msk
Definition: Nano100Series.h:1360
CLK_PLLCTL_IN_DV_Msk
#define CLK_PLLCTL_IN_DV_Msk
Definition: Nano100Series.h:1441
CLK_CLKSEL0_HCLK_S_HXT
#define CLK_CLKSEL0_HCLK_S_HXT
Definition: clk.h:101
__HXT
#define __HXT
Definition: system_Nano100Series.h:26
CLK_PLLCTL_OUT_DV
#define CLK_PLLCTL_OUT_DV
Definition: clk.h:204
CLK_PLLCTL_FB_DV_Msk
#define CLK_PLLCTL_FB_DV_Msk
Definition: Nano100Series.h:1438
__LXT
#define __LXT
Definition: system_Nano100Series.h:27
SysGet_PLLClockFreq
uint32_t SysGet_PLLClockFreq(void)
Calculate current PLL clock frequency.
Definition: system_Nano100Series.c:28
CLK_PLLCTL_PLL_SRC_Msk
#define CLK_PLLCTL_PLL_SRC_Msk
Definition: Nano100Series.h:1450
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Nano100Series.c:20
SysGet_HCLKFreq
uint32_t SysGet_HCLKFreq(void)
Get current HCLK clock frequency.
Definition: system_Nano100Series.c:66
CLK_CLKSEL0_HCLK_S_LIRC
#define CLK_CLKSEL0_HCLK_S_LIRC
Definition: clk.h:104
CLK_PLLCTL_PD
#define CLK_PLLCTL_PD
Definition: clk.h:205
__LIRC
#define __LIRC
Definition: system_Nano100Series.h:29