| MINI51DE_BSP V3.02.004 The Board Support Package for Mini51DE Series MCU

clk.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "Mini51Series.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);
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
233 void CLK_EnableSysTick(uint32_t u32ClkSrc, uint32_t u32Count)
234{
235 SysTick->CTRL=0;
236 if( u32ClkSrc== CLK_CLKSEL0_STCLK_S_HCLK ) /* Set System Tick clock source */
237 SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk;
238 else
239 {
240 SysTick->CTRL &= ~SysTick_CTRL_CLKSOURCE_Msk;
241 CLK->CLKSEL0 = (CLK->CLKSEL0 & ~CLK_CLKSEL0_STCLK_S_Msk) | u32ClkSrc;
242 }
243 SysTick->LOAD = u32Count; /* Set System Tick reload value */
244 SysTick->VAL = 0; /* Clear System Tick current value and counter flag */
245 SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; /* Set System Tick counter enabled */
246}
247
253 void CLK_DisableSysTick(void)
254{
255 SysTick->CTRL = 0; /* Set System Tick counter disabled */
256}
257
266 void CLK_EnableXtalRC(uint32_t u32ClkMask)
267{
268 CLK->PWRCON |=u32ClkMask;
269}
270
279 void CLK_DisableXtalRC(uint32_t u32ClkMask)
280{
281 CLK->PWRCON &=~u32ClkMask;
282}
283
301 void CLK_EnableModuleClock(uint32_t u32ModuleIdx)
302{
303 *(volatile uint32_t *)((uint32_t)&CLK->APBCLK+(MODULE_APBCLK(u32ModuleIdx)*4)) |= 1<<MODULE_IP_EN_Pos(u32ModuleIdx);
304}
305
323 void CLK_DisableModuleClock(uint32_t u32ModuleIdx)
324{
325 *(volatile uint32_t *)((uint32_t)&CLK->APBCLK+(MODULE_APBCLK(u32ModuleIdx)*4)) &= ~(1<<MODULE_IP_EN_Pos(u32ModuleIdx));
326}
327
336 void CLK_SysTickDelay(uint32_t us)
337{
338 SysTick->LOAD = us * CyclesPerUs;
339 SysTick->VAL = (0x00);
340 SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
341
342 /* Waiting for down-count to zero */
343 while((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0);
344 SysTick->CTRL = 0;
345}
346
360uint32_t CLK_WaitClockReady(uint32_t u32ClkMask)
361{
362 int32_t i32TimeOutCnt;
363
364 i32TimeOutCnt = __HSI / 200; /* About 5ms */
365
366 while((CLK->CLKSTATUS & u32ClkMask) != u32ClkMask)
367 {
368 if(i32TimeOutCnt-- <= 0)
369 return 0;
370 }
371 return 1;
372}
373
374 /* end of group MINI51_CLK_EXPORTED_FUNCTIONS */
376 /* end of group MINI51_CLK_Driver */
378 /* end of group MINI51_Device_Driver */
380
381/*** (C) COPYRIGHT 2013~2015 Nuvoton Technology Corp. ***/
Mini51Series.h
Mini51 series peripheral access layer header file. This file contains all the peripheral register's d...
MODULE_NoMsk
#define MODULE_NoMsk
Definition: clk.h:121
MODULE_CLKSEL_Msk
#define MODULE_CLKSEL_Msk(x)
Definition: clk.h:115
CLK_CLKSEL0_STCLK_S_HCLK
#define CLK_CLKSEL0_STCLK_S_HCLK
Definition: clk.h:56
MODULE_CLKSEL_Pos
#define MODULE_CLKSEL_Pos(x)
Definition: clk.h:116
MODULE_CLKDIV_Pos
#define MODULE_CLKDIV_Pos(x)
Definition: clk.h:119
MODULE_IP_EN_Pos
#define MODULE_IP_EN_Pos(x)
Definition: clk.h:120
MODULE_CLKDIV_Msk
#define MODULE_CLKDIV_Msk(x)
Definition: clk.h:118
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:117
MODULE_APBCLK
#define MODULE_APBCLK(x)
Definition: clk.h:113
MODULE_CLKSEL
#define MODULE_CLKSEL(x)
Definition: clk.h:114
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:301
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:323
CLK_SysTickDelay
void CLK_SysTickDelay(uint32_t us)
This function execute delay function.
Definition: clk.c:336
CLK_WaitClockReady
uint32_t CLK_WaitClockReady(uint32_t u32ClkMask)
This function check selected clock source status.
Definition: clk.c:360
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_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:279
CLK_DisableSysTick
void CLK_DisableSysTick(void)
Disable System Tick counter.
Definition: clk.c:253
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:266
CLK_EnableSysTick
void CLK_EnableSysTick(uint32_t u32ClkSrc, uint32_t u32Count)
Enable System Tick counter.
Definition: clk.c:233
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_PWRCON_PD_WU_STS_Msk
#define CLK_PWRCON_PD_WU_STS_Msk
Definition: Mini51Series.h:890
CLK_FRQDIV_DIVIDER1_Pos
#define CLK_FRQDIV_DIVIDER1_Pos
Definition: Mini51Series.h:1003
CLK_PWRCON_PWR_DOWN_EN_Msk
#define CLK_PWRCON_PWR_DOWN_EN_Msk
Definition: Mini51Series.h:893
CLK_APBCLK_FDIV_EN_Msk
#define CLK_APBCLK_FDIV_EN_Msk
Definition: Mini51Series.h:911
CLK_FRQDIV_DIVIDER_EN_Msk
#define CLK_FRQDIV_DIVIDER_EN_Msk
Definition: Mini51Series.h:1001
CLK
#define CLK
Pointer to CLK register structure.
Definition: Mini51Series.h:5736
__XTAL32K
#define __XTAL32K
Definition: system_Mini51Series.h:30
CyclesPerUs
uint32_t CyclesPerUs
Definition: system_Mini51Series.c:21
__XTAL12M
#define __XTAL12M
Definition: system_Mini51Series.h:29
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Mini51Series.c:20
__HSI
#define __HSI
Definition: system_Mini51Series.h:34
SystemCoreClockUpdate
void SystemCoreClockUpdate(void)
Updates the SystemCoreClock with current core Clock retrieved from CPU registers.
Definition: system_Mini51Series.c:31