system_stm32f10x.c 33 KB

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