system_stm32f10x.c 37 KB

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