123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987 |
- /*!
- \file system_gd32f30x.c
- \brief CMSIS Cortex-M4 Device Peripheral Access Layer Source File for
- GD32F30x Device Series
- */
- /* Copyright (c) 2012 ARM LIMITED
- All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- - Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- - Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- - Neither the name of ARM nor the names of its contributors may be used
- to endorse or promote products derived from this software without
- specific prior written permission.
- *
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- POSSIBILITY OF SUCH DAMAGE.
- ---------------------------------------------------------------------------*/
- /* This file refers the CMSIS standard, some adjustments are made according to GigaDevice chips */
- #include "gd32f30x.h"
- /* system frequency define */
- #define __IRC8M (IRC8M_VALUE) /* internal 8 MHz RC oscillator frequency */
- #define __HXTAL (HXTAL_VALUE) /* high speed crystal oscillator frequency */
- #define __SYS_OSC_CLK (__IRC8M) /* main oscillator frequency */
- /* select a system clock by uncommenting the following line */
- /* use IRC8M */
- //#define __SYSTEM_CLOCK_IRC8M (uint32_t)(__IRC8M)
- //#define __SYSTEM_CLOCK_48M_PLL_IRC8M (uint32_t)(48000000)
- //#define __SYSTEM_CLOCK_72M_PLL_IRC8M (uint32_t)(72000000)
- //#define __SYSTEM_CLOCK_108M_PLL_IRC8M (uint32_t)(108000000)
- //#define __SYSTEM_CLOCK_120M_PLL_IRC8M (uint32_t)(120000000)
- /* use HXTAL(XD series CK_HXTAL = 8M, CL series CK_HXTAL = 25M) */
- //#define __SYSTEM_CLOCK_HXTAL (uint32_t)(__HXTAL)
- //#define __SYSTEM_CLOCK_48M_PLL_HXTAL (uint32_t)(48000000)
- //#define __SYSTEM_CLOCK_72M_PLL_HXTAL (uint32_t)(72000000)
- //#define __SYSTEM_CLOCK_108M_PLL_HXTAL (uint32_t)(108000000)
- #define __SYSTEM_CLOCK_120M_PLL_HXTAL (uint32_t)(120000000)
- #define SEL_IRC8M 0x00U
- #define SEL_HXTAL 0x01U
- #define SEL_PLL 0x02U
- /* set the system clock frequency and declare the system clock configuration function */
- #ifdef __SYSTEM_CLOCK_IRC8M
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_IRC8M;
- static void system_clock_8m_irc8m(void);
- #elif defined (__SYSTEM_CLOCK_48M_PLL_IRC8M)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_48M_PLL_IRC8M;
- static void system_clock_48m_irc8m(void);
- #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_IRC8M;
- static void system_clock_72m_irc8m(void);
- #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_108M_PLL_IRC8M;
- static void system_clock_108m_irc8m(void);
- #elif defined (__SYSTEM_CLOCK_120M_PLL_IRC8M)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_120M_PLL_IRC8M;
- static void system_clock_120m_irc8m(void);
- #elif defined (__SYSTEM_CLOCK_HXTAL)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_HXTAL;
- static void system_clock_hxtal(void);
- #elif defined (__SYSTEM_CLOCK_48M_PLL_HXTAL)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_48M_PLL_HXTAL;
- static void system_clock_48m_hxtal(void);
- #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_HXTAL;
- static void system_clock_72m_hxtal(void);
- #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_108M_PLL_HXTAL;
- static void system_clock_108m_hxtal(void);
- #elif defined (__SYSTEM_CLOCK_120M_PLL_HXTAL)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_120M_PLL_HXTAL;
- static void system_clock_120m_hxtal(void);
- #endif /* __SYSTEM_CLOCK_IRC8M */
- /* configure the system clock */
- static void system_clock_config(void);
- /*!
- \brief setup the microcontroller system, initialize the system
- \param[in] none
- \param[out] none
- \retval none
- */
- void SystemInit (void)
- {
- /* FPU settings */
- #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
- SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
- #endif
- /* reset the RCU clock configuration to the default reset state */
- /* Set IRC8MEN bit */
- RCU_CTL |= RCU_CTL_IRC8MEN;
- /* Reset CFG0 and CFG1 registers */
- RCU_CFG0 = 0x00000000U;
- RCU_CFG1 = 0x00000000U;
- #if (defined(GD32F30X_HD) || defined(GD32F30X_XD))
- /* reset HXTALEN, CKMEN and PLLEN bits */
- RCU_CTL &= ~(RCU_CTL_PLLEN | RCU_CTL_CKMEN | RCU_CTL_HXTALEN);
- /* disable all interrupts */
- RCU_INT = 0x009f0000U;
- #elif defined(GD32F30X_CL)
- /* Reset HXTALEN, CKMEN, PLLEN, PLL1EN and PLL2EN bits */
- RCU_CTL &= ~(RCU_CTL_PLLEN |RCU_CTL_PLL1EN | RCU_CTL_PLL2EN | RCU_CTL_CKMEN | RCU_CTL_HXTALEN);
- /* disable all interrupts */
- RCU_INT = 0x00ff0000U;
- #endif
- /* reset HXTALBPS bit */
- RCU_CTL &= ~(RCU_CTL_HXTALBPS);
- /* configure the system clock source, PLL Multiplier, AHB/APBx prescalers and Flash settings */
- system_clock_config();
- }
- /*!
- \brief configure the system clock
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_config(void)
- {
- #ifdef __SYSTEM_CLOCK_IRC8M
- system_clock_8m_irc8m();
- #elif defined (__SYSTEM_CLOCK_48M_PLL_IRC8M)
- system_clock_48m_irc8m();
- #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M)
- system_clock_72m_irc8m();
- #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M)
- system_clock_108m_irc8m();
- #elif defined (__SYSTEM_CLOCK_120M_PLL_IRC8M)
- system_clock_120m_irc8m();
- #elif defined (__SYSTEM_CLOCK_HXTAL)
- system_clock_hxtal();
- #elif defined (__SYSTEM_CLOCK_48M_PLL_HXTAL)
- system_clock_48m_hxtal();
- #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
- system_clock_72m_hxtal();
- #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
- system_clock_108m_hxtal();
- #elif defined (__SYSTEM_CLOCK_120M_PLL_HXTAL)
- system_clock_120m_hxtal();
- #endif /* __SYSTEM_CLOCK_IRC8M */
- }
- #ifdef __SYSTEM_CLOCK_IRC8M
- /*!
- \brief configure the system clock to 8M by IRC8M
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_8m_irc8m(void)
- {
- uint32_t timeout = 0U;
- uint32_t stab_flag = 0U;
-
- /* enable IRC8M */
- RCU_CTL |= RCU_CTL_IRC8MEN;
-
- /* wait until IRC8M is stable or the startup time is longer than IRC8M_STARTUP_TIMEOUT */
- do{
- timeout++;
- stab_flag = (RCU_CTL & RCU_CTL_IRC8MSTB);
- }
- while((0U == stab_flag) && (IRC8M_STARTUP_TIMEOUT != timeout));
-
- /* if fail */
- if(0U == (RCU_CTL & RCU_CTL_IRC8MSTB)){
- while(1){
- }
- }
-
- /* AHB = SYSCLK */
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
- /* APB2 = AHB/1 */
- RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
- /* APB1 = AHB/2 */
- RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
-
- /* select IRC8M as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_IRC8M;
-
- /* wait until IRC8M is selected as system clock */
- while(0U != (RCU_CFG0 & RCU_SCSS_IRC8M)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_48M_PLL_IRC8M)
- /*!
- \brief configure the system clock to 48M by PLL which selects IRC8M as its clock source
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_48m_irc8m(void)
- {
- uint32_t timeout = 0U;
- uint32_t stab_flag = 0U;
-
- /* enable IRC8M */
- RCU_CTL |= RCU_CTL_IRC8MEN;
- /* wait until IRC8M is stable or the startup time is longer than IRC8M_STARTUP_TIMEOUT */
- do{
- timeout++;
- stab_flag = (RCU_CTL & RCU_CTL_IRC8MSTB);
- }
- while((0U == stab_flag) && (IRC8M_STARTUP_TIMEOUT != timeout));
- /* if fail */
- if(0U == (RCU_CTL & RCU_CTL_IRC8MSTB)){
- while(1){
- }
- }
- /* LDO output voltage high mode */
- RCU_APB1EN |= RCU_APB1EN_PMUEN;
- PMU_CTL |= PMU_CTL_LDOVS;
- /* IRC8M is stable */
- /* AHB = SYSCLK */
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
- /* APB2 = AHB/1 */
- RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
- /* APB1 = AHB/2 */
- RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
- /* CK_PLL = (CK_IRC8M/2) * 12 = 48 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4 | RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= RCU_PLL_MUL12;
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
-
- /* enable the high-drive to extend the clock frequency to 120 MHz */
- PMU_CTL |= PMU_CTL_HDEN;
- while(0U == (PMU_CS & PMU_CS_HDRF)){
- }
-
- /* select the high-drive mode */
- PMU_CTL |= PMU_CTL_HDS;
- while(0U == (PMU_CS & PMU_CS_HDSRF)){
- }
-
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M)
- /*!
- \brief configure the system clock to 72M by PLL which selects IRC8M as its clock source
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_72m_irc8m(void)
- {
- uint32_t timeout = 0U;
- uint32_t stab_flag = 0U;
-
- /* enable IRC8M */
- RCU_CTL |= RCU_CTL_IRC8MEN;
- /* wait until IRC8M is stable or the startup time is longer than IRC8M_STARTUP_TIMEOUT */
- do{
- timeout++;
- stab_flag = (RCU_CTL & RCU_CTL_IRC8MSTB);
- }while((0U == stab_flag) && (IRC8M_STARTUP_TIMEOUT != timeout));
- /* if fail */
- if(0U == (RCU_CTL & RCU_CTL_IRC8MSTB)){
- while(1){
- }
- }
- /* LDO output voltage high mode */
- RCU_APB1EN |= RCU_APB1EN_PMUEN;
- PMU_CTL |= PMU_CTL_LDOVS;
- /* IRC8M is stable */
- /* AHB = SYSCLK */
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
- /* APB2 = AHB/1 */
- RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
- /* APB1 = AHB/2 */
- RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
- /* CK_PLL = (CK_IRC8M/2) * 18 = 72 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4 | RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= RCU_PLL_MUL18;
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
-
- /* enable the high-drive to extend the clock frequency to 120 MHz */
- PMU_CTL |= PMU_CTL_HDEN;
- while(0U == (PMU_CS & PMU_CS_HDRF)){
- }
-
- /* select the high-drive mode */
- PMU_CTL |= PMU_CTL_HDS;
- while(0U == (PMU_CS & PMU_CS_HDSRF)){
- }
-
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M)
- /*!
- \brief configure the system clock to 108M by PLL which selects IRC8M as its clock source
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_108m_irc8m(void)
- {
- uint32_t timeout = 0U;
- uint32_t stab_flag = 0U;
-
- /* enable IRC8M */
- RCU_CTL |= RCU_CTL_IRC8MEN;
- /* wait until IRC8M is stable or the startup time is longer than IRC8M_STARTUP_TIMEOUT */
- do{
- timeout++;
- stab_flag = (RCU_CTL & RCU_CTL_IRC8MSTB);
- }while((0U == stab_flag) && (IRC8M_STARTUP_TIMEOUT != timeout));
- /* if fail */
- if(0U == (RCU_CTL & RCU_CTL_IRC8MSTB)){
- while(1){
- }
- }
- /* LDO output voltage high mode */
- RCU_APB1EN |= RCU_APB1EN_PMUEN;
- PMU_CTL |= PMU_CTL_LDOVS;
- /* IRC8M is stable */
- /* AHB = SYSCLK */
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
- /* APB2 = AHB/1 */
- RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
- /* APB1 = AHB/2 */
- RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
- /* CK_PLL = (CK_IRC8M/2) * 27 = 108 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4 | RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= RCU_PLL_MUL27;
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
-
- /* enable the high-drive to extend the clock frequency to 120 MHz */
- PMU_CTL |= PMU_CTL_HDEN;
- while(0U == (PMU_CS & PMU_CS_HDRF)){
- }
-
- /* select the high-drive mode */
- PMU_CTL |= PMU_CTL_HDS;
- while(0U == (PMU_CS & PMU_CS_HDSRF)){
- }
-
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_120M_PLL_IRC8M)
- /*!
- \brief configure the system clock to 120M by PLL which selects IRC8M as its clock source
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_120m_irc8m(void)
- {
- uint32_t timeout = 0U;
- uint32_t stab_flag = 0U;
-
- /* enable IRC8M */
- RCU_CTL |= RCU_CTL_IRC8MEN;
- /* wait until IRC8M is stable or the startup time is longer than IRC8M_STARTUP_TIMEOUT */
- do{
- timeout++;
- stab_flag = (RCU_CTL & RCU_CTL_IRC8MSTB);
- }while((0U == stab_flag) && (IRC8M_STARTUP_TIMEOUT != timeout));
- /* if fail */
- if(0U == (RCU_CTL & RCU_CTL_IRC8MSTB)){
- while(1){
- }
- }
- /* LDO output voltage high mode */
- RCU_APB1EN |= RCU_APB1EN_PMUEN;
- PMU_CTL |= PMU_CTL_LDOVS;
- /* IRC8M is stable */
- /* AHB = SYSCLK */
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
- /* APB2 = AHB/1 */
- RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
- /* APB1 = AHB/2 */
- RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
- /* CK_PLL = (CK_IRC8M/2) * 30 = 120 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4 | RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= RCU_PLL_MUL30;
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
-
- /* enable the high-drive to extend the clock frequency to 120 MHz */
- PMU_CTL |= PMU_CTL_HDEN;
- while(0U == (PMU_CS & PMU_CS_HDRF)){
- }
-
- /* select the high-drive mode */
- PMU_CTL |= PMU_CTL_HDS;
- while(0U == (PMU_CS & PMU_CS_HDSRF)){
- }
-
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_HXTAL)
- /*!
- \brief configure the system clock to HXTAL
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_hxtal(void)
- {
- uint32_t timeout = 0U;
- uint32_t stab_flag = 0U;
-
- /* enable HXTAL */
- RCU_CTL |= RCU_CTL_HXTALEN;
-
- /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
- do{
- timeout++;
- stab_flag = (RCU_CTL & RCU_CTL_HXTALSTB);
- }while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
-
- /* if fail */
- if(0U == (RCU_CTL & RCU_CTL_HXTALSTB)){
- while(1){
- }
- }
-
- /* AHB = SYSCLK */
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
- /* APB2 = AHB/1 */
- RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
- /* APB1 = AHB/2 */
- RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
-
- /* select HXTAL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_HXTAL;
-
- /* wait until HXTAL is selected as system clock */
- while(0 == (RCU_CFG0 & RCU_SCSS_HXTAL)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_48M_PLL_HXTAL)
- /*!
- \brief configure the system clock to 48M by PLL which selects HXTAL(8M) as its clock source
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_48m_hxtal(void)
- {
- uint32_t timeout = 0U;
- uint32_t stab_flag = 0U;
- /* enable HXTAL */
- RCU_CTL |= RCU_CTL_HXTALEN;
- /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
- do{
- timeout++;
- stab_flag = (RCU_CTL & RCU_CTL_HXTALSTB);
- }while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
- /* if fail */
- if(0U == (RCU_CTL & RCU_CTL_HXTALSTB)){
- while(1){
- }
- }
- RCU_APB1EN |= RCU_APB1EN_PMUEN;
- PMU_CTL |= PMU_CTL_LDOVS;
- /* HXTAL is stable */
- /* AHB = SYSCLK */
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
- /* APB2 = AHB/1 */
- RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
- /* APB1 = AHB/2 */
- RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
- #if (defined(GD32F30X_HD) || defined(GD32F30X_XD))
- /* select HXTAL/2 as clock source */
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_CFG0_PREDV0);
- /* CK_PLL = (CK_HXTAL/2) * 12 = 48 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4 | RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= RCU_PLL_MUL12;
- #elif defined(GD32F30X_CL)
- /* CK_PLL = (CK_PREDIV0) * 12 = 48 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4 | RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_PLL_MUL12);
- /* CK_PREDIV0 = (CK_HXTAL)/5 *8 /10 = 4 MHz */
- RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0);
- RCU_CFG1 |= (RCU_PLLPRESRC_HXTAL | RCU_PREDV0SRC_CKPLL1 | RCU_PLL1_MUL8 | RCU_PREDV1_DIV5 | RCU_PREDV0_DIV10);
- /* enable PLL1 */
- RCU_CTL |= RCU_CTL_PLL1EN;
- /* wait till PLL1 is ready */
- while((RCU_CTL & RCU_CTL_PLL1STB) == 0){
- }
- #endif /* GD32F30X_HD and GD32F30X_XD */
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* enable the high-drive to extend the clock frequency to 120 MHz */
- PMU_CTL |= PMU_CTL_HDEN;
- while(0U == (PMU_CS & PMU_CS_HDRF)){
- }
- /* select the high-drive mode */
- PMU_CTL |= PMU_CTL_HDS;
- while(0U == (PMU_CS & PMU_CS_HDSRF)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
- /*!
- \brief configure the system clock to 72M by PLL which selects HXTAL(8M) as its clock source
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_72m_hxtal(void)
- {
- uint32_t timeout = 0U;
- uint32_t stab_flag = 0U;
- /* enable HXTAL */
- RCU_CTL |= RCU_CTL_HXTALEN;
- /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
- do{
- timeout++;
- stab_flag = (RCU_CTL & RCU_CTL_HXTALSTB);
- }while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
- /* if fail */
- if(0U == (RCU_CTL & RCU_CTL_HXTALSTB)){
- while(1){
- }
- }
- RCU_APB1EN |= RCU_APB1EN_PMUEN;
- PMU_CTL |= PMU_CTL_LDOVS;
- /* HXTAL is stable */
- /* AHB = SYSCLK */
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
- /* APB2 = AHB/1 */
- RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
- /* APB1 = AHB/2 */
- RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
- #if (defined(GD32F30X_HD) || defined(GD32F30X_XD))
- /* select HXTAL/2 as clock source */
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_CFG0_PREDV0);
- /* CK_PLL = (CK_HXTAL/2) * 18 = 72 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4 | RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= RCU_PLL_MUL18;
- #elif defined(GD32F30X_CL)
- /* CK_PLL = (CK_PREDIV0) * 18 = 72 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4 | RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_PLL_MUL18);
- /* CK_PREDIV0 = (CK_HXTAL)/5 *8 /10 = 4 MHz */
- RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0);
- RCU_CFG1 |= (RCU_PLLPRESRC_HXTAL | RCU_PREDV0SRC_CKPLL1 | RCU_PLL1_MUL8 | RCU_PREDV1_DIV5 | RCU_PREDV0_DIV10);
- /* enable PLL1 */
- RCU_CTL |= RCU_CTL_PLL1EN;
- /* wait till PLL1 is ready */
- while((RCU_CTL & RCU_CTL_PLL1STB) == 0){
- }
- #endif /* GD32F30X_HD and GD32F30X_XD */
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* enable the high-drive to extend the clock frequency to 120 MHz */
- PMU_CTL |= PMU_CTL_HDEN;
- while(0U == (PMU_CS & PMU_CS_HDRF)){
- }
- /* select the high-drive mode */
- PMU_CTL |= PMU_CTL_HDS;
- while(0U == (PMU_CS & PMU_CS_HDSRF)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
- /*!
- \brief configure the system clock to 108M by PLL which selects HXTAL(8M) as its clock source
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_108m_hxtal(void)
- {
- uint32_t timeout = 0U;
- uint32_t stab_flag = 0U;
- /* enable HXTAL */
- RCU_CTL |= RCU_CTL_HXTALEN;
- /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
- do{
- timeout++;
- stab_flag = (RCU_CTL & RCU_CTL_HXTALSTB);
- }while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
- /* if fail */
- if(0U == (RCU_CTL & RCU_CTL_HXTALSTB)){
- while(1){
- }
- }
- RCU_APB1EN |= RCU_APB1EN_PMUEN;
- PMU_CTL |= PMU_CTL_LDOVS;
- /* HXTAL is stable */
- /* AHB = SYSCLK */
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
- /* APB2 = AHB/1 */
- RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
- /* APB1 = AHB/2 */
- RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
- #if (defined(GD32F30X_HD) || defined(GD32F30X_XD))
- /* select HXTAL/2 as clock source */
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_CFG0_PREDV0);
- /* CK_PLL = (CK_HXTAL/2) * 27 = 108 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4 | RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= RCU_PLL_MUL27;
- #elif defined(GD32F30X_CL)
- /* CK_PLL = (CK_PREDIV0) * 27 = 108 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4 | RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_PLL_MUL27);
- /* CK_PREDIV0 = (CK_HXTAL)/5 *8 /10 = 4 MHz */
- RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0);
- RCU_CFG1 |= (RCU_PLLPRESRC_HXTAL | RCU_PREDV0SRC_CKPLL1 | RCU_PLL1_MUL8 | RCU_PREDV1_DIV5 | RCU_PREDV0_DIV10);
- /* enable PLL1 */
- RCU_CTL |= RCU_CTL_PLL1EN;
- /* wait till PLL1 is ready */
- while((RCU_CTL & RCU_CTL_PLL1STB) == 0){
- }
- #endif /* GD32F30X_HD and GD32F30X_XD */
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* enable the high-drive to extend the clock frequency to 120 MHz */
- PMU_CTL |= PMU_CTL_HDEN;
- while(0U == (PMU_CS & PMU_CS_HDRF)){
- }
- /* select the high-drive mode */
- PMU_CTL |= PMU_CTL_HDS;
- while(0U == (PMU_CS & PMU_CS_HDSRF)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_120M_PLL_HXTAL)
- /*!
- \brief configure the system clock to 120M by PLL which selects HXTAL(8M) as its clock source
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_120m_hxtal(void)
- {
- uint32_t timeout = 0U;
- uint32_t stab_flag = 0U;
- /* enable HXTAL */
- RCU_CTL |= RCU_CTL_HXTALEN;
- /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
- do{
- timeout++;
- stab_flag = (RCU_CTL & RCU_CTL_HXTALSTB);
- }while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
- /* if fail */
- if(0U == (RCU_CTL & RCU_CTL_HXTALSTB)){
- while(1){
- }
- }
- RCU_APB1EN |= RCU_APB1EN_PMUEN;
- PMU_CTL |= PMU_CTL_LDOVS;
- /* HXTAL is stable */
- /* AHB = SYSCLK */
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
- /* APB2 = AHB/1 */
- RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
- /* APB1 = AHB/2 */
- RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
- #if (defined(GD32F30X_HD) || defined(GD32F30X_XD))
- /* select HXTAL/2 as clock source */
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_CFG0_PREDV0);
- /* CK_PLL = (CK_HXTAL/2) * 30 = 120 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4 | RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= RCU_PLL_MUL30;
- #elif defined(GD32F30X_CL)
- /* CK_PLL = (CK_PREDIV0) * 30 = 120 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4 | RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_PLL_MUL30);
- /* CK_PREDIV0 = (CK_HXTAL)/5 *8 /10 = 4 MHz */
- RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0);
- RCU_CFG1 |= (RCU_PLLPRESRC_HXTAL | RCU_PREDV0SRC_CKPLL1 | RCU_PLL1_MUL8 | RCU_PREDV1_DIV5 | RCU_PREDV0_DIV10);
- /* enable PLL1 */
- RCU_CTL |= RCU_CTL_PLL1EN;
- /* wait till PLL1 is ready */
- while((RCU_CTL & RCU_CTL_PLL1STB) == 0U){
- }
- #endif /* GD32F30X_HD and GD32F30X_XD */
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* enable the high-drive to extend the clock frequency to 120 MHz */
- PMU_CTL |= PMU_CTL_HDEN;
- while(0U == (PMU_CS & PMU_CS_HDRF)){
- }
- /* select the high-drive mode */
- PMU_CTL |= PMU_CTL_HDS;
- while(0U == (PMU_CS & PMU_CS_HDSRF)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
- }
- }
- #endif /* __SYSTEM_CLOCK_IRC8M */
- /*!
- \brief update the SystemCoreClock with current core clock retrieved from cpu registers
- \param[in] none
- \param[out] none
- \retval none
- */
- void SystemCoreClockUpdate (void)
- {
- uint32_t sws;
- uint32_t pllsel, pllpresel, predv0sel, pllmf,ck_src;
- #ifdef GD32F30X_CL
- uint32_t predv0, predv1, pll1mf;
- #endif /* GD32F30X_CL */
- sws = GET_BITS(RCU_CFG0, 2, 3);
- switch(sws){
- /* IRC8M is selected as CK_SYS */
- case SEL_IRC8M:
- SystemCoreClock = IRC8M_VALUE;
- break;
- /* HXTAL is selected as CK_SYS */
- case SEL_HXTAL:
- SystemCoreClock = HXTAL_VALUE;
- break;
- /* PLL is selected as CK_SYS */
- case SEL_PLL:
- /* PLL clock source selection, HXTAL, IRC48M or IRC8M/2 */
- pllsel = (RCU_CFG0 & RCU_CFG0_PLLSEL);
- if (RCU_PLLSRC_HXTAL_IRC48M == pllsel) {
- /* PLL clock source is HXTAL or IRC48M */
- pllpresel = (RCU_CFG1 & RCU_CFG1_PLLPRESEL);
-
- if(RCU_PLLPRESRC_HXTAL == pllpresel){
- /* PLL clock source is HXTAL */
- ck_src = HXTAL_VALUE;
- }else{
- /* PLL clock source is IRC48 */
- ck_src = IRC48M_VALUE;
- }
- #if (defined(GD32F30X_HD) || defined(GD32F30X_XD))
- predv0sel = (RCU_CFG0 & RCU_CFG0_PREDV0);
- /* PREDV0 input source clock divided by 2 */
- if(RCU_CFG0_PREDV0 == predv0sel){
- ck_src = HXTAL_VALUE/2U;
- }
- #elif defined(GD32F30X_CL)
- predv0sel = (RCU_CFG1 & RCU_CFG1_PREDV0SEL);
- /* source clock use PLL1 */
- if(RCU_PREDV0SRC_CKPLL1 == predv0sel){
- predv1 = ((RCU_CFG1 & RCU_CFG1_PREDV1) >> 4) + 1U;
- pll1mf = ((RCU_CFG1 & RCU_CFG1_PLL1MF) >> 8) + 2U;
- if(17U == pll1mf){
- pll1mf = 20U;
- }
- ck_src = (ck_src/predv1)*pll1mf;
- }
- predv0 = (RCU_CFG1 & RCU_CFG1_PREDV0) + 1U;
- ck_src /= predv0;
- #endif /* GD32F30X_HD and GD32F30X_XD */
- }else{
- /* PLL clock source is IRC8M/2 */
- ck_src = IRC8M_VALUE/2U;
- }
- /* PLL multiplication factor */
- pllmf = GET_BITS(RCU_CFG0, 18, 21);
- if((RCU_CFG0 & RCU_CFG0_PLLMF_4)){
- pllmf |= 0x10U;
- }
- if((RCU_CFG0 & RCU_CFG0_PLLMF_5)){
- pllmf |= 0x20U;
- }
- if( pllmf >= 15U){
- pllmf += 1U;
- }else{
- pllmf += 2U;
- }
- if(pllmf > 61U){
- pllmf = 63U;
- }
- SystemCoreClock = ck_src*pllmf;
- #ifdef GD32F30X_CL
- if(15U == pllmf){
- SystemCoreClock = ck_src*6U + ck_src/2U;
- }
- #endif /* GD32F30X_CL */
- break;
- /* IRC8M is selected as CK_SYS */
- default:
- SystemCoreClock = IRC8M_VALUE;
- break;
- }
- }
|