/**************************************************************************//** * @file clk.h * @version V3.0 * $Revision: 17 $ * $Date: 14/01/28 1:11p $ * @brief M051 Series Clock Control Driver Header File * * @note * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved. * ******************************************************************************/ #ifndef __CLK_H__ #define __CLK_H__ #include "M051Series.h" /** @addtogroup M051_Device_Driver M051 Device Driver @{ */ /** @addtogroup M051_CLK_Driver CLK Driver @{ */ /** @addtogroup M051_CLK_EXPORTED_FUNCTIONS CLK Exported Constants @{ */ #define FREQ_25MHZ 25000000 #define FREQ_50MHZ 50000000 #define FREQ_100MHZ 100000000 #define FREQ_200MHZ 200000000 /*---------------------------------------------------------------------------------------------------------*/ /* PWRCON constant definitions. */ /*---------------------------------------------------------------------------------------------------------*/ #define CLK_PWRCON_HXT_EN (0x01UL<>31) & 0x1) /*!< Calculate APBCLK offset on MODULE index */ #define MODULE_CLKSEL(x) ((x >>29) & 0x3) /*!< Calculate CLKSEL offset on MODULE index */ #define MODULE_CLKSEL_Msk(x) ((x >>25) & 0xf) /*!< Calculate CLKSEL mask offset on MODULE index */ #define MODULE_CLKSEL_Pos(x) ((x >>20) & 0x1f) /*!< Calculate CLKSEL position offset on MODULE index */ #define MODULE_CLKDIV(x) ((x >>18) & 0x3) /*!< Calculate APBCLK CLKDIV on MODULE index */ #define MODULE_CLKDIV_Msk(x) ((x >>10) & 0xff) /*!< Calculate CLKDIV mask offset on MODULE index */ #define MODULE_CLKDIV_Pos(x) ((x >>5 ) & 0x1f) /*!< Calculate CLKDIV position offset on MODULE index */ #define MODULE_IP_EN_Pos(x) ((x >>0 ) & 0x1f) /*!< Calculate APBCLK offset on MODULE index */ #define MODULE_NoMsk 0x0 /*!< Not mask on MODULE index */ /*-------------------------------------------------------------------------------------------------------------------------------------------------*/ /* APBCLK(31)|CLKSEL(30:29)|CLKSEL_Msk(28:25) |CLKSEL_Pos(24:20)|CLKDIV(19:18)|CLKDIV_Msk(17:10)|CLKDIV_Pos(9:5)|IP_EN_Pos(4:0)*/ /*-------------------------------------------------------------------------------------------------------------------------------------------------*/ #define WDT_MODULE ((0x0<<31)|(0x1<<29) |(0x3<<25) |( 0<<20) |(MODULE_NoMsk<<10) |CLK_APBCLK_WDT_EN_Pos ) /*!< WDT Module */ #define TMR0_MODULE ((0x0<<31)|(0x1<<29) |(0x7<<25) |( 8<<20) |(MODULE_NoMsk<<10) |CLK_APBCLK_TMR0_EN_Pos) /*!< TMR0 Module */ #define TMR1_MODULE ((0x0<<31)|(0x1<<29) |(0x7<<25) |(12<<20) |(MODULE_NoMsk<<10) |CLK_APBCLK_TMR1_EN_Pos) /*!< TMR1 Module */ #define TMR2_MODULE ((0x0<<31)|(0x1<<29) |(0x7<<25) |(16<<20) |(MODULE_NoMsk<<10) |CLK_APBCLK_TMR0_EN_Pos) /*!< TMR2 Module */ #define TMR3_MODULE ((0x0<<31)|(0x1<<29) |(0x7<<25) |(20<<20) |(MODULE_NoMsk<<10) |CLK_APBCLK_TMR1_EN_Pos) /*!< TMR3 Module */ #define FDIV_MODULE ((0x0<<31)|(0x3<<29) |(0x3<<25) |( 2<<20) |(MODULE_NoMsk<<10) |CLK_APBCLK_FDIV_EN_Pos) /*!< FDIV Module */ #define I2C0_MODULE ((0x0<<31) |(MODULE_NoMsk<<25) |(MODULE_NoMsk<<10) |CLK_APBCLK_I2C0_EN_Pos) /*!< I2C0 Module */ #define I2C1_MODULE ((0x0<<31) |(MODULE_NoMsk<<25) |(MODULE_NoMsk<<10) |CLK_APBCLK_I2C1_EN_Pos) /*!< I2C1 Module */ #define SPI0_MODULE ((0x0<<31)|(0x1<<29) |(0x1<<25) |( 4<<20) |(MODULE_NoMsk<<10) |CLK_APBCLK_SPI0_EN_Pos) /*!< SPI0 Module */ #define SPI1_MODULE ((0x0<<31)|(0x1<<29) |(0x1<<25) |( 5<<20) |(MODULE_NoMsk<<10) |CLK_APBCLK_SPI1_EN_Pos) /*!< SPI1 Module */ #define UART0_MODULE ((0x0<<31)|(0x1<<29) |(0x3<<25) |(24<<20) |(0x0<<18) |(0x0F<<10) |( 8<<5) |CLK_APBCLK_UART0_EN_Pos) /*!< UART0 Module */ #define UART1_MODULE ((0x0<<31)|(0x1<<29) |(0x3<<25) |(24<<20) |(0x0<<18) |(0x0F<<10) |( 8<<5) |CLK_APBCLK_UART1_EN_Pos) /*!< UART1 Module */ #define PWM01_MODULE ((0x0<<31)|(0x1<<29) |(0x3<<25) |(28<<20) |(MODULE_NoMsk<<10) |CLK_APBCLK_PWM01_EN_Pos) /*!< PWM01 Module */ #define PWM23_MODULE ((0x0<<31)|(0x1<<29) |(0x3<<25) |(30<<20) |(MODULE_NoMsk<<10) |CLK_APBCLK_PWM23_EN_Pos) /*!< PWM23 Module */ #define PWM45_MODULE ((0x0<<31)|(0x3<<29) |(0x3<<25) |( 4<<20) |(MODULE_NoMsk<<10) |CLK_APBCLK_PWM45_EN_Pos) /*!< PWM45 Module */ #define PWM67_MODULE ((0x0<<31)|(0x3<<29) |(0x3<<25) |( 6<<20) |(MODULE_NoMsk<<10) |CLK_APBCLK_PWM67_EN_Pos) /*!< PWM67 Module */ #define ADC_MODULE ((0x0<<31)|(0x1<<29) |(0x3<<25) |( 2<<20) |(0x0<<18) |(0xFF<<10) |(16<<5) |CLK_APBCLK_ADC_EN_Pos) /*!< ADC Module */ #define ACMP01_MODULE ((0x0<<31) |(MODULE_NoMsk<<25) |(MODULE_NoMsk<<10) |CLK_APBCLK_ACMP01_EN_Pos) /*!< ACMP01 Module */ #define ACMP23_MODULE ((0x0<<31) |(MODULE_NoMsk<<25) |(MODULE_NoMsk<<10) |CLK_APBCLK_ACMP23_EN_Pos) /*!< ACMP23 Module */ #define WWDT_MODULE ((MODULE_NoMsk<<31)|(0x3<<29)|(0x3<<25) |(16<<20) |(MODULE_NoMsk<<10) |MODULE_NoMsk ) /*!< WWDT Module */ /*@}*/ /* end of group M051_CLK_EXPORTED_CONSTANTS */ /** @addtogroup M051_CLK_EXPORTED_FUNCTIONS CLK Exported Functions @{ */ /** * @brief This function get PLL frequency. The frequency unit is Hz. * @return PLL frequency */ static __INLINE uint32_t CLK_GetPLLClockFreq(void) { uint32_t u32PllFreq = 0, u32PllReg; uint32_t u32FIN, u32NF, u32NR, u32NO; uint8_t au8NoTbl[4] = {1, 2, 2, 4}; u32PllReg = CLK->PLLCON; if(u32PllReg & (CLK_PLLCON_PD_Msk | CLK_PLLCON_OE_Msk)) return 0; /* PLL is in power down mode or fix low */ if(u32PllReg & CLK_PLLCON_PLL_SRC_HIRC) u32FIN = __HIRC; /* PLL source clock from HIRC */ else u32FIN = __HXT; /* PLL source clock from HXT */ if(u32PllReg & CLK_PLLCON_BP_Msk) return u32FIN; /* PLL is in bypass mode */ /* PLL is output enabled in normal work mode */ u32NO = au8NoTbl[((u32PllReg & CLK_PLLCON_OUT_DV_Msk) >> CLK_PLLCON_OUT_DV_Pos)]; u32NF = ((u32PllReg & CLK_PLLCON_FB_DV_Msk) >> CLK_PLLCON_FB_DV_Pos) + 2; u32NR = ((u32PllReg & CLK_PLLCON_IN_DV_Msk) >> CLK_PLLCON_IN_DV_Pos) + 2; /* u32FIN is shifted 2 bits to avoid overflow */ u32PllFreq = (((u32FIN >> 2) * u32NF) / (u32NR * u32NO) << 2); return u32PllFreq; } /** * @brief This function execute delay function. * @param us Delay time. The Max value is 2^24 / CPU Clock(MHz). Ex: * 50MHz => 335544us, 48MHz => 349525us, 28MHz => 699050us ... * @return None * @details Use the SysTick to generate the delay time and the UNIT is in us. * The SysTick clock source is from HCLK, i.e the same as system core clock. */ static __INLINE void CLK_SysTickDelay(uint32_t us) { SysTick->LOAD = us * CyclesPerUs; SysTick->VAL = (0x00); SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk; /* Waiting for down-count to zero */ while((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0); } void CLK_DisableCKO(void); void CLK_EnableCKO(uint32_t u32ClkSrc, uint32_t u32ClkDiv, uint32_t u32ClkDivBy1En); void CLK_PowerDown(void); void CLK_Idle(void); uint32_t CLK_GetHXTFreq(void); uint32_t CLK_GetLXTFreq(void); uint32_t CLK_GetHCLKFreq(void); uint32_t CLK_GetPCLKFreq(void); uint32_t CLK_GetCPUFreq(void); uint32_t CLK_SetCoreClock(uint32_t u32Hclk); void CLK_SetHCLK(uint32_t u32ClkSrc, uint32_t u32ClkDiv); void CLK_SetModuleClock(uint32_t u32ModuleIdx, uint32_t u32ClkSrc, uint32_t u32ClkDiv); void CLK_SetSysTickClockSrc(uint32_t u32ClkSrc); void CLK_EnableXtalRC(uint32_t u32ClkMask); void CLK_DisableXtalRC(uint32_t u32ClkMask); void CLK_EnableModuleClock(uint32_t u32ModuleIdx); void CLK_DisableModuleClock(uint32_t u32ModuleIdx); uint32_t CLK_EnablePLL(uint32_t u32PllClkSrc, uint32_t u32PllFreq); void CLK_DisablePLL(void); void CLK_WaitClockReady(uint32_t u32ClkMask); /*@}*/ /* end of group M051_CLK_EXPORTED_FUNCTIONS */ /*@}*/ /* end of group M051_CLK_Driver */ /*@}*/ /* end of group M051_Device_Driver */ #endif //__CLK_H__ /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/