system_stm32f10x.c 37 KB

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