system_stm32f10x.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f10x.c
  4. * @author MCD Application Team
  5. * @version V3.5.0
  6. * @date 08-April-2011
  7. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  8. *
  9. * 1. This file provides two functions and one global variable to be called from
  10. * user application:
  11. * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
  12. * factors, AHB/APBx prescalers and Flash settings).
  13. * This function is called at startup just after reset and
  14. * before branch to main program. This call is made inside
  15. * the "startup_stm32f10x_xx.s" file.
  16. *
  17. * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  18. * by the user application to setup the SysTick
  19. * timer or configure other parameters.
  20. *
  21. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  22. * be called whenever the core clock is changed
  23. * during program execution.
  24. *
  25. * 2. After each device reset the HSI (8 MHz) is used as system clock source.
  26. * Then SystemInit() function is called, in "startup_stm32f10x_xx.s" file, to
  27. * configure the system clock before to branch to main program.
  28. *
  29. * 3. If the system clock source selected by user fails to startup, the SystemInit()
  30. * function will do nothing and HSI still used as system clock source. User can
  31. * add some code to deal with this issue inside the SetSysClock() function.
  32. *
  33. * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depedning on
  34. * the product used), refer to "HSE_VALUE" define in "stm32f10x.h" file.
  35. * When HSE is used as system clock source, directly or through PLL, and you
  36. * are using different crystal you have to adapt the HSE value to your own
  37. * configuration.
  38. *
  39. ******************************************************************************
  40. * @attention
  41. *
  42. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  43. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  44. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  45. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  46. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  47. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  48. *
  49. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  50. ******************************************************************************
  51. */
  52. /** @addtogroup CMSIS
  53. * @{
  54. */
  55. /** @addtogroup stm32f10x_system
  56. * @{
  57. */
  58. /** @addtogroup STM32F10x_System_Private_Includes
  59. * @{
  60. */
  61. #include "stm32f10x.h"
  62. /**
  63. * @}
  64. */
  65. /** @addtogroup STM32F10x_System_Private_TypesDefinitions
  66. * @{
  67. */
  68. /**
  69. * @}
  70. */
  71. /** @addtogroup STM32F10x_System_Private_Defines
  72. * @{
  73. */
  74. /*!< Uncomment the line corresponding to the desired System clock (SYSCLK)
  75. frequency (after reset the HSI is used as SYSCLK source)
  76. IMPORTANT NOTE:
  77. ==============
  78. 1. After each device reset the HSI is used as System clock source.
  79. 2. Please make sure that the selected System clock doesn't exceed your device's
  80. maximum frequency.
  81. 3. If none of the define below is enabled, the HSI is used as System clock
  82. source.
  83. 4. The System clock configuration functions provided within this file assume that:
  84. - For Low, Medium and High density Value line devices an external 8MHz
  85. crystal is used to drive the System clock.
  86. - For Low, Medium and High density devices an external 8MHz crystal is
  87. used to drive the System clock.
  88. - For Connectivity line devices an external 25MHz crystal is used to drive
  89. the System clock.
  90. If you are using different crystal you have to adapt those functions accordingly.
  91. */
  92. #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  93. /* #define SYSCLK_FREQ_HSE HSE_VALUE */
  94. #define SYSCLK_FREQ_24MHz 24000000
  95. #else
  96. /* #define SYSCLK_FREQ_HSE HSE_VALUE */
  97. /* #define SYSCLK_FREQ_24MHz 24000000 */
  98. /* #define SYSCLK_FREQ_36MHz 36000000 */
  99. /* #define SYSCLK_FREQ_48MHz 48000000 */
  100. //#define SYSCLK_FREQ_56MHz 56000000 */
  101. #define SYSCLK_FREQ_72MHz 72000000
  102. #endif
  103. /*!< Uncomment the following line if you need to use external SRAM mounted
  104. on STM3210E-EVAL board (STM32 High density and XL-density devices) or on
  105. STM32100E-EVAL board (STM32 High-density value line devices) as data memory */
  106. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
  107. /* #define DATA_IN_ExtSRAM */
  108. #endif
  109. /*!< Uncomment the following line if you need to relocate your vector Table in
  110. Internal SRAM. */
  111. /* #define VECT_TAB_SRAM */
  112. #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
  113. This value must be a multiple of 0x200. */
  114. /**
  115. * @}
  116. */
  117. /** @addtogroup STM32F10x_System_Private_Macros
  118. * @{
  119. */
  120. /**
  121. * @}
  122. */
  123. /** @addtogroup STM32F10x_System_Private_Variables
  124. * @{
  125. */
  126. /*******************************************************************************
  127. * Clock Definitions
  128. *******************************************************************************/
  129. #ifdef SYSCLK_FREQ_HSE
  130. uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */
  131. #elif defined SYSCLK_FREQ_24MHz
  132. uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz; /*!< System Clock Frequency (Core Clock) */
  133. #elif defined SYSCLK_FREQ_36MHz
  134. uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz; /*!< System Clock Frequency (Core Clock) */
  135. #elif defined SYSCLK_FREQ_48MHz
  136. uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */
  137. #elif defined SYSCLK_FREQ_56MHz
  138. uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz; /*!< System Clock Frequency (Core Clock) */
  139. #elif defined SYSCLK_FREQ_72MHz
  140. uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */
  141. #else /*!< HSI Selected as System Clock source */
  142. uint32_t SystemCoreClock = HSI_VALUE; /*!< System Clock Frequency (Core Clock) */
  143. #endif
  144. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  145. /**
  146. * @}
  147. */
  148. /** @addtogroup STM32F10x_System_Private_FunctionPrototypes
  149. * @{
  150. */
  151. static void SetSysClock(void);
  152. #ifdef SYSCLK_FREQ_HSE
  153. static void SetSysClockToHSE(void);
  154. #elif defined SYSCLK_FREQ_24MHz
  155. static void SetSysClockTo24(void);
  156. #elif defined SYSCLK_FREQ_36MHz
  157. static void SetSysClockTo36(void);
  158. #elif defined SYSCLK_FREQ_48MHz
  159. static void SetSysClockTo48(void);
  160. #elif defined SYSCLK_FREQ_56MHz
  161. static void SetSysClockTo56(void);
  162. #elif defined SYSCLK_FREQ_72MHz
  163. static void SetSysClockTo72(void);
  164. #endif
  165. #ifdef DATA_IN_ExtSRAM
  166. static void SystemInit_ExtMemCtl(void);
  167. #endif /* DATA_IN_ExtSRAM */
  168. /**
  169. * @}
  170. */
  171. /** @addtogroup STM32F10x_System_Private_Functions
  172. * @{
  173. */
  174. /**
  175. * @brief Setup the microcontroller system
  176. * Initialize the Embedded Flash Interface, the PLL and update the
  177. * SystemCoreClock variable.
  178. * @note This function should be used only after reset.
  179. * @param None
  180. * @retval None
  181. */
  182. void SystemInit (void)
  183. {
  184. /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  185. //将RCC时钟配置重置为默认的重置状态(用于调试)
  186. /* Set HSION bit */ //打开内部8M时钟,HSI
  187. RCC->CR |= (uint32_t)0x00000001;
  188. /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
  189. #ifndef STM32F10X_CL
  190. RCC->CFGR &= (uint32_t)0xF8FF0000;
  191. #else
  192. RCC->CFGR &= (uint32_t)0xF0FF0000;
  193. #endif /* STM32F10X_CL */
  194. /* Reset HSEON, CSSON and PLLON bits */
  195. RCC->CR &= (uint32_t)0xFEF6FFFF;
  196. /* Reset HSEBYP bit */
  197. RCC->CR &= (uint32_t)0xFFFBFFFF;
  198. /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  199. RCC->CFGR &= (uint32_t)0xFF80FFFF;
  200. #ifdef STM32F10X_CL
  201. /* Reset PLL2ON and PLL3ON bits */
  202. RCC->CR &= (uint32_t)0xEBFFFFFF;
  203. /* Disable all interrupts and clear pending bits */
  204. RCC->CIR = 0x00FF0000;
  205. /* Reset CFGR2 register */
  206. RCC->CFGR2 = 0x00000000;
  207. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  208. /* Disable all interrupts and clear pending bits */
  209. RCC->CIR = 0x009F0000;
  210. /* Reset CFGR2 register */
  211. RCC->CFGR2 = 0x00000000;
  212. #else
  213. /* Disable all interrupts and clear pending bits */ //禁止所有中断,清除中断位
  214. RCC->CIR = 0x009F0000;
  215. #endif /* STM32F10X_CL */
  216. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
  217. #ifdef DATA_IN_ExtSRAM
  218. SystemInit_ExtMemCtl();
  219. #endif /* DATA_IN_ExtSRAM */
  220. #endif
  221. /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  222. //配置系统时钟频率,HCLK, PCLK2和PCLK1分频器
  223. /* Configure the Flash Latency cycles and enable prefetch buffer */
  224. //配置Flash延迟周期并启用预取缓冲区
  225. SetSysClock(); //AHB、APB1、APB2、PLL/SYSCLK时钟配置
  226. #ifdef VECT_TAB_SRAM
  227. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  228. #else
  229. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  230. #endif
  231. }
  232. /**
  233. * @brief Update SystemCoreClock variable according to Clock Register Values.
  234. * The SystemCoreClock variable contains the core clock (HCLK), it can
  235. * be used by the user application to setup the SysTick timer or configure
  236. * other parameters.
  237. *
  238. * @note Each time the core clock (HCLK) changes, this function must be called
  239. * to update SystemCoreClock variable value. Otherwise, any configuration
  240. * based on this variable will be incorrect.
  241. *
  242. * @note - The system frequency computed by this function is not the real
  243. * frequency in the chip. It is calculated based on the predefined
  244. * constant and the selected clock source:
  245. *
  246. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  247. *
  248. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  249. *
  250. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  251. * or HSI_VALUE(*) multiplied by the PLL factors.
  252. *
  253. * (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value
  254. * 8 MHz) but the real value may vary depending on the variations
  255. * in voltage and temperature.
  256. *
  257. * (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value
  258. * 8 MHz or 25 MHz, depedning on the product used), user has to ensure
  259. * that HSE_VALUE is same as the real frequency of the crystal used.
  260. * Otherwise, this function may have wrong result.
  261. *
  262. * - The result of this function could be not correct when using fractional
  263. * value for HSE crystal.
  264. * @param None
  265. * @retval None
  266. */
  267. void SystemCoreClockUpdate (void)
  268. {
  269. uint32_t tmp = 0, pllmull = 0, pllsource = 0;
  270. #ifdef STM32F10X_CL
  271. uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
  272. #endif /* STM32F10X_CL */
  273. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  274. uint32_t prediv1factor = 0;
  275. #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL or STM32F10X_HD_VL */
  276. /* Get SYSCLK source -------------------------------------------------------*/
  277. tmp = RCC->CFGR & RCC_CFGR_SWS;
  278. switch (tmp)
  279. {
  280. case 0x00: /* HSI used as system clock */
  281. SystemCoreClock = HSI_VALUE;
  282. break;
  283. case 0x04: /* HSE used as system clock */
  284. SystemCoreClock = HSE_VALUE;
  285. break;
  286. case 0x08: /* PLL used as system clock */
  287. /* Get PLL clock source and multiplication factor ----------------------*/
  288. pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  289. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  290. #ifndef STM32F10X_CL
  291. pllmull = ( pllmull >> 18) + 2;
  292. if (pllsource == 0x00)
  293. {
  294. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  295. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  296. }
  297. else
  298. {
  299. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  300. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  301. /* HSE oscillator clock selected as PREDIV1 clock entry */
  302. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  303. #else
  304. /* HSE selected as PLL clock entry */
  305. if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
  306. {/* HSE oscillator clock divided by 2 */
  307. SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
  308. }
  309. else
  310. {
  311. SystemCoreClock = HSE_VALUE * pllmull;
  312. }
  313. #endif
  314. }
  315. #else
  316. pllmull = pllmull >> 18;
  317. if (pllmull != 0x0D)
  318. {
  319. pllmull += 2;
  320. }
  321. else
  322. { /* PLL multiplication factor = PLL input clock * 6.5 */
  323. pllmull = 13 / 2;
  324. }
  325. if (pllsource == 0x00)
  326. {
  327. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  328. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  329. }
  330. else
  331. {/* PREDIV1 selected as PLL clock entry */
  332. /* Get PREDIV1 clock source and division factor */
  333. prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
  334. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  335. if (prediv1source == 0)
  336. {
  337. /* HSE oscillator clock selected as PREDIV1 clock entry */
  338. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  339. }
  340. else
  341. {/* PLL2 clock selected as PREDIV1 clock entry */
  342. /* Get PREDIV2 division factor and PLL2 multiplication factor */
  343. prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;
  344. pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2;
  345. SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
  346. }
  347. }
  348. #endif /* STM32F10X_CL */
  349. break;
  350. default:
  351. SystemCoreClock = HSI_VALUE;
  352. break;
  353. }
  354. /* Compute HCLK clock frequency ----------------*/
  355. /* Get HCLK prescaler */
  356. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  357. /* HCLK clock frequency */
  358. SystemCoreClock >>= tmp;
  359. }
  360. /**
  361. * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  362. * @param None
  363. * @retval None
  364. */
  365. static void SetSysClock(void)
  366. {
  367. #ifdef SYSCLK_FREQ_HSE
  368. SetSysClockToHSE();
  369. #elif defined SYSCLK_FREQ_24MHz
  370. SetSysClockTo24();
  371. #elif defined SYSCLK_FREQ_36MHz
  372. SetSysClockTo36();
  373. #elif defined SYSCLK_FREQ_48MHz
  374. SetSysClockTo48();
  375. #elif defined SYSCLK_FREQ_56MHz
  376. SetSysClockTo56();
  377. #elif defined SYSCLK_FREQ_72MHz //系统时钟设为72M
  378. SetSysClockTo72();
  379. #endif
  380. /* If none of the define above is enabled, the HSI is used as System clock
  381. source (default after reset) */
  382. }
  383. /**
  384. * @brief Setup the external memory controller. Called in startup_stm32f10x.s
  385. * before jump to __main
  386. * @param None
  387. * @retval None
  388. */
  389. #ifdef DATA_IN_ExtSRAM
  390. /**
  391. * @brief Setup the external memory controller.
  392. * Called in startup_stm32f10x_xx.s/.c before jump to main.
  393. * This function configures the external SRAM mounted on STM3210E-EVAL
  394. * board (STM32 High density devices). This SRAM will be used as program
  395. * data memory (including heap and stack).
  396. * @param None
  397. * @retval None
  398. */
  399. void SystemInit_ExtMemCtl(void)
  400. {
  401. /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
  402. required, then adjust the Register Addresses */
  403. /* Enable FSMC clock */
  404. RCC->AHBENR = 0x00000114;
  405. /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
  406. RCC->APB2ENR = 0x000001E0;
  407. /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
  408. /*---------------- SRAM Address lines configuration -------------------------*/
  409. /*---------------- NOE and NWE configuration --------------------------------*/
  410. /*---------------- NE3 configuration ----------------------------------------*/
  411. /*---------------- NBL0, NBL1 configuration ---------------------------------*/
  412. GPIOD->CRL = 0x44BB44BB;
  413. GPIOD->CRH = 0xBBBBBBBB;
  414. GPIOE->CRL = 0xB44444BB;
  415. GPIOE->CRH = 0xBBBBBBBB;
  416. GPIOF->CRL = 0x44BBBBBB;
  417. GPIOF->CRH = 0xBBBB4444;
  418. GPIOG->CRL = 0x44BBBBBB;
  419. GPIOG->CRH = 0x44444B44;
  420. /*---------------- FSMC Configuration ---------------------------------------*/
  421. /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
  422. FSMC_Bank1->BTCR[4] = 0x00001011;
  423. FSMC_Bank1->BTCR[5] = 0x00000200;
  424. }
  425. #endif /* DATA_IN_ExtSRAM */
  426. #ifdef SYSCLK_FREQ_HSE
  427. /**
  428. * @brief Selects HSE as System clock source and configure HCLK, PCLK2
  429. * and PCLK1 prescalers.
  430. * @note This function should be used only after reset.
  431. * @param None
  432. * @retval None
  433. */
  434. static void SetSysClockToHSE(void)
  435. {
  436. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  437. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  438. /* Enable HSE */
  439. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  440. /* Wait till HSE is ready and if Time out is reached exit */
  441. do
  442. {
  443. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  444. StartUpCounter++;
  445. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  446. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  447. {
  448. HSEStatus = (uint32_t)0x01;
  449. }
  450. else
  451. {
  452. HSEStatus = (uint32_t)0x00;
  453. }
  454. if (HSEStatus == (uint32_t)0x01)
  455. {
  456. #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
  457. /* Enable Prefetch Buffer */
  458. FLASH->ACR |= FLASH_ACR_PRFTBE;
  459. /* Flash 0 wait state */
  460. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  461. #ifndef STM32F10X_CL
  462. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
  463. #else
  464. if (HSE_VALUE <= 24000000)
  465. {
  466. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
  467. }
  468. else
  469. {
  470. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  471. }
  472. #endif /* STM32F10X_CL */
  473. #endif
  474. /* HCLK = SYSCLK */
  475. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  476. /* PCLK2 = HCLK */
  477. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  478. /* PCLK1 = HCLK */
  479. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  480. /* Select HSE as system clock source */
  481. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  482. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;
  483. /* Wait till HSE is used as system clock source */
  484. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)
  485. {
  486. }
  487. }
  488. else
  489. { /* If HSE fails to start-up, the application will have wrong clock
  490. configuration. User can add here some code to deal with this error */
  491. }
  492. }
  493. #elif defined SYSCLK_FREQ_24MHz
  494. /**
  495. * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2
  496. * and PCLK1 prescalers.
  497. * @note This function should be used only after reset.
  498. * @param None
  499. * @retval None
  500. */
  501. static void SetSysClockTo24(void)
  502. {
  503. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  504. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  505. /* Enable HSE */
  506. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  507. /* Wait till HSE is ready and if Time out is reached exit */
  508. do
  509. {
  510. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  511. StartUpCounter++;
  512. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  513. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  514. {
  515. HSEStatus = (uint32_t)0x01;
  516. }
  517. else
  518. {
  519. HSEStatus = (uint32_t)0x00;
  520. }
  521. if (HSEStatus == (uint32_t)0x01)
  522. {
  523. #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
  524. /* Enable Prefetch Buffer */
  525. FLASH->ACR |= FLASH_ACR_PRFTBE;
  526. /* Flash 0 wait state */
  527. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  528. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
  529. #endif
  530. /* HCLK = SYSCLK */
  531. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  532. /* PCLK2 = HCLK */
  533. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  534. /* PCLK1 = HCLK */
  535. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  536. #ifdef STM32F10X_CL
  537. /* Configure PLLs ------------------------------------------------------*/
  538. /* PLL configuration: PLLCLK = PREDIV1 * 6 = 24 MHz */
  539. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  540. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  541. RCC_CFGR_PLLMULL6);
  542. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  543. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
  544. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  545. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  546. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  547. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
  548. /* Enable PLL2 */
  549. RCC->CR |= RCC_CR_PLL2ON;
  550. /* Wait till PLL2 is ready */
  551. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  552. {
  553. }
  554. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
  555. /* PLL configuration: = (HSE / 2) * 6 = 24 MHz */
  556. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  557. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1_Div2 | RCC_CFGR_PLLMULL6);
  558. #else
  559. /* PLL configuration: = (HSE / 2) * 6 = 24 MHz */
  560. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  561. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
  562. #endif /* STM32F10X_CL */
  563. /* Enable PLL */
  564. RCC->CR |= RCC_CR_PLLON;
  565. /* Wait till PLL is ready */
  566. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  567. {
  568. }
  569. /* Select PLL as system clock source */
  570. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  571. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  572. /* Wait till PLL is used as system clock source */
  573. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  574. {
  575. }
  576. }
  577. else
  578. { /* If HSE fails to start-up, the application will have wrong clock
  579. configuration. User can add here some code to deal with this error */
  580. }
  581. }
  582. #elif defined SYSCLK_FREQ_36MHz
  583. /**
  584. * @brief Sets System clock frequency to 36MHz and configure HCLK, PCLK2
  585. * and PCLK1 prescalers.
  586. * @note This function should be used only after reset.
  587. * @param None
  588. * @retval None
  589. */
  590. static void SetSysClockTo36(void)
  591. {
  592. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  593. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  594. /* Enable HSE */
  595. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  596. /* Wait till HSE is ready and if Time out is reached exit */
  597. do
  598. {
  599. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  600. StartUpCounter++;
  601. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  602. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  603. {
  604. HSEStatus = (uint32_t)0x01;
  605. }
  606. else
  607. {
  608. HSEStatus = (uint32_t)0x00;
  609. }
  610. if (HSEStatus == (uint32_t)0x01)
  611. {
  612. /* Enable Prefetch Buffer */
  613. FLASH->ACR |= FLASH_ACR_PRFTBE;
  614. /* Flash 1 wait state */
  615. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  616. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  617. /* HCLK = SYSCLK */
  618. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  619. /* PCLK2 = HCLK */
  620. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  621. /* PCLK1 = HCLK */
  622. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  623. #ifdef STM32F10X_CL
  624. /* Configure PLLs ------------------------------------------------------*/
  625. /* PLL configuration: PLLCLK = PREDIV1 * 9 = 36 MHz */
  626. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  627. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  628. RCC_CFGR_PLLMULL9);
  629. /*!< PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  630. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
  631. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  632. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  633. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  634. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
  635. /* Enable PLL2 */
  636. RCC->CR |= RCC_CR_PLL2ON;
  637. /* Wait till PLL2 is ready */
  638. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  639. {
  640. }
  641. #else
  642. /* PLL configuration: PLLCLK = (HSE / 2) * 9 = 36 MHz */
  643. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  644. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL9);
  645. #endif /* STM32F10X_CL */
  646. /* Enable PLL */
  647. RCC->CR |= RCC_CR_PLLON;
  648. /* Wait till PLL is ready */
  649. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  650. {
  651. }
  652. /* Select PLL as system clock source */
  653. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  654. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  655. /* Wait till PLL is used as system clock source */
  656. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  657. {
  658. }
  659. }
  660. else
  661. { /* If HSE fails to start-up, the application will have wrong clock
  662. configuration. User can add here some code to deal with this error */
  663. }
  664. }
  665. #elif defined SYSCLK_FREQ_48MHz
  666. /**
  667. * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2
  668. * and PCLK1 prescalers.
  669. * @note This function should be used only after reset.
  670. * @param None
  671. * @retval None
  672. */
  673. static void SetSysClockTo48(void)
  674. {
  675. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  676. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  677. /* Enable HSE */
  678. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  679. /* Wait till HSE is ready and if Time out is reached exit */
  680. do
  681. {
  682. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  683. StartUpCounter++;
  684. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  685. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  686. {
  687. HSEStatus = (uint32_t)0x01;
  688. }
  689. else
  690. {
  691. HSEStatus = (uint32_t)0x00;
  692. }
  693. if (HSEStatus == (uint32_t)0x01)
  694. {
  695. /* Enable Prefetch Buffer */
  696. FLASH->ACR |= FLASH_ACR_PRFTBE;
  697. /* Flash 1 wait state */
  698. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  699. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  700. /* HCLK = SYSCLK */
  701. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  702. /* PCLK2 = HCLK */
  703. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  704. /* PCLK1 = HCLK */
  705. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  706. #ifdef STM32F10X_CL
  707. /* Configure PLLs ------------------------------------------------------*/
  708. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  709. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  710. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  711. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  712. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  713. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
  714. /* Enable PLL2 */
  715. RCC->CR |= RCC_CR_PLL2ON;
  716. /* Wait till PLL2 is ready */
  717. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  718. {
  719. }
  720. /* PLL configuration: PLLCLK = PREDIV1 * 6 = 48 MHz */
  721. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  722. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  723. RCC_CFGR_PLLMULL6);
  724. #else
  725. /* PLL configuration: PLLCLK = HSE * 6 = 48 MHz */
  726. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  727. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
  728. #endif /* STM32F10X_CL */
  729. /* Enable PLL */
  730. RCC->CR |= RCC_CR_PLLON;
  731. /* Wait till PLL is ready */
  732. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  733. {
  734. }
  735. /* Select PLL as system clock source */
  736. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  737. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  738. /* Wait till PLL is used as system clock source */
  739. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  740. {
  741. }
  742. }
  743. else
  744. { /* If HSE fails to start-up, the application will have wrong clock
  745. configuration. User can add here some code to deal with this error */
  746. }
  747. }
  748. #elif defined SYSCLK_FREQ_56MHz
  749. /**
  750. * @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2
  751. * and PCLK1 prescalers.
  752. * @note This function should be used only after reset.
  753. * @param None
  754. * @retval None
  755. */
  756. static void SetSysClockTo56(void)
  757. {
  758. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  759. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  760. /* Enable HSE */
  761. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  762. /* Wait till HSE is ready and if Time out is reached exit */
  763. do
  764. {
  765. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  766. StartUpCounter++;
  767. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  768. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  769. {
  770. HSEStatus = (uint32_t)0x01;
  771. }
  772. else
  773. {
  774. HSEStatus = (uint32_t)0x00;
  775. }
  776. if (HSEStatus == (uint32_t)0x01)
  777. {
  778. /* Enable Prefetch Buffer */
  779. FLASH->ACR |= FLASH_ACR_PRFTBE;
  780. /* Flash 2 wait state */
  781. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  782. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  783. /* HCLK = SYSCLK */
  784. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  785. /* PCLK2 = HCLK */
  786. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  787. /* PCLK1 = HCLK */
  788. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  789. #ifdef STM32F10X_CL
  790. /* Configure PLLs ------------------------------------------------------*/
  791. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  792. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  793. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  794. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  795. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  796. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
  797. /* Enable PLL2 */
  798. RCC->CR |= RCC_CR_PLL2ON;
  799. /* Wait till PLL2 is ready */
  800. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  801. {
  802. }
  803. /* PLL configuration: PLLCLK = PREDIV1 * 7 = 56 MHz */
  804. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  805. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  806. RCC_CFGR_PLLMULL7);
  807. #else
  808. /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */
  809. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  810. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7);
  811. #endif /* STM32F10X_CL */
  812. /* Enable PLL */
  813. RCC->CR |= RCC_CR_PLLON;
  814. /* Wait till PLL is ready */
  815. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  816. {
  817. }
  818. /* Select PLL as system clock source */
  819. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  820. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  821. /* Wait till PLL is used as system clock source */
  822. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  823. {
  824. }
  825. }
  826. else
  827. { /* If HSE fails to start-up, the application will have wrong clock
  828. configuration. User can add here some code to deal with this error */
  829. }
  830. }
  831. #elif defined SYSCLK_FREQ_72MHz
  832. /**
  833. * @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2
  834. * and PCLK1 prescalers.
  835. * @note This function should be used only after reset.
  836. * @param None
  837. * @retval None
  838. */
  839. static void SetSysClockTo72(void)
  840. {
  841. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  842. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  843. /* Enable HSE */ //打开外部高速时钟
  844. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  845. /* Wait till HSE is ready and if Time out is reached exit */
  846. do
  847. {
  848. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  849. StartUpCounter++;
  850. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  851. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  852. {
  853. HSEStatus = (uint32_t)0x01; //HSE就绪
  854. }
  855. else
  856. {
  857. HSEStatus = (uint32_t)0x00;
  858. }
  859. if (HSEStatus == (uint32_t)0x01)
  860. {
  861. /* Enable Prefetch Buffer */
  862. FLASH->ACR |= FLASH_ACR_PRFTBE;
  863. /* Flash 2 wait state */
  864. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  865. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  866. /* HCLK = SYSCLK */ //配置AHB时钟等于系统时钟,SYSCLK不分频
  867. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  868. /* PCLK2 = HCLK */ //配置APB2总线时钟,HCLK不分频
  869. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  870. /* PCLK1 = HCLK/2 */ //配置APB1总线时钟,HCLK-2分频
  871. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  872. #ifdef STM32F10X_CL
  873. /* Configure PLLs ------------------------------------------------------*/
  874. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  875. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  876. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  877. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  878. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  879. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
  880. /* Enable PLL2 */
  881. RCC->CR |= RCC_CR_PLL2ON;
  882. /* Wait till PLL2 is ready */
  883. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  884. {
  885. }
  886. /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */
  887. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  888. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  889. RCC_CFGR_PLLMULL9);
  890. #else
  891. /* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */ //PLLCLK等于外部时钟的9倍
  892. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
  893. RCC_CFGR_PLLMULL));
  894. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
  895. #endif /* STM32F10X_CL */
  896. /* Enable PLL */ //使能PLL时钟
  897. RCC->CR |= RCC_CR_PLLON;
  898. /* Wait till PLL is ready */
  899. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  900. {
  901. }
  902. /* Select PLL as system clock source */ //把PLL作为系统时钟
  903. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  904. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  905. /* Wait till PLL is used as system clock source */
  906. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  907. {
  908. }
  909. }
  910. else
  911. { /* If HSE fails to start-up, the application will have wrong clock
  912. configuration. User can add here some code to deal with this error */
  913. }
  914. }
  915. #endif
  916. /**
  917. * @}
  918. */
  919. /**
  920. * @}
  921. */
  922. /**
  923. * @}
  924. */
  925. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/