[应用相关] STM32系统时钟

[复制链接]
976|6
 楼主| parameters 发表于 2019-7-22 14:44 | 显示全部楼层 |阅读模式
一、时钟树
101647152298612.jpg
STM32有4个时钟源:
1)HSE(高速外部时钟源)  外部晶振作为时钟源,范围为4~16MHz,常取为8MHz
2)HSI(高速内部时钟源)  由内部RC振荡器产生,频率为8MHz,但不稳定
3)LSE(低速外部时钟)     以外部晶振作为时钟源,主要供给实时时钟模块,一般用32.768KHz。
4)LSI(低速内部时钟)         由内部RC振荡器产生,也是提供给实时时钟模块,频率约为40KHz。

 楼主| parameters 发表于 2019-7-22 14:44 | 显示全部楼层
二、系统启动过程中时钟设置过程

  以使用STM32库函数SystemInit为例进行说明:

上电后:默认使用HSI

SystemInit: 倘若调用了函数SetSysClockTo72,将启用外部晶振HSE,并将系统时钟设置到72MHz。
 楼主| parameters 发表于 2019-7-22 14:44 | 显示全部楼层
附SetSysClockTo72函数代码:
  1. /**
  2.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Sets System clock frequency to 72MHz and configure HCLK, PCLK2
  3.   *         and PCLK1 prescalers.
  4.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]   This function should be used only after reset.
  5.   * @param  None
  6.   * @retval None
  7.   */
  8. static void SetSysClockTo72(void)
  9. {
  10.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  11.   
  12.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   
  13.   /* Enable HSE */   
  14.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);

  15.   /* Wait till HSE is ready and if Time out is reached exit */
  16.   do
  17.   {
  18.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  19.     StartUpCounter++;  
  20.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));

  21.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  22.   {
  23.     HSEStatus = (uint32_t)0x01;
  24.   }
  25.   else
  26.   {
  27.     HSEStatus = (uint32_t)0x00;
  28.   }  

  29.   if (HSEStatus == (uint32_t)0x01)
  30.   {
  31.     /* Enable Prefetch Buffer */
  32.     FLASH->ACR |= FLASH_ACR_PRFTBE;

  33.     /* Flash 2 wait state */
  34.     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  35.     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;   


  36.     /* HCLK = SYSCLK */
  37.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  38.       
  39.     /* PCLK2 = HCLK */
  40.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  41.    
  42.     /* PCLK1 = HCLK */
  43.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;

  44. #ifdef STM32F10X_CL
  45.     /* Configure PLLs ------------------------------------------------------*/
  46.     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  47.     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  48.         
  49.     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  50.                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  51.     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  52.                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
  53.   
  54.     /* Enable PLL2 */
  55.     RCC->CR |= RCC_CR_PLL2ON;
  56.     /* Wait till PLL2 is ready */
  57.     while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  58.     {
  59.     }
  60.    
  61.    
  62.     /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */
  63.     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  64.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  65.                             RCC_CFGR_PLLMULL9);
  66. #else   
  67.     /*  PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
  68.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
  69.                                         RCC_CFGR_PLLMULL));
  70.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
  71. #endif /* STM32F10X_CL */

  72.     /* Enable PLL */
  73.     RCC->CR |= RCC_CR_PLLON;

  74.     /* Wait till PLL is ready */
  75.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  76.     {
  77.     }
  78.    
  79.     /* Select PLL as system clock source */
  80.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  81.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   

  82.     /* Wait till PLL is used as system clock source */
  83.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  84.     {
  85.     }
  86.   }
  87.   else
  88.   { /* If HSE fails to start-up, the application will have wrong clock
  89.          configuration. User can add here some code to deal with this error */
  90.   }
  91. }

renzheshengui 发表于 2019-8-12 11:37 | 显示全部楼层
非常感谢楼主分享
wakayi 发表于 2019-8-12 11:39 | 显示全部楼层
非常感谢楼主分享
wowu 发表于 2019-8-12 11:44 | 显示全部楼层

非常感谢楼主分享
yaweq 发表于 2019-8-12 17:43 | 显示全部楼层
非常感谢楼主分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

20

主题

361

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部