Skip to content

Commit 5439ef2

Browse files
committed
[bsp] GD32F4xx: Add the ability to manually configure HXTAL clock parameters
1 parent 3fdb940 commit 5439ef2

File tree

1 file changed

+85
-3
lines changed
  • bsp/gd32/arm/libraries/GD32F4xx_Firmware_Library/CMSIS/GD/GD32F4xx/Source

1 file changed

+85
-3
lines changed

bsp/gd32/arm/libraries/GD32F4xx_Firmware_Library/CMSIS/GD/GD32F4xx/Source/system_gd32f4xx.c

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
/* This file refers the CMSIS standard, some adjustments are made according to GigaDevice chips */
3535

3636
#include "gd32f4xx.h"
37+
#include "rtconfig.h"
3738

3839
/* system frequency define */
3940
#define __IRC16M (IRC16M_VALUE) /* internal 16 MHz RC oscillator frequency */
@@ -79,7 +80,10 @@
7980
#define SEL_PLLP 0x02U
8081

8182
/* 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)
8387
uint32_t SystemCoreClock = __SYSTEM_CLOCK_IRC16M;
8488
static void system_clock_16m_irc16m(void);
8589
#elif defined (__SYSTEM_CLOCK_HXTAL)
@@ -179,7 +183,9 @@ void SystemInit (void)
179183
*/
180184
static void system_clock_config(void)
181185
{
182-
#ifdef __SYSTEM_CLOCK_IRC16M
186+
#ifdef BSP_GD32_HXTAL_CLOCK_CFG
187+
system_clock_config_hxtal();
188+
#elif defined (__SYSTEM_CLOCK_IRC16M)
183189
system_clock_16m_irc16m();
184190
#elif defined (__SYSTEM_CLOCK_HXTAL)
185191
system_clock_hxtal();
@@ -210,7 +216,83 @@ static void system_clock_config(void)
210216
#endif /* __SYSTEM_CLOCK_IRC16M */
211217
}
212218

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)
214296
/*!
215297
\brief configure the system clock to 16M by IRC16M
216298
\param[in] none

0 commit comments

Comments
 (0)