2 ******************************************************************************
\r
3 * @file system_stm32f10x.c
\r
4 * @author MCD Application Team
\r
6 * @date 11-March-2011
\r
7 * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
\r
9 * 1. This file provides two functions and one global variable to be called from
\r
11 * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
\r
12 * factors, AHB/APBx prescalers and Flash settings).
\r
13 * This function is called at startup just after reset and
\r
14 * before branch to main program. This call is made inside
\r
15 * the "startup_stm32f10x_xx.s" file.
\r
17 * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
\r
18 * by the user application to setup the SysTick
\r
19 * timer or configure other parameters.
\r
21 * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
\r
22 * be called whenever the core clock is changed
\r
23 * during program execution.
\r
25 * 2. After each device reset the HSI (8 MHz) is used as system clock source.
\r
26 * Then SystemInit() function is called, in "startup_stm32f10x_xx.s" file, to
\r
27 * configure the system clock before to branch to main program.
\r
29 * 3. If the system clock source selected by user fails to startup, the SystemInit()
\r
30 * function will do nothing and HSI still used as system clock source. User can
\r
31 * add some code to deal with this issue inside the SetSysClock() function.
\r
33 * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depedning on
\r
34 * the product used), refer to "HSE_VALUE" define in "stm32f10x.h" file.
\r
35 * When HSE is used as system clock source, directly or through PLL, and you
\r
36 * are using different crystal you have to adapt the HSE value to your own
\r
39 ******************************************************************************
\r
42 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
43 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
\r
44 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
\r
45 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
\r
46 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
\r
47 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
49 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
\r
50 ******************************************************************************
\r
53 /** @addtogroup CMSIS
\r
57 /** @addtogroup stm32f10x_system
\r
61 /** @addtogroup STM32F10x_System_Private_Includes
\r
65 #include "stm32f10x.h"
\r
71 /** @addtogroup STM32F10x_System_Private_TypesDefinitions
\r
79 /** @addtogroup STM32F10x_System_Private_Defines
\r
83 /*!< Uncomment the line corresponding to the desired System clock (SYSCLK)
\r
84 frequency (after reset the HSI is used as SYSCLK source)
\r
88 1. After each device reset the HSI is used as System clock source.
\r
90 2. Please make sure that the selected System clock doesn't exceed your device's
\r
93 3. If none of the define below is enabled, the HSI is used as System clock
\r
96 4. The System clock configuration functions provided within this file assume that:
\r
97 - For Low, Medium and High density Value line devices an external 8MHz
\r
98 crystal is used to drive the System clock.
\r
99 - For Low, Medium and High density devices an external 8MHz crystal is
\r
100 used to drive the System clock.
\r
101 - For Connectivity line devices an external 25MHz crystal is used to drive
\r
103 If you are using different crystal you have to adapt those functions accordingly.
\r
106 #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
\r
107 /* #define SYSCLK_FREQ_HSE HSE_VALUE */
\r
108 #define SYSCLK_FREQ_24MHz 24000000
\r
110 /* #define SYSCLK_FREQ_HSE HSE_VALUE */
\r
111 /* #define SYSCLK_FREQ_24MHz 24000000 */
\r
112 /* #define SYSCLK_FREQ_36MHz 36000000 */
\r
113 /* #define SYSCLK_FREQ_48MHz 48000000 */
\r
114 /* #define SYSCLK_FREQ_56MHz 56000000 */
\r
115 #define SYSCLK_FREQ_72MHz 72000000
\r
118 /*!< Uncomment the following line if you need to use external SRAM mounted
\r
119 on STM3210E-EVAL board (STM32 High density and XL-density devices) or on
\r
120 STM32100E-EVAL board (STM32 High-density value line devices) as data memory */
\r
121 #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
\r
122 /* #define DATA_IN_ExtSRAM */
\r
125 /*!< Uncomment the following line if you need to relocate your vector Table in
\r
127 /* #define VECT_TAB_SRAM */
\r
128 #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
\r
129 This value must be a multiple of 0x200. */
\r
136 /** @addtogroup STM32F10x_System_Private_Macros
\r
144 /** @addtogroup STM32F10x_System_Private_Variables
\r
148 /*******************************************************************************
\r
149 * Clock Definitions
\r
150 *******************************************************************************/
\r
151 #ifdef SYSCLK_FREQ_HSE
\r
152 uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */
\r
153 #elif defined SYSCLK_FREQ_24MHz
\r
154 uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz; /*!< System Clock Frequency (Core Clock) */
\r
155 #elif defined SYSCLK_FREQ_36MHz
\r
156 uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz; /*!< System Clock Frequency (Core Clock) */
\r
157 #elif defined SYSCLK_FREQ_48MHz
\r
158 uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */
\r
159 #elif defined SYSCLK_FREQ_56MHz
\r
160 uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz; /*!< System Clock Frequency (Core Clock) */
\r
161 #elif defined SYSCLK_FREQ_72MHz
\r
162 uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */
\r
163 #else /*!< HSI Selected as System Clock source */
\r
164 uint32_t SystemCoreClock = HSI_VALUE; /*!< System Clock Frequency (Core Clock) */
\r
167 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
\r
172 /** @addtogroup STM32F10x_System_Private_FunctionPrototypes
\r
176 static void SetSysClock(void);
\r
178 #ifdef SYSCLK_FREQ_HSE
\r
179 static void SetSysClockToHSE(void);
\r
180 #elif defined SYSCLK_FREQ_24MHz
\r
181 static void SetSysClockTo24(void);
\r
182 #elif defined SYSCLK_FREQ_36MHz
\r
183 static void SetSysClockTo36(void);
\r
184 #elif defined SYSCLK_FREQ_48MHz
\r
185 static void SetSysClockTo48(void);
\r
186 #elif defined SYSCLK_FREQ_56MHz
\r
187 static void SetSysClockTo56(void);
\r
188 #elif defined SYSCLK_FREQ_72MHz
\r
189 static void SetSysClockTo72(void);
\r
192 #ifdef DATA_IN_ExtSRAM
\r
193 static void SystemInit_ExtMemCtl(void);
\r
194 #endif /* DATA_IN_ExtSRAM */
\r
200 /** @addtogroup STM32F10x_System_Private_Functions
\r
205 * @brief Setup the microcontroller system
\r
206 * Initialize the Embedded Flash Interface, the PLL and update the
\r
207 * SystemCoreClock variable.
\r
208 * @note This function should be used only after reset.
\r
212 void SystemInit (void)
\r
214 /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
\r
215 /* Set HSION bit */
\r
216 RCC->CR |= (uint32_t)0x00000001;
\r
218 /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
\r
219 #ifndef STM32F10X_CL
\r
220 RCC->CFGR &= (uint32_t)0xF8FF0000;
\r
222 RCC->CFGR &= (uint32_t)0xF0FF0000;
\r
223 #endif /* STM32F10X_CL */
\r
225 /* Reset HSEON, CSSON and PLLON bits */
\r
226 RCC->CR &= (uint32_t)0xFEF6FFFF;
\r
228 /* Reset HSEBYP bit */
\r
229 RCC->CR &= (uint32_t)0xFFFBFFFF;
\r
231 /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
\r
232 RCC->CFGR &= (uint32_t)0xFF80FFFF;
\r
234 #ifdef STM32F10X_CL
\r
235 /* Reset PLL2ON and PLL3ON bits */
\r
236 RCC->CR &= (uint32_t)0xEBFFFFFF;
\r
238 /* Disable all interrupts and clear pending bits */
\r
239 RCC->CIR = 0x00FF0000;
\r
241 /* Reset CFGR2 register */
\r
242 RCC->CFGR2 = 0x00000000;
\r
243 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
\r
244 /* Disable all interrupts and clear pending bits */
\r
245 RCC->CIR = 0x009F0000;
\r
247 /* Reset CFGR2 register */
\r
248 RCC->CFGR2 = 0x00000000;
\r
250 /* Disable all interrupts and clear pending bits */
\r
251 RCC->CIR = 0x009F0000;
\r
252 #endif /* STM32F10X_CL */
\r
254 #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
\r
255 #ifdef DATA_IN_ExtSRAM
\r
256 SystemInit_ExtMemCtl();
\r
257 #endif /* DATA_IN_ExtSRAM */
\r
260 /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
\r
261 /* Configure the Flash Latency cycles and enable prefetch buffer */
\r
264 #ifdef VECT_TAB_SRAM
\r
265 SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
\r
267 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
\r
272 * @brief Update SystemCoreClock variable according to Clock Register Values.
\r
273 * The SystemCoreClock variable contains the core clock (HCLK), it can
\r
274 * be used by the user application to setup the SysTick timer or configure
\r
275 * other parameters.
\r
277 * @note Each time the core clock (HCLK) changes, this function must be called
\r
278 * to update SystemCoreClock variable value. Otherwise, any configuration
\r
279 * based on this variable will be incorrect.
\r
281 * @note - The system frequency computed by this function is not the real
\r
282 * frequency in the chip. It is calculated based on the predefined
\r
283 * constant and the selected clock source:
\r
285 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
\r
287 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
\r
289 * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
\r
290 * or HSI_VALUE(*) multiplied by the PLL factors.
\r
292 * (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value
\r
293 * 8 MHz) but the real value may vary depending on the variations
\r
294 * in voltage and temperature.
\r
296 * (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value
\r
297 * 8 MHz or 25 MHz, depedning on the product used), user has to ensure
\r
298 * that HSE_VALUE is same as the real frequency of the crystal used.
\r
299 * Otherwise, this function may have wrong result.
\r
301 * - The result of this function could be not correct when using fractional
\r
302 * value for HSE crystal.
\r
306 void SystemCoreClockUpdate (void)
\r
308 uint32_t tmp = 0, pllmull = 0, pllsource = 0;
\r
310 #ifdef STM32F10X_CL
\r
311 uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
\r
312 #endif /* STM32F10X_CL */
\r
314 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
\r
315 uint32_t prediv1factor = 0;
\r
316 #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL or STM32F10X_HD_VL */
\r
318 /* Get SYSCLK source -------------------------------------------------------*/
\r
319 tmp = RCC->CFGR & RCC_CFGR_SWS;
\r
323 case 0x00: /* HSI used as system clock */
\r
324 SystemCoreClock = HSI_VALUE;
\r
326 case 0x04: /* HSE used as system clock */
\r
327 SystemCoreClock = HSE_VALUE;
\r
329 case 0x08: /* PLL used as system clock */
\r
331 /* Get PLL clock source and multiplication factor ----------------------*/
\r
332 pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
\r
333 pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
\r
335 #ifndef STM32F10X_CL
\r
336 pllmull = ( pllmull >> 18) + 2;
\r
338 if (pllsource == 0x00)
\r
340 /* HSI oscillator clock divided by 2 selected as PLL clock entry */
\r
341 SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
\r
345 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
\r
346 prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
\r
347 /* HSE oscillator clock selected as PREDIV1 clock entry */
\r
348 SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
\r
350 /* HSE selected as PLL clock entry */
\r
351 if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
\r
352 {/* HSE oscillator clock divided by 2 */
\r
353 SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
\r
357 SystemCoreClock = HSE_VALUE * pllmull;
\r
362 pllmull = pllmull >> 18;
\r
364 if (pllmull != 0x0D)
\r
369 { /* PLL multiplication factor = PLL input clock * 6.5 */
\r
373 if (pllsource == 0x00)
\r
375 /* HSI oscillator clock divided by 2 selected as PLL clock entry */
\r
376 SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
\r
379 {/* PREDIV1 selected as PLL clock entry */
\r
381 /* Get PREDIV1 clock source and division factor */
\r
382 prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
\r
383 prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
\r
385 if (prediv1source == 0)
\r
387 /* HSE oscillator clock selected as PREDIV1 clock entry */
\r
388 SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
\r
391 {/* PLL2 clock selected as PREDIV1 clock entry */
\r
393 /* Get PREDIV2 division factor and PLL2 multiplication factor */
\r
394 prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;
\r
395 pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2;
\r
396 SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
\r
399 #endif /* STM32F10X_CL */
\r
403 SystemCoreClock = HSI_VALUE;
\r
407 /* Compute HCLK clock frequency ----------------*/
\r
408 /* Get HCLK prescaler */
\r
409 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
\r
410 /* HCLK clock frequency */
\r
411 SystemCoreClock >>= tmp;
\r
415 * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
\r
419 static void SetSysClock(void)
\r
421 #ifdef SYSCLK_FREQ_HSE
\r
422 SetSysClockToHSE();
\r
423 #elif defined SYSCLK_FREQ_24MHz
\r
425 #elif defined SYSCLK_FREQ_36MHz
\r
427 #elif defined SYSCLK_FREQ_48MHz
\r
429 #elif defined SYSCLK_FREQ_56MHz
\r
430 SetSysClockTo56();
\r
431 #elif defined SYSCLK_FREQ_72MHz
\r
435 /* If none of the define above is enabled, the HSI is used as System clock
\r
436 source (default after reset) */
\r
440 * @brief Setup the external memory controller. Called in startup_stm32f10x.s
\r
441 * before jump to __main
\r
445 #ifdef DATA_IN_ExtSRAM
\r
447 * @brief Setup the external memory controller.
\r
448 * Called in startup_stm32f10x_xx.s/.c before jump to main.
\r
449 * This function configures the external SRAM mounted on STM3210E-EVAL
\r
450 * board (STM32 High density devices). This SRAM will be used as program
\r
451 * data memory (including heap and stack).
\r
455 void SystemInit_ExtMemCtl(void)
\r
457 /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
\r
458 required, then adjust the Register Addresses */
\r
460 /* Enable FSMC clock */
\r
461 RCC->AHBENR = 0x00000114;
\r
463 /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
\r
464 RCC->APB2ENR = 0x000001E0;
\r
466 /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
\r
467 /*---------------- SRAM Address lines configuration -------------------------*/
\r
468 /*---------------- NOE and NWE configuration --------------------------------*/
\r
469 /*---------------- NE3 configuration ----------------------------------------*/
\r
470 /*---------------- NBL0, NBL1 configuration ---------------------------------*/
\r
472 GPIOD->CRL = 0x44BB44BB;
\r
473 GPIOD->CRH = 0xBBBBBBBB;
\r
475 GPIOE->CRL = 0xB44444BB;
\r
476 GPIOE->CRH = 0xBBBBBBBB;
\r
478 GPIOF->CRL = 0x44BBBBBB;
\r
479 GPIOF->CRH = 0xBBBB4444;
\r
481 GPIOG->CRL = 0x44BBBBBB;
\r
482 GPIOG->CRH = 0x44444B44;
\r
484 /*---------------- FSMC Configuration ---------------------------------------*/
\r
485 /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
\r
487 FSMC_Bank1->BTCR[4] = 0x00001011;
\r
488 FSMC_Bank1->BTCR[5] = 0x00000200;
\r
490 #endif /* DATA_IN_ExtSRAM */
\r
492 #ifdef SYSCLK_FREQ_HSE
\r
494 * @brief Selects HSE as System clock source and configure HCLK, PCLK2
\r
495 * and PCLK1 prescalers.
\r
496 * @note This function should be used only after reset.
\r
500 static void SetSysClockToHSE(void)
\r
502 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
\r
504 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
\r
506 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
\r
508 /* Wait till HSE is ready and if Time out is reached exit */
\r
511 HSEStatus = RCC->CR & RCC_CR_HSERDY;
\r
513 } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
\r
515 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
\r
517 HSEStatus = (uint32_t)0x01;
\r
521 HSEStatus = (uint32_t)0x00;
\r
524 if (HSEStatus == (uint32_t)0x01)
\r
527 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
\r
528 /* Enable Prefetch Buffer */
\r
529 FLASH->ACR |= FLASH_ACR_PRFTBE;
\r
531 /* Flash 0 wait state */
\r
532 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
\r
534 #ifndef STM32F10X_CL
\r
535 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
\r
537 if (HSE_VALUE <= 24000000)
\r
539 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
\r
543 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
\r
545 #endif /* STM32F10X_CL */
\r
548 /* HCLK = SYSCLK */
\r
549 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
\r
552 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
\r
555 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
\r
557 /* Select HSE as system clock source */
\r
558 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
\r
559 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;
\r
561 /* Wait till HSE is used as system clock source */
\r
562 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)
\r
567 { /* If HSE fails to start-up, the application will have wrong clock
\r
568 configuration. User can add here some code to deal with this error */
\r
571 #elif defined SYSCLK_FREQ_24MHz
\r
573 * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2
\r
574 * and PCLK1 prescalers.
\r
575 * @note This function should be used only after reset.
\r
579 static void SetSysClockTo24(void)
\r
581 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
\r
583 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
\r
585 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
\r
587 /* Wait till HSE is ready and if Time out is reached exit */
\r
590 HSEStatus = RCC->CR & RCC_CR_HSERDY;
\r
592 } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
\r
594 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
\r
596 HSEStatus = (uint32_t)0x01;
\r
600 HSEStatus = (uint32_t)0x00;
\r
603 if (HSEStatus == (uint32_t)0x01)
\r
605 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
\r
606 /* Enable Prefetch Buffer */
\r
607 FLASH->ACR |= FLASH_ACR_PRFTBE;
\r
609 /* Flash 0 wait state */
\r
610 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
\r
611 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
\r
614 /* HCLK = SYSCLK */
\r
615 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
\r
618 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
\r
621 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
\r
623 #ifdef STM32F10X_CL
\r
624 /* Configure PLLs ------------------------------------------------------*/
\r
625 /* PLL configuration: PLLCLK = PREDIV1 * 6 = 24 MHz */
\r
626 RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
\r
627 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
\r
628 RCC_CFGR_PLLMULL6);
\r
630 /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
\r
631 /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
\r
632 RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
\r
633 RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
\r
634 RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
\r
635 RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
\r
638 RCC->CR |= RCC_CR_PLL2ON;
\r
639 /* Wait till PLL2 is ready */
\r
640 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
\r
643 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
\r
644 /* PLL configuration: = (HSE / 2) * 6 = 24 MHz */
\r
645 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
\r
646 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1_Div2 | RCC_CFGR_PLLMULL6);
\r
648 /* PLL configuration: = (HSE / 2) * 6 = 24 MHz */
\r
649 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
\r
650 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
\r
651 #endif /* STM32F10X_CL */
\r
654 RCC->CR |= RCC_CR_PLLON;
\r
656 /* Wait till PLL is ready */
\r
657 while((RCC->CR & RCC_CR_PLLRDY) == 0)
\r
661 /* Select PLL as system clock source */
\r
662 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
\r
663 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
\r
665 /* Wait till PLL is used as system clock source */
\r
666 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
\r
671 { /* If HSE fails to start-up, the application will have wrong clock
\r
672 configuration. User can add here some code to deal with this error */
\r
675 #elif defined SYSCLK_FREQ_36MHz
\r
677 * @brief Sets System clock frequency to 36MHz and configure HCLK, PCLK2
\r
678 * and PCLK1 prescalers.
\r
679 * @note This function should be used only after reset.
\r
683 static void SetSysClockTo36(void)
\r
685 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
\r
687 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
\r
689 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
\r
691 /* Wait till HSE is ready and if Time out is reached exit */
\r
694 HSEStatus = RCC->CR & RCC_CR_HSERDY;
\r
696 } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
\r
698 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
\r
700 HSEStatus = (uint32_t)0x01;
\r
704 HSEStatus = (uint32_t)0x00;
\r
707 if (HSEStatus == (uint32_t)0x01)
\r
709 /* Enable Prefetch Buffer */
\r
710 FLASH->ACR |= FLASH_ACR_PRFTBE;
\r
712 /* Flash 1 wait state */
\r
713 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
\r
714 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
\r
716 /* HCLK = SYSCLK */
\r
717 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
\r
720 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
\r
723 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
\r
725 #ifdef STM32F10X_CL
\r
726 /* Configure PLLs ------------------------------------------------------*/
\r
728 /* PLL configuration: PLLCLK = PREDIV1 * 9 = 36 MHz */
\r
729 RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
\r
730 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
\r
731 RCC_CFGR_PLLMULL9);
\r
733 /*!< PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
\r
734 /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
\r
736 RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
\r
737 RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
\r
738 RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
\r
739 RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
\r
742 RCC->CR |= RCC_CR_PLL2ON;
\r
743 /* Wait till PLL2 is ready */
\r
744 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
\r
749 /* PLL configuration: PLLCLK = (HSE / 2) * 9 = 36 MHz */
\r
750 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
\r
751 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL9);
\r
752 #endif /* STM32F10X_CL */
\r
755 RCC->CR |= RCC_CR_PLLON;
\r
757 /* Wait till PLL is ready */
\r
758 while((RCC->CR & RCC_CR_PLLRDY) == 0)
\r
762 /* Select PLL as system clock source */
\r
763 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
\r
764 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
\r
766 /* Wait till PLL is used as system clock source */
\r
767 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
\r
772 { /* If HSE fails to start-up, the application will have wrong clock
\r
773 configuration. User can add here some code to deal with this error */
\r
776 #elif defined SYSCLK_FREQ_48MHz
\r
778 * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2
\r
779 * and PCLK1 prescalers.
\r
780 * @note This function should be used only after reset.
\r
784 static void SetSysClockTo48(void)
\r
786 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
\r
788 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
\r
790 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
\r
792 /* Wait till HSE is ready and if Time out is reached exit */
\r
795 HSEStatus = RCC->CR & RCC_CR_HSERDY;
\r
797 } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
\r
799 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
\r
801 HSEStatus = (uint32_t)0x01;
\r
805 HSEStatus = (uint32_t)0x00;
\r
808 if (HSEStatus == (uint32_t)0x01)
\r
810 /* Enable Prefetch Buffer */
\r
811 FLASH->ACR |= FLASH_ACR_PRFTBE;
\r
813 /* Flash 1 wait state */
\r
814 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
\r
815 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
\r
817 /* HCLK = SYSCLK */
\r
818 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
\r
821 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
\r
824 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
\r
826 #ifdef STM32F10X_CL
\r
827 /* Configure PLLs ------------------------------------------------------*/
\r
828 /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
\r
829 /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
\r
831 RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
\r
832 RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
\r
833 RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
\r
834 RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
\r
837 RCC->CR |= RCC_CR_PLL2ON;
\r
838 /* Wait till PLL2 is ready */
\r
839 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
\r
844 /* PLL configuration: PLLCLK = PREDIV1 * 6 = 48 MHz */
\r
845 RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
\r
846 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
\r
847 RCC_CFGR_PLLMULL6);
\r
849 /* PLL configuration: PLLCLK = HSE * 6 = 48 MHz */
\r
850 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
\r
851 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
\r
852 #endif /* STM32F10X_CL */
\r
855 RCC->CR |= RCC_CR_PLLON;
\r
857 /* Wait till PLL is ready */
\r
858 while((RCC->CR & RCC_CR_PLLRDY) == 0)
\r
862 /* Select PLL as system clock source */
\r
863 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
\r
864 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
\r
866 /* Wait till PLL is used as system clock source */
\r
867 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
\r
872 { /* If HSE fails to start-up, the application will have wrong clock
\r
873 configuration. User can add here some code to deal with this error */
\r
877 #elif defined SYSCLK_FREQ_56MHz
\r
879 * @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2
\r
880 * and PCLK1 prescalers.
\r
881 * @note This function should be used only after reset.
\r
885 static void SetSysClockTo56(void)
\r
887 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
\r
889 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
\r
891 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
\r
893 /* Wait till HSE is ready and if Time out is reached exit */
\r
896 HSEStatus = RCC->CR & RCC_CR_HSERDY;
\r
898 } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
\r
900 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
\r
902 HSEStatus = (uint32_t)0x01;
\r
906 HSEStatus = (uint32_t)0x00;
\r
909 if (HSEStatus == (uint32_t)0x01)
\r
911 /* Enable Prefetch Buffer */
\r
912 FLASH->ACR |= FLASH_ACR_PRFTBE;
\r
914 /* Flash 2 wait state */
\r
915 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
\r
916 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
\r
918 /* HCLK = SYSCLK */
\r
919 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
\r
922 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
\r
925 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
\r
927 #ifdef STM32F10X_CL
\r
928 /* Configure PLLs ------------------------------------------------------*/
\r
929 /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
\r
930 /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
\r
932 RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
\r
933 RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
\r
934 RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
\r
935 RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
\r
938 RCC->CR |= RCC_CR_PLL2ON;
\r
939 /* Wait till PLL2 is ready */
\r
940 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
\r
945 /* PLL configuration: PLLCLK = PREDIV1 * 7 = 56 MHz */
\r
946 RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
\r
947 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
\r
948 RCC_CFGR_PLLMULL7);
\r
950 /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */
\r
951 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
\r
952 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7);
\r
954 #endif /* STM32F10X_CL */
\r
957 RCC->CR |= RCC_CR_PLLON;
\r
959 /* Wait till PLL is ready */
\r
960 while((RCC->CR & RCC_CR_PLLRDY) == 0)
\r
964 /* Select PLL as system clock source */
\r
965 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
\r
966 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
\r
968 /* Wait till PLL is used as system clock source */
\r
969 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
\r
974 { /* If HSE fails to start-up, the application will have wrong clock
\r
975 configuration. User can add here some code to deal with this error */
\r
979 #elif defined SYSCLK_FREQ_72MHz
\r
981 * @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2
\r
982 * and PCLK1 prescalers.
\r
983 * @note This function should be used only after reset.
\r
987 static void SetSysClockTo72(void)
\r
989 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
\r
991 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
\r
993 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
\r
995 /* Wait till HSE is ready and if Time out is reached exit */
\r
998 HSEStatus = RCC->CR & RCC_CR_HSERDY;
\r
1000 } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
\r
1002 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
\r
1004 HSEStatus = (uint32_t)0x01;
\r
1008 HSEStatus = (uint32_t)0x00;
\r
1011 if (HSEStatus == (uint32_t)0x01)
\r
1013 /* Enable Prefetch Buffer */
\r
1014 FLASH->ACR |= FLASH_ACR_PRFTBE;
\r
1016 /* Flash 2 wait state */
\r
1017 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
\r
1018 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
\r
1021 /* HCLK = SYSCLK */
\r
1022 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
\r
1024 /* PCLK2 = HCLK */
\r
1025 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
\r
1027 /* PCLK1 = HCLK */
\r
1028 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
\r
1030 #ifdef STM32F10X_CL
\r
1031 /* Configure PLLs ------------------------------------------------------*/
\r
1032 /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
\r
1033 /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
\r
1035 RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
\r
1036 RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
\r
1037 RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
\r
1038 RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
\r
1041 RCC->CR |= RCC_CR_PLL2ON;
\r
1042 /* Wait till PLL2 is ready */
\r
1043 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
\r
1048 /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */
\r
1049 RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
\r
1050 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
\r
1051 RCC_CFGR_PLLMULL9);
\r
1053 /* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
\r
1054 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
\r
1055 RCC_CFGR_PLLMULL));
\r
1056 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
\r
1057 #endif /* STM32F10X_CL */
\r
1060 RCC->CR |= RCC_CR_PLLON;
\r
1062 /* Wait till PLL is ready */
\r
1063 while((RCC->CR & RCC_CR_PLLRDY) == 0)
\r
1067 /* Select PLL as system clock source */
\r
1068 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
\r
1069 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
\r
1071 /* Wait till PLL is used as system clock source */
\r
1072 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
\r
1077 { /* If HSE fails to start-up, the application will have wrong clock
\r
1078 configuration. User can add here some code to deal with this error */
\r
1094 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
\r