所以在函数定义中只会运行SetSysClockTo72()函数,该函数的功能是启动HSE,并且配置相应的AHB、APB等,并保证系统安全运行在72MHz频率下
#elif defined SYSCLK_FREQ_72MHz
static void SetSysClockTo72(void)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
/* Enable HSE */
RCC->CR |= ((uint32_t)RCC_CR_HSEON); // RCC_CR_HSEON = 0x01, 使能HSEON位
/* Wait till HSE is ready and if Time out is reached exit */
do //等待HSE就绪
{
HSEStatus = RCC->CR & RCC_CR_HSERDY; //RCC_CR_HSERDY = 0x00020000 HSERDY就绪标志位
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if ((RCC->CR & RCC_CR_HSERDY) != RESET) //do-while 之后用if 条件判断,保证结果正确
{
HSEStatus = (uint32_t)0x01; //合法标识
}
else
{
HSEStatus = (uint32_t)0x00; //HSEStatus除了0x00020000之外的非零非法值(比如0x20000)
}
if (HSEStatus == (uint32_t)0x01)
{
/* Enable Prefetch Buffer */
FLASH->ACR |= FLASH_ACR_PRFTBE; //预取值缓存,CPU从FLASH缓存器中读取数据
/* Flash 2 wait state */
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
/* HCLK = SYSCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; //AHP 不分频
// RCC_CFGR_HPRE_DIV1 = 0x00000000 , CFGR的HPRE[3:0] = 0000
/* PCLK2 = HCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1; //APB2 不分频
//RCC_CFGR_PPRE2_DIV1 = 0x00000000, CFGR的PPRE2[2:0] = 000
/* PCLK1 = HCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2; //APB1 2分频
//RCC_CFGR_PPRE1_DIV2 = 0x00000400 , CFGR的PPRE1[2:0] = 100
/* 以下代码不运行
#ifdef STM32F10X_CL
/* Configure PLLs ------------------------------------------------------*/
/* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
/* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
//
RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
/* Enable PLL2 */
RCC->CR |= RCC_CR_PLL2ON;
/* Wait till PLL2 is ready */
while((RCC->CR & RCC_CR_PLL2RDY) == 0)
{
}
/* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */
RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
RCC_CFGR_PLLMULL9);
以上代码不运行*/
#else
/* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */ //PLL选择 9倍频,此时HSE已经作为其时钟源
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
RCC_CFGR_PLLMULL));
// CFGR = 0x00000000 ,目的是将CFGR的bit21-bit16清零,然而CFGR再刚开机的时候就是0x00..
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
// CFGR = 0000 0000 0001 1101 0000 0000,CFGR的 PLLMUL[21:18] = 0111 ,九倍频输出
// bit[17:16] = 01 , PREDIV1作为PLL输入时钟,就是HSE作为PLL输入!
#endif /* STM32F10X_CL */
/* Enable PLL */
RCC->CR |= RCC_CR_PLLON; //使能PLL
/* Wait till PLL is ready */
while((RCC->CR & RCC_CR_PLLRDY) == 0) //等待PLL稳定
{
}
/* Select PLL as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW)); //PLL作为系统时钟源
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
// 操作类似 配置PLLMUL以及PLLSRC
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08) //SFGR->SWS位作为系统时钟源的指示,read-only。用于判断!
{
}
}
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 */
}
}
#endif
|