| NUC029FAE_BSP V3.02.000 The Board Support Package for NUC029FAE MCU

clk.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "NUC029FAE.h"
30 void CLK_DisableCKO(void)
31{
32 /* Disable CKO clock source */
33 CLK->APBCLK &= (~CLK_APBCLK_FDIV_EN_Msk);
34}
35
53 void CLK_EnableCKO(uint32_t u32ClkSrc, uint32_t u32ClkDiv, uint32_t u32ClkDivBy1En)
54{
55 /* CKO = clock source / 2^(u32ClkDiv + 1) */
56 CLK->FRQDIV = CLK_FRQDIV_DIVIDER_EN_Msk | u32ClkDiv | u32ClkDivBy1En<<CLK_FRQDIV_DIVIDER1_Pos;
57
58 /* Enable CKO clock source */
59 CLK->APBCLK |= CLK_APBCLK_FDIV_EN_Msk;
60
61 /* Select CKO clock source */
62 CLK->CLKSEL2 = (CLK->CLKSEL2 & (~CLK_CLKSEL2_FRQDIV_S_Msk)) | u32ClkSrc;
63}
64
69 void CLK_PowerDown(void)
70{
71 SCB->SCR = SCB_SCR_SLEEPDEEP_Msk;
72 CLK->PWRCON |= (CLK_PWRCON_PWR_DOWN_EN_Msk | CLK_PWRCON_PD_WU_STS_Msk);
73 __WFI();
74}
75
80 void CLK_Idle(void)
81{
82 CLK->PWRCON |= (CLK_PWRCON_PWR_DOWN_EN_Msk | CLK_PWRCON_PD_WU_STS_Msk);
83 __WFI();
84}
85
90uint32_t CLK_GetHXTFreq(void)
91{
92 if(CLK->PWRCON & CLK_PWRCON_XTL12M )
93 return __XTAL12M;
94 else
95 return 0;
96}
97
102uint32_t CLK_GetLXTFreq(void)
103{
104 if(CLK->PWRCON & CLK_PWRCON_LXT )
105 return __XTAL32K;
106 else
107 return 0;
108}
109
114uint32_t CLK_GetHCLKFreq(void)
115{
116 SystemCoreClockUpdate();
117 return SystemCoreClock;
118}
119
120
125uint32_t CLK_GetCPUFreq(void)
126{
127 SystemCoreClockUpdate();
128 return SystemCoreClock;
129}
130
141 void CLK_SetHCLK(uint32_t u32ClkSrc, uint32_t u32ClkDiv)
142{
143 CLK->CLKSEL0 = (CLK->CLKSEL0 & ~CLK_CLKSEL0_HCLK_S_Msk) | u32ClkSrc;
144 CLK->CLKDIV = (CLK->CLKDIV & ~CLK_CLKDIV_HCLK_N_Msk) | u32ClkDiv;
145}
146
185 void CLK_SetModuleClock(uint32_t u32ModuleIdx, uint32_t u32ClkSrc, uint32_t u32ClkDiv)
186{
187 uint32_t u32tmp=0,u32sel=0,u32div=0;
188
189 if(MODULE_CLKSEL_Msk(u32ModuleIdx)!=MODULE_NoMsk)
190 {
191 u32sel = (uint32_t)&CLK->CLKSEL0+((MODULE_CLKSEL(u32ModuleIdx))*4);
192 u32tmp = *(volatile uint32_t *)(u32sel);
193 u32tmp = ( u32tmp & ~(MODULE_CLKSEL_Msk(u32ModuleIdx)<<MODULE_CLKSEL_Pos(u32ModuleIdx)) ) | u32ClkSrc;
194 *(volatile uint32_t *)(u32sel) = u32tmp;
195 }
196
197 if(MODULE_CLKDIV_Msk(u32ModuleIdx)!=MODULE_NoMsk)
198 {
199 u32div =(uint32_t)&CLK->CLKDIV+((MODULE_CLKDIV(u32ModuleIdx))*4);
200 u32tmp = *(volatile uint32_t *)(u32div);
201 u32tmp = ( u32tmp & ~(MODULE_CLKDIV_Msk(u32ModuleIdx)<<MODULE_CLKDIV_Pos(u32ModuleIdx)) ) | u32ClkDiv;
202 *(volatile uint32_t *)(u32div) = u32tmp;
203 }
204}
205
215 void CLK_SetSysTickClockSrc(uint32_t u32ClkSrc)
216{
217 CLK->CLKSEL0 = (CLK->CLKSEL0 & ~CLK_CLKSEL0_STCLK_S_Msk) | u32ClkSrc;
218}
219
228 void CLK_EnableXtalRC(uint32_t u32ClkMask)
229{
230 CLK->PWRCON |=u32ClkMask;
231}
232
241 void CLK_DisableXtalRC(uint32_t u32ClkMask)
242{
243 CLK->PWRCON &=~u32ClkMask;
244}
245
263 void CLK_EnableModuleClock(uint32_t u32ModuleIdx)
264{
265 *(volatile uint32_t *)((uint32_t)&CLK->APBCLK+(MODULE_APBCLK(u32ModuleIdx)*4)) |= 1<<MODULE_IP_EN_Pos(u32ModuleIdx);
266}
267
285 void CLK_DisableModuleClock(uint32_t u32ModuleIdx)
286{
287 *(volatile uint32_t *)((uint32_t)&CLK->APBCLK+(MODULE_APBCLK(u32ModuleIdx)*4)) &= ~(1<<MODULE_IP_EN_Pos(u32ModuleIdx));
288}
289
298int32_t CLK_SysTickDelay(uint32_t us)
299{
300 int32_t tout = SystemCoreClock * ((us / 1000000) + 1) + (SystemCoreClock / 2);
301
302 SysTick->LOAD = us * CyclesPerUs;
303 SysTick->VAL = (0x00);
304 SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
305
306 /* Waiting for down-count to zero */
307 while (((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0) &&
308 (tout-- > 0));
309 if ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0)
310 return -1; /* time out */
311 SysTick->CTRL = 0;
312 return 0;
313}
314
328uint32_t CLK_WaitClockReady(uint32_t u32ClkMask)
329{
330 int32_t i32TimeOutCnt;
331
332 i32TimeOutCnt = __HSI / 200; /* About 5ms */
333
334 while((CLK->CLKSTATUS & u32ClkMask) != u32ClkMask)
335 {
336 if(i32TimeOutCnt-- <= 0)
337 return 0;
338 }
339 return 1;
340}
341
342 /* end of group NUC029FAE_CLK_EXPORTED_FUNCTIONS */
344 /* end of group NUC029FAE_CLK_Driver */
346 /* end of group NUC029FAE_Device_Driver */
348
349/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
NUC029FAE.h
NUC029FAE peripheral access layer header file. This file contains all the peripheral register's defin...
SCB_SCR_SLEEPDEEP_Msk
#define SCB_SCR_SLEEPDEEP_Msk
Definition: core_cm0.h:467
SysTick_CTRL_ENABLE_Msk
#define SysTick_CTRL_ENABLE_Msk
Definition: core_cm0.h:515
SysTick_CTRL_COUNTFLAG_Msk
#define SysTick_CTRL_COUNTFLAG_Msk
Definition: core_cm0.h:506
SysTick_CTRL_CLKSOURCE_Msk
#define SysTick_CTRL_CLKSOURCE_Msk
Definition: core_cm0.h:509
SCB
#define SCB
Definition: core_cm0.h:587
SysTick
#define SysTick
Definition: core_cm0.h:588
MODULE_NoMsk
#define MODULE_NoMsk
Definition: clk.h:120
MODULE_CLKSEL_Msk
#define MODULE_CLKSEL_Msk(x)
Definition: clk.h:114
MODULE_CLKSEL_Pos
#define MODULE_CLKSEL_Pos(x)
Definition: clk.h:115
MODULE_CLKDIV_Pos
#define MODULE_CLKDIV_Pos(x)
Definition: clk.h:118
MODULE_IP_EN_Pos
#define MODULE_IP_EN_Pos(x)
Definition: clk.h:119
MODULE_CLKDIV_Msk
#define MODULE_CLKDIV_Msk(x)
Definition: clk.h:117
CLK_PWRCON_XTL12M
#define CLK_PWRCON_XTL12M
Definition: clk.h:38
CLK_PWRCON_LXT
#define CLK_PWRCON_LXT
Definition: clk.h:41
MODULE_CLKDIV
#define MODULE_CLKDIV(x)
Definition: clk.h:116
MODULE_APBCLK
#define MODULE_APBCLK(x)
Definition: clk.h:112
MODULE_CLKSEL
#define MODULE_CLKSEL(x)
Definition: clk.h:113
CLK_Idle
void CLK_Idle(void)
This function let system enter to Idle mode.
Definition: clk.c:80
CLK_SetSysTickClockSrc
void CLK_SetSysTickClockSrc(uint32_t u32ClkSrc)
This function set SysTick clock source.
Definition: clk.c:215
CLK_GetHCLKFreq
uint32_t CLK_GetHCLKFreq(void)
This function get HCLK frequency. The frequency unit is Hz.
Definition: clk.c:114
CLK_DisableCKO
void CLK_DisableCKO(void)
This function disable frequency output function.
Definition: clk.c:30
CLK_EnableModuleClock
void CLK_EnableModuleClock(uint32_t u32ModuleIdx)
This function enable module clock.
Definition: clk.c:263
CLK_EnableCKO
void CLK_EnableCKO(uint32_t u32ClkSrc, uint32_t u32ClkDiv, uint32_t u32ClkDivBy1En)
This function enable frequency divider module clock, enable frequency divider clock function and conf...
Definition: clk.c:53
CLK_DisableModuleClock
void CLK_DisableModuleClock(uint32_t u32ModuleIdx)
This function disable module clock.
Definition: clk.c:285
CLK_WaitClockReady
uint32_t CLK_WaitClockReady(uint32_t u32ClkMask)
This function check selected clock source status.
Definition: clk.c:328
CLK_GetLXTFreq
uint32_t CLK_GetLXTFreq(void)
This function get external low frequency crystal frequency. The frequency unit is Hz.
Definition: clk.c:102
CLK_PowerDown
void CLK_PowerDown(void)
This function let system enter to Power-down mode.
Definition: clk.c:69
CLK_GetCPUFreq
uint32_t CLK_GetCPUFreq(void)
This function get CPU frequency. The frequency unit is Hz.
Definition: clk.c:125
CLK_SysTickDelay
int32_t CLK_SysTickDelay(uint32_t us)
This function execute delay function.
Definition: clk.c:298
CLK_SetHCLK
void CLK_SetHCLK(uint32_t u32ClkSrc, uint32_t u32ClkDiv)
This function set HCLK clock source and HCLK clock divider.
Definition: clk.c:141
CLK_DisableXtalRC
void CLK_DisableXtalRC(uint32_t u32ClkMask)
This function disable clock source.
Definition: clk.c:241
CLK_SetModuleClock
void CLK_SetModuleClock(uint32_t u32ModuleIdx, uint32_t u32ClkSrc, uint32_t u32ClkDiv)
This function set selected module clock source and module clock divider.
Definition: clk.c:185
CLK_EnableXtalRC
void CLK_EnableXtalRC(uint32_t u32ClkMask)
This function enable clock source.
Definition: clk.c:228
CLK_GetHXTFreq
uint32_t CLK_GetHXTFreq(void)
This function get external high frequency crystal frequency. The frequency unit is Hz.
Definition: clk.c:90
CLK
#define CLK
Pointer to CLK register structure.
Definition: NUC029FAE.h:3228
__XTAL32K
#define __XTAL32K
Definition: system_NUC029FAE.h:30
CyclesPerUs
uint32_t CyclesPerUs
Definition: system_NUC029FAE.c:21
__XTAL12M
#define __XTAL12M
Definition: system_NUC029FAE.h:29
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_NUC029FAE.c:20
__HSI
#define __HSI
Definition: system_NUC029FAE.h:34
SystemCoreClockUpdate
void SystemCoreClockUpdate(void)
Updates the SystemCoreClock with current core Clock retrieved from CPU registers.
Definition: system_NUC029FAE.c:31