/*! \file system_gd32e23x.c \brief CMSIS Cortex-M23 Device Peripheral Access Layer Source File for GD32E23x Device Series */ /* Copyright (c) 2012 ARM LIMITED Copyright (c) 2025, GigaDevice Semiconductor Inc. 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 "gd32e23x.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 */ //#define __SYSTEM_CLOCK_8M_HXTAL (__HXTAL) //#define __SYSTEM_CLOCK_8M_IRC8M (__IRC8M) // #define __SYSTEM_CLOCK_72M_PLL_HXTAL (uint32_t)(72000000) #define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2 (uint32_t)(72000000) /* The following is to prevent Vcore fluctuations caused by frequency switching. It is strongly recommended to include it to avoid issues caused by self-removal. */ #define RCU_MODIFY(__delay) do{ \ volatile uint32_t i,reg; \ if(0 != __delay){ \ reg = RCU_CFG0; \ reg &= ~(RCU_CFG0_AHBPSC); \ /* CK_AHB = SYSCLK/2 */ \ reg |= RCU_AHB_CKSYS_DIV2; \ RCU_CFG0 = reg; \ for(i=0; i<__delay; i++){ \ } \ reg = RCU_CFG0; \ reg &= ~(RCU_CFG0_AHBPSC); \ reg |= RCU_AHB_CKSYS_DIV4; \ /* CK_AHB = SYSCLK/4 */ \ RCU_CFG0 = reg; \ for(i=0; i<__delay; i++){ \ } \ } \ }while(0) #define SEL_IRC8M 0x00 #define SEL_HXTAL 0x01 #define SEL_PLL 0x02 /* set the system clock frequency and declare the system clock configuration function */ #ifdef __SYSTEM_CLOCK_8M_HXTAL uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_HXTAL; static void system_clock_8m_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_72M_PLL_IRC8M_DIV2) uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2; static void system_clock_72m_irc8m(void); #else uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_IRC8M; static void system_clock_8m_irc8m(void); #endif /* __SYSTEM_CLOCK_8M_HXTAL */ /* configure the system clock */ static void system_clock_config(void); /* software delay to prevent the impact of Vcore fluctuations. It is strongly recommended to include it to avoid issues caused by self-removal. */ static void _soft_delay_(uint32_t time) { __IO uint32_t i; for(i=0; i> 1) * pllmf; } 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 >>= clk_exp; } #ifdef __FIRMWARE_VERSION_DEFINE /*! \brief get firmware version \param[in] none \param[out] none \retval firmware version */ uint32_t gd32e23x_firmware_version_get(void) { return __GD32E23x_STDPERIPH_VERSION; } #endif /* __FIRMWARE_VERSION_DEFINE */