system_stm32f10x.c 36 KB

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