215 #include "stm32f4xx.h"
237 #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx)
241 #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
248 #define VECT_TAB_OFFSET 0x00
258 #if defined (STM32F40_41xxx)
264 #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
270 #if defined (STM32F401xx)
294 #if defined (STM32F40_41xxx)
295 uint32_t SystemCoreClock = 168000000;
298 #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
299 uint32_t SystemCoreClock = 180000000;
302 #if defined (STM32F401xx)
303 uint32_t SystemCoreClock = 84000000;
306 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
318 #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
319 static void SystemInit_ExtMemCtl(
void);
340 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
341 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));
345 RCC->CR |= (uint32_t)0x00000001;
348 RCC->CFGR = 0x00000000;
351 RCC->CR &= (uint32_t)0xFEF6FFFF;
354 RCC->PLLCFGR = 0x24003010;
357 RCC->CR &= (uint32_t)0xFFFBFFFF;
360 RCC->CIR = 0x00000000;
362 #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
363 SystemInit_ExtMemCtl();
416 uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
419 tmp = RCC->CFGR & RCC_CFGR_SWS;
424 SystemCoreClock = HSI_VALUE;
427 SystemCoreClock = HSE_VALUE;
434 pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
435 pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
440 pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
445 pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
448 pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
449 SystemCoreClock = pllvco/pllp;
452 SystemCoreClock = HSI_VALUE;
457 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
459 SystemCoreClock >>= tmp;
475 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
478 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
483 HSEStatus = RCC->CR & RCC_CR_HSERDY;
485 }
while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
487 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
489 HSEStatus = (uint32_t)0x01;
493 HSEStatus = (uint32_t)0x00;
496 if (HSEStatus == (uint32_t)0x01)
499 RCC->APB1ENR |= RCC_APB1ENR_PWREN;
500 PWR->CR |= PWR_CR_VOS;
503 RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
505 #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx)
507 RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
510 RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
513 #if defined (STM32F401xx)
515 RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
518 RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
522 RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
523 (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
526 RCC->CR |= RCC_CR_PLLON;
529 while((RCC->CR & RCC_CR_PLLRDY) == 0)
533 #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
535 PWR->CR |= PWR_CR_ODEN;
536 while((PWR->CSR & PWR_CSR_ODRDY) == 0)
539 PWR->CR |= PWR_CR_ODSWEN;
540 while((PWR->CSR & PWR_CSR_ODSWRDY) == 0)
544 FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
547 #if defined (STM32F40_41xxx)
549 FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
552 #if defined (STM32F401xx)
554 FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
558 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
559 RCC->CFGR |= RCC_CFGR_SW_PLL;
562 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
579 #ifdef DATA_IN_ExtSRAM
588 void SystemInit_ExtMemCtl(
void)
611 RCC->AHB1ENR |= 0x00000078;
614 GPIOD->AFR[0] = 0x00cc00cc;
615 GPIOD->AFR[1] = 0xcccccccc;
617 GPIOD->MODER = 0xaaaa0a0a;
619 GPIOD->OSPEEDR = 0xffff0f0f;
621 GPIOD->OTYPER = 0x00000000;
623 GPIOD->PUPDR = 0x00000000;
626 GPIOE->AFR[0] = 0xcccccccc;
627 GPIOE->AFR[1] = 0xcccccccc;
629 GPIOE->MODER = 0xaaaaaaaa;
631 GPIOE->OSPEEDR = 0xffffffff;
633 GPIOE->OTYPER = 0x00000000;
635 GPIOE->PUPDR = 0x00000000;
638 GPIOF->AFR[0] = 0x00cccccc;
639 GPIOF->AFR[1] = 0xcccc0000;
641 GPIOF->MODER = 0xaa000aaa;
643 GPIOF->OSPEEDR = 0xff000fff;
645 GPIOF->OTYPER = 0x00000000;
647 GPIOF->PUPDR = 0x00000000;
650 GPIOG->AFR[0] = 0x00cccccc;
651 GPIOG->AFR[1] = 0x000000c0;
653 GPIOG->MODER = 0x00080aaa;
655 GPIOG->OSPEEDR = 0x000c0fff;
657 GPIOG->OTYPER = 0x00000000;
659 GPIOG->PUPDR = 0x00000000;
663 RCC->AHB3ENR |= 0x00000001;
665 #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
667 FMC_Bank1->BTCR[2] = 0x00001011;
668 FMC_Bank1->BTCR[3] = 0x00000201;
669 FMC_Bank1E->BWTR[2] = 0x0fffffff;
672 #if defined (STM32F40_41xxx)
674 FSMC_Bank1->BTCR[2] = 0x00001011;
675 FSMC_Bank1->BTCR[3] = 0x00000201;
676 FSMC_Bank1E->BWTR[2] = 0x0fffffff;
736 #ifdef DATA_IN_ExtSDRAM
745 void SystemInit_ExtMemCtl(
void)
747 register uint32_t tmpreg = 0, timeout = 0xFFFF;
748 register uint32_t index;
752 RCC->AHB1ENR |= 0x000001FC;
755 GPIOC->AFR[0] = 0x0000000c;
756 GPIOC->AFR[1] = 0x00007700;
758 GPIOC->MODER = 0x00a00002;
760 GPIOC->OSPEEDR = 0x00a00002;
762 GPIOC->OTYPER = 0x00000000;
764 GPIOC->PUPDR = 0x00500000;
767 GPIOD->AFR[0] = 0x000000CC;
768 GPIOD->AFR[1] = 0xCC000CCC;
770 GPIOD->MODER = 0xA02A000A;
772 GPIOD->OSPEEDR = 0xA02A000A;
774 GPIOD->OTYPER = 0x00000000;
776 GPIOD->PUPDR = 0x00000000;
779 GPIOE->AFR[0] = 0xC00000CC;
780 GPIOE->AFR[1] = 0xCCCCCCCC;
782 GPIOE->MODER = 0xAAAA800A;
784 GPIOE->OSPEEDR = 0xAAAA800A;
786 GPIOE->OTYPER = 0x00000000;
788 GPIOE->PUPDR = 0x00000000;
791 GPIOF->AFR[0] = 0xcccccccc;
792 GPIOF->AFR[1] = 0xcccccccc;
794 GPIOF->MODER = 0xAA800AAA;
796 GPIOF->OSPEEDR = 0xAA800AAA;
798 GPIOF->OTYPER = 0x00000000;
800 GPIOF->PUPDR = 0x00000000;
803 GPIOG->AFR[0] = 0xcccccccc;
804 GPIOG->AFR[1] = 0xcccccccc;
806 GPIOG->MODER = 0xaaaaaaaa;
808 GPIOG->OSPEEDR = 0xaaaaaaaa;
810 GPIOG->OTYPER = 0x00000000;
812 GPIOG->PUPDR = 0x00000000;
815 GPIOH->AFR[0] = 0x00C0CC00;
816 GPIOH->AFR[1] = 0xCCCCCCCC;
818 GPIOH->MODER = 0xAAAA08A0;
820 GPIOH->OSPEEDR = 0xAAAA08A0;
822 GPIOH->OTYPER = 0x00000000;
824 GPIOH->PUPDR = 0x00000000;
827 GPIOI->AFR[0] = 0xCCCCCCCC;
828 GPIOI->AFR[1] = 0x00000CC0;
830 GPIOI->MODER = 0x0028AAAA;
832 GPIOI->OSPEEDR = 0x0028AAAA;
834 GPIOI->OTYPER = 0x00000000;
836 GPIOI->PUPDR = 0x00000000;
840 RCC->AHB3ENR |= 0x00000001;
843 FMC_Bank5_6->SDCR[0] = 0x000039D0;
844 FMC_Bank5_6->SDTR[0] = 0x01115351;
848 FMC_Bank5_6->SDCMR = 0x00000011;
849 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
850 while((tmpreg != 0) & (timeout-- > 0))
852 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
856 for (index = 0; index<1000; index++);
859 FMC_Bank5_6->SDCMR = 0x00000012;
861 while((tmpreg != 0) & (timeout-- > 0))
863 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
867 FMC_Bank5_6->SDCMR = 0x00000073;
869 while((tmpreg != 0) & (timeout-- > 0))
871 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
875 FMC_Bank5_6->SDCMR = 0x00046014;
877 while((tmpreg != 0) & (timeout-- > 0))
879 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
883 tmpreg = FMC_Bank5_6->SDRTR;
884 FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
887 tmpreg = FMC_Bank5_6->SDCR[0];
888 FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
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...
static void SetSysClock(void)
Configures the System clock source, PLL Multiplier and Divider factors, AHB/APBx prescalers and Flash...