/** * @brief Setup the microcontroller system * Initialize the Embedded Flash Interface, the PLL and update the * SystemCoreClock variable. * @note This function should be used only after reset. * @param None * @retval None */ voidSystemInit(void) { /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
/* * 时钟控制寄存器: * 将RCC_CR的第0(HSION)位赋1,高速内部时钟使能,其他位不变 */ /* Set HSION bit */ RCC->CR |= (uint32_t)0x00000001;
/* * 时钟中断寄存器: * 将RCC_CR的[16:20]位、PLLRDYC[20]、CSSC[23]等置1,其他为0. */ /* Disable all interrupts and clear pending bits */ RCC->CIR = 0x009F0000;
/* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */ /* Configure the Flash Latency cycles and enable prefetch buffer */ SetSysClock();
/** * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers. * @param None * @retval None */ staticvoidSetSysClock(void) { #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_24MHz // ... #elif defined SYSCLK_FREQ_72MHz SetSysClockTo72(); #endif /* If none of the define above is enabled, the HSI is used as System clock source (default after reset) */ }
/** * @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2 * and PCLK1 prescalers. * @note This function should be used only after reset. * @param None * @retval None */ staticvoidSetSysClockTo72(void) { __IO uint32_t StartUpCounter = 0, HSEStatus = 0; /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/ // 使HSE时钟源使能。[16] /* Enable HSE */ RCC->CR |= ((uint32_t)RCC_CR_HSEON); // 循环等待RCC_CR寄存器的HSERDY[17]被置为1,此时才代表HSE时钟稳定了。 /* Wait till HSE is ready and if Time out is reached exit */ do { HSEStatus = RCC->CR & RCC_CR_HSERDY; StartUpCounter++; } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
// PLLRDY被置为,等待PLL时钟稳定。 /* Wait till PLL is ready */ while((RCC->CR & RCC_CR_PLLRDY) == 0) { } // 1、SW[1:0]置为 00 // 2、SW[1:0]置为 10,设置sw选择器选择PLL输出作为系统时钟 /* Select PLL as system clock source */ RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW)); RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
// 等待SWS[3:2]被置为 10,等待时钟稳定。 /* Wait till PLL is used as system clock source */ while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08) { } } else { /* If HSE fails to start-up, the application will have wrong clock configuration. User can add here some code to deal with this error */ } }