Skip to content

Commit b1ea70c

Browse files
Updated hardware.c so that the clock speed can be set to either 72,96 or 120Mhz, with the appropriate USB divider automatically selected. Build is currently set to 72Mhz
1 parent 4c39175 commit b1ea70c

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed
0 Bytes
Binary file not shown.

GD32F1/hardware.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,46 @@ void systemReset(void) {
106106
}
107107

108108
void setupCLK(void) {
109+
volatile int waitLoop;
109110
/* enable HSE */
110111
SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x00010001);
111112
while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */
112113

113114
/* enable flash prefetch buffer */
114115
SET_REG(FLASH_ACR, 0x00000012);
115116

116-
/* Configure PLL */
117-
// 0B100111010000010000000000
118-
// 0B101000010000010000000000
117+
119118
#define APB1PS 0B100
120119
#define APB1PS_SHIFT 8
121120
#define PLLSEL 0B1
122121
#define PLLSEL_SHIFT 16
123-
124-
#define PLLMF 10
125122
#define PLLMF_SHIFT 18
126-
#define USBPS 0B10
127123
#define USBPS_SHIFT 22
128124

125+
// set the speed option to 72Mhz as this is safe (as is 96Mhz)
126+
#define SPEED_72MHZ 1
127+
128+
#if defined(SPEED_72MHZ)
129+
#define PLLMF 6
130+
#define USBPS 0x00
131+
#elif defined(SPEED_96MHZ)
132+
#define PLLMF 8
133+
#define USBPS 0x03
134+
#elif defined(SPEED_120MHZ)
135+
#define PLLMF 10
136+
#define USBPS 0x02
137+
#endif
138+
139+
140+
141+
142+
129143
SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | (USBPS<< USBPS_SHIFT) | (PLLMF-2)<<PLLMF_SHIFT | (PLLSEL<<PLLSEL_SHIFT) | (APB1PS<<APB1PS_SHIFT) );// 0x001D0400); /* pll=108Mhz,APB1=36Mhz,AHB=72Mhz */
130-
// SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0B100111010000010000000000);
131-
// 987654321098765432109876543210
132-
SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x01000000); /* enable the pll */
144+
145+
SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x01000000); /* enable the pll */
133146
while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */
134147

148+
135149
/* Set SYSCLK as PLL */
136150
SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00000002);
137151
while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */

0 commit comments

Comments
 (0)