| MINI55_BSP V3.02.004 The Board Support Package for Mini55 Series MCU

clk.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "Mini55Series.h"
30 void CLK_DisableCKO(void)
31{
32 /* Disable CKO clock source */
33 CLK->APBCLK &= (~CLK_APBCLK_CLKOCKEN_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->CLKOCTL = CLK_CLKOCTL_CLKOEN_Msk | u32ClkDiv | u32ClkDivBy1En<<CLK_CLKOCTL_DIV1EN_Pos;
57
58 /* Enable CKO clock source */
59 CLK->APBCLK |= CLK_APBCLK_CLKOCKEN_Msk;
60
61 /* Select CKO clock source */
62 CLK->CLKSEL2 = (CLK->CLKSEL2 & (~CLK_CLKSEL2_FDIVSEL_Msk)) | u32ClkSrc;
63}
64
69 void CLK_PowerDown(void)
70{
71 SCB->SCR = SCB_SCR_SLEEPDEEP_Msk;
72 CLK->PWRCTL |= (CLK_PWRCTL_PDEN_Msk | CLK_PWRCTL_PDWKIF_Msk);
73 __WFI();
74}
75
80 void CLK_Idle(void)
81{
82 CLK->PWRCTL &= ~CLK_PWRCTL_PDEN_Msk;
83 __WFI();
84}
85
90uint32_t CLK_GetHXTFreq(void)
91{
92 if(CLK->PWRCTL & CLK_PWRCTL_XTL12M )
93 return __XTAL12M;
94 else
95 return 0;
96}
97
102uint32_t CLK_GetLXTFreq(void)
103{
104 if(CLK->PWRCTL & CLK_PWRCTL_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_HCLKSEL_Msk) | u32ClkSrc;
144 CLK->CLKDIV = (CLK->CLKDIV & ~CLK_CLKDIV_HCLKDIV_Msk) | u32ClkDiv;
145}
146
187 void CLK_SetModuleClock(uint32_t u32ModuleIdx, uint32_t u32ClkSrc, uint32_t u32ClkDiv)
188{
189 uint32_t u32tmp=0,u32sel=0,u32div=0;
190
191 if(MODULE_CLKSEL_Msk(u32ModuleIdx)!=MODULE_NoMsk)
192 {
193 u32sel = (uint32_t)&CLK->CLKSEL0+((MODULE_CLKSEL(u32ModuleIdx))*4);
194 u32tmp = *(volatile uint32_t *)(u32sel);
195 u32tmp = ( u32tmp & ~(MODULE_CLKSEL_Msk(u32ModuleIdx)<<MODULE_CLKSEL_Pos(u32ModuleIdx)) ) | u32ClkSrc;
196 *(volatile uint32_t *)(u32sel) = u32tmp;
197 }
198
199 if(MODULE_CLKDIV_Msk(u32ModuleIdx)!=MODULE_NoMsk)
200 {
201 u32div =(uint32_t)&CLK->CLKDIV+((MODULE_CLKDIV(u32ModuleIdx))*4);
202 u32tmp = *(volatile uint32_t *)(u32div);
203 u32tmp = ( u32tmp & ~(MODULE_CLKDIV_Msk(u32ModuleIdx)<<MODULE_CLKDIV_Pos(u32ModuleIdx)) ) | u32ClkDiv;
204 *(volatile uint32_t *)(u32div) = u32tmp;
205 }
206}
207
217 void CLK_SetSysTickClockSrc(uint32_t u32ClkSrc)
218{
219 CLK->CLKSEL0 = (CLK->CLKSEL0 & ~CLK_CLKSEL0_STCLKSEL_Msk) | u32ClkSrc;
220}
221
230 void CLK_EnableXtalRC(uint32_t u32ClkMask)
231{
232 if(u32ClkMask & CLK_PWRCTL_XTLEN_Msk)
233 CLK->PWRCTL &=~CLK_PWRCTL_XTLEN_Msk;
234 CLK->PWRCTL |=u32ClkMask;
235}
236
245 void CLK_DisableXtalRC(uint32_t u32ClkMask)
246{
247 CLK->PWRCTL &=~u32ClkMask;
248}
249
268 void CLK_EnableModuleClock(uint32_t u32ModuleIdx)
269{
270 *(volatile uint32_t *)((uint32_t)&CLK->APBCLK+(MODULE_APBCLK(u32ModuleIdx)*4)) |= 1<<MODULE_IP_EN_Pos(u32ModuleIdx);
271}
272
291 void CLK_DisableModuleClock(uint32_t u32ModuleIdx)
292{
293 *(volatile uint32_t *)((uint32_t)&CLK->APBCLK+(MODULE_APBCLK(u32ModuleIdx)*4)) &= ~(1<<MODULE_IP_EN_Pos(u32ModuleIdx));
294}
295
309uint32_t CLK_WaitClockReady(uint32_t u32ClkMask)
310{
311 int32_t i32TimeOutCnt;
312
313 i32TimeOutCnt = __HSI / 200; /* About 5ms */
314
315 while((CLK->STATUS & u32ClkMask) != u32ClkMask)
316 {
317 if(i32TimeOutCnt-- <= 0)
318 return 0;
319 }
320 return 1;
321}
322
336 void CLK_EnableSysTick(uint32_t u32ClkSrc, uint32_t u32Count)
337{
338 /* Set System Tick counter disabled */
339 SysTick->CTRL = 0;
340
341 /* Set System Tick clock source */
342 if( u32ClkSrc == CLK_CLKSEL0_STCLKSEL_HCLK )
343 SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk;
344 else
345 CLK->CLKSEL0 = (CLK->CLKSEL0 & ~CLK_CLKSEL0_STCLKSEL_Msk) | u32ClkSrc;
346
347 /* Set System Tick reload value */
348 SysTick->LOAD = u32Count;
349
350 /* Clear System Tick current value and counter flag */
351 SysTick->VAL = 0;
352
353 /* Set System Tick interrupt enabled and counter enabled */
354 SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
355}
356
363 void CLK_DisableSysTick(void)
364{
365 /* Set System Tick counter disabled */
366 SysTick->CTRL = 0;
367}
368 /* end of group MINI55_CLK_EXPORTED_FUNCTIONS */
370 /* end of group MINI55_CLK_Driver */
372 /* end of group MINI55_Device_Driver */
374
375/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
Mini55Series.h
Mini55 series peripheral access layer header file. This file contains all the peripheral register's d...
CLK_CLKSEL0_STCLKSEL_HCLK
#define CLK_CLKSEL0_STCLKSEL_HCLK
Definition: clk.h:53
MODULE_NoMsk
#define MODULE_NoMsk
Definition: clk.h:114
MODULE_CLKSEL_Msk
#define MODULE_CLKSEL_Msk(x)
Definition: clk.h:108
MODULE_CLKSEL_Pos
#define MODULE_CLKSEL_Pos(x)
Definition: clk.h:109
CLK_PWRCTL_LXT
#define CLK_PWRCTL_LXT
Definition: clk.h:41
MODULE_CLKDIV_Pos
#define MODULE_CLKDIV_Pos(x)
Definition: clk.h:112
MODULE_IP_EN_Pos
#define MODULE_IP_EN_Pos(x)
Definition: clk.h:113
MODULE_CLKDIV_Msk
#define MODULE_CLKDIV_Msk(x)
Definition: clk.h:111
MODULE_CLKDIV
#define MODULE_CLKDIV(x)
Definition: clk.h:110
MODULE_APBCLK
#define MODULE_APBCLK(x)
Definition: clk.h:106
CLK_PWRCTL_XTL12M
#define CLK_PWRCTL_XTL12M
Definition: clk.h:38
MODULE_CLKSEL
#define MODULE_CLKSEL(x)
Definition: clk.h:107
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:217
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:268
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:291
CLK_WaitClockReady
uint32_t CLK_WaitClockReady(uint32_t u32ClkMask)
This function check selected clock source status.
Definition: clk.c:309
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:245
CLK_DisableSysTick
void CLK_DisableSysTick(void)
Disable System Tick counter.
Definition: clk.c:363
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:187
CLK_EnableXtalRC
void CLK_EnableXtalRC(uint32_t u32ClkMask)
This function enable clock source.
Definition: clk.c:230
CLK_EnableSysTick
void CLK_EnableSysTick(uint32_t u32ClkSrc, uint32_t u32Count)
Enable System Tick counter.
Definition: clk.c:336
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_PWRCTL_PDEN_Msk
#define CLK_PWRCTL_PDEN_Msk
Definition: Mini55Series.h:1148
CLK_CLKOCTL_CLKOEN_Msk
#define CLK_CLKOCTL_CLKOEN_Msk
Definition: Mini55Series.h:1259
CLK_APBCLK_CLKOCKEN_Msk
#define CLK_APBCLK_CLKOCKEN_Msk
Definition: Mini55Series.h:1172
CLK_PWRCTL_XTLEN_Msk
#define CLK_PWRCTL_XTLEN_Msk
Definition: Mini55Series.h:1130
CLK_CLKOCTL_DIV1EN_Pos
#define CLK_CLKOCTL_DIV1EN_Pos
Definition: Mini55Series.h:1261
CLK_PWRCTL_PDWKIF_Msk
#define CLK_PWRCTL_PDWKIF_Msk
Definition: Mini55Series.h:1145
CLK
#define CLK
Pointer to CLK register structure.
Definition: Mini55Series.h:7010
__HSI
uint32_t __HSI
Definition: system_Mini55Series.c:21
__XTAL32K
#define __XTAL32K
Definition: system_Mini55Series.h:30
__XTAL12M
#define __XTAL12M
Definition: system_Mini55Series.h:29
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Mini55Series.c:22
SystemCoreClockUpdate
void SystemCoreClockUpdate(void)
Updates the SystemCoreClock with current core Clock retrieved from CPU registers.
Definition: system_Mini55Series.c:59