40
40
* value to your own configuration.
41
41
*
42
42
* 5. This file configures the system clock as follows:
43
- *=============================================================================
44
- * Supported STM32F30x device
45
43
*-----------------------------------------------------------------------------
46
- * System Clock source | PLL(HSI)
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) |
47
48
*-----------------------------------------------------------------------------
48
- * SYSCLK(Hz) | 64000000
49
+ * SYSCLK(MHz) | 72 | 64
49
50
*-----------------------------------------------------------------------------
50
- * HCLK(Hz) | 64000000
51
+ * AHBCLK (MHz) | 72 | 64
51
52
*-----------------------------------------------------------------------------
52
- * AHB Prescaler | 1
53
+ * APB1CLK (MHz) | 36 | 32
53
54
*-----------------------------------------------------------------------------
54
- * APB2 Prescaler | 1
55
+ * APB2CLK (MHz) | 72 | 64
55
56
*-----------------------------------------------------------------------------
56
- * APB1 Prescaler (Max = 36MHz) | 2 (SPI, ...)
57
- *-----------------------------------------------------------------------------
58
- * HSE Frequency(Hz) | 8000000
59
- *----------------------------------------------------------------------------
60
- * PLLMUL | 16
61
- *-----------------------------------------------------------------------------
62
- * PREDIV | 2
63
- *-----------------------------------------------------------------------------
64
- * USB Clock | DISABLE
65
- *-----------------------------------------------------------------------------
66
- * Flash Latency(WS) | 2
67
- *-----------------------------------------------------------------------------
68
- * Prefetch Buffer | OFF
69
- *-----------------------------------------------------------------------------
70
- *=============================================================================
57
+ * USB capable (48 MHz precise clock) | YES | NO
58
+ *-----------------------------------------------------------------------------
71
59
******************************************************************************
72
60
* @attention
73
61
*
97
85
*
98
86
******************************************************************************
99
87
*/
88
+
100
89
/** @addtogroup CMSIS
101
90
* @{
102
91
*/
126
115
/** @addtogroup STM32F30x_System_Private_Defines
127
116
* @{
128
117
*/
118
+
129
119
/*!< Uncomment the following line if you need to relocate your vector Table in
130
120
Internal SRAM. */
131
121
/* #define VECT_TAB_SRAM */
139
129
* @{
140
130
*/
141
131
132
+ /* Select the clock sources (other than HSI) to start with (0=OFF, 1=ON) */
133
+ #define USE_PLL_HSE_EXTC (1) /* Use external clock */
134
+ #define USE_PLL_HSE_XTAL (1) /* Use external xtal */
135
+
142
136
/**
143
137
* @}
144
138
*/
147
141
* @{
148
142
*/
149
143
150
- uint32_t SystemCoreClock = 64000000 ;
144
+ uint32_t SystemCoreClock = 64000000 ; /* Default with HSI. Will be updated if HSE is used */
151
145
152
- __I uint8_t AHBPrescTable [16 ] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 2 , 3 , 4 , 6 , 7 , 8 , 9 };
146
+ __I uint8_t AHBPrescTable [16 ] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 2 , 3 , 4 , 6 , 7 , 8 , 9 };
153
147
154
148
/**
155
149
* @}
161
155
162
156
void SetSysClock (void );
163
157
158
+ #if (USE_PLL_HSE_XTAL != 0 ) || (USE_PLL_HSE_EXTC != 0 )
159
+ uint8_t SetSysClock_PLL_HSE (uint8_t bypass );
160
+ #endif
161
+
162
+ uint8_t SetSysClock_PLL_HSI (void );
163
+
164
164
/**
165
165
* @}
166
166
*/
@@ -208,31 +208,16 @@ void SystemInit(void)
208
208
/* Disable all interrupts */
209
209
RCC -> CIR = 0x00000000 ;
210
210
211
- /* Configure the System clock source, PLL Multiplier and Divider factors,
212
- AHB/APBx prescalers and Flash settings ----------------------------------*/
213
- SetSysClock ();
214
-
211
+ /* Configure the Vector Table location add offset address ------------------*/
215
212
#ifdef VECT_TAB_SRAM
216
- SCB -> VTOR = SRAM_BASE | VECT_TAB_OFFSET ; /* Vector Table Relocation in Internal SRAM. */
213
+ SCB -> VTOR = SRAM_BASE | VECT_TAB_OFFSET ; /* Vector Table Relocation in Internal SRAM */
217
214
#else
218
- SCB -> VTOR = FLASH_BASE | VECT_TAB_OFFSET ; /* Vector Table Relocation in Internal FLASH. */
215
+ SCB -> VTOR = FLASH_BASE | VECT_TAB_OFFSET ; /* Vector Table Relocation in Internal FLASH */
219
216
#endif
220
217
221
- // ADDED FOR MBED DEBUGGING PURPOSE
222
- /*
223
- // Enable GPIOA clock
224
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
225
- // Configure MCO pin (PA8)
226
- GPIO_InitTypeDef GPIO_InitStructure;
227
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
228
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
229
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
230
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
231
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
232
- GPIO_Init(GPIOA, &GPIO_InitStructure);
233
- // Select the clock to output
234
- RCC_MCOConfig(RCC_MCOSource_SYSCLK, RCC_MCOPrescaler_1);
235
- */
218
+ /* Configure the System clock source, PLL Multiplier and Divider factors,
219
+ AHB/APBx prescalers and Flash settings */
220
+ SetSysClock ();
236
221
}
237
222
238
223
/**
@@ -325,31 +310,137 @@ void SystemCoreClockUpdate (void)
325
310
*/
326
311
void SetSysClock (void )
327
312
{
313
+ /* 1- Try to start with HSE and external clock */
314
+ #if USE_PLL_HSE_EXTC != 0
315
+ if (SetSysClock_PLL_HSE (1 ) == 0 )
316
+ #endif
317
+ {
318
+ /* 2- If fail try to start with HSE and external xtal */
319
+ #if USE_PLL_HSE_XTAL != 0
320
+ if (SetSysClock_PLL_HSE (0 ) == 0 )
321
+ #endif
322
+ {
323
+ /* 3- If fail start with HSI clock */
324
+ if (SetSysClock_PLL_HSI () == 0 )
325
+ {
326
+ while (1 )
327
+ {
328
+ // [TODO] Put something here to tell the user that a problem occured...
329
+ }
330
+ }
331
+ }
332
+ }
333
+
334
+ /* Update SystemCoreClock variable */
335
+ SystemCoreClockUpdate ();
336
+
337
+ /* Output SYSCLK on MCO pin(PA8) for debugging purpose */
338
+ /*
339
+ // Enable GPIOA clock
340
+ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
341
+ // Configure MCO pin (PA8)
342
+ GPIO_InitTypeDef GPIO_InitStructure;
343
+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
344
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
345
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
346
+ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
347
+ GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
348
+ GPIO_Init(GPIOA, &GPIO_InitStructure);
349
+ // Select the clock to output
350
+ RCC_MCOConfig(RCC_MCOSource_SYSCLK, RCC_MCOPrescaler_1);
351
+ */
352
+ }
353
+
354
+ #if (USE_PLL_HSE_XTAL != 0 ) || (USE_PLL_HSE_EXTC != 0 )
328
355
/******************************************************************************/
329
- /* PLL (clocked by HSI ) used as System clock source */
356
+ /* PLL (clocked by HSE ) used as System clock source */
330
357
/******************************************************************************/
358
+ uint8_t SetSysClock_PLL_HSE (uint8_t bypass )
359
+ {
360
+ __IO uint32_t StartUpCounter = 0 ;
361
+ __IO uint32_t HSEStatus = 0 ;
331
362
332
- /* At this stage the HSI is already enabled and used as System clock source */
363
+ /* Bypass HSE: can be done only if HSE is OFF */
364
+ if (bypass != 0 )
365
+ {
366
+ RCC -> CR &= ((uint32_t )~RCC_CR_HSEON ); /* To be sure HSE is OFF */
367
+ RCC -> CR |= ((uint32_t )RCC_CR_HSEBYP );
368
+ }
333
369
334
- /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
335
-
336
- /* Disable Prefetch Buffer and set Flash Latency */
337
- FLASH -> ACR = (uint32_t )FLASH_ACR_LATENCY_1 ;
338
-
339
- /* HCLK = 64 MHz */
340
- RCC -> CFGR |= (uint32_t )RCC_CFGR_HPRE_DIV1 ;
341
-
342
- /* PCLK2 = 64 MHz */
343
- RCC -> CFGR |= (uint32_t )RCC_CFGR_PPRE2_DIV1 ;
370
+ /* Enable HSE */
371
+ RCC -> CR |= ((uint32_t )RCC_CR_HSEON );
372
+
373
+ /* Wait till HSE is ready */
374
+ do
375
+ {
376
+ HSEStatus = RCC -> CR & RCC_CR_HSERDY ;
377
+ StartUpCounter ++ ;
378
+ } while ((HSEStatus == 0 ) && (StartUpCounter != HSE_STARTUP_TIMEOUT ));
344
379
345
- /* PCLK1 = 32 MHz (SPI, ...) */
346
- RCC -> CFGR |= (uint32_t )RCC_CFGR_PPRE1_DIV2 ;
380
+ /* Check if HSE has started correctly */
381
+ if ((RCC -> CR & RCC_CR_HSERDY ) != RESET )
382
+ {
383
+ /* Enable prefetch buffer and set flash latency
384
+ 0WS for 0 < SYSCLK <= 24 MHz
385
+ 1WS for 24 < SYSCLK <= 48 MHz
386
+ 2WS for 48 < SYSCLK <= 72 MHz */
387
+ FLASH -> ACR = FLASH_ACR_PRFTBE | (uint32_t )FLASH_ACR_LATENCY_1 ; /* 2 WS */
388
+
389
+ /* Warning: values are obtained with external xtal or clock = 8 MHz */
390
+ /* SYSCLK = 72 MHz (8 MHz * 9) */
391
+ RCC -> CFGR &= (uint32_t )((uint32_t )~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL ));
392
+ RCC -> CFGR |= (uint32_t )(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL9
393
+ | RCC_CFGR_HPRE_DIV1 /* HCLK = 72 MHz */
394
+ | RCC_CFGR_PPRE2_DIV1 /* PCLK2 = 72 MHz */
395
+ | RCC_CFGR_PPRE1_DIV2 ); /* PCLK1 = 36 MHz */
396
+ /* USBCLK = 48 MHz (72 MHz / 1.5) --> USB OK */
397
+
398
+ /* Enable PLL */
399
+ RCC -> CR |= RCC_CR_PLLON ;
400
+
401
+ /* Wait till PLL is ready */
402
+ while ((RCC -> CR & RCC_CR_PLLRDY ) == 0 )
403
+ {
404
+ }
405
+
406
+ /* Select PLL as system clock source */
407
+ RCC -> CFGR &= (uint32_t )((uint32_t )~(RCC_CFGR_SW ));
408
+ RCC -> CFGR |= (uint32_t )RCC_CFGR_SW_PLL ;
409
+
410
+ /* Wait till PLL is used as system clock source */
411
+ while ((RCC -> CFGR & (uint32_t )RCC_CFGR_SWS ) != (uint32_t )RCC_CFGR_SWS_PLL )
412
+ {
413
+ }
414
+
415
+ return 1 ; // OK
416
+ }
417
+ else
418
+ {
419
+ return 0 ; // FAIL
420
+ }
421
+ }
422
+ #endif
347
423
348
- /* PLL configuration
349
- SYSCLK = 4 MHz * 16 = 64 MHz
350
- */
424
+ /******************************************************************************/
425
+ /* PLL (clocked by HSI) used as System clock source */
426
+ /******************************************************************************/
427
+ uint8_t SetSysClock_PLL_HSI (void )
428
+ {
429
+ /* At this stage the HSI is already enabled and used as System clock source */
430
+
431
+ /* Enable prefetch buffer and set flash latency
432
+ 0WS for 0 < SYSCLK <= 24 MHz
433
+ 1WS for 24 < SYSCLK <= 48 MHz
434
+ 2WS for 48 < SYSCLK <= 72 MHz */
435
+ FLASH -> ACR = FLASH_ACR_PRFTBE | (uint32_t )FLASH_ACR_LATENCY_1 ; /* 2 WS */
436
+
437
+ /* SYSCLK = 64 MHz (8 MHz / 2 * 16) */
351
438
RCC -> CFGR &= (uint32_t )((uint32_t )~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL ));
352
- RCC -> CFGR |= (uint32_t )(RCC_CFGR_PLLSRC_HSI_Div2 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL16 );
439
+ RCC -> CFGR |= (uint32_t )(RCC_CFGR_PLLSRC_HSI_Div2 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL16
440
+ | RCC_CFGR_HPRE_DIV1 /* HCLK = 64 MHz */
441
+ | RCC_CFGR_PPRE2_DIV1 /* PCLK2 = 64 MHz */
442
+ | RCC_CFGR_PPRE1_DIV2 ); /* PCLK1 = 32 MHz */
443
+ /* USBCLK = 42.667 MHz (64 MHz / 1.5) --> USB NOT POSSIBLE */
353
444
354
445
/* Enable PLL */
355
446
RCC -> CR |= RCC_CR_PLLON ;
@@ -367,6 +458,8 @@ void SetSysClock(void)
367
458
while ((RCC -> CFGR & (uint32_t )RCC_CFGR_SWS ) != (uint32_t )RCC_CFGR_SWS_PLL )
368
459
{
369
460
}
461
+
462
+ return 1 ; // OK
370
463
}
371
464
372
465
/**
@@ -382,4 +475,3 @@ void SetSysClock(void)
382
475
*/
383
476
384
477
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
385
-
0 commit comments