dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cmsis_system.c
Go to the documentation of this file.
1 
69 #include "stm32f10x.h"
70 
110 #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
111 /* #define SYSCLK_FREQ_HSE HSE_VALUE */
112  #define SYSCLK_FREQ_24MHz 24000000
113 #else
114 /* #define SYSCLK_FREQ_HSE HSE_VALUE */
115 /* #define SYSCLK_FREQ_24MHz 24000000 */
116 /* #define SYSCLK_FREQ_36MHz 36000000 */
117 /* #define SYSCLK_FREQ_48MHz 48000000 */
118 /* #define SYSCLK_FREQ_56MHz 56000000 */
119 #define SYSCLK_FREQ_72MHz 72000000
120 #endif
121 
125 #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
126 /* #define DATA_IN_ExtSRAM */
127 #endif
128 
131 /* #define VECT_TAB_SRAM */
132 #define VECT_TAB_OFFSET 0x0
152 /*******************************************************************************
153 * Clock Definitions
154 *******************************************************************************/
155 #ifdef SYSCLK_FREQ_HSE
156  uint32_t SystemCoreClock = SYSCLK_FREQ_HSE;
157 #elif defined SYSCLK_FREQ_24MHz
158  uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz;
159 #elif defined SYSCLK_FREQ_36MHz
160  uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz;
161 #elif defined SYSCLK_FREQ_48MHz
162  uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz;
163 #elif defined SYSCLK_FREQ_56MHz
164  uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz;
165 #elif defined SYSCLK_FREQ_72MHz
167 #else
168  uint32_t SystemCoreClock = HSI_VALUE;
169 #endif
170 
171 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
180 static void SetSysClock(void);
181 
182 // this is required for the system_rcc code because
183 // of the clock rate switching in naze32
184 uint32_t hse_value = 8000000;
186 #ifdef SYSCLK_FREQ_HSE
187  static void SetSysClockToHSE(void);
188 #elif defined SYSCLK_FREQ_24MHz
189  static void SetSysClockTo24(void);
190 #elif defined SYSCLK_FREQ_36MHz
191  static void SetSysClockTo36(void);
192 #elif defined SYSCLK_FREQ_48MHz
193  static void SetSysClockTo48(void);
194 #elif defined SYSCLK_FREQ_56MHz
195  static void SetSysClockTo56(void);
196 #elif defined SYSCLK_FREQ_72MHz
197  static void SetSysClockTo72(void);
198 #endif
199 
200 #ifdef DATA_IN_ExtSRAM
201  static void SystemInit_ExtMemCtl(void);
202 #endif /* DATA_IN_ExtSRAM */
203 
219 void SystemInit (void)
220 {
221  /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
222  /* Set HSION bit */
223  RCC->CR |= (uint32_t)0x00000001;
224 
225  /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
226 #ifndef STM32F10X_CL
227  RCC->CFGR &= (uint32_t)0xF8FF0000;
228 #else
229  RCC->CFGR &= (uint32_t)0xF0FF0000;
230 #endif /* STM32F10X_CL */
231 
232  /* Reset HSEON, CSSON and PLLON bits */
233  RCC->CR &= (uint32_t)0xFEF6FFFF;
234 
235  /* Reset HSEBYP bit */
236  RCC->CR &= (uint32_t)0xFFFBFFFF;
237 
238  /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
239  RCC->CFGR &= (uint32_t)0xFF80FFFF;
240 
241 #ifdef STM32F10X_CL
242  /* Reset PLL2ON and PLL3ON bits */
243  RCC->CR &= (uint32_t)0xEBFFFFFF;
244 
245  /* Disable all interrupts and clear pending bits */
246  RCC->CIR = 0x00FF0000;
247 
248  /* Reset CFGR2 register */
249  RCC->CFGR2 = 0x00000000;
250 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
251  /* Disable all interrupts and clear pending bits */
252  RCC->CIR = 0x009F0000;
253 
254  /* Reset CFGR2 register */
255  RCC->CFGR2 = 0x00000000;
256 #else
257  /* Disable all interrupts and clear pending bits */
258  RCC->CIR = 0x009F0000;
259 #endif /* STM32F10X_CL */
260 
261 #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
262  #ifdef DATA_IN_ExtSRAM
263  SystemInit_ExtMemCtl();
264  #endif /* DATA_IN_ExtSRAM */
265 #endif
266 
267  /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
268  /* Configure the Flash Latency cycles and enable prefetch buffer */
269  SetSysClock();
270 
271 #ifdef VECT_TAB_SRAM
272  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
273 #else
274  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
275 #endif
276 }
277 
312 void SystemCoreClockUpdate (void)
313 {
314  uint32_t tmp = 0, pllmull = 0, pllsource = 0;
315 
316 #ifdef STM32F10X_CL
317  uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
318 #endif /* STM32F10X_CL */
319 
320 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
321  uint32_t prediv1factor = 0;
322 #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL or STM32F10X_HD_VL */
323 
324  /* Get SYSCLK source -------------------------------------------------------*/
325  tmp = RCC->CFGR & RCC_CFGR_SWS;
326 
327  switch (tmp)
328  {
329  case 0x00: /* HSI used as system clock */
330  SystemCoreClock = HSI_VALUE;
331  break;
332  case 0x04: /* HSE used as system clock */
333  SystemCoreClock = HSE_VALUE;
334  break;
335  case 0x08: /* PLL used as system clock */
336 
337  /* Get PLL clock source and multiplication factor ----------------------*/
338  pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
339  pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
340 
341 #ifndef STM32F10X_CL
342  pllmull = ( pllmull >> 18) + 2;
343 
344  if (pllsource == 0x00)
345  {
346  /* HSI oscillator clock divided by 2 selected as PLL clock entry */
347  SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
348  }
349  else
350  {
351  #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
352  prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
353  /* HSE oscillator clock selected as PREDIV1 clock entry */
354  SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
355  #else
356  /* HSE selected as PLL clock entry */
357  if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
358  {/* HSE oscillator clock divided by 2 */
359  SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
360  }
361  else
362  {
363  SystemCoreClock = HSE_VALUE * pllmull;
364  }
365  #endif
366  }
367 #else
368  pllmull = pllmull >> 18;
369 
370  if (pllmull != 0x0D)
371  {
372  pllmull += 2;
373  }
374  else
375  { /* PLL multiplication factor = PLL input clock * 6.5 */
376  pllmull = 13 / 2;
377  }
378 
379  if (pllsource == 0x00)
380  {
381  /* HSI oscillator clock divided by 2 selected as PLL clock entry */
382  SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
383  }
384  else
385  {/* PREDIV1 selected as PLL clock entry */
386 
387  /* Get PREDIV1 clock source and division factor */
388  prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
389  prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
390 
391  if (prediv1source == 0)
392  {
393  /* HSE oscillator clock selected as PREDIV1 clock entry */
394  SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
395  }
396  else
397  {/* PLL2 clock selected as PREDIV1 clock entry */
398 
399  /* Get PREDIV2 division factor and PLL2 multiplication factor */
400  prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;
401  pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2;
402  SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
403  }
404  }
405 #endif /* STM32F10X_CL */
406  break;
407 
408  default:
409  SystemCoreClock = HSI_VALUE;
410  break;
411  }
412 
413  /* Compute HCLK clock frequency ----------------*/
414  /* Get HCLK prescaler */
415  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
416  /* HCLK clock frequency */
417  SystemCoreClock >>= tmp;
418 }
419 
424 static void SetSysClock(void)
425 {
426 #ifdef SYSCLK_FREQ_HSE
427  SetSysClockToHSE();
428 #elif defined SYSCLK_FREQ_24MHz
429  SetSysClockTo24();
430 #elif defined SYSCLK_FREQ_36MHz
431  SetSysClockTo36();
432 #elif defined SYSCLK_FREQ_48MHz
433  SetSysClockTo48();
434 #elif defined SYSCLK_FREQ_56MHz
435  SetSysClockTo56();
436 #elif defined SYSCLK_FREQ_72MHz
437  SetSysClockTo72();
438 #endif
439 
440  /* If none of the define above is enabled, the HSI is used as System clock
441  source (default after reset) */
442 }
443 
449 #ifdef DATA_IN_ExtSRAM
450 
458 void SystemInit_ExtMemCtl(void)
459 {
463  /* Enable FSMC clock */
464  RCC->AHBENR = 0x00000114;
465 
466  /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
467  RCC->APB2ENR = 0x000001E0;
468 
469 /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
470 /*---------------- SRAM Address lines configuration -------------------------*/
471 /*---------------- NOE and NWE configuration --------------------------------*/
472 /*---------------- NE3 configuration ----------------------------------------*/
473 /*---------------- NBL0, NBL1 configuration ---------------------------------*/
474 
475  GPIOD->CRL = 0x44BB44BB;
476  GPIOD->CRH = 0xBBBBBBBB;
477 
478  GPIOE->CRL = 0xB44444BB;
479  GPIOE->CRH = 0xBBBBBBBB;
480 
481  GPIOF->CRL = 0x44BBBBBB;
482  GPIOF->CRH = 0xBBBB4444;
483 
484  GPIOG->CRL = 0x44BBBBBB;
485  GPIOG->CRH = 0x44444B44;
486 
487 /*---------------- FSMC Configuration ---------------------------------------*/
488 /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
489 
490  FSMC_Bank1->BTCR[4] = 0x00001011;
491  FSMC_Bank1->BTCR[5] = 0x00000200;
492 }
493 #endif /* DATA_IN_ExtSRAM */
494 
495 #ifdef SYSCLK_FREQ_HSE
496 
502 static void SetSysClockToHSE(void)
503 {
504  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
505 
506  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
507  /* Enable HSE */
508  RCC->CR |= ((uint32_t)RCC_CR_HSEON);
509 
510  /* Wait till HSE is ready and if Time out is reached exit */
511  do
512  {
513  HSEStatus = RCC->CR & RCC_CR_HSERDY;
514  StartUpCounter++;
515  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
516 
517  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
518  {
519  HSEStatus = (uint32_t)0x01;
520  }
521  else
522  {
523  HSEStatus = (uint32_t)0x00;
524  }
525 
526  if (HSEStatus == (uint32_t)0x01)
527  {
528 
529 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
530  /* Enable Prefetch Buffer */
531  FLASH->ACR |= FLASH_ACR_PRFTBE;
532 
533  /* Flash 0 wait state */
534  FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
535 
536 #ifndef STM32F10X_CL
537  FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
538 #else
539  if (HSE_VALUE <= 24000000)
540  {
541  FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
542  }
543  else
544  {
545  FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
546  }
547 #endif /* STM32F10X_CL */
548 #endif
549 
550  /* HCLK = SYSCLK */
551  RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
552 
553  /* PCLK2 = HCLK */
554  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
555 
556  /* PCLK1 = HCLK */
557  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
558 
559  /* Select HSE as system clock source */
560  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
561  RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;
562 
563  /* Wait till HSE is used as system clock source */
564  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)
565  {
566  }
567  }
568  else
569  { /* If HSE fails to start-up, the application will have wrong clock
570  configuration. User can add here some code to deal with this error */
571 
572  /* Go to infinite loop */
573  while (1)
574  {
575  }
576  }
577 }
578 #elif defined SYSCLK_FREQ_24MHz
579 
585 static void SetSysClockTo24(void)
586 {
587  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
588 
589  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
590  /* Enable HSE */
591  RCC->CR |= ((uint32_t)RCC_CR_HSEON);
592 
593  /* Wait till HSE is ready and if Time out is reached exit */
594  do
595  {
596  HSEStatus = RCC->CR & RCC_CR_HSERDY;
597  StartUpCounter++;
598  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
599 
600  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
601  {
602  HSEStatus = (uint32_t)0x01;
603  }
604  else
605  {
606  HSEStatus = (uint32_t)0x00;
607  }
608 
609  if (HSEStatus == (uint32_t)0x01)
610  {
611 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
612  /* Enable Prefetch Buffer */
613  FLASH->ACR |= FLASH_ACR_PRFTBE;
614 
615  /* Flash 0 wait state */
616  FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
617  FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
618 #endif
619 
620  /* HCLK = SYSCLK */
621  RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
622 
623  /* PCLK2 = HCLK */
624  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
625 
626  /* PCLK1 = HCLK */
627  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
628 
629 #ifdef STM32F10X_CL
630  /* Configure PLLs ------------------------------------------------------*/
631  /* PLL configuration: PLLCLK = PREDIV1 * 6 = 24 MHz */
632  RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
633  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
634  RCC_CFGR_PLLMULL6);
635 
636  /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
637  /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
638  RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
639  RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
640  RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
641  RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
642 
643  /* Enable PLL2 */
644  RCC->CR |= RCC_CR_PLL2ON;
645  /* Wait till PLL2 is ready */
646  while((RCC->CR & RCC_CR_PLL2RDY) == 0)
647  {
648  }
649 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
650  /* PLL configuration: = (HSE / 2) * 6 = 24 MHz */
651  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
652  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1_Div2 | RCC_CFGR_PLLMULL6);
653 #else
654  /* PLL configuration: = (HSE / 2) * 6 = 24 MHz */
655  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
656  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
657 #endif /* STM32F10X_CL */
658 
659  /* Enable PLL */
660  RCC->CR |= RCC_CR_PLLON;
661 
662  /* Wait till PLL is ready */
663  while((RCC->CR & RCC_CR_PLLRDY) == 0)
664  {
665  }
666 
667  /* Select PLL as system clock source */
668  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
669  RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
670 
671  /* Wait till PLL is used as system clock source */
672  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
673  {
674  }
675  }
676  else
677  { /* If HSE fails to start-up, the application will have wrong clock
678  configuration. User can add here some code to deal with this error */
679 
680  /* Go to infinite loop */
681  while (1)
682  {
683  }
684  }
685 }
686 #elif defined SYSCLK_FREQ_36MHz
687 
693 static void SetSysClockTo36(void)
694 {
695  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
696 
697  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
698  /* Enable HSE */
699  RCC->CR |= ((uint32_t)RCC_CR_HSEON);
700 
701  /* Wait till HSE is ready and if Time out is reached exit */
702  do
703  {
704  HSEStatus = RCC->CR & RCC_CR_HSERDY;
705  StartUpCounter++;
706  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
707 
708  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
709  {
710  HSEStatus = (uint32_t)0x01;
711  }
712  else
713  {
714  HSEStatus = (uint32_t)0x00;
715  }
716 
717  if (HSEStatus == (uint32_t)0x01)
718  {
719  /* Enable Prefetch Buffer */
720  FLASH->ACR |= FLASH_ACR_PRFTBE;
721 
722  /* Flash 1 wait state */
723  FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
724  FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
725 
726  /* HCLK = SYSCLK */
727  RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
728 
729  /* PCLK2 = HCLK */
730  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
731 
732  /* PCLK1 = HCLK */
733  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
734 
735 #ifdef STM32F10X_CL
736  /* Configure PLLs ------------------------------------------------------*/
737 
738  /* PLL configuration: PLLCLK = PREDIV1 * 9 = 36 MHz */
739  RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
740  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
741  RCC_CFGR_PLLMULL9);
742 
744  /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
745 
746  RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
747  RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
748  RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
749  RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
750 
751  /* Enable PLL2 */
752  RCC->CR |= RCC_CR_PLL2ON;
753  /* Wait till PLL2 is ready */
754  while((RCC->CR & RCC_CR_PLL2RDY) == 0)
755  {
756  }
757 
758 #else
759  /* PLL configuration: PLLCLK = (HSE / 2) * 9 = 36 MHz */
760  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
761  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL9);
762 #endif /* STM32F10X_CL */
763 
764  /* Enable PLL */
765  RCC->CR |= RCC_CR_PLLON;
766 
767  /* Wait till PLL is ready */
768  while((RCC->CR & RCC_CR_PLLRDY) == 0)
769  {
770  }
771 
772  /* Select PLL as system clock source */
773  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
774  RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
775 
776  /* Wait till PLL is used as system clock source */
777  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
778  {
779  }
780  }
781  else
782  { /* If HSE fails to start-up, the application will have wrong clock
783  configuration. User can add here some code to deal with this error */
784 
785  /* Go to infinite loop */
786  while (1)
787  {
788  }
789  }
790 }
791 #elif defined SYSCLK_FREQ_48MHz
792 
798 static void SetSysClockTo48(void)
799 {
800  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
801 
802  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
803  /* Enable HSE */
804  RCC->CR |= ((uint32_t)RCC_CR_HSEON);
805 
806  /* Wait till HSE is ready and if Time out is reached exit */
807  do
808  {
809  HSEStatus = RCC->CR & RCC_CR_HSERDY;
810  StartUpCounter++;
811  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
812 
813  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
814  {
815  HSEStatus = (uint32_t)0x01;
816  }
817  else
818  {
819  HSEStatus = (uint32_t)0x00;
820  }
821 
822  if (HSEStatus == (uint32_t)0x01)
823  {
824  /* Enable Prefetch Buffer */
825  FLASH->ACR |= FLASH_ACR_PRFTBE;
826 
827  /* Flash 1 wait state */
828  FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
829  FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
830 
831  /* HCLK = SYSCLK */
832  RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
833 
834  /* PCLK2 = HCLK */
835  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
836 
837  /* PCLK1 = HCLK */
838  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
839 
840 #ifdef STM32F10X_CL
841  /* Configure PLLs ------------------------------------------------------*/
842  /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
843  /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
844 
845  RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
846  RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
847  RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
848  RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
849 
850  /* Enable PLL2 */
851  RCC->CR |= RCC_CR_PLL2ON;
852  /* Wait till PLL2 is ready */
853  while((RCC->CR & RCC_CR_PLL2RDY) == 0)
854  {
855  }
856 
857 
858  /* PLL configuration: PLLCLK = PREDIV1 * 6 = 48 MHz */
859  RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
860  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
861  RCC_CFGR_PLLMULL6);
862 #else
863  /* PLL configuration: PLLCLK = HSE * 6 = 48 MHz */
864  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
865  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
866 #endif /* STM32F10X_CL */
867 
868  /* Enable PLL */
869  RCC->CR |= RCC_CR_PLLON;
870 
871  /* Wait till PLL is ready */
872  while((RCC->CR & RCC_CR_PLLRDY) == 0)
873  {
874  }
875 
876  /* Select PLL as system clock source */
877  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
878  RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
879 
880  /* Wait till PLL is used as system clock source */
881  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
882  {
883  }
884  }
885  else
886  { /* If HSE fails to start-up, the application will have wrong clock
887  configuration. User can add here some code to deal with this error */
888 
889  /* Go to infinite loop */
890  while (1)
891  {
892  }
893  }
894 }
895 
896 #elif defined SYSCLK_FREQ_56MHz
897 
903 static void SetSysClockTo56(void)
904 {
905  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
906 
907  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
908  /* Enable HSE */
909  RCC->CR |= ((uint32_t)RCC_CR_HSEON);
910 
911  /* Wait till HSE is ready and if Time out is reached exit */
912  do
913  {
914  HSEStatus = RCC->CR & RCC_CR_HSERDY;
915  StartUpCounter++;
916  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
917 
918  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
919  {
920  HSEStatus = (uint32_t)0x01;
921  }
922  else
923  {
924  HSEStatus = (uint32_t)0x00;
925  }
926 
927  if (HSEStatus == (uint32_t)0x01)
928  {
929  /* Enable Prefetch Buffer */
930  FLASH->ACR |= FLASH_ACR_PRFTBE;
931 
932  /* Flash 2 wait state */
933  FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
934  FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
935 
936  /* HCLK = SYSCLK */
937  RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
938 
939  /* PCLK2 = HCLK */
940  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
941 
942  /* PCLK1 = HCLK */
943  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
944 
945 #ifdef STM32F10X_CL
946  /* Configure PLLs ------------------------------------------------------*/
947  /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
948  /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
949 
950  RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
951  RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
952  RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
953  RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
954 
955  /* Enable PLL2 */
956  RCC->CR |= RCC_CR_PLL2ON;
957  /* Wait till PLL2 is ready */
958  while((RCC->CR & RCC_CR_PLL2RDY) == 0)
959  {
960  }
961 
962 
963  /* PLL configuration: PLLCLK = PREDIV1 * 7 = 56 MHz */
964  RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
965  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
966  RCC_CFGR_PLLMULL7);
967 #else
968  /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */
969  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
970  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7);
971 
972 #endif /* STM32F10X_CL */
973 
974  /* Enable PLL */
975  RCC->CR |= RCC_CR_PLLON;
976 
977  /* Wait till PLL is ready */
978  while((RCC->CR & RCC_CR_PLLRDY) == 0)
979  {
980  }
981 
982  /* Select PLL as system clock source */
983  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
984  RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
985 
986  /* Wait till PLL is used as system clock source */
987  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
988  {
989  }
990  }
991  else
992  { /* If HSE fails to start-up, the application will have wrong clock
993  configuration. User can add here some code to deal with this error */
994 
995  /* Go to infinite loop */
996  while (1)
997  {
998  }
999  }
1000 }
1001 
1002 #elif defined SYSCLK_FREQ_72MHz
1003 
1009 static void SetSysClockTo72(void)
1011  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
1012 
1013  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
1014  /* Enable HSE */
1015  RCC->CR |= ((uint32_t)RCC_CR_HSEON);
1016 
1017  /* Wait till HSE is ready and if Time out is reached exit */
1018  do
1019  {
1020  HSEStatus = RCC->CR & RCC_CR_HSERDY;
1021  StartUpCounter++;
1022  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
1023 
1024  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
1025  {
1026  HSEStatus = (uint32_t)0x01;
1027  }
1028  else
1029  {
1030  HSEStatus = (uint32_t)0x00;
1031  }
1032 
1033  if (HSEStatus == (uint32_t)0x01)
1034  {
1035  /* Enable Prefetch Buffer */
1036  FLASH->ACR |= FLASH_ACR_PRFTBE;
1037 
1038  /* Flash 2 wait state */
1039  FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
1040  FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
1041 
1042 
1043  /* HCLK = SYSCLK */
1044  RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
1045 
1046  /* PCLK2 = HCLK */
1047  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
1048 
1049  /* PCLK1 = HCLK */
1050  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
1051 
1052 #ifdef STM32F10X_CL
1053  /* Configure PLLs ------------------------------------------------------*/
1054  /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
1055  /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
1056 
1057  RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
1058  RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
1059  RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
1060  RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
1061 
1062  /* Enable PLL2 */
1063  RCC->CR |= RCC_CR_PLL2ON;
1064  /* Wait till PLL2 is ready */
1065  while((RCC->CR & RCC_CR_PLL2RDY) == 0)
1066  {
1067  }
1068 
1069 
1070  /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */
1071  RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
1072  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
1073  RCC_CFGR_PLLMULL9);
1074 #else
1075  /* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
1076  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
1077  RCC_CFGR_PLLMULL));
1078  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
1079 #endif /* STM32F10X_CL */
1080 
1081  /* Enable PLL */
1082  RCC->CR |= RCC_CR_PLLON;
1083 
1084  /* Wait till PLL is ready */
1085  while((RCC->CR & RCC_CR_PLLRDY) == 0)
1086  {
1087  }
1088 
1089  /* Select PLL as system clock source */
1090  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
1091  RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
1092 
1093  /* Wait till PLL is used as system clock source */
1094  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
1095  {
1096  }
1097  }
1098  else
1099  { /* If HSE fails to start-up, the application will have wrong clock
1100  configuration. User can add here some code to deal with this error */
1101 
1102  /* Go to infinite loop */
1103  while (1)
1104  {
1105  }
1106  }
1107 }
1108 #endif
1109 
1126 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
uint32_t hse_value
Definition: cmsis_system.c:185
#define VECT_TAB_OFFSET
Definition: cmsis_system.c:132
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
Definition: cmsis_system.c:289
#define SYSCLK_FREQ_72MHz
Definition: cmsis_system.c:119
void SystemInit(void)
Setup the microcontroller system Initialize the Embedded Flash Interface, the PLL and update the Syst...
Definition: cmsis_system.c:213
__I uint8_t AHBPrescTable[16]
Definition: cmsis_system.c:184
static void SetSysClockTo72(void)
Setup the external memory controller. Called in startup_stm32f10x.s before jump to __main...
uint32_t SystemCoreClock
Definition: cmsis_system.c:182
static void SetSysClock(void)
Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
Definition: cmsis_system.c:425