system_stm32f10x.c 35 KB

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