查看: 6232|回复: 1

[资料] GD32F103XX 108M时钟配置【转】

[复制链接]
  • TA的每日心情
    奋斗
    2016-8-15 09:28
  • 签到天数: 222 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2015-5-13 09:47:59 | 显示全部楼层 |阅读模式
    分享到:
    GD32F103XX 108M时钟配置【转】

    通过HSE(8MHz)得到108MHz SysClk的配置过程:
    1. static void SetSysClockTo108(void)
    2. {
    3.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
    4.   
    5.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   
    6.   /* Enable HSE */   
    7.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);

    8.   /* Wait till HSE is ready and if Time out is reached exit */
    9.   do
    10.   {
    11.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
    12.     StartUpCounter++;  
    13.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));

    14.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
    15.   {
    16.     HSEStatus = (uint32_t)0x01;
    17.   }
    18.   else
    19.   {
    20.     HSEStatus = (uint32_t)0x00;
    21.   }  

    22.   if (HSEStatus == (uint32_t)0x01)
    23.   {

    24.     /* HCLK = SYSCLK */
    25.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
    26.       
    27.     /* PCLK2 = HCLK */
    28.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
    29.    
    30.     /* PCLK1 = HCLK */
    31.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
    32.    
    33.     /*  PLL configuration: PLLCLK = HSE/2* (15+12) = 108 MHz */
    34.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
    35.                                         RCC_CFGR_PLLMULL));
    36.     RCC->CFGR |= (uint32_t)(0x08000000 | RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL12);

    37.     /* Enable PLL */
    38.     RCC->CR |= RCC_CR_PLLON;

    39.     /* Wait till PLL is ready */
    40.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
    41.     {
    42.     }
    43.    
    44.     /* Select PLL as system clock source */
    45.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
    46.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   

    47.     /* Wait till PLL is used as system clock source */
    48.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
    49.     {
    50.     }
    51.   }
    52.   else
    53.   { /* If HSE fails to start-up, the application will have wrong clock
    54.          configuration. User can add here some code to deal with this error */
    55.   }
    56. }

    57. 通过HSI(8MHz)得到108MHzSysClk的配置过程

    58. static void SetSysClockTo108(void)
    59. {
    60.    /* HCLK = SYSCLK */
    61.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;

    62.     /* PCLK2 = HCLK */
    63.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
    64.    
    65.     /* PCLK1 = HCLK */
    66.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
    67.    
    68.     /*  PLL configuration: PLLCLK = HSI/2* (15+12) = 108 MHz */
    69.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL));
    70.     RCC->CFGR |= (uint32_t)(0x08000000 | RCC_CFGR_PLLSRC_HSI_Div2  | RCC_CFGR_PLLMULL12);

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

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

    81.     /* Wait till PLL is used as system clock source */
    82.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
    83.     {
    84.     }
    85. }



    86. 如果使用串口
    87. 还应该修改 RCC_GetClocksFreq 函数,以获得正确的系统时钟,具体修改方法


    88. RCC_GetClocksFreq的修改:
    89. 先修改以下几个值:
    90. #define CFGR_PLL_Mask             ((uint32_t)0xF7C0FFFF)
    91. #define CFGR_PLLMull_Mask         ((uint32_t)0x083C0000)
    92. #define CFGR_ADCPRE_Reset_Mask    ((uint32_t)0xEFFF3FFF)
    93. #define CFGR_ADCPRE_Set_Mask      ((uint32_t)0x1000C000)

    94. static __I uint8_t ADCPrescTable[8] = {2, 4, 6, 8, 2, 12, 6, 16};

    95. 修改函数:
    96. void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks)
    97. {
    98.   uint32_t tmp = 0, pllmull = 0, pllsource = 0, presc = 0;

    99.   /* Get SYSCLK source -------------------------------------------------------*/
    100.   tmp = RCC->CFGR & CFGR_SWS_Mask;

    101.   switch (tmp)
    102.   {
    103.     case 0x00:  /* HSI used as system clock */
    104.       RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
    105.       break;
    106.     case 0x04:  /* HSE used as system clock */
    107.       RCC_Clocks->SYSCLK_Frequency = HSE_VALUE;
    108.       break;
    109.     case 0x08:  /* PLL used as system clock */

    110.       /* Get PLL clock source and multiplication factor ----------------------*/
    111.       pllmull = RCC->CFGR & CFGR_PLLMull_Mask;
    112.       pllsource = RCC->CFGR & CFGR_PLLSRC_Mask;

    113.       if(((pllmull)&(0x08000000)) != 0)
    114.           pllmull = (((pllmull)&(0xF7FFFFFF)) >> 18) + 17;
    115.       else
    116.           pllmull = ( pllmull >> 18) +2;

    117.       if (pllsource == 0x00)
    118.       {/* HSI oscillator clock divided by 2 selected as PLL clock entry */
    119.         RCC_Clocks->SYSCLK_Frequency = (HSI_VALUE >> 1) * pllmull;
    120.       }
    121.       else
    122.       {
    123.         /* HSE selected as PLL clock entry */
    124.         if ((RCC->CFGR & CFGR_PLLXTPRE_Mask) != (uint32_t)RESET)
    125.         {/* HSE oscillator clock divided by 2 */
    126.           RCC_Clocks->SYSCLK_Frequency = (HSE_VALUE >> 1) * pllmull;
    127.         }
    128.         else
    129.         {
    130.           RCC_Clocks->SYSCLK_Frequency = HSE_VALUE * pllmull;
    131.         }
    132.       }
    133.       break;
    134.     default:
    135.       RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
    136.       break;
    137.   }

    138.   /* Compute HCLK, PCLK1, PCLK2 and ADCCLK clocks frequencies ----------------*/
    139.   /* Get HCLK prescaler */
    140.   tmp = RCC->CFGR & CFGR_HPRE_Set_Mask;
    141.   tmp = tmp >> 4;
    142.   presc = APBAHBPrescTable[tmp];
    143.   /* HCLK clock frequency */
    144.   RCC_Clocks->HCLK_Frequency = RCC_Clocks->SYSCLK_Frequency >> presc;
    145.   /* Get PCLK1 prescaler */
    146.   tmp = RCC->CFGR & CFGR_PPRE1_Set_Mask;
    147.   tmp = tmp >> 8;
    148.   presc = APBAHBPrescTable[tmp];
    149.   /* PCLK1 clock frequency */
    150.   RCC_Clocks->PCLK1_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
    151.   /* Get PCLK2 prescaler */
    152.   tmp = RCC->CFGR & CFGR_PPRE2_Set_Mask;
    153.   tmp = tmp >> 11;
    154.   presc = APBAHBPrescTable[tmp];
    155.   /* PCLK2 clock frequency */
    156.   RCC_Clocks->PCLK2_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
    157.   /* Get ADCCLK prescaler */
    158.   tmp = RCC->CFGR & CFGR_ADCPRE_Set_Mask;
    159. tmp = ((tmp >> 14)&(0xF))+(tmp >> 26);
    160.   presc = ADCPrescTable[tmp];
    161.   /* ADCCLK clock frequency */
    162.   RCC_Clocks->ADCCLK_Frequency = RCC_Clocks->PCLK2_Frequency / presc;
    163. }
    复制代码
    108MHz.rar (9.31 KB, 下载次数: 44)
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2016-6-15 16:25
  • 签到天数: 3 天

    连续签到: 1 天

    [LV.2]偶尔看看I

    发表于 2017-9-22 09:20:13 | 显示全部楼层
    学习了,多谢群主!
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-11-24 12:56 , Processed in 0.115589 second(s), 18 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.