@@ -232,7 +232,12 @@ void handle_command(unsigned char command) {
232
232
}
233
233
234
234
#ifdef GD32F103
235
- void rcc_clock_setup_in_hse_12mhz_out_120mhz (void ) {
235
+ #define RCC_GCFGR_ADCPS_DIV12 ((uint32_t)0x10004000)
236
+ #define RCC_GCFGR_ADCPS_DIV16 ((uint32_t)0x1000C000)
237
+ #define RCC_GCFGR_USBPS_Div2_5 ((uint32_t)0x00800000)
238
+ #define RCC_GCFGR_USBPS_Div2 ((uint32_t)0x00C00000)
239
+
240
+ static void rcc_clock_setup_in_hse_12mhz_out_96mhz (void ) {
236
241
/* Enable internal high-speed oscillator. */
237
242
rcc_osc_on (RCC_HSI );
238
243
rcc_wait_for_osc_ready (RCC_HSI );
@@ -247,16 +252,68 @@ void rcc_clock_setup_in_hse_12mhz_out_120mhz(void) {
247
252
248
253
/*
249
254
* Set prescalers for AHB, ADC, ABP1, ABP2.
250
- * Do this before touching the PLL (TODO: why?).
255
+ * Do this before touching the PLL
256
+ */
257
+ rcc_set_hpre (RCC_CFGR_HPRE_SYSCLK_NODIV ); /* Set. 96MHz Max. 108MHz */
258
+ rcc_set_adcpre (RCC_CFGR_ADCPRE_PCLK2_DIV8 ); /* Set. 12MHz Max. 14MHz */
259
+ rcc_set_ppre1 (RCC_CFGR_PPRE1_HCLK_DIV2 ); /* Set. 48MHz Max. 54MHz */
260
+ rcc_set_ppre2 (RCC_CFGR_PPRE2_HCLK_NODIV ); /* Set. 96MHz Max. 108MHz */
261
+ RCC_CFGR |= RCC_GCFGR_USBPS_Div2 ; /* USB Set. 48MHz Max. 48MHz */
262
+
263
+ /* GD32 has 0-wait-state flash, do not touch anything! */
264
+
265
+ /*
266
+ * Set the PLL multiplication factor to 10.
267
+ * 12MHz (external) * 8 (multiplier) = 96MHz
268
+ */
269
+ rcc_set_pll_multiplication_factor (RCC_CFGR_PLLMUL_PLL_CLK_MUL8 );
270
+
271
+ /* Select HSE as PLL source. */
272
+ rcc_set_pll_source (RCC_CFGR_PLLSRC_HSE_CLK );
273
+
274
+ /*
275
+ * External frequency undivided before entering PLL
276
+ * (only valid/needed for HSE).
277
+ */
278
+ rcc_set_pllxtpre (RCC_CFGR_PLLXTPRE_HSE_CLK );
279
+
280
+ /* Enable PLL oscillator and wait for it to stabilize. */
281
+ rcc_osc_on (RCC_PLL );
282
+ rcc_wait_for_osc_ready (RCC_PLL );
283
+
284
+ /* Select PLL as SYSCLK source. */
285
+ rcc_set_sysclk_source (RCC_CFGR_SW_SYSCLKSEL_PLLCLK );
286
+
287
+ /* Set the peripheral clock frequencies used */
288
+ rcc_ahb_frequency = 96000000 ;
289
+ rcc_apb1_frequency = 48000000 ;
290
+ rcc_apb2_frequency = 96000000 ;
291
+ }
292
+
293
+ static void rcc_clock_setup_in_hse_12mhz_out_120mhz (void ) {
294
+ /* Enable internal high-speed oscillator. */
295
+ rcc_osc_on (RCC_HSI );
296
+ rcc_wait_for_osc_ready (RCC_HSI );
297
+
298
+ /* Select HSI as SYSCLK source. */
299
+ rcc_set_sysclk_source (RCC_CFGR_SW_SYSCLKSEL_HSICLK );
300
+
301
+ /* Enable external high-speed oscillator 12MHz. */
302
+ rcc_osc_on (RCC_HSE );
303
+ rcc_wait_for_osc_ready (RCC_HSE );
304
+ rcc_set_sysclk_source (RCC_CFGR_SW_SYSCLKSEL_HSECLK );
305
+
306
+ /*
307
+ * Set prescalers for AHB, ADC, ABP1, ABP2.
308
+ * Do this before touching the PLL
251
309
*/
252
310
rcc_set_hpre (RCC_CFGR_HPRE_SYSCLK_NODIV ); /* Set. 120MHz Max. 108MHz */
253
- RCC_CFGR = (RCC_CFGR & ~RCC_CFGR_ADCPRE ) | (( uint32_t ) 0x10004000 ) ; /* ADC Set. 12MHz Max. 14MHz */
311
+ RCC_CFGR = (RCC_CFGR & ~RCC_CFGR_ADCPRE ) | RCC_GCFGR_ADCPS_DIV12 ; /* ADC Set. 10MHz Max. 14MHz */
254
312
rcc_set_ppre1 (RCC_CFGR_PPRE1_HCLK_DIV2 ); /* Set. 60MHz Max. 54MHz */
255
313
rcc_set_ppre2 (RCC_CFGR_PPRE2_HCLK_NODIV ); /* Set. 120MHz Max. 108MHz */
256
- RCC_CFGR |= (( uint32_t ) 0x00800000 ) ; /* USB Set. 48MHz Max. 48MHz */
314
+ RCC_CFGR |= RCC_GCFGR_USBPS_Div2_5 ; /* USB Set. 48MHz Max. 48MHz */
257
315
258
- /* GD32 has 0-wait-state flash */
259
- flash_set_ws (FLASH_ACR_LATENCY_0WS );
316
+ /* GD32 has 0-wait-state flash, do not touch anything! */
260
317
261
318
/*
262
319
* Set the PLL multiplication factor to 10.
@@ -319,15 +376,24 @@ int main(void) {
319
376
320
377
usbcdc_init ();
321
378
spi_setup (SPI_DEFAULT_CLOCK );
322
- /* Wait 500ms for USB setup to complete before trying to send anything. */
323
- /* FIXME: in ST's USB library there is some way to tell whether USB has finished initialization. */
324
- for (i = 0 ; i < 10000000 ; i ++ ) {
325
- asm("nop" );
326
- }
327
- LED_IDLE ();
328
379
329
380
/* The loop. */
330
381
while (true) {
382
+ /* Wait and blink if USB is not ready. */
383
+ LED_IDLE ();
384
+ while (!usb_ready ) {
385
+ LED_DISABLE ();
386
+ for (i = 0 ; i < 1000000 ; i ++ ) {
387
+ asm("nop" );
388
+ }
389
+ LED_ENABLE ();
390
+ for (i = 0 ; i < 1000000 ; i ++ ) {
391
+ asm("nop" );
392
+ }
393
+ }
394
+
395
+ /* Actual thing */
396
+ /* TODO: we are blocked here, hence no knowledge about USB bet reset. */
331
397
handle_command (usbcdc_getc ());
332
398
}
333
399
0 commit comments