uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
stm32f10x_rcc.c
Go to the documentation of this file.
1 
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32f10x_rcc.h"
24 
46 /* ------------ RCC registers bit address in the alias region ----------- */
47 #define RCC_OFFSET (RCC_BASE - PERIPH_BASE)
48 
49 /* --- CR Register ---*/
50 
51 /* Alias word address of HSION bit */
52 #define CR_OFFSET (RCC_OFFSET + 0x00)
53 #define HSION_BitNumber 0x00
54 #define CR_HSION_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (HSION_BitNumber * 4))
55 
56 /* Alias word address of PLLON bit */
57 #define PLLON_BitNumber 0x18
58 #define CR_PLLON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLON_BitNumber * 4))
59 
60 #ifdef STM32F10X_CL
61  /* Alias word address of PLL2ON bit */
62  #define PLL2ON_BitNumber 0x1A
63  #define CR_PLL2ON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLL2ON_BitNumber * 4))
64 
65  /* Alias word address of PLL3ON bit */
66  #define PLL3ON_BitNumber 0x1C
67  #define CR_PLL3ON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLL3ON_BitNumber * 4))
68 #endif /* STM32F10X_CL */
69 
70 /* Alias word address of CSSON bit */
71 #define CSSON_BitNumber 0x13
72 #define CR_CSSON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (CSSON_BitNumber * 4))
73 
74 /* --- CFGR Register ---*/
75 
76 /* Alias word address of USBPRE bit */
77 #define CFGR_OFFSET (RCC_OFFSET + 0x04)
78 
79 #ifndef STM32F10X_CL
80  #define USBPRE_BitNumber 0x16
81  #define CFGR_USBPRE_BB (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (USBPRE_BitNumber * 4))
82 #else
83  #define OTGFSPRE_BitNumber 0x16
84  #define CFGR_OTGFSPRE_BB (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (OTGFSPRE_BitNumber * 4))
85 #endif /* STM32F10X_CL */
86 
87 /* --- BDCR Register ---*/
88 
89 /* Alias word address of RTCEN bit */
90 #define BDCR_OFFSET (RCC_OFFSET + 0x20)
91 #define RTCEN_BitNumber 0x0F
92 #define BDCR_RTCEN_BB (PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (RTCEN_BitNumber * 4))
93 
94 /* Alias word address of BDRST bit */
95 #define BDRST_BitNumber 0x10
96 #define BDCR_BDRST_BB (PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (BDRST_BitNumber * 4))
97 
98 /* --- CSR Register ---*/
99 
100 /* Alias word address of LSION bit */
101 #define CSR_OFFSET (RCC_OFFSET + 0x24)
102 #define LSION_BitNumber 0x00
103 #define CSR_LSION_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (LSION_BitNumber * 4))
104 
105 #ifdef STM32F10X_CL
106 /* --- CFGR2 Register ---*/
107 
108  /* Alias word address of I2S2SRC bit */
109  #define CFGR2_OFFSET (RCC_OFFSET + 0x2C)
110  #define I2S2SRC_BitNumber 0x11
111  #define CFGR2_I2S2SRC_BB (PERIPH_BB_BASE + (CFGR2_OFFSET * 32) + (I2S2SRC_BitNumber * 4))
112 
113  /* Alias word address of I2S3SRC bit */
114  #define I2S3SRC_BitNumber 0x12
115  #define CFGR2_I2S3SRC_BB (PERIPH_BB_BASE + (CFGR2_OFFSET * 32) + (I2S3SRC_BitNumber * 4))
116 #endif /* STM32F10X_CL */
117 
118 /* ---------------------- RCC registers bit mask ------------------------ */
119 
120 /* CR register bit mask */
121 #define CR_HSEBYP_Reset ((uint32_t)0xFFFBFFFF)
122 #define CR_HSEBYP_Set ((uint32_t)0x00040000)
123 #define CR_HSEON_Reset ((uint32_t)0xFFFEFFFF)
124 #define CR_HSEON_Set ((uint32_t)0x00010000)
125 #define CR_HSITRIM_Mask ((uint32_t)0xFFFFFF07)
126 
127 /* CFGR register bit mask */
128 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL)
129  #define CFGR_PLL_Mask ((uint32_t)0xFFC2FFFF)
130 #else
131  #define CFGR_PLL_Mask ((uint32_t)0xFFC0FFFF)
132 #endif /* STM32F10X_CL */
133 
134 #define CFGR_PLLMull_Mask ((uint32_t)0x003C0000)
135 #define CFGR_PLLSRC_Mask ((uint32_t)0x00010000)
136 #define CFGR_PLLXTPRE_Mask ((uint32_t)0x00020000)
137 #define CFGR_SWS_Mask ((uint32_t)0x0000000C)
138 #define CFGR_SW_Mask ((uint32_t)0xFFFFFFFC)
139 #define CFGR_HPRE_Reset_Mask ((uint32_t)0xFFFFFF0F)
140 #define CFGR_HPRE_Set_Mask ((uint32_t)0x000000F0)
141 #define CFGR_PPRE1_Reset_Mask ((uint32_t)0xFFFFF8FF)
142 #define CFGR_PPRE1_Set_Mask ((uint32_t)0x00000700)
143 #define CFGR_PPRE2_Reset_Mask ((uint32_t)0xFFFFC7FF)
144 #define CFGR_PPRE2_Set_Mask ((uint32_t)0x00003800)
145 #define CFGR_ADCPRE_Reset_Mask ((uint32_t)0xFFFF3FFF)
146 #define CFGR_ADCPRE_Set_Mask ((uint32_t)0x0000C000)
147 
148 /* CSR register bit mask */
149 #define CSR_RMVF_Set ((uint32_t)0x01000000)
150 
151 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL)
152 /* CFGR2 register bit mask */
153  #define CFGR2_PREDIV1SRC ((uint32_t)0x00010000)
154  #define CFGR2_PREDIV1 ((uint32_t)0x0000000F)
155 #endif
156 #ifdef STM32F10X_CL
157  #define CFGR2_PREDIV2 ((uint32_t)0x000000F0)
158  #define CFGR2_PLL2MUL ((uint32_t)0x00000F00)
159  #define CFGR2_PLL3MUL ((uint32_t)0x0000F000)
160 #endif /* STM32F10X_CL */
161 
162 /* RCC Flag Mask */
163 #define FLAG_Mask ((uint8_t)0x1F)
164 
165 /* CIR register byte 2 (Bits[15:8]) base address */
166 #define CIR_BYTE2_ADDRESS ((uint32_t)0x40021009)
167 
168 /* CIR register byte 3 (Bits[23:16]) base address */
169 #define CIR_BYTE3_ADDRESS ((uint32_t)0x4002100A)
170 
171 /* CFGR register byte 4 (Bits[31:24]) base address */
172 #define CFGR_BYTE4_ADDRESS ((uint32_t)0x40021007)
173 
174 /* BDCR register base address */
175 #define BDCR_ADDRESS (PERIPH_BASE + BDCR_OFFSET)
176 
193 static __I uint8_t APBAHBPrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};
194 static __I uint8_t ADCPrescTable[4] = {2, 4, 6, 8};
195 
217 void RCC_DeInit(void)
218 {
219  /* Set HSION bit */
220  RCC->CR |= (uint32_t)0x00000001;
221 
222  /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
223 #ifndef STM32F10X_CL
224  RCC->CFGR &= (uint32_t)0xF8FF0000;
225 #else
226  RCC->CFGR &= (uint32_t)0xF0FF0000;
227 #endif /* STM32F10X_CL */
228 
229  /* Reset HSEON, CSSON and PLLON bits */
230  RCC->CR &= (uint32_t)0xFEF6FFFF;
231 
232  /* Reset HSEBYP bit */
233  RCC->CR &= (uint32_t)0xFFFBFFFF;
234 
235  /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
236  RCC->CFGR &= (uint32_t)0xFF80FFFF;
237 
238 #ifdef STM32F10X_CL
239  /* Reset PLL2ON and PLL3ON bits */
240  RCC->CR &= (uint32_t)0xEBFFFFFF;
241 
242  /* Disable all interrupts and clear pending bits */
243  RCC->CIR = 0x00FF0000;
244 
245  /* Reset CFGR2 register */
246  RCC->CFGR2 = 0x00000000;
247 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
248  /* Disable all interrupts and clear pending bits */
249  RCC->CIR = 0x009F0000;
250 
251  /* Reset CFGR2 register */
252  RCC->CFGR2 = 0x00000000;
253 #else
254  /* Disable all interrupts and clear pending bits */
255  RCC->CIR = 0x009F0000;
256 #endif /* STM32F10X_CL */
257 
258 }
259 
270 void RCC_HSEConfig(uint32_t RCC_HSE)
271 {
272  /* Check the parameters */
273  assert_param(IS_RCC_HSE(RCC_HSE));
274  /* Reset HSEON and HSEBYP bits before configuring the HSE ------------------*/
275  /* Reset HSEON bit */
276  RCC->CR &= CR_HSEON_Reset;
277  /* Reset HSEBYP bit */
278  RCC->CR &= CR_HSEBYP_Reset;
279  /* Configure HSE (RCC_HSE_OFF is already covered by the code section above) */
280  switch(RCC_HSE)
281  {
282  case RCC_HSE_ON:
283  /* Set HSEON bit */
284  RCC->CR |= CR_HSEON_Set;
285  break;
286 
287  case RCC_HSE_Bypass:
288  /* Set HSEBYP and HSEON bits */
289  RCC->CR |= CR_HSEBYP_Set | CR_HSEON_Set;
290  break;
291 
292  default:
293  break;
294  }
295 }
296 
305 {
306  __IO uint32_t StartUpCounter = 0;
307  ErrorStatus status = ERROR;
308  FlagStatus HSEStatus = RESET;
309 
310  /* Wait till HSE is ready and if Time out is reached exit */
311  do
312  {
313  HSEStatus = RCC_GetFlagStatus(RCC_FLAG_HSERDY);
314  StartUpCounter++;
315  } while((StartUpCounter != HSE_STARTUP_TIMEOUT) && (HSEStatus == RESET));
316 
318  {
319  status = SUCCESS;
320  }
321  else
322  {
323  status = ERROR;
324  }
325  return (status);
326 }
327 
334 void RCC_AdjustHSICalibrationValue(uint8_t HSICalibrationValue)
335 {
336  uint32_t tmpreg = 0;
337  /* Check the parameters */
338  assert_param(IS_RCC_CALIBRATION_VALUE(HSICalibrationValue));
339  tmpreg = RCC->CR;
340  /* Clear HSITRIM[4:0] bits */
341  tmpreg &= CR_HSITRIM_Mask;
342  /* Set the HSITRIM[4:0] bits according to HSICalibrationValue value */
343  tmpreg |= (uint32_t)HSICalibrationValue << 3;
344  /* Store the new value */
345  RCC->CR = tmpreg;
346 }
347 
355 {
356  /* Check the parameters */
358  *(__IO uint32_t *) CR_HSION_BB = (uint32_t)NewState;
359 }
360 
378 void RCC_PLLConfig(uint32_t RCC_PLLSource, uint32_t RCC_PLLMul)
379 {
380  uint32_t tmpreg = 0;
381 
382  /* Check the parameters */
383  assert_param(IS_RCC_PLL_SOURCE(RCC_PLLSource));
384  assert_param(IS_RCC_PLL_MUL(RCC_PLLMul));
385 
386  tmpreg = RCC->CFGR;
387  /* Clear PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
388  tmpreg &= CFGR_PLL_Mask;
389  /* Set the PLL configuration bits */
390  tmpreg |= RCC_PLLSource | RCC_PLLMul;
391  /* Store the new value */
392  RCC->CFGR = tmpreg;
393 }
394 
402 {
403  /* Check the parameters */
405 
406  *(__IO uint32_t *) CR_PLLON_BB = (uint32_t)NewState;
407 }
408 
409 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL)
410 
426 void RCC_PREDIV1Config(uint32_t RCC_PREDIV1_Source, uint32_t RCC_PREDIV1_Div)
427 {
428  uint32_t tmpreg = 0;
429 
430  /* Check the parameters */
431  assert_param(IS_RCC_PREDIV1_SOURCE(RCC_PREDIV1_Source));
432  assert_param(IS_RCC_PREDIV1(RCC_PREDIV1_Div));
433 
434  tmpreg = RCC->CFGR2;
435  /* Clear PREDIV1[3:0] and PREDIV1SRC bits */
436  tmpreg &= ~(CFGR2_PREDIV1 | CFGR2_PREDIV1SRC);
437  /* Set the PREDIV1 clock source and division factor */
438  tmpreg |= RCC_PREDIV1_Source | RCC_PREDIV1_Div ;
439  /* Store the new value */
440  RCC->CFGR2 = tmpreg;
441 }
442 #endif
443 
444 #ifdef STM32F10X_CL
445 
454 void RCC_PREDIV2Config(uint32_t RCC_PREDIV2_Div)
455 {
456  uint32_t tmpreg = 0;
457 
458  /* Check the parameters */
459  assert_param(IS_RCC_PREDIV2(RCC_PREDIV2_Div));
460 
461  tmpreg = RCC->CFGR2;
462  /* Clear PREDIV2[3:0] bits */
463  tmpreg &= ~CFGR2_PREDIV2;
464  /* Set the PREDIV2 division factor */
465  tmpreg |= RCC_PREDIV2_Div;
466  /* Store the new value */
467  RCC->CFGR2 = tmpreg;
468 }
469 
479 void RCC_PLL2Config(uint32_t RCC_PLL2Mul)
480 {
481  uint32_t tmpreg = 0;
482 
483  /* Check the parameters */
484  assert_param(IS_RCC_PLL2_MUL(RCC_PLL2Mul));
485 
486  tmpreg = RCC->CFGR2;
487  /* Clear PLL2Mul[3:0] bits */
488  tmpreg &= ~CFGR2_PLL2MUL;
489  /* Set the PLL2 configuration bits */
490  tmpreg |= RCC_PLL2Mul;
491  /* Store the new value */
492  RCC->CFGR2 = tmpreg;
493 }
494 
495 
505 void RCC_PLL2Cmd(FunctionalState NewState)
506 {
507  /* Check the parameters */
509 
510  *(__IO uint32_t *) CR_PLL2ON_BB = (uint32_t)NewState;
511 }
512 
513 
523 void RCC_PLL3Config(uint32_t RCC_PLL3Mul)
524 {
525  uint32_t tmpreg = 0;
526 
527  /* Check the parameters */
528  assert_param(IS_RCC_PLL3_MUL(RCC_PLL3Mul));
529 
530  tmpreg = RCC->CFGR2;
531  /* Clear PLL3Mul[3:0] bits */
532  tmpreg &= ~CFGR2_PLL3MUL;
533  /* Set the PLL3 configuration bits */
534  tmpreg |= RCC_PLL3Mul;
535  /* Store the new value */
536  RCC->CFGR2 = tmpreg;
537 }
538 
539 
546 void RCC_PLL3Cmd(FunctionalState NewState)
547 {
548  /* Check the parameters */
549 
551  *(__IO uint32_t *) CR_PLL3ON_BB = (uint32_t)NewState;
552 }
553 #endif /* STM32F10X_CL */
554 
564 void RCC_SYSCLKConfig(uint32_t RCC_SYSCLKSource)
565 {
566  uint32_t tmpreg = 0;
567  /* Check the parameters */
568  assert_param(IS_RCC_SYSCLK_SOURCE(RCC_SYSCLKSource));
569  tmpreg = RCC->CFGR;
570  /* Clear SW[1:0] bits */
571  tmpreg &= CFGR_SW_Mask;
572  /* Set SW[1:0] bits according to RCC_SYSCLKSource value */
573  tmpreg |= RCC_SYSCLKSource;
574  /* Store the new value */
575  RCC->CFGR = tmpreg;
576 }
577 
587 uint8_t RCC_GetSYSCLKSource(void)
588 {
589  return ((uint8_t)(RCC->CFGR & CFGR_SWS_Mask));
590 }
591 
608 void RCC_HCLKConfig(uint32_t RCC_SYSCLK)
609 {
610  uint32_t tmpreg = 0;
611  /* Check the parameters */
612  assert_param(IS_RCC_HCLK(RCC_SYSCLK));
613  tmpreg = RCC->CFGR;
614  /* Clear HPRE[3:0] bits */
615  tmpreg &= CFGR_HPRE_Reset_Mask;
616  /* Set HPRE[3:0] bits according to RCC_SYSCLK value */
617  tmpreg |= RCC_SYSCLK;
618  /* Store the new value */
619  RCC->CFGR = tmpreg;
620 }
621 
634 void RCC_PCLK1Config(uint32_t RCC_HCLK)
635 {
636  uint32_t tmpreg = 0;
637  /* Check the parameters */
638  assert_param(IS_RCC_PCLK(RCC_HCLK));
639  tmpreg = RCC->CFGR;
640  /* Clear PPRE1[2:0] bits */
641  tmpreg &= CFGR_PPRE1_Reset_Mask;
642  /* Set PPRE1[2:0] bits according to RCC_HCLK value */
643  tmpreg |= RCC_HCLK;
644  /* Store the new value */
645  RCC->CFGR = tmpreg;
646 }
647 
660 void RCC_PCLK2Config(uint32_t RCC_HCLK)
661 {
662  uint32_t tmpreg = 0;
663  /* Check the parameters */
664  assert_param(IS_RCC_PCLK(RCC_HCLK));
665  tmpreg = RCC->CFGR;
666  /* Clear PPRE2[2:0] bits */
667  tmpreg &= CFGR_PPRE2_Reset_Mask;
668  /* Set PPRE2[2:0] bits according to RCC_HCLK value */
669  tmpreg |= RCC_HCLK << 3;
670  /* Store the new value */
671  RCC->CFGR = tmpreg;
672 }
673 
700 void RCC_ITConfig(uint8_t RCC_IT, FunctionalState NewState)
701 {
702  /* Check the parameters */
703  assert_param(IS_RCC_IT(RCC_IT));
705  if (NewState != DISABLE)
706  {
707  /* Perform Byte access to RCC_CIR bits to enable the selected interrupts */
708  *(__IO uint8_t *) CIR_BYTE2_ADDRESS |= RCC_IT;
709  }
710  else
711  {
712  /* Perform Byte access to RCC_CIR bits to disable the selected interrupts */
713  *(__IO uint8_t *) CIR_BYTE2_ADDRESS &= (uint8_t)~RCC_IT;
714  }
715 }
716 
717 #ifndef STM32F10X_CL
718 
728 void RCC_USBCLKConfig(uint32_t RCC_USBCLKSource)
729 {
730  /* Check the parameters */
731  assert_param(IS_RCC_USBCLK_SOURCE(RCC_USBCLKSource));
732 
733  *(__IO uint32_t *) CFGR_USBPRE_BB = RCC_USBCLKSource;
734 }
735 #else
736 
746 void RCC_OTGFSCLKConfig(uint32_t RCC_OTGFSCLKSource)
747 {
748  /* Check the parameters */
749  assert_param(IS_RCC_OTGFSCLK_SOURCE(RCC_OTGFSCLKSource));
750 
751  *(__IO uint32_t *) CFGR_OTGFSPRE_BB = RCC_OTGFSCLKSource;
752 }
753 #endif /* STM32F10X_CL */
754 
766 void RCC_ADCCLKConfig(uint32_t RCC_PCLK2)
767 {
768  uint32_t tmpreg = 0;
769  /* Check the parameters */
770  assert_param(IS_RCC_ADCCLK(RCC_PCLK2));
771  tmpreg = RCC->CFGR;
772  /* Clear ADCPRE[1:0] bits */
773  tmpreg &= CFGR_ADCPRE_Reset_Mask;
774  /* Set ADCPRE[1:0] bits according to RCC_PCLK2 value */
775  tmpreg |= RCC_PCLK2;
776  /* Store the new value */
777  RCC->CFGR = tmpreg;
778 }
779 
780 #ifdef STM32F10X_CL
781 
792 void RCC_I2S2CLKConfig(uint32_t RCC_I2S2CLKSource)
793 {
794  /* Check the parameters */
795  assert_param(IS_RCC_I2S2CLK_SOURCE(RCC_I2S2CLKSource));
796 
797  *(__IO uint32_t *) CFGR2_I2S2SRC_BB = RCC_I2S2CLKSource;
798 }
799 
811 void RCC_I2S3CLKConfig(uint32_t RCC_I2S3CLKSource)
812 {
813  /* Check the parameters */
814  assert_param(IS_RCC_I2S3CLK_SOURCE(RCC_I2S3CLKSource));
815 
816  *(__IO uint32_t *) CFGR2_I2S3SRC_BB = RCC_I2S3CLKSource;
817 }
818 #endif /* STM32F10X_CL */
819 
829 void RCC_LSEConfig(uint8_t RCC_LSE)
830 {
831  /* Check the parameters */
832  assert_param(IS_RCC_LSE(RCC_LSE));
833  /* Reset LSEON and LSEBYP bits before configuring the LSE ------------------*/
834  /* Reset LSEON bit */
835  *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_OFF;
836  /* Reset LSEBYP bit */
837  *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_OFF;
838  /* Configure LSE (RCC_LSE_OFF is already covered by the code section above) */
839  switch(RCC_LSE)
840  {
841  case RCC_LSE_ON:
842  /* Set LSEON bit */
843  *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_ON;
844  break;
845 
846  case RCC_LSE_Bypass:
847  /* Set LSEBYP and LSEON bits */
848  *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_Bypass | RCC_LSE_ON;
849  break;
850 
851  default:
852  break;
853  }
854 }
855 
863 {
864  /* Check the parameters */
866  *(__IO uint32_t *) CSR_LSION_BB = (uint32_t)NewState;
867 }
868 
879 void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource)
880 {
881  /* Check the parameters */
882  assert_param(IS_RCC_RTCCLK_SOURCE(RCC_RTCCLKSource));
883  /* Select the RTC clock source */
884  RCC->BDCR |= RCC_RTCCLKSource;
885 }
886 
894 {
895  /* Check the parameters */
897  *(__IO uint32_t *) BDCR_RTCEN_BB = (uint32_t)NewState;
898 }
899 
909 {
910  uint32_t tmp = 0, pllmull = 0, pllsource = 0, presc = 0;
911 
912 #ifdef STM32F10X_CL
913  uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
914 #endif /* STM32F10X_CL */
915 
916 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
917  uint32_t prediv1factor = 0;
918 #endif
919 
920  /* Get SYSCLK source -------------------------------------------------------*/
921  tmp = RCC->CFGR & CFGR_SWS_Mask;
922 
923  switch (tmp)
924  {
925  case 0x00: /* HSI used as system clock */
926  RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
927  break;
928  case 0x04: /* HSE used as system clock */
929  RCC_Clocks->SYSCLK_Frequency = HSE_VALUE;
930  break;
931  case 0x08: /* PLL used as system clock */
932 
933  /* Get PLL clock source and multiplication factor ----------------------*/
934  pllmull = RCC->CFGR & CFGR_PLLMull_Mask;
935  pllsource = RCC->CFGR & CFGR_PLLSRC_Mask;
936 
937 #ifndef STM32F10X_CL
938  pllmull = ( pllmull >> 18) + 2;
939 
940  if (pllsource == 0x00)
941  {/* HSI oscillator clock divided by 2 selected as PLL clock entry */
942  RCC_Clocks->SYSCLK_Frequency = (HSI_VALUE >> 1) * pllmull;
943  }
944  else
945  {
946  #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
947  prediv1factor = (RCC->CFGR2 & CFGR2_PREDIV1) + 1;
948  /* HSE oscillator clock selected as PREDIV1 clock entry */
949  RCC_Clocks->SYSCLK_Frequency = (HSE_VALUE / prediv1factor) * pllmull;
950  #else
951  /* HSE selected as PLL clock entry */
952  if ((RCC->CFGR & CFGR_PLLXTPRE_Mask) != (uint32_t)RESET)
953  {/* HSE oscillator clock divided by 2 */
954  RCC_Clocks->SYSCLK_Frequency = (HSE_VALUE >> 1) * pllmull;
955  }
956  else
957  {
958  RCC_Clocks->SYSCLK_Frequency = HSE_VALUE * pllmull;
959  }
960  #endif
961  }
962 #else
963  pllmull = pllmull >> 18;
964 
965  if (pllmull != 0x0D)
966  {
967  pllmull += 2;
968  }
969  else
970  { /* PLL multiplication factor = PLL input clock * 6.5 */
971  pllmull = 13 / 2;
972  }
973 
974  if (pllsource == 0x00)
975  {/* HSI oscillator clock divided by 2 selected as PLL clock entry */
976  RCC_Clocks->SYSCLK_Frequency = (HSI_VALUE >> 1) * pllmull;
977  }
978  else
979  {/* PREDIV1 selected as PLL clock entry */
980 
981  /* Get PREDIV1 clock source and division factor */
982  prediv1source = RCC->CFGR2 & CFGR2_PREDIV1SRC;
983  prediv1factor = (RCC->CFGR2 & CFGR2_PREDIV1) + 1;
984 
985  if (prediv1source == 0)
986  { /* HSE oscillator clock selected as PREDIV1 clock entry */
987  RCC_Clocks->SYSCLK_Frequency = (HSE_VALUE / prediv1factor) * pllmull;
988  }
989  else
990  {/* PLL2 clock selected as PREDIV1 clock entry */
991 
992  /* Get PREDIV2 division factor and PLL2 multiplication factor */
993  prediv2factor = ((RCC->CFGR2 & CFGR2_PREDIV2) >> 4) + 1;
994  pll2mull = ((RCC->CFGR2 & CFGR2_PLL2MUL) >> 8 ) + 2;
995  RCC_Clocks->SYSCLK_Frequency = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
996  }
997  }
998 #endif /* STM32F10X_CL */
999  break;
1000 
1001  default:
1002  RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
1003  break;
1004  }
1005 
1006  /* Compute HCLK, PCLK1, PCLK2 and ADCCLK clocks frequencies ----------------*/
1007  /* Get HCLK prescaler */
1008  tmp = RCC->CFGR & CFGR_HPRE_Set_Mask;
1009  tmp = tmp >> 4;
1010  presc = APBAHBPrescTable[tmp];
1011  /* HCLK clock frequency */
1012  RCC_Clocks->HCLK_Frequency = RCC_Clocks->SYSCLK_Frequency >> presc;
1013  /* Get PCLK1 prescaler */
1014  tmp = RCC->CFGR & CFGR_PPRE1_Set_Mask;
1015  tmp = tmp >> 8;
1016  presc = APBAHBPrescTable[tmp];
1017  /* PCLK1 clock frequency */
1018  RCC_Clocks->PCLK1_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
1019  /* Get PCLK2 prescaler */
1020  tmp = RCC->CFGR & CFGR_PPRE2_Set_Mask;
1021  tmp = tmp >> 11;
1022  presc = APBAHBPrescTable[tmp];
1023  /* PCLK2 clock frequency */
1024  RCC_Clocks->PCLK2_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
1025  /* Get ADCCLK prescaler */
1026  tmp = RCC->CFGR & CFGR_ADCPRE_Set_Mask;
1027  tmp = tmp >> 14;
1028  presc = ADCPrescTable[tmp];
1029  /* ADCCLK clock frequency */
1030  RCC_Clocks->ADCCLK_Frequency = RCC_Clocks->PCLK2_Frequency / presc;
1031 }
1032 
1064 void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)
1065 {
1066  /* Check the parameters */
1067  assert_param(IS_RCC_AHB_PERIPH(RCC_AHBPeriph));
1068  assert_param(IS_FUNCTIONAL_STATE(NewState));
1069 
1070  if (NewState != DISABLE)
1071  {
1072  RCC->AHBENR |= RCC_AHBPeriph;
1073  }
1074  else
1075  {
1076  RCC->AHBENR &= ~RCC_AHBPeriph;
1077  }
1078 }
1079 
1095 void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
1096 {
1097  /* Check the parameters */
1098  assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
1099  assert_param(IS_FUNCTIONAL_STATE(NewState));
1100  if (NewState != DISABLE)
1101  {
1102  RCC->APB2ENR |= RCC_APB2Periph;
1103  }
1104  else
1105  {
1106  RCC->APB2ENR &= ~RCC_APB2Periph;
1107  }
1108 }
1109 
1126 void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
1127 {
1128  /* Check the parameters */
1129  assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
1130  assert_param(IS_FUNCTIONAL_STATE(NewState));
1131  if (NewState != DISABLE)
1132  {
1133  RCC->APB1ENR |= RCC_APB1Periph;
1134  }
1135  else
1136  {
1137  RCC->APB1ENR &= ~RCC_APB1Periph;
1138  }
1139 }
1140 
1141 #ifdef STM32F10X_CL
1142 
1153 void RCC_AHBPeriphResetCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)
1154 {
1155  /* Check the parameters */
1156  assert_param(IS_RCC_AHB_PERIPH_RESET(RCC_AHBPeriph));
1157  assert_param(IS_FUNCTIONAL_STATE(NewState));
1158 
1159  if (NewState != DISABLE)
1160  {
1161  RCC->AHBRSTR |= RCC_AHBPeriph;
1162  }
1163  else
1164  {
1165  RCC->AHBRSTR &= ~RCC_AHBPeriph;
1166  }
1167 }
1168 #endif /* STM32F10X_CL */
1169 
1185 void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
1186 {
1187  /* Check the parameters */
1188  assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
1189  assert_param(IS_FUNCTIONAL_STATE(NewState));
1190  if (NewState != DISABLE)
1191  {
1192  RCC->APB2RSTR |= RCC_APB2Periph;
1193  }
1194  else
1195  {
1196  RCC->APB2RSTR &= ~RCC_APB2Periph;
1197  }
1198 }
1199 
1216 void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
1217 {
1218  /* Check the parameters */
1219  assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
1220  assert_param(IS_FUNCTIONAL_STATE(NewState));
1221  if (NewState != DISABLE)
1222  {
1223  RCC->APB1RSTR |= RCC_APB1Periph;
1224  }
1225  else
1226  {
1227  RCC->APB1RSTR &= ~RCC_APB1Periph;
1228  }
1229 }
1230 
1238 {
1239  /* Check the parameters */
1240  assert_param(IS_FUNCTIONAL_STATE(NewState));
1241  *(__IO uint32_t *) BDCR_BDRST_BB = (uint32_t)NewState;
1242 }
1243 
1251 {
1252  /* Check the parameters */
1253  assert_param(IS_FUNCTIONAL_STATE(NewState));
1254  *(__IO uint32_t *) CR_CSSON_BB = (uint32_t)NewState;
1255 }
1256 
1282 void RCC_MCOConfig(uint8_t RCC_MCO)
1283 {
1284  /* Check the parameters */
1285  assert_param(IS_RCC_MCO(RCC_MCO));
1286 
1287  /* Perform Byte access to MCO bits to select the MCO source */
1288  *(__IO uint8_t *) CFGR_BYTE4_ADDRESS = RCC_MCO;
1289 }
1290 
1327 {
1328  uint32_t tmp = 0;
1329  uint32_t statusreg = 0;
1330  FlagStatus bitstatus = RESET;
1331  /* Check the parameters */
1332  assert_param(IS_RCC_FLAG(RCC_FLAG));
1333 
1334  /* Get the RCC register index */
1335  tmp = RCC_FLAG >> 5;
1336  if (tmp == 1) /* The flag to check is in CR register */
1337  {
1338  statusreg = RCC->CR;
1339  }
1340  else if (tmp == 2) /* The flag to check is in BDCR register */
1341  {
1342  statusreg = RCC->BDCR;
1343  }
1344  else /* The flag to check is in CSR register */
1345  {
1346  statusreg = RCC->CSR;
1347  }
1348 
1349  /* Get the flag position */
1350  tmp = RCC_FLAG & FLAG_Mask;
1351  if ((statusreg & ((uint32_t)1 << tmp)) != (uint32_t)RESET)
1352  {
1353  bitstatus = SET;
1354  }
1355  else
1356  {
1357  bitstatus = RESET;
1358  }
1359 
1360  /* Return the flag status */
1361  return bitstatus;
1362 }
1363 
1371 void RCC_ClearFlag(void)
1372 {
1373  /* Set RMVF bit to clear the reset flags */
1374  RCC->CSR |= CSR_RMVF_Set;
1375 }
1376 
1402 ITStatus RCC_GetITStatus(uint8_t RCC_IT)
1403 {
1404  ITStatus bitstatus = RESET;
1405  /* Check the parameters */
1406  assert_param(IS_RCC_GET_IT(RCC_IT));
1407 
1408  /* Check the status of the specified RCC interrupt */
1409  if ((RCC->CIR & RCC_IT) != (uint32_t)RESET)
1410  {
1411  bitstatus = SET;
1412  }
1413  else
1414  {
1415  bitstatus = RESET;
1416  }
1417 
1418  /* Return the RCC_IT status */
1419  return bitstatus;
1420 }
1421 
1448 void RCC_ClearITPendingBit(uint8_t RCC_IT)
1449 {
1450  /* Check the parameters */
1451  assert_param(IS_RCC_CLEAR_IT(RCC_IT));
1452 
1453  /* Perform Byte access to RCC_CIR[23:16] bits to clear the selected interrupt
1454  pending bits */
1455  *(__IO uint8_t *) CIR_BYTE3_ADDRESS = RCC_IT;
1456 }
1457 
1470 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/