Clock.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "Clock.h"
  2. void HSEJCQ_Init(void)
  3. {
  4. /******************************************************************************/
  5. /* 寄存器配置高速的外部(HSE)时钟 */
  6. /******************************************************************************/
  7. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  8. /* 复位RCC的所有寄存器 */
  9. RCC_DeInit();
  10. /* Enable HSE */
  11. /* 使能HSE */
  12. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  13. /* Wait till HSE is ready and if Time out is reached exit */
  14. /* 等待HSE启动稳定,如果超时则退出 */
  15. do
  16. {
  17. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  18. StartUpCounter++;
  19. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  20. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  21. {
  22. HSEStatus = (uint32_t)0x01;
  23. }
  24. else
  25. {
  26. HSEStatus = (uint32_t)0x00;
  27. }
  28. /* HSE 启动成功 */
  29. if (HSEStatus == (uint32_t)0x01)
  30. {
  31. /* Select regulator voltage output Scale 1 mode */
  32. /* 选择电压调节器的模式为1 */
  33. RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  34. PWR->CR |= PWR_CR_VOS;
  35. /* HCLK = SYSCLK / 1*/
  36. RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
  37. /* PCLK2 = HCLK / 2*/
  38. RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
  39. /* PCLK1 = HCLK / 4*/
  40. RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
  41. /* Configure the main PLL */
  42. /* 配置主PLL */
  43. // RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
  44. // (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
  45. RCC->PLLCFGR = 25 | (336 << 6) | (((2 >> 1) -1) << 16) |
  46. (RCC_PLLCFGR_PLLSRC_HSE) | (7 << 24);
  47. /* Enable the main PLL */
  48. /* 使能主PLL */
  49. RCC->CR |= RCC_CR_PLLON;
  50. /* Wait till the main PLL is ready */
  51. /* 等待主PLL稳定 */
  52. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  53. {
  54. }
  55. /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  56. /* 配置FLASH预取指,指令缓存,数据缓存,等待周期 */
  57. FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
  58. /* Select the main PLL as system clock source */
  59. /* 选择主PLL时钟作为系统时钟 */
  60. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  61. RCC->CFGR |= RCC_CFGR_SW_PLL;
  62. /* Wait till the main PLL is used as system clock source */
  63. /* 确保主PLL时钟选为系统时钟 */
  64. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
  65. {
  66. }
  67. }
  68. else
  69. {
  70. /* HSE 启动失败,在这里添加启动失败的处理代码 */
  71. }
  72. }
  73. /******************************************************************************/
  74. /* 库函数配置高速的外部(HSE)时钟 */
  75. /******************************************************************************/
  76. void HSE_Init(uint32_t PLLM, uint32_t PLLN, uint32_t PLLP, uint32_t PLLQ)
  77. {
  78. ErrorStatus HSE_ErrorStatus = ERROR;
  79. /* 复位RCC的所有寄存器 */
  80. RCC_DeInit();
  81. /* 使能HSE */
  82. RCC_HSEConfig(RCC_HSE_ON);
  83. HSE_ErrorStatus = RCC_WaitForHSEStartUp();
  84. if( HSE_ErrorStatus == SUCCESS )
  85. {
  86. /* 选择电压调节器的模式为1 */
  87. RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  88. PWR->CR |= PWR_CR_VOS;
  89. RCC_HCLKConfig(RCC_SYSCLK_Div1);
  90. RCC_PCLK1Config(RCC_HCLK_Div4);
  91. RCC_PCLK2Config(RCC_HCLK_Div2);
  92. RCC_PLLConfig(RCC_PLLSource_HSE, PLLM, PLLN, PLLP, PLLQ);
  93. RCC_PLLCmd(ENABLE);
  94. while( RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET )
  95. {}
  96. /* 配置FLASH预取指,指令缓存,数据缓存,等待周期 */
  97. FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
  98. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  99. while( RCC_GetSYSCLKSource() != 0x08 ){}
  100. }
  101. else
  102. {
  103. /* HSE启动失败,在这里添加错误处理代码 */
  104. }
  105. }
  106. /******************************************************************************/
  107. /* 库函数配置高速的内部(HSI)时钟 */
  108. /******************************************************************************/
  109. void HSI_Init(uint32_t PLLM, uint32_t PLLN, uint32_t PLLP, uint32_t PLLQ)
  110. {
  111. volatile uint32_t HSI_ErrorStatus = 0;
  112. /* 复位RCC的所有寄存器 */
  113. RCC_DeInit();
  114. /* 使能HSI */
  115. RCC_HSICmd(ENABLE);
  116. HSI_ErrorStatus = RCC->CR & RCC_CR_HSIRDY;
  117. if( HSI_ErrorStatus == RCC_CR_HSIRDY )
  118. {
  119. /* 选择电压调节器的模式为1 */
  120. RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  121. PWR->CR |= PWR_CR_VOS;
  122. RCC_HCLKConfig(RCC_SYSCLK_Div1);
  123. RCC_PCLK1Config(RCC_HCLK_Div4);
  124. RCC_PCLK2Config(RCC_HCLK_Div2);
  125. RCC_PLLConfig(RCC_PLLSource_HSI, PLLM, PLLN, PLLP, PLLQ);
  126. RCC_PLLCmd(ENABLE);
  127. while( RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET )
  128. {}
  129. /* 配置FLASH预取指,指令缓存,数据缓存,等待周期 */
  130. FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
  131. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  132. while( RCC_GetSYSCLKSource() != 0x08 ){}
  133. }
  134. else
  135. {
  136. /* HSI启动失败,在这里添加错误处理代码 */
  137. }
  138. }
  139. /******************************************************************************/
  140. /* 初始化MCO1,MCO2,可用于给外部输出时钟源 */
  141. /******************************************************************************/
  142. void MCO1_Init(void)
  143. {
  144. GPIO_InitTypeDef GPIO_InitStructure;
  145. RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA, ENABLE);
  146. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  147. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  148. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  149. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  150. GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
  151. GPIO_Init(GPIOA, &GPIO_InitStructure);
  152. }
  153. void MCO2_Init(void)
  154. {
  155. GPIO_InitTypeDef GPIO_InitStructure;
  156. RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOC, ENABLE);
  157. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  158. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  159. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  160. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  161. GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
  162. GPIO_Init(GPIOC, &GPIO_InitStructure);
  163. }