M4521_BSP V3.02.000
The Board Support Package for M4521
system_M4521.c
Go to the documentation of this file.
1/**************************************************************************/
13#include "M4521.h"
14
15
16/*----------------------------------------------------------------------------
17 DEFINES
18 *----------------------------------------------------------------------------*/
19
20
21/*----------------------------------------------------------------------------
22 Clock Variable definitions
23 *----------------------------------------------------------------------------*/
25uint32_t CyclesPerUs = (__HSI / 1000000); /* Cycles per micro second */
26uint32_t PllClock = __HSI;
27uint32_t gau32ClkSrcTbl[] = {__HXT, __LXT, 0, __LIRC, 0, 0, 0, __HIRC};
28
29/*----------------------------------------------------------------------------
30 Clock functions
31 *----------------------------------------------------------------------------*/
32void SystemCoreClockUpdate(void) /* Get Core Clock Frequency */
33{
34#if 1
35 uint32_t u32Freq, u32ClkSrc;
36 uint32_t u32HclkDiv;
37
38 /* Update PLL Clock */
40
41 u32ClkSrc = CLK->CLKSEL0 & CLK_CLKSEL0_HCLKSEL_Msk;
42
43 if(u32ClkSrc == CLK_CLKSEL0_HCLKSEL_PLL)
44 {
45 /* Use PLL clock */
46 u32Freq = PllClock;
47 }
48 else
49 {
50 /* Use the clock sources directly */
51 u32Freq = gau32ClkSrcTbl[u32ClkSrc];
52 }
53
54 u32HclkDiv = (CLK->CLKDIV0 & CLK_CLKDIV0_HCLKDIV_Msk) + 1;
55
56 /* Update System Core Clock */
57 SystemCoreClock = u32Freq / u32HclkDiv;
58
59
60 //if(SystemCoreClock == 0)
61 // __BKPT(0);
62
63 CyclesPerUs = (SystemCoreClock + 500000) / 1000000;
64#endif
65}
66
76void SystemInit(void)
77{
78 /* ToDo: add code to initialize the system
79 do not use global variables because this function is called before
80 reaching pre-main. RW section maybe overwritten afterwards. */
81
83 /* One-time POR18 */
84 if((SYS->PDID >> 12) == 0x945)
85 {
86 M32(GCR_BASE+0x14) |= BIT7;
87 }
88 /* Force to use INV type with HXT */
89 CLK->PWRCTL &= ~CLK_PWRCTL_HXTSELTYP_Msk;
91
92
93#ifdef EBI_INIT
94 extern void SYS_Init();
95 extern void EBI_Init();
96
98 SYS_Init();
99 EBI_Init();
100 SYS_LockReg();
101#endif
102
103 /* FPU settings ------------------------------------------------------------*/
104#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
105 SCB->CPACR |= ((3UL << 10 * 2) | /* set CP10 Full Access */
106 (3UL << 11 * 2)); /* set CP11 Full Access */
107#endif
108
109}
110
111/*** (C) COPYRIGHT 2022 Nuvoton Technology Corp. ***/
#define CLK_CLKSEL0_HCLKSEL_PLL
Definition: clk.h:47
__STATIC_INLINE uint32_t CLK_GetPLLClockFreq(void)
Get PLL clock frequency.
Definition: clk.h:332
#define CLK_CLKDIV0_HCLKDIV_Msk
Definition: M4521.h:1623
#define CLK_CLKSEL0_HCLKSEL_Msk
Definition: M4521.h:1566
#define GCR_BASE
Definition: M4521.h:13770
#define CLK
Definition: M4521.h:13822
#define BIT7
Definition: M4521.h:13867
#define M32(adr)
Definition: M4521.h:13941
#define SYS
Definition: M4521.h:13820
__STATIC_INLINE void SYS_LockReg(void)
Enable register write-protection function.
Definition: sys.h:702
__STATIC_INLINE void SYS_UnlockReg(void)
Disable register write-protection function.
Definition: sys.h:684
uint32_t gau32ClkSrcTbl[]
Definition: system_M4521.c:27
uint32_t CyclesPerUs
Definition: system_M4521.c:25
void SystemInit(void)
Setup the microcontroller system. Initialize the System.
Definition: system_M4521.c:76
uint32_t PllClock
Definition: system_M4521.c:26
uint32_t SystemCoreClock
Definition: system_M4521.c:24
void SystemCoreClockUpdate(void)
Updates the SystemCoreClock with current core Clock retrieved from cpu registers.
Definition: system_M4521.c:32
#define __HIRC
Definition: system_M4521.h:37
#define __SYSTEM_CLOCK
Definition: system_M4521.h:42
#define __HXT
Definition: system_M4521.h:35
#define __LIRC
Definition: system_M4521.h:38
#define __LXT
Definition: system_M4521.h:36
#define __HSI
Definition: system_M4521.h:34