system_stm32f10x.c 34 KB

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