system_stm32f10x.c 37 KB

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