system_stm32f10x.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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 66355200
  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. 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. g_ucHSE_Flag=0;
  455. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  456. /* Enable HSE */
  457. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  458. /* Wait till HSE is ready and if Time out is reached exit */
  459. do
  460. {
  461. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  462. StartUpCounter++;
  463. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  464. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  465. {
  466. HSEStatus = (uint32_t)0x01;
  467. }
  468. else
  469. {
  470. HSEStatus = (uint32_t)0x00;
  471. }
  472. if (HSEStatus == (uint32_t)0x01)
  473. {
  474. #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
  475. /* Enable Prefetch Buffer */
  476. FLASH->ACR |= FLASH_ACR_PRFTBE;
  477. /* Flash 0 wait state */
  478. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  479. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
  480. #endif
  481. /* HCLK = SYSCLK */
  482. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  483. /* PCLK2 = HCLK */
  484. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  485. /* PCLK1 = HCLK */
  486. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  487. #ifdef STM32F10X_CL
  488. /* Configure PLLs ------------------------------------------------------*/
  489. /* PLL configuration: PLLCLK = PREDIV1 * 6 = 24 MHz */
  490. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  491. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  492. RCC_CFGR_PLLMULL6);
  493. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  494. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
  495. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  496. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  497. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  498. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
  499. /* Enable PLL2 */
  500. RCC->CR |= RCC_CR_PLL2ON;
  501. /* Wait till PLL2 is ready */
  502. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  503. {
  504. }
  505. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
  506. /* PLL configuration: = (HSE / 2) * 6 = 24 MHz */
  507. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  508. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1_Div2 | RCC_CFGR_PLLMULL6);
  509. #else
  510. /* PLL configuration: = (HSE / 2) * 4 = 24 MHz */
  511. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  512. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL4);
  513. #endif /* STM32F10X_CL */
  514. /* Enable PLL */
  515. RCC->CR |= RCC_CR_PLLON;
  516. /* Wait till PLL is ready */
  517. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  518. {
  519. }
  520. /* Select PLL as system clock source */
  521. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  522. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  523. /* Wait till PLL is used as system clock source */
  524. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  525. {
  526. }
  527. g_ucHSE_Flag=1;
  528. }
  529. else
  530. { /* If HSE fails to start-up, the application will have wrong clock
  531. configuration. User can add here some code to deal with this error */
  532. g_ucHSE_Flag=0;
  533. while(1)
  534. {
  535. }
  536. }
  537. }
  538. #elif defined SYSCLK_FREQ_36MHz
  539. /**
  540. * @brief Sets System clock frequency to 36MHz and configure HCLK, PCLK2
  541. * and PCLK1 prescalers.
  542. * @note This function should be used only after reset.
  543. * @param None
  544. * @retval None
  545. */
  546. static void SetSysClockTo36(void)
  547. {
  548. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  549. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  550. /* Enable HSE */
  551. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  552. /* Wait till HSE is ready and if Time out is reached exit */
  553. do
  554. {
  555. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  556. StartUpCounter++;
  557. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  558. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  559. {
  560. HSEStatus = (uint32_t)0x01;
  561. }
  562. else
  563. {
  564. HSEStatus = (uint32_t)0x00;
  565. }
  566. if (HSEStatus == (uint32_t)0x01)
  567. {
  568. /* Enable Prefetch Buffer */
  569. FLASH->ACR |= FLASH_ACR_PRFTBE;
  570. /* Flash 1 wait state */
  571. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  572. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  573. /* HCLK = SYSCLK */
  574. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  575. /* PCLK2 = HCLK */
  576. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  577. /* PCLK1 = HCLK */
  578. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  579. #ifdef STM32F10X_CL
  580. /* Configure PLLs ------------------------------------------------------*/
  581. /* PLL configuration: PLLCLK = PREDIV1 * 9 = 36 MHz */
  582. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  583. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  584. RCC_CFGR_PLLMULL9);
  585. /*!< PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  586. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
  587. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  588. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  589. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  590. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
  591. /* Enable PLL2 */
  592. RCC->CR |= RCC_CR_PLL2ON;
  593. /* Wait till PLL2 is ready */
  594. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  595. {
  596. }
  597. #else
  598. /* PLL configuration: PLLCLK = (HSE / 2) * 9 = 36 MHz */
  599. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  600. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL9);
  601. #endif /* STM32F10X_CL */
  602. /* Enable PLL */
  603. RCC->CR |= RCC_CR_PLLON;
  604. /* Wait till PLL is ready */
  605. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  606. {
  607. }
  608. /* Select PLL as system clock source */
  609. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  610. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  611. /* Wait till PLL is used as system clock source */
  612. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  613. {
  614. }
  615. }
  616. else
  617. { /* If HSE fails to start-up, the application will have wrong clock
  618. configuration. User can add here some code to deal with this error */
  619. }
  620. }
  621. #elif defined SYSCLK_FREQ_48MHz
  622. /**
  623. * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2
  624. * and PCLK1 prescalers.
  625. * @note This function should be used only after reset.
  626. * @param None
  627. * @retval None
  628. */
  629. static void SetSysClockTo48(void)
  630. {
  631. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  632. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  633. /* Enable HSE */
  634. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  635. /* Wait till HSE is ready and if Time out is reached exit */
  636. do
  637. {
  638. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  639. StartUpCounter++;
  640. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  641. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  642. {
  643. HSEStatus = (uint32_t)0x01;
  644. }
  645. else
  646. {
  647. HSEStatus = (uint32_t)0x00;
  648. }
  649. if (HSEStatus == (uint32_t)0x01)
  650. {
  651. /* Enable Prefetch Buffer */
  652. FLASH->ACR |= FLASH_ACR_PRFTBE;
  653. /* Flash 1 wait state */
  654. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  655. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  656. /* HCLK = SYSCLK */
  657. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  658. /* PCLK2 = HCLK */
  659. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  660. /* PCLK1 = HCLK */
  661. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  662. #ifdef STM32F10X_CL
  663. /* Configure PLLs ------------------------------------------------------*/
  664. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  665. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  666. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  667. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  668. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  669. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
  670. /* Enable PLL2 */
  671. RCC->CR |= RCC_CR_PLL2ON;
  672. /* Wait till PLL2 is ready */
  673. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  674. {
  675. }
  676. /* PLL configuration: PLLCLK = PREDIV1 * 6 = 48 MHz */
  677. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  678. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  679. RCC_CFGR_PLLMULL6);
  680. #else
  681. /* PLL configuration: PLLCLK = HSE * 6 = 48 MHz */
  682. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  683. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
  684. #endif /* STM32F10X_CL */
  685. /* Enable PLL */
  686. RCC->CR |= RCC_CR_PLLON;
  687. /* Wait till PLL is ready */
  688. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  689. {
  690. }
  691. /* Select PLL as system clock source */
  692. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  693. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  694. /* Wait till PLL is used as system clock source */
  695. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  696. {
  697. }
  698. }
  699. else
  700. { /* If HSE fails to start-up, the application will have wrong clock
  701. configuration. User can add here some code to deal with this error */
  702. while(1);
  703. }
  704. }
  705. #elif defined SYSCLK_FREQ_56MHz
  706. /**
  707. * @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2
  708. * and PCLK1 prescalers.
  709. * @note This function should be used only after reset.
  710. * @param None
  711. * @retval None
  712. */
  713. static void SetSysClockTo56(void)
  714. {
  715. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  716. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  717. /* Enable HSE */
  718. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  719. /* Wait till HSE is ready and if Time out is reached exit */
  720. do
  721. {
  722. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  723. StartUpCounter++;
  724. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  725. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  726. {
  727. HSEStatus = (uint32_t)0x01;
  728. }
  729. else
  730. {
  731. HSEStatus = (uint32_t)0x00;
  732. }
  733. if (HSEStatus == (uint32_t)0x01)
  734. {
  735. /* Enable Prefetch Buffer */
  736. FLASH->ACR |= FLASH_ACR_PRFTBE;
  737. /* Flash 2 wait state */
  738. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  739. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  740. /* HCLK = SYSCLK */
  741. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  742. /* PCLK2 = HCLK */
  743. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  744. /* PCLK1 = HCLK */
  745. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  746. #ifdef STM32F10X_CL
  747. /* Configure PLLs ------------------------------------------------------*/
  748. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  749. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  750. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  751. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  752. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  753. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
  754. /* Enable PLL2 */
  755. RCC->CR |= RCC_CR_PLL2ON;
  756. /* Wait till PLL2 is ready */
  757. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  758. {
  759. }
  760. /* PLL configuration: PLLCLK = PREDIV1 * 7 = 56 MHz */
  761. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  762. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  763. RCC_CFGR_PLLMULL7);
  764. #else
  765. /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */
  766. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  767. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7);
  768. #endif /* STM32F10X_CL */
  769. /* Enable PLL */
  770. RCC->CR |= RCC_CR_PLLON;
  771. /* Wait till PLL is ready */
  772. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  773. {
  774. }
  775. /* Select PLL as system clock source */
  776. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  777. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  778. /* Wait till PLL is used as system clock source */
  779. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  780. {
  781. }
  782. }
  783. else
  784. { /* If HSE fails to start-up, the application will have wrong clock
  785. configuration. User can add here some code to deal with this error */
  786. }
  787. }
  788. #elif defined SYSCLK_FREQ_66MHz
  789. /*
  790. ʹÓÃHSE£¬¾§Ìå11.0592M,6±¶Æµ
  791. */
  792. unsigned char g_ucSysError;
  793. static void SetSysClockTo66(void)
  794. {
  795. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  796. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  797. /* Enable HSE */
  798. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  799. /* Wait till HSE is ready and if Time out is reached exit */
  800. do
  801. {
  802. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  803. StartUpCounter++;
  804. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  805. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  806. {
  807. HSEStatus = (uint32_t)0x01;
  808. }
  809. else
  810. {
  811. HSEStatus = (uint32_t)0x00;
  812. }
  813. if (HSEStatus == (uint32_t)0x01)
  814. {
  815. /* Enable Prefetch Buffer */
  816. FLASH->ACR |= FLASH_ACR_PRFTBE;
  817. /* Flash 2 wait state */
  818. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  819. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  820. /* HCLK = SYSCLK */
  821. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  822. /* PCLK2 = HCLK */
  823. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  824. /* PCLK1 = HCLK */
  825. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  826. /* PLL configuration: PLLCLK = HSE * 6 = 66.3552 MHz */ //modify by shiliangwen
  827. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
  828. RCC_CFGR_PLLMULL));
  829. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
  830. /* Enable PLL */
  831. RCC->CR |= RCC_CR_PLLON;
  832. /* Wait till PLL is ready */
  833. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  834. {
  835. }
  836. /* Select PLL as system clock source */
  837. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  838. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  839. /* Wait till PLL is used as system clock source */
  840. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  841. {
  842. }
  843. }
  844. else
  845. { /* If HSE fails to start-up, the application will have wrong clock
  846. configuration. User can add here some code to deal with this error */
  847. g_ucSysError=0xff;
  848. }
  849. }
  850. #elif defined SYSCLK_FREQ_72MHz
  851. /**
  852. * @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2
  853. * and PCLK1 prescalers.
  854. * @note This function should be used only after reset.
  855. * @param None
  856. * @retval None
  857. */
  858. static void SetSysClockTo72(void)
  859. {
  860. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  861. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  862. /* Enable HSE */
  863. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  864. /* Wait till HSE is ready and if Time out is reached exit */
  865. do
  866. {
  867. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  868. StartUpCounter++;
  869. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  870. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  871. {
  872. HSEStatus = (uint32_t)0x01;
  873. }
  874. else
  875. {
  876. HSEStatus = (uint32_t)0x00;
  877. }
  878. if (HSEStatus == (uint32_t)0x01)
  879. {
  880. /* Enable Prefetch Buffer */
  881. FLASH->ACR |= FLASH_ACR_PRFTBE;
  882. /* Flash 2 wait state */
  883. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  884. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  885. /* HCLK = SYSCLK */
  886. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  887. /* PCLK2 = HCLK */
  888. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  889. /* PCLK1 = HCLK */
  890. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  891. #ifdef STM32F10X_CL
  892. /* Configure PLLs ------------------------------------------------------*/
  893. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  894. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  895. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  896. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  897. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  898. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
  899. /* Enable PLL2 */
  900. RCC->CR |= RCC_CR_PLL2ON;
  901. /* Wait till PLL2 is ready */
  902. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  903. {
  904. }
  905. /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */
  906. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  907. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  908. RCC_CFGR_PLLMULL9);
  909. #else
  910. /* PLL configuration: PLLCLK = HSE * 6 = 72 MHz */
  911. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |RCC_CFGR_PLLMULL));
  912. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
  913. #endif /* STM32F10X_CL */
  914. /* Enable PLL */
  915. RCC->CR |= RCC_CR_PLLON;
  916. /* Wait till PLL is ready */
  917. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  918. {
  919. }
  920. /* Select PLL as system clock source */
  921. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  922. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  923. /* Wait till PLL is used as system clock source */
  924. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  925. {
  926. }
  927. }
  928. else
  929. { /* If HSE fails to start-up, the application will have wrong clock
  930. configuration. User can add here some code to deal with this error */
  931. g_ucHSE_Flag=0;
  932. while(1);
  933. }
  934. }
  935. #elif defined SYSCLK_FREQ_60MHz
  936. static void SetSysClockTo60(void)
  937. {
  938. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  939. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  940. /* Enable HSE */
  941. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  942. /* Wait till HSE is ready and if Time out is reached exit */
  943. do
  944. {
  945. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  946. StartUpCounter++;
  947. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  948. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  949. {
  950. HSEStatus = (uint32_t)0x01;
  951. }
  952. else
  953. {
  954. HSEStatus = (uint32_t)0x00;
  955. }
  956. if (HSEStatus == (uint32_t)0x01)
  957. {
  958. /* Enable Prefetch Buffer */
  959. FLASH->ACR |= FLASH_ACR_PRFTBE;
  960. /* Flash 2 wait state */
  961. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  962. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  963. /* HCLK = SYSCLK */
  964. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  965. /* PCLK2 = HCLK */
  966. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  967. /* PCLK1 = HCLK */
  968. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  969. /* PLL configuration: PLLCLK = HSE * 5 = 60 MHz */
  970. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |RCC_CFGR_PLLMULL));
  971. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL5);
  972. /* Enable PLL */
  973. RCC->CR |= RCC_CR_PLLON;
  974. /* Wait till PLL is ready */
  975. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  976. {
  977. }
  978. /* Select PLL as system clock source */
  979. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  980. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  981. /* Wait till PLL is used as system clock source */
  982. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  983. {
  984. }
  985. }
  986. else
  987. { /* If HSE fails to start-up, the application will have wrong clock
  988. configuration. User can add here some code to deal with this error */
  989. g_ucHSE_Flag=0;
  990. while(1);
  991. }
  992. }
  993. #endif
  994. /**
  995. * @}
  996. */
  997. /**
  998. * @}
  999. */
  1000. /**
  1001. * @}
  1002. */
  1003. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/