128 #include "stm32f4xx.h"
149 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx)
153 #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
157 #if defined(STM32F411xE)
167 #if defined(USE_HSE_BYPASS)
168 #define HSE_BYPASS_INPUT_FREQUENCY 8000000
175 #define VECT_TAB_OFFSET 0x00
180 #if SYSCLK_FREQ == 180000000
181 #if defined(STM32F40_41xxx)
182 #error 180MHz is not supported!
194 #elif SYSCLK_FREQ == 168000000
201 #error Invalid SYSCLK_FREQ
222 __I uint8_t
AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
234 #if defined(DATA_IN_ExtSRAM) || defined(DATA_IN_ExtSDRAM)
235 static void SystemInit_ExtMemCtl(
void);
255 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
256 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));
260 RCC->CR |= (uint32_t)0x00000001;
263 RCC->CFGR = 0x00000000;
266 RCC->CR &= (uint32_t)0xFEF6FFFF;
269 RCC->PLLCFGR = 0x24003010;
272 RCC->CR &= (uint32_t)0xFFFBFFFF;
275 RCC->CIR = 0x00000000;
277 #if defined(DATA_IN_ExtSRAM) || defined(DATA_IN_ExtSDRAM)
278 SystemInit_ExtMemCtl();
330 uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
331 #if defined(STM32F446xx)
335 tmp = RCC->CFGR & RCC_CFGR_SWS;
349 pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
350 pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
352 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx) || defined(STM32F446xx)
356 pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
361 pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
363 #elif defined(STM32F411xE)
364 #if defined(USE_HSE_BYPASS)
368 pllvco = (HSE_BYPASS_INPUT_FREQUENCY / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
374 pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
378 pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
381 #if defined(STM32F446xx)
386 pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
387 pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
391 pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
396 pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
399 pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >>28) + 1 ) *2;
423 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx) || defined(STM32F446xx)
427 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
430 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
435 HSEStatus = RCC->CR & RCC_CR_HSERDY;
437 }
while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
439 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
441 HSEStatus = (uint32_t)0x01;
445 HSEStatus = (uint32_t)0x00;
448 if (HSEStatus == (uint32_t)0x01)
451 RCC->APB1ENR |= RCC_APB1ENR_PWREN;
452 PWR->CR |= PWR_CR_VOS;
455 RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
457 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
459 RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
462 RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
465 #if defined(STM32F401xx)
467 RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
470 RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
473 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx)
475 RCC->PLLCFGR =
PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
476 (RCC_PLLCFGR_PLLSRC_HSE) | (
PLL_Q << 24);
479 #if defined(STM32F446xx)
481 RCC->PLLCFGR =
PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
482 (RCC_PLLCFGR_PLLSRC_HSE) | (
PLL_Q << 24) | (PLL_R << 28);
486 RCC->CR |= RCC_CR_PLLON;
489 while((RCC->CR & RCC_CR_PLLRDY) == 0);
491 #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
493 PWR->CR |= PWR_CR_ODEN;
494 while((PWR->CSR & PWR_CSR_ODRDY) == 0);
496 PWR->CR |= PWR_CR_ODSWEN;
497 while((PWR->CSR & PWR_CSR_ODSWRDY) == 0);
500 FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
503 #if defined(STM32F40_41xxx)
505 FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
508 #if defined(STM32F401xx)
510 FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
514 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
515 RCC->CFGR |= RCC_CFGR_SW_PLL;
518 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
524 #elif defined(STM32F411xE)
525 #if defined(USE_HSE_BYPASS)
529 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
532 RCC->CR |= ((uint32_t)RCC_CR_HSEON | RCC_CR_HSEBYP);
537 HSEStatus = RCC->CR & RCC_CR_HSERDY;
539 }
while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
541 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
543 HSEStatus = (uint32_t)0x01;
547 HSEStatus = (uint32_t)0x00;
550 if (HSEStatus == (uint32_t)0x01)
553 RCC->APB1ENR |= RCC_APB1ENR_PWREN;
554 PWR->CR |= PWR_CR_VOS;
557 RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
560 RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
563 RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
566 RCC->PLLCFGR =
PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
567 (RCC_PLLCFGR_PLLSRC_HSE) | (
PLL_Q << 24);
570 RCC->CR |= RCC_CR_PLLON;
573 while((RCC->CR & RCC_CR_PLLRDY) == 0)
578 FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
581 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
582 RCC->CFGR |= RCC_CFGR_SW_PLL;
585 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
595 RCC->APB1ENR |= RCC_APB1ENR_PWREN;
596 PWR->CR |= PWR_CR_VOS;
599 RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
602 RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
605 RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
608 RCC->PLLCFGR =
PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) | (
PLL_Q << 24);
611 RCC->CR |= RCC_CR_PLLON;
614 while((RCC->CR & RCC_CR_PLLRDY) == 0)
619 FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
622 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
623 RCC->CFGR |= RCC_CFGR_SW_PLL;
626 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
632 #if defined(STM32F446xx)
633 #if SYSCLK_FREQ == 180000000
636 RCC_48MHzClockSourceConfig(RCC_48MHZCLKSource_PLLSAI);
638 RCC_PLLSAICmd(DISABLE);
639 #define RCC_PLLSAI_GET_FLAG() ((RCC->CR & (RCC_CR_PLLSAIRDY)) == (RCC_CR_PLLSAIRDY))
641 while (RCC_PLLSAI_GET_FLAG() != 0)
643 RCC_PLLSAIConfig(PLLSAI_M, PLLSAI_N, PLLSAI_P, PLLSAI_Q);
644 RCC_PLLSAICmd(ENABLE);
646 while (RCC_PLLSAI_GET_FLAG() == 0)
649 RCC_48MHzClockSourceConfig(RCC_48MHZCLKSource_PLL);
659 #ifdef DATA_IN_ExtSRAM
667 void SystemInit_ExtMemCtl(
void)
690 RCC->AHB1ENR |= 0x00000078;
693 GPIOD->AFR[0] = 0x00cc00cc;
694 GPIOD->AFR[1] = 0xcccccccc;
696 GPIOD->MODER = 0xaaaa0a0a;
698 GPIOD->OSPEEDR = 0xffff0f0f;
700 GPIOD->OTYPER = 0x00000000;
702 GPIOD->PUPDR = 0x00000000;
705 GPIOE->AFR[0] = 0xcccccccc;
706 GPIOE->AFR[1] = 0xcccccccc;
708 GPIOE->MODER = 0xaaaaaaaa;
710 GPIOE->OSPEEDR = 0xffffffff;
712 GPIOE->OTYPER = 0x00000000;
714 GPIOE->PUPDR = 0x00000000;
717 GPIOF->AFR[0] = 0x00cccccc;
718 GPIOF->AFR[1] = 0xcccc0000;
720 GPIOF->MODER = 0xaa000aaa;
722 GPIOF->OSPEEDR = 0xff000fff;
724 GPIOF->OTYPER = 0x00000000;
726 GPIOF->PUPDR = 0x00000000;
729 GPIOG->AFR[0] = 0x00cccccc;
730 GPIOG->AFR[1] = 0x000000c0;
732 GPIOG->MODER = 0x00080aaa;
734 GPIOG->OSPEEDR = 0x000c0fff;
736 GPIOG->OTYPER = 0x00000000;
738 GPIOG->PUPDR = 0x00000000;
742 RCC->AHB3ENR |= 0x00000001;
744 #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
746 FMC_Bank1->BTCR[2] = 0x00001011;
747 FMC_Bank1->BTCR[3] = 0x00000201;
748 FMC_Bank1E->BWTR[2] = 0x0fffffff;
751 #if defined(STM32F40_41xxx)
753 FSMC_Bank1->BTCR[2] = 0x00001011;
754 FSMC_Bank1->BTCR[3] = 0x00000201;
755 FSMC_Bank1E->BWTR[2] = 0x0fffffff;
815 #ifdef DATA_IN_ExtSDRAM
823 void SystemInit_ExtMemCtl(
void)
825 register uint32_t tmpreg = 0, timeout = 0xFFFF;
826 register uint32_t index;
830 RCC->AHB1ENR |= 0x000001FC;
833 GPIOC->AFR[0] = 0x0000000c;
834 GPIOC->AFR[1] = 0x00007700;
836 GPIOC->MODER = 0x00a00002;
838 GPIOC->OSPEEDR = 0x00a00002;
840 GPIOC->OTYPER = 0x00000000;
842 GPIOC->PUPDR = 0x00500000;
845 GPIOD->AFR[0] = 0x000000CC;
846 GPIOD->AFR[1] = 0xCC000CCC;
848 GPIOD->MODER = 0xA02A000A;
850 GPIOD->OSPEEDR = 0xA02A000A;
852 GPIOD->OTYPER = 0x00000000;
854 GPIOD->PUPDR = 0x00000000;
857 GPIOE->AFR[0] = 0xC00000CC;
858 GPIOE->AFR[1] = 0xCCCCCCCC;
860 GPIOE->MODER = 0xAAAA800A;
862 GPIOE->OSPEEDR = 0xAAAA800A;
864 GPIOE->OTYPER = 0x00000000;
866 GPIOE->PUPDR = 0x00000000;
869 GPIOF->AFR[0] = 0xcccccccc;
870 GPIOF->AFR[1] = 0xcccccccc;
872 GPIOF->MODER = 0xAA800AAA;
874 GPIOF->OSPEEDR = 0xAA800AAA;
876 GPIOF->OTYPER = 0x00000000;
878 GPIOF->PUPDR = 0x00000000;
881 GPIOG->AFR[0] = 0xcccccccc;
882 GPIOG->AFR[1] = 0xcccccccc;
884 GPIOG->MODER = 0xaaaaaaaa;
886 GPIOG->OSPEEDR = 0xaaaaaaaa;
888 GPIOG->OTYPER = 0x00000000;
890 GPIOG->PUPDR = 0x00000000;
893 GPIOH->AFR[0] = 0x00C0CC00;
894 GPIOH->AFR[1] = 0xCCCCCCCC;
896 GPIOH->MODER = 0xAAAA08A0;
898 GPIOH->OSPEEDR = 0xAAAA08A0;
900 GPIOH->OTYPER = 0x00000000;
902 GPIOH->PUPDR = 0x00000000;
905 GPIOI->AFR[0] = 0xCCCCCCCC;
906 GPIOI->AFR[1] = 0x00000CC0;
908 GPIOI->MODER = 0x0028AAAA;
910 GPIOI->OSPEEDR = 0x0028AAAA;
912 GPIOI->OTYPER = 0x00000000;
914 GPIOI->PUPDR = 0x00000000;
918 RCC->AHB3ENR |= 0x00000001;
921 FMC_Bank5_6->SDCR[0] = 0x000039D0;
922 FMC_Bank5_6->SDTR[0] = 0x01115351;
926 FMC_Bank5_6->SDCMR = 0x00000011;
927 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
928 while((tmpreg != 0) & (timeout-- > 0))
930 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
934 for (index = 0; index<1000; index++);
937 FMC_Bank5_6->SDCMR = 0x00000012;
939 while((tmpreg != 0) & (timeout-- > 0))
941 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
945 FMC_Bank5_6->SDCMR = 0x00000073;
947 while((tmpreg != 0) & (timeout-- > 0))
949 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
953 FMC_Bank5_6->SDCMR = 0x00046014;
955 while((tmpreg != 0) & (timeout-- > 0))
957 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
961 tmpreg = FMC_Bank5_6->SDRTR;
962 FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
965 tmpreg = FMC_Bank5_6->SDCR[0];
966 FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
static void SetSysClock(void)
Configures the System clock source, PLL Multiplier and Divider factors, AHB/APBx prescalers and Flash...
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
void SystemInit(void)
Setup the microcontroller system Initialize the Embedded Flash Interface, the PLL and update the Syst...
__I uint8_t AHBPrescTable[16]