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 
128 #include "stm32f4xx.h"
129 
146 /************************* Miscellaneous Configuration ************************/
149 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx)
150 /* #define DATA_IN_ExtSRAM */
151 #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx */
152 
153 #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
154 /* #define DATA_IN_ExtSDRAM */
155 #endif /* STM32F427_437x || STM32F429_439xx || STM32F446xx */
156 
157 #if defined(STM32F411xE)
158 
165 /* #define USE_HSE_BYPASS */
166 
167 #if defined(USE_HSE_BYPASS)
168 #define HSE_BYPASS_INPUT_FREQUENCY 8000000
169 #endif /* USE_HSE_BYPASS */
170 #endif /* STM32F411xE */
171 
174 /* #define VECT_TAB_SRAM */
175 #define VECT_TAB_OFFSET 0x00
177 /******************************************************************************/
178 
179 /************************* PLL Parameters *************************************/
180 #if SYSCLK_FREQ == 180000000
181 #if defined(STM32F40_41xxx)
182 #error 180MHz is not supported!
183 #endif
184 #define PLL_M 8
185 #define PLL_N 180
186 #define PLL_P 2
187 #define PLL_Q 2
188 #define PLL_R 2
189 
190 #define PLLSAI_M 8
191 #define PLLSAI_N 192
192 #define PLLSAI_P 8
193 #define PLLSAI_Q 2
194 #elif SYSCLK_FREQ == 168000000
195 #define PLL_M 16
196 #define PLL_N 336
197 #define PLL_P 2
198 #define PLL_Q 7
199 #define PLL_R 7
200 #else
201 #error Invalid SYSCLK_FREQ
202 #endif
203 /******************************************************************************/
204 
220 uint32_t SystemCoreClock = SYSCLK_FREQ;
222 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
232 static void SetSysClock(void);
233 
234 #if defined(DATA_IN_ExtSRAM) || defined(DATA_IN_ExtSDRAM)
235 static void SystemInit_ExtMemCtl(void);
236 #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
237 
252 void SystemInit(void)
253 {
254  /* FPU settings ------------------------------------------------------------*/
255  #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
256  SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
257  #endif
258  /* Reset the RCC clock configuration to the default reset state ------------*/
259  /* Set HSION bit */
260  RCC->CR |= (uint32_t)0x00000001;
261 
262  /* Reset CFGR register */
263  RCC->CFGR = 0x00000000;
264 
265  /* Reset HSEON, CSSON and PLLON bits */
266  RCC->CR &= (uint32_t)0xFEF6FFFF;
267 
268  /* Reset PLLCFGR register */
269  RCC->PLLCFGR = 0x24003010;
270 
271  /* Reset HSEBYP bit */
272  RCC->CR &= (uint32_t)0xFFFBFFFF;
273 
274  /* Disable all interrupts */
275  RCC->CIR = 0x00000000;
276 
277 #if defined(DATA_IN_ExtSRAM) || defined(DATA_IN_ExtSDRAM)
278  SystemInit_ExtMemCtl();
279 #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
280 
281  /* Configure the System clock source, PLL Multiplier and Divider factors,
282  AHB/APBx prescalers and Flash settings ----------------------------------*/
283  SetSysClock();
284 
285  /* Configure the Vector Table location add offset address ------------------*/
286 #ifdef VECT_TAB_SRAM
287  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
288 #else
289  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
290 #endif
291 }
292 
328 void SystemCoreClockUpdate(void)
329 {
330  uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
331 #if defined(STM32F446xx)
332  uint32_t pllr = 2;
333 #endif /* STM32F446xx */
334  /* Get SYSCLK source -------------------------------------------------------*/
335  tmp = RCC->CFGR & RCC_CFGR_SWS;
336 
337  switch (tmp)
338  {
339  case 0x00: /* HSI used as system clock source */
340  SystemCoreClock = HSI_VALUE;
341  break;
342  case 0x04: /* HSE used as system clock source */
343  SystemCoreClock = HSE_VALUE;
344  break;
345  case 0x08: /* PLL P used as system clock source */
346  /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
347  SYSCLK = PLL_VCO / PLL_P
348  */
349  pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
350  pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
351 
352 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx) || defined(STM32F446xx)
353  if (pllsource != 0)
354  {
355  /* HSE used as PLL clock source */
356  pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
357  }
358  else
359  {
360  /* HSI used as PLL clock source */
361  pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
362  }
363 #elif defined(STM32F411xE)
364 #if defined(USE_HSE_BYPASS)
365  if (pllsource != 0)
366  {
367  /* HSE used as PLL clock source */
368  pllvco = (HSE_BYPASS_INPUT_FREQUENCY / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
369  }
370 #else
371  if (pllsource == 0)
372  {
373  /* HSI used as PLL clock source */
374  pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
375  }
376 #endif /* USE_HSE_BYPASS */
377 #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx || STM32F446xx */
378  pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
379  SystemCoreClock = pllvco/pllp;
380  break;
381 #if defined(STM32F446xx)
382  case 0x0C: /* PLL R used as system clock source */
383  /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
384  SYSCLK = PLL_VCO / PLL_R
385  */
386  pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
387  pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
388  if (pllsource != 0)
389  {
390  /* HSE used as PLL clock source */
391  pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
392  }
393  else
394  {
395  /* HSI used as PLL clock source */
396  pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
397  }
398 
399  pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >>28) + 1 ) *2;
400  SystemCoreClock = pllvco/pllr;
401  break;
402 #endif /* STM32F446xx */
403  default:
404  SystemCoreClock = HSI_VALUE;
405  break;
406  }
407  /* Compute HCLK frequency --------------------------------------------------*/
408  /* Get HCLK prescaler */
409  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
410  /* HCLK frequency */
411  SystemCoreClock >>= tmp;
412 }
413 
421 static void SetSysClock(void)
422 {
423 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx) || defined(STM32F446xx)
424 /******************************************************************************/
425 /* PLL (clocked by HSE) used as System clock source */
426 /******************************************************************************/
427  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
428 
429  /* Enable HSE */
430  RCC->CR |= ((uint32_t)RCC_CR_HSEON);
431 
432  /* Wait till HSE is ready and if Time out is reached exit */
433  do
434  {
435  HSEStatus = RCC->CR & RCC_CR_HSERDY;
436  StartUpCounter++;
437  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
438 
439  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
440  {
441  HSEStatus = (uint32_t)0x01;
442  }
443  else
444  {
445  HSEStatus = (uint32_t)0x00;
446  }
447 
448  if (HSEStatus == (uint32_t)0x01)
449  {
450  /* Select regulator voltage output Scale 1 mode */
451  RCC->APB1ENR |= RCC_APB1ENR_PWREN;
452  PWR->CR |= PWR_CR_VOS;
453 
454  /* HCLK = SYSCLK / 1*/
455  RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
456 
457 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
458  /* PCLK2 = HCLK / 2*/
459  RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
460 
461  /* PCLK1 = HCLK / 4*/
462  RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
463 #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx || STM32F446xx */
464 
465 #if defined(STM32F401xx)
466  /* PCLK2 = HCLK / 2*/
467  RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
468 
469  /* PCLK1 = HCLK / 4*/
470  RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
471 #endif /* STM32F401xx */
472 
473 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx)
474  /* Configure the main PLL */
475  RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
476  (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
477 #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx || STM32F401xx */
478 
479 #if defined(STM32F446xx)
480  /* Configure the main PLL */
481  RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
482  (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24) | (PLL_R << 28);
483 #endif /* STM32F446xx */
484 
485  /* Enable the main PLL */
486  RCC->CR |= RCC_CR_PLLON;
487 
488  /* Wait till the main PLL is ready */
489  while((RCC->CR & RCC_CR_PLLRDY) == 0);
490 
491 #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
492  /* Enable the Over-drive to extend the clock frequency to 180 Mhz */
493  PWR->CR |= PWR_CR_ODEN;
494  while((PWR->CSR & PWR_CSR_ODRDY) == 0);
495 
496  PWR->CR |= PWR_CR_ODSWEN;
497  while((PWR->CSR & PWR_CSR_ODSWRDY) == 0);
498 
499  /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
500  FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
501 #endif /* STM32F427_437x || STM32F429_439xx || STM32F446xx */
502 
503 #if defined(STM32F40_41xxx)
504  /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
505  FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
506 #endif /* STM32F40_41xxx */
507 
508 #if defined(STM32F401xx)
509  /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
510  FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
511 #endif /* STM32F401xx */
512 
513  /* Select the main PLL as system clock source */
514  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
515  RCC->CFGR |= RCC_CFGR_SW_PLL;
516 
517  /* Wait till the main PLL is used as system clock source */
518  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
519  }
520  else
521  { /* If HSE fails to start-up, the application will have wrong clock
522  configuration. User can add here some code to deal with this error */
523  }
524 #elif defined(STM32F411xE)
525 #if defined(USE_HSE_BYPASS)
526 /******************************************************************************/
527 /* PLL (clocked by HSE) used as System clock source */
528 /******************************************************************************/
529  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
530 
531  /* Enable HSE and HSE BYPASS */
532  RCC->CR |= ((uint32_t)RCC_CR_HSEON | RCC_CR_HSEBYP);
533 
534  /* Wait till HSE is ready and if Time out is reached exit */
535  do
536  {
537  HSEStatus = RCC->CR & RCC_CR_HSERDY;
538  StartUpCounter++;
539  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
540 
541  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
542  {
543  HSEStatus = (uint32_t)0x01;
544  }
545  else
546  {
547  HSEStatus = (uint32_t)0x00;
548  }
549 
550  if (HSEStatus == (uint32_t)0x01)
551  {
552  /* Select regulator voltage output Scale 1 mode */
553  RCC->APB1ENR |= RCC_APB1ENR_PWREN;
554  PWR->CR |= PWR_CR_VOS;
555 
556  /* HCLK = SYSCLK / 1*/
557  RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
558 
559  /* PCLK2 = HCLK / 2*/
560  RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
561 
562  /* PCLK1 = HCLK / 4*/
563  RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
564 
565  /* Configure the main PLL */
566  RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
567  (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
568 
569  /* Enable the main PLL */
570  RCC->CR |= RCC_CR_PLLON;
571 
572  /* Wait till the main PLL is ready */
573  while((RCC->CR & RCC_CR_PLLRDY) == 0)
574  {
575  }
576 
577  /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
578  FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
579 
580  /* Select the main PLL as system clock source */
581  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
582  RCC->CFGR |= RCC_CFGR_SW_PLL;
583 
584  /* Wait till the main PLL is used as system clock source */
585  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
586  {
587  }
588  }
589  else
590  { /* If HSE fails to start-up, the application will have wrong clock
591  configuration. User can add here some code to deal with this error */
592  }
593 #else /* HSI will be used as PLL clock source */
594  /* Select regulator voltage output Scale 1 mode */
595  RCC->APB1ENR |= RCC_APB1ENR_PWREN;
596  PWR->CR |= PWR_CR_VOS;
597 
598  /* HCLK = SYSCLK / 1*/
599  RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
600 
601  /* PCLK2 = HCLK / 2*/
602  RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
603 
604  /* PCLK1 = HCLK / 4*/
605  RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
606 
607  /* Configure the main PLL */
608  RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) | (PLL_Q << 24);
609 
610  /* Enable the main PLL */
611  RCC->CR |= RCC_CR_PLLON;
612 
613  /* Wait till the main PLL is ready */
614  while((RCC->CR & RCC_CR_PLLRDY) == 0)
615  {
616  }
617 
618  /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
619  FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
620 
621  /* Select the main PLL as system clock source */
622  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
623  RCC->CFGR |= RCC_CFGR_SW_PLL;
624 
625  /* Wait till the main PLL is used as system clock source */
626  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
627  {
628  }
629 #endif /* USE_HSE_BYPASS */
630 #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx */
631 
632 #if defined(STM32F446xx)
633 #if SYSCLK_FREQ == 180000000
634  /* Configure 48MHz clock for USB */
635  // Set 48MHz clock source
636  RCC_48MHzClockSourceConfig(RCC_48MHZCLKSource_PLLSAI);
637  // Enable PLLSAI
638  RCC_PLLSAICmd(DISABLE);
639  #define RCC_PLLSAI_GET_FLAG() ((RCC->CR & (RCC_CR_PLLSAIRDY)) == (RCC_CR_PLLSAIRDY))
640  // wait for PLLSAI to be disabled
641  while (RCC_PLLSAI_GET_FLAG() != 0)
642  {}
643  RCC_PLLSAIConfig(PLLSAI_M, PLLSAI_N, PLLSAI_P, PLLSAI_Q);
644  RCC_PLLSAICmd(ENABLE);
645  // wait for PLLSAI to be enabled
646  while (RCC_PLLSAI_GET_FLAG() == 0)
647  {}
648 #else
649  RCC_48MHzClockSourceConfig(RCC_48MHZCLKSource_PLL);
650 #endif /* SYSCLK_FREQ == 180000000 */
651 #endif /* STM32F446xx */
652 }
653 
659 #ifdef DATA_IN_ExtSRAM
660 
667 void SystemInit_ExtMemCtl(void)
668 {
669 /*-- GPIOs Configuration -----------------------------------------------------*/
670 /*
671  +-------------------+--------------------+------------------+--------------+
672  + SRAM pins assignment +
673  +-------------------+--------------------+------------------+--------------+
674  | PD0 <-> FMC_D2 | PE0 <-> FMC_NBL0 | PF0 <-> FMC_A0 | PG0 <-> FMC_A10 |
675  | PD1 <-> FMC_D3 | PE1 <-> FMC_NBL1 | PF1 <-> FMC_A1 | PG1 <-> FMC_A11 |
676  | PD4 <-> FMC_NOE | PE3 <-> FMC_A19 | PF2 <-> FMC_A2 | PG2 <-> FMC_A12 |
677  | PD5 <-> FMC_NWE | PE4 <-> FMC_A20 | PF3 <-> FMC_A3 | PG3 <-> FMC_A13 |
678  | PD8 <-> FMC_D13 | PE7 <-> FMC_D4 | PF4 <-> FMC_A4 | PG4 <-> FMC_A14 |
679  | PD9 <-> FMC_D14 | PE8 <-> FMC_D5 | PF5 <-> FMC_A5 | PG5 <-> FMC_A15 |
680  | PD10 <-> FMC_D15 | PE9 <-> FMC_D6 | PF12 <-> FMC_A6 | PG9 <-> FMC_NE2 |
681  | PD11 <-> FMC_A16 | PE10 <-> FMC_D7 | PF13 <-> FMC_A7 |-----------------+
682  | PD12 <-> FMC_A17 | PE11 <-> FMC_D8 | PF14 <-> FMC_A8 |
683  | PD13 <-> FMC_A18 | PE12 <-> FMC_D9 | PF15 <-> FMC_A9 |
684  | PD14 <-> FMC_D0 | PE13 <-> FMC_D10 |-----------------+
685  | PD15 <-> FMC_D1 | PE14 <-> FMC_D11 |
686  | | PE15 <-> FMC_D12 |
687  +------------------+------------------+
688 */
689  /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
690  RCC->AHB1ENR |= 0x00000078;
691 
692  /* Connect PDx pins to FMC Alternate function */
693  GPIOD->AFR[0] = 0x00cc00cc;
694  GPIOD->AFR[1] = 0xcccccccc;
695  /* Configure PDx pins in Alternate function mode */
696  GPIOD->MODER = 0xaaaa0a0a;
697  /* Configure PDx pins speed to 100 MHz */
698  GPIOD->OSPEEDR = 0xffff0f0f;
699  /* Configure PDx pins Output type to push-pull */
700  GPIOD->OTYPER = 0x00000000;
701  /* No pull-up, pull-down for PDx pins */
702  GPIOD->PUPDR = 0x00000000;
703 
704  /* Connect PEx pins to FMC Alternate function */
705  GPIOE->AFR[0] = 0xcccccccc;
706  GPIOE->AFR[1] = 0xcccccccc;
707  /* Configure PEx pins in Alternate function mode */
708  GPIOE->MODER = 0xaaaaaaaa;
709  /* Configure PEx pins speed to 100 MHz */
710  GPIOE->OSPEEDR = 0xffffffff;
711  /* Configure PEx pins Output type to push-pull */
712  GPIOE->OTYPER = 0x00000000;
713  /* No pull-up, pull-down for PEx pins */
714  GPIOE->PUPDR = 0x00000000;
715 
716  /* Connect PFx pins to FMC Alternate function */
717  GPIOF->AFR[0] = 0x00cccccc;
718  GPIOF->AFR[1] = 0xcccc0000;
719  /* Configure PFx pins in Alternate function mode */
720  GPIOF->MODER = 0xaa000aaa;
721  /* Configure PFx pins speed to 100 MHz */
722  GPIOF->OSPEEDR = 0xff000fff;
723  /* Configure PFx pins Output type to push-pull */
724  GPIOF->OTYPER = 0x00000000;
725  /* No pull-up, pull-down for PFx pins */
726  GPIOF->PUPDR = 0x00000000;
727 
728  /* Connect PGx pins to FMC Alternate function */
729  GPIOG->AFR[0] = 0x00cccccc;
730  GPIOG->AFR[1] = 0x000000c0;
731  /* Configure PGx pins in Alternate function mode */
732  GPIOG->MODER = 0x00080aaa;
733  /* Configure PGx pins speed to 100 MHz */
734  GPIOG->OSPEEDR = 0x000c0fff;
735  /* Configure PGx pins Output type to push-pull */
736  GPIOG->OTYPER = 0x00000000;
737  /* No pull-up, pull-down for PGx pins */
738  GPIOG->PUPDR = 0x00000000;
739 
740 /*-- FMC Configuration ------------------------------------------------------*/
741  /* Enable the FMC/FSMC interface clock */
742  RCC->AHB3ENR |= 0x00000001;
743 
744 #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
745  /* Configure and enable Bank1_SRAM2 */
746  FMC_Bank1->BTCR[2] = 0x00001011;
747  FMC_Bank1->BTCR[3] = 0x00000201;
748  FMC_Bank1E->BWTR[2] = 0x0fffffff;
749 #endif /* STM32F427_437xx || STM32F429_439xx */
750 
751 #if defined(STM32F40_41xxx)
752  /* Configure and enable Bank1_SRAM2 */
753  FSMC_Bank1->BTCR[2] = 0x00001011;
754  FSMC_Bank1->BTCR[3] = 0x00000201;
755  FSMC_Bank1E->BWTR[2] = 0x0fffffff;
756 #endif /* STM32F40_41xxx */
757 
758 /*
759  Bank1_SRAM2 is configured as follow:
760  In case of FSMC configuration
761  NORSRAMTimingStructure.FSMC_AddressSetupTime = 1;
762  NORSRAMTimingStructure.FSMC_AddressHoldTime = 0;
763  NORSRAMTimingStructure.FSMC_DataSetupTime = 2;
764  NORSRAMTimingStructure.FSMC_BusTurnAroundDuration = 0;
765  NORSRAMTimingStructure.FSMC_CLKDivision = 0;
766  NORSRAMTimingStructure.FSMC_DataLatency = 0;
767  NORSRAMTimingStructure.FSMC_AccessMode = FMC_AccessMode_A;
768 
769  FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
770  FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
771  FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
772  FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
773  FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
774  FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
775  FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
776  FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
777  FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
778  FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
779  FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
780  FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
781  FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
782  FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &NORSRAMTimingStructure;
783  FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &NORSRAMTimingStructure;
784 
785  In case of FMC configuration
786  NORSRAMTimingStructure.FMC_AddressSetupTime = 1;
787  NORSRAMTimingStructure.FMC_AddressHoldTime = 0;
788  NORSRAMTimingStructure.FMC_DataSetupTime = 2;
789  NORSRAMTimingStructure.FMC_BusTurnAroundDuration = 0;
790  NORSRAMTimingStructure.FMC_CLKDivision = 0;
791  NORSRAMTimingStructure.FMC_DataLatency = 0;
792  NORSRAMTimingStructure.FMC_AccessMode = FMC_AccessMode_A;
793 
794  FMC_NORSRAMInitStructure.FMC_Bank = FMC_Bank1_NORSRAM2;
795  FMC_NORSRAMInitStructure.FMC_DataAddressMux = FMC_DataAddressMux_Disable;
796  FMC_NORSRAMInitStructure.FMC_MemoryType = FMC_MemoryType_SRAM;
797  FMC_NORSRAMInitStructure.FMC_MemoryDataWidth = FMC_MemoryDataWidth_16b;
798  FMC_NORSRAMInitStructure.FMC_BurstAccessMode = FMC_BurstAccessMode_Disable;
799  FMC_NORSRAMInitStructure.FMC_AsynchronousWait = FMC_AsynchronousWait_Disable;
800  FMC_NORSRAMInitStructure.FMC_WaitSignalPolarity = FMC_WaitSignalPolarity_Low;
801  FMC_NORSRAMInitStructure.FMC_WrapMode = FMC_WrapMode_Disable;
802  FMC_NORSRAMInitStructure.FMC_WaitSignalActive = FMC_WaitSignalActive_BeforeWaitState;
803  FMC_NORSRAMInitStructure.FMC_WriteOperation = FMC_WriteOperation_Enable;
804  FMC_NORSRAMInitStructure.FMC_WaitSignal = FMC_WaitSignal_Disable;
805  FMC_NORSRAMInitStructure.FMC_ExtendedMode = FMC_ExtendedMode_Disable;
806  FMC_NORSRAMInitStructure.FMC_WriteBurst = FMC_WriteBurst_Disable;
807  FMC_NORSRAMInitStructure.FMC_ContinousClock = FMC_CClock_SyncOnly;
808  FMC_NORSRAMInitStructure.FMC_ReadWriteTimingStruct = &NORSRAMTimingStructure;
809  FMC_NORSRAMInitStructure.FMC_WriteTimingStruct = &NORSRAMTimingStructure;
810 */
811 
812 }
813 #endif /* DATA_IN_ExtSRAM */
814 
815 #ifdef DATA_IN_ExtSDRAM
816 
823 void SystemInit_ExtMemCtl(void)
824 {
825  register uint32_t tmpreg = 0, timeout = 0xFFFF;
826  register uint32_t index;
827 
828  /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface
829  clock */
830  RCC->AHB1ENR |= 0x000001FC;
831 
832  /* Connect PCx pins to FMC Alternate function */
833  GPIOC->AFR[0] = 0x0000000c;
834  GPIOC->AFR[1] = 0x00007700;
835  /* Configure PCx pins in Alternate function mode */
836  GPIOC->MODER = 0x00a00002;
837  /* Configure PCx pins speed to 50 MHz */
838  GPIOC->OSPEEDR = 0x00a00002;
839  /* Configure PCx pins Output type to push-pull */
840  GPIOC->OTYPER = 0x00000000;
841  /* No pull-up, pull-down for PCx pins */
842  GPIOC->PUPDR = 0x00500000;
843 
844  /* Connect PDx pins to FMC Alternate function */
845  GPIOD->AFR[0] = 0x000000CC;
846  GPIOD->AFR[1] = 0xCC000CCC;
847  /* Configure PDx pins in Alternate function mode */
848  GPIOD->MODER = 0xA02A000A;
849  /* Configure PDx pins speed to 50 MHz */
850  GPIOD->OSPEEDR = 0xA02A000A;
851  /* Configure PDx pins Output type to push-pull */
852  GPIOD->OTYPER = 0x00000000;
853  /* No pull-up, pull-down for PDx pins */
854  GPIOD->PUPDR = 0x00000000;
855 
856  /* Connect PEx pins to FMC Alternate function */
857  GPIOE->AFR[0] = 0xC00000CC;
858  GPIOE->AFR[1] = 0xCCCCCCCC;
859  /* Configure PEx pins in Alternate function mode */
860  GPIOE->MODER = 0xAAAA800A;
861  /* Configure PEx pins speed to 50 MHz */
862  GPIOE->OSPEEDR = 0xAAAA800A;
863  /* Configure PEx pins Output type to push-pull */
864  GPIOE->OTYPER = 0x00000000;
865  /* No pull-up, pull-down for PEx pins */
866  GPIOE->PUPDR = 0x00000000;
867 
868  /* Connect PFx pins to FMC Alternate function */
869  GPIOF->AFR[0] = 0xcccccccc;
870  GPIOF->AFR[1] = 0xcccccccc;
871  /* Configure PFx pins in Alternate function mode */
872  GPIOF->MODER = 0xAA800AAA;
873  /* Configure PFx pins speed to 50 MHz */
874  GPIOF->OSPEEDR = 0xAA800AAA;
875  /* Configure PFx pins Output type to push-pull */
876  GPIOF->OTYPER = 0x00000000;
877  /* No pull-up, pull-down for PFx pins */
878  GPIOF->PUPDR = 0x00000000;
879 
880  /* Connect PGx pins to FMC Alternate function */
881  GPIOG->AFR[0] = 0xcccccccc;
882  GPIOG->AFR[1] = 0xcccccccc;
883  /* Configure PGx pins in Alternate function mode */
884  GPIOG->MODER = 0xaaaaaaaa;
885  /* Configure PGx pins speed to 50 MHz */
886  GPIOG->OSPEEDR = 0xaaaaaaaa;
887  /* Configure PGx pins Output type to push-pull */
888  GPIOG->OTYPER = 0x00000000;
889  /* No pull-up, pull-down for PGx pins */
890  GPIOG->PUPDR = 0x00000000;
891 
892  /* Connect PHx pins to FMC Alternate function */
893  GPIOH->AFR[0] = 0x00C0CC00;
894  GPIOH->AFR[1] = 0xCCCCCCCC;
895  /* Configure PHx pins in Alternate function mode */
896  GPIOH->MODER = 0xAAAA08A0;
897  /* Configure PHx pins speed to 50 MHz */
898  GPIOH->OSPEEDR = 0xAAAA08A0;
899  /* Configure PHx pins Output type to push-pull */
900  GPIOH->OTYPER = 0x00000000;
901  /* No pull-up, pull-down for PHx pins */
902  GPIOH->PUPDR = 0x00000000;
903 
904  /* Connect PIx pins to FMC Alternate function */
905  GPIOI->AFR[0] = 0xCCCCCCCC;
906  GPIOI->AFR[1] = 0x00000CC0;
907  /* Configure PIx pins in Alternate function mode */
908  GPIOI->MODER = 0x0028AAAA;
909  /* Configure PIx pins speed to 50 MHz */
910  GPIOI->OSPEEDR = 0x0028AAAA;
911  /* Configure PIx pins Output type to push-pull */
912  GPIOI->OTYPER = 0x00000000;
913  /* No pull-up, pull-down for PIx pins */
914  GPIOI->PUPDR = 0x00000000;
915 
916 /*-- FMC Configuration ------------------------------------------------------*/
917  /* Enable the FMC interface clock */
918  RCC->AHB3ENR |= 0x00000001;
919 
920  /* Configure and enable SDRAM bank1 */
921  FMC_Bank5_6->SDCR[0] = 0x000039D0;
922  FMC_Bank5_6->SDTR[0] = 0x01115351;
923 
924  /* SDRAM initialization sequence */
925  /* Clock enable command */
926  FMC_Bank5_6->SDCMR = 0x00000011;
927  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
928  while((tmpreg != 0) & (timeout-- > 0))
929  {
930  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
931  }
932 
933  /* Delay */
934  for (index = 0; index<1000; index++);
935 
936  /* PALL command */
937  FMC_Bank5_6->SDCMR = 0x00000012;
938  timeout = 0xFFFF;
939  while((tmpreg != 0) & (timeout-- > 0))
940  {
941  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
942  }
943 
944  /* Auto refresh command */
945  FMC_Bank5_6->SDCMR = 0x00000073;
946  timeout = 0xFFFF;
947  while((tmpreg != 0) & (timeout-- > 0))
948  {
949  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
950  }
951 
952  /* MRD register program */
953  FMC_Bank5_6->SDCMR = 0x00046014;
954  timeout = 0xFFFF;
955  while((tmpreg != 0) & (timeout-- > 0))
956  {
957  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
958  }
959 
960  /* Set refresh count */
961  tmpreg = FMC_Bank5_6->SDRTR;
962  FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
963 
964  /* Disable write protection */
965  tmpreg = FMC_Bank5_6->SDCR[0];
966  FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
967 
968 /*
969  Bank1_SDRAM is configured as follow:
970 
971  FMC_SDRAMTimingInitStructure.FMC_LoadToActiveDelay = 2;
972  FMC_SDRAMTimingInitStructure.FMC_ExitSelfRefreshDelay = 6;
973  FMC_SDRAMTimingInitStructure.FMC_SelfRefreshTime = 4;
974  FMC_SDRAMTimingInitStructure.FMC_RowCycleDelay = 6;
975  FMC_SDRAMTimingInitStructure.FMC_WriteRecoveryTime = 2;
976  FMC_SDRAMTimingInitStructure.FMC_RPDelay = 2;
977  FMC_SDRAMTimingInitStructure.FMC_RCDDelay = 2;
978 
979  FMC_SDRAMInitStructure.FMC_Bank = SDRAM_BANK;
980  FMC_SDRAMInitStructure.FMC_ColumnBitsNumber = FMC_ColumnBits_Number_8b;
981  FMC_SDRAMInitStructure.FMC_RowBitsNumber = FMC_RowBits_Number_11b;
982  FMC_SDRAMInitStructure.FMC_SDMemoryDataWidth = FMC_SDMemory_Width_16b;
983  FMC_SDRAMInitStructure.FMC_InternalBankNumber = FMC_InternalBank_Number_4;
984  FMC_SDRAMInitStructure.FMC_CASLatency = FMC_CAS_Latency_3;
985  FMC_SDRAMInitStructure.FMC_WriteProtection = FMC_Write_Protection_Disable;
986  FMC_SDRAMInitStructure.FMC_SDClockPeriod = FMC_SDClock_Period_2;
987  FMC_SDRAMInitStructure.FMC_ReadBurst = FMC_Read_Burst_disable;
988  FMC_SDRAMInitStructure.FMC_ReadPipeDelay = FMC_ReadPipe_Delay_1;
989  FMC_SDRAMInitStructure.FMC_SDRAMTimingStruct = &FMC_SDRAMTimingInitStructure;
990 */
991 
992 }
993 #endif /* DATA_IN_ExtSDRAM */
994 
995 
1007 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
static void SetSysClock(void)
Configures the System clock source, PLL Multiplier and Divider factors, AHB/APBx prescalers and Flash...
Definition: cmsis_system.c:422
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
Definition: cmsis_system.c:289
void SystemInit(void)
Setup the microcontroller system Initialize the Embedded Flash Interface, the PLL and update the Syst...
Definition: cmsis_system.c:213
#define PLL_Q
Definition: cmsis_system.c:162
#define PLL_M
Definition: cmsis_system.c:155
__I uint8_t AHBPrescTable[16]
Definition: cmsis_system.c:184
#define VECT_TAB_OFFSET
Definition: cmsis_system.c:175
uint32_t SystemCoreClock
Definition: cmsis_system.c:182