1 | /**
|
2 | * @brief Configures the System clock source, PLL Multiplier and Divider factors,
|
3 | * AHB/APBx prescalers and Flash settings
|
4 | * @Note This function should be called only once the RCC clock configuration
|
5 | * is reset to the default reset state (done in SystemInit() function).
|
6 | * @param None
|
7 | * @retval None
|
8 | */
|
9 | static void SetSysClock(void)
|
10 | {
|
11 | /******************************************************************************/
|
12 | /* PLL (clocked by HSE) used as System clock source */
|
13 | /******************************************************************************/
|
14 | __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
15 |
|
16 | /* Enable HSE */
|
17 | RCC->CR |= ((uint32_t)RCC_CR_HSEON);
|
18 |
|
19 | /* Wait till HSE is ready and if Time out is reached exit */
|
20 | do
|
21 | {
|
22 | HSEStatus = RCC->CR & RCC_CR_HSERDY;
|
23 | StartUpCounter++;
|
24 | } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
25 |
|
26 | if ((RCC->CR & RCC_CR_HSERDY) != RESET)
|
27 | {
|
28 | HSEStatus = (uint32_t)0x01;
|
29 | }
|
30 | else
|
31 | {
|
32 | HSEStatus = (uint32_t)0x00;
|
33 | }
|
34 |
|
35 | if (HSEStatus == (uint32_t)0x01)
|
36 | {
|
37 | /* Enable high performance mode, System frequency up to 168 MHz */
|
38 | RCC->APB1ENR |= RCC_APB1ENR_PWREN;
|
39 | PWR->CR |= PWR_CR_PMODE;
|
40 |
|
41 | /* HCLK = SYSCLK / 1*/
|
42 | RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
|
43 |
|
44 | /* PCLK2 = HCLK / 2*/
|
45 | RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
|
46 |
|
47 | /* PCLK1 = HCLK / 4*/
|
48 | RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
|
49 |
|
50 | /* Configure the main PLL */
|
51 | RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
|
52 | (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
|
53 |
|
54 | /* Enable the main PLL */
|
55 | RCC->CR |= RCC_CR_PLLON;
|
56 |
|
57 | /* Wait till the main PLL is ready */
|
58 | while((RCC->CR & RCC_CR_PLLRDY) == 0)
|
59 | {
|
60 | }
|
61 |
|
62 | /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
|
63 | FLASH->ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
|
64 |
|
65 | /* Select the main PLL as system clock source */
|
66 | RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
|
67 | RCC->CFGR |= RCC_CFGR_SW_PLL;
|
68 |
|
69 | /* Wait till the main PLL is used as system clock source */
|
70 | while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
|
71 | {
|
72 | }
|
73 | }
|
74 | else
|
75 | { /* If HSE fails to start-up, the application will have wrong clock
|
76 | configuration. User can add here some code to deal with this error */
|
77 | }
|
78 |
|
79 | }
|