69 #include "stm32f10x.h"
110 #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
112 #define SYSCLK_FREQ_24MHz 24000000
119 #define SYSCLK_FREQ_72MHz 72000000
125 #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
132 #define VECT_TAB_OFFSET 0x0
155 #ifdef SYSCLK_FREQ_HSE
157 #elif defined SYSCLK_FREQ_24MHz
159 #elif defined SYSCLK_FREQ_36MHz
161 #elif defined SYSCLK_FREQ_48MHz
163 #elif defined SYSCLK_FREQ_56MHz
165 #elif defined SYSCLK_FREQ_72MHz
168 uint32_t SystemCoreClock = HSI_VALUE;
171 __I uint8_t
AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
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
200 #ifdef DATA_IN_ExtSRAM
201 static void SystemInit_ExtMemCtl(
void);
223 RCC->CR |= (uint32_t)0x00000001;
227 RCC->CFGR &= (uint32_t)0xF8FF0000;
229 RCC->CFGR &= (uint32_t)0xF0FF0000;
233 RCC->CR &= (uint32_t)0xFEF6FFFF;
236 RCC->CR &= (uint32_t)0xFFFBFFFF;
239 RCC->CFGR &= (uint32_t)0xFF80FFFF;
243 RCC->CR &= (uint32_t)0xEBFFFFFF;
246 RCC->CIR = 0x00FF0000;
249 RCC->CFGR2 = 0x00000000;
250 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
252 RCC->CIR = 0x009F0000;
255 RCC->CFGR2 = 0x00000000;
258 RCC->CIR = 0x009F0000;
261 #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
262 #ifdef DATA_IN_ExtSRAM
263 SystemInit_ExtMemCtl();
314 uint32_t tmp = 0, pllmull = 0, pllsource = 0;
317 uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
320 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
321 uint32_t prediv1factor = 0;
325 tmp = RCC->CFGR & RCC_CFGR_SWS;
338 pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
339 pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
342 pllmull = ( pllmull >> 18) + 2;
344 if (pllsource == 0x00)
351 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
352 prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
357 if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
368 pllmull = pllmull >> 18;
379 if (pllsource == 0x00)
388 prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
389 prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
391 if (prediv1source == 0)
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;
426 #ifdef SYSCLK_FREQ_HSE
428 #elif defined SYSCLK_FREQ_24MHz
430 #elif defined SYSCLK_FREQ_36MHz
432 #elif defined SYSCLK_FREQ_48MHz
434 #elif defined SYSCLK_FREQ_56MHz
436 #elif defined SYSCLK_FREQ_72MHz
449 #ifdef DATA_IN_ExtSRAM
458 void SystemInit_ExtMemCtl(
void)
464 RCC->AHBENR = 0x00000114;
467 RCC->APB2ENR = 0x000001E0;
475 GPIOD->CRL = 0x44BB44BB;
476 GPIOD->CRH = 0xBBBBBBBB;
478 GPIOE->CRL = 0xB44444BB;
479 GPIOE->CRH = 0xBBBBBBBB;
481 GPIOF->CRL = 0x44BBBBBB;
482 GPIOF->CRH = 0xBBBB4444;
484 GPIOG->CRL = 0x44BBBBBB;
485 GPIOG->CRH = 0x44444B44;
490 FSMC_Bank1->BTCR[4] = 0x00001011;
491 FSMC_Bank1->BTCR[5] = 0x00000200;
495 #ifdef SYSCLK_FREQ_HSE
502 static void SetSysClockToHSE(
void)
504 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
508 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
513 HSEStatus = RCC->CR & RCC_CR_HSERDY;
515 }
while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
517 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
519 HSEStatus = (uint32_t)0x01;
523 HSEStatus = (uint32_t)0x00;
526 if (HSEStatus == (uint32_t)0x01)
529 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
531 FLASH->ACR |= FLASH_ACR_PRFTBE;
534 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
537 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
539 if (HSE_VALUE <= 24000000)
541 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
545 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
551 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
554 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
557 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
560 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
561 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;
564 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)
578 #elif defined SYSCLK_FREQ_24MHz
585 static void SetSysClockTo24(
void)
587 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
591 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
596 HSEStatus = RCC->CR & RCC_CR_HSERDY;
598 }
while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
600 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
602 HSEStatus = (uint32_t)0x01;
606 HSEStatus = (uint32_t)0x00;
609 if (HSEStatus == (uint32_t)0x01)
611 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
613 FLASH->ACR |= FLASH_ACR_PRFTBE;
616 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
617 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
621 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
624 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
627 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
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 |
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);
644 RCC->CR |= RCC_CR_PLL2ON;
646 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
649 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
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);
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);
660 RCC->CR |= RCC_CR_PLLON;
663 while((RCC->CR & RCC_CR_PLLRDY) == 0)
668 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
669 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
672 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
686 #elif defined SYSCLK_FREQ_36MHz
693 static void SetSysClockTo36(
void)
695 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
699 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
704 HSEStatus = RCC->CR & RCC_CR_HSERDY;
706 }
while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
708 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
710 HSEStatus = (uint32_t)0x01;
714 HSEStatus = (uint32_t)0x00;
717 if (HSEStatus == (uint32_t)0x01)
720 FLASH->ACR |= FLASH_ACR_PRFTBE;
723 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
724 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
727 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
730 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
733 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
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 |
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);
752 RCC->CR |= RCC_CR_PLL2ON;
754 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
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);
765 RCC->CR |= RCC_CR_PLLON;
768 while((RCC->CR & RCC_CR_PLLRDY) == 0)
773 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
774 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
777 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
791 #elif defined SYSCLK_FREQ_48MHz
798 static void SetSysClockTo48(
void)
800 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
804 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
809 HSEStatus = RCC->CR & RCC_CR_HSERDY;
811 }
while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
813 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
815 HSEStatus = (uint32_t)0x01;
819 HSEStatus = (uint32_t)0x00;
822 if (HSEStatus == (uint32_t)0x01)
825 FLASH->ACR |= FLASH_ACR_PRFTBE;
828 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
829 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
832 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
835 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
838 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
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);
851 RCC->CR |= RCC_CR_PLL2ON;
853 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
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 |
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);
869 RCC->CR |= RCC_CR_PLLON;
872 while((RCC->CR & RCC_CR_PLLRDY) == 0)
877 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
878 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
881 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
896 #elif defined SYSCLK_FREQ_56MHz
903 static void SetSysClockTo56(
void)
905 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
909 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
914 HSEStatus = RCC->CR & RCC_CR_HSERDY;
916 }
while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
918 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
920 HSEStatus = (uint32_t)0x01;
924 HSEStatus = (uint32_t)0x00;
927 if (HSEStatus == (uint32_t)0x01)
930 FLASH->ACR |= FLASH_ACR_PRFTBE;
933 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
934 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
937 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
940 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
943 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
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);
956 RCC->CR |= RCC_CR_PLL2ON;
958 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
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 |
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);
975 RCC->CR |= RCC_CR_PLLON;
978 while((RCC->CR & RCC_CR_PLLRDY) == 0)
983 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
984 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
987 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
1002 #elif defined SYSCLK_FREQ_72MHz
1011 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
1015 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
1020 HSEStatus = RCC->CR & RCC_CR_HSERDY;
1022 }
while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
1024 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
1026 HSEStatus = (uint32_t)0x01;
1030 HSEStatus = (uint32_t)0x00;
1033 if (HSEStatus == (uint32_t)0x01)
1036 FLASH->ACR |= FLASH_ACR_PRFTBE;
1039 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
1040 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
1044 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
1047 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
1050 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
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);
1063 RCC->CR |= RCC_CR_PLL2ON;
1065 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
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 |
1076 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
1078 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
1082 RCC->CR |= RCC_CR_PLLON;
1085 while((RCC->CR & RCC_CR_PLLRDY) == 0)
1090 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
1091 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
1094 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
#define SYSCLK_FREQ_72MHz
void SystemInit(void)
Setup the microcontroller system Initialize the Embedded Flash Interface, the PLL and update the Syst...
__I uint8_t AHBPrescTable[16]
static void SetSysClockTo72(void)
Setup the external memory controller. Called in startup_stm32f10x.s before jump to __main...
static void SetSysClock(void)
Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.