CARME-M4 BSP  V1.5
system_stm32f4xx.c
1 
215 #include "stm32f4xx.h"
216 
233 /************************* Miscellaneous Configuration ************************/
237 #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx)
238 /* #define DATA_IN_ExtSRAM */
239 #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx */
240 
241 #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
242 /* #define DATA_IN_ExtSDRAM */
243 #endif /* STM32F427_437x || STM32F429_439xx */
244 
247 /* #define VECT_TAB_SRAM */
248 #define VECT_TAB_OFFSET 0x00
250 /******************************************************************************/
251 
252 /************************* PLL Parameters *************************************/
253 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
254 #define PLL_M 25
255 /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
256 #define PLL_Q 7
257 
258 #if defined (STM32F40_41xxx)
259 #define PLL_N 336
260 /* SYSCLK = PLL_VCO / PLL_P */
261 #define PLL_P 2
262 #endif /* STM32F40_41xxx */
263 
264 #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
265 #define PLL_N 360
266 /* SYSCLK = PLL_VCO / PLL_P */
267 #define PLL_P 2
268 #endif /* STM32F427_437x || STM32F429_439xx */
269 
270 #if defined (STM32F401xx)
271 #define PLL_N 336
272 /* SYSCLK = PLL_VCO / PLL_P */
273 #define PLL_P 4
274 #endif /* STM32F401xx */
275 
276 /******************************************************************************/
277 
294 #if defined (STM32F40_41xxx)
295  uint32_t SystemCoreClock = 168000000;
296 #endif /* STM32F40_41xxx */
297 
298 #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
299  uint32_t SystemCoreClock = 180000000;
300 #endif /* STM32F427_437x || STM32F429_439xx */
301 
302 #if defined (STM32F401xx)
303  uint32_t SystemCoreClock = 84000000;
304 #endif /* STM32F401xx */
305 
306  __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
307 
316 static void SetSysClock(void);
317 
318 #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
319 static void SystemInit_ExtMemCtl(void);
320 #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
321 
337 void SystemInit(void)
338 {
339  /* FPU settings ------------------------------------------------------------*/
340  #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
341  SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
342  #endif
343  /* Reset the RCC clock configuration to the default reset state ------------*/
344  /* Set HSION bit */
345  RCC->CR |= (uint32_t)0x00000001;
346 
347  /* Reset CFGR register */
348  RCC->CFGR = 0x00000000;
349 
350  /* Reset HSEON, CSSON and PLLON bits */
351  RCC->CR &= (uint32_t)0xFEF6FFFF;
352 
353  /* Reset PLLCFGR register */
354  RCC->PLLCFGR = 0x24003010;
355 
356  /* Reset HSEBYP bit */
357  RCC->CR &= (uint32_t)0xFFFBFFFF;
358 
359  /* Disable all interrupts */
360  RCC->CIR = 0x00000000;
361 
362 #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
363  SystemInit_ExtMemCtl();
364 #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
365 
366  /* Configure the System clock source, PLL Multiplier and Divider factors,
367  AHB/APBx prescalers and Flash settings ----------------------------------*/
368  SetSysClock();
369 
370  /* Configure the Vector Table location add offset address ------------------*/
371 #ifdef VECT_TAB_SRAM
372  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
373 #else
374  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
375 #endif
376 }
377 
414 void SystemCoreClockUpdate(void)
415 {
416  uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
417 
418  /* Get SYSCLK source -------------------------------------------------------*/
419  tmp = RCC->CFGR & RCC_CFGR_SWS;
420 
421  switch (tmp)
422  {
423  case 0x00: /* HSI used as system clock source */
424  SystemCoreClock = HSI_VALUE;
425  break;
426  case 0x04: /* HSE used as system clock source */
427  SystemCoreClock = HSE_VALUE;
428  break;
429  case 0x08: /* PLL used as system clock source */
430 
431  /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
432  SYSCLK = PLL_VCO / PLL_P
433  */
434  pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
435  pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
436 
437  if (pllsource != 0)
438  {
439  /* HSE used as PLL clock source */
440  pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
441  }
442  else
443  {
444  /* HSI used as PLL clock source */
445  pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
446  }
447 
448  pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
449  SystemCoreClock = pllvco/pllp;
450  break;
451  default:
452  SystemCoreClock = HSI_VALUE;
453  break;
454  }
455  /* Compute HCLK frequency --------------------------------------------------*/
456  /* Get HCLK prescaler */
457  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
458  /* HCLK frequency */
459  SystemCoreClock >>= tmp;
460 }
461 
470 static void SetSysClock(void)
471 {
472 /******************************************************************************/
473 /* PLL (clocked by HSE) used as System clock source */
474 /******************************************************************************/
475  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
476 
477  /* Enable HSE */
478  RCC->CR |= ((uint32_t)RCC_CR_HSEON);
479 
480  /* Wait till HSE is ready and if Time out is reached exit */
481  do
482  {
483  HSEStatus = RCC->CR & RCC_CR_HSERDY;
484  StartUpCounter++;
485  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
486 
487  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
488  {
489  HSEStatus = (uint32_t)0x01;
490  }
491  else
492  {
493  HSEStatus = (uint32_t)0x00;
494  }
495 
496  if (HSEStatus == (uint32_t)0x01)
497  {
498  /* Select regulator voltage output Scale 1 mode */
499  RCC->APB1ENR |= RCC_APB1ENR_PWREN;
500  PWR->CR |= PWR_CR_VOS;
501 
502  /* HCLK = SYSCLK / 1*/
503  RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
504 
505 #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx)
506  /* PCLK2 = HCLK / 2*/
507  RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
508 
509  /* PCLK1 = HCLK / 4*/
510  RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
511 #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx */
512 
513 #if defined (STM32F401xx)
514  /* PCLK2 = HCLK / 2*/
515  RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
516 
517  /* PCLK1 = HCLK / 4*/
518  RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
519 #endif /* STM32F401xx */
520 
521  /* Configure the main PLL */
522  RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
523  (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
524 
525  /* Enable the main PLL */
526  RCC->CR |= RCC_CR_PLLON;
527 
528  /* Wait till the main PLL is ready */
529  while((RCC->CR & RCC_CR_PLLRDY) == 0)
530  {
531  }
532 
533 #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
534  /* Enable the Over-drive to extend the clock frequency to 180 Mhz */
535  PWR->CR |= PWR_CR_ODEN;
536  while((PWR->CSR & PWR_CSR_ODRDY) == 0)
537  {
538  }
539  PWR->CR |= PWR_CR_ODSWEN;
540  while((PWR->CSR & PWR_CSR_ODSWRDY) == 0)
541  {
542  }
543  /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
544  FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
545 #endif /* STM32F427_437x || STM32F429_439xx */
546 
547 #if defined (STM32F40_41xxx)
548  /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
549  FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
550 #endif /* STM32F40_41xxx */
551 
552 #if defined (STM32F401xx)
553  /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
554  FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
555 #endif /* STM32F401xx */
556 
557  /* Select the main PLL as system clock source */
558  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
559  RCC->CFGR |= RCC_CFGR_SW_PLL;
560 
561  /* Wait till the main PLL is used as system clock source */
562  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
563  {
564  }
565  }
566  else
567  { /* If HSE fails to start-up, the application will have wrong clock
568  configuration. User can add here some code to deal with this error */
569  }
570 
571 }
572 
579 #ifdef DATA_IN_ExtSRAM
580 
588 void SystemInit_ExtMemCtl(void)
589 {
590 /*-- GPIOs Configuration -----------------------------------------------------*/
591 /*
592  +-------------------+--------------------+------------------+--------------+
593  + SRAM pins assignment +
594  +-------------------+--------------------+------------------+--------------+
595  | PD0 <-> FMC_D2 | PE0 <-> FMC_NBL0 | PF0 <-> FMC_A0 | PG0 <-> FMC_A10 |
596  | PD1 <-> FMC_D3 | PE1 <-> FMC_NBL1 | PF1 <-> FMC_A1 | PG1 <-> FMC_A11 |
597  | PD4 <-> FMC_NOE | PE3 <-> FMC_A19 | PF2 <-> FMC_A2 | PG2 <-> FMC_A12 |
598  | PD5 <-> FMC_NWE | PE4 <-> FMC_A20 | PF3 <-> FMC_A3 | PG3 <-> FMC_A13 |
599  | PD8 <-> FMC_D13 | PE7 <-> FMC_D4 | PF4 <-> FMC_A4 | PG4 <-> FMC_A14 |
600  | PD9 <-> FMC_D14 | PE8 <-> FMC_D5 | PF5 <-> FMC_A5 | PG5 <-> FMC_A15 |
601  | PD10 <-> FMC_D15 | PE9 <-> FMC_D6 | PF12 <-> FMC_A6 | PG9 <-> FMC_NE2 |
602  | PD11 <-> FMC_A16 | PE10 <-> FMC_D7 | PF13 <-> FMC_A7 |-----------------+
603  | PD12 <-> FMC_A17 | PE11 <-> FMC_D8 | PF14 <-> FMC_A8 |
604  | PD13 <-> FMC_A18 | PE12 <-> FMC_D9 | PF15 <-> FMC_A9 |
605  | PD14 <-> FMC_D0 | PE13 <-> FMC_D10 |-----------------+
606  | PD15 <-> FMC_D1 | PE14 <-> FMC_D11 |
607  | | PE15 <-> FMC_D12 |
608  +------------------+------------------+
609 */
610  /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
611  RCC->AHB1ENR |= 0x00000078;
612 
613  /* Connect PDx pins to FMC Alternate function */
614  GPIOD->AFR[0] = 0x00cc00cc;
615  GPIOD->AFR[1] = 0xcccccccc;
616  /* Configure PDx pins in Alternate function mode */
617  GPIOD->MODER = 0xaaaa0a0a;
618  /* Configure PDx pins speed to 100 MHz */
619  GPIOD->OSPEEDR = 0xffff0f0f;
620  /* Configure PDx pins Output type to push-pull */
621  GPIOD->OTYPER = 0x00000000;
622  /* No pull-up, pull-down for PDx pins */
623  GPIOD->PUPDR = 0x00000000;
624 
625  /* Connect PEx pins to FMC Alternate function */
626  GPIOE->AFR[0] = 0xcccccccc;
627  GPIOE->AFR[1] = 0xcccccccc;
628  /* Configure PEx pins in Alternate function mode */
629  GPIOE->MODER = 0xaaaaaaaa;
630  /* Configure PEx pins speed to 100 MHz */
631  GPIOE->OSPEEDR = 0xffffffff;
632  /* Configure PEx pins Output type to push-pull */
633  GPIOE->OTYPER = 0x00000000;
634  /* No pull-up, pull-down for PEx pins */
635  GPIOE->PUPDR = 0x00000000;
636 
637  /* Connect PFx pins to FMC Alternate function */
638  GPIOF->AFR[0] = 0x00cccccc;
639  GPIOF->AFR[1] = 0xcccc0000;
640  /* Configure PFx pins in Alternate function mode */
641  GPIOF->MODER = 0xaa000aaa;
642  /* Configure PFx pins speed to 100 MHz */
643  GPIOF->OSPEEDR = 0xff000fff;
644  /* Configure PFx pins Output type to push-pull */
645  GPIOF->OTYPER = 0x00000000;
646  /* No pull-up, pull-down for PFx pins */
647  GPIOF->PUPDR = 0x00000000;
648 
649  /* Connect PGx pins to FMC Alternate function */
650  GPIOG->AFR[0] = 0x00cccccc;
651  GPIOG->AFR[1] = 0x000000c0;
652  /* Configure PGx pins in Alternate function mode */
653  GPIOG->MODER = 0x00080aaa;
654  /* Configure PGx pins speed to 100 MHz */
655  GPIOG->OSPEEDR = 0x000c0fff;
656  /* Configure PGx pins Output type to push-pull */
657  GPIOG->OTYPER = 0x00000000;
658  /* No pull-up, pull-down for PGx pins */
659  GPIOG->PUPDR = 0x00000000;
660 
661 /*-- FMC Configuration ------------------------------------------------------*/
662  /* Enable the FMC/FSMC interface clock */
663  RCC->AHB3ENR |= 0x00000001;
664 
665 #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
666  /* Configure and enable Bank1_SRAM2 */
667  FMC_Bank1->BTCR[2] = 0x00001011;
668  FMC_Bank1->BTCR[3] = 0x00000201;
669  FMC_Bank1E->BWTR[2] = 0x0fffffff;
670 #endif /* STM32F427_437xx || STM32F429_439xx */
671 
672 #if defined (STM32F40_41xxx)
673  /* Configure and enable Bank1_SRAM2 */
674  FSMC_Bank1->BTCR[2] = 0x00001011;
675  FSMC_Bank1->BTCR[3] = 0x00000201;
676  FSMC_Bank1E->BWTR[2] = 0x0fffffff;
677 #endif /* STM32F40_41xxx */
678 
679 /*
680  Bank1_SRAM2 is configured as follow:
681  In case of FSMC configuration
682  NORSRAMTimingStructure.FSMC_AddressSetupTime = 1;
683  NORSRAMTimingStructure.FSMC_AddressHoldTime = 0;
684  NORSRAMTimingStructure.FSMC_DataSetupTime = 2;
685  NORSRAMTimingStructure.FSMC_BusTurnAroundDuration = 0;
686  NORSRAMTimingStructure.FSMC_CLKDivision = 0;
687  NORSRAMTimingStructure.FSMC_DataLatency = 0;
688  NORSRAMTimingStructure.FSMC_AccessMode = FMC_AccessMode_A;
689 
690  FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
691  FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
692  FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
693  FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
694  FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
695  FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
696  FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
697  FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
698  FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
699  FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
700  FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
701  FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
702  FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
703  FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &NORSRAMTimingStructure;
704  FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &NORSRAMTimingStructure;
705 
706  In case of FMC configuration
707  NORSRAMTimingStructure.FMC_AddressSetupTime = 1;
708  NORSRAMTimingStructure.FMC_AddressHoldTime = 0;
709  NORSRAMTimingStructure.FMC_DataSetupTime = 2;
710  NORSRAMTimingStructure.FMC_BusTurnAroundDuration = 0;
711  NORSRAMTimingStructure.FMC_CLKDivision = 0;
712  NORSRAMTimingStructure.FMC_DataLatency = 0;
713  NORSRAMTimingStructure.FMC_AccessMode = FMC_AccessMode_A;
714 
715  FMC_NORSRAMInitStructure.FMC_Bank = FMC_Bank1_NORSRAM2;
716  FMC_NORSRAMInitStructure.FMC_DataAddressMux = FMC_DataAddressMux_Disable;
717  FMC_NORSRAMInitStructure.FMC_MemoryType = FMC_MemoryType_SRAM;
718  FMC_NORSRAMInitStructure.FMC_MemoryDataWidth = FMC_MemoryDataWidth_16b;
719  FMC_NORSRAMInitStructure.FMC_BurstAccessMode = FMC_BurstAccessMode_Disable;
720  FMC_NORSRAMInitStructure.FMC_AsynchronousWait = FMC_AsynchronousWait_Disable;
721  FMC_NORSRAMInitStructure.FMC_WaitSignalPolarity = FMC_WaitSignalPolarity_Low;
722  FMC_NORSRAMInitStructure.FMC_WrapMode = FMC_WrapMode_Disable;
723  FMC_NORSRAMInitStructure.FMC_WaitSignalActive = FMC_WaitSignalActive_BeforeWaitState;
724  FMC_NORSRAMInitStructure.FMC_WriteOperation = FMC_WriteOperation_Enable;
725  FMC_NORSRAMInitStructure.FMC_WaitSignal = FMC_WaitSignal_Disable;
726  FMC_NORSRAMInitStructure.FMC_ExtendedMode = FMC_ExtendedMode_Disable;
727  FMC_NORSRAMInitStructure.FMC_WriteBurst = FMC_WriteBurst_Disable;
728  FMC_NORSRAMInitStructure.FMC_ContinousClock = FMC_CClock_SyncOnly;
729  FMC_NORSRAMInitStructure.FMC_ReadWriteTimingStruct = &NORSRAMTimingStructure;
730  FMC_NORSRAMInitStructure.FMC_WriteTimingStruct = &NORSRAMTimingStructure;
731 */
732 
733 }
734 #endif /* DATA_IN_ExtSRAM */
735 
736 #ifdef DATA_IN_ExtSDRAM
737 
745 void SystemInit_ExtMemCtl(void)
746 {
747  register uint32_t tmpreg = 0, timeout = 0xFFFF;
748  register uint32_t index;
749 
750  /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface
751  clock */
752  RCC->AHB1ENR |= 0x000001FC;
753 
754  /* Connect PCx pins to FMC Alternate function */
755  GPIOC->AFR[0] = 0x0000000c;
756  GPIOC->AFR[1] = 0x00007700;
757  /* Configure PCx pins in Alternate function mode */
758  GPIOC->MODER = 0x00a00002;
759  /* Configure PCx pins speed to 50 MHz */
760  GPIOC->OSPEEDR = 0x00a00002;
761  /* Configure PCx pins Output type to push-pull */
762  GPIOC->OTYPER = 0x00000000;
763  /* No pull-up, pull-down for PCx pins */
764  GPIOC->PUPDR = 0x00500000;
765 
766  /* Connect PDx pins to FMC Alternate function */
767  GPIOD->AFR[0] = 0x000000CC;
768  GPIOD->AFR[1] = 0xCC000CCC;
769  /* Configure PDx pins in Alternate function mode */
770  GPIOD->MODER = 0xA02A000A;
771  /* Configure PDx pins speed to 50 MHz */
772  GPIOD->OSPEEDR = 0xA02A000A;
773  /* Configure PDx pins Output type to push-pull */
774  GPIOD->OTYPER = 0x00000000;
775  /* No pull-up, pull-down for PDx pins */
776  GPIOD->PUPDR = 0x00000000;
777 
778  /* Connect PEx pins to FMC Alternate function */
779  GPIOE->AFR[0] = 0xC00000CC;
780  GPIOE->AFR[1] = 0xCCCCCCCC;
781  /* Configure PEx pins in Alternate function mode */
782  GPIOE->MODER = 0xAAAA800A;
783  /* Configure PEx pins speed to 50 MHz */
784  GPIOE->OSPEEDR = 0xAAAA800A;
785  /* Configure PEx pins Output type to push-pull */
786  GPIOE->OTYPER = 0x00000000;
787  /* No pull-up, pull-down for PEx pins */
788  GPIOE->PUPDR = 0x00000000;
789 
790  /* Connect PFx pins to FMC Alternate function */
791  GPIOF->AFR[0] = 0xcccccccc;
792  GPIOF->AFR[1] = 0xcccccccc;
793  /* Configure PFx pins in Alternate function mode */
794  GPIOF->MODER = 0xAA800AAA;
795  /* Configure PFx pins speed to 50 MHz */
796  GPIOF->OSPEEDR = 0xAA800AAA;
797  /* Configure PFx pins Output type to push-pull */
798  GPIOF->OTYPER = 0x00000000;
799  /* No pull-up, pull-down for PFx pins */
800  GPIOF->PUPDR = 0x00000000;
801 
802  /* Connect PGx pins to FMC Alternate function */
803  GPIOG->AFR[0] = 0xcccccccc;
804  GPIOG->AFR[1] = 0xcccccccc;
805  /* Configure PGx pins in Alternate function mode */
806  GPIOG->MODER = 0xaaaaaaaa;
807  /* Configure PGx pins speed to 50 MHz */
808  GPIOG->OSPEEDR = 0xaaaaaaaa;
809  /* Configure PGx pins Output type to push-pull */
810  GPIOG->OTYPER = 0x00000000;
811  /* No pull-up, pull-down for PGx pins */
812  GPIOG->PUPDR = 0x00000000;
813 
814  /* Connect PHx pins to FMC Alternate function */
815  GPIOH->AFR[0] = 0x00C0CC00;
816  GPIOH->AFR[1] = 0xCCCCCCCC;
817  /* Configure PHx pins in Alternate function mode */
818  GPIOH->MODER = 0xAAAA08A0;
819  /* Configure PHx pins speed to 50 MHz */
820  GPIOH->OSPEEDR = 0xAAAA08A0;
821  /* Configure PHx pins Output type to push-pull */
822  GPIOH->OTYPER = 0x00000000;
823  /* No pull-up, pull-down for PHx pins */
824  GPIOH->PUPDR = 0x00000000;
825 
826  /* Connect PIx pins to FMC Alternate function */
827  GPIOI->AFR[0] = 0xCCCCCCCC;
828  GPIOI->AFR[1] = 0x00000CC0;
829  /* Configure PIx pins in Alternate function mode */
830  GPIOI->MODER = 0x0028AAAA;
831  /* Configure PIx pins speed to 50 MHz */
832  GPIOI->OSPEEDR = 0x0028AAAA;
833  /* Configure PIx pins Output type to push-pull */
834  GPIOI->OTYPER = 0x00000000;
835  /* No pull-up, pull-down for PIx pins */
836  GPIOI->PUPDR = 0x00000000;
837 
838 /*-- FMC Configuration ------------------------------------------------------*/
839  /* Enable the FMC interface clock */
840  RCC->AHB3ENR |= 0x00000001;
841 
842  /* Configure and enable SDRAM bank1 */
843  FMC_Bank5_6->SDCR[0] = 0x000039D0;
844  FMC_Bank5_6->SDTR[0] = 0x01115351;
845 
846  /* SDRAM initialization sequence */
847  /* Clock enable command */
848  FMC_Bank5_6->SDCMR = 0x00000011;
849  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
850  while((tmpreg != 0) & (timeout-- > 0))
851  {
852  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
853  }
854 
855  /* Delay */
856  for (index = 0; index<1000; index++);
857 
858  /* PALL command */
859  FMC_Bank5_6->SDCMR = 0x00000012;
860  timeout = 0xFFFF;
861  while((tmpreg != 0) & (timeout-- > 0))
862  {
863  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
864  }
865 
866  /* Auto refresh command */
867  FMC_Bank5_6->SDCMR = 0x00000073;
868  timeout = 0xFFFF;
869  while((tmpreg != 0) & (timeout-- > 0))
870  {
871  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
872  }
873 
874  /* MRD register program */
875  FMC_Bank5_6->SDCMR = 0x00046014;
876  timeout = 0xFFFF;
877  while((tmpreg != 0) & (timeout-- > 0))
878  {
879  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
880  }
881 
882  /* Set refresh count */
883  tmpreg = FMC_Bank5_6->SDRTR;
884  FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
885 
886  /* Disable write protection */
887  tmpreg = FMC_Bank5_6->SDCR[0];
888  FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
889 
890 /*
891  Bank1_SDRAM is configured as follow:
892 
893  FMC_SDRAMTimingInitStructure.FMC_LoadToActiveDelay = 2;
894  FMC_SDRAMTimingInitStructure.FMC_ExitSelfRefreshDelay = 6;
895  FMC_SDRAMTimingInitStructure.FMC_SelfRefreshTime = 4;
896  FMC_SDRAMTimingInitStructure.FMC_RowCycleDelay = 6;
897  FMC_SDRAMTimingInitStructure.FMC_WriteRecoveryTime = 2;
898  FMC_SDRAMTimingInitStructure.FMC_RPDelay = 2;
899  FMC_SDRAMTimingInitStructure.FMC_RCDDelay = 2;
900 
901  FMC_SDRAMInitStructure.FMC_Bank = SDRAM_BANK;
902  FMC_SDRAMInitStructure.FMC_ColumnBitsNumber = FMC_ColumnBits_Number_8b;
903  FMC_SDRAMInitStructure.FMC_RowBitsNumber = FMC_RowBits_Number_11b;
904  FMC_SDRAMInitStructure.FMC_SDMemoryDataWidth = FMC_SDMemory_Width_16b;
905  FMC_SDRAMInitStructure.FMC_InternalBankNumber = FMC_InternalBank_Number_4;
906  FMC_SDRAMInitStructure.FMC_CASLatency = FMC_CAS_Latency_3;
907  FMC_SDRAMInitStructure.FMC_WriteProtection = FMC_Write_Protection_Disable;
908  FMC_SDRAMInitStructure.FMC_SDClockPeriod = FMC_SDClock_Period_2;
909  FMC_SDRAMInitStructure.FMC_ReadBurst = FMC_Read_Burst_disable;
910  FMC_SDRAMInitStructure.FMC_ReadPipeDelay = FMC_ReadPipe_Delay_1;
911  FMC_SDRAMInitStructure.FMC_SDRAMTimingStruct = &FMC_SDRAMTimingInitStructure;
912 */
913 
914 }
915 #endif /* DATA_IN_ExtSDRAM */
916 
917 
929 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
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...
#define VECT_TAB_OFFSET