STM32F4时钟设置分析

STM32F4时钟设置分析


本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.


环境:

主机:WIN7

开发环境:MDK4.72

MCU:STM32F407VGT6


STM32F4启动与STM32F10X不同,时钟已经默认配置好.

1.启动代码:

文件:startup_stm32f4xx.s

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">; Reset handler  
  2. Reset_Handler    PROC  
  3.                  EXPORT  Reset_Handler             [WEAK]  
  4.         IMPORT  SystemInit  
  5.         IMPORT  __main  
  6.   
  7.                  LDR     R0, =SystemInit  
  8.                  BLX     R0  
  9.                  LDR     R0, =__main  
  10.                  BX      R0  
  11.                  ENDP</span>  

可以看出,在进入main函数之前,系统调用了SystemInit函数.


2.SystemInit函数分析

SystemInit函数位于system_stm32f4xx.c文件中.此文件提供几个宏定义可以设置各个时钟:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">/************************* PLL Parameters *************************************/  
  2. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */  
  3. #define PLL_M      25  
  4. #define PLL_N      336  
  5.   
  6. /* SYSCLK = PLL_VCO / PLL_P */  
  7. #define PLL_P      2  
  8.   
  9. /* USB OTG FS, SDIO and RNG Clock =  PLL_VCO / PLLQ */  
  10. #define PLL_Q      7  
  11.   
  12. /******************************************************************************/</span>  

而晶振频率则是在文件stm32f4xx.h中进行设置:

外部晶振:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">#if !defined  (HSE_VALUE)   
  2.   #define HSE_VALUE    ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */  
  3. #endif /* HSE_VALUE */</span>  

内部晶振:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">#if !defined  (HSI_VALUE)     
  2.   #define HSI_VALUE    ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/  
  3. #endif /* HSI_VALUE */ </span>  

综上,可以得出默认配置中:

锁相环压腔振荡器时钟PLL_VCO = 25 / 25 * 336 = 336MHz

系统时钟SYSCLK = 336 / 2 = 168MHz

USB,SD卡时钟 = 336 / 7 = 48MHz

时钟图:


SystemInit函数代码:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">/** 
  2.   * @brief  Setup the microcontroller system 
  3.   *         Initialize the Embedded Flash Interface, the PLL and update the  
  4.   *         SystemFrequency variable. 
  5.   * @param  None 
  6.   * @retval None 
  7.   */  
  8. void SystemInit(void)  
  9. {  
  10.   /* FPU settings ------------------------------------------------------------*/  
  11.   #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)  
  12.     SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */  
  13.   #endif  
  14.   
  15.   /* Reset the RCC clock configuration to the default reset state ------------*/  
  16.   /* Set HSION bit */  
  17.   RCC->CR |= (uint32_t)0x00000001;  
  18.   
  19.   /* Reset CFGR register */  
  20.   RCC->CFGR = 0x00000000;  
  21.   
  22.   /* Reset HSEON, CSSON and PLLON bits */  
  23.   RCC->CR &= (uint32_t)0xFEF6FFFF;  
  24.   
  25.   /* Reset PLLCFGR register */  
  26.   RCC->PLLCFGR = 0x24003010;  
  27.   
  28.   /* Reset HSEBYP bit */  
  29.   RCC->CR &= (uint32_t)0xFFFBFFFF;  
  30.   
  31.   /* Disable all interrupts */  
  32.   RCC->CIR = 0x00000000;  
  33.   
  34. #ifdef DATA_IN_ExtSRAM  
  35.   SystemInit_ExtMemCtl();   
  36. #endif /* DATA_IN_ExtSRAM */  
  37.            
  38.   /* Configure the System clock source, PLL Multiplier and Divider factors,  
  39.      AHB/APBx prescalers and Flash settings ----------------------------------*/  
  40.   SetSysClock();  
  41.   
  42.   /* Configure the Vector Table location add offset address ------------------*/  
  43. #ifdef VECT_TAB_SRAM  
  44.   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */  
  45. #else  
  46.   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */  
  47. #endif  
  48. }</span>  


3.SetSysClock函数分析
在SetSysClock函数中,配置了系统时钟,PLL倍频以及分频系数:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">/** 
  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.     /* Select regulator voltage output Scale 1 mode, System frequency up to 168 MHz */  
  38.     RCC->APB1ENR |= RCC_APB1ENR_PWREN;  
  39.     PWR->CR |= PWR_CR_VOS;  
  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. }</span>  

如果外部时钟启动失败,系统会使用内部时钟

默认配置:

HCLK = SYSCLK / 1 = 168MHz

PCLK2 = HCLK / 2 = 84MHz

PCLK1 = HCLK / 4 = 42MHz

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值