STM32F10x Standard Peripherals Library  3.5.0
/opt/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rcc.c
Go to the documentation of this file.
00001 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f10x_rcc.h"
00024 
00046 /* ------------ RCC registers bit address in the alias region ----------- */
00047 #define RCC_OFFSET                (RCC_BASE - PERIPH_BASE)
00048 
00049 /* --- CR Register ---*/
00050 
00051 /* Alias word address of HSION bit */
00052 #define CR_OFFSET                 (RCC_OFFSET + 0x00)
00053 #define HSION_BitNumber           0x00
00054 #define CR_HSION_BB               (PERIPH_BB_BASE + (CR_OFFSET * 32) + (HSION_BitNumber * 4))
00055 
00056 /* Alias word address of PLLON bit */
00057 #define PLLON_BitNumber           0x18
00058 #define CR_PLLON_BB               (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLON_BitNumber * 4))
00059 
00060 #ifdef STM32F10X_CL
00061  /* Alias word address of PLL2ON bit */
00062  #define PLL2ON_BitNumber          0x1A
00063  #define CR_PLL2ON_BB              (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLL2ON_BitNumber * 4))
00064 
00065  /* Alias word address of PLL3ON bit */
00066  #define PLL3ON_BitNumber          0x1C
00067  #define CR_PLL3ON_BB              (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLL3ON_BitNumber * 4))
00068 #endif /* STM32F10X_CL */ 
00069 
00070 /* Alias word address of CSSON bit */
00071 #define CSSON_BitNumber           0x13
00072 #define CR_CSSON_BB               (PERIPH_BB_BASE + (CR_OFFSET * 32) + (CSSON_BitNumber * 4))
00073 
00074 /* --- CFGR Register ---*/
00075 
00076 /* Alias word address of USBPRE bit */
00077 #define CFGR_OFFSET               (RCC_OFFSET + 0x04)
00078 
00079 #ifndef STM32F10X_CL
00080  #define USBPRE_BitNumber          0x16
00081  #define CFGR_USBPRE_BB            (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (USBPRE_BitNumber * 4))
00082 #else
00083  #define OTGFSPRE_BitNumber        0x16
00084  #define CFGR_OTGFSPRE_BB          (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (OTGFSPRE_BitNumber * 4))
00085 #endif /* STM32F10X_CL */ 
00086 
00087 /* --- BDCR Register ---*/
00088 
00089 /* Alias word address of RTCEN bit */
00090 #define BDCR_OFFSET               (RCC_OFFSET + 0x20)
00091 #define RTCEN_BitNumber           0x0F
00092 #define BDCR_RTCEN_BB             (PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (RTCEN_BitNumber * 4))
00093 
00094 /* Alias word address of BDRST bit */
00095 #define BDRST_BitNumber           0x10
00096 #define BDCR_BDRST_BB             (PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (BDRST_BitNumber * 4))
00097 
00098 /* --- CSR Register ---*/
00099 
00100 /* Alias word address of LSION bit */
00101 #define CSR_OFFSET                (RCC_OFFSET + 0x24)
00102 #define LSION_BitNumber           0x00
00103 #define CSR_LSION_BB              (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (LSION_BitNumber * 4))
00104 
00105 #ifdef STM32F10X_CL
00106 /* --- CFGR2 Register ---*/
00107 
00108  /* Alias word address of I2S2SRC bit */
00109  #define CFGR2_OFFSET              (RCC_OFFSET + 0x2C)
00110  #define I2S2SRC_BitNumber         0x11
00111  #define CFGR2_I2S2SRC_BB          (PERIPH_BB_BASE + (CFGR2_OFFSET * 32) + (I2S2SRC_BitNumber * 4))
00112 
00113  /* Alias word address of I2S3SRC bit */
00114  #define I2S3SRC_BitNumber         0x12
00115  #define CFGR2_I2S3SRC_BB          (PERIPH_BB_BASE + (CFGR2_OFFSET * 32) + (I2S3SRC_BitNumber * 4))
00116 #endif /* STM32F10X_CL */
00117 
00118 /* ---------------------- RCC registers bit mask ------------------------ */
00119 
00120 /* CR register bit mask */
00121 #define CR_HSEBYP_Reset           ((uint32_t)0xFFFBFFFF)
00122 #define CR_HSEBYP_Set             ((uint32_t)0x00040000)
00123 #define CR_HSEON_Reset            ((uint32_t)0xFFFEFFFF)
00124 #define CR_HSEON_Set              ((uint32_t)0x00010000)
00125 #define CR_HSITRIM_Mask           ((uint32_t)0xFFFFFF07)
00126 
00127 /* CFGR register bit mask */
00128 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL) 
00129  #define CFGR_PLL_Mask            ((uint32_t)0xFFC2FFFF)
00130 #else
00131  #define CFGR_PLL_Mask            ((uint32_t)0xFFC0FFFF)
00132 #endif /* STM32F10X_CL */ 
00133 
00134 #define CFGR_PLLMull_Mask         ((uint32_t)0x003C0000)
00135 #define CFGR_PLLSRC_Mask          ((uint32_t)0x00010000)
00136 #define CFGR_PLLXTPRE_Mask        ((uint32_t)0x00020000)
00137 #define CFGR_SWS_Mask             ((uint32_t)0x0000000C)
00138 #define CFGR_SW_Mask              ((uint32_t)0xFFFFFFFC)
00139 #define CFGR_HPRE_Reset_Mask      ((uint32_t)0xFFFFFF0F)
00140 #define CFGR_HPRE_Set_Mask        ((uint32_t)0x000000F0)
00141 #define CFGR_PPRE1_Reset_Mask     ((uint32_t)0xFFFFF8FF)
00142 #define CFGR_PPRE1_Set_Mask       ((uint32_t)0x00000700)
00143 #define CFGR_PPRE2_Reset_Mask     ((uint32_t)0xFFFFC7FF)
00144 #define CFGR_PPRE2_Set_Mask       ((uint32_t)0x00003800)
00145 #define CFGR_ADCPRE_Reset_Mask    ((uint32_t)0xFFFF3FFF)
00146 #define CFGR_ADCPRE_Set_Mask      ((uint32_t)0x0000C000)
00147 
00148 /* CSR register bit mask */
00149 #define CSR_RMVF_Set              ((uint32_t)0x01000000)
00150 
00151 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL) 
00152 /* CFGR2 register bit mask */
00153  #define CFGR2_PREDIV1SRC         ((uint32_t)0x00010000)
00154  #define CFGR2_PREDIV1            ((uint32_t)0x0000000F)
00155 #endif
00156 #ifdef STM32F10X_CL
00157  #define CFGR2_PREDIV2            ((uint32_t)0x000000F0)
00158  #define CFGR2_PLL2MUL            ((uint32_t)0x00000F00)
00159  #define CFGR2_PLL3MUL            ((uint32_t)0x0000F000)
00160 #endif /* STM32F10X_CL */ 
00161 
00162 /* RCC Flag Mask */
00163 #define FLAG_Mask                 ((uint8_t)0x1F)
00164 
00165 /* CIR register byte 2 (Bits[15:8]) base address */
00166 #define CIR_BYTE2_ADDRESS         ((uint32_t)0x40021009)
00167 
00168 /* CIR register byte 3 (Bits[23:16]) base address */
00169 #define CIR_BYTE3_ADDRESS         ((uint32_t)0x4002100A)
00170 
00171 /* CFGR register byte 4 (Bits[31:24]) base address */
00172 #define CFGR_BYTE4_ADDRESS        ((uint32_t)0x40021007)
00173 
00174 /* BDCR register base address */
00175 #define BDCR_ADDRESS              (PERIPH_BASE + BDCR_OFFSET)
00176 
00193 static __I uint8_t APBAHBPrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};
00194 static __I uint8_t ADCPrescTable[4] = {2, 4, 6, 8};
00195 
00217 void RCC_DeInit(void)
00218 {
00219   /* Set HSION bit */
00220   RCC->CR |= (uint32_t)0x00000001;
00221 
00222   /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
00223 #ifndef STM32F10X_CL
00224   RCC->CFGR &= (uint32_t)0xF8FF0000;
00225 #else
00226   RCC->CFGR &= (uint32_t)0xF0FF0000;
00227 #endif /* STM32F10X_CL */   
00228   
00229   /* Reset HSEON, CSSON and PLLON bits */
00230   RCC->CR &= (uint32_t)0xFEF6FFFF;
00231 
00232   /* Reset HSEBYP bit */
00233   RCC->CR &= (uint32_t)0xFFFBFFFF;
00234 
00235   /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
00236   RCC->CFGR &= (uint32_t)0xFF80FFFF;
00237 
00238 #ifdef STM32F10X_CL
00239   /* Reset PLL2ON and PLL3ON bits */
00240   RCC->CR &= (uint32_t)0xEBFFFFFF;
00241 
00242   /* Disable all interrupts and clear pending bits  */
00243   RCC->CIR = 0x00FF0000;
00244 
00245   /* Reset CFGR2 register */
00246   RCC->CFGR2 = 0x00000000;
00247 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00248   /* Disable all interrupts and clear pending bits  */
00249   RCC->CIR = 0x009F0000;
00250 
00251   /* Reset CFGR2 register */
00252   RCC->CFGR2 = 0x00000000;      
00253 #else
00254   /* Disable all interrupts and clear pending bits  */
00255   RCC->CIR = 0x009F0000;
00256 #endif /* STM32F10X_CL */
00257 
00258 }
00259 
00270 void RCC_HSEConfig(uint32_t RCC_HSE)
00271 {
00272   /* Check the parameters */
00273   assert_param(IS_RCC_HSE(RCC_HSE));
00274   /* Reset HSEON and HSEBYP bits before configuring the HSE ------------------*/
00275   /* Reset HSEON bit */
00276   RCC->CR &= CR_HSEON_Reset;
00277   /* Reset HSEBYP bit */
00278   RCC->CR &= CR_HSEBYP_Reset;
00279   /* Configure HSE (RCC_HSE_OFF is already covered by the code section above) */
00280   switch(RCC_HSE)
00281   {
00282     case RCC_HSE_ON:
00283       /* Set HSEON bit */
00284       RCC->CR |= CR_HSEON_Set;
00285       break;
00286       
00287     case RCC_HSE_Bypass:
00288       /* Set HSEBYP and HSEON bits */
00289       RCC->CR |= CR_HSEBYP_Set | CR_HSEON_Set;
00290       break;
00291       
00292     default:
00293       break;
00294   }
00295 }
00296 
00304 ErrorStatus RCC_WaitForHSEStartUp(void)
00305 {
00306   __IO uint32_t StartUpCounter = 0;
00307   ErrorStatus status = ERROR;
00308   FlagStatus HSEStatus = RESET;
00309   
00310   /* Wait till HSE is ready and if Time out is reached exit */
00311   do
00312   {
00313     HSEStatus = RCC_GetFlagStatus(RCC_FLAG_HSERDY);
00314     StartUpCounter++;  
00315   } while((StartUpCounter != HSE_STARTUP_TIMEOUT) && (HSEStatus == RESET));
00316   
00317   if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET)
00318   {
00319     status = SUCCESS;
00320   }
00321   else
00322   {
00323     status = ERROR;
00324   }  
00325   return (status);
00326 }
00327 
00334 void RCC_AdjustHSICalibrationValue(uint8_t HSICalibrationValue)
00335 {
00336   uint32_t tmpreg = 0;
00337   /* Check the parameters */
00338   assert_param(IS_RCC_CALIBRATION_VALUE(HSICalibrationValue));
00339   tmpreg = RCC->CR;
00340   /* Clear HSITRIM[4:0] bits */
00341   tmpreg &= CR_HSITRIM_Mask;
00342   /* Set the HSITRIM[4:0] bits according to HSICalibrationValue value */
00343   tmpreg |= (uint32_t)HSICalibrationValue << 3;
00344   /* Store the new value */
00345   RCC->CR = tmpreg;
00346 }
00347 
00354 void RCC_HSICmd(FunctionalState NewState)
00355 {
00356   /* Check the parameters */
00357   assert_param(IS_FUNCTIONAL_STATE(NewState));
00358   *(__IO uint32_t *) CR_HSION_BB = (uint32_t)NewState;
00359 }
00360 
00378 void RCC_PLLConfig(uint32_t RCC_PLLSource, uint32_t RCC_PLLMul)
00379 {
00380   uint32_t tmpreg = 0;
00381 
00382   /* Check the parameters */
00383   assert_param(IS_RCC_PLL_SOURCE(RCC_PLLSource));
00384   assert_param(IS_RCC_PLL_MUL(RCC_PLLMul));
00385 
00386   tmpreg = RCC->CFGR;
00387   /* Clear PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
00388   tmpreg &= CFGR_PLL_Mask;
00389   /* Set the PLL configuration bits */
00390   tmpreg |= RCC_PLLSource | RCC_PLLMul;
00391   /* Store the new value */
00392   RCC->CFGR = tmpreg;
00393 }
00394 
00401 void RCC_PLLCmd(FunctionalState NewState)
00402 {
00403   /* Check the parameters */
00404   assert_param(IS_FUNCTIONAL_STATE(NewState));
00405 
00406   *(__IO uint32_t *) CR_PLLON_BB = (uint32_t)NewState;
00407 }
00408 
00409 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL)
00410 
00426 void RCC_PREDIV1Config(uint32_t RCC_PREDIV1_Source, uint32_t RCC_PREDIV1_Div)
00427 {
00428   uint32_t tmpreg = 0;
00429   
00430   /* Check the parameters */
00431   assert_param(IS_RCC_PREDIV1_SOURCE(RCC_PREDIV1_Source));
00432   assert_param(IS_RCC_PREDIV1(RCC_PREDIV1_Div));
00433 
00434   tmpreg = RCC->CFGR2;
00435   /* Clear PREDIV1[3:0] and PREDIV1SRC bits */
00436   tmpreg &= ~(CFGR2_PREDIV1 | CFGR2_PREDIV1SRC);
00437   /* Set the PREDIV1 clock source and division factor */
00438   tmpreg |= RCC_PREDIV1_Source | RCC_PREDIV1_Div ;
00439   /* Store the new value */
00440   RCC->CFGR2 = tmpreg;
00441 }
00442 #endif
00443 
00444 #ifdef STM32F10X_CL
00445 
00454 void RCC_PREDIV2Config(uint32_t RCC_PREDIV2_Div)
00455 {
00456   uint32_t tmpreg = 0;
00457 
00458   /* Check the parameters */
00459   assert_param(IS_RCC_PREDIV2(RCC_PREDIV2_Div));
00460 
00461   tmpreg = RCC->CFGR2;
00462   /* Clear PREDIV2[3:0] bits */
00463   tmpreg &= ~CFGR2_PREDIV2;
00464   /* Set the PREDIV2 division factor */
00465   tmpreg |= RCC_PREDIV2_Div;
00466   /* Store the new value */
00467   RCC->CFGR2 = tmpreg;
00468 }
00469 
00479 void RCC_PLL2Config(uint32_t RCC_PLL2Mul)
00480 {
00481   uint32_t tmpreg = 0;
00482 
00483   /* Check the parameters */
00484   assert_param(IS_RCC_PLL2_MUL(RCC_PLL2Mul));
00485 
00486   tmpreg = RCC->CFGR2;
00487   /* Clear PLL2Mul[3:0] bits */
00488   tmpreg &= ~CFGR2_PLL2MUL;
00489   /* Set the PLL2 configuration bits */
00490   tmpreg |= RCC_PLL2Mul;
00491   /* Store the new value */
00492   RCC->CFGR2 = tmpreg;
00493 }
00494 
00495 
00505 void RCC_PLL2Cmd(FunctionalState NewState)
00506 {
00507   /* Check the parameters */
00508   assert_param(IS_FUNCTIONAL_STATE(NewState));
00509 
00510   *(__IO uint32_t *) CR_PLL2ON_BB = (uint32_t)NewState;
00511 }
00512 
00513 
00523 void RCC_PLL3Config(uint32_t RCC_PLL3Mul)
00524 {
00525   uint32_t tmpreg = 0;
00526 
00527   /* Check the parameters */
00528   assert_param(IS_RCC_PLL3_MUL(RCC_PLL3Mul));
00529 
00530   tmpreg = RCC->CFGR2;
00531   /* Clear PLL3Mul[3:0] bits */
00532   tmpreg &= ~CFGR2_PLL3MUL;
00533   /* Set the PLL3 configuration bits */
00534   tmpreg |= RCC_PLL3Mul;
00535   /* Store the new value */
00536   RCC->CFGR2 = tmpreg;
00537 }
00538 
00539 
00546 void RCC_PLL3Cmd(FunctionalState NewState)
00547 {
00548   /* Check the parameters */
00549 
00550   assert_param(IS_FUNCTIONAL_STATE(NewState));
00551   *(__IO uint32_t *) CR_PLL3ON_BB = (uint32_t)NewState;
00552 }
00553 #endif /* STM32F10X_CL */
00554 
00564 void RCC_SYSCLKConfig(uint32_t RCC_SYSCLKSource)
00565 {
00566   uint32_t tmpreg = 0;
00567   /* Check the parameters */
00568   assert_param(IS_RCC_SYSCLK_SOURCE(RCC_SYSCLKSource));
00569   tmpreg = RCC->CFGR;
00570   /* Clear SW[1:0] bits */
00571   tmpreg &= CFGR_SW_Mask;
00572   /* Set SW[1:0] bits according to RCC_SYSCLKSource value */
00573   tmpreg |= RCC_SYSCLKSource;
00574   /* Store the new value */
00575   RCC->CFGR = tmpreg;
00576 }
00577 
00587 uint8_t RCC_GetSYSCLKSource(void)
00588 {
00589   return ((uint8_t)(RCC->CFGR & CFGR_SWS_Mask));
00590 }
00591 
00608 void RCC_HCLKConfig(uint32_t RCC_SYSCLK)
00609 {
00610   uint32_t tmpreg = 0;
00611   /* Check the parameters */
00612   assert_param(IS_RCC_HCLK(RCC_SYSCLK));
00613   tmpreg = RCC->CFGR;
00614   /* Clear HPRE[3:0] bits */
00615   tmpreg &= CFGR_HPRE_Reset_Mask;
00616   /* Set HPRE[3:0] bits according to RCC_SYSCLK value */
00617   tmpreg |= RCC_SYSCLK;
00618   /* Store the new value */
00619   RCC->CFGR = tmpreg;
00620 }
00621 
00634 void RCC_PCLK1Config(uint32_t RCC_HCLK)
00635 {
00636   uint32_t tmpreg = 0;
00637   /* Check the parameters */
00638   assert_param(IS_RCC_PCLK(RCC_HCLK));
00639   tmpreg = RCC->CFGR;
00640   /* Clear PPRE1[2:0] bits */
00641   tmpreg &= CFGR_PPRE1_Reset_Mask;
00642   /* Set PPRE1[2:0] bits according to RCC_HCLK value */
00643   tmpreg |= RCC_HCLK;
00644   /* Store the new value */
00645   RCC->CFGR = tmpreg;
00646 }
00647 
00660 void RCC_PCLK2Config(uint32_t RCC_HCLK)
00661 {
00662   uint32_t tmpreg = 0;
00663   /* Check the parameters */
00664   assert_param(IS_RCC_PCLK(RCC_HCLK));
00665   tmpreg = RCC->CFGR;
00666   /* Clear PPRE2[2:0] bits */
00667   tmpreg &= CFGR_PPRE2_Reset_Mask;
00668   /* Set PPRE2[2:0] bits according to RCC_HCLK value */
00669   tmpreg |= RCC_HCLK << 3;
00670   /* Store the new value */
00671   RCC->CFGR = tmpreg;
00672 }
00673 
00700 void RCC_ITConfig(uint8_t RCC_IT, FunctionalState NewState)
00701 {
00702   /* Check the parameters */
00703   assert_param(IS_RCC_IT(RCC_IT));
00704   assert_param(IS_FUNCTIONAL_STATE(NewState));
00705   if (NewState != DISABLE)
00706   {
00707     /* Perform Byte access to RCC_CIR bits to enable the selected interrupts */
00708     *(__IO uint8_t *) CIR_BYTE2_ADDRESS |= RCC_IT;
00709   }
00710   else
00711   {
00712     /* Perform Byte access to RCC_CIR bits to disable the selected interrupts */
00713     *(__IO uint8_t *) CIR_BYTE2_ADDRESS &= (uint8_t)~RCC_IT;
00714   }
00715 }
00716 
00717 #ifndef STM32F10X_CL
00718 
00728 void RCC_USBCLKConfig(uint32_t RCC_USBCLKSource)
00729 {
00730   /* Check the parameters */
00731   assert_param(IS_RCC_USBCLK_SOURCE(RCC_USBCLKSource));
00732 
00733   *(__IO uint32_t *) CFGR_USBPRE_BB = RCC_USBCLKSource;
00734 }
00735 #else
00736 
00746 void RCC_OTGFSCLKConfig(uint32_t RCC_OTGFSCLKSource)
00747 {
00748   /* Check the parameters */
00749   assert_param(IS_RCC_OTGFSCLK_SOURCE(RCC_OTGFSCLKSource));
00750 
00751   *(__IO uint32_t *) CFGR_OTGFSPRE_BB = RCC_OTGFSCLKSource;
00752 }
00753 #endif /* STM32F10X_CL */ 
00754 
00766 void RCC_ADCCLKConfig(uint32_t RCC_PCLK2)
00767 {
00768   uint32_t tmpreg = 0;
00769   /* Check the parameters */
00770   assert_param(IS_RCC_ADCCLK(RCC_PCLK2));
00771   tmpreg = RCC->CFGR;
00772   /* Clear ADCPRE[1:0] bits */
00773   tmpreg &= CFGR_ADCPRE_Reset_Mask;
00774   /* Set ADCPRE[1:0] bits according to RCC_PCLK2 value */
00775   tmpreg |= RCC_PCLK2;
00776   /* Store the new value */
00777   RCC->CFGR = tmpreg;
00778 }
00779 
00780 #ifdef STM32F10X_CL
00781 
00792 void RCC_I2S2CLKConfig(uint32_t RCC_I2S2CLKSource)
00793 {
00794   /* Check the parameters */
00795   assert_param(IS_RCC_I2S2CLK_SOURCE(RCC_I2S2CLKSource));
00796 
00797   *(__IO uint32_t *) CFGR2_I2S2SRC_BB = RCC_I2S2CLKSource;
00798 }
00799 
00811 void RCC_I2S3CLKConfig(uint32_t RCC_I2S3CLKSource)
00812 {
00813   /* Check the parameters */
00814   assert_param(IS_RCC_I2S3CLK_SOURCE(RCC_I2S3CLKSource));
00815 
00816   *(__IO uint32_t *) CFGR2_I2S3SRC_BB = RCC_I2S3CLKSource;
00817 }
00818 #endif /* STM32F10X_CL */
00819 
00829 void RCC_LSEConfig(uint8_t RCC_LSE)
00830 {
00831   /* Check the parameters */
00832   assert_param(IS_RCC_LSE(RCC_LSE));
00833   /* Reset LSEON and LSEBYP bits before configuring the LSE ------------------*/
00834   /* Reset LSEON bit */
00835   *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_OFF;
00836   /* Reset LSEBYP bit */
00837   *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_OFF;
00838   /* Configure LSE (RCC_LSE_OFF is already covered by the code section above) */
00839   switch(RCC_LSE)
00840   {
00841     case RCC_LSE_ON:
00842       /* Set LSEON bit */
00843       *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_ON;
00844       break;
00845       
00846     case RCC_LSE_Bypass:
00847       /* Set LSEBYP and LSEON bits */
00848       *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_Bypass | RCC_LSE_ON;
00849       break;            
00850       
00851     default:
00852       break;      
00853   }
00854 }
00855 
00862 void RCC_LSICmd(FunctionalState NewState)
00863 {
00864   /* Check the parameters */
00865   assert_param(IS_FUNCTIONAL_STATE(NewState));
00866   *(__IO uint32_t *) CSR_LSION_BB = (uint32_t)NewState;
00867 }
00868 
00879 void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource)
00880 {
00881   /* Check the parameters */
00882   assert_param(IS_RCC_RTCCLK_SOURCE(RCC_RTCCLKSource));
00883   /* Select the RTC clock source */
00884   RCC->BDCR |= RCC_RTCCLKSource;
00885 }
00886 
00893 void RCC_RTCCLKCmd(FunctionalState NewState)
00894 {
00895   /* Check the parameters */
00896   assert_param(IS_FUNCTIONAL_STATE(NewState));
00897   *(__IO uint32_t *) BDCR_RTCEN_BB = (uint32_t)NewState;
00898 }
00899 
00908 void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks)
00909 {
00910   uint32_t tmp = 0, pllmull = 0, pllsource = 0, presc = 0;
00911 
00912 #ifdef  STM32F10X_CL
00913   uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
00914 #endif /* STM32F10X_CL */
00915 
00916 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00917   uint32_t prediv1factor = 0;
00918 #endif
00919     
00920   /* Get SYSCLK source -------------------------------------------------------*/
00921   tmp = RCC->CFGR & CFGR_SWS_Mask;
00922   
00923   switch (tmp)
00924   {
00925     case 0x00:  /* HSI used as system clock */
00926       RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
00927       break;
00928     case 0x04:  /* HSE used as system clock */
00929       RCC_Clocks->SYSCLK_Frequency = HSE_VALUE;
00930       break;
00931     case 0x08:  /* PLL used as system clock */
00932 
00933       /* Get PLL clock source and multiplication factor ----------------------*/
00934       pllmull = RCC->CFGR & CFGR_PLLMull_Mask;
00935       pllsource = RCC->CFGR & CFGR_PLLSRC_Mask;
00936       
00937 #ifndef STM32F10X_CL      
00938       pllmull = ( pllmull >> 18) + 2;
00939       
00940       if (pllsource == 0x00)
00941       {/* HSI oscillator clock divided by 2 selected as PLL clock entry */
00942         RCC_Clocks->SYSCLK_Frequency = (HSI_VALUE >> 1) * pllmull;
00943       }
00944       else
00945       {
00946  #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00947        prediv1factor = (RCC->CFGR2 & CFGR2_PREDIV1) + 1;
00948        /* HSE oscillator clock selected as PREDIV1 clock entry */
00949        RCC_Clocks->SYSCLK_Frequency = (HSE_VALUE / prediv1factor) * pllmull; 
00950  #else
00951         /* HSE selected as PLL clock entry */
00952         if ((RCC->CFGR & CFGR_PLLXTPRE_Mask) != (uint32_t)RESET)
00953         {/* HSE oscillator clock divided by 2 */
00954           RCC_Clocks->SYSCLK_Frequency = (HSE_VALUE >> 1) * pllmull;
00955         }
00956         else
00957         {
00958           RCC_Clocks->SYSCLK_Frequency = HSE_VALUE * pllmull;
00959         }
00960  #endif
00961       }
00962 #else
00963       pllmull = pllmull >> 18;
00964       
00965       if (pllmull != 0x0D)
00966       {
00967          pllmull += 2;
00968       }
00969       else
00970       { /* PLL multiplication factor = PLL input clock * 6.5 */
00971         pllmull = 13 / 2; 
00972       }
00973             
00974       if (pllsource == 0x00)
00975       {/* HSI oscillator clock divided by 2 selected as PLL clock entry */
00976         RCC_Clocks->SYSCLK_Frequency = (HSI_VALUE >> 1) * pllmull;
00977       }
00978       else
00979       {/* PREDIV1 selected as PLL clock entry */
00980         
00981         /* Get PREDIV1 clock source and division factor */
00982         prediv1source = RCC->CFGR2 & CFGR2_PREDIV1SRC;
00983         prediv1factor = (RCC->CFGR2 & CFGR2_PREDIV1) + 1;
00984         
00985         if (prediv1source == 0)
00986         { /* HSE oscillator clock selected as PREDIV1 clock entry */
00987           RCC_Clocks->SYSCLK_Frequency = (HSE_VALUE / prediv1factor) * pllmull;          
00988         }
00989         else
00990         {/* PLL2 clock selected as PREDIV1 clock entry */
00991           
00992           /* Get PREDIV2 division factor and PLL2 multiplication factor */
00993           prediv2factor = ((RCC->CFGR2 & CFGR2_PREDIV2) >> 4) + 1;
00994           pll2mull = ((RCC->CFGR2 & CFGR2_PLL2MUL) >> 8 ) + 2; 
00995           RCC_Clocks->SYSCLK_Frequency = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;                         
00996         }
00997       }
00998 #endif /* STM32F10X_CL */ 
00999       break;
01000 
01001     default:
01002       RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
01003       break;
01004   }
01005 
01006   /* Compute HCLK, PCLK1, PCLK2 and ADCCLK clocks frequencies ----------------*/
01007   /* Get HCLK prescaler */
01008   tmp = RCC->CFGR & CFGR_HPRE_Set_Mask;
01009   tmp = tmp >> 4;
01010   presc = APBAHBPrescTable[tmp];
01011   /* HCLK clock frequency */
01012   RCC_Clocks->HCLK_Frequency = RCC_Clocks->SYSCLK_Frequency >> presc;
01013   /* Get PCLK1 prescaler */
01014   tmp = RCC->CFGR & CFGR_PPRE1_Set_Mask;
01015   tmp = tmp >> 8;
01016   presc = APBAHBPrescTable[tmp];
01017   /* PCLK1 clock frequency */
01018   RCC_Clocks->PCLK1_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
01019   /* Get PCLK2 prescaler */
01020   tmp = RCC->CFGR & CFGR_PPRE2_Set_Mask;
01021   tmp = tmp >> 11;
01022   presc = APBAHBPrescTable[tmp];
01023   /* PCLK2 clock frequency */
01024   RCC_Clocks->PCLK2_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
01025   /* Get ADCCLK prescaler */
01026   tmp = RCC->CFGR & CFGR_ADCPRE_Set_Mask;
01027   tmp = tmp >> 14;
01028   presc = ADCPrescTable[tmp];
01029   /* ADCCLK clock frequency */
01030   RCC_Clocks->ADCCLK_Frequency = RCC_Clocks->PCLK2_Frequency / presc;
01031 }
01032 
01064 void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)
01065 {
01066   /* Check the parameters */
01067   assert_param(IS_RCC_AHB_PERIPH(RCC_AHBPeriph));
01068   assert_param(IS_FUNCTIONAL_STATE(NewState));
01069 
01070   if (NewState != DISABLE)
01071   {
01072     RCC->AHBENR |= RCC_AHBPeriph;
01073   }
01074   else
01075   {
01076     RCC->AHBENR &= ~RCC_AHBPeriph;
01077   }
01078 }
01079 
01095 void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
01096 {
01097   /* Check the parameters */
01098   assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
01099   assert_param(IS_FUNCTIONAL_STATE(NewState));
01100   if (NewState != DISABLE)
01101   {
01102     RCC->APB2ENR |= RCC_APB2Periph;
01103   }
01104   else
01105   {
01106     RCC->APB2ENR &= ~RCC_APB2Periph;
01107   }
01108 }
01109 
01126 void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
01127 {
01128   /* Check the parameters */
01129   assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
01130   assert_param(IS_FUNCTIONAL_STATE(NewState));
01131   if (NewState != DISABLE)
01132   {
01133     RCC->APB1ENR |= RCC_APB1Periph;
01134   }
01135   else
01136   {
01137     RCC->APB1ENR &= ~RCC_APB1Periph;
01138   }
01139 }
01140 
01141 #ifdef STM32F10X_CL
01142 
01153 void RCC_AHBPeriphResetCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)
01154 {
01155   /* Check the parameters */
01156   assert_param(IS_RCC_AHB_PERIPH_RESET(RCC_AHBPeriph));
01157   assert_param(IS_FUNCTIONAL_STATE(NewState));
01158 
01159   if (NewState != DISABLE)
01160   {
01161     RCC->AHBRSTR |= RCC_AHBPeriph;
01162   }
01163   else
01164   {
01165     RCC->AHBRSTR &= ~RCC_AHBPeriph;
01166   }
01167 }
01168 #endif /* STM32F10X_CL */ 
01169 
01185 void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
01186 {
01187   /* Check the parameters */
01188   assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
01189   assert_param(IS_FUNCTIONAL_STATE(NewState));
01190   if (NewState != DISABLE)
01191   {
01192     RCC->APB2RSTR |= RCC_APB2Periph;
01193   }
01194   else
01195   {
01196     RCC->APB2RSTR &= ~RCC_APB2Periph;
01197   }
01198 }
01199 
01216 void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
01217 {
01218   /* Check the parameters */
01219   assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
01220   assert_param(IS_FUNCTIONAL_STATE(NewState));
01221   if (NewState != DISABLE)
01222   {
01223     RCC->APB1RSTR |= RCC_APB1Periph;
01224   }
01225   else
01226   {
01227     RCC->APB1RSTR &= ~RCC_APB1Periph;
01228   }
01229 }
01230 
01237 void RCC_BackupResetCmd(FunctionalState NewState)
01238 {
01239   /* Check the parameters */
01240   assert_param(IS_FUNCTIONAL_STATE(NewState));
01241   *(__IO uint32_t *) BDCR_BDRST_BB = (uint32_t)NewState;
01242 }
01243 
01250 void RCC_ClockSecuritySystemCmd(FunctionalState NewState)
01251 {
01252   /* Check the parameters */
01253   assert_param(IS_FUNCTIONAL_STATE(NewState));
01254   *(__IO uint32_t *) CR_CSSON_BB = (uint32_t)NewState;
01255 }
01256 
01282 void RCC_MCOConfig(uint8_t RCC_MCO)
01283 {
01284   /* Check the parameters */
01285   assert_param(IS_RCC_MCO(RCC_MCO));
01286 
01287   /* Perform Byte access to MCO bits to select the MCO source */
01288   *(__IO uint8_t *) CFGR_BYTE4_ADDRESS = RCC_MCO;
01289 }
01290 
01326 FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG)
01327 {
01328   uint32_t tmp = 0;
01329   uint32_t statusreg = 0;
01330   FlagStatus bitstatus = RESET;
01331   /* Check the parameters */
01332   assert_param(IS_RCC_FLAG(RCC_FLAG));
01333 
01334   /* Get the RCC register index */
01335   tmp = RCC_FLAG >> 5;
01336   if (tmp == 1)               /* The flag to check is in CR register */
01337   {
01338     statusreg = RCC->CR;
01339   }
01340   else if (tmp == 2)          /* The flag to check is in BDCR register */
01341   {
01342     statusreg = RCC->BDCR;
01343   }
01344   else                       /* The flag to check is in CSR register */
01345   {
01346     statusreg = RCC->CSR;
01347   }
01348 
01349   /* Get the flag position */
01350   tmp = RCC_FLAG & FLAG_Mask;
01351   if ((statusreg & ((uint32_t)1 << tmp)) != (uint32_t)RESET)
01352   {
01353     bitstatus = SET;
01354   }
01355   else
01356   {
01357     bitstatus = RESET;
01358   }
01359 
01360   /* Return the flag status */
01361   return bitstatus;
01362 }
01363 
01371 void RCC_ClearFlag(void)
01372 {
01373   /* Set RMVF bit to clear the reset flags */
01374   RCC->CSR |= CSR_RMVF_Set;
01375 }
01376 
01402 ITStatus RCC_GetITStatus(uint8_t RCC_IT)
01403 {
01404   ITStatus bitstatus = RESET;
01405   /* Check the parameters */
01406   assert_param(IS_RCC_GET_IT(RCC_IT));
01407 
01408   /* Check the status of the specified RCC interrupt */
01409   if ((RCC->CIR & RCC_IT) != (uint32_t)RESET)
01410   {
01411     bitstatus = SET;
01412   }
01413   else
01414   {
01415     bitstatus = RESET;
01416   }
01417 
01418   /* Return the RCC_IT status */
01419   return  bitstatus;
01420 }
01421 
01448 void RCC_ClearITPendingBit(uint8_t RCC_IT)
01449 {
01450   /* Check the parameters */
01451   assert_param(IS_RCC_CLEAR_IT(RCC_IT));
01452 
01453   /* Perform Byte access to RCC_CIR[23:16] bits to clear the selected interrupt
01454      pending bits */
01455   *(__IO uint8_t *) CIR_BYTE3_ADDRESS = RCC_IT;
01456 }
01457 
01470 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/