GD32设置时钟108M方法

[复制链接]
7216|6
 楼主| listentoenglish 发表于 2013-7-15 07:55 | 显示全部楼层 |阅读模式
本帖最后由 listentoenglish 于 2013-7-15 08:01 编辑

将system_stm32f10x.c替换为system_gd32f10x.c。
#define SYSCLK_FREQ  108000000UL修改时钟主频。
  1. #include "stm32f10x.h"

  2. //==============================================================================
  3. //设置主频
  4. #define SYSCLK_FREQ  108000000UL

  5. //以下不用更改
  6. //==============================================================================
  7. #define RCC_CFGR_PLLMULL17 (0x08000000UL|RCC_CFGR_PLLMULL2)
  8. #define RCC_CFGR_PLLMULL18 (0x08000000UL|RCC_CFGR_PLLMULL3)
  9. #define RCC_CFGR_PLLMULL19 (0x08000000UL|RCC_CFGR_PLLMULL4)
  10. #define RCC_CFGR_PLLMULL20 (0x08000000UL|RCC_CFGR_PLLMULL5)
  11. #define RCC_CFGR_PLLMULL21 (0x08000000UL|RCC_CFGR_PLLMULL6)
  12. #define RCC_CFGR_PLLMULL22 (0x08000000UL|RCC_CFGR_PLLMULL7)
  13. #define RCC_CFGR_PLLMULL23 (0x08000000UL|RCC_CFGR_PLLMULL8)
  14. #define RCC_CFGR_PLLMULL24 (0x08000000UL|RCC_CFGR_PLLMULL9)
  15. #define RCC_CFGR_PLLMULL25 (0x08000000UL|RCC_CFGR_PLLMULL10)
  16. #define RCC_CFGR_PLLMULL26 (0x08000000UL|RCC_CFGR_PLLMULL11)
  17. #define RCC_CFGR_PLLMULL27 (0x08000000UL|RCC_CFGR_PLLMULL12)
  18. #define RCC_CFGR_PLLMULL28 (0x08000000UL|RCC_CFGR_PLLMULL13)
  19. #define RCC_CFGR_PLLMULL29 (0x08000000UL|RCC_CFGR_PLLMULL14)
  20. #define RCC_CFGR_PLLMULL30 (0x08000000UL|RCC_CFGR_PLLMULL15)
  21. #define RCC_CFGR_PLLMULL31 (0x08000000UL|RCC_CFGR_PLLMULL16)
  22. #define RCC_CFGR_PLLMULL32 (0x08040000UL|RCC_CFGR_PLLMULL16)

  23. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
  24. /* #define DATA_IN_ExtSRAM */
  25. #endif

  26. /*!< Uncomment the following line if you need to relocate your vector Table in
  27. Internal SRAM. */
  28. /* #define VECT_TAB_SRAM */
  29. #define VECT_TAB_OFFSET  0x0 /*!< Vector Table base offset field.
  30. This value must be a multiple of 0x200. */


  31. /*******************************************************************************
  32. *  Clock Definitions
  33. *******************************************************************************/
  34. uint32_t SystemCoreClock         = SYSCLK_FREQ;

  35. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};

  36. static void SetSysClock(void);

  37. void SystemInit (void)
  38. {
  39.   /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  40.   /* Set HSION bit */
  41.   RCC->CR |= (uint32_t)0x00000001;
  42.   
  43.   /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
  44. #ifndef STM32F10X_CL
  45.   RCC->CFGR &= (uint32_t)0xF8FF0000;
  46. #else
  47.   RCC->CFGR &= (uint32_t)0xF0FF0000;
  48. #endif /* STM32F10X_CL */   
  49.   
  50.   /* Reset HSEON, CSSON and PLLON bits */
  51.   RCC->CR &= (uint32_t)0xFEF6FFFF;
  52.   
  53.   /* Reset HSEBYP bit */
  54.   RCC->CR &= (uint32_t)0xFFFBFFFF;
  55.   
  56.   /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  57.   RCC->CFGR &= (uint32_t)0xFF80FFFF;
  58.   
  59. #ifdef STM32F10X_CL
  60.   /* Reset PLL2ON and PLL3ON bits */
  61.   RCC->CR &= (uint32_t)0xEBFFFFFF;
  62.   
  63.   /* Disable all interrupts and clear pending bits  */
  64.   RCC->CIR = 0x00FF0000;
  65.   
  66.   /* Reset CFGR2 register */
  67.   RCC->CFGR2 = 0x00000000;
  68. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  69.   /* Disable all interrupts and clear pending bits  */
  70.   RCC->CIR = 0x009F0000;
  71.   
  72.   /* Reset CFGR2 register */
  73.   RCC->CFGR2 = 0x00000000;      
  74. #else
  75.   /* Disable all interrupts and clear pending bits  */
  76.   RCC->CIR = 0x009F0000;
  77. #endif /* STM32F10X_CL */
  78.   
  79. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
  80. #ifdef DATA_IN_ExtSRAM
  81.   SystemInit_ExtMemCtl();
  82. #endif /* DATA_IN_ExtSRAM */
  83. #endif
  84.   
  85.   /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  86.   /* Configure the Flash Latency cycles and enable prefetch buffer */
  87.   SetSysClock();
  88.   
  89. #ifdef VECT_TAB_SRAM
  90.   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  91. #else
  92.   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  93. #endif
  94. }

  95. /**
  96. * [url=home.php?mod=space&uid=247401]@brief[/url]  Update SystemCoreClock variable according to Clock Register Values.
  97. *         The SystemCoreClock variable contains the core clock (HCLK), it can
  98. *         be used by the user application to setup the SysTick timer or configure
  99. *         other parameters.
  100. *           
  101. * [url=home.php?mod=space&uid=536309]@NOTE[/url]   Each time the core clock (HCLK) changes, this function must be called
  102. *         to update SystemCoreClock variable value. Otherwise, any configuration
  103. *         based on this variable will be incorrect.         
  104. *     
  105. * [url=home.php?mod=space&uid=536309]@NOTE[/url]   - The system frequency computed by this function is not the real
  106. *           frequency in the chip. It is calculated based on the predefined
  107. *           constant and the selected clock source:
  108. *            
  109. *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  110. *                                             
  111. *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  112. *                          
  113. *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  114. *             or HSI_VALUE(*) multiplied by the PLL factors.
  115. *         
  116. *         (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value
  117. *             8 MHz) but the real value may vary depending on the variations
  118. *             in voltage and temperature.   
  119. *   
  120. *         (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value
  121. *              8 MHz or 25 MHz, depedning on the product used), user has to ensure
  122. *              that HSE_VALUE is same as the real frequency of the crystal used.
  123. *              Otherwise, this function may have wrong result.
  124. *               
  125. *         - The result of this function could be not correct when using fractional
  126. *           value for HSE crystal.
  127. * @param  None
  128. * @retval None
  129. */
  130. void SystemCoreClockUpdate (void)
  131. {
  132.   uint32_t tmp = 0, pllmull = 0, pllsource = 0;
  133.   
  134. #ifdef  STM32F10X_CL
  135.   uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
  136. #endif /* STM32F10X_CL */
  137.   
  138. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  139.   uint32_t prediv1factor = 0;
  140. #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL or STM32F10X_HD_VL */
  141.   
  142.   /* Get SYSCLK source -------------------------------------------------------*/
  143.   tmp = RCC->CFGR & RCC_CFGR_SWS;
  144.   
  145.   switch (tmp)
  146.   {
  147.   case 0x00:  /* HSI used as system clock */
  148.     SystemCoreClock = HSI_VALUE;
  149.     break;
  150.   case 0x04:  /* HSE used as system clock */
  151.     SystemCoreClock = HSE_VALUE;
  152.     break;
  153.   case 0x08:  /* PLL used as system clock */
  154.    
  155.     /* Get PLL clock source and multiplication factor ----------------------*/
  156.     pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  157.     pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  158.    
  159. #ifndef STM32F10X_CL      
  160.     pllmull = ( pllmull >> 18) + 2;
  161.    
  162.     if (pllsource == 0x00)
  163.     {
  164.       /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  165.       SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  166.     }
  167.     else
  168.     {
  169. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  170.       prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  171.       /* HSE oscillator clock selected as PREDIV1 clock entry */
  172.       SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  173. #else
  174.       /* HSE selected as PLL clock entry */
  175.       if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
  176.       {/* HSE oscillator clock divided by 2 */
  177.         SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
  178.       }
  179.       else
  180.       {
  181.         SystemCoreClock = HSE_VALUE * pllmull;
  182.       }
  183. #endif
  184.     }
  185. #else
  186.     pllmull = pllmull >> 18;
  187.    
  188.     if (pllmull != 0x0D)
  189.     {
  190.       pllmull += 2;
  191.     }
  192.     else
  193.     { /* PLL multiplication factor = PLL input clock * 6.5 */
  194.       pllmull = 13 / 2;
  195.     }
  196.    
  197.     if (pllsource == 0x00)
  198.     {
  199.       /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  200.       SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  201.     }
  202.     else
  203.     {/* PREDIV1 selected as PLL clock entry */
  204.       
  205.       /* Get PREDIV1 clock source and division factor */
  206.       prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
  207.       prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  208.       
  209.       if (prediv1source == 0)
  210.       {
  211.         /* HSE oscillator clock selected as PREDIV1 clock entry */
  212.         SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;         
  213.       }
  214.       else
  215.       {/* PLL2 clock selected as PREDIV1 clock entry */
  216.         
  217.         /* Get PREDIV2 division factor and PLL2 multiplication factor */
  218.         prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;
  219.         pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2;
  220.         SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;                        
  221.       }
  222.     }
  223. #endif /* STM32F10X_CL */
  224.     break;
  225.    
  226.   default:
  227.     SystemCoreClock = HSI_VALUE;
  228.     break;
  229.   }
  230.   
  231.   /* Compute HCLK clock frequency ----------------*/
  232.   /* Get HCLK prescaler */
  233.   tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  234.   /* HCLK clock frequency */
  235.   SystemCoreClock >>= tmp;  
  236. }

  237. /**
  238. * [url=home.php?mod=space&uid=247401]@brief[/url]  Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  239. * @param  None
  240. * @retval None
  241. */


  242. const uint32_t RCC_CFGR_PLL_TAB[]=
  243. {
  244.   0,
  245.   0,
  246.   RCC_CFGR_PLLMULL2,
  247.   RCC_CFGR_PLLMULL3,  
  248.   RCC_CFGR_PLLMULL4,
  249.   
  250.   RCC_CFGR_PLLMULL5,
  251.   RCC_CFGR_PLLMULL6,
  252.   RCC_CFGR_PLLMULL7,
  253.   RCC_CFGR_PLLMULL8,  
  254.   RCC_CFGR_PLLMULL9,
  255.   
  256.   RCC_CFGR_PLLMULL10,
  257.   RCC_CFGR_PLLMULL11,
  258.   RCC_CFGR_PLLMULL12,
  259.   RCC_CFGR_PLLMULL13,  
  260.   RCC_CFGR_PLLMULL14,
  261.   
  262.   RCC_CFGR_PLLMULL15,
  263.   RCC_CFGR_PLLMULL16,
  264.   RCC_CFGR_PLLMULL17,
  265.   RCC_CFGR_PLLMULL18,  
  266.   RCC_CFGR_PLLMULL19,
  267.   
  268.   RCC_CFGR_PLLMULL20,
  269.   RCC_CFGR_PLLMULL21,
  270.   RCC_CFGR_PLLMULL22,
  271.   RCC_CFGR_PLLMULL23,  
  272.   RCC_CFGR_PLLMULL24,
  273.   
  274.   RCC_CFGR_PLLMULL25,
  275.   RCC_CFGR_PLLMULL26,
  276.   RCC_CFGR_PLLMULL27,
  277.   RCC_CFGR_PLLMULL28,  
  278.   RCC_CFGR_PLLMULL29,
  279.   
  280.   RCC_CFGR_PLLMULL30,
  281.   RCC_CFGR_PLLMULL31,  
  282.   RCC_CFGR_PLLMULL32  
  283. };

  284. static void SetSysClockToPll(uint8_t __pre,uint8_t __pllmul)
  285. {
  286.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  287.   
  288.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   
  289.   /* Enable HSE */   
  290.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  291.   
  292.   /* Wait till HSE is ready and if Time out is reached exit */
  293.   do
  294.   {
  295.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  296.     StartUpCounter++;  
  297.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  298.   
  299.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  300.   {
  301.     HSEStatus = (uint32_t)0x01;
  302.   }
  303.   else
  304.   {
  305.     HSEStatus = (uint32_t)0x00;
  306.   }  
  307.   
  308.   if (HSEStatus == (uint32_t)0x01)
  309.   {
  310.     /* Enable Prefetch Buffer */
  311.     FLASH->ACR |= FLASH_ACR_PRFTBE;
  312.    
  313.     /* Flash 2 wait state */
  314.     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  315.    
  316. #if SYSCLK_FREQ>48000000ul
  317.     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;   
  318. #elif SYSCLK_FREQ>24000000ul
  319.     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  320. #endif
  321.    
  322.     /* HCLK = SYSCLK */
  323.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  324.    
  325.     /* PCLK2 = HCLK */
  326.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  327.    
  328.     /* PCLK1 = HCLK */
  329.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  330.    
  331.     //8/2*(15+12)=108MHz
  332.    
  333.     uint32_t __cfgr=RCC_CFGR_PLLSRC_HSE;
  334.     if(__pre)
  335.     {
  336.       __cfgr |=RCC_CFGR_PLLXTPRE_HSE_Div2;
  337.     }
  338.    
  339.     if(__pllmul<=32)
  340.     {
  341.       __cfgr |= RCC_CFGR_PLL_TAB[__pllmul];
  342.     }
  343.    
  344.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
  345.                                         RCC_CFGR_PLLMULL));
  346.    
  347.     RCC->CFGR |= __cfgr;
  348.    
  349.     /* Enable PLL */
  350.     RCC->CR |= RCC_CR_PLLON;
  351.    
  352.     /* Wait till PLL is ready */
  353.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  354.     {
  355.     }
  356.    
  357.     /* Select PLL as system clock source */
  358.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  359.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   
  360.    
  361.     /* Wait till PLL is used as system clock source */
  362.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  363.     {
  364.     }
  365.   }
  366.   else
  367.   { /* If HSE fails to start-up, the application will have wrong clock
  368.     configuration. User can add here some code to deal with this error */
  369.   }
  370. }

  371. static void SetSysClock(void)
  372. {
  373.   uint8_t __pre=1;
  374.   uint8_t __pllmul=(SYSCLK_FREQ) / ((HSE_VALUE)/2);
  375.   
  376.   SetSysClockToPll(__pre,__pllmul);
  377. }

system_gd32f10x.rar

3.16 KB, 下载次数: 131

周立功GD32 发表于 2013-7-15 08:54 | 显示全部楼层
楼主辛苦啊!
但还是采用修改源代码里的配置参数的思路来更改主频,不如我的sysClockSet()函数灵活啊,可在应用当中随时调用更改主频!
详见“【原创】《wdxLib_for_GD32F103》驱动开发(提供高质量源代码)”贴子
tonydong 发表于 2013-7-15 13:36 | 显示全部楼层
周立功GD32 发表于 2013-7-15 08:54
楼主辛苦啊!
但还是采用修改源代码里的配置参数的思路来更改主频,不如我的sysClockSet()函数灵活啊,可在 ...

是滴是滴,sysClockSet()函数更灵活,一旦定义,可随时调用
shenpingbing 发表于 2013-8-29 20:37 | 显示全部楼层
下载学习一下
筱禾1988 发表于 2013-8-30 20:53 | 显示全部楼层
向楼主学习一下
fengshuo123 发表于 2013-9-28 11:11 | 显示全部楼层
时钟频率这么高   工业中不会有问题吧?
爱情转移 发表于 2015-3-7 12:15 | 显示全部楼层
借鉴一下楼主的成果
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

16

帖子

0

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