system_stm32f10x.c 36 KB

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