用stm32cube生成工程的時候,要配置各個時鍾,之前一直沿用的模板例程,所以還真不知道各個時鍾到底是多少。上圖,這是要配置的,可是自己程序配置的是怎樣呢?

接下來,看程序,程序開始
1 startup_stm32f40_41xxx.s
1 Reset_Handler PROC 2 EXPORT Reset_Handler [WEAK] 3 IMPORT SystemInit 4 IMPORT __main 5 6 LDR R0, =SystemInit 7 BLX R0 8 LDR R0, =__main 9 BX R0 10 ENDP
在進入main函數之前,系統調用了SystemInit函數.
2 system_stm32f4xx.c
在SystemInit里截取一段程序
1 /* Configure the System clock source, PLL Multiplier and Divider factors, 2 AHB/APBx prescalers and Flash settings ----------------------------------*/ 3 SetSysClock();
注釋:配置系統時鍾源,....
進入SetSysClock();
1 static void SetSysClock(void) 2 { 3 .... 4 5 /* HCLK = SYSCLK / 1*/ 6 RCC->CFGR |= RCC_CFGR_HPRE_DIV1; 7 8 9 #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx) 10 /* PCLK2 = HCLK / 2* 11 RCC->CFGR |= RCC_CFGR_PPRE2_DIV2; 12 13 /* PCLK1 = HCLK / 4*/ 14 RCC->CFGR |= RCC_CFGR_PPRE1_DIV4; 15 #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx */ 16 .... 17 /* Configure the main PLL */ 18 RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) | 19 (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24); 20 21 /* Enable the main PLL */ 22 RCC->CR |= RCC_CR_PLLON; 23 }
可以知道HCLK = SYSCLK/1,PCLK2 = HCLK / 2,PCLK1 = HCLK / 4。接下來就和M,P,Q有關了
這些值在前邊有define,以我的工程為例
1 .... 2 #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx) || defined (STM32F401xx) 3 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */ 4 #define PLL_M 8 5 #else /* STM32F411xE */ 6 ...... 7 /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */ 8 #define PLL_Q 7 9 10 #if defined (STM32F40_41xxx) 11 #define PLL_N 336 12 /* SYSCLK = PLL_VCO / PLL_P */ 13 #define PLL_P 2 14 #endif /* STM32F40_41xxx */ 15 ....
至此就差一項還不知道了,就是外部晶振頻率,它在stm32f4xx.h中有定義,
#if !defined (HSE_VALUE) #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ #endif /* HSE_VALUE */ /** * @brief In the following line adjust the External High Speed oscillator (HSE) Startup Timeout value */ #if !defined (HSE_STARTUP_TIMEOUT) #define HSE_STARTUP_TIMEOUT ((uint16_t)0x05000) /*!< Time out for HSE start up */ #endif /* HSE_STARTUP_TIMEOUT */ #if !defined (HSI_VALUE) #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ #endif /* HSI_VALUE */
綜合這些,就可以刻第一幅圖對應起來了。
