|
34 | 34 | /* This file refers the CMSIS standard, some adjustments are made according to GigaDevice chips */ |
35 | 35 |
|
36 | 36 | #include "gd32f4xx.h" |
| 37 | +#include "rtconfig.h" |
37 | 38 |
|
38 | 39 | /* system frequency define */ |
39 | 40 | #define __IRC16M (IRC16M_VALUE) /* internal 16 MHz RC oscillator frequency */ |
|
79 | 80 | #define SEL_PLLP 0x02U |
80 | 81 |
|
81 | 82 | /* set the system clock frequency and declare the system clock configuration function */ |
82 | | -#ifdef __SYSTEM_CLOCK_IRC16M |
| 83 | +#ifdef BSP_GD32_HXTAL_CLOCK_CFG |
| 84 | +uint32_t SystemCoreClock = BSP_GD32_SYSTEM_CLOCK; |
| 85 | +static void system_clock_config_hxtal(void); |
| 86 | +#elif defined (__SYSTEM_CLOCK_IRC16M) |
83 | 87 | uint32_t SystemCoreClock = __SYSTEM_CLOCK_IRC16M; |
84 | 88 | static void system_clock_16m_irc16m(void); |
85 | 89 | #elif defined (__SYSTEM_CLOCK_HXTAL) |
@@ -179,7 +183,9 @@ void SystemInit (void) |
179 | 183 | */ |
180 | 184 | static void system_clock_config(void) |
181 | 185 | { |
182 | | -#ifdef __SYSTEM_CLOCK_IRC16M |
| 186 | +#ifdef BSP_GD32_HXTAL_CLOCK_CFG |
| 187 | + system_clock_config_hxtal(); |
| 188 | +#elif defined (__SYSTEM_CLOCK_IRC16M) |
183 | 189 | system_clock_16m_irc16m(); |
184 | 190 | #elif defined (__SYSTEM_CLOCK_HXTAL) |
185 | 191 | system_clock_hxtal(); |
@@ -210,7 +216,83 @@ static void system_clock_config(void) |
210 | 216 | #endif /* __SYSTEM_CLOCK_IRC16M */ |
211 | 217 | } |
212 | 218 |
|
213 | | -#ifdef __SYSTEM_CLOCK_IRC16M |
| 219 | +#ifdef BSP_GD32_HXTAL_CLOCK_CFG |
| 220 | +/*! |
| 221 | + \brief configure the system clock to BSP_GD32_SYSTEM_CLOCK |
| 222 | + by PLL which selects HXTAL as its clock source |
| 223 | + \param[in] none |
| 224 | + \param[out] none |
| 225 | + \retval none |
| 226 | +*/ |
| 227 | +static void system_clock_config_hxtal(void) |
| 228 | +{ |
| 229 | + uint32_t timeout = 0U; |
| 230 | + uint32_t stab_flag = 0U; |
| 231 | + |
| 232 | + /* enable HXTAL */ |
| 233 | + RCU_CTL |= RCU_CTL_HXTALEN; |
| 234 | + |
| 235 | + /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */ |
| 236 | + do |
| 237 | + { |
| 238 | + timeout++; |
| 239 | + stab_flag = (RCU_CTL & RCU_CTL_HXTALSTB); |
| 240 | + } |
| 241 | + while ((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout)); |
| 242 | + |
| 243 | + /* if fail */ |
| 244 | + if (0U == (RCU_CTL & RCU_CTL_HXTALSTB)) |
| 245 | + { |
| 246 | + while (1) |
| 247 | + { |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + RCU_APB1EN |= RCU_APB1EN_PMUEN; |
| 252 | + PMU_CTL |= PMU_CTL_LDOVS; |
| 253 | + |
| 254 | + /* HXTAL is stable */ |
| 255 | + /* AHB = SYSCLK */ |
| 256 | + RCU_CFG0 |= RCU_AHB_CKSYS_DIV1; |
| 257 | + /* APB2 = AHB/2 */ |
| 258 | + RCU_CFG0 |= RCU_APB2_CKAHB_DIV2; |
| 259 | + /* APB1 = AHB/4 */ |
| 260 | + RCU_CFG0 |= RCU_APB1_CKAHB_DIV4; |
| 261 | + |
| 262 | + /* Configure the main PLL */ |
| 263 | + RCU_PLL = (BSP_GD32_CLOCK_PSC | (BSP_GD32_CLOCK_PLL_N << 6U) | (((BSP_GD32_CLOCK_PLL_P >> 1U) - 1U) << 16U) | |
| 264 | + (RCU_PLLSRC_HXTAL) | (BSP_GD32_CLOCK_PLL_Q << 24U)); |
| 265 | + |
| 266 | + /* enable PLL */ |
| 267 | + RCU_CTL |= RCU_CTL_PLLEN; |
| 268 | + |
| 269 | + /* wait until PLL is stable */ |
| 270 | + while (0U == (RCU_CTL & RCU_CTL_PLLSTB)) |
| 271 | + { |
| 272 | + } |
| 273 | + |
| 274 | + /* Enable the high-drive to extend the clock frequency to 240 Mhz */ |
| 275 | + PMU_CTL |= PMU_CTL_HDEN; |
| 276 | + while (0U == (PMU_CS & PMU_CS_HDRF)) |
| 277 | + { |
| 278 | + } |
| 279 | + |
| 280 | + /* select the high-drive mode */ |
| 281 | + PMU_CTL |= PMU_CTL_HDS; |
| 282 | + while (0U == (PMU_CS & PMU_CS_HDSRF)) |
| 283 | + { |
| 284 | + } |
| 285 | + |
| 286 | + /* select PLL as system clock */ |
| 287 | + RCU_CFG0 &= ~RCU_CFG0_SCS; |
| 288 | + RCU_CFG0 |= RCU_CKSYSSRC_PLLP; |
| 289 | + |
| 290 | + /* wait until PLL is selected as system clock */ |
| 291 | + while (0U == (RCU_CFG0 & RCU_SCSS_PLLP)) |
| 292 | + { |
| 293 | + } |
| 294 | +} |
| 295 | +#elif defined (__SYSTEM_CLOCK_IRC16M) |
214 | 296 | /*! |
215 | 297 | \brief configure the system clock to 16M by IRC16M |
216 | 298 | \param[in] none |
|
0 commit comments