找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8226|回复: 1
收起左侧

system_stm32f10x.c时钟系统的设置

[复制链接]
ID:72519 发表于 2015-1-23 03:50 | 显示全部楼层 |阅读模式
  1. /**
  2.   ******************************************************************************
  3.   * @file    GPIO/IOToggle/system_stm32f10x.c
  4.   * @author  MCD Application Team
  5.   * @version V3.4.0
  6.   * @date    10/15/2010
  7.   * @brief   CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  8.   ******************************************************************************
  9.   *
  10.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  11.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  12.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  13.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  14.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  15.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  16.   *
  17.   * <h2><center>© COPYRIGHT 2010 STMicroelectronics</center></h2>
  18.   ******************************************************************************
  19.   */
  20. /** @addtogroup CMSIS
  21.   * @{
  22.   */
  23. /** @addtogroup stm32f10x_system
  24.   * @{
  25.   */

  26. /** @addtogroup STM32F10x_System_Private_Includes
  27.   * @{
  28.   */
  29. #include "stm32f10x.h"
  30. /**
  31.   * @}
  32.   */
  33. /** @addtogroup STM32F10x_System_Private_TypesDefinitions
  34.   * @{
  35.   */
  36. /**
  37.   * @}
  38.   */
  39. /** @addtogroup STM32F10x_System_Private_Defines
  40.   * @{
  41.   */
  42. /*!< Uncomment the line corresponding to the desired System clock (SYSCLK)
  43.    frequency (after reset the HSI is used as SYSCLK source)
  44.   
  45.    IMPORTANT NOTE:
  46.    ==============
  47.    1. After each device reset the HSI is used as System clock source.
  48.    2. Please make sure that the selected System clock doesn't exceed your device's
  49.       maximum frequency.
  50.      
  51.    3. If none of the define below is enabled, the HSI is used as System clock
  52.     source.
  53.    4. The System clock configuration functions provided within this file assume that:
  54.         - For Low, Medium and High density Value line devices an external 8MHz
  55.           crystal is used to drive the System clock.
  56.         - For Low, Medium and High density devices an external 8MHz crystal is
  57.           used to drive the System clock.
  58.         - For Connectivity line devices an external 25MHz crystal is used to drive
  59.           the System clock.
  60.      If you are using different crystal you have to adapt those functions accordingly.
  61.     */
  62.    
  63. #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)//如果定义了这些系统时钟将设为24M,如果没有定义则为72M
  64. /* #define SYSCLK_FREQ_HSE    HSE_VALUE */
  65. #define SYSCLK_FREQ_24MHz  24000000
  66. #else
  67. /* #define SYSCLK_FREQ_HSE    HSE_VALUE */
  68. /* #define SYSCLK_FREQ_24MHz  24000000 */
  69. /* #define SYSCLK_FREQ_36MHz  36000000 */
  70. /* #define SYSCLK_FREQ_48MHz  48000000 */
  71. /* #define SYSCLK_FREQ_56MHz  56000000 */
  72. #define SYSCLK_FREQ_72MHz  72000000      //系统时钟默认值的定义 ,如果没有定义外部高速时钟则用内部高速时钟,为8000000
  73. #endif
  74. /*!< Uncomment the following line if you need to use external SRAM mounted
  75.      on STM3210E-EVAL board (STM32 High density and XL-density devices) or on
  76.      STM32100E-EVAL board (STM32 High-density value line devices) as data memory */
  77. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)//内外部SRAM选择
  78. /* #define DATA_IN_ExtSRAM */
  79. #endif
  80. /*!< Uncomment the following line if you need to relocate your vector Table in
  81.      Internal SRAM. */
  82. /* #define VECT_TAB_SRAM */
  83. #define VECT_TAB_OFFSET  0x0 /*!< Vector Table base offset field. //向量表的基址偏移量
  84.                                   This value must be a multiple of 0x100. */

  85. /**
  86.   * @}
  87.   */
  88. /** @addtogroup STM32F10x_System_Private_Macros
  89.   * @{
  90.   */
  91. /**
  92.   * @}
  93.   */
  94. /** @addtogroup STM32F10x_System_Private_Variables
  95.   * @{
  96.   */
  97. /*******************************************************************************
  98. *  Clock Definitions;以下为把系统时钟的定义值传给系统内核时钟变量,如果没有定义外部高速时钟则用内部高速时钟,为8M
  99. *******************************************************************************/
  100. #ifdef SYSCLK_FREQ_HSE
  101.   uint32_t SystemCoreClock         = SYSCLK_FREQ_HSE;        /*!< System Clock Frequency (Core Clock) */
  102. #elif defined SYSCLK_FREQ_24MHz
  103.   uint32_t SystemCoreClock         = SYSCLK_FREQ_24MHz;        /*!< System Clock Frequency (Core Clock) */
  104. #elif defined SYSCLK_FREQ_36MHz
  105.   uint32_t SystemCoreClock         = SYSCLK_FREQ_36MHz;        /*!< System Clock Frequency (Core Clock) */
  106. #elif defined SYSCLK_FREQ_48MHz
  107.   uint32_t SystemCoreClock         = SYSCLK_FREQ_48MHz;        /*!< System Clock Frequency (Core Clock) */
  108. #elif defined SYSCLK_FREQ_56MHz
  109.   uint32_t SystemCoreClock         = SYSCLK_FREQ_56MHz;        /*!< System Clock Frequency (Core Clock) */
  110. #elif defined SYSCLK_FREQ_72MHz
  111.   uint32_t SystemCoreClock         = SYSCLK_FREQ_72MHz;        /*!< System Clock Frequency (Core Clock) */
  112. #else /*!< HSI Selected as System Clock source */
  113.   uint32_t SystemCoreClock         = HSI_VALUE;        /*!< System Clock Frequency (Core Clock) 如果没有定义外部高速时钟则用内部高速时钟,为8000000*/
  114. #endif
  115. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};//AHB配方表
  116. /**
  117.   * @}
  118.   */
  119. /** @addtogroup STM32F10x_System_Private_FunctionPrototypes
  120.   * @{
  121.   */
  122. /*********************************************************************************
  123.                以下为函数声明
  124. *********************************************************************************/
  125. static void SetSysClock(void); //设置系统时钟的函数声明
  126. //以下为根据不同的系统时钟的定义来声明用到的相应的函数,为后面的函数调用做好准备
  127. #ifdef SYSCLK_FREQ_HSE
  128.   static void SetSysClockToHSE(void);
  129. #elif defined SYSCLK_FREQ_24MHz
  130.   static void SetSysClockTo24(void);
  131. #elif defined SYSCLK_FREQ_36MHz
  132.   static void SetSysClockTo36(void);
  133. #elif defined SYSCLK_FREQ_48MHz
  134.   static void SetSysClockTo48(void);
  135. #elif defined SYSCLK_FREQ_56MHz
  136.   static void SetSysClockTo56(void);
  137. #elif defined SYSCLK_FREQ_72MHz
  138.   static void SetSysClockTo72(void);
  139. #endif
  140. #ifdef DATA_IN_ExtSRAM //外部SRAM选择后的初始化函数声明
  141.   static void SystemInit_ExtMemCtl(void);
  142. #endif /* DATA_IN_ExtSRAM */
  143. /**
  144.   * @}
  145.   */
  146. /** @addtogroup STM32F10x_System_Private_Functions
  147.   * @{
  148.   */
  149. /**
  150.   * @brief  Setup the microcontroller system
  151.   *         Initialize the Embedded Flash Interface, the PLL and update the
  152.   *         SystemCoreClock variable.
  153.   * @note   This function should be used only after reset.
  154.   * @param  None
  155.   * @retval None
  156.   */
  157. void SystemInit (void)//系统初始化函数,设置系统的时钟及时钟中断(在startup_stm32f10x_md.s中调用)(复位RCC时钟配置为默认状态,直到设置时钟函数)
  158. {
  159.   /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  160.   /* Set HSION bit */
  161.   RCC->CR |= (uint32_t)0x00000001; //内部高速时钟使能,内部8MHz时钟开启
  162.   /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
  163. #ifndef STM32F10X_CL
  164.   RCC->CFGR &= (uint32_t)0xF8FF0000;//MCO微控制器没有时钟输出(对外部引脚),ADC预分频PCLK2 2分频后作为ADC时钟,APB预分频HCLK不分频,AHB预分频SYSCLK不分频,HSI作为系统时钟
  165.                                     //HSI作为系统时钟输出(已输出),SYSCLK=PCLK=PCLK1=PCLK2=8M,ADCCLK=1/2(PCLK2)=4M
  166. #else
  167.   RCC->CFGR &= (uint32_t)0xF0FF0000;//同上;RCC->CFGR的27位为保留位始终为0 ,HSI作为系统时钟输出(未输出原因为未编译)
  168. #endif /* STM32F10X_CL */  

  169.   /* Reset HSEON, CSSON and PLLON bits */
  170.   RCC->CR &= (uint32_t)0xFEF6FFFF;//时钟监测器关闭,HSE振荡器关闭
  171.   /* Reset HSEBYP bit */
  172.   RCC->CR &= (uint32_t)0xFFFBFFFF;//外部4-25MHz振荡器没有旁路
  173.   /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  174.   RCC->CFGR &= (uint32_t)0xFF80FFFF; //PLL时钟1.5倍分频作为USB时钟,PLL 2倍频输出,HSE不分频,HSI时钟2分频后作为PLL输入时钟
  175.                                      //PLLCLK=HSICLK=8M(还未输出),HSECLK=HSEOSC,USBCLK=PLLCLK/1.5 ,除PLL外其他分频系数都为0
  176. #ifdef STM32F10X_CL
  177.   /* Reset PLL2ON and PLL3ON bits */
  178.   RCC->CR &= (uint32_t)0xEBFFFFFF;//CR中的26和28位置0
  179.   /* Disable all interrupts and clear pending bits  */
  180.   RCC->CIR = 0x00FF0000;//清除中断标志,关闭一些中断
  181.   /* Reset CFGR2 register */
  182.   RCC->CFGR2 = 0x00000000; //没有此寄存器
  183. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  184.   /* Disable all interrupts and clear pending bits  */
  185.   RCC->CIR = 0x009F0000;//清除中断标志,关闭一些中断
  186.   /* Reset CFGR2 register */
  187.   RCC->CFGR2 = 0x00000000; //没有此寄存器   
  188. #else
  189.   /* Disable all interrupts and clear pending bits  */
  190.   RCC->CIR = 0x009F0000; //清除中断标志,关闭一些中断
  191. #endif /* STM32F10X_CL */
  192.    
  193. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
  194.   #ifdef DATA_IN_ExtSRAM
  195.     SystemInit_ExtMemCtl();//如果宏定义了外部SRAM则对其初始化控制
  196.   #endif /* DATA_IN_ExtSRAM */
  197. #endif
  198.   /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  199.   /* Configure the Flash Latency cycles and enable prefetch buffer */
  200.   SetSysClock();//设置系统时钟
  201. #ifdef VECT_TAB_SRAM
  202.   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. 向量表放在内部SRAM中*/
  203. #else
  204.   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. 向量表放在内部flash中*/
  205. #endif
  206. }
  207. /**
  208.   * @brief  Update SystemCoreClock according to Clock Register Values
  209.   * @note   None
  210.   * @param  None
  211.   * @retval None
  212.   */
  213. void SystemCoreClockUpdate (void)
  214. {
  215.   uint32_t tmp = 0, pllmull = 0, pllsource = 0;
  216. #ifdef  STM32F10X_CL
  217.   uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
  218. #endif /* STM32F10X_CL */
  219. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  220.   uint32_t prediv1factor = 0;
  221. #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL or STM32F10X_HD_VL */
  222.    
  223.   /* Get SYSCLK source -------------------------------------------------------*/
  224.   tmp = RCC->CFGR & RCC_CFGR_SWS;

  225.   switch (tmp)
  226.   {
  227.     case 0x00:  /* HSI used as system clock */
  228.       SystemCoreClock = HSI_VALUE;
  229.       break;
  230.     case 0x04:  /* HSE used as system clock */
  231.       SystemCoreClock = HSE_VALUE;
  232.       break;
  233.     case 0x08:  /* PLL used as system clock */
  234.       /* Get PLL clock source and multiplication factor ----------------------*/
  235.       pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  236.       pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  237.      
  238. #ifndef STM32F10X_CL     
  239.       pllmull = ( pllmull >> 18) + 2;
  240.      
  241.       if (pllsource == 0x00)
  242.       {
  243.         /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  244.         SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  245.       }
  246.       else
  247.       {
  248. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  249.        prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  250.        /* HSE oscillator clock selected as PREDIV1 clock entry */
  251.        SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  252. #else
  253.         /* HSE selected as PLL clock entry */
  254.         if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
  255.         {/* HSE oscillator clock divided by 2 */
  256.           SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
  257.         }
  258.         else
  259.         {
  260.           SystemCoreClock = HSE_VALUE * pllmull;
  261.         }
  262. #endif
  263.       }
  264. #else
  265.       pllmull = pllmull >> 18;
  266.      
  267.       if (pllmull != 0x0D)
  268.       {
  269.          pllmull += 2;
  270.       }
  271.       else
  272.       { /* PLL multiplication factor = PLL input clock * 6.5 */
  273.         pllmull = 13 / 2;
  274.       }
  275.            
  276.       if (pllsource == 0x00)
  277.       {
  278.         /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  279.         SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  280.       }
  281.       else
  282.       {/* PREDIV1 selected as PLL clock entry */
  283.       
  284.         /* Get PREDIV1 clock source and division factor */
  285.         prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
  286.         prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  287.       
  288.         if (prediv1source == 0)
  289.         {
  290.           /* HSE oscillator clock selected as PREDIV1 clock entry */
  291.           SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;         
  292.         }
  293.         else
  294.         {/* PLL2 clock selected as PREDIV1 clock entry */
  295.          
  296.           /* Get PREDIV2 division factor and PLL2 multiplication factor */
  297.           prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;
  298.           pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2;
  299.           SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;                        
  300.         }
  301.       }
  302. #endif /* STM32F10X_CL */
  303.       break;
  304.     default:
  305.       SystemCoreClock = HSI_VALUE;
  306.       break;
  307.   }

  308.   /* Compute HCLK clock frequency ----------------*/
  309.   /* Get HCLK prescaler */
  310.   tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  311.   /* HCLK clock frequency */
  312.   SystemCoreClock >>= tmp;
  313. }
  314. /**
  315.   * @brief  Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  316.   * @param  None
  317.   * @retval None
  318.   */
  319. static void SetSysClock(void)//根据不同的宏定义,设置不同的系统时钟
  320. {
  321. #ifdef SYSCLK_FREQ_HSE
  322.   SetSysClockToHSE();
  323. #elif defined SYSCLK_FREQ_24MHz
  324.   SetSysClockTo24();
  325. #elif defined SYSCLK_FREQ_36MHz
  326.   SetSysClockTo36();
  327. #elif defined SYSCLK_FREQ_48MHz
  328.   SetSysClockTo48();
  329. #elif defined SYSCLK_FREQ_56MHz
  330.   SetSysClockTo56();
  331. #elif defined SYSCLK_FREQ_72MHz
  332.   SetSysClockTo72();
  333. #endif

  334. /* If none of the define above is enabled, the HSI is used as System clock
  335.     source (default after reset) */
  336. }
  337. /**
  338.   * @brief  Setup the external memory controller. Called in startup_stm32f10x.s
  339.   *          before jump to __main
  340.   * @param  None
  341.   * @retval None
  342.   */
  343. #ifdef DATA_IN_ExtSRAM
  344. /**
  345.   * @brief  Setup the external memory controller.
  346.   *         Called in startup_stm32f10x_xx.s/.c before jump to main.
  347.   *        This function configures the external SRAM mounted on STM3210E-EVAL
  348.   *         board (STM32 High density devices). This SRAM will be used as program
  349.   *         data memory (including heap and stack).
  350.   * @param  None
  351.   * @retval None
  352.   */
  353. void SystemInit_ExtMemCtl(void)
  354. {
  355. /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
  356.   required, then adjust the Register Addresses */
  357.   /* Enable FSMC clock */
  358.   RCC->AHBENR = 0x00000114;

  359.   /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
  360.   RCC->APB2ENR = 0x000001E0;

  361. /* ---------------  SRAM Data lines, NOE and NWE configuration ---------------*/
  362. /*----------------  SRAM Address lines configuration -------------------------*/
  363. /*----------------  NOE and NWE configuration --------------------------------*/
  364. /*----------------  NE3 configuration ----------------------------------------*/
  365. /*----------------  NBL0, NBL1 configuration ---------------------------------*/

  366.   GPIOD->CRL = 0x44BB44BB;
  367.   GPIOD->CRH = 0xBBBBBBBB;
  368.   GPIOE->CRL = 0xB44444BB;
  369.   GPIOE->CRH = 0xBBBBBBBB;
  370.   GPIOF->CRL = 0x44BBBBBB;
  371.   GPIOF->CRH = 0xBBBB4444;
  372.   GPIOG->CRL = 0x44BBBBBB;
  373.   GPIOG->CRH = 0x44444B44;
  374.   
  375. /*----------------  FSMC Configuration ---------------------------------------*/
  376. /*----------------  Enable FSMC Bank1_SRAM Bank ------------------------------*/

  377.   FSMC_Bank1->BTCR[4] = 0x00001011;
  378.   FSMC_Bank1->BTCR[5] = 0x00000200;
  379. }
  380. #endif /* DATA_IN_ExtSRAM */
  381. #ifdef SYSCLK_FREQ_HSE
  382. /**
  383.   * @brief  Selects HSE as System clock source and configure HCLK, PCLK2
  384.   *          and PCLK1 prescalers.
  385.   * @note   This function should be used only after reset.
  386.   * @param  None
  387.   * @retval None
  388.   */
  389. static void SetSysClockToHSE(void)
  390. {
  391.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;

  392.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   
  393.   /* Enable HSE */   
  394.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);

  395.   /* Wait till HSE is ready and if Time out is reached exit */
  396.   do
  397.   {
  398.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  399.     StartUpCounter++;
  400.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  401.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  402.   {
  403.     HSEStatus = (uint32_t)0x01;
  404.   }
  405.   else
  406.   {
  407.     HSEStatus = (uint32_t)0x00;
  408.   }
  409.   if (HSEStatus == (uint32_t)0x01)
  410.   {
  411. #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
  412.     /* Enable Prefetch Buffer */
  413.     FLASH->ACR |= FLASH_ACR_PRFTBE;
  414.     /* Flash 0 wait state */
  415.     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  416. #ifndef STM32F10X_CL
  417.     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
  418. #else
  419.     if (HSE_VALUE <= 24000000)
  420. {
  421.       FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
  422. }
  423. else
  424. {
  425.       FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  426. }
  427. #endif /* STM32F10X_CL */
  428. #endif

  429.     /* HCLK = SYSCLK */
  430.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  431.      
  432.     /* PCLK2 = HCLK */
  433.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  434.    
  435.     /* PCLK1 = HCLK */
  436.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  437.    
  438.     /* Select HSE as system clock source */
  439.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  440.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;   
  441.     /* Wait till HSE is used as system clock source */
  442.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)
  443.     {
  444.     }
  445.   }
  446.   else
  447.   { /* If HSE fails to start-up, the application will have wrong clock
  448.          configuration. User can add here some code to deal with this error */
  449.   }
  450. }
  451. #elif defined SYSCLK_FREQ_24MHz
  452. /**
  453.   * @brief  Sets System clock frequency to 24MHz and configure HCLK, PCLK2
  454.   *          and PCLK1 prescalers.
  455.   * @note   This function should be used only after reset.
  456.   * @param  None
  457.   * @retval None
  458.   */
  459. static void SetSysClockTo24(void)//系统时钟设置为24M:SYSCLK=24M,HCLK=24M,PCLK1=24M(最高36M),PCLK2=24M,ADCCLK=12M,
  460. {
  461.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;

  462.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   
  463.   /* Enable HSE */   
  464.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);

  465.   /* Wait till HSE is ready and if Time out is reached exit */
  466.   do
  467.   {
  468.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  469.     StartUpCounter++;
  470.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  471.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  472.   {
  473.     HSEStatus = (uint32_t)0x01;
  474.   }
  475.   else
  476.   {
  477.     HSEStatus = (uint32_t)0x00;
  478.   }
  479.   if (HSEStatus == (uint32_t)0x01)
  480.   {
  481. #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
  482.     /* Enable Prefetch Buffer */
  483.     FLASH->ACR |= FLASH_ACR_PRFTBE;
  484.     /* Flash 0 wait state */
  485.     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  486.     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;   
  487. #endif

  488.     /* HCLK = SYSCLK */
  489.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  490.      
  491.     /* PCLK2 = HCLK */
  492.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  493.    
  494.     /* PCLK1 = HCLK */
  495.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  496.    
  497. #ifdef STM32F10X_CL
  498.     /* Configure PLLs ------------------------------------------------------*/
  499.     /* PLL configuration: PLLCLK = PREDIV1 * 6 = 24 MHz */
  500.     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  501.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  502.                             RCC_CFGR_PLLMULL6);
  503.     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  504.     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */      
  505.     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  506.                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  507.     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  508.                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);

  509.     /* Enable PLL2 */
  510.     RCC->CR |= RCC_CR_PLL2ON;
  511.     /* Wait till PLL2 is ready */
  512.     while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  513.     {
  514.     }  
  515. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
  516.     /*  PLL configuration:  = (HSE / 2) * 6 = 24 MHz */
  517.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  518.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1_Div2 | RCC_CFGR_PLLMULL6);
  519. #else   
  520.     /*  PLL configuration:  = (HSE / 2) * 6 = 24 MHz */
  521.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  522.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
  523. #endif /* STM32F10X_CL */
  524.     /* Enable PLL */
  525.     RCC->CR |= RCC_CR_PLLON;
  526.     /* Wait till PLL is ready */
  527.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  528.     {
  529.     }
  530.     /* Select PLL as system clock source */
  531.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  532.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   
  533.     /* Wait till PLL is used as system clock source */
  534.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  535.     {
  536.     }
  537.   }
  538.   else
  539.   { /* If HSE fails to start-up, the application will have wrong clock
  540.          configuration. User can add here some code to deal with this error */
  541.   }
  542. }
  543. #elif defined SYSCLK_FREQ_36MHz
  544. /**
  545.   * @brief  Sets System clock frequency to 36MHz and configure HCLK, PCLK2
  546.   *          and PCLK1 prescalers.
  547.   * @note   This function should be used only after reset.
  548.   * @param  None
  549.   * @retval None
  550.   */
  551. static void SetSysClockTo36(void)//系统时钟设置为36M:SYSCLK=36M,HCLK=36M,PCLK1=36M(最高36M),PCLK2=36M,ADCCLK=18M,
  552. {
  553.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;

  554.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   
  555.   /* Enable HSE */   
  556.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);

  557.   /* Wait till HSE is ready and if Time out is reached exit */
  558.   do
  559.   {
  560.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  561.     StartUpCounter++;
  562.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  563.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  564.   {
  565.     HSEStatus = (uint32_t)0x01;
  566.   }
  567.   else
  568.   {
  569.     HSEStatus = (uint32_t)0x00;
  570.   }
  571.   if (HSEStatus == (uint32_t)0x01)
  572.   {
  573.     /* Enable Prefetch Buffer */
  574.     FLASH->ACR |= FLASH_ACR_PRFTBE;
  575.     /* Flash 1 wait state */
  576.     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  577.     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;   

  578.     /* HCLK = SYSCLK */
  579.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  580.      
  581.     /* PCLK2 = HCLK */
  582.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  583.    
  584.     /* PCLK1 = HCLK */
  585.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  586.    
  587. #ifdef STM32F10X_CL
  588.     /* Configure PLLs ------------------------------------------------------*/
  589.    
  590.     /* PLL configuration: PLLCLK = PREDIV1 * 9 = 36 MHz */
  591.     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  592.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  593.                             RCC_CFGR_PLLMULL9);
  594. /*!< PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  595.     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
  596.       
  597.     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  598.                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  599.     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  600.                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);

  601.     /* Enable PLL2 */
  602.     RCC->CR |= RCC_CR_PLL2ON;
  603.     /* Wait till PLL2 is ready */
  604.     while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  605.     {
  606.     }
  607.    
  608. #else   
  609.     /*  PLL configuration: PLLCLK = (HSE / 2) * 9 = 36 MHz */
  610.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  611.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL9);
  612. #endif /* STM32F10X_CL */
  613.     /* Enable PLL */
  614.     RCC->CR |= RCC_CR_PLLON;
  615.     /* Wait till PLL is ready */
  616.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  617.     {
  618.     }
  619.     /* Select PLL as system clock source */
  620.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  621.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   
  622.     /* Wait till PLL is used as system clock source */
  623.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  624.     {
  625.     }
  626.   }
  627.   else
  628.   { /* If HSE fails to start-up, the application will have wrong clock
  629.          configuration. User can add here some code to deal with this error */
  630.   }
  631. }
  632. #elif defined SYSCLK_FREQ_48MHz
  633. /**
  634.   * @brief  Sets System clock frequency to 48MHz and configure HCLK, PCLK2
  635.   *          and PCLK1 prescalers.
  636.   * @note   This function should be used only after reset.
  637.   * @param  None
  638.   * @retval None
  639.   */
  640. static void SetSysClockTo48(void)//系统时钟设置为48M:SYSCLK=48M,HCLK=48M,PCLK1=24M(最高36M),PCLK2=48M,ADCCLK=24M,
  641. {
  642.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;

  643.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   
  644.   /* Enable HSE */   
  645.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);

  646.   /* Wait till HSE is ready and if Time out is reached exit */
  647.   do
  648.   {
  649.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  650.     StartUpCounter++;
  651.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  652.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  653.   {
  654.     HSEStatus = (uint32_t)0x01;
  655.   }
  656.   else
  657.   {
  658.     HSEStatus = (uint32_t)0x00;
  659.   }
  660.   if (HSEStatus == (uint32_t)0x01)
  661.   {
  662.     /* Enable Prefetch Buffer */
  663.     FLASH->ACR |= FLASH_ACR_PRFTBE;
  664.     /* Flash 1 wait state */
  665.     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  666.     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;   

  667.     /* HCLK = SYSCLK */
  668.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  669.      
  670.     /* PCLK2 = HCLK */
  671.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  672.    
  673.     /* PCLK1 = HCLK/2 */
  674.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  675.    
  676. #ifdef STM32F10X_CL
  677.     /* Configure PLLs ------------------------------------------------------*/
  678.     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  679.     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  680.       
  681.     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  682.                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  683.     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  684.                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);

  685.     /* Enable PLL2 */
  686.     RCC->CR |= RCC_CR_PLL2ON;
  687.     /* Wait till PLL2 is ready */
  688.     while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  689.     {
  690.     }
  691.    
  692.   
  693.     /* PLL configuration: PLLCLK = PREDIV1 * 6 = 48 MHz */
  694.     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  695.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  696.                             RCC_CFGR_PLLMULL6);
  697. #else   
  698.     /*  PLL configuration: PLLCLK = HSE * 6 = 48 MHz */
  699.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  700.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
  701. #endif /* STM32F10X_CL */
  702.     /* Enable PLL */
  703.     RCC->CR |= RCC_CR_PLLON;
  704.     /* Wait till PLL is ready */
  705.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  706.     {
  707.     }
  708.     /* Select PLL as system clock source */
  709.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  710.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   
  711.     /* Wait till PLL is used as system clock source */
  712.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  713.     {
  714.     }
  715.   }
  716.   else
  717.   { /* If HSE fails to start-up, the application will have wrong clock
  718.          configuration. User can add here some code to deal with this error */
  719.   }
  720. }
  721. #elif defined SYSCLK_FREQ_56MHz
  722. /**
  723.   * @brief  Sets System clock frequency to 56MHz and configure HCLK, PCLK2
  724.   *          and PCLK1 prescalers.
  725.   * @note   This function should be used only after reset.
  726.   * @param  None
  727.   * @retval None
  728.   */
  729. static void SetSysClockTo56(void)//系统时钟设置为56M:SYSCLK=56M,HCLK=56M,PCLK1=28M(最高36M),PCLK2=56M,ADCCLK=28M,
  730. {
  731.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;

  732.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/  
  733.   /* Enable HSE */   
  734.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);

  735.   /* Wait till HSE is ready and if Time out is reached exit */
  736.   do
  737.   {
  738.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  739.     StartUpCounter++;
  740.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  741.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  742.   {
  743.     HSEStatus = (uint32_t)0x01;
  744.   }
  745.   else
  746.   {
  747.     HSEStatus = (uint32_t)0x00;
  748.   }
  749.   if (HSEStatus == (uint32_t)0x01)
  750.   {
  751.     /* Enable Prefetch Buffer */
  752.     FLASH->ACR |= FLASH_ACR_PRFTBE;
  753.     /* Flash 2 wait state */
  754.     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  755.     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;   

  756.     /* HCLK = SYSCLK */
  757.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  758.      
  759.     /* PCLK2 = HCLK */
  760.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  761.    
  762.     /* PCLK1 = HCLK/2 */
  763.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  764. #ifdef STM32F10X_CL
  765.     /* Configure PLLs ------------------------------------------------------*/
  766.     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  767.     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  768.       
  769.     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  770.                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  771.     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  772.                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);

  773.     /* Enable PLL2 */
  774.     RCC->CR |= RCC_CR_PLL2ON;
  775.     /* Wait till PLL2 is ready */
  776.     while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  777.     {
  778.     }
  779.    
  780.   
  781.     /* PLL configuration: PLLCLK = PREDIV1 * 7 = 56 MHz */
  782.     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  783.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  784.                             RCC_CFGR_PLLMULL7);
  785. #else   
  786.     /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */
  787.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  788.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7);
  789. #endif /* STM32F10X_CL */
  790.     /* Enable PLL */
  791.     RCC->CR |= RCC_CR_PLLON;
  792.     /* Wait till PLL is ready */
  793.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  794.     {
  795.     }
  796.     /* Select PLL as system clock source */
  797.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  798.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   
  799.     /* Wait till PLL is used as system clock source */
  800.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  801.     {
  802.     }
  803.   }
  804.   else
  805.   { /* If HSE fails to start-up, the application will have wrong clock
  806.          configuration. User can add here some code to deal with this error */
  807.   }
  808. }
  809. #elif defined SYSCLK_FREQ_72MHz
  810. /**
  811.   * @brief  Sets System clock frequency to 72MHz and configure HCLK, PCLK2
  812.   *          and PCLK1 prescalers.
  813.   * @note   This function should be used only after reset.
  814.   * @param  None
  815.   * @retval None
  816.   */
  817. static void SetSysClockTo72(void)//系统时钟设置为72M:SYSCLK=72M,HCLK=72M,PCLK1=36M(最高36M),PCLK2=72M,ADCCLK=36M,
  818. {
  819.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;//启动计数,HSE状态

  820.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   
  821.   /* Enable HSE */   
  822.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);//HSE使能

  823.   /* Wait till HSE is ready and if Time out is reached exit */
  824.   do //循环,直到HSE使能成功或者超时
  825.   {
  826.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  827.     StartUpCounter++;
  828.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  829.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  830.   {
  831.     HSEStatus = (uint32_t)0x01;//HSE使能成功
  832.   }
  833.   else
  834.   {
  835.     HSEStatus = (uint32_t)0x00;//HSE使能不成功
  836.   }
  837.   if (HSEStatus == (uint32_t)0x01)//HSE使能成功
  838.   {
  839.     /* Enable Prefetch Buffer */
  840.     FLASH->ACR |= FLASH_ACR_PRFTBE;//flash缓存使能
  841.     /* Flash 2 wait state */
  842.     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);//
  843.     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;//   

  844.     /* HCLK = SYSCLK */
  845.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;//RCC_CFGR_HPRE_DIV1=0,CFGR中的值不变
  846.      
  847.     /* PCLK2 = HCLK */
  848.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;//RCC_CFGR_PPRE2_DIV1=0,CFGR中的值不变
  849.    
  850.     /* PCLK1 = HCLK/2 */
  851.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;//低速APB预分频把HCLK 2分频,APB1CLK=HCLK/2
  852. #ifdef STM32F10X_CL
  853.     /* Configure PLLs ------------------------------------------------------*/
  854.     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  855.     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  856.       
  857.     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  858.                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  859.     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  860.                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);

  861.     /* Enable PLL2 */
  862.     RCC->CR |= RCC_CR_PLL2ON;
  863.     /* Wait till PLL2 is ready */
  864.     while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  865.     {
  866.     }
  867.    
  868.   
  869.     /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */
  870.     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  871.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  872.                             RCC_CFGR_PLLMULL9);
  873. #else   
  874.     /*  PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
  875.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |  //PLL输入时钟源HSI时钟2分频后作为PLL输入时钟,HSE分频器作为PLL输入HSE不分频
  876.                                         RCC_CFGR_PLLMULL)); //PLL倍频系数PLL 2倍频输出(为了清零其他位)
  877.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);//PLL输入时钟源HSE时钟作为PLL输入时钟,PLL倍频系数PLL 9倍频输出
  878. #endif /* STM32F10X_CL */
  879.     /* Enable PLL */
  880.     RCC->CR |= RCC_CR_PLLON; //PLL使能
  881.     /* Wait till PLL is ready */
  882.     while((RCC->CR & RCC_CR_PLLRDY) == 0)//等待PLL使能成功
  883.     {
  884.     }
  885.    
  886.     /* Select PLL as system clock source */
  887.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));//HSI作为系统时钟(为了清零其他位)
  888.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; //PLL输出作为系统时钟  
  889.     /* Wait till PLL is used as system clock source */
  890.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)//等待直到PLL成功用作系统时钟源
  891.     {
  892.     }
  893.   }
  894.   else
  895.   { /* If HSE fails to start-up, the application will have wrong clock
  896.          configuration. User can add here some code to deal with this error */
  897.   }
  898. }
  899. #endif
  900. /**
  901.   * @}
  902.   */
  903. /**
  904.   * @}
  905.   */

  906. /**
  907.   * @}
  908.   */   
  909. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
复制代码


回复

使用道具 举报

ID:21482 发表于 2015-2-12 09:43 | 显示全部楼层
很详细,知道了不少东西
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表