1 /**************************************************************************//**
2 * @file system_M2L31.c
3 * @version V0.10
4 * @brief System Setting Source File
5 *
6 * @note
7 * SPDX-License-Identifier: Apache-2.0
8 * @copyright (C) 2023 Nuvoton Technology Corp. All rights reserved.
9 ****************************************************************************/
10
11 //#include <arm_cmse.h>
12 #include <stdio.h>
13 #include <stdint.h>
14 #include "NuMicro.h"
15
16 extern void *__Vectors; /* see startup file */
17
18 /*----------------------------------------------------------------------------
19 Clock Variable definitions
20 *----------------------------------------------------------------------------*/
21 uint32_t SystemCoreClock = __HSI; /*!< System Clock Frequency (Core Clock) */
22 uint32_t CyclesPerUs = (__HSI / 1000000); /*!< Cycles per micro second */
23 uint32_t PllClock = __HSI; /*!< PLL Output Clock Frequency */
24 const uint32_t gau32ClkSrcTbl[8] = {__HXT, __LXT, 0UL, __LIRC, 0UL, __MIRC, __HIRC48, __HIRC};
25
26
27 /**
28 * @brief Update the Variable SystemCoreClock
29 *
30 * @param None
31 *
32 * @return None
33 *
34 * @details This function is used to update the variable SystemCoreClock
35 * and must be called whenever the core clock is changed.
36 */
SystemCoreClockUpdate(void)37 void SystemCoreClockUpdate(void)
38 {
39 uint32_t u32Freq, u32ClkSrc;
40 uint32_t u32HclkDiv;
41
42 u32ClkSrc = CLK->CLKSEL0 & CLK_CLKSEL0_HCLK0SEL_Msk;
43
44 /* Update PLL Clock */
45 PllClock = CLK_GetPLLClockFreq();
46
47 if (u32ClkSrc != CLK_CLKSEL0_HCLKSEL_PLL)
48 {
49 /* Use the clock sources directly */
50 u32Freq = gau32ClkSrcTbl[u32ClkSrc];
51 }
52 else
53 {
54 /* Use PLL clock */
55 u32Freq = PllClock;
56 }
57
58 u32HclkDiv = (CLK->CLKDIV0 & CLK_CLKDIV0_HCLK0DIV_Msk) + 1;
59
60 /* Update System Core Clock */
61 SystemCoreClock = u32Freq / u32HclkDiv;
62
63 CyclesPerUs = (SystemCoreClock + 500000) / 1000000;
64
65 if (CyclesPerUs ==0)
66 CyclesPerUs = 1; // avoid the SYSTICK cannot count to value
67 }
68
69
70 /**
71 * @brief System Initialization
72 *
73 * @param None
74 *
75 * @return None
76 *
77 * @details The necessary initialization of system. Global variables are forbidden here.
78 */
SystemInit(void)79 void SystemInit(void)
80 {
81
82 }
83
84
85 #if USE_ASSERT
86
87 /**
88 * @brief Assert Error Message
89 *
90 * @param[in] file the source file name
91 * @param[in] line line number
92 *
93 * @return None
94 *
95 * @details The function prints the source file name and line number where
96 * the ASSERT_PARAM() error occurs, and then stops in an infinite loop.
97 */
AssertError(uint8_t * file,uint32_t line)98 void AssertError(uint8_t *file, uint32_t line)
99 {
100 printf("[%s] line %u : wrong parameters.\r\n", file, line);
101
102 /* Infinite loop */
103 while (1) ;
104 }
105 #endif
106
107 /**
108 * @brief Set UART0 Default MPF
109 *
110 * @param None
111 *
112 * @return None
113 *
114 * @details The initialization of uart0 default multi function pin.
115 */
116 #if 1
117 #if defined( __ICCARM__ )
118 __WEAK
119 #else
120 __attribute__((weak))
121 #endif
Uart0DefaultMPF(void)122 void Uart0DefaultMPF(void)
123 {
124 /* Set GPB multi-function pins for UART0 RXD and TXD */
125 SYS->GPB_MFP3 = (SYS->GPB_MFP3 & ~SYS_GPB_MFP3_PB12MFP_Msk) | SYS_GPB_MFP3_PB12MFP_UART0_RXD;
126 SYS->GPB_MFP3 = (SYS->GPB_MFP3 & ~SYS_GPB_MFP3_PB13MFP_Msk) | SYS_GPB_MFP3_PB13MFP_UART0_TXD;
127 }
128 #endif
129