Loading...
Searching...
No Matches
system_stm32f4xx.c
Go to the documentation of this file.
1
34
38
42
46
47
48#include "stm32f4xx.h"
49#include "config.h"
50
51// Function prototypes
52void SystemCoreClockUpdate(void); // Update the variables relating to the system clock.
53static void SetSysClock(void); // Set the system clock.
54
55#if !defined (HSE_VALUE)
56 #define HSE_VALUE ((uint32_t)12000000)
57#endif /* HSE_VALUE */
58
59#if !defined (HSI_VALUE)
60 #define HSI_VALUE ((uint32_t)16000000)
61#endif /* HSI_VALUE */
62
63
67
71
75
79
80/************************* Miscellaneous Configuration ************************/
82#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\
83 || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
84 || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx)
85/* #define DATA_IN_ExtSRAM */
86#endif /* STM32F40xxx || STM32F41xxx || STM32F42xxx || STM32F43xxx || STM32F469xx || STM32F479xx ||\
87 STM32F412Zx || STM32F412Vx */
88
89#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
90 || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
91/* #define DATA_IN_ExtSDRAM */
92#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx ||\
93 STM32F479xx */
94
95/* Note: Following vector table addresses must be defined in line with linker
96 configuration. */
100/* #define USER_VECT_TAB_ADDRESS */
101
102#if defined(USER_VECT_TAB_ADDRESS)
105/* #define VECT_TAB_SRAM */
106#if defined(VECT_TAB_SRAM)
107#define VECT_TAB_BASE_ADDRESS SRAM_BASE
109#define VECT_TAB_OFFSET 0x00000000U
111#else
112#define VECT_TAB_BASE_ADDRESS FLASH_BASE
114#define VECT_TAB_OFFSET 0x00000000U
116#endif /* VECT_TAB_SRAM */
117#endif /* USER_VECT_TAB_ADDRESS */
118/******************************************************************************/
119
123
127
131
135 /* This variable is updated in three ways:
136 1) by calling CMSIS function SystemCoreClockUpdate()
137 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
138 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
139 Note: If you use this function to configure the system clock; then there
140 is no need to call the 2 first functions listed above, since SystemCoreClock
141 variable is updated automatically.
142 */
143
144uint32_t SystemCoreClock = 16800000;
145const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
146const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
150
154
155#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
156 static void SystemInit_ExtMemCtl(void);
157#endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
158
162
166
174void SystemInit(void)
175{
176 /* FPU settings ------------------------------------------------------------*/
177 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
178 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
179 #endif
180
181 #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
182 SystemInit_ExtMemCtl();
183 #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
184
185 /* Configure the Vector Table location -------------------------------------*/
186 #if defined(USER_VECT_TAB_ADDRESS)
187 SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
188 #endif /* USER_VECT_TAB_ADDRESS */
189
190 /* Reset the RCC clock configuration to the default reset state ------------*/
191 /* Set HSION bit */
192 RCC->CR |= (uint32_t)0x00000001;
193
194 /* Reset CFGR register */
195 RCC->CFGR = 0x00000000;
196
197 /* Reset HSEON, CSSON and PLLON bits */
198 RCC->CR &= (uint32_t)0xFEF6FFFF;
199
200 /* Reset PLLCFGR register, HSI used by default */
201 RCC->PLLCFGR = 0x24003010;
202
203 /* Reset HSEBYP bit */
204 RCC->CR &= (uint32_t)0xFFFBFFFF;
205
206 /* Disable all interrupts */
207 RCC->CIR = 0x00000000;
208
209 // Set the custom configuration for the system clock.
210 // The HAL variables are updated at the end of this function.
211 SetSysClock();
212}
213
214//******************************************************************************//
215// Function: SetSysClock()
216// Input : None
217// Return : None
218// Description : Configure the system clocks
219// *****************************************************************************//
220static void SetSysClock(void)
221{
222 // By default the High Speed External Osciallator (HSE) is used as the
223 // PLL clock source.
224 // For the STM32F4 Discovery, the external crystal oscillator is 8MHz
225 // For the Emcraft SOM, the external crystal oscillator is 12MHz
226 volatile unsigned int StartUpCounter = 0;
227
228
229 //volatile unsigned int tmp = 0x00;
230 #if HSE_USED
231 // Enable the High Speed Oscillator
232 RCC->CR |= RCC_CR_HSEON;
233
234 // Wait until the High Speed Oscillator is ready, or a timeout reached.
235
236 while(((RCC->CR & RCC_CR_HSERDY) == 0) && StartUpCounter!= HSE_STARTUP_TIMEOUT)
237 {
238 StartUpCounter++;
239 }
240
241 // Check to see that the High Speed Oscillator started successfully.
242 if((RCC->CR & RCC_CR_HSERDY) == RCC_CR_HSERDY)
243 {
244 // The oscillator started successfully
245
246 // Enable the power control (PWR) clock
247 RCC->APB1ENR |= RCC_APB1ENR_PWREN;
248
249 /* Select regulator voltage output Scale 1 mode, System frequency up to 168 MHz */
250 PWR->CR |= PWR_CR_VOS;
251
252 // Set the HCLK to the same speed as the system clock (AHB Bus (STM32F407 = 168MHz))
253 RCC->CFGR &= ~(RCC_CFGR_HPRE | RCC_CFGR_PPRE2 | RCC_CFGR_PPRE1); // This should clear the bits.
254
255 // Set the PCLK2 to the HCLK divided by 2. (APB2 Bus (High Speed) (STM32F407 = 84MHz))
256 RCC->CFGR |= (0x04 << RCC_CFGR_PPRE2_Pos);
257
258 // Set the PCLK1 to the HCLK divided by 4. (APB1 Bus (Low Speed) (STM32F407 = 42MHz))
259 RCC->CFGR |= (0x05 << RCC_CFGR_PPRE1_Pos);
260
261 // Set the PLL configuration register. The PLL_VCO frequency is given by:
262 // PLL_VCO = (HSE_VALUE / PLL_M) * PLL_N
263 // SYS_CLK = PLL_VCO / PLL_P (168MHz)
264 // USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ (48MHz)
265 // For the STM32F407, this equates to a PLL_VCO of
266 // For the Emcraft board, this equates to a PLL_VCO of 336MHz (Sysclock = 168MHz)
267
268 // Configure the main PLL
269 RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) | (1 << 22) | (PLL_Q << 24);
270
271 // Now that the main configuration is complete, enable the PLL
272 RCC->CR |= RCC_CR_PLLON;
273
274 // Wait until the main PLL has started
275 while((RCC->CR & RCC_CR_PLLRDY) == 0);
276
277 // Configure Flash prefetch, Instruction cache, Data cache and wait state
278 FLASH->ACR &= ~(FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | (FLASH_ACR_LATENCY));
279 FLASH->ACR |= (FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | (0x05 << FLASH_ACR_LATENCY_Pos));
280
281 // FLASH->ACR.b.prften = 1; // Flash prefetch enable
282 // FLASH->ACR.b.icen = 1; // Instruction cache enable
283 // FLASH->ACR.b.dcen = 1; // Data cache enable
284 // FLASH->ACR.b.latency = 0x05; // 5 wait states.
285
286 // Select the main PLL as system clock source
287 RCC->CFGR &= ~(RCC_CFGR_SW_0 | RCC_CFGR_SW_1);
288 RCC->CFGR |= RCC_CFGR_SW_1;
289 // RCC->CFGR.b.sw0 = 0x00;
290 // RCC->CFGR.b.sw1 = 0x01;
291
292 // Wait until the main PLL is used as the system clock source
293 while((RCC->CR & RCC_CR_PLLON) != RCC_CR_PLLON);
294 }
295 else
296 {
297 // Oscillator failed to start.
298 // HSEStatus = 0x00;
299
300 /* If HSE fails to start-up, the application will have wrong clock
301 configuration. User can add here some code to deal with this error */
302 }
303
304 #elif !HSE_USED
305 // Adjust the frequency of the internal oscillator
306 // First clear the adjustment values (HSICAL and HSITRIM), before setting them
307 //RCC->CR &= ~( 0xF << RCC_CR_HSITRIM_Pos);
308 //RCC->CR &= ~( 0xFF << RCC_CR_HSICAL_Pos);
309 //RCC->CR |= ( (HSITRIM & 0xF) << RCC_CR_HSITRIM_Pos);
310 //RCC->CR |= ( (HSICAL & 0xFF) << RCC_CR_HSICAL_Pos);
311
312 if((RCC->CR & RCC_CR_HSIRDY) == RCC_CR_HSIRDY)
313 {
314 // The oscillator started successfully
315
316 // Enable the power control (PWR) clock
317 RCC->APB1ENR |= RCC_APB1ENR_PWREN;
318
319 /* Select regulator voltage output Scale 1 mode, System frequency up to 168 MHz */
320 PWR->CR |= PWR_CR_VOS;
321
322 // Set the HCLK to the same speed as the system clock (AHB Bus (STM32F407 = 168MHz))
323 RCC->CFGR &= ~(RCC_CFGR_HPRE | RCC_CFGR_PPRE2 | RCC_CFGR_PPRE1); // This should clear the bits.
324
325 // Set the PCLK2 to the HCLK divided by 2. (APB2 Bus (High Speed) (STM32F407 = 84MHz))
326 RCC->CFGR |= (0x04 << RCC_CFGR_PPRE2_Pos);
327
328 // Set the PCLK1 to the HCLK divided by 4. (APB1 Bus (Low Speed) (STM32F407 = 42MHz))
329 RCC->CFGR |= (0x05 << RCC_CFGR_PPRE1_Pos);
330
331 // Set the PLL configuration register. The PLL_VCO frequency is given by:
332 // PLL_VCO = (HSE_VALUE / PLL_M) * PLL_N
333 // SYS_CLK = PLL_VCO / PLL_P (168MHz)
334 // USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ (48MHz)
335 // For the STM32F407, this equates to a PLL_VCO of
336 // For the Emcraft board, this equates to a PLL_VCO of 336MHz (Sysclock = 168MHz)
337
338 // Configure the main PLL
339 RCC->PLLCFGR = HSIPLL_M | (HSIPLL_N << 6) | (((HSIPLL_P >> 1) -1) << 16) | (HSIPLL_Q << 24);
340
341 // Now that the main configuration is complete, enable the PLL
342 RCC->CR |= RCC_CR_PLLON;
343
344 // Wait until the main PLL has started
345 while((RCC->CR & RCC_CR_PLLRDY) == 0);
346
347 // Configure Flash prefetch, Instruction cache, Data cache and wait state
348 FLASH->ACR &= ~(FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | (FLASH_ACR_LATENCY));
349 FLASH->ACR |= (FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | (0x05 << FLASH_ACR_LATENCY_Pos));
350
351 // FLASH->ACR.b.prften = 1; // Flash prefetch enable
352 // FLASH->ACR.b.icen = 1; // Instruction cache enable
353 // FLASH->ACR.b.dcen = 1; // Data cache enable
354 // FLASH->ACR.b.latency = 0x05; // 5 wait states.
355
356 // Select the main PLL as system clock source
357 RCC->CFGR &= ~(RCC_CFGR_SW_0 | RCC_CFGR_SW_1);
358 RCC->CFGR |= RCC_CFGR_SW_1;
359 // RCC->CFGR.b.sw0 = 0x00;
360 // RCC->CFGR.b.sw1 = 0x01;
361
362 // Wait until the main PLL is used as the system clock source
363 while((RCC->CR & RCC_CR_PLLON) != RCC_CR_PLLON);
364 }
365 {
366 // HSI Oscillator failed to start!
367 }
368 #endif
369
370 #if defined (RTC_ENABLE)
371 volatile unsigned int BackupResetCounter = 0;
372
373 // Enable access to the real time clock (use external low-speed oscillator).
374 PWR->CR |= PWR_CR_DBP;
375
376 // Reset the backup domain interface and delay.
377 RCC->BDCR |= RCC_BDCR_BDRST;
378 for(BackupResetCounter = 0; BackupResetCounter <= 5; BackupResetCounter++);
379 RCC->BDCR &= ~(RCC_BDCR_BDRST);
380 for(BackupResetCounter = 0; BackupResetCounter <= 5; BackupResetCounter++);
381
382 // Clear out the individual bits for the backup domain clocks (including wait states)
383 RCC->BDCR &= ~(RCC_BDCR_RTCEN | RCC_BDCR_RTCSEL | RCC_BDCR_LSEON);
384
385 // Now set the bits of interest.
386 // Enable the real-time clock peripheral.
387 // External LSE as the clock source.
388 // Enable the external oscillator.
389 RCC->BDCR |= RCC_BDCR_RTCEN | (0x01 << RCC_BDCR_RTCSEL_Pos) | RCC_BDCR_LSEON;
390
391 // Wait for the LSE to become ready.
392 while((RCC->BDCR & RCC_BDCR_LSERDY_Msk) != 0x02)
393
394 // Disable access to the backup domain.
395 PWR->CR &= ~(PWR_CR_DBP);
396 #endif
397
398 // Set the variables relating to the system clock speed.
400}
401
439{
440 uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
441
442 /* Get SYSCLK source -------------------------------------------------------*/
443 tmp = RCC->CFGR & RCC_CFGR_SWS;
444
445 switch (tmp)
446 {
447 case 0x00: /* HSI used as system clock source */
448 SystemCoreClock = HSI_VALUE;
449 break;
450 case 0x04: /* HSE used as system clock source */
451 SystemCoreClock = HSE_VALUE;
452 break;
453 case 0x08: /* PLL used as system clock source */
454
455 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
456 SYSCLK = PLL_VCO / PLL_P
457 */
458 pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
459 pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
460
461 if (pllsource != 0)
462 {
463 /* HSE used as PLL clock source */
464 pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
465 }
466 else
467 {
468 /* HSI used as PLL clock source */
469 pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
470 }
471
472 pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
473 SystemCoreClock = pllvco/pllp;
474 break;
475 default:
476 SystemCoreClock = HSI_VALUE;
477 break;
478 }
479 /* Compute HCLK frequency --------------------------------------------------*/
480 /* Get HCLK prescaler */
481 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
482 /* HCLK frequency */
483 SystemCoreClock >>= tmp;
484}
485
486#if defined (DATA_IN_ExtSRAM) && defined (DATA_IN_ExtSDRAM)
487#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
488 || defined(STM32F469xx) || defined(STM32F479xx)
497void SystemInit_ExtMemCtl(void)
498{
499 __IO uint32_t tmp = 0x00;
500
501 register uint32_t tmpreg = 0, timeout = 0xFFFF;
502 register __IO uint32_t index;
503
504 /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface clock */
505 RCC->AHB1ENR |= 0x000001F8;
506
507 /* Delay after an RCC peripheral clock enabling */
508 tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
509
510 /* Connect PDx pins to FMC Alternate function */
511 GPIOD->AFR[0] = 0x00CCC0CC;
512 GPIOD->AFR[1] = 0xCCCCCCCC;
513 /* Configure PDx pins in Alternate function mode */
514 GPIOD->MODER = 0xAAAA0A8A;
515 /* Configure PDx pins speed to 100 MHz */
516 GPIOD->OSPEEDR = 0xFFFF0FCF;
517 /* Configure PDx pins Output type to push-pull */
518 GPIOD->OTYPER = 0x00000000;
519 /* No pull-up, pull-down for PDx pins */
520 GPIOD->PUPDR = 0x00000000;
521
522 /* Connect PEx pins to FMC Alternate function */
523 GPIOE->AFR[0] = 0xC00CC0CC;
524 GPIOE->AFR[1] = 0xCCCCCCCC;
525 /* Configure PEx pins in Alternate function mode */
526 GPIOE->MODER = 0xAAAA828A;
527 /* Configure PEx pins speed to 100 MHz */
528 GPIOE->OSPEEDR = 0xFFFFC3CF;
529 /* Configure PEx pins Output type to push-pull */
530 GPIOE->OTYPER = 0x00000000;
531 /* No pull-up, pull-down for PEx pins */
532 GPIOE->PUPDR = 0x00000000;
533
534 /* Connect PFx pins to FMC Alternate function */
535 GPIOF->AFR[0] = 0xCCCCCCCC;
536 GPIOF->AFR[1] = 0xCCCCCCCC;
537 /* Configure PFx pins in Alternate function mode */
538 GPIOF->MODER = 0xAA800AAA;
539 /* Configure PFx pins speed to 50 MHz */
540 GPIOF->OSPEEDR = 0xAA800AAA;
541 /* Configure PFx pins Output type to push-pull */
542 GPIOF->OTYPER = 0x00000000;
543 /* No pull-up, pull-down for PFx pins */
544 GPIOF->PUPDR = 0x00000000;
545
546 /* Connect PGx pins to FMC Alternate function */
547 GPIOG->AFR[0] = 0xCCCCCCCC;
548 GPIOG->AFR[1] = 0xCCCCCCCC;
549 /* Configure PGx pins in Alternate function mode */
550 GPIOG->MODER = 0xAAAAAAAA;
551 /* Configure PGx pins speed to 50 MHz */
552 GPIOG->OSPEEDR = 0xAAAAAAAA;
553 /* Configure PGx pins Output type to push-pull */
554 GPIOG->OTYPER = 0x00000000;
555 /* No pull-up, pull-down for PGx pins */
556 GPIOG->PUPDR = 0x00000000;
557
558 /* Connect PHx pins to FMC Alternate function */
559 GPIOH->AFR[0] = 0x00C0CC00;
560 GPIOH->AFR[1] = 0xCCCCCCCC;
561 /* Configure PHx pins in Alternate function mode */
562 GPIOH->MODER = 0xAAAA08A0;
563 /* Configure PHx pins speed to 50 MHz */
564 GPIOH->OSPEEDR = 0xAAAA08A0;
565 /* Configure PHx pins Output type to push-pull */
566 GPIOH->OTYPER = 0x00000000;
567 /* No pull-up, pull-down for PHx pins */
568 GPIOH->PUPDR = 0x00000000;
569
570 /* Connect PIx pins to FMC Alternate function */
571 GPIOI->AFR[0] = 0xCCCCCCCC;
572 GPIOI->AFR[1] = 0x00000CC0;
573 /* Configure PIx pins in Alternate function mode */
574 GPIOI->MODER = 0x0028AAAA;
575 /* Configure PIx pins speed to 50 MHz */
576 GPIOI->OSPEEDR = 0x0028AAAA;
577 /* Configure PIx pins Output type to push-pull */
578 GPIOI->OTYPER = 0x00000000;
579 /* No pull-up, pull-down for PIx pins */
580 GPIOI->PUPDR = 0x00000000;
581
582/*-- FMC Configuration -------------------------------------------------------*/
583 /* Enable the FMC interface clock */
584 RCC->AHB3ENR |= 0x00000001;
585 /* Delay after an RCC peripheral clock enabling */
586 tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
587
588 FMC_Bank5_6->SDCR[0] = 0x000019E4;
589 FMC_Bank5_6->SDTR[0] = 0x01115351;
590
591 /* SDRAM initialization sequence */
592 /* Clock enable command */
593 FMC_Bank5_6->SDCMR = 0x00000011;
594 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
595 while((tmpreg != 0) && (timeout-- > 0))
596 {
597 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
598 }
599
600 /* Delay */
601 for (index = 0; index<1000; index++);
602
603 /* PALL command */
604 FMC_Bank5_6->SDCMR = 0x00000012;
605 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
606 timeout = 0xFFFF;
607 while((tmpreg != 0) && (timeout-- > 0))
608 {
609 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
610 }
611
612 /* Auto refresh command */
613 FMC_Bank5_6->SDCMR = 0x00000073;
614 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
615 timeout = 0xFFFF;
616 while((tmpreg != 0) && (timeout-- > 0))
617 {
618 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
619 }
620
621 /* MRD register program */
622 FMC_Bank5_6->SDCMR = 0x00046014;
623 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
624 timeout = 0xFFFF;
625 while((tmpreg != 0) && (timeout-- > 0))
626 {
627 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
628 }
629
630 /* Set refresh count */
631 tmpreg = FMC_Bank5_6->SDRTR;
632 FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
633
634 /* Disable write protection */
635 tmpreg = FMC_Bank5_6->SDCR[0];
636 FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
637
638#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
639 /* Configure and enable Bank1_SRAM2 */
640 FMC_Bank1->BTCR[2] = 0x00001011;
641 FMC_Bank1->BTCR[3] = 0x00000201;
642 FMC_Bank1E->BWTR[2] = 0x0fffffff;
643#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
644#if defined(STM32F469xx) || defined(STM32F479xx)
645 /* Configure and enable Bank1_SRAM2 */
646 FMC_Bank1->BTCR[2] = 0x00001091;
647 FMC_Bank1->BTCR[3] = 0x00110212;
648 FMC_Bank1E->BWTR[2] = 0x0fffffff;
649#endif /* STM32F469xx || STM32F479xx */
650
651 (void)(tmp);
652}
653#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
654#elif defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
663void SystemInit_ExtMemCtl(void)
664{
665 __IO uint32_t tmp = 0x00;
666#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
667 || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
668#if defined (DATA_IN_ExtSDRAM)
669 register uint32_t tmpreg = 0, timeout = 0xFFFF;
670 register __IO uint32_t index;
671
672#if defined(STM32F446xx)
673 /* Enable GPIOA, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG interface
674 clock */
675 RCC->AHB1ENR |= 0x0000007D;
676#else
677 /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface
678 clock */
679 RCC->AHB1ENR |= 0x000001F8;
680#endif /* STM32F446xx */
681 /* Delay after an RCC peripheral clock enabling */
682 tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
683
684#if defined(STM32F446xx)
685 /* Connect PAx pins to FMC Alternate function */
686 GPIOA->AFR[0] |= 0xC0000000;
687 GPIOA->AFR[1] |= 0x00000000;
688 /* Configure PDx pins in Alternate function mode */
689 GPIOA->MODER |= 0x00008000;
690 /* Configure PDx pins speed to 50 MHz */
691 GPIOA->OSPEEDR |= 0x00008000;
692 /* Configure PDx pins Output type to push-pull */
693 GPIOA->OTYPER |= 0x00000000;
694 /* No pull-up, pull-down for PDx pins */
695 GPIOA->PUPDR |= 0x00000000;
696
697 /* Connect PCx pins to FMC Alternate function */
698 GPIOC->AFR[0] |= 0x00CC0000;
699 GPIOC->AFR[1] |= 0x00000000;
700 /* Configure PDx pins in Alternate function mode */
701 GPIOC->MODER |= 0x00000A00;
702 /* Configure PDx pins speed to 50 MHz */
703 GPIOC->OSPEEDR |= 0x00000A00;
704 /* Configure PDx pins Output type to push-pull */
705 GPIOC->OTYPER |= 0x00000000;
706 /* No pull-up, pull-down for PDx pins */
707 GPIOC->PUPDR |= 0x00000000;
708#endif /* STM32F446xx */
709
710 /* Connect PDx pins to FMC Alternate function */
711 GPIOD->AFR[0] = 0x000000CC;
712 GPIOD->AFR[1] = 0xCC000CCC;
713 /* Configure PDx pins in Alternate function mode */
714 GPIOD->MODER = 0xA02A000A;
715 /* Configure PDx pins speed to 50 MHz */
716 GPIOD->OSPEEDR = 0xA02A000A;
717 /* Configure PDx pins Output type to push-pull */
718 GPIOD->OTYPER = 0x00000000;
719 /* No pull-up, pull-down for PDx pins */
720 GPIOD->PUPDR = 0x00000000;
721
722 /* Connect PEx pins to FMC Alternate function */
723 GPIOE->AFR[0] = 0xC00000CC;
724 GPIOE->AFR[1] = 0xCCCCCCCC;
725 /* Configure PEx pins in Alternate function mode */
726 GPIOE->MODER = 0xAAAA800A;
727 /* Configure PEx pins speed to 50 MHz */
728 GPIOE->OSPEEDR = 0xAAAA800A;
729 /* Configure PEx pins Output type to push-pull */
730 GPIOE->OTYPER = 0x00000000;
731 /* No pull-up, pull-down for PEx pins */
732 GPIOE->PUPDR = 0x00000000;
733
734 /* Connect PFx pins to FMC Alternate function */
735 GPIOF->AFR[0] = 0xCCCCCCCC;
736 GPIOF->AFR[1] = 0xCCCCCCCC;
737 /* Configure PFx pins in Alternate function mode */
738 GPIOF->MODER = 0xAA800AAA;
739 /* Configure PFx pins speed to 50 MHz */
740 GPIOF->OSPEEDR = 0xAA800AAA;
741 /* Configure PFx pins Output type to push-pull */
742 GPIOF->OTYPER = 0x00000000;
743 /* No pull-up, pull-down for PFx pins */
744 GPIOF->PUPDR = 0x00000000;
745
746 /* Connect PGx pins to FMC Alternate function */
747 GPIOG->AFR[0] = 0xCCCCCCCC;
748 GPIOG->AFR[1] = 0xCCCCCCCC;
749 /* Configure PGx pins in Alternate function mode */
750 GPIOG->MODER = 0xAAAAAAAA;
751 /* Configure PGx pins speed to 50 MHz */
752 GPIOG->OSPEEDR = 0xAAAAAAAA;
753 /* Configure PGx pins Output type to push-pull */
754 GPIOG->OTYPER = 0x00000000;
755 /* No pull-up, pull-down for PGx pins */
756 GPIOG->PUPDR = 0x00000000;
757
758#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
759 || defined(STM32F469xx) || defined(STM32F479xx)
760 /* Connect PHx pins to FMC Alternate function */
761 GPIOH->AFR[0] = 0x00C0CC00;
762 GPIOH->AFR[1] = 0xCCCCCCCC;
763 /* Configure PHx pins in Alternate function mode */
764 GPIOH->MODER = 0xAAAA08A0;
765 /* Configure PHx pins speed to 50 MHz */
766 GPIOH->OSPEEDR = 0xAAAA08A0;
767 /* Configure PHx pins Output type to push-pull */
768 GPIOH->OTYPER = 0x00000000;
769 /* No pull-up, pull-down for PHx pins */
770 GPIOH->PUPDR = 0x00000000;
771
772 /* Connect PIx pins to FMC Alternate function */
773 GPIOI->AFR[0] = 0xCCCCCCCC;
774 GPIOI->AFR[1] = 0x00000CC0;
775 /* Configure PIx pins in Alternate function mode */
776 GPIOI->MODER = 0x0028AAAA;
777 /* Configure PIx pins speed to 50 MHz */
778 GPIOI->OSPEEDR = 0x0028AAAA;
779 /* Configure PIx pins Output type to push-pull */
780 GPIOI->OTYPER = 0x00000000;
781 /* No pull-up, pull-down for PIx pins */
782 GPIOI->PUPDR = 0x00000000;
783#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
784
785/*-- FMC Configuration -------------------------------------------------------*/
786 /* Enable the FMC interface clock */
787 RCC->AHB3ENR |= 0x00000001;
788 /* Delay after an RCC peripheral clock enabling */
789 tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
790
791 /* Configure and enable SDRAM bank1 */
792#if defined(STM32F446xx)
793 FMC_Bank5_6->SDCR[0] = 0x00001954;
794#else
795 FMC_Bank5_6->SDCR[0] = 0x000019E4;
796#endif /* STM32F446xx */
797 FMC_Bank5_6->SDTR[0] = 0x01115351;
798
799 /* SDRAM initialization sequence */
800 /* Clock enable command */
801 FMC_Bank5_6->SDCMR = 0x00000011;
802 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
803 while((tmpreg != 0) && (timeout-- > 0))
804 {
805 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
806 }
807
808 /* Delay */
809 for (index = 0; index<1000; index++);
810
811 /* PALL command */
812 FMC_Bank5_6->SDCMR = 0x00000012;
813 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
814 timeout = 0xFFFF;
815 while((tmpreg != 0) && (timeout-- > 0))
816 {
817 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
818 }
819
820 /* Auto refresh command */
821#if defined(STM32F446xx)
822 FMC_Bank5_6->SDCMR = 0x000000F3;
823#else
824 FMC_Bank5_6->SDCMR = 0x00000073;
825#endif /* STM32F446xx */
826 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
827 timeout = 0xFFFF;
828 while((tmpreg != 0) && (timeout-- > 0))
829 {
830 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
831 }
832
833 /* MRD register program */
834#if defined(STM32F446xx)
835 FMC_Bank5_6->SDCMR = 0x00044014;
836#else
837 FMC_Bank5_6->SDCMR = 0x00046014;
838#endif /* STM32F446xx */
839 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
840 timeout = 0xFFFF;
841 while((tmpreg != 0) && (timeout-- > 0))
842 {
843 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
844 }
845
846 /* Set refresh count */
847 tmpreg = FMC_Bank5_6->SDRTR;
848#if defined(STM32F446xx)
849 FMC_Bank5_6->SDRTR = (tmpreg | (0x0000050C<<1));
850#else
851 FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
852#endif /* STM32F446xx */
853
854 /* Disable write protection */
855 tmpreg = FMC_Bank5_6->SDCR[0];
856 FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
857#endif /* DATA_IN_ExtSDRAM */
858#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx */
859
860#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\
861 || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
862 || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx)
863
864#if defined(DATA_IN_ExtSRAM)
865/*-- GPIOs Configuration -----------------------------------------------------*/
866 /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
867 RCC->AHB1ENR |= 0x00000078;
868 /* Delay after an RCC peripheral clock enabling */
869 tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN);
870
871 /* Connect PDx pins to FMC Alternate function */
872 GPIOD->AFR[0] = 0x00CCC0CC;
873 GPIOD->AFR[1] = 0xCCCCCCCC;
874 /* Configure PDx pins in Alternate function mode */
875 GPIOD->MODER = 0xAAAA0A8A;
876 /* Configure PDx pins speed to 100 MHz */
877 GPIOD->OSPEEDR = 0xFFFF0FCF;
878 /* Configure PDx pins Output type to push-pull */
879 GPIOD->OTYPER = 0x00000000;
880 /* No pull-up, pull-down for PDx pins */
881 GPIOD->PUPDR = 0x00000000;
882
883 /* Connect PEx pins to FMC Alternate function */
884 GPIOE->AFR[0] = 0xC00CC0CC;
885 GPIOE->AFR[1] = 0xCCCCCCCC;
886 /* Configure PEx pins in Alternate function mode */
887 GPIOE->MODER = 0xAAAA828A;
888 /* Configure PEx pins speed to 100 MHz */
889 GPIOE->OSPEEDR = 0xFFFFC3CF;
890 /* Configure PEx pins Output type to push-pull */
891 GPIOE->OTYPER = 0x00000000;
892 /* No pull-up, pull-down for PEx pins */
893 GPIOE->PUPDR = 0x00000000;
894
895 /* Connect PFx pins to FMC Alternate function */
896 GPIOF->AFR[0] = 0x00CCCCCC;
897 GPIOF->AFR[1] = 0xCCCC0000;
898 /* Configure PFx pins in Alternate function mode */
899 GPIOF->MODER = 0xAA000AAA;
900 /* Configure PFx pins speed to 100 MHz */
901 GPIOF->OSPEEDR = 0xFF000FFF;
902 /* Configure PFx pins Output type to push-pull */
903 GPIOF->OTYPER = 0x00000000;
904 /* No pull-up, pull-down for PFx pins */
905 GPIOF->PUPDR = 0x00000000;
906
907 /* Connect PGx pins to FMC Alternate function */
908 GPIOG->AFR[0] = 0x00CCCCCC;
909 GPIOG->AFR[1] = 0x000000C0;
910 /* Configure PGx pins in Alternate function mode */
911 GPIOG->MODER = 0x00085AAA;
912 /* Configure PGx pins speed to 100 MHz */
913 GPIOG->OSPEEDR = 0x000CAFFF;
914 /* Configure PGx pins Output type to push-pull */
915 GPIOG->OTYPER = 0x00000000;
916 /* No pull-up, pull-down for PGx pins */
917 GPIOG->PUPDR = 0x00000000;
918
919/*-- FMC/FSMC Configuration --------------------------------------------------*/
920 /* Enable the FMC/FSMC interface clock */
921 RCC->AHB3ENR |= 0x00000001;
922
923#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
924 /* Delay after an RCC peripheral clock enabling */
925 tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
926 /* Configure and enable Bank1_SRAM2 */
927 FMC_Bank1->BTCR[2] = 0x00001011;
928 FMC_Bank1->BTCR[3] = 0x00000201;
929 FMC_Bank1E->BWTR[2] = 0x0fffffff;
930#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
931#if defined(STM32F469xx) || defined(STM32F479xx)
932 /* Delay after an RCC peripheral clock enabling */
933 tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
934 /* Configure and enable Bank1_SRAM2 */
935 FMC_Bank1->BTCR[2] = 0x00001091;
936 FMC_Bank1->BTCR[3] = 0x00110212;
937 FMC_Bank1E->BWTR[2] = 0x0fffffff;
938#endif /* STM32F469xx || STM32F479xx */
939#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx)\
940 || defined(STM32F412Zx) || defined(STM32F412Vx)
941 /* Delay after an RCC peripheral clock enabling */
942 tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FSMCEN);
943 /* Configure and enable Bank1_SRAM2 */
944 FSMC_Bank1->BTCR[2] = 0x00001011;
945 FSMC_Bank1->BTCR[3] = 0x00000201;
946 FSMC_Bank1E->BWTR[2] = 0x0FFFFFFF;
947#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F412Zx || STM32F412Vx */
948
949#endif /* DATA_IN_ExtSRAM */
950#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
951 STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx */
952 (void)(tmp);
953}
954#endif /* DATA_IN_ExtSRAM && DATA_IN_ExtSDRAM */
958
962
#define RCC_CFGR_SWS
#define RCC_BDCR_LSERDY_Msk
#define RCC_CFGR_PPRE1
#define RCC_CFGR_SW_1
#define RCC_CFGR_SW_0
#define PWR_CR_VOS
#define RCC_CFGR_PPRE2
#define PWR_CR_DBP
#define RCC_CFGR_HPRE
void SystemInit(void)
Setup the microcontroller system Initialize the FPU setting, vector table location and External memor...
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
const uint8_t APBPrescTable[8]
const uint8_t AHBPrescTable[16]
uint32_t SystemCoreClock
CMSIS STM32F4xx Device Peripheral Access Layer Header File.