Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1034 lines
31KB

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