Skip to content

Commit 14e2e13

Browse files
committed
board: gd32e103: change cpu freq to 120M
change cpu freq to 120M Signed-off-by: YuLong Yao <[email protected]>
1 parent 0e26c9c commit 14e2e13

File tree

6 files changed

+183
-8
lines changed

6 files changed

+183
-8
lines changed

boards/arm/gd32e103v_eval/gd32e103v_eval.dts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
};
3333
};
3434

35-
&rtc {
36-
status = "okay";
37-
};
38-
3935
&flash0 {
4036
/*
4137
* For more information, see:
@@ -54,9 +50,16 @@
5450
};
5551
};
5652

53+
&pll {
54+
status = "okay";
55+
clocks = <&clk_hsi>;
56+
prediv = <2>;
57+
mul = <27>;
58+
};
59+
5760
&rcc {
5861
clocks = <&pll>;
59-
clock-frequency = <DT_FREQ_M(72)>;
62+
clock-frequency = <DT_FREQ_M(108)>;
6063
ahb-prescaler = <1>;
6164
apb1-prescaler = <2>;
6265
apb2-prescaler = <1>;

boards/arm/gd32e103v_eval/gd32e103v_eval_defconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
CONFIG_SOC_SERIES_GD32E1X=y
44
CONFIG_SOC_GD32E103VB=y
5-
CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=8000000
5+
CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=120000000
66

77
# Enable MPU
88
CONFIG_ARM_MPU=y

drivers/clock_control/clock_gd32e1.c

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,123 @@
1111
#include <sys/util.h>
1212
#include <drivers/clock_control/gd32_clock_control.h>
1313
#include <gd32e10x_rcu.h>
14+
#include <gd32e10x.h>
15+
1416

1517
#define RCC_BASE DT_REG_ADDR_BY_IDX(DT_NODELABEL(rcc), 0)
1618
#undef RCU_REG_VAL
1719
#define RCU_REG_VAL(periph) (REG32(RCC_BASE | periph ))
1820

19-
// todo: work here later
21+
static inline int gd32_clock_reset()
22+
{
23+
/* reset the RCU clock configuration to the default reset state */
24+
/* Set IRC8MEN bit */
25+
RCU_CTL |= RCU_CTL_IRC8MEN;
26+
27+
/* Reset CFG0 and CFG1 registers */
28+
RCU_CFG0 = 0x00000000U;
29+
RCU_CFG1 = 0x00000000U;
30+
31+
/* Reset HXTALEN, CKMEN, PLLEN, PLL1EN and PLL2EN bits */
32+
RCU_CTL &= ~(RCU_CTL_PLLEN |RCU_CTL_PLL1EN | RCU_CTL_PLL2EN | RCU_CTL_CKMEN | RCU_CTL_HXTALEN);
33+
/* disable all interrupts */
34+
RCU_INT = 0x00ff0000U;
35+
36+
/* reset HXTALBPS bit */
37+
RCU_CTL &= ~(RCU_CTL_HXTALBPS);
38+
39+
return 0;
40+
}
41+
42+
static inline void system_clock_120m_hxtal(void)
43+
{
44+
uint32_t timeout = 0U;
45+
uint32_t stab_flag = 0U;
46+
47+
/* enable HXTAL */
48+
RCU_CTL |= RCU_CTL_HXTALEN;
49+
50+
/* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
51+
do{
52+
timeout++;
53+
stab_flag = (RCU_CTL & RCU_CTL_HXTALSTB);
54+
}while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
55+
56+
/* if fail */
57+
if(0U == (RCU_CTL & RCU_CTL_HXTALSTB)){
58+
while(1){
59+
}
60+
}
61+
62+
FMC_WS = (FMC_WS & (~FMC_WS_WSCNT)) | FMC_WAIT_STATE_3;
63+
64+
/* HXTAL is stable */
65+
/* AHB = SYSCLK */
66+
RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
67+
/* APB2 = AHB/1 */
68+
RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
69+
/* APB1 = AHB/2 */
70+
RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
71+
72+
/* CK_PLL = (CK_PREDIV0) * 30 = 120 MHz */
73+
RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
74+
RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_PLL_MUL30);
75+
76+
RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0);
77+
#ifdef HXTAL_VALUE_8M
78+
/* CK_PREDIV0 = (CK_HXTAL)/2 *10 /10 = 4 MHz */
79+
RCU_CFG1 |= (RCU_PLLPRESRC_HXTAL | RCU_PREDV0SRC_CKPLL1 | RCU_PLL1_MUL10 | RCU_PREDV1_DIV2 | RCU_PREDV0_DIV10);
80+
#elif defined (HXTAL_VALUE_25M)
81+
/* CK_PREDIV0 = (CK_HXTAL)/5 *8/10 = 4 MHz */
82+
RCU_CFG1 |= (RCU_PLLPRESRC_HXTAL | RCU_PREDV0SRC_CKPLL1 | RCU_PLL1_MUL8 | RCU_PREDV1_DIV5 | RCU_PREDV0_DIV10);
83+
#endif
84+
85+
/* enable PLL1 */
86+
RCU_CTL |= RCU_CTL_PLL1EN;
87+
/* wait till PLL1 is ready */
88+
while((RCU_CTL & RCU_CTL_PLL1STB) == 0U){
89+
}
90+
91+
/* enable PLL */
92+
RCU_CTL |= RCU_CTL_PLLEN;
93+
94+
/* wait until PLL is stable */
95+
while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
96+
}
97+
98+
/* select PLL as system clock */
99+
RCU_CFG0 &= ~RCU_CFG0_SCS;
100+
RCU_CFG0 |= RCU_CKSYSSRC_PLL;
101+
102+
/* wait until PLL is selected as system clock */
103+
while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
104+
}
105+
}
106+
107+
static inline int gd32_clock_init()
108+
{
109+
system_clock_120m_hxtal();
110+
111+
return 0;
112+
}
113+
114+
/**
115+
* @brief Initialize clocks for the gd32
116+
*
117+
* This routine is called to enable and configure the clocks and PLL
118+
* of the soc on the board. It depends on the board definition.
119+
* This function is called on the startup and also to restore the config
120+
* when exiting for low power mode.
121+
*
122+
* @param dev clock device struct
123+
*
124+
* @return 0
125+
*/
20126
int gd32_clock_control_init(const struct device *dev)
21127
{
128+
gd32_clock_reset();
129+
gd32_clock_init();
130+
22131
return 0;
23132
}
24133

dts/arm/gigadevice/e1/gd32e1.dtsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
pll: pll {
6565
#clock-cells = <0>;
66-
compatible = "st,stm32g4-pll-clock";
66+
compatible = "gd,gd32e103-pll-clock";
6767
status = "disabled";
6868
};
6969
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) 2021, Linaro ltd
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: GD32 HSE Clock
5+
6+
compatible: "gd,gd32-hse-clock"
7+
8+
include: [fixed-clock.yaml]
9+
10+
properties:
11+
hse-bypass:
12+
type: boolean
13+
required: false
14+
description: |
15+
HSE crystal oscillator bypass
16+
Set to the property to by-pass the oscillator with an external clock.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) 2021, Linaro ltd
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
Main PLL node binding for Connectivity line devices (STM32F105/STM32F107)
6+
7+
Takes one of clk_hse, pll2 or clk_hsi as input clock.
8+
When clk_hsi is used a fixed prescaler is applied. When input clock is hse or
9+
pll2, configurable prescaler is used.
10+
11+
Up to 2 output clocks could be supported and for each output clock, the
12+
frequency can be computed with the following formula:
13+
14+
f(PLLCLK) = f(PLLIN) x PLLMUL --> SYSCLK (System Clock)
15+
f(USBCLK) = f(PLLCLK) * 2 / USBPRE --> USB
16+
17+
with, depending on the case:
18+
f(PLLIN) = f(input_clk) / 2 if input_clk = clk_hsi
19+
f(PLLIN) = f(input_clk) / PREDIV if input_clk = clk_hse or pll2
20+
21+
The PLL output frequency must not exceed 72 MHz.
22+
23+
24+
compatible: "gd,gd32e103-pll-clock"
25+
26+
include: [clock-controller.yaml, base.yaml]
27+
28+
properties:
29+
"#clock-cells":
30+
const: 0
31+
32+
clocks:
33+
required: true
34+
35+
mul:
36+
type: int
37+
required: true
38+
description: |
39+
Configurable mul
40+
Valid range: 2 - 31
41+
42+
prediv:
43+
type: int
44+
required: false
45+
description: |
46+
Configurable prescaler
47+
Valid range: 1 - 16

0 commit comments

Comments
 (0)