system_stm32f10x.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /**
  2. ******************************************************************************
  3. * @file 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>&copy; 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. #include "userSys.h"
  31. /**
  32. * @}
  33. */
  34. /** @addtogroup STM32F10x_System_Private_TypesDefinitions
  35. * @{
  36. */
  37. /**
  38. * @}
  39. */
  40. /** @addtogroup STM32F10x_System_Private_Defines
  41. * @{
  42. */
  43. /*!< Uncomment the line corresponding to the desired System clock (SYSCLK)
  44. frequency (after reset the HSI is used as SYSCLK source)
  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. 3. If none of the define below is enabled, the HSI is used as System clock
  51. source.
  52. 4. The System clock configuration functions provided within this file assume that:
  53. - For Low, Medium and High density Value line devices an external 8MHz
  54. crystal is used to drive the System clock.
  55. - For Low, Medium and High density devices an external 8MHz crystal is
  56. used to drive the System clock.
  57. - For Connectivity line devices an external 25MHz crystal is used to drive
  58. the System clock.
  59. If you are using different crystal you have to adapt those functions accordingly.
  60. */
  61. unsigned char g_ucHSE_Flag;//0--HSE Fail 1--HSE OK
  62. #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  63. /* #define SYSCLK_FREQ_HSE HSE_VALUE */
  64. // #define SYSCLK_FREQ_24MHz 24000000
  65. #else
  66. /* #define SYSCLK_FREQ_HSE HSE_VALUE */
  67. //file modify by wjl 20171109
  68. #define SYSCLK_PREQ_USER
  69. #define SYSCLK_PREQ_CLOCK USER_MAIN_SYSCLK
  70. #endif
  71. /*!< Uncomment the following line if you need to use external SRAM mounted
  72. on STM3210E-EVAL board (STM32 High density and XL-density devices) or on
  73. STM32100E-EVAL board (STM32 High-density value line devices) as data memory */
  74. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
  75. /* #define DATA_IN_ExtSRAM */
  76. #endif
  77. /*!< Uncomment the following line if you need to relocate your vector Table in
  78. Internal SRAM. */
  79. /* #define VECT_TAB_SRAM */
  80. #define VECT_TAB_OFFSET 0x4000 /*!< Vector Table base offset field.
  81. This value must be a multiple of 0x100. */
  82. /**
  83. * @}
  84. */
  85. /** @addtogroup STM32F10x_System_Private_Macros
  86. * @{
  87. */
  88. /**
  89. * @}
  90. */
  91. /** @addtogroup STM32F10x_System_Private_Variables
  92. * @{
  93. */
  94. /*******************************************************************************
  95. * Clock Definitions
  96. *******************************************************************************/
  97. #if defined SYSCLK_PREQ_USER
  98. uint32_t SystemCoreClock = SYSCLK_PREQ_CLOCK;
  99. #else /*!< HSI Selected as System Clock source */
  100. #error "请定义相关时钟选项"
  101. #endif
  102. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  103. /**
  104. * @}
  105. */
  106. /** @addtogroup STM32F10x_System_Private_FunctionPrototypes
  107. * @{
  108. */
  109. static void SetSysClock(void);
  110. #if defined SYSCLK_PREQ_USER
  111. static void SetSysClockToMy(void);
  112. #endif
  113. #ifdef DATA_IN_ExtSRAM
  114. static void SystemInit_ExtMemCtl(void);
  115. #endif /* DATA_IN_ExtSRAM */
  116. /**
  117. * @}
  118. */
  119. /** @addtogroup STM32F10x_System_Private_Functions
  120. * @{
  121. */
  122. /**
  123. * @brief Setup the microcontroller system
  124. * Initialize the Embedded Flash Interface, the PLL and update the
  125. * SystemCoreClock variable.
  126. * @note This function should be used only after reset.
  127. * @param None
  128. * @retval None
  129. */
  130. void SystemInit (void)
  131. {
  132. /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  133. /* Set HSION bit */
  134. RCC->CR |= (uint32_t)0x00000001;
  135. /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
  136. #ifndef STM32F10X_CL
  137. RCC->CFGR &= (uint32_t)0xF8FF0000;
  138. #else
  139. RCC->CFGR &= (uint32_t)0xF0FF0000;
  140. #endif /* STM32F10X_CL */
  141. /* Reset HSEON, CSSON and PLLON bits */
  142. RCC->CR &= (uint32_t)0xFEF6FFFF;
  143. /* Reset HSEBYP bit */
  144. RCC->CR &= (uint32_t)0xFFFBFFFF;
  145. /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  146. RCC->CFGR &= (uint32_t)0xFF80FFFF;
  147. #ifdef STM32F10X_CL
  148. /* Reset PLL2ON and PLL3ON bits */
  149. RCC->CR &= (uint32_t)0xEBFFFFFF;
  150. /* Disable all interrupts and clear pending bits */
  151. RCC->CIR = 0x00FF0000;
  152. /* Reset CFGR2 register */
  153. RCC->CFGR2 = 0x00000000;
  154. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  155. /* Disable all interrupts and clear pending bits */
  156. RCC->CIR = 0x009F0000;
  157. /* Reset CFGR2 register */
  158. RCC->CFGR2 = 0x00000000;
  159. #else
  160. /* Disable all interrupts and clear pending bits */
  161. RCC->CIR = 0x009F0000;
  162. #endif /* STM32F10X_CL */
  163. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
  164. #ifdef DATA_IN_ExtSRAM
  165. SystemInit_ExtMemCtl();
  166. #endif /* DATA_IN_ExtSRAM */
  167. #endif
  168. /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  169. /* Configure the Flash Latency cycles and enable prefetch buffer */
  170. SetSysClock();
  171. #ifdef VECT_TAB_SRAM
  172. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  173. #else
  174. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  175. #endif
  176. }
  177. /**
  178. * @brief Update SystemCoreClock according to Clock Register Values
  179. * @note None
  180. * @param None
  181. * @retval None
  182. */
  183. void SystemCoreClockUpdate (void)
  184. {
  185. uint32_t tmp = 0, pllmull = 0, pllsource = 0;
  186. #ifdef STM32F10X_CL
  187. uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
  188. #endif /* STM32F10X_CL */
  189. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  190. uint32_t prediv1factor = 0;
  191. #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL or STM32F10X_HD_VL */
  192. /* Get SYSCLK source -------------------------------------------------------*/
  193. tmp = RCC->CFGR & RCC_CFGR_SWS;
  194. switch (tmp)
  195. {
  196. case 0x00: /* HSI used as system clock */
  197. SystemCoreClock = HSI_VALUE;
  198. break;
  199. case 0x04: /* HSE used as system clock */
  200. SystemCoreClock = HSE_VALUE;
  201. break;
  202. case 0x08: /* PLL used as system clock */
  203. /* Get PLL clock source and multiplication factor ----------------------*/
  204. pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  205. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  206. #ifndef STM32F10X_CL
  207. pllmull = ( pllmull >> 18) + 2;
  208. if (pllsource == 0x00)
  209. {
  210. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  211. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  212. }
  213. else
  214. {
  215. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  216. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  217. /* HSE oscillator clock selected as PREDIV1 clock entry */
  218. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  219. #else
  220. /* HSE selected as PLL clock entry */
  221. if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
  222. {/* HSE oscillator clock divided by 2 */
  223. SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
  224. }
  225. else
  226. {
  227. SystemCoreClock = HSE_VALUE * pllmull;
  228. }
  229. #endif
  230. }
  231. #else
  232. pllmull = pllmull >> 18;
  233. if (pllmull != 0x0D)
  234. {
  235. pllmull += 2;
  236. }
  237. else
  238. { /* PLL multiplication factor = PLL input clock * 6.5 */
  239. pllmull = 13 / 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. {/* PREDIV1 selected as PLL clock entry */
  248. /* Get PREDIV1 clock source and division factor */
  249. prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
  250. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  251. if (prediv1source == 0)
  252. {
  253. /* HSE oscillator clock selected as PREDIV1 clock entry */
  254. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  255. }
  256. else
  257. {/* PLL2 clock selected as PREDIV1 clock entry */
  258. /* Get PREDIV2 division factor and PLL2 multiplication factor */
  259. prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;
  260. pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2;
  261. SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
  262. }
  263. }
  264. #endif /* STM32F10X_CL */
  265. break;
  266. default:
  267. SystemCoreClock = HSI_VALUE;
  268. break;
  269. }
  270. /* Compute HCLK clock frequency ----------------*/
  271. /* Get HCLK prescaler */
  272. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  273. /* HCLK clock frequency */
  274. SystemCoreClock >>= tmp;
  275. }
  276. /**
  277. * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  278. * @param None
  279. * @retval None
  280. */
  281. static void SetSysClock(void)
  282. {
  283. #if defined SYSCLK_PREQ_USER
  284. SetSysClockToMy();
  285. #endif
  286. /* If none of the define above is enabled, the HSI is used as System clock
  287. source (default after reset) */
  288. }
  289. /**
  290. * @brief Setup the external memory controller. Called in startup_stm32f10x.s
  291. * before jump to __main
  292. * @param None
  293. * @retval None
  294. */
  295. #ifdef DATA_IN_ExtSRAM
  296. /**
  297. * @brief Setup the external memory controller.
  298. * Called in startup_stm32f10x_xx.s/.c before jump to main.
  299. * This function configures the external SRAM mounted on STM3210E-EVAL
  300. * board (STM32 High density devices). This SRAM will be used as program
  301. * data memory (including heap and stack).
  302. * @param None
  303. * @retval None
  304. */
  305. void SystemInit_ExtMemCtl(void)
  306. {
  307. /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
  308. required, then adjust the Register Addresses */
  309. /* Enable FSMC clock */
  310. RCC->AHBENR = 0x00000114;
  311. /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
  312. RCC->APB2ENR = 0x000001E0;
  313. /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
  314. /*---------------- SRAM Address lines configuration -------------------------*/
  315. /*---------------- NOE and NWE configuration --------------------------------*/
  316. /*---------------- NE3 configuration ----------------------------------------*/
  317. /*---------------- NBL0, NBL1 configuration ---------------------------------*/
  318. GPIOD->CRL = 0x44BB44BB;
  319. GPIOD->CRH = 0xBBBBBBBB;
  320. GPIOE->CRL = 0xB44444BB;
  321. GPIOE->CRH = 0xBBBBBBBB;
  322. GPIOF->CRL = 0x44BBBBBB;
  323. GPIOF->CRH = 0xBBBB4444;
  324. GPIOG->CRL = 0x44BBBBBB;
  325. GPIOG->CRH = 0x44444B44;
  326. /*---------------- FSMC Configuration ---------------------------------------*/
  327. /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
  328. FSMC_Bank1->BTCR[4] = 0x00001011;
  329. FSMC_Bank1->BTCR[5] = 0x00000200;
  330. }
  331. #endif /* DATA_IN_ExtSRAM */
  332. #if defined SYSCLK_PREQ_USER
  333. static void SetSysClockToMy(void)
  334. {
  335. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  336. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  337. /* Enable HSE */
  338. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  339. /* Wait till HSE is ready and if Time out is reached exit */
  340. do
  341. {
  342. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  343. StartUpCounter++;
  344. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  345. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  346. {
  347. HSEStatus = (uint32_t)0x01;
  348. }
  349. else
  350. {
  351. HSEStatus = (uint32_t)0x00;
  352. }
  353. if (HSEStatus == (uint32_t)0x01)
  354. {
  355. /* Enable Prefetch Buffer */
  356. FLASH->ACR |= FLASH_ACR_PRFTBE;
  357. /* Flash 2 wait state */
  358. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  359. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  360. /* HCLK = SYSCLK */
  361. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; //配置AHB Prescaler
  362. /* PCLK2 = HCLK */
  363. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;//配置APB2 Prescaler
  364. #ifdef STM32F10X_CL
  365. /* Configure PLLs ------------------------------------------------------*/
  366. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  367. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  368. #if(SYSCLK_PREQ_CLOCK==20000000)
  369. /* PCLK1 = HCLK */
  370. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;//配置APB1 Prescaler 此总线时钟有上限
  371. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  372. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  373. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_PLL2 | //Prediv1 Source Mux选择为PLL2 这个我们需要
  374. RCC_CFGR2_PREDIV2_DIV3 | //Prediv2 除的倍数 这个我们需要
  375. RCC_CFGR2_PLL2MUL10 ); //PLL2Mul 乘的倍数 这个我们需�
  376. //PLL =PREDIV1_CLK = 12M/3*10/8=5M
  377. /* Enable PLL2 */
  378. RCC->CR |= RCC_CR_PLL2ON;
  379. /* Wait till PLL2 is ready */
  380. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  381. {
  382. }
  383. /* PLL configuration: PLLCLK = PREDIV1_CLK * 4 = 20 MHz */
  384. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  385. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  386. RCC_CFGR_PLLMULL4 //倍频倍数 这个我们需要
  387. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  388. );
  389. RCC->CFGR2 |= (uint32_t)RCC_CFGR2_PREDIV1_DIV8; //Prediv1 除的倍数 这个我们需要 //这个在使用PLL2时,一定要放在最后
  390. #elif(SYSCLK_PREQ_CLOCK==24000000)
  391. /* PCLK1 = HCLK */
  392. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;//配置APB1 Prescaler 此总线时钟有上限
  393. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  394. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  395. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_PLL2 | //Prediv1 Source Mux选择为PLL2 这个我们需要
  396. RCC_CFGR2_PREDIV2_DIV3 | //Prediv2 除的倍数 这个我们需要
  397. RCC_CFGR2_PLL2MUL10 ); //PLL2Mul 乘的倍数 这个我们需�
  398. //PLL =PREDIV1_CLK = 12M/3*10/10=4M
  399. /* Enable PLL2 */
  400. RCC->CR |= RCC_CR_PLL2ON;
  401. /* Wait till PLL2 is ready */
  402. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  403. {
  404. }
  405. /* PLL configuration: PLLCLK = PREDIV1_CLK * 6 = 24 MHz */
  406. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  407. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  408. RCC_CFGR_PLLMULL6 //倍频倍数 这个我们需要
  409. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  410. );
  411. RCC->CFGR2 |= (uint32_t)RCC_CFGR2_PREDIV1_DIV10; //Prediv1 除的倍数 这个我们需要 //这个在使用PLL2时,一定要放在最后
  412. #elif(SYSCLK_PREQ_CLOCK==25000000)
  413. /* PCLK1 = HCLK */
  414. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;//配置APB1 Prescaler 此总线时钟有上限
  415. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  416. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  417. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_PLL2 | //Prediv1 Source Mux选择为PLL2 这个我们需要
  418. RCC_CFGR2_PREDIV2_DIV3 | //Prediv2 除的倍数 这个我们需要
  419. RCC_CFGR2_PLL2MUL10 ); //PLL2Mul 乘的倍数 这个我们需�
  420. //PLL =PREDIV1_CLK = 12M/3*10/8=5M
  421. /* Enable PLL2 */
  422. RCC->CR |= RCC_CR_PLL2ON;
  423. /* Wait till PLL2 is ready */
  424. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  425. {
  426. }
  427. /* PLL configuration: PLLCLK = PREDIV1_CLK * 5 = 25 MHz */
  428. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  429. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  430. RCC_CFGR_PLLMULL5 //倍频倍数 这个我们需要
  431. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  432. );
  433. RCC->CFGR2 |= (uint32_t)RCC_CFGR2_PREDIV1_DIV8; //Prediv1 除的倍数 这个我们需要 //这个在使用PLL2时,一定要放在最后
  434. #elif(SYSCLK_PREQ_CLOCK==28000000)
  435. /* PCLK1 = HCLK */
  436. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;//配置APB1 Prescaler 此总线时钟有上限
  437. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  438. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  439. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_PLL2 | //Prediv1 Source Mux选择为PLL2 这个我们需要
  440. RCC_CFGR2_PREDIV2_DIV3 | //Prediv2 除的倍数 这个我们需要
  441. RCC_CFGR2_PLL2MUL10 ); //PLL2Mul 乘的倍数 这个我们需�
  442. //PLL =PREDIV1_CLK = 12M/3*10/10=4M
  443. /* Enable PLL2 */
  444. RCC->CR |= RCC_CR_PLL2ON;
  445. /* Wait till PLL2 is ready */
  446. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  447. {
  448. }
  449. /* PLL configuration: PLLCLK = PREDIV1_CLK * 7 = 28 MHz */
  450. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  451. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  452. RCC_CFGR_PLLMULL7 //倍频倍数 这个我们需要
  453. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  454. );
  455. RCC->CFGR2 |= (uint32_t)RCC_CFGR2_PREDIV1_DIV10; //Prediv1 除的倍数 这个我们需要 //这个在使用PLL2时,一定要放在最后
  456. #elif(SYSCLK_PREQ_CLOCK==30000000)
  457. /* PCLK1 = HCLK */
  458. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;//配置APB1 Prescaler 此总线时钟有上限
  459. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  460. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  461. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_PLL2 | //Prediv1 Source Mux选择为PLL2 这个我们需要
  462. RCC_CFGR2_PREDIV2_DIV3 | //Prediv2 除的倍数 这个我们需要
  463. RCC_CFGR2_PLL2MUL10 ); //PLL2Mul 乘的倍数 这个我们需�
  464. //PLL =PREDIV1_CLK = 12M/3*10/8=5M
  465. /* Enable PLL2 */
  466. RCC->CR |= RCC_CR_PLL2ON;
  467. /* Wait till PLL2 is ready */
  468. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  469. {
  470. }
  471. /* PLL configuration: PLLCLK = PREDIV1_CLK * 6 = 30 MHz */
  472. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  473. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  474. RCC_CFGR_PLLMULL6 //倍频倍数 这个我们需要
  475. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  476. );
  477. RCC->CFGR2 |= (uint32_t)RCC_CFGR2_PREDIV1_DIV8; //Prediv1 除的倍数 这个我们需要 //这个在使用PLL2时,一定要放在最后
  478. #elif(SYSCLK_PREQ_CLOCK==32000000)
  479. /* PCLK1 = HCLK */
  480. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;//配置APB1 Prescaler 此总线时钟有上限
  481. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  482. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  483. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_PLL2 | //Prediv1 Source Mux选择为PLL2 这个我们需要
  484. RCC_CFGR2_PREDIV2_DIV3 | //Prediv2 除的倍数 这个我们需要
  485. RCC_CFGR2_PLL2MUL10 ); //PLL2Mul 乘的倍数 这个我们需�
  486. //PLL =PREDIV1_CLK = 12M/3*10/10=4M
  487. /* Enable PLL2 */
  488. RCC->CR |= RCC_CR_PLL2ON;
  489. /* Wait till PLL2 is ready */
  490. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  491. {
  492. }
  493. /* PLL configuration: PLLCLK = PREDIV1_CLK * 8 = 32 MHz */
  494. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  495. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  496. RCC_CFGR_PLLMULL8 //倍频倍数 这个我们需要
  497. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  498. );
  499. RCC->CFGR2 |= (uint32_t)RCC_CFGR2_PREDIV1_DIV10; //Prediv1 除的倍数 这个我们需要 //这个在使用PLL2时,一定要放在最后
  500. #elif(SYSCLK_PREQ_CLOCK==40000000)
  501. /* PCLK1 = HCLK */
  502. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;//配置APB1 Prescaler 此总线时钟有上限
  503. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  504. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  505. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_PLL2 | //Prediv1 Source Mux选择为PLL2 这个我们需要
  506. RCC_CFGR2_PREDIV2_DIV3 | //Prediv2 除的倍数 这个我们需要
  507. RCC_CFGR2_PLL2MUL10 ); //PLL2Mul 乘的倍数 这个我们需�
  508. //PLL =PREDIV1_CLK = 12M/3*10/5=8M
  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. /* PLL configuration: PLLCLK = PREDIV1_CLK * 5 = 40 MHz */
  516. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  517. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  518. RCC_CFGR_PLLMULL5 //倍频倍数 这个我们需要
  519. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  520. );
  521. RCC->CFGR2 |= (uint32_t)RCC_CFGR2_PREDIV1_DIV5; //Prediv1 除的倍数 这个我们需要 //这个在使用PLL2时,一定要放在最后
  522. #elif(SYSCLK_PREQ_CLOCK==45000000)
  523. /* PCLK1 = HCLK */
  524. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;//配置APB1 Prescaler 此总线时钟有上限
  525. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  526. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  527. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_PLL2 | //Prediv1 Source Mux选择为PLL2 这个我们需要
  528. RCC_CFGR2_PREDIV2_DIV3 | //Prediv2 除的倍数 这个我们需要
  529. RCC_CFGR2_PLL2MUL10 ); //PLL2Mul 乘的倍数 这个我们需�
  530. //PLL =PREDIV1_CLK = 12M/3*10/8=5M
  531. /* Enable PLL2 */
  532. RCC->CR |= RCC_CR_PLL2ON;
  533. /* Wait till PLL2 is ready */
  534. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  535. {
  536. }
  537. /* PLL configuration: PLLCLK = PREDIV1_CLK * 9 = 45 MHz */
  538. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  539. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  540. RCC_CFGR_PLLMULL9 //倍频倍数 这个我们需要
  541. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  542. );
  543. RCC->CFGR2 |= (uint32_t)RCC_CFGR2_PREDIV1_DIV8; //Prediv1 除的倍数 这个我们需要 //这个在使用PLL2时,一定要放在最后
  544. #elif(SYSCLK_PREQ_CLOCK==48000000)
  545. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  546. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  547. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_HSE | //Prediv1 Source Mux选择为HSE 这个我们需要
  548. RCC_CFGR2_PREDIV1_DIV1 | //Prediv1 除的倍数 这个我们需要
  549. RCC_CFGR2_PREDIV2_DIV4 | //Prediv2 除的倍数 这部分我们没有使用,不用理
  550. RCC_CFGR2_PLL2MUL16 ); //PLL2Mul 乘的倍数 这部分我们没有使用,不用理
  551. //PLL =PREDIV1_CLK = 12M/1=12M
  552. #if 0//我们不需要PLL2
  553. /* Enable PLL2 */
  554. RCC->CR |= RCC_CR_PLL2ON;
  555. /* Wait till PLL2 is ready */
  556. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  557. {
  558. }
  559. #endif
  560. /* PLL configuration: PLLCLK = PREDIV1_CLK * 4 = 48 MHz */
  561. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  562. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  563. RCC_CFGR_PLLMULL4 //倍频倍数 这个我们需要
  564. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  565. );
  566. #elif(SYSCLK_PREQ_CLOCK==50000000)
  567. /* PCLK1 = HCLK */
  568. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;//配置APB1 Prescaler 此总线时钟有上限
  569. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  570. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  571. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_PLL2 | //Prediv1 Source Mux选择为PLL2 这个我们需要
  572. RCC_CFGR2_PREDIV2_DIV3 | //Prediv2 除的倍数 这个我们需要
  573. RCC_CFGR2_PLL2MUL10 ); //PLL2Mul 乘的倍数 这个我们需�
  574. //PLL =PREDIV1_CLK = 12M/3*10/4=10M
  575. /* Enable PLL2 */
  576. RCC->CR |= RCC_CR_PLL2ON;
  577. /* Wait till PLL2 is ready */
  578. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  579. {
  580. }
  581. /* PLL configuration: PLLCLK = PREDIV1_CLK * 5 = 45 MHz */
  582. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  583. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  584. RCC_CFGR_PLLMULL5 //倍频倍数 这个我们需要
  585. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  586. );
  587. RCC->CFGR2 |= (uint32_t)RCC_CFGR2_PREDIV1_DIV4; //Prediv1 除的倍数 这个我们需要 //这个在使用PLL2时,一定要放在最后
  588. #elif(SYSCLK_PREQ_CLOCK==57600000)
  589. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;//配置APB1 Prescaler 此总线时钟有上限
  590. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  591. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  592. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV4 | //Prediv2 除的倍数
  593. RCC_CFGR2_PLL2MUL16 | //PLL2Mul 乘的倍数
  594. RCC_CFGR2_PREDIV1SRC_PLL2 | //选择PLL2作为Prediv1的输入源
  595. RCC_CFGR2_PREDIV1_DIV6); //Prediv1 除的倍数
  596. //PLL =PREDIV1_CLK = 12/4*16/6=8M
  597. /* Enable PLL2 */
  598. RCC->CR |= RCC_CR_PLL2ON;
  599. /* Wait till PLL2 is ready */
  600. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  601. {
  602. }
  603. //上面的是配置PLL2的,我们不用,可以不理会
  604. //下面的是配置PLL,我们需要
  605. /* PLL configuration: PLLCLK = PREDIV1_CLK * 6 = 48 MHz */
  606. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  607. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | //Prediv1 除的倍数
  608. RCC_CFGR_PLLSRC_PREDIV1 | //PLL源,选择Prediv1(由)
  609. RCC_CFGR_PLLMULL6); //倍频倍数
  610. #elif(SYSCLK_PREQ_CLOCK==60000000)
  611. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  612. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  613. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_HSE | //Prediv1 Source Mux选择为HSE 这个我们需要
  614. RCC_CFGR2_PREDIV1_DIV1 | //Prediv1 除的倍数 这个我们需要
  615. RCC_CFGR2_PREDIV2_DIV4 | //Prediv2 除的倍数 这部分我们没有使用,不用理
  616. RCC_CFGR2_PLL2MUL16 ); //PLL2Mul 乘的倍数 这部分我们没有使用,不用理
  617. //PLL =PREDIV1_CLK = 12M/1=12M
  618. #if 0//我们不需要PLL2
  619. /* Enable PLL2 */
  620. RCC->CR |= RCC_CR_PLL2ON;
  621. /* Wait till PLL2 is ready */
  622. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  623. {
  624. }
  625. #endif
  626. /* PLL configuration: PLLCLK = PREDIV1_CLK * 5 = 60 MHz */
  627. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  628. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  629. RCC_CFGR_PLLMULL5 //倍频倍数 这个我们需要
  630. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  631. );
  632. #elif(SYSCLK_PREQ_CLOCK==64000000)
  633. /* PCLK1 = HCLK */
  634. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;//配置APB1 Prescaler 此总线时钟有上限
  635. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  636. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  637. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_PLL2 | //Prediv1 Source Mux选择为PLL2 这个我们需要
  638. RCC_CFGR2_PREDIV2_DIV3 | //Prediv2 除的倍数 这个我们需要
  639. RCC_CFGR2_PLL2MUL10 ); //PLL2Mul 乘的倍数 这个我们需�
  640. //PLL =PREDIV1_CLK = 12M/3*10/5=8
  641. /* Enable PLL2 */
  642. RCC->CR |= RCC_CR_PLL2ON;
  643. /* Wait till PLL2 is ready */
  644. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  645. {
  646. }
  647. /* PLL configuration: PLLCLK = PREDIV1_CLK * 8 = 64 MHz */
  648. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  649. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  650. RCC_CFGR_PLLMULL8 //倍频倍数 这个我们需要
  651. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  652. );
  653. RCC->CFGR2 |= (uint32_t)RCC_CFGR2_PREDIV1_DIV5; //Prediv1 除的倍数 这个我们需要 //这个在使用PLL2时,一定要放在最后
  654. #elif(SYSCLK_PREQ_CLOCK==70000000)
  655. /* PCLK1 = HCLK */
  656. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;//配置APB1 Prescaler 此总线时钟有上限
  657. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  658. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  659. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_PLL2 | //Prediv1 Source Mux选择为PLL2 这个我们需要
  660. RCC_CFGR2_PREDIV2_DIV3 | //Prediv2 除的倍数 这个我们需要
  661. RCC_CFGR2_PLL2MUL10 ); //PLL2Mul 乘的倍数 这个我们需�
  662. //PLL =PREDIV1_CLK = 12M/3*10/4=10M
  663. /* Enable PLL2 */
  664. RCC->CR |= RCC_CR_PLL2ON;
  665. /* Wait till PLL2 is ready */
  666. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  667. {
  668. }
  669. /* PLL configuration: PLLCLK = PREDIV1_CLK * 7 = 70 MHz */
  670. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  671. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  672. RCC_CFGR_PLLMULL7 //倍频倍数 这个我们需要
  673. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  674. );
  675. RCC->CFGR2 |= (uint32_t)RCC_CFGR2_PREDIV1_DIV4; //Prediv1 除的倍数 这个我们需要 //这个在使用PLL2时,一定要放在最后
  676. #elif(SYSCLK_PREQ_CLOCK==72000000)
  677. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  678. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  679. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV1SRC_HSE | //Prediv1 Source Mux选择为HSE 这个我们需要
  680. RCC_CFGR2_PREDIV1_DIV1 | //Prediv1 除的倍数 这个我们需要
  681. RCC_CFGR2_PREDIV2_DIV4 | //Prediv2 除的倍数 这部分我们没有使用,不用理
  682. RCC_CFGR2_PLL2MUL16 ); //PLL2Mul 乘的倍数 这部分我们没有使用,不用理
  683. //PLL =PREDIV1_CLK = 12M/1=12M
  684. #if 0//我们不需要PLL2
  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. #endif
  692. /* PLL configuration: PLLCLK = PREDIV1_CLK * 6 = 72 MHz */
  693. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  694. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | //PLL Source Mux 选择Prediv1作为输入源 这个我们需要
  695. RCC_CFGR_PLLMULL6 //倍频倍数 这个我们需要
  696. //RCC_CFGR_PLLXTPRE_PREDIV1 //这个不明白是配置哪个的
  697. );
  698. #else
  699. #error "请选择频率"
  700. #endif
  701. #else
  702. /* PLL configuration: PLLCLK = HSE * 6 = 72 MHz */
  703. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |RCC_CFGR_PLLMULL));
  704. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
  705. #endif /* STM32F10X_CL */
  706. /* Enable PLL */
  707. RCC->CR |= RCC_CR_PLLON;
  708. /* Wait till PLL is ready */
  709. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  710. {
  711. }
  712. /* Select PLL as system clock source */
  713. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  714. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;//System Clock Mux选择
  715. /* Wait till PLL is used as system clock source */
  716. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  717. {
  718. }
  719. }
  720. else
  721. { /* If HSE fails to start-up, the application will have wrong clock
  722. configuration. User can add here some code to deal with this error */
  723. g_ucHSE_Flag=0;
  724. while(1);
  725. }
  726. }
  727. #endif
  728. /**
  729. * @}
  730. */
  731. /**
  732. * @}
  733. */
  734. /**
  735. * @}
  736. */
  737. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/