12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058 |
- /*!
- \file system_gd32f10x.c
- \brief CMSIS Cortex-M3 Device Peripheral Access Layer Source File for
- GD32F10x 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:
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
- 2. 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.
- 3. Neither the name of the copyright holder 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 THE COPYRIGHT HOLDER OR 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 "gd32f10x.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 */
- #define VECT_TAB_OFFSET (uint32_t)0x00 /* vector table base offset */
- /* select a system clock by uncommenting the following line */
- /* use 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)
- /* use HXTAL (XD series CK_HXTAL = 8M, CL series CK_HXTAL = 25M) */
- //#define __SYSTEM_CLOCK_HXTAL (uint32_t)(__HXTAL)
- //#define __SYSTEM_CLOCK_24M_PLL_HXTAL (uint32_t)(24000000)
- //#define __SYSTEM_CLOCK_36M_PLL_HXTAL (uint32_t)(36000000)
- //#define __SYSTEM_CLOCK_48M_PLL_HXTAL (uint32_t)(48000000)
- //#define __SYSTEM_CLOCK_56M_PLL_HXTAL (uint32_t)(56000000)
- //#define __SYSTEM_CLOCK_72M_PLL_HXTAL (uint32_t)(72000000)
- //#define __SYSTEM_CLOCK_96M_PLL_HXTAL (uint32_t)(96000000)
- #define __SYSTEM_CLOCK_108M_PLL_HXTAL (uint32_t)(108000000)
- #define RCU_MODIFY(__delay) do{ \
- volatile uint32_t i; \
- if(0 != __delay){ \
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV2; \
- for(i=0; i<__delay; i++){ \
- } \
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV4; \
- for(i=0; i<__delay; i++){ \
- } \
- } \
- }while(0)
- #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_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_HXTAL)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_HXTAL;
- static void system_clock_hxtal(void);
- #elif defined (__SYSTEM_CLOCK_24M_PLL_HXTAL)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_24M_PLL_HXTAL;
- static void system_clock_24m_hxtal(void);
- #elif defined (__SYSTEM_CLOCK_36M_PLL_HXTAL)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_36M_PLL_HXTAL;
- static void system_clock_36m_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_56M_PLL_HXTAL)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_56M_PLL_HXTAL;
- static void system_clock_56m_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_96M_PLL_HXTAL)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_96M_PLL_HXTAL;
- static void system_clock_96m_hxtal(void);
- #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
- uint32_t SystemCoreClock = __SYSTEM_CLOCK_108M_PLL_HXTAL;
- static void system_clock_108m_hxtal(void);
- #endif /* __SYSTEM_CLOCK_48M_PLL_IRC8M */
- /* configure the system clock */
- static void system_clock_config(void);
- /*!
- \brief configure the system clock
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_config(void)
- {
- #ifdef __SYSTEM_CLOCK_HXTAL
- system_clock_hxtal();
- #elif defined (__SYSTEM_CLOCK_24M_PLL_HXTAL)
- system_clock_24m_hxtal();
- #elif defined (__SYSTEM_CLOCK_36M_PLL_HXTAL)
- system_clock_36m_hxtal();
- #elif defined (__SYSTEM_CLOCK_48M_PLL_HXTAL)
- system_clock_48m_hxtal();
- #elif defined (__SYSTEM_CLOCK_56M_PLL_HXTAL)
- system_clock_56m_hxtal();
- #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
- system_clock_72m_hxtal();
- #elif defined (__SYSTEM_CLOCK_96M_PLL_HXTAL)
- system_clock_96m_hxtal();
- #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
- system_clock_108m_hxtal();
- #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();
- #endif /* __SYSTEM_CLOCK_HXTAL */
- }
- /*!
- \brief setup the microcontroller system, initialize the system
- \param[in] none
- \param[out] none
- \retval none
- */
- void SystemInit(void)
- {
- /* reset the RCC clock configuration to the default reset state */
- /* enable IRC8M */
- RCU_CTL |= RCU_CTL_IRC8MEN;
- while(0U == (RCU_CTL & RCU_CTL_IRC8MSTB)){
- }
- RCU_MODIFY(0x50);
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- /* reset HXTALEN, CKMEN, PLLEN bits */
- RCU_CTL &= ~(RCU_CTL_HXTALEN | RCU_CTL_CKMEN | RCU_CTL_PLLEN);
- /* reset SCS, AHBPSC, APB1PSC, APB2PSC, ADCPSC, CKOUT0SEL bits */
- RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |
- RCU_CFG0_ADCPSC | RCU_CFG0_ADCPSC_2 | RCU_CFG0_CKOUT0SEL);
- /* reset HXTALEN, CKMEN, PLLEN bits */
- RCU_CTL &= ~(RCU_CTL_HXTALEN | RCU_CTL_CKMEN | RCU_CTL_PLLEN);
- /* Reset HXTALBPS bit */
- RCU_CTL &= ~(RCU_CTL_HXTALBPS);
- /* reset PLLSEL, PREDV0_LSB, PLLMF, USBFSPSC bits */
-
- #ifdef GD32F10X_CL
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0_LSB | RCU_CFG0_PLLMF |
- RCU_CFG0_USBFSPSC | RCU_CFG0_PLLMF_4);
- RCU_CFG1 = 0x00000000U;
- #else
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0 | RCU_CFG0_PLLMF |
- RCU_CFG0_USBDPSC | RCU_CFG0_PLLMF_4);
- #endif /* GD32F10X_CL */
- #if (defined(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_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(GD32F10X_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
- /* Configure the System clock source, PLL Multiplier, AHB/APBx prescalers and Flash settings */
- system_clock_config();
-
- #ifdef VECT_TAB_SRAM
- nvic_vector_table_set(NVIC_VECTTAB_RAM, VECT_TAB_OFFSET);
- #else
- nvic_vector_table_set(NVIC_VECTTAB_FLASH, VECT_TAB_OFFSET);
- #endif
- }
- #ifdef __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(RCU_SCSS_HXTAL != (RCU_CFG0 & RCU_CFG0_SCSS)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_24M_PLL_HXTAL)
- /*!
- \brief configure the system clock to 24M by PLL which selects HXTAL(MD/HD/XD:8M; CL:25M) as its clock source
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_24m_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){
- }
- }
- /* 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(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
- /* select HXTAL/2 as clock source */
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_CFG0_PREDV0);
- /* CK_PLL = (CK_HXTAL/2) * 6 = 24 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= RCU_PLL_MUL6;
- #elif defined(GD32F10X_CL)
- /* CK_PLL = (CK_PREDIV0) * 6 = 24 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_PLL_MUL6);
- /* CK_PREDIV0 = (CK_HXTAL)/5 *8 /10 = 4 MHz */
- RCU_CFG1 &= ~(RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0);
- RCU_CFG1 |= (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 /* GD32F10X_MD and GD32F10X_HD and GD32F10X_XD */
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(RCU_SCSS_PLL != (RCU_CFG0 & RCU_CFG0_SCSS)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_36M_PLL_HXTAL)
- /*!
- \brief configure the system clock to 36M by PLL which selects HXTAL(MD/HD/XD:8M; CL:25M) as its clock source
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_36m_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){
- }
- }
- /* 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(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
- /* select HXTAL/2 as clock source */
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_CFG0_PREDV0);
- /* CK_PLL = (CK_HXTAL/2) * 9 = 36 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= RCU_PLL_MUL9;
- #elif defined(GD32F10X_CL)
- /* CK_PLL = (CK_PREDIV0) * 9 = 36 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_PLL_MUL9);
- /* CK_PREDIV0 = (CK_HXTAL)/5 *8 /10 = 4 MHz */
- RCU_CFG1 &= ~(RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0);
- RCU_CFG1 |= (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 /* GD32F10X_MD and GD32F10X_HD and GD32F10X_XD */
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(RCU_SCSS_PLL != (RCU_CFG0 & RCU_CFG0_SCSS)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_48M_PLL_HXTAL)
- /*!
- \brief configure the system clock to 48M by PLL which selects HXTAL(MD/HD/XD:8M; CL:25M) 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){
- }
- }
- /* 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(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
- /* select HXTAL/2 as clock source */
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_CFG0_PREDV0);
- /* CK_PLL = (CK_HXTAL/2) * 12 = 48 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= RCU_PLL_MUL12;
- #elif defined(GD32F10X_CL)
- /* CK_PLL = (CK_PREDIV0) * 12 = 48 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_PLL_MUL12);
- /* CK_PREDIV0 = (CK_HXTAL)/5 *8 /10 = 4 MHz */
- RCU_CFG1 &= ~(RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0);
- RCU_CFG1 |= (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 /* GD32F10X_MD and GD32F10X_HD and GD32F10X_XD */
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(RCU_SCSS_PLL != (RCU_CFG0 & RCU_CFG0_SCSS)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_56M_PLL_HXTAL)
- /*!
- \brief configure the system clock to 56M by PLL which selects HXTAL(MD/HD/XD:8M; CL:25M) as its clock source
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_56m_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){
- }
- }
- /* 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(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
- /* select HXTAL/2 as clock source */
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_CFG0_PREDV0);
- /* CK_PLL = (CK_HXTAL/2) * 14 = 56 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= RCU_PLL_MUL14;
- #elif defined(GD32F10X_CL)
- /* CK_PLL = (CK_PREDIV0) * 14 = 56 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_PLL_MUL14);
- /* CK_PREDIV0 = (CK_HXTAL)/5 *8 /10 = 4 MHz */
- RCU_CFG1 &= ~(RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0);
- RCU_CFG1 |= (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 /* GD32F10X_MD and GD32F10X_HD and GD32F10X_XD */
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(RCU_SCSS_PLL != (RCU_CFG0 & RCU_CFG0_SCSS)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
- /*!
- \brief configure the system clock to 72M by PLL which selects HXTAL(MD/HD/XD:8M; CL:25M) 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){
- }
- }
- /* 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(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
- /* select HXTAL/2 as clock source */
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_CFG0_PREDV0);
- /* CK_PLL = (CK_HXTAL/2) * 18 = 72 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= RCU_PLL_MUL18;
- #elif defined(GD32F10X_CL)
- /* CK_PLL = (CK_PREDIV0) * 18 = 72 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_PLL_MUL18);
- /* CK_PREDIV0 = (CK_HXTAL)/5 *8 /10 = 4 MHz */
- RCU_CFG1 &= ~(RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0);
- RCU_CFG1 |= (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 /* GD32F10X_MD and GD32F10X_HD and GD32F10X_XD */
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(RCU_SCSS_PLL != (RCU_CFG0 & RCU_CFG0_SCSS)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_96M_PLL_HXTAL)
- /*!
- \brief configure the system clock to 96M by PLL which selects HXTAL(MD/HD/XD:8M; CL:25M) as its clock source
- \param[in] none
- \param[out] none
- \retval none
- */
- static void system_clock_96m_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){
- }
- }
- /* 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(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
- /* select HXTAL/2 as clock source */
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_CFG0_PREDV0);
- /* CK_PLL = (CK_HXTAL/2) * 24 = 96 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= RCU_PLL_MUL24;
- #elif defined(GD32F10X_CL)
- /* CK_PLL = (CK_PREDIV0) * 24 = 96 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_PLL_MUL24);
- /* CK_PREDIV0 = (CK_HXTAL)/5 *8 /10 = 4 MHz */
- RCU_CFG1 &= ~(RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0);
- RCU_CFG1 |= (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 /* GD32F10X_MD and GD32F10X_HD and GD32F10X_XD */
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(RCU_SCSS_PLL != (RCU_CFG0 & RCU_CFG0_SCSS)){
- }
- }
- #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
- /*!
- \brief configure the system clock to 108M by PLL which selects HXTAL(MD/HD/XD:8M; CL:25M) 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){
- }
- }
- /* 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(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
- /* select HXTAL/2 as clock source */
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_CFG0_PREDV0);
- /* CK_PLL = (CK_HXTAL/2) * 27 = 108 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= RCU_PLL_MUL27;
- #elif defined(GD32F10X_CL)
- /* CK_PLL = (CK_PREDIV0) * 27 = 108 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_PLL_MUL27);
- /* CK_PREDIV0 = (CK_HXTAL)/5 *8 /10 = 4 MHz */
- RCU_CFG1 &= ~(RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0);
- RCU_CFG1 |= (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(0U == (RCU_CTL & RCU_CTL_PLL1STB)){
- }
- #endif /* GD32F10X_MD and GD32F10X_HD and GD32F10X_XD */
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(RCU_SCSS_PLL != (RCU_CFG0 & RCU_CFG0_SCSS)){
- }
- }
- #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){
- }
- }
- /* 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 |= RCU_PLL_MUL12;
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(RCU_SCSS_PLL != (RCU_CFG0 & RCU_CFG0_SCSS)){
- }
- }
- #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){
- }
- }
- /* 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 |= RCU_PLL_MUL18;
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(RCU_SCSS_PLL != (RCU_CFG0 & RCU_CFG0_SCSS)){
- }
- }
- #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){
- }
- }
- /* 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 |= RCU_PLL_MUL27;
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(RCU_SCSS_PLL != (RCU_CFG0 & RCU_CFG0_SCSS)){
- }
- }
- /*!
- \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 scss;
- uint32_t pllsel, predv0sel, pllmf, ck_src, idx, clk_exp;
- #ifdef GD32F10X_CL
- uint32_t predv0, predv1, pll1mf;
- #endif /* GD32F10X_CL */
- /* exponent of AHB, APB1 and APB2 clock divider */
- const uint8_t ahb_exp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
- scss = GET_BITS(RCU_CFG0, 2, 3);
- switch (scss)
- {
- /* 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 or IRC8M/2 */
- pllsel = (RCU_CFG0 & RCU_CFG0_PLLSEL);
- if(RCU_PLLSRC_IRC8M_DIV2 == pllsel){
- /* PLL clock source is IRC8M/2 */
- ck_src = IRC8M_VALUE / 2U;
- }else{
- /* PLL clock source is HXTAL */
- ck_src = HXTAL_VALUE;
-
- #if (defined(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_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(GD32F10X_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 /* GD32F10X_MD and GD32F10X_HD and GD32F10X_XD */
- }
- /* PLL multiplication factor */
- pllmf = GET_BITS(RCU_CFG0, 18, 21);
- if((RCU_CFG0 & RCU_CFG0_PLLMF_4)){
- pllmf |= 0x10U;
- }
- if(pllmf >= 15U){
- pllmf += 1U;
- }else{
- pllmf += 2U;
- }
- SystemCoreClock = ck_src * pllmf;
- #ifdef GD32F10X_CL
- if(15U == pllmf){
- /* PLL source clock multiply by 6.5 */
- SystemCoreClock = ck_src * 6U + ck_src / 2U;
- }
- #endif /* GD32F10X_CL */
- break;
- /* IRC8M is selected as CK_SYS */
- default:
- SystemCoreClock = IRC8M_VALUE;
- break;
- }
- /* calculate AHB clock frequency */
- idx = GET_BITS(RCU_CFG0, 4, 7);
- clk_exp = ahb_exp[idx];
- SystemCoreClock = SystemCoreClock >> clk_exp;
- }
- #endif
|