system_stm32f10x.c 35 KB

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