STM32F10x Standard Peripherals Library  3.5.0
/opt/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.c
Go to the documentation of this file.
00001 
00065 #include "stm32f10x.h"
00066 
00106 #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
00107 /* #define SYSCLK_FREQ_HSE    HSE_VALUE */
00108  #define SYSCLK_FREQ_24MHz  24000000
00109 #else
00110 /* #define SYSCLK_FREQ_HSE    HSE_VALUE */
00111 /* #define SYSCLK_FREQ_24MHz  24000000 */ 
00112 /* #define SYSCLK_FREQ_36MHz  36000000 */
00113 /* #define SYSCLK_FREQ_48MHz  48000000 */
00114 /* #define SYSCLK_FREQ_56MHz  56000000 */
00115 #define SYSCLK_FREQ_72MHz  72000000
00116 #endif
00117 
00121 #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
00122 /* #define DATA_IN_ExtSRAM */
00123 #endif
00124 
00127 /* #define VECT_TAB_SRAM */
00128 #define VECT_TAB_OFFSET  0x0 
00148 /*******************************************************************************
00149 *  Clock Definitions
00150 *******************************************************************************/
00151 #ifdef SYSCLK_FREQ_HSE
00152   uint32_t SystemCoreClock         = SYSCLK_FREQ_HSE;        
00153 #elif defined SYSCLK_FREQ_24MHz
00154   uint32_t SystemCoreClock         = SYSCLK_FREQ_24MHz;        
00155 #elif defined SYSCLK_FREQ_36MHz
00156   uint32_t SystemCoreClock         = SYSCLK_FREQ_36MHz;        
00157 #elif defined SYSCLK_FREQ_48MHz
00158   uint32_t SystemCoreClock         = SYSCLK_FREQ_48MHz;        
00159 #elif defined SYSCLK_FREQ_56MHz
00160   uint32_t SystemCoreClock         = SYSCLK_FREQ_56MHz;        
00161 #elif defined SYSCLK_FREQ_72MHz
00162   uint32_t SystemCoreClock         = SYSCLK_FREQ_72MHz;        
00163 #else 
00164   uint32_t SystemCoreClock         = HSI_VALUE;        
00165 #endif
00166 
00167 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
00176 static void SetSysClock(void);
00177 
00178 #ifdef SYSCLK_FREQ_HSE
00179   static void SetSysClockToHSE(void);
00180 #elif defined SYSCLK_FREQ_24MHz
00181   static void SetSysClockTo24(void);
00182 #elif defined SYSCLK_FREQ_36MHz
00183   static void SetSysClockTo36(void);
00184 #elif defined SYSCLK_FREQ_48MHz
00185   static void SetSysClockTo48(void);
00186 #elif defined SYSCLK_FREQ_56MHz
00187   static void SetSysClockTo56(void);  
00188 #elif defined SYSCLK_FREQ_72MHz
00189   static void SetSysClockTo72(void);
00190 #endif
00191 
00192 #ifdef DATA_IN_ExtSRAM
00193   static void SystemInit_ExtMemCtl(void); 
00194 #endif /* DATA_IN_ExtSRAM */
00195 
00212 void SystemInit (void)
00213 {
00214   /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
00215   /* Set HSION bit */
00216   RCC->CR |= (uint32_t)0x00000001;
00217 
00218   /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
00219 #ifndef STM32F10X_CL
00220   RCC->CFGR &= (uint32_t)0xF8FF0000;
00221 #else
00222   RCC->CFGR &= (uint32_t)0xF0FF0000;
00223 #endif /* STM32F10X_CL */   
00224   
00225   /* Reset HSEON, CSSON and PLLON bits */
00226   RCC->CR &= (uint32_t)0xFEF6FFFF;
00227 
00228   /* Reset HSEBYP bit */
00229   RCC->CR &= (uint32_t)0xFFFBFFFF;
00230 
00231   /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
00232   RCC->CFGR &= (uint32_t)0xFF80FFFF;
00233 
00234 #ifdef STM32F10X_CL
00235   /* Reset PLL2ON and PLL3ON bits */
00236   RCC->CR &= (uint32_t)0xEBFFFFFF;
00237 
00238   /* Disable all interrupts and clear pending bits  */
00239   RCC->CIR = 0x00FF0000;
00240 
00241   /* Reset CFGR2 register */
00242   RCC->CFGR2 = 0x00000000;
00243 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
00244   /* Disable all interrupts and clear pending bits  */
00245   RCC->CIR = 0x009F0000;
00246 
00247   /* Reset CFGR2 register */
00248   RCC->CFGR2 = 0x00000000;      
00249 #else
00250   /* Disable all interrupts and clear pending bits  */
00251   RCC->CIR = 0x009F0000;
00252 #endif /* STM32F10X_CL */
00253     
00254 #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
00255   #ifdef DATA_IN_ExtSRAM
00256     SystemInit_ExtMemCtl(); 
00257   #endif /* DATA_IN_ExtSRAM */
00258 #endif 
00259 
00260   /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
00261   /* Configure the Flash Latency cycles and enable prefetch buffer */
00262   SetSysClock();
00263 
00264 #ifdef VECT_TAB_SRAM
00265   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
00266 #else
00267   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
00268 #endif 
00269 }
00270 
00306 void SystemCoreClockUpdate (void)
00307 {
00308   uint32_t tmp = 0, pllmull = 0, pllsource = 0;
00309 
00310 #ifdef  STM32F10X_CL
00311   uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
00312 #endif /* STM32F10X_CL */
00313 
00314 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
00315   uint32_t prediv1factor = 0;
00316 #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL or STM32F10X_HD_VL */
00317     
00318   /* Get SYSCLK source -------------------------------------------------------*/
00319   tmp = RCC->CFGR & RCC_CFGR_SWS;
00320   
00321   switch (tmp)
00322   {
00323     case 0x00:  /* HSI used as system clock */
00324       SystemCoreClock = HSI_VALUE;
00325       break;
00326     case 0x04:  /* HSE used as system clock */
00327       SystemCoreClock = HSE_VALUE;
00328       break;
00329     case 0x08:  /* PLL used as system clock */
00330 
00331       /* Get PLL clock source and multiplication factor ----------------------*/
00332       pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
00333       pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
00334       
00335 #ifndef STM32F10X_CL      
00336       pllmull = ( pllmull >> 18) + 2;
00337       
00338       if (pllsource == 0x00)
00339       {
00340         /* HSI oscillator clock divided by 2 selected as PLL clock entry */
00341         SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
00342       }
00343       else
00344       {
00345  #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
00346        prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
00347        /* HSE oscillator clock selected as PREDIV1 clock entry */
00348        SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull; 
00349  #else
00350         /* HSE selected as PLL clock entry */
00351         if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
00352         {/* HSE oscillator clock divided by 2 */
00353           SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
00354         }
00355         else
00356         {
00357           SystemCoreClock = HSE_VALUE * pllmull;
00358         }
00359  #endif
00360       }
00361 #else
00362       pllmull = pllmull >> 18;
00363       
00364       if (pllmull != 0x0D)
00365       {
00366          pllmull += 2;
00367       }
00368       else
00369       { /* PLL multiplication factor = PLL input clock * 6.5 */
00370         pllmull = 13 / 2; 
00371       }
00372             
00373       if (pllsource == 0x00)
00374       {
00375         /* HSI oscillator clock divided by 2 selected as PLL clock entry */
00376         SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
00377       }
00378       else
00379       {/* PREDIV1 selected as PLL clock entry */
00380         
00381         /* Get PREDIV1 clock source and division factor */
00382         prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
00383         prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
00384         
00385         if (prediv1source == 0)
00386         { 
00387           /* HSE oscillator clock selected as PREDIV1 clock entry */
00388           SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;          
00389         }
00390         else
00391         {/* PLL2 clock selected as PREDIV1 clock entry */
00392           
00393           /* Get PREDIV2 division factor and PLL2 multiplication factor */
00394           prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;
00395           pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2; 
00396           SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;                         
00397         }
00398       }
00399 #endif /* STM32F10X_CL */ 
00400       break;
00401 
00402     default:
00403       SystemCoreClock = HSI_VALUE;
00404       break;
00405   }
00406   
00407   /* Compute HCLK clock frequency ----------------*/
00408   /* Get HCLK prescaler */
00409   tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
00410   /* HCLK clock frequency */
00411   SystemCoreClock >>= tmp;  
00412 }
00413 
00419 static void SetSysClock(void)
00420 {
00421 #ifdef SYSCLK_FREQ_HSE
00422   SetSysClockToHSE();
00423 #elif defined SYSCLK_FREQ_24MHz
00424   SetSysClockTo24();
00425 #elif defined SYSCLK_FREQ_36MHz
00426   SetSysClockTo36();
00427 #elif defined SYSCLK_FREQ_48MHz
00428   SetSysClockTo48();
00429 #elif defined SYSCLK_FREQ_56MHz
00430   SetSysClockTo56();  
00431 #elif defined SYSCLK_FREQ_72MHz
00432   SetSysClockTo72();
00433 #endif
00434  
00435  /* If none of the define above is enabled, the HSI is used as System clock
00436     source (default after reset) */ 
00437 }
00438 
00445 #ifdef DATA_IN_ExtSRAM
00446 
00455 void SystemInit_ExtMemCtl(void) 
00456 {
00460   /* Enable FSMC clock */
00461   RCC->AHBENR = 0x00000114;
00462   
00463   /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */  
00464   RCC->APB2ENR = 0x000001E0;
00465   
00466 /* ---------------  SRAM Data lines, NOE and NWE configuration ---------------*/
00467 /*----------------  SRAM Address lines configuration -------------------------*/
00468 /*----------------  NOE and NWE configuration --------------------------------*/  
00469 /*----------------  NE3 configuration ----------------------------------------*/
00470 /*----------------  NBL0, NBL1 configuration ---------------------------------*/
00471   
00472   GPIOD->CRL = 0x44BB44BB;  
00473   GPIOD->CRH = 0xBBBBBBBB;
00474 
00475   GPIOE->CRL = 0xB44444BB;  
00476   GPIOE->CRH = 0xBBBBBBBB;
00477 
00478   GPIOF->CRL = 0x44BBBBBB;  
00479   GPIOF->CRH = 0xBBBB4444;
00480 
00481   GPIOG->CRL = 0x44BBBBBB;  
00482   GPIOG->CRH = 0x44444B44;
00483    
00484 /*----------------  FSMC Configuration ---------------------------------------*/  
00485 /*----------------  Enable FSMC Bank1_SRAM Bank ------------------------------*/
00486   
00487   FSMC_Bank1->BTCR[4] = 0x00001011;
00488   FSMC_Bank1->BTCR[5] = 0x00000200;
00489 }
00490 #endif /* DATA_IN_ExtSRAM */
00491 
00492 #ifdef SYSCLK_FREQ_HSE
00493 
00500 static void SetSysClockToHSE(void)
00501 {
00502   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
00503   
00504   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    
00505   /* Enable HSE */    
00506   RCC->CR |= ((uint32_t)RCC_CR_HSEON);
00507  
00508   /* Wait till HSE is ready and if Time out is reached exit */
00509   do
00510   {
00511     HSEStatus = RCC->CR & RCC_CR_HSERDY;
00512     StartUpCounter++;  
00513   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
00514 
00515   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
00516   {
00517     HSEStatus = (uint32_t)0x01;
00518   }
00519   else
00520   {
00521     HSEStatus = (uint32_t)0x00;
00522   }  
00523 
00524   if (HSEStatus == (uint32_t)0x01)
00525   {
00526 
00527 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
00528     /* Enable Prefetch Buffer */
00529     FLASH->ACR |= FLASH_ACR_PRFTBE;
00530 
00531     /* Flash 0 wait state */
00532     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
00533 
00534 #ifndef STM32F10X_CL
00535     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
00536 #else
00537     if (HSE_VALUE <= 24000000)
00538         {
00539       FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
00540         }
00541         else
00542         {
00543       FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
00544         }
00545 #endif /* STM32F10X_CL */
00546 #endif
00547  
00548     /* HCLK = SYSCLK */
00549     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
00550       
00551     /* PCLK2 = HCLK */
00552     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
00553     
00554     /* PCLK1 = HCLK */
00555     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
00556     
00557     /* Select HSE as system clock source */
00558     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
00559     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;    
00560 
00561     /* Wait till HSE is used as system clock source */
00562     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)
00563     {
00564     }
00565   }
00566   else
00567   { /* If HSE fails to start-up, the application will have wrong clock 
00568          configuration. User can add here some code to deal with this error */
00569   }  
00570 }
00571 #elif defined SYSCLK_FREQ_24MHz
00572 
00579 static void SetSysClockTo24(void)
00580 {
00581   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
00582   
00583   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    
00584   /* Enable HSE */    
00585   RCC->CR |= ((uint32_t)RCC_CR_HSEON);
00586  
00587   /* Wait till HSE is ready and if Time out is reached exit */
00588   do
00589   {
00590     HSEStatus = RCC->CR & RCC_CR_HSERDY;
00591     StartUpCounter++;  
00592   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
00593 
00594   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
00595   {
00596     HSEStatus = (uint32_t)0x01;
00597   }
00598   else
00599   {
00600     HSEStatus = (uint32_t)0x00;
00601   }  
00602 
00603   if (HSEStatus == (uint32_t)0x01)
00604   {
00605 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL 
00606     /* Enable Prefetch Buffer */
00607     FLASH->ACR |= FLASH_ACR_PRFTBE;
00608 
00609     /* Flash 0 wait state */
00610     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
00611     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;    
00612 #endif
00613  
00614     /* HCLK = SYSCLK */
00615     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
00616       
00617     /* PCLK2 = HCLK */
00618     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
00619     
00620     /* PCLK1 = HCLK */
00621     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
00622     
00623 #ifdef STM32F10X_CL
00624     /* Configure PLLs ------------------------------------------------------*/
00625     /* PLL configuration: PLLCLK = PREDIV1 * 6 = 24 MHz */ 
00626     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
00627     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | 
00628                             RCC_CFGR_PLLMULL6); 
00629 
00630     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
00631     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */       
00632     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
00633                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
00634     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
00635                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
00636   
00637     /* Enable PLL2 */
00638     RCC->CR |= RCC_CR_PLL2ON;
00639     /* Wait till PLL2 is ready */
00640     while((RCC->CR & RCC_CR_PLL2RDY) == 0)
00641     {
00642     }   
00643 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00644     /*  PLL configuration:  = (HSE / 2) * 6 = 24 MHz */
00645     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
00646     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1_Div2 | RCC_CFGR_PLLMULL6);
00647 #else    
00648     /*  PLL configuration:  = (HSE / 2) * 6 = 24 MHz */
00649     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
00650     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
00651 #endif /* STM32F10X_CL */
00652 
00653     /* Enable PLL */
00654     RCC->CR |= RCC_CR_PLLON;
00655 
00656     /* Wait till PLL is ready */
00657     while((RCC->CR & RCC_CR_PLLRDY) == 0)
00658     {
00659     }
00660 
00661     /* Select PLL as system clock source */
00662     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
00663     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    
00664 
00665     /* Wait till PLL is used as system clock source */
00666     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
00667     {
00668     }
00669   }
00670   else
00671   { /* If HSE fails to start-up, the application will have wrong clock 
00672          configuration. User can add here some code to deal with this error */
00673   } 
00674 }
00675 #elif defined SYSCLK_FREQ_36MHz
00676 
00683 static void SetSysClockTo36(void)
00684 {
00685   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
00686   
00687   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    
00688   /* Enable HSE */    
00689   RCC->CR |= ((uint32_t)RCC_CR_HSEON);
00690  
00691   /* Wait till HSE is ready and if Time out is reached exit */
00692   do
00693   {
00694     HSEStatus = RCC->CR & RCC_CR_HSERDY;
00695     StartUpCounter++;  
00696   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
00697 
00698   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
00699   {
00700     HSEStatus = (uint32_t)0x01;
00701   }
00702   else
00703   {
00704     HSEStatus = (uint32_t)0x00;
00705   }  
00706 
00707   if (HSEStatus == (uint32_t)0x01)
00708   {
00709     /* Enable Prefetch Buffer */
00710     FLASH->ACR |= FLASH_ACR_PRFTBE;
00711 
00712     /* Flash 1 wait state */
00713     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
00714     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;    
00715  
00716     /* HCLK = SYSCLK */
00717     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
00718       
00719     /* PCLK2 = HCLK */
00720     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
00721     
00722     /* PCLK1 = HCLK */
00723     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
00724     
00725 #ifdef STM32F10X_CL
00726     /* Configure PLLs ------------------------------------------------------*/
00727     
00728     /* PLL configuration: PLLCLK = PREDIV1 * 9 = 36 MHz */ 
00729     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
00730     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | 
00731                             RCC_CFGR_PLLMULL9); 
00732 
00734     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
00735         
00736     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
00737                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
00738     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
00739                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
00740   
00741     /* Enable PLL2 */
00742     RCC->CR |= RCC_CR_PLL2ON;
00743     /* Wait till PLL2 is ready */
00744     while((RCC->CR & RCC_CR_PLL2RDY) == 0)
00745     {
00746     }
00747     
00748 #else    
00749     /*  PLL configuration: PLLCLK = (HSE / 2) * 9 = 36 MHz */
00750     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
00751     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL9);
00752 #endif /* STM32F10X_CL */
00753 
00754     /* Enable PLL */
00755     RCC->CR |= RCC_CR_PLLON;
00756 
00757     /* Wait till PLL is ready */
00758     while((RCC->CR & RCC_CR_PLLRDY) == 0)
00759     {
00760     }
00761 
00762     /* Select PLL as system clock source */
00763     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
00764     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    
00765 
00766     /* Wait till PLL is used as system clock source */
00767     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
00768     {
00769     }
00770   }
00771   else
00772   { /* If HSE fails to start-up, the application will have wrong clock 
00773          configuration. User can add here some code to deal with this error */
00774   } 
00775 }
00776 #elif defined SYSCLK_FREQ_48MHz
00777 
00784 static void SetSysClockTo48(void)
00785 {
00786   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
00787   
00788   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    
00789   /* Enable HSE */    
00790   RCC->CR |= ((uint32_t)RCC_CR_HSEON);
00791  
00792   /* Wait till HSE is ready and if Time out is reached exit */
00793   do
00794   {
00795     HSEStatus = RCC->CR & RCC_CR_HSERDY;
00796     StartUpCounter++;  
00797   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
00798 
00799   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
00800   {
00801     HSEStatus = (uint32_t)0x01;
00802   }
00803   else
00804   {
00805     HSEStatus = (uint32_t)0x00;
00806   }  
00807 
00808   if (HSEStatus == (uint32_t)0x01)
00809   {
00810     /* Enable Prefetch Buffer */
00811     FLASH->ACR |= FLASH_ACR_PRFTBE;
00812 
00813     /* Flash 1 wait state */
00814     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
00815     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;    
00816  
00817     /* HCLK = SYSCLK */
00818     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
00819       
00820     /* PCLK2 = HCLK */
00821     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
00822     
00823     /* PCLK1 = HCLK */
00824     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
00825     
00826 #ifdef STM32F10X_CL
00827     /* Configure PLLs ------------------------------------------------------*/
00828     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
00829     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
00830         
00831     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
00832                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
00833     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
00834                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
00835   
00836     /* Enable PLL2 */
00837     RCC->CR |= RCC_CR_PLL2ON;
00838     /* Wait till PLL2 is ready */
00839     while((RCC->CR & RCC_CR_PLL2RDY) == 0)
00840     {
00841     }
00842     
00843    
00844     /* PLL configuration: PLLCLK = PREDIV1 * 6 = 48 MHz */ 
00845     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
00846     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | 
00847                             RCC_CFGR_PLLMULL6); 
00848 #else    
00849     /*  PLL configuration: PLLCLK = HSE * 6 = 48 MHz */
00850     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
00851     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
00852 #endif /* STM32F10X_CL */
00853 
00854     /* Enable PLL */
00855     RCC->CR |= RCC_CR_PLLON;
00856 
00857     /* Wait till PLL is ready */
00858     while((RCC->CR & RCC_CR_PLLRDY) == 0)
00859     {
00860     }
00861 
00862     /* Select PLL as system clock source */
00863     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
00864     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    
00865 
00866     /* Wait till PLL is used as system clock source */
00867     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
00868     {
00869     }
00870   }
00871   else
00872   { /* If HSE fails to start-up, the application will have wrong clock 
00873          configuration. User can add here some code to deal with this error */
00874   } 
00875 }
00876 
00877 #elif defined SYSCLK_FREQ_56MHz
00878 
00885 static void SetSysClockTo56(void)
00886 {
00887   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
00888   
00889   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   
00890   /* Enable HSE */    
00891   RCC->CR |= ((uint32_t)RCC_CR_HSEON);
00892  
00893   /* Wait till HSE is ready and if Time out is reached exit */
00894   do
00895   {
00896     HSEStatus = RCC->CR & RCC_CR_HSERDY;
00897     StartUpCounter++;  
00898   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
00899 
00900   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
00901   {
00902     HSEStatus = (uint32_t)0x01;
00903   }
00904   else
00905   {
00906     HSEStatus = (uint32_t)0x00;
00907   }  
00908 
00909   if (HSEStatus == (uint32_t)0x01)
00910   {
00911     /* Enable Prefetch Buffer */
00912     FLASH->ACR |= FLASH_ACR_PRFTBE;
00913 
00914     /* Flash 2 wait state */
00915     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
00916     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;    
00917  
00918     /* HCLK = SYSCLK */
00919     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
00920       
00921     /* PCLK2 = HCLK */
00922     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
00923     
00924     /* PCLK1 = HCLK */
00925     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
00926 
00927 #ifdef STM32F10X_CL
00928     /* Configure PLLs ------------------------------------------------------*/
00929     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
00930     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
00931         
00932     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
00933                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
00934     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
00935                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
00936   
00937     /* Enable PLL2 */
00938     RCC->CR |= RCC_CR_PLL2ON;
00939     /* Wait till PLL2 is ready */
00940     while((RCC->CR & RCC_CR_PLL2RDY) == 0)
00941     {
00942     }
00943     
00944    
00945     /* PLL configuration: PLLCLK = PREDIV1 * 7 = 56 MHz */ 
00946     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
00947     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | 
00948                             RCC_CFGR_PLLMULL7); 
00949 #else     
00950     /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */
00951     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
00952     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7);
00953 
00954 #endif /* STM32F10X_CL */
00955 
00956     /* Enable PLL */
00957     RCC->CR |= RCC_CR_PLLON;
00958 
00959     /* Wait till PLL is ready */
00960     while((RCC->CR & RCC_CR_PLLRDY) == 0)
00961     {
00962     }
00963 
00964     /* Select PLL as system clock source */
00965     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
00966     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    
00967 
00968     /* Wait till PLL is used as system clock source */
00969     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
00970     {
00971     }
00972   }
00973   else
00974   { /* If HSE fails to start-up, the application will have wrong clock 
00975          configuration. User can add here some code to deal with this error */
00976   } 
00977 }
00978 
00979 #elif defined SYSCLK_FREQ_72MHz
00980 
00987 static void SetSysClockTo72(void)
00988 {
00989   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
00990   
00991   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    
00992   /* Enable HSE */    
00993   RCC->CR |= ((uint32_t)RCC_CR_HSEON);
00994  
00995   /* Wait till HSE is ready and if Time out is reached exit */
00996   do
00997   {
00998     HSEStatus = RCC->CR & RCC_CR_HSERDY;
00999     StartUpCounter++;  
01000   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
01001 
01002   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
01003   {
01004     HSEStatus = (uint32_t)0x01;
01005   }
01006   else
01007   {
01008     HSEStatus = (uint32_t)0x00;
01009   }  
01010 
01011   if (HSEStatus == (uint32_t)0x01)
01012   {
01013     /* Enable Prefetch Buffer */
01014     FLASH->ACR |= FLASH_ACR_PRFTBE;
01015 
01016     /* Flash 2 wait state */
01017     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
01018     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;    
01019 
01020  
01021     /* HCLK = SYSCLK */
01022     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
01023       
01024     /* PCLK2 = HCLK */
01025     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
01026     
01027     /* PCLK1 = HCLK */
01028     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
01029 
01030 #ifdef STM32F10X_CL
01031     /* Configure PLLs ------------------------------------------------------*/
01032     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
01033     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
01034         
01035     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
01036                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
01037     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
01038                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
01039   
01040     /* Enable PLL2 */
01041     RCC->CR |= RCC_CR_PLL2ON;
01042     /* Wait till PLL2 is ready */
01043     while((RCC->CR & RCC_CR_PLL2RDY) == 0)
01044     {
01045     }
01046     
01047    
01048     /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */ 
01049     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
01050     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | 
01051                             RCC_CFGR_PLLMULL9); 
01052 #else    
01053     /*  PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
01054     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
01055                                         RCC_CFGR_PLLMULL));
01056     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
01057 #endif /* STM32F10X_CL */
01058 
01059     /* Enable PLL */
01060     RCC->CR |= RCC_CR_PLLON;
01061 
01062     /* Wait till PLL is ready */
01063     while((RCC->CR & RCC_CR_PLLRDY) == 0)
01064     {
01065     }
01066     
01067     /* Select PLL as system clock source */
01068     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
01069     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    
01070 
01071     /* Wait till PLL is used as system clock source */
01072     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
01073     {
01074     }
01075   }
01076   else
01077   { /* If HSE fails to start-up, the application will have wrong clock 
01078          configuration. User can add here some code to deal with this error */
01079   }
01080 }
01081 #endif
01082 
01094 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/