STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
system_stm32f7xx.c
Go to the documentation of this file.
1 
66 #include "stm32f7xx.h"
67 
68 #if !defined (HSE_VALUE)
69  #define HSE_VALUE ((uint32_t)25000000)
70 #endif /* HSE_VALUE */
71 
72 #if !defined (HSI_VALUE)
73  #define HSI_VALUE ((uint32_t)16000000)
74 #endif /* HSI_VALUE */
75 
92 /************************* Miscellaneous Configuration ************************/
95 /* #define DATA_IN_ExtSDRAM */
96 
99 /* #define VECT_TAB_SRAM */
100 #define VECT_TAB_OFFSET 0x00
102 /******************************************************************************/
103 
120  /* This variable is updated in three ways:
121  1) by calling CMSIS function SystemCoreClockUpdate()
122  2) by calling HAL API function HAL_RCC_GetHCLKFreq()
123  3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
124  Note: If you use this function to configure the system clock; then there
125  is no need to call the 2 first functions listed above, since SystemCoreClock
126  variable is updated automatically.
127  */
128  uint32_t SystemCoreClock = 16000000;
129  const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
130  const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
139 #if defined (DATA_IN_ExtSDRAM)
140  static void SystemInit_ExtMemCtl(void);
141 #endif /* DATA_IN_ExtSDRAM */
142 
151 #define __FPU_PRESENT 1
152 #undef __FPU_USED
153 #define __FPU_USED 1
162 void SystemInit(void)
163 {
164  /* FPU settings ------------------------------------------------------------*/
165  #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
166  SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
167  #endif
168  /* Reset the RCC clock configuration to the default reset state ------------*/
169  /* Set HSION bit */
170  RCC->CR |= (uint32_t)0x00000001;
171 
172  /* Reset CFGR register */
173  RCC->CFGR = 0x00000000;
174 
175  /* Reset HSEON, CSSON and PLLON bits */
176  RCC->CR &= (uint32_t)0xFEF6FFFF;
177 
178  /* Reset PLLCFGR register */
179  RCC->PLLCFGR = 0x24003010;
180 
181  /* Reset HSEBYP bit */
182  RCC->CR &= (uint32_t)0xFFFBFFFF;
183 
184  /* Disable all interrupts */
185  RCC->CIR = 0x00000000;
186 
187 #if defined (DATA_IN_ExtSDRAM)
188  SystemInit_ExtMemCtl();
189 #endif /* DATA_IN_ExtSDRAM */
190 
191  /* Configure the Vector Table location add offset address ------------------*/
192 #ifdef VECT_TAB_SRAM
193  SCB->VTOR = SRAM1_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
194 #else
195  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
196 #endif
197 }
198 
235 void SystemCoreClockUpdate(void)
236 {
237  uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
238 
239  /* Get SYSCLK source -------------------------------------------------------*/
240  tmp = RCC->CFGR & RCC_CFGR_SWS;
241 
242  switch (tmp)
243  {
244  case 0x00: /* HSI used as system clock source */
246  break;
247  case 0x04: /* HSE used as system clock source */
249  break;
250  case 0x08: /* PLL used as system clock source */
251 
252  /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
253  SYSCLK = PLL_VCO / PLL_P
254  */
255  pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
256  pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
257 
258  if (pllsource != 0)
259  {
260  /* HSE used as PLL clock source */
261  pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
262  }
263  else
264  {
265  /* HSI used as PLL clock source */
266  pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
267  }
268 
269  pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
270  SystemCoreClock = pllvco/pllp;
271  break;
272  default:
274  break;
275  }
276  /* Compute HCLK frequency --------------------------------------------------*/
277  /* Get HCLK prescaler */
278  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
279  /* HCLK frequency */
280  SystemCoreClock >>= tmp;
281 }
282 
283 #if defined (DATA_IN_ExtSDRAM)
284 
292 void SystemInit_ExtMemCtl(void)
293 {
294  register uint32_t tmpreg = 0, timeout = 0xFFFF;
295  register __IO uint32_t index;
296 
297  /* Enable GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface
298  clock */
299  RCC->AHB1ENR |= 0x000001F8;
300 
301  /* Connect PDx pins to FMC Alternate function */
302  GPIOD->AFR[0] = 0x000000CC;
303  GPIOD->AFR[1] = 0xCC000CCC;
304  /* Configure PDx pins in Alternate function mode */
305  GPIOD->MODER = 0xA02A000A;
306  /* Configure PDx pins speed to 100 MHz */
307  GPIOD->OSPEEDR = 0xF03F000F;
308  /* Configure PDx pins Output type to push-pull */
309  GPIOD->OTYPER = 0x00000000;
310  /* No pull-up, pull-down for PDx pins */
311  GPIOD->PUPDR = 0x50150005;
312 
313  /* Connect PEx pins to FMC Alternate function */
314  GPIOE->AFR[0] = 0xC00000CC;
315  GPIOE->AFR[1] = 0xCCCCCCCC;
316  /* Configure PEx pins in Alternate function mode */
317  GPIOE->MODER = 0xAAAA800A;
318  /* Configure PEx pins speed to 100 MHz */
319  GPIOE->OSPEEDR = 0xFFFFC00F;
320  /* Configure PEx pins Output type to push-pull */
321  GPIOE->OTYPER = 0x00000000;
322  /* No pull-up, pull-down for PEx pins */
323  GPIOE->PUPDR = 0x55554005;
324 
325  /* Connect PFx pins to FMC Alternate function */
326  GPIOF->AFR[0] = 0x00CCCCCC;
327  GPIOF->AFR[1] = 0xCCCCC000;
328  /* Configure PFx pins in Alternate function mode */
329  GPIOF->MODER = 0xAA800AAA;
330  /* Configure PFx pins speed to 100 MHz */
331  GPIOF->OSPEEDR = 0xFFC00FFF;
332  /* Configure PFx pins Output type to push-pull */
333  GPIOF->OTYPER = 0x00000000;
334  /* No pull-up, pull-down for PFx pins */
335  GPIOF->PUPDR = 0x55400555;
336 
337  /* Connect PGx pins to FMC Alternate function */
338  GPIOG->AFR[0] = 0x00CC0CCC;
339  GPIOG->AFR[1] = 0xC000000C;
340  /* Configure PGx pins in Alternate function mode */
341  GPIOG->MODER = 0x80020A2A;
342  /* Configure PGx pins speed to 100 MHz */
343  GPIOG->OSPEEDR = 0xC0030F3F;
344  /* Configure PGx pins Output type to push-pull */
345  GPIOG->OTYPER = 0x00000000;
346  /* No pull-up, pull-down for PGx pins */
347  GPIOG->PUPDR = 0x40010515;
348 
349  /* Connect PHx pins to FMC Alternate function */
350  GPIOH->AFR[0] = 0x00C0CC00;
351  GPIOH->AFR[1] = 0xCCCCCCCC;
352  /* Configure PHx pins in Alternate function mode */
353  GPIOH->MODER = 0xAAAA08A0;
354  /* Configure PHx pins speed to 100 MHz */
355  GPIOH->OSPEEDR = 0xFFFF0CF0;
356  /* Configure PHx pins Output type to push-pull */
357  GPIOH->OTYPER = 0x00000000;
358  /* No pull-up, pull-down for PHx pins */
359  GPIOH->PUPDR = 0x55550450;
360 
361  /* Connect PIx pins to FMC Alternate function */
362  GPIOI->AFR[0] = 0xCCCCCCCC;
363  GPIOI->AFR[1] = 0x00000CC0;
364  /* Configure PIx pins in Alternate function mode */
365  GPIOI->MODER = 0x0028AAAA;
366  /* Configure PIx pins speed to 100 MHz */
367  GPIOI->OSPEEDR = 0x003CFFFF;
368  /* Configure PIx pins Output type to push-pull */
369  GPIOI->OTYPER = 0x00000000;
370  /* No pull-up, pull-down for PIx pins */
371  GPIOI->PUPDR = 0x00145555;
372 
373  /* Enable the FMC interface clock */
374  RCC->AHB3ENR |= 0x00000001;
375 
376  /* Configure and enable SDRAM bank1 */
377  FMC_Bank5_6->SDCR[0] = 0x000019E4;
378  FMC_Bank5_6->SDTR[0] = 0x01116361;
379 
380  /* SDRAM initialization sequence */
381  /* Clock enable command */
382  FMC_Bank5_6->SDCMR = 0x00000011;
383  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
384  while((tmpreg != 0) && (timeout-- > 0))
385  {
386  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
387  }
388 
389  /* Delay */
390  for (index = 0; index<1000; index++);
391 
392  /* PALL command */
393  FMC_Bank5_6->SDCMR = 0x00000012;
394  timeout = 0xFFFF;
395  while((tmpreg != 0) && (timeout-- > 0))
396  {
397  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
398  }
399 
400  /* Auto refresh command */
401  FMC_Bank5_6->SDCMR = 0x000000F3;
402  timeout = 0xFFFF;
403  while((tmpreg != 0) && (timeout-- > 0))
404  {
405  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
406  }
407 
408  /* MRD register program */
409  FMC_Bank5_6->SDCMR = 0x00046014;
410  timeout = 0xFFFF;
411  while((tmpreg != 0) && (timeout-- > 0))
412  {
413  tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
414  }
415 
416  /* Set refresh count */
417  tmpreg = FMC_Bank5_6->SDRTR;
418  FMC_Bank5_6->SDRTR = (tmpreg | (0x00000603<<1));
419 
420  /* Disable write protection */
421  tmpreg = FMC_Bank5_6->SDCR[0];
422  FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
423 
424  /*
425  * Disable the FMC bank1 (enabled after reset).
426  * This, prevents CPU speculation access on this bank which blocks the use of FMC during
427  * 24us. During this time the others FMC master (such as LTDC) cannot use it!
428  */
429  FMC_Bank1->BTCR[0] = 0x000030d2;
430 }
431 #endif /* DATA_IN_ExtSDRAM */
432 
445 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#define GPIOD
Definition: stm32f745xx.h:1316
#define RCC_PLLCFGR_PLLN
Definition: stm32f745xx.h:5213
#define GPIOE
Definition: stm32f745xx.h:1317
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
#define SRAM1_BASE
Definition: stm32f745xx.h:1109
void SystemInit(void)
Setup the microcontroller system Initialize the Embedded Flash Interface, the PLL and update the Syst...
#define RCC
Definition: stm32f745xx.h:1325
#define RCC_PLLCFGR_PLLP
Definition: stm32f745xx.h:5223
#define RCC_PLLCFGR_PLLM
Definition: stm32f745xx.h:5206
#define __IO
Definition: core_cm0.h:213
#define RCC_CFGR_HPRE
Definition: stm32f745xx.h:5254
#define RCC_PLLCFGR_PLLSRC
Definition: stm32f745xx.h:5226
#define SCB
Definition: core_cm0.h:587
#define GPIOI
Definition: stm32f745xx.h:1321
#define GPIOH
Definition: stm32f745xx.h:1320
CMSIS STM32F7xx Device Peripheral Access Layer Header File.
#define HSE_VALUE
const uint8_t APBPrescTable[8]
#define HSI_VALUE
#define FLASH_BASE
Definition: stm32f745xx.h:1114
#define GPIOF
Definition: stm32f745xx.h:1318
#define FMC_Bank1
Definition: stm32f745xx.h:1349
#define GPIOG
Definition: stm32f745xx.h:1319
#define FMC_Bank5_6
Definition: stm32f745xx.h:1352
#define VECT_TAB_OFFSET
uint32_t SystemCoreClock
#define RCC_CFGR_SWS
Definition: stm32f745xx.h:5246
const uint8_t AHBPrescTable[16]