40
40
* value to your own configuration.
41
41
*
42
42
* 5. This file configures the system clock as follows:
43
- *=============================================================================
44
- *=============================================================================
45
- * System Clock source | HSI
46
43
*-----------------------------------------------------------------------------
47
- * SYSCLK(Hz) | 8000000
44
+ * System clock source | 1- PLL_HSE_EXTC | 3- PLL_HSI
45
+ * | (external 8 MHz clock) | (internal 8 MHz)
46
+ * | 2- PLL_HSE_XTAL |
47
+ * | (external 8 MHz xtal) |
48
48
*-----------------------------------------------------------------------------
49
- * HCLK(Hz) | 8000000
49
+ * SYSCLK(MHz) | 48 | 48
50
50
*-----------------------------------------------------------------------------
51
- * AHB Prescaler | 1
51
+ * AHBCLK (MHz) | 48 | 48
52
52
*-----------------------------------------------------------------------------
53
- * APB Prescaler | 1
54
- *-----------------------------------------------------------------------------
55
- * HSE Frequency(Hz) | NA
56
- *----------------------------------------------------------------------------
57
- * PLLMUL | NA
58
- *-----------------------------------------------------------------------------
59
- * PREDIV | NA
60
- *-----------------------------------------------------------------------------
61
- * Flash Latency(WS) | 0
62
- *-----------------------------------------------------------------------------
63
- * Prefetch Buffer | ON
53
+ * APBCLK (MHz) | 48 | 48
64
54
*-----------------------------------------------------------------------------
65
55
******************************************************************************
66
56
* @attention
129
119
* @{
130
120
*/
131
121
122
+ /* Select the clock sources (other than HSI) to start with (0=OFF, 1=ON) */
123
+ #define USE_PLL_HSE_EXTC (1) /* Use external clock */
124
+ #define USE_PLL_HSE_XTAL (1) /* Use external xtal */
125
+
132
126
/**
133
127
* @}
134
128
*/
135
129
136
130
/** @addtogroup STM32F0xx_System_Private_Variables
137
131
* @{
138
132
*/
139
- uint32_t SystemCoreClock = 8000000 ;
133
+
134
+ uint32_t SystemCoreClock = 48000000 ;
140
135
__I uint8_t AHBPrescTable [16 ] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 2 , 3 , 4 , 6 , 7 , 8 , 9 };
141
136
142
137
/**
@@ -147,7 +142,11 @@ __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}
147
142
* @{
148
143
*/
149
144
150
- static void SetSysClock (void );
145
+ #if (USE_PLL_HSE_XTAL != 0 ) || (USE_PLL_HSE_EXTC != 0 )
146
+ uint8_t SetSysClock_PLL_HSE (uint8_t bypass );
147
+ #endif
148
+
149
+ uint8_t SetSysClock_PLL_HSI (void );
151
150
152
151
/**
153
152
* @}
@@ -192,9 +191,6 @@ void SystemInit (void)
192
191
193
192
/* Disable all interrupts */
194
193
RCC -> CIR = 0x00000000 ;
195
-
196
- /* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
197
- SetSysClock ();
198
194
}
199
195
200
196
/**
@@ -277,30 +273,155 @@ void SystemCoreClockUpdate (void)
277
273
}
278
274
279
275
/**
280
- * @brief Configures the System clock frequency, AHB/APBx prescalers and Flash
281
- * settings.
282
- * @note This function should be called only once the RCC clock configuration
283
- * is reset to the default reset state (done in SystemInit() function).
276
+ * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
284
277
* @param None
285
278
* @retval None
286
279
*/
287
- static void SetSysClock (void )
280
+ void SetSysClock (void )
281
+ {
282
+ /* 1- Try to start with HSE and external clock */
283
+ #if USE_PLL_HSE_EXTC != 0
284
+ if (SetSysClock_PLL_HSE (1 ) == 0 )
285
+ #endif
286
+ {
287
+ /* 2- If fail try to start with HSE and external xtal */
288
+ #if USE_PLL_HSE_XTAL != 0
289
+ if (SetSysClock_PLL_HSE (0 ) == 0 )
290
+ #endif
291
+ {
292
+ /* 3- If fail start with HSI clock */
293
+ if (SetSysClock_PLL_HSI () == 0 )
294
+ {
295
+ while (1 )
296
+ {
297
+ // [TODO] Put something here to tell the user that a problem occured...
298
+ }
299
+ }
300
+ }
301
+ }
302
+
303
+ // Output clock on MCO pin (PA8) for debugging purpose
304
+ /*
305
+ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
306
+ GPIO_InitTypeDef GPIO_InitStructure;
307
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
308
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
309
+ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
310
+ GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
311
+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
312
+ GPIO_Init(GPIOA, &GPIO_InitStructure);
313
+ GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_0);
314
+ // Output clock on MCO pin
315
+ // Warning: only RCC_MCOPrescaler_1 is available on STM32F030x8 devices
316
+ RCC_MCOConfig(RCC_MCOSource_SYSCLK, RCC_MCOPrescaler_1);
317
+ */
318
+ }
319
+
320
+ #if (USE_PLL_HSE_XTAL != 0 ) || (USE_PLL_HSE_EXTC != 0 )
321
+ /******************************************************************************/
322
+ /* PLL (clocked by HSE) used as System clock source */
323
+ /******************************************************************************/
324
+ uint8_t SetSysClock_PLL_HSE (uint8_t bypass )
288
325
{
326
+ __IO uint32_t StartUpCounter = 0 ;
327
+ __IO uint32_t HSEStatus = 0 ;
328
+
329
+ /* Bypass HSE: can be done only if HSE is OFF */
330
+ RCC -> CR &= ((uint32_t )~RCC_CR_HSEON ); /* To be sure HSE is OFF */
331
+ if (bypass != 0 )
332
+ {
333
+ RCC -> CR |= ((uint32_t )RCC_CR_HSEBYP );
334
+ }
335
+ else
336
+ {
337
+ RCC -> CR &= ((uint32_t )~RCC_CR_HSEBYP );
338
+ }
339
+
340
+ /* Enable HSE */
341
+ RCC -> CR |= ((uint32_t )RCC_CR_HSEON );
342
+
343
+ /* Wait till HSE is ready */
344
+ do
345
+ {
346
+ HSEStatus = RCC -> CR & RCC_CR_HSERDY ;
347
+ StartUpCounter ++ ;
348
+ } while ((HSEStatus == 0 ) && (StartUpCounter != HSE_STARTUP_TIMEOUT ));
349
+
350
+ /* Check if HSE has started correctly */
351
+ if ((RCC -> CR & RCC_CR_HSERDY ) != RESET )
352
+ {
353
+ /* Enable Prefetch Buffer */
354
+ FLASH -> ACR |= FLASH_ACR_PRFTBE ;
355
+
356
+ /* Enable Prefetch Buffer and set Flash Latency */
357
+ FLASH -> ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY ;
358
+
359
+ /* PLL configuration
360
+ PLLCLK = 48 MHz (xtal 8 MHz * 6) */
361
+ RCC -> CFGR &= (uint32_t )((uint32_t )~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL ));
362
+ RCC -> CFGR |= (uint32_t )(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6
363
+ | RCC_CFGR_HPRE_DIV1 /* HCLK = 48 MHz */
364
+ | RCC_CFGR_PPRE_DIV1 ); /* PCLK = 48 MHz */
365
+
366
+ /* Enable PLL */
367
+ RCC -> CR |= RCC_CR_PLLON ;
368
+
369
+ /* Wait till PLL is ready */
370
+ while ((RCC -> CR & RCC_CR_PLLRDY ) == 0 )
371
+ {
372
+ }
373
+
374
+ /* Select PLL as system clock source */
375
+ RCC -> CFGR &= (uint32_t )((uint32_t )~(RCC_CFGR_SW ));
376
+ RCC -> CFGR |= (uint32_t )RCC_CFGR_SW_PLL ;
377
+
378
+ /* Wait till PLL is used as system clock source */
379
+ while ((RCC -> CFGR & (uint32_t )RCC_CFGR_SWS ) != (uint32_t )RCC_CFGR_SWS_PLL )
380
+ {
381
+ }
382
+
383
+ return 1 ; // OK
384
+ }
385
+ else
386
+ {
387
+ return 0 ; // FAIL
388
+ }
389
+ }
390
+ #endif
391
+
289
392
/******************************************************************************/
290
- /* HSI used as System clock source */
393
+ /* PLL (clocked by HSI) used as System clock source */
291
394
/******************************************************************************/
395
+ uint8_t SetSysClock_PLL_HSI (void )
396
+ {
397
+ /* Enable Prefetch Buffer and set Flash Latency */
398
+ FLASH -> ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY ;
292
399
293
- /* At this stage the HSI is already enabled and used as System clock source */
400
+ /* PLL configuration
401
+ PLLCLK = 48 MHz ((HSI 8 MHz / 2) * 12) */
402
+ RCC -> CFGR &= (uint32_t )((uint32_t )~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL ));
403
+ RCC -> CFGR |= (uint32_t )(RCC_CFGR_PLLSRC_HSI_Div2 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL12
404
+ | RCC_CFGR_HPRE_DIV1 /* HCLK = 48 MHz */
405
+ | RCC_CFGR_PPRE_DIV1 ); /* PCLK = 48 MHz */
294
406
295
- /* Enable Prefetch Buffer and Flash 0 wait state */
296
- FLASH -> ACR = FLASH_ACR_PRFTBE ;
407
+ /* Enable PLL */
408
+ RCC -> CR |= RCC_CR_PLLON ;
297
409
298
- /* HCLK = SYSCLK / 1 */
299
- RCC -> CFGR |= (uint32_t )RCC_CFGR_HPRE_DIV1 ;
410
+ /* Wait till PLL is ready */
411
+ while ((RCC -> CR & RCC_CR_PLLRDY ) == 0 )
412
+ {
413
+ }
414
+
415
+ /* Select PLL as system clock source */
416
+ RCC -> CFGR &= (uint32_t )((uint32_t )~(RCC_CFGR_SW ));
417
+ RCC -> CFGR |= (uint32_t )RCC_CFGR_SW_PLL ;
300
418
301
- /* PCLK = HCLK / 1 */
302
- RCC -> CFGR |= (uint32_t )RCC_CFGR_PPRE_DIV1 ;
419
+ /* Wait till PLL is used as system clock source */
420
+ while ((RCC -> CFGR & (uint32_t )RCC_CFGR_SWS ) != (uint32_t )RCC_CFGR_SWS_PLL )
421
+ {
422
+ }
303
423
424
+ return 1 ; // OK
304
425
}
305
426
306
427
/**
0 commit comments