system_stm32f10x.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. /**
  31. * @}
  32. */
  33. /** @addtogroup STM32F10x_System_Private_TypesDefinitions
  34. * @{
  35. */
  36. /**
  37. * @}
  38. */
  39. /** @addtogroup STM32F10x_System_Private_Defines
  40. * @{
  41. */
  42. /*!< Uncomment the line corresponding to the desired System clock (SYSCLK)
  43. frequency (after reset the HSI is used as SYSCLK source)
  44. IMPORTANT NOTE:
  45. ==============
  46. 1. After each device reset the HSI is used as System clock source.
  47. 2. Please make sure that the selected System clock doesn't exceed your device's
  48. maximum frequency.
  49. 3. If none of the define below is enabled, the HSI is used as System clock
  50. source.
  51. 4. The System clock configuration functions provided within this file assume that:
  52. - For Low, Medium and High density Value line devices an external 8MHz
  53. crystal is used to drive the System clock.
  54. - For Low, Medium and High density devices an external 8MHz crystal is
  55. used to drive the System clock.
  56. - For Connectivity line devices an external 25MHz crystal is used to drive
  57. the System clock.
  58. If you are using different crystal you have to adapt those functions accordingly.
  59. */
  60. unsigned char g_ucHSE_Flag;//0--HSE Fail 1--HSE OK
  61. #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  62. /* #define SYSCLK_FREQ_HSE HSE_VALUE */
  63. // #define SYSCLK_FREQ_24MHz 24000000
  64. #else
  65. /* #define SYSCLK_FREQ_HSE HSE_VALUE */
  66. //file modify by wjl 20171109
  67. #define SYSCLK_PREQ_USER
  68. #define SYSCLK_PREQ_CLOCK USER_MAIN_SYSCLK
  69. #endif
  70. /*!< Uncomment the following line if you need to use external SRAM mounted
  71. on STM3210E-EVAL board (STM32 High density and XL-density devices) or on
  72. STM32100E-EVAL board (STM32 High-density value line devices) as data memory */
  73. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
  74. /* #define DATA_IN_ExtSRAM */
  75. #endif
  76. /*!< Uncomment the following line if you need to relocate your vector Table in
  77. Internal SRAM. */
  78. /* #define VECT_TAB_SRAM */
  79. #define VECT_TAB_OFFSET 0x4000 /*!< Vector Table base offset field.
  80. This value must be a multiple of 0x100. */
  81. /**
  82. * @}
  83. */
  84. /** @addtogroup STM32F10x_System_Private_Macros
  85. * @{
  86. */
  87. /**
  88. * @}
  89. */
  90. /** @addtogroup STM32F10x_System_Private_Variables
  91. * @{
  92. */
  93. /*******************************************************************************
  94. * Clock Definitions
  95. *******************************************************************************/
  96. #if defined SYSCLK_PREQ_USER
  97. uint32_t SystemCoreClock = SYSCLK_PREQ_CLOCK;
  98. #else /*!< HSI Selected as System Clock source */
  99. #error "请定义相关时钟选项"
  100. #endif
  101. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  102. /**
  103. * @}
  104. */
  105. /** @addtogroup STM32F10x_System_Private_FunctionPrototypes
  106. * @{
  107. */
  108. static void SetSysClock(void);
  109. #if defined SYSCLK_PREQ_USER
  110. static void SetSysClockToMy(void);
  111. #endif
  112. #ifdef DATA_IN_ExtSRAM
  113. static void SystemInit_ExtMemCtl(void);
  114. #endif /* DATA_IN_ExtSRAM */
  115. /**
  116. * @}
  117. */
  118. /** @addtogroup STM32F10x_System_Private_Functions
  119. * @{
  120. */
  121. /**
  122. * @brief Setup the microcontroller system
  123. * Initialize the Embedded Flash Interface, the PLL and update the
  124. * SystemCoreClock variable.
  125. * @note This function should be used only after reset.
  126. * @param None
  127. * @retval None
  128. */
  129. void SystemInit (void)
  130. {
  131. /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  132. /* Set HSION bit */
  133. RCC->CR |= (uint32_t)0x00000001;
  134. /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
  135. #ifndef STM32F10X_CL
  136. RCC->CFGR &= (uint32_t)0xF8FF0000;
  137. #else
  138. RCC->CFGR &= (uint32_t)0xF0FF0000;
  139. #endif /* STM32F10X_CL */
  140. /* Reset HSEON, CSSON and PLLON bits */
  141. RCC->CR &= (uint32_t)0xFEF6FFFF;
  142. /* Reset HSEBYP bit */
  143. RCC->CR &= (uint32_t)0xFFFBFFFF;
  144. /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  145. RCC->CFGR &= (uint32_t)0xFF80FFFF;
  146. #ifdef STM32F10X_CL
  147. /* Reset PLL2ON and PLL3ON bits */
  148. RCC->CR &= (uint32_t)0xEBFFFFFF;
  149. /* Disable all interrupts and clear pending bits */
  150. RCC->CIR = 0x00FF0000;
  151. /* Reset CFGR2 register */
  152. RCC->CFGR2 = 0x00000000;
  153. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  154. /* Disable all interrupts and clear pending bits */
  155. RCC->CIR = 0x009F0000;
  156. /* Reset CFGR2 register */
  157. RCC->CFGR2 = 0x00000000;
  158. #else
  159. /* Disable all interrupts and clear pending bits */
  160. RCC->CIR = 0x009F0000;
  161. #endif /* STM32F10X_CL */
  162. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
  163. #ifdef DATA_IN_ExtSRAM
  164. SystemInit_ExtMemCtl();
  165. #endif /* DATA_IN_ExtSRAM */
  166. #endif
  167. /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  168. /* Configure the Flash Latency cycles and enable prefetch buffer */
  169. SetSysClock();
  170. #ifdef VECT_TAB_SRAM
  171. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  172. #else
  173. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  174. #endif
  175. }
  176. /**
  177. * @brief Update SystemCoreClock according to Clock Register Values
  178. * @note None
  179. * @param None
  180. * @retval None
  181. */
  182. void SystemCoreClockUpdate (void)
  183. {
  184. uint32_t tmp = 0, pllmull = 0, pllsource = 0;
  185. #ifdef STM32F10X_CL
  186. uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
  187. #endif /* STM32F10X_CL */
  188. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  189. uint32_t prediv1factor = 0;
  190. #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL or STM32F10X_HD_VL */
  191. /* Get SYSCLK source -------------------------------------------------------*/
  192. tmp = RCC->CFGR & RCC_CFGR_SWS;
  193. switch (tmp)
  194. {
  195. case 0x00: /* HSI used as system clock */
  196. SystemCoreClock = HSI_VALUE;
  197. break;
  198. case 0x04: /* HSE used as system clock */
  199. SystemCoreClock = HSE_VALUE;
  200. break;
  201. case 0x08: /* PLL used as system clock */
  202. /* Get PLL clock source and multiplication factor ----------------------*/
  203. pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  204. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  205. #ifndef STM32F10X_CL
  206. pllmull = ( pllmull >> 18) + 2;
  207. if (pllsource == 0x00)
  208. {
  209. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  210. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  211. }
  212. else
  213. {
  214. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  215. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  216. /* HSE oscillator clock selected as PREDIV1 clock entry */
  217. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  218. #else
  219. /* HSE selected as PLL clock entry */
  220. if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
  221. {/* HSE oscillator clock divided by 2 */
  222. SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
  223. }
  224. else
  225. {
  226. SystemCoreClock = HSE_VALUE * pllmull;
  227. }
  228. #endif
  229. }
  230. #else
  231. pllmull = pllmull >> 18;
  232. if (pllmull != 0x0D)
  233. {
  234. pllmull += 2;
  235. }
  236. else
  237. { /* PLL multiplication factor = PLL input clock * 6.5 */
  238. pllmull = 13 / 2;
  239. }
  240. if (pllsource == 0x00)
  241. {
  242. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  243. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  244. }
  245. else
  246. {/* PREDIV1 selected as PLL clock entry */
  247. /* Get PREDIV1 clock source and division factor */
  248. prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
  249. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  250. if (prediv1source == 0)
  251. {
  252. /* HSE oscillator clock selected as PREDIV1 clock entry */
  253. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  254. }
  255. else
  256. {/* PLL2 clock selected as PREDIV1 clock entry */
  257. /* Get PREDIV2 division factor and PLL2 multiplication factor */
  258. prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;
  259. pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2;
  260. SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
  261. }
  262. }
  263. #endif /* STM32F10X_CL */
  264. break;
  265. default:
  266. SystemCoreClock = HSI_VALUE;
  267. break;
  268. }
  269. /* Compute HCLK clock frequency ----------------*/
  270. /* Get HCLK prescaler */
  271. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  272. /* HCLK clock frequency */
  273. SystemCoreClock >>= tmp;
  274. }
  275. /**
  276. * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  277. * @param None
  278. * @retval None
  279. */
  280. static void SetSysClock(void)
  281. {
  282. #if defined SYSCLK_PREQ_USER
  283. SetSysClockToMy();
  284. #endif
  285. /* If none of the define above is enabled, the HSI is used as System clock
  286. source (default after reset) */
  287. }
  288. /**
  289. * @brief Setup the external memory controller. Called in startup_stm32f10x.s
  290. * before jump to __main
  291. * @param None
  292. * @retval None
  293. */
  294. #ifdef DATA_IN_ExtSRAM
  295. /**
  296. * @brief Setup the external memory controller.
  297. * Called in startup_stm32f10x_xx.s/.c before jump to main.
  298. * This function configures the external SRAM mounted on STM3210E-EVAL
  299. * board (STM32 High density devices). This SRAM will be used as program
  300. * data memory (including heap and stack).
  301. * @param None
  302. * @retval None
  303. */
  304. void SystemInit_ExtMemCtl(void)
  305. {
  306. /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
  307. required, then adjust the Register Addresses */
  308. /* Enable FSMC clock */
  309. RCC->AHBENR = 0x00000114;
  310. /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
  311. RCC->APB2ENR = 0x000001E0;
  312. /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
  313. /*---------------- SRAM Address lines configuration -------------------------*/
  314. /*---------------- NOE and NWE configuration --------------------------------*/
  315. /*---------------- NE3 configuration ----------------------------------------*/
  316. /*---------------- NBL0, NBL1 configuration ---------------------------------*/
  317. GPIOD->CRL = 0x44BB44BB;
  318. GPIOD->CRH = 0xBBBBBBBB;
  319. GPIOE->CRL = 0xB44444BB;
  320. GPIOE->CRH = 0xBBBBBBBB;
  321. GPIOF->CRL = 0x44BBBBBB;
  322. GPIOF->CRH = 0xBBBB4444;
  323. GPIOG->CRL = 0x44BBBBBB;
  324. GPIOG->CRH = 0x44444B44;
  325. /*---------------- FSMC Configuration ---------------------------------------*/
  326. /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
  327. FSMC_Bank1->BTCR[4] = 0x00001011;
  328. FSMC_Bank1->BTCR[5] = 0x00000200;
  329. }
  330. #endif /* DATA_IN_ExtSRAM */
  331. #if defined SYSCLK_PREQ_USER
  332. static void SetSysClockToMy(void)
  333. {
  334. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  335. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  336. /* Enable HSE */
  337. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  338. /* Wait till HSE is ready and if Time out is reached exit */
  339. do
  340. {
  341. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  342. StartUpCounter++;
  343. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  344. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  345. {
  346. HSEStatus = (uint32_t)0x01;
  347. }
  348. else
  349. {
  350. HSEStatus = (uint32_t)0x00;
  351. }
  352. if (HSEStatus == (uint32_t)0x01)
  353. {
  354. /* Enable Prefetch Buffer */
  355. FLASH->ACR |= FLASH_ACR_PRFTBE;
  356. /* Flash 2 wait state */
  357. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  358. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  359. /* HCLK = SYSCLK */
  360. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  361. /* PCLK2 = HCLK */
  362. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  363. /* PCLK1 = HCLK */
  364. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;//此总线时钟上限一般比较慢
  365. #ifdef STM32F10X_CL
  366. /* Configure PLLs ------------------------------------------------------*/
  367. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  368. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  369. #if(SYSCLK_PREQ_CLOCK==32000000)
  370. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  371. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  372. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV4 | //Prediv2 除的倍数
  373. RCC_CFGR2_PLL2MUL16 | //PLL2Mul 乘的倍数
  374. RCC_CFGR2_PREDIV1SRC_PLL2 | //选择PLL2作为Prediv1的输入源
  375. RCC_CFGR2_PREDIV1_DIV6); //Prediv1 除的倍数
  376. //PLL =PREDIV1_CLK = 12/4*16/6=8M
  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. //上面的是配置PLL2的,我们不用,可以不理会
  384. //下面的是配置PLL,我们需要
  385. /* PLL configuration: PLLCLK = PREDIV1_CLK * 5 = 40 MHz */
  386. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  387. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | //Prediv1 除的倍数
  388. RCC_CFGR_PLLSRC_PREDIV1 | //PLL源,选择Prediv1(由)
  389. RCC_CFGR_PLLMULL4); //倍频倍数
  390. #elif(SYSCLK_PREQ_CLOCK==40000000)
  391. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  392. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  393. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV4 | //Prediv2 除的倍数
  394. RCC_CFGR2_PLL2MUL16 | //PLL2Mul 乘的倍数
  395. RCC_CFGR2_PREDIV1SRC_PLL2 | //选择PLL2作为Prediv1的输入源
  396. RCC_CFGR2_PREDIV1_DIV6); //Prediv1 除的倍数
  397. //PLL =PREDIV1_CLK = 12/4*16/6=8M
  398. /* Enable PLL2 */
  399. RCC->CR |= RCC_CR_PLL2ON;
  400. /* Wait till PLL2 is ready */
  401. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  402. {
  403. }
  404. //上面的是配置PLL2的,我们不用,可以不理会
  405. //下面的是配置PLL,我们需要
  406. /* PLL configuration: PLLCLK = PREDIV1_CLK * 5 = 40 MHz */
  407. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  408. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | //Prediv1 除的倍数
  409. RCC_CFGR_PLLSRC_PREDIV1 | //PLL源,选择Prediv1(由)
  410. RCC_CFGR_PLLMULL5); //倍频倍数
  411. #elif(SYSCLK_PREQ_CLOCK==48000000)
  412. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  413. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  414. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV4 | //Prediv2 除的倍数
  415. RCC_CFGR2_PLL2MUL16 | //PLL2Mul 乘的倍数
  416. RCC_CFGR2_PREDIV1SRC_PLL2 | //选择PLL2作为Prediv1的输入源
  417. RCC_CFGR2_PREDIV1_DIV6); //Prediv1 除的倍数
  418. //PLL =PREDIV1_CLK = 12/4*16/6=8M
  419. /* Enable PLL2 */
  420. RCC->CR |= RCC_CR_PLL2ON;
  421. /* Wait till PLL2 is ready */
  422. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  423. {
  424. }
  425. //上面的是配置PLL2的,我们不用,可以不理会
  426. //下面的是配置PLL,我们需要
  427. /* PLL configuration: PLLCLK = PREDIV1_CLK * 6 = 48 MHz */
  428. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  429. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | //Prediv1 除的倍数
  430. RCC_CFGR_PLLSRC_PREDIV1 | //PLL源,选择Prediv1(由)
  431. RCC_CFGR_PLLMULL6); //倍频倍数
  432. #elif(SYSCLK_PREQ_CLOCK==56000000)
  433. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  434. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  435. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV4 | //Prediv2 除的倍数
  436. RCC_CFGR2_PLL2MUL16 | //PLL2Mul 乘的倍数
  437. RCC_CFGR2_PREDIV1SRC_PLL2 | //选择PLL2作为Prediv1的输入源
  438. RCC_CFGR2_PREDIV1_DIV6); //Prediv1 除的倍数
  439. //PLL =PREDIV1_CLK = 12/4*16/6=8M
  440. /* Enable PLL2 */
  441. RCC->CR |= RCC_CR_PLL2ON;
  442. /* Wait till PLL2 is ready */
  443. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  444. {
  445. }
  446. //上面的是配置PLL2的,我们不用,可以不理会
  447. //下面的是配置PLL,我们需要
  448. /* PLL configuration: PLLCLK = PREDIV1_CLK * 7 = 56 MHz */
  449. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  450. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | //Prediv1 除的倍数
  451. RCC_CFGR_PLLSRC_PREDIV1 | //PLL源,选择Prediv1(由)
  452. RCC_CFGR_PLLMULL7); //倍频倍数
  453. #elif(SYSCLK_PREQ_CLOCK==64000000)
  454. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  455. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  456. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV4 | //Prediv2 除的倍数
  457. RCC_CFGR2_PLL2MUL16 | //PLL2Mul 乘的倍数
  458. RCC_CFGR2_PREDIV1SRC_PLL2 | //选择PLL2作为Prediv1的输入源
  459. RCC_CFGR2_PREDIV1_DIV6); //Prediv1 除的倍数
  460. //PLL =PREDIV1_CLK = 12/4*16/6=8M
  461. /* Enable PLL2 */
  462. RCC->CR |= RCC_CR_PLL2ON;
  463. /* Wait till PLL2 is ready */
  464. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  465. {
  466. }
  467. //上面的是配置PLL2的,我们不用,可以不理会
  468. //下面的是配置PLL,我们需要
  469. /* PLL configuration: PLLCLK = PREDIV1_CLK * 8 = 64 MHz */
  470. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  471. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | //Prediv1 除的倍数
  472. RCC_CFGR_PLLSRC_PREDIV1 | //PLL源,选择Prediv1(由)
  473. RCC_CFGR_PLLMULL8); //倍频倍数
  474. #elif(SYSCLK_PREQ_CLOCK==72000000)
  475. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  476. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  477. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV4 | //Prediv2 除的倍数
  478. RCC_CFGR2_PLL2MUL16 | //PLL2Mul 乘的倍数
  479. RCC_CFGR2_PREDIV1SRC_PLL2 | //选择PLL2作为Prediv1的输入源
  480. RCC_CFGR2_PREDIV1_DIV6); //Prediv1 除的倍数
  481. //PLL =PREDIV1_CLK = 12/4*16/6=8M
  482. /* Enable PLL2 */
  483. RCC->CR |= RCC_CR_PLL2ON;
  484. /* Wait till PLL2 is ready */
  485. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  486. {
  487. }
  488. //上面的是配置PLL2的,我们不用,可以不理会
  489. //下面的是配置PLL,我们需要
  490. /* PLL configuration: PLLCLK = PREDIV1_CLK * 9 = 72 MHz */
  491. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  492. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | //Prediv1 除的倍数
  493. RCC_CFGR_PLLSRC_PREDIV1 | //PLL源,选择Prediv1(由)
  494. RCC_CFGR_PLLMULL9); //倍频倍数
  495. #endif
  496. #else
  497. /* PLL configuration: PLLCLK = HSE * 6 = 72 MHz */
  498. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |RCC_CFGR_PLLMULL));
  499. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
  500. #endif /* STM32F10X_CL */
  501. /* Enable PLL */
  502. RCC->CR |= RCC_CR_PLLON;
  503. /* Wait till PLL is ready */
  504. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  505. {
  506. }
  507. /* Select PLL as system clock source */
  508. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  509. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  510. /* Wait till PLL is used as system clock source */
  511. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  512. {
  513. }
  514. }
  515. else
  516. { /* If HSE fails to start-up, the application will have wrong clock
  517. configuration. User can add here some code to deal with this error */
  518. g_ucHSE_Flag=0;
  519. while(1);
  520. }
  521. }
  522. #endif
  523. /**
  524. * @}
  525. */
  526. /**
  527. * @}
  528. */
  529. /**
  530. * @}
  531. */
  532. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/