2 ******************************************************************************
3 * @file system_stm32f4xx.c
4 * @author MCD Application Team
6 * @date 21-October-2015
7 * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File.
8 * This file contains the system clock configuration for STM32F4xx devices.
10 * 1. This file provides two functions and one global variable to be called from
12 * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
13 * and Divider factors, AHB/APBx prescalers and Flash settings),
14 * depending on the configuration made in the clock xls tool.
15 * This function is called at startup just after reset and
16 * before branch to main program. This call is made inside
17 * the "startup_stm32f4xx.s" file.
19 * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
20 * by the user application to setup the SysTick
21 * timer or configure other parameters.
23 * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
24 * be called whenever the core clock is changed
25 * during program execution.
27 * 2. After each device reset the HSI (16 MHz) is used as system clock source.
28 * Then SystemInit() function is called, in "startup_stm32f4xx.s" file, to
29 * configure the system clock before to branch to main program.
31 * 3. If the system clock source selected by user fails to startup, the SystemInit()
32 * function will do nothing and HSI still used as system clock source. User can
33 * add some code to deal with this issue inside the SetSysClock() function.
35 * 4. The default value of HSE crystal is set to 25MHz, refer to "HSE_VALUE" define
36 * in "stm32f4xx.h" file. When HSE is used as system clock source, directly or
37 * through PLL, and you are using different crystal you have to adapt the HSE
38 * value to your own configuration.
40 * 5. This file configures the system clock as follows:
41 *=============================================================================
42 *=============================================================================
43 * Supported STM32F40xxx/41xxx devices
44 *-----------------------------------------------------------------------------
45 * System Clock source | PLL (HSE)
46 *-----------------------------------------------------------------------------
47 * SYSCLK(Hz) | 168000000
48 *-----------------------------------------------------------------------------
49 * HCLK(Hz) | 168000000
50 *-----------------------------------------------------------------------------
52 *-----------------------------------------------------------------------------
54 *-----------------------------------------------------------------------------
56 *-----------------------------------------------------------------------------
57 * HSE Frequency(Hz) | 8000000
58 *-----------------------------------------------------------------------------
60 *-----------------------------------------------------------------------------
62 *-----------------------------------------------------------------------------
64 *-----------------------------------------------------------------------------
66 *-----------------------------------------------------------------------------
68 *-----------------------------------------------------------------------------
70 *-----------------------------------------------------------------------------
71 * I2S input clock | NA
72 *-----------------------------------------------------------------------------
74 *-----------------------------------------------------------------------------
75 * Main regulator output voltage | Scale1 mode
76 *-----------------------------------------------------------------------------
77 * Flash Latency(WS) | 5
78 *-----------------------------------------------------------------------------
79 * Prefetch Buffer | ON
80 *-----------------------------------------------------------------------------
81 * Instruction cache | ON
82 *-----------------------------------------------------------------------------
84 *-----------------------------------------------------------------------------
85 * Require 48MHz for USB OTG FS, | Disabled
86 * SDIO and RNG clock |
87 *-----------------------------------------------------------------------------
88 *=============================================================================
89 *=============================================================================
90 * Supported STM32F42xxx/43xxx devices
91 *-----------------------------------------------------------------------------
92 * System Clock source | PLL (HSE)
93 *-----------------------------------------------------------------------------
94 * SYSCLK(Hz) | 180000000
95 *-----------------------------------------------------------------------------
96 * HCLK(Hz) | 180000000
97 *-----------------------------------------------------------------------------
99 *-----------------------------------------------------------------------------
101 *-----------------------------------------------------------------------------
103 *-----------------------------------------------------------------------------
104 * HSE Frequency(Hz) | 25000000
105 *-----------------------------------------------------------------------------
107 *-----------------------------------------------------------------------------
109 *-----------------------------------------------------------------------------
111 *-----------------------------------------------------------------------------
113 *-----------------------------------------------------------------------------
115 *-----------------------------------------------------------------------------
117 *-----------------------------------------------------------------------------
118 * I2S input clock | NA
119 *-----------------------------------------------------------------------------
121 *-----------------------------------------------------------------------------
122 * Main regulator output voltage | Scale1 mode
123 *-----------------------------------------------------------------------------
124 * Flash Latency(WS) | 5
125 *-----------------------------------------------------------------------------
126 * Prefetch Buffer | ON
127 *-----------------------------------------------------------------------------
128 * Instruction cache | ON
129 *-----------------------------------------------------------------------------
131 *-----------------------------------------------------------------------------
132 * Require 48MHz for USB OTG FS, | Disabled
133 * SDIO and RNG clock |
134 *-----------------------------------------------------------------------------
135 *=============================================================================
136 *=============================================================================
137 * Supported STM32F401xx devices
138 *-----------------------------------------------------------------------------
139 * System Clock source | PLL (HSE)
140 *-----------------------------------------------------------------------------
141 * SYSCLK(Hz) | 84000000
142 *-----------------------------------------------------------------------------
143 * HCLK(Hz) | 84000000
144 *-----------------------------------------------------------------------------
146 *-----------------------------------------------------------------------------
148 *-----------------------------------------------------------------------------
150 *-----------------------------------------------------------------------------
151 * HSE Frequency(Hz) | 25000000
152 *-----------------------------------------------------------------------------
154 *-----------------------------------------------------------------------------
156 *-----------------------------------------------------------------------------
158 *-----------------------------------------------------------------------------
160 *-----------------------------------------------------------------------------
162 *-----------------------------------------------------------------------------
164 *-----------------------------------------------------------------------------
165 * I2S input clock | NA
166 *-----------------------------------------------------------------------------
168 *-----------------------------------------------------------------------------
169 * Main regulator output voltage | Scale1 mode
170 *-----------------------------------------------------------------------------
171 * Flash Latency(WS) | 2
172 *-----------------------------------------------------------------------------
173 * Prefetch Buffer | ON
174 *-----------------------------------------------------------------------------
175 * Instruction cache | ON
176 *-----------------------------------------------------------------------------
178 *-----------------------------------------------------------------------------
179 * Require 48MHz for USB OTG FS, | Disabled
180 * SDIO and RNG clock |
181 *-----------------------------------------------------------------------------
182 *=============================================================================
183 *=============================================================================
184 * Supported STM32F411xx/STM32F410xx devices
185 *-----------------------------------------------------------------------------
186 * System Clock source | PLL (HSI)
187 *-----------------------------------------------------------------------------
188 * SYSCLK(Hz) | 100000000
189 *-----------------------------------------------------------------------------
190 * HCLK(Hz) | 100000000
191 *-----------------------------------------------------------------------------
193 *-----------------------------------------------------------------------------
195 *-----------------------------------------------------------------------------
197 *-----------------------------------------------------------------------------
198 * HSI Frequency(Hz) | 16000000
199 *-----------------------------------------------------------------------------
201 *-----------------------------------------------------------------------------
203 *-----------------------------------------------------------------------------
205 *-----------------------------------------------------------------------------
207 *-----------------------------------------------------------------------------
209 *-----------------------------------------------------------------------------
211 *-----------------------------------------------------------------------------
212 * I2S input clock | NA
213 *-----------------------------------------------------------------------------
215 *-----------------------------------------------------------------------------
216 * Main regulator output voltage | Scale1 mode
217 *-----------------------------------------------------------------------------
218 * Flash Latency(WS) | 3
219 *-----------------------------------------------------------------------------
220 * Prefetch Buffer | ON
221 *-----------------------------------------------------------------------------
222 * Instruction cache | ON
223 *-----------------------------------------------------------------------------
225 *-----------------------------------------------------------------------------
226 * Require 48MHz for USB OTG FS, | Disabled
227 * SDIO and RNG clock |
228 *-----------------------------------------------------------------------------
229 *=============================================================================
230 *=============================================================================
231 * Supported STM32F446xx devices
232 *-----------------------------------------------------------------------------
233 * System Clock source | PLL (HSE)
234 *-----------------------------------------------------------------------------
235 * SYSCLK(Hz) | 180000000
236 *-----------------------------------------------------------------------------
237 * HCLK(Hz) | 180000000
238 *-----------------------------------------------------------------------------
240 *-----------------------------------------------------------------------------
242 *-----------------------------------------------------------------------------
244 *-----------------------------------------------------------------------------
245 * HSE Frequency(Hz) | 8000000
246 *-----------------------------------------------------------------------------
248 *-----------------------------------------------------------------------------
250 *-----------------------------------------------------------------------------
252 *-----------------------------------------------------------------------------
254 *-----------------------------------------------------------------------------
256 *-----------------------------------------------------------------------------
258 *-----------------------------------------------------------------------------
260 *-----------------------------------------------------------------------------
262 *-----------------------------------------------------------------------------
264 *-----------------------------------------------------------------------------
266 *-----------------------------------------------------------------------------
267 * I2S input clock | NA
268 *-----------------------------------------------------------------------------
270 *-----------------------------------------------------------------------------
271 * Main regulator output voltage | Scale1 mode
272 *-----------------------------------------------------------------------------
273 * Flash Latency(WS) | 5
274 *-----------------------------------------------------------------------------
275 * Prefetch Buffer | ON
276 *-----------------------------------------------------------------------------
277 * Instruction cache | ON
278 *-----------------------------------------------------------------------------
280 *-----------------------------------------------------------------------------
281 * Require 48MHz for USB OTG FS, | Disabled
282 * SDIO and RNG clock |
283 *-----------------------------------------------------------------------------
284 *=============================================================================
285 ******************************************************************************
288 * <h2><center>© COPYRIGHT 2015 STMicroelectronics</center></h2>
290 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
291 * You may not use this file except in compliance with the License.
292 * You may obtain a copy of the License at:
294 * http://www.st.com/software_license_agreement_liberty_v2
296 * Unless required by applicable law or agreed to in writing, software
297 * distributed under the License is distributed on an "AS IS" BASIS,
298 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
299 * See the License for the specific language governing permissions and
300 * limitations under the License.
302 ******************************************************************************
305 /** @addtogroup CMSIS
309 /** @addtogroup stm32f4xx_system
313 /** @addtogroup STM32F4xx_System_Private_Includes
317 #include "stm32f4xx.h"
319 #include "system_stm32f4xx.h"
320 #include "drivers/system.h"
322 uint32_t hse_value
= HSE_VALUE
;
328 /** @addtogroup STM32F4xx_System_Private_TypesDefinitions
336 /** @addtogroup STM32F4xx_System_Private_Defines
340 /************************* Miscellaneous Configuration ************************/
341 /*!< Uncomment the following line if you need to use external SRAM or SDRAM mounted
342 on STM324xG_EVAL/STM324x7I_EVAL/STM324x9I_EVAL boards as data memory */
343 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx)
344 /* #define DATA_IN_ExtSRAM */
345 #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx || STM32F469_479xx */
347 #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx) || defined(STM32F469_479xx)
348 /* #define DATA_IN_ExtSDRAM */
349 #endif /* STM32F427_437x || STM32F429_439xx || STM32F446xx || STM32F469_479xx */
351 #if defined(STM32F410xx) || defined(STM32F411xE)
352 /*!< Uncomment the following line if you need to clock the STM32F410xx/STM32F411xE by HSE Bypass
353 through STLINK MCO pin of STM32F103 microcontroller. The frequency cannot be changed
354 and is fixed at 8 MHz.
355 Hardware configuration needed for Nucleo Board:
359 /* #define USE_HSE_BYPASS */
361 #if defined(USE_HSE_BYPASS)
362 #define HSE_BYPASS_INPUT_FREQUENCY 8000000
363 #endif /* USE_HSE_BYPASS */
364 #endif /* STM32F410xx || STM32F411xE */
366 /*!< Uncomment the following line if you need to relocate your vector Table in
368 /* #define VECT_TAB_SRAM */
369 #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
370 This value must be a multiple of 0x200. */
371 /******************************************************************************/
373 /************************* PLL Parameters *************************************/
374 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx) || defined(STM32F469_479xx) || defined (STM32F446xx) || defined (STM32F410xx) || defined (STM32F411xE)
375 #if HSE_VALUE == 25000000
377 #elif HSE_VALUE == 24000000
379 #elif HSE_VALUE == 16000000
381 #elif HSE_VALUE == 8000000
384 #error Invalid HSE_VALUE
388 #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx || STM32F469_479xx */
390 #if defined(STM32F446xx)
391 /* PLL division factor for I2S, SAI, SYSTEM and SPDIF: Clock = PLL_VCO / PLLR */
393 #endif /* STM32F446xx */
395 #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx) || defined(STM32F469_479xx)
397 /* SYSCLK = PLL_VCO / PLL_P */
399 /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
401 #endif /* STM32F427_437x || STM32F429_439xx || STM32F446xx || STM32F469_479xx */
403 #if defined (STM32F40_41xxx)
405 /* SYSCLK = PLL_VCO / PLL_P */
407 /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
409 #endif /* STM32F40_41xxx */
411 #if defined(STM32F401xx)
413 /* SYSCLK = PLL_VCO / PLL_P */
415 /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
417 #endif /* STM32F401xx */
419 #if defined(STM32F410xx) || defined(STM32F411xE)
421 /* SYSCLK = PLL_VCO / PLL_P */
423 /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
425 #endif /* STM32F410xx || STM32F411xE */
427 /******************************************************************************/
433 /** @addtogroup STM32F4xx_System_Private_Macros
441 /** @addtogroup STM32F4xx_System_Private_Variables
445 /* core clock is simply a mhz of PLL_N / PLL_P */
446 uint32_t SystemCoreClock
= 1000000 * PLL_N
/ PLL_P
;
448 __I
uint8_t AHBPrescTable
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
454 /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
458 void SetSysClock(void);
460 #if defined(DATA_IN_ExtSRAM) || defined(DATA_IN_ExtSDRAM)
461 static void SystemInit_ExtMemCtl(void);
462 #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
468 /** @addtogroup STM32F4xx_System_Private_Functions
473 * @brief Setup the microcontroller system
474 * Initialize the Embedded Flash Interface, the PLL and update the
475 * SystemFrequency variable.
479 void SystemInit(void)
481 initialiseMemorySections();
483 /* FPU settings ------------------------------------------------------------*/
484 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
485 SCB
->CPACR
|= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
487 /* Reset the RCC clock configuration to the default reset state ------------*/
489 RCC
->CR
|= (uint32_t)0x00000001;
491 /* Reset CFGR register */
492 RCC
->CFGR
= 0x00000000;
494 /* Reset HSEON, CSSON and PLLON bits */
495 RCC
->CR
&= (uint32_t)0xFEF6FFFF;
497 /* Reset PLLCFGR register */
498 RCC
->PLLCFGR
= 0x24003010;
500 /* Reset HSEBYP bit */
501 RCC
->CR
&= (uint32_t)0xFFFBFFFF;
503 /* Disable all interrupts */
504 RCC
->CIR
= 0x00000000;
506 #if defined(DATA_IN_ExtSRAM) || defined(DATA_IN_ExtSDRAM)
507 SystemInit_ExtMemCtl();
508 #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
510 /* Configure the System clock source, PLL Multiplier and Divider factors,
511 AHB/APBx prescalers and Flash settings ----------------------------------*/
514 /* Configure the Vector Table location add offset address ------------------*/
516 SCB
->VTOR
= SRAM_BASE
| VECT_TAB_OFFSET
; /* Vector Table Relocation in Internal SRAM */
518 SCB
->VTOR
= (uint32_t) &isr_vector_table_base
; /* Vector Table Relocation in Internal FLASH */
523 * @brief Update SystemCoreClock variable according to Clock Register Values.
524 * The SystemCoreClock variable contains the core clock (HCLK), it can
525 * be used by the user application to setup the SysTick timer or configure
528 * @note Each time the core clock (HCLK) changes, this function must be called
529 * to update SystemCoreClock variable value. Otherwise, any configuration
530 * based on this variable will be incorrect.
532 * @note - The system frequency computed by this function is not the real
533 * frequency in the chip. It is calculated based on the predefined
534 * constant and the selected clock source:
536 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
538 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
540 * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
541 * or HSI_VALUE(*) multiplied/divided by the PLL factors.
543 * (*) HSI_VALUE is a constant defined in stm32f4xx.h file (default value
544 * 16 MHz) but the real value may vary depending on the variations
545 * in voltage and temperature.
547 * (**) HSE_VALUE is a constant defined in stm32f4xx.h file (default value
548 * 25 MHz), user has to ensure that HSE_VALUE is same as the real
549 * frequency of the crystal used. Otherwise, this function may
552 * - The result of this function could be not correct when using fractional
553 * value for HSE crystal.
558 void SystemCoreClockUpdate(void)
560 uint32_t tmp
= 0, pllvco
= 0, pllp
= 2, pllsource
= 0, pllm
= 2;
561 #if defined(STM32F446xx)
563 #endif /* STM32F446xx */
564 /* Get SYSCLK source -------------------------------------------------------*/
565 tmp
= RCC
->CFGR
& RCC_CFGR_SWS
;
569 case 0x00: /* HSI used as system clock source */
570 SystemCoreClock
= HSI_VALUE
;
572 case 0x04: /* HSE used as system clock source */
573 SystemCoreClock
= HSE_VALUE
;
575 case 0x08: /* PLL P used as system clock source */
576 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
577 SYSCLK = PLL_VCO / PLL_P
579 pllsource
= (RCC
->PLLCFGR
& RCC_PLLCFGR_PLLSRC
) >> 22;
580 pllm
= RCC
->PLLCFGR
& RCC_PLLCFGR_PLLM
;
584 /* HSE used as PLL clock source */
585 pllvco
= (HSE_VALUE
/ pllm
) * ((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLN
) >> 6);
589 /* HSI used as PLL clock source */
590 pllvco
= (HSI_VALUE
/ pllm
) * ((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLN
) >> 6);
593 pllp
= (((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLP
) >>16) + 1 ) *2;
594 SystemCoreClock
= pllvco
/pllp
;
596 #if defined(STM32F446xx)
597 case 0x0C: /* PLL R used as system clock source */
598 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
599 SYSCLK = PLL_VCO / PLL_R
601 pllsource
= (RCC
->PLLCFGR
& RCC_PLLCFGR_PLLSRC
) >> 22;
602 pllm
= RCC
->PLLCFGR
& RCC_PLLCFGR_PLLM
;
605 /* HSE used as PLL clock source */
606 pllvco
= (HSE_VALUE
/ pllm
) * ((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLN
) >> 6);
610 /* HSI used as PLL clock source */
611 pllvco
= (HSI_VALUE
/ pllm
) * ((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLN
) >> 6);
614 pllr
= (((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLR
) >>28) + 1 ) *2;
615 SystemCoreClock
= pllvco
/pllr
;
617 #endif /* STM32F446xx */
619 SystemCoreClock
= HSI_VALUE
;
622 /* Compute HCLK frequency --------------------------------------------------*/
623 /* Get HCLK prescaler */
624 tmp
= AHBPrescTable
[((RCC
->CFGR
& RCC_CFGR_HPRE
) >> 4)];
626 SystemCoreClock
>>= tmp
;
630 * @brief Configures the System clock source, PLL Multiplier and Divider factors,
631 * AHB/APBx prescalers and Flash settings
632 * @Note This function should be called only once the RCC clock configuration
633 * is reset to the default reset state (done in SystemInit() function).
637 void SetSysClock(void)
639 /******************************************************************************/
640 /* PLL (clocked by HSE) used as System clock source */
641 /******************************************************************************/
642 __IO
uint32_t StartUpCounter
= 0, HSEStatus
= 0;
645 RCC
->CR
|= ((uint32_t)RCC_CR_HSEON
);
647 /* Wait till HSE is ready and if Time out is reached exit */
650 HSEStatus
= RCC
->CR
& RCC_CR_HSERDY
;
652 } while ((HSEStatus
== 0) && (StartUpCounter
!= HSE_STARTUP_TIMEOUT
));
654 if ((RCC
->CR
& RCC_CR_HSERDY
) != RESET
)
656 HSEStatus
= (uint32_t)0x01;
660 HSEStatus
= (uint32_t)0x00;
663 if (HSEStatus
== (uint32_t)0x01)
665 /* Select regulator voltage output Scale 1 mode */
666 RCC
->APB1ENR
|= RCC_APB1ENR_PWREN
;
667 PWR
->CR
|= PWR_CR_VOS
;
669 /* HCLK = SYSCLK / 1*/
670 RCC
->CFGR
|= RCC_CFGR_HPRE_DIV1
;
672 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx) || defined(STM32F469_479xx)
673 /* PCLK2 = HCLK / 2*/
674 RCC
->CFGR
|= RCC_CFGR_PPRE2_DIV2
;
676 /* PCLK1 = HCLK / 4*/
677 RCC
->CFGR
|= RCC_CFGR_PPRE1_DIV4
;
678 #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx || STM32F446xx || STM32F469_479xx */
680 #if defined(STM32F401xx)
681 /* PCLK2 = HCLK / 2*/
682 RCC
->CFGR
|= RCC_CFGR_PPRE2_DIV1
;
684 /* PCLK1 = HCLK / 4*/
685 RCC
->CFGR
|= RCC_CFGR_PPRE1_DIV2
;
686 #endif /* STM32F401xx */
688 #if defined(STM32F410xx) || defined(STM32F411xE)
689 /* PCLK2 = HCLK / 2*/
690 RCC
->CFGR
|= RCC_CFGR_PPRE2_DIV1
;
692 /* PCLK1 = HCLK / 4*/
693 RCC
->CFGR
|= RCC_CFGR_PPRE1_DIV2
;
694 #endif /* STM32F410xx || STM32F411xE */
696 #if defined(STM32F446xx)
697 /* Configure the main PLL */
698 RCC
->PLLCFGR
= PLL_M
| (PLL_N
<< 6) | (((PLL_P
>> 1) -1) << 16) |
699 (RCC_PLLCFGR_PLLSRC_HSE
) | (PLL_Q
<< 24) | (PLL_R
<< 28);
701 /* Configure the main PLL */
702 RCC
->PLLCFGR
= PLL_M
| (PLL_N
<< 6) | (((PLL_P
>> 1) -1) << 16) |
703 (RCC_PLLCFGR_PLLSRC_HSE
) | (PLL_Q
<< 24);
704 #endif /* STM32F446xx */
706 /* Enable the main PLL */
707 RCC
->CR
|= RCC_CR_PLLON
;
709 /* Wait till the main PLL is ready */
710 while ((RCC
->CR
& RCC_CR_PLLRDY
) == 0)
714 #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx) || defined(STM32F469_479xx)
715 /* Enable the Over-drive to extend the clock frequency to 180 Mhz */
716 PWR
->CR
|= PWR_CR_ODEN
;
717 while ((PWR
->CSR
& PWR_CSR_ODRDY
) == 0)
720 PWR
->CR
|= PWR_CR_ODSWEN
;
721 while ((PWR
->CSR
& PWR_CSR_ODSWRDY
) == 0)
724 #endif /* STM32F427_437x || STM32F429_439xx || STM32F446xx || STM32F469_479xx */
726 #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx) || defined(STM32F469_479xx)
727 /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
728 FLASH
->ACR
= FLASH_ACR_PRFTEN
| FLASH_ACR_ICEN
|FLASH_ACR_DCEN
|FLASH_ACR_LATENCY_5WS
;
729 #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx || STM32F446xx || STM32F469_479xx */
731 #if defined(STM32F401xx) || defined(STM32F410xx) || defined(STM32F411xE)
732 /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
733 FLASH
->ACR
= FLASH_ACR_PRFTEN
| FLASH_ACR_ICEN
|FLASH_ACR_DCEN
|FLASH_ACR_LATENCY_2WS
;
734 #endif /* STM32F401xx || STM32F410xx || STM32F411xE*/
736 /* Select the main PLL as system clock source */
737 RCC
->CFGR
&= (uint32_t)((uint32_t)~(RCC_CFGR_SW
));
738 RCC
->CFGR
|= RCC_CFGR_SW_PLL
;
740 /* Wait till the main PLL is used as system clock source */
741 while ((RCC
->CFGR
& (uint32_t)RCC_CFGR_SWS
) != RCC_CFGR_SWS_PLL
)
746 { /* If HSE fails to start-up, the application will have wrong clock
747 configuration. User can add here some code to deal with this error */
748 while(1) { __NOP(); }
751 SystemCoreClockUpdate();
755 * @brief Setup the external memory controller. Called in startup_stm32f4xx.s
756 * before jump to __main
760 #ifdef DATA_IN_ExtSRAM
762 * @brief Setup the external memory controller.
763 * Called in startup_stm32f4xx.s before jump to main.
764 * This function configures the external SRAM mounted on STM324xG_EVAL/STM324x7I boards
765 * This SRAM will be used as program data memory (including heap and stack).
769 void SystemInit_ExtMemCtl(void)
771 /*-- GPIOs Configuration -----------------------------------------------------*/
773 +-------------------+--------------------+------------------+--------------+
774 + SRAM pins assignment +
775 +-------------------+--------------------+------------------+--------------+
776 | PD0 <-> FMC_D2 | PE0 <-> FMC_NBL0 | PF0 <-> FMC_A0 | PG0 <-> FMC_A10 |
777 | PD1 <-> FMC_D3 | PE1 <-> FMC_NBL1 | PF1 <-> FMC_A1 | PG1 <-> FMC_A11 |
778 | PD4 <-> FMC_NOE | PE3 <-> FMC_A19 | PF2 <-> FMC_A2 | PG2 <-> FMC_A12 |
779 | PD5 <-> FMC_NWE | PE4 <-> FMC_A20 | PF3 <-> FMC_A3 | PG3 <-> FMC_A13 |
780 | PD8 <-> FMC_D13 | PE7 <-> FMC_D4 | PF4 <-> FMC_A4 | PG4 <-> FMC_A14 |
781 | PD9 <-> FMC_D14 | PE8 <-> FMC_D5 | PF5 <-> FMC_A5 | PG5 <-> FMC_A15 |
782 | PD10 <-> FMC_D15 | PE9 <-> FMC_D6 | PF12 <-> FMC_A6 | PG9 <-> FMC_NE2 |
783 | PD11 <-> FMC_A16 | PE10 <-> FMC_D7 | PF13 <-> FMC_A7 |-----------------+
784 | PD12 <-> FMC_A17 | PE11 <-> FMC_D8 | PF14 <-> FMC_A8 |
785 | PD13 <-> FMC_A18 | PE12 <-> FMC_D9 | PF15 <-> FMC_A9 |
786 | PD14 <-> FMC_D0 | PE13 <-> FMC_D10 |-----------------+
787 | PD15 <-> FMC_D1 | PE14 <-> FMC_D11 |
788 | | PE15 <-> FMC_D12 |
789 +------------------+------------------+
791 /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
792 RCC
->AHB1ENR
|= 0x00000078;
794 /* Connect PDx pins to FMC Alternate function */
795 GPIOD
->AFR
[0] = 0x00cc00cc;
796 GPIOD
->AFR
[1] = 0xcccccccc;
797 /* Configure PDx pins in Alternate function mode */
798 GPIOD
->MODER
= 0xaaaa0a0a;
799 /* Configure PDx pins speed to 100 MHz */
800 GPIOD
->OSPEEDR
= 0xffff0f0f;
801 /* Configure PDx pins Output type to push-pull */
802 GPIOD
->OTYPER
= 0x00000000;
803 /* No pull-up, pull-down for PDx pins */
804 GPIOD
->PUPDR
= 0x00000000;
806 /* Connect PEx pins to FMC Alternate function */
807 GPIOE
->AFR
[0] = 0xcccccccc;
808 GPIOE
->AFR
[1] = 0xcccccccc;
809 /* Configure PEx pins in Alternate function mode */
810 GPIOE
->MODER
= 0xaaaaaaaa;
811 /* Configure PEx pins speed to 100 MHz */
812 GPIOE
->OSPEEDR
= 0xffffffff;
813 /* Configure PEx pins Output type to push-pull */
814 GPIOE
->OTYPER
= 0x00000000;
815 /* No pull-up, pull-down for PEx pins */
816 GPIOE
->PUPDR
= 0x00000000;
818 /* Connect PFx pins to FMC Alternate function */
819 GPIOF
->AFR
[0] = 0x00cccccc;
820 GPIOF
->AFR
[1] = 0xcccc0000;
821 /* Configure PFx pins in Alternate function mode */
822 GPIOF
->MODER
= 0xaa000aaa;
823 /* Configure PFx pins speed to 100 MHz */
824 GPIOF
->OSPEEDR
= 0xff000fff;
825 /* Configure PFx pins Output type to push-pull */
826 GPIOF
->OTYPER
= 0x00000000;
827 /* No pull-up, pull-down for PFx pins */
828 GPIOF
->PUPDR
= 0x00000000;
830 /* Connect PGx pins to FMC Alternate function */
831 GPIOG
->AFR
[0] = 0x00cccccc;
832 GPIOG
->AFR
[1] = 0x000000c0;
833 /* Configure PGx pins in Alternate function mode */
834 GPIOG
->MODER
= 0x00080aaa;
835 /* Configure PGx pins speed to 100 MHz */
836 GPIOG
->OSPEEDR
= 0x000c0fff;
837 /* Configure PGx pins Output type to push-pull */
838 GPIOG
->OTYPER
= 0x00000000;
839 /* No pull-up, pull-down for PGx pins */
840 GPIOG
->PUPDR
= 0x00000000;
842 /*-- FMC Configuration ------------------------------------------------------*/
843 /* Enable the FMC/FSMC interface clock */
844 RCC
->AHB3ENR
|= 0x00000001;
846 #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx) || defined(STM32F469_479xx)
847 /* Configure and enable Bank1_SRAM2 */
848 FMC_Bank1
->BTCR
[2] = 0x00001011;
849 FMC_Bank1
->BTCR
[3] = 0x00000201;
850 FMC_Bank1E
->BWTR
[2] = 0x0fffffff;
851 #endif /* STM32F427_437xx || STM32F429_439xx || STM32F446xx || STM32F469_479xx */
853 #if defined(STM32F40_41xxx)
854 /* Configure and enable Bank1_SRAM2 */
855 FSMC_Bank1
->BTCR
[2] = 0x00001011;
856 FSMC_Bank1
->BTCR
[3] = 0x00000201;
857 FSMC_Bank1E
->BWTR
[2] = 0x0fffffff;
858 #endif /* STM32F40_41xxx */
861 Bank1_SRAM2 is configured as follow:
862 In case of FSMC configuration
863 NORSRAMTimingStructure.FSMC_AddressSetupTime = 1;
864 NORSRAMTimingStructure.FSMC_AddressHoldTime = 0;
865 NORSRAMTimingStructure.FSMC_DataSetupTime = 2;
866 NORSRAMTimingStructure.FSMC_BusTurnAroundDuration = 0;
867 NORSRAMTimingStructure.FSMC_CLKDivision = 0;
868 NORSRAMTimingStructure.FSMC_DataLatency = 0;
869 NORSRAMTimingStructure.FSMC_AccessMode = FMC_AccessMode_A;
871 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
872 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
873 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
874 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
875 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
876 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
877 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
878 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
879 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
880 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
881 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
882 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
883 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
884 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &NORSRAMTimingStructure;
885 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &NORSRAMTimingStructure;
887 In case of FMC configuration
888 NORSRAMTimingStructure.FMC_AddressSetupTime = 1;
889 NORSRAMTimingStructure.FMC_AddressHoldTime = 0;
890 NORSRAMTimingStructure.FMC_DataSetupTime = 2;
891 NORSRAMTimingStructure.FMC_BusTurnAroundDuration = 0;
892 NORSRAMTimingStructure.FMC_CLKDivision = 0;
893 NORSRAMTimingStructure.FMC_DataLatency = 0;
894 NORSRAMTimingStructure.FMC_AccessMode = FMC_AccessMode_A;
896 FMC_NORSRAMInitStructure.FMC_Bank = FMC_Bank1_NORSRAM2;
897 FMC_NORSRAMInitStructure.FMC_DataAddressMux = FMC_DataAddressMux_Disable;
898 FMC_NORSRAMInitStructure.FMC_MemoryType = FMC_MemoryType_SRAM;
899 FMC_NORSRAMInitStructure.FMC_MemoryDataWidth = FMC_MemoryDataWidth_16b;
900 FMC_NORSRAMInitStructure.FMC_BurstAccessMode = FMC_BurstAccessMode_Disable;
901 FMC_NORSRAMInitStructure.FMC_AsynchronousWait = FMC_AsynchronousWait_Disable;
902 FMC_NORSRAMInitStructure.FMC_WaitSignalPolarity = FMC_WaitSignalPolarity_Low;
903 FMC_NORSRAMInitStructure.FMC_WrapMode = FMC_WrapMode_Disable;
904 FMC_NORSRAMInitStructure.FMC_WaitSignalActive = FMC_WaitSignalActive_BeforeWaitState;
905 FMC_NORSRAMInitStructure.FMC_WriteOperation = FMC_WriteOperation_Enable;
906 FMC_NORSRAMInitStructure.FMC_WaitSignal = FMC_WaitSignal_Disable;
907 FMC_NORSRAMInitStructure.FMC_ExtendedMode = FMC_ExtendedMode_Disable;
908 FMC_NORSRAMInitStructure.FMC_WriteBurst = FMC_WriteBurst_Disable;
909 FMC_NORSRAMInitStructure.FMC_ContinousClock = FMC_CClock_SyncOnly;
910 FMC_NORSRAMInitStructure.FMC_ReadWriteTimingStruct = &NORSRAMTimingStructure;
911 FMC_NORSRAMInitStructure.FMC_WriteTimingStruct = &NORSRAMTimingStructure;
915 #endif /* DATA_IN_ExtSRAM */
917 #ifdef DATA_IN_ExtSDRAM
919 * @brief Setup the external memory controller.
920 * Called in startup_stm32f4xx.s before jump to main.
921 * This function configures the external SDRAM mounted on STM324x9I_EVAL board
922 * This SDRAM will be used as program data memory (including heap and stack).
926 void SystemInit_ExtMemCtl(void)
928 register uint32_t tmpreg
= 0, timeout
= 0xFFFF;
929 register uint32_t index
;
931 /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface
933 RCC
->AHB1ENR
|= 0x000001FC;
935 /* Connect PCx pins to FMC Alternate function */
936 GPIOC
->AFR
[0] = 0x0000000c;
937 GPIOC
->AFR
[1] = 0x00007700;
938 /* Configure PCx pins in Alternate function mode */
939 GPIOC
->MODER
= 0x00a00002;
940 /* Configure PCx pins speed to 50 MHz */
941 GPIOC
->OSPEEDR
= 0x00a00002;
942 /* Configure PCx pins Output type to push-pull */
943 GPIOC
->OTYPER
= 0x00000000;
944 /* No pull-up, pull-down for PCx pins */
945 GPIOC
->PUPDR
= 0x00500000;
947 /* Connect PDx pins to FMC Alternate function */
948 GPIOD
->AFR
[0] = 0x000000CC;
949 GPIOD
->AFR
[1] = 0xCC000CCC;
950 /* Configure PDx pins in Alternate function mode */
951 GPIOD
->MODER
= 0xA02A000A;
952 /* Configure PDx pins speed to 50 MHz */
953 GPIOD
->OSPEEDR
= 0xA02A000A;
954 /* Configure PDx pins Output type to push-pull */
955 GPIOD
->OTYPER
= 0x00000000;
956 /* No pull-up, pull-down for PDx pins */
957 GPIOD
->PUPDR
= 0x00000000;
959 /* Connect PEx pins to FMC Alternate function */
960 GPIOE
->AFR
[0] = 0xC00000CC;
961 GPIOE
->AFR
[1] = 0xCCCCCCCC;
962 /* Configure PEx pins in Alternate function mode */
963 GPIOE
->MODER
= 0xAAAA800A;
964 /* Configure PEx pins speed to 50 MHz */
965 GPIOE
->OSPEEDR
= 0xAAAA800A;
966 /* Configure PEx pins Output type to push-pull */
967 GPIOE
->OTYPER
= 0x00000000;
968 /* No pull-up, pull-down for PEx pins */
969 GPIOE
->PUPDR
= 0x00000000;
971 /* Connect PFx pins to FMC Alternate function */
972 GPIOF
->AFR
[0] = 0xcccccccc;
973 GPIOF
->AFR
[1] = 0xcccccccc;
974 /* Configure PFx pins in Alternate function mode */
975 GPIOF
->MODER
= 0xAA800AAA;
976 /* Configure PFx pins speed to 50 MHz */
977 GPIOF
->OSPEEDR
= 0xAA800AAA;
978 /* Configure PFx pins Output type to push-pull */
979 GPIOF
->OTYPER
= 0x00000000;
980 /* No pull-up, pull-down for PFx pins */
981 GPIOF
->PUPDR
= 0x00000000;
983 /* Connect PGx pins to FMC Alternate function */
984 GPIOG
->AFR
[0] = 0xcccccccc;
985 GPIOG
->AFR
[1] = 0xcccccccc;
986 /* Configure PGx pins in Alternate function mode */
987 GPIOG
->MODER
= 0xaaaaaaaa;
988 /* Configure PGx pins speed to 50 MHz */
989 GPIOG
->OSPEEDR
= 0xaaaaaaaa;
990 /* Configure PGx pins Output type to push-pull */
991 GPIOG
->OTYPER
= 0x00000000;
992 /* No pull-up, pull-down for PGx pins */
993 GPIOG
->PUPDR
= 0x00000000;
995 /* Connect PHx pins to FMC Alternate function */
996 GPIOH
->AFR
[0] = 0x00C0CC00;
997 GPIOH
->AFR
[1] = 0xCCCCCCCC;
998 /* Configure PHx pins in Alternate function mode */
999 GPIOH
->MODER
= 0xAAAA08A0;
1000 /* Configure PHx pins speed to 50 MHz */
1001 GPIOH
->OSPEEDR
= 0xAAAA08A0;
1002 /* Configure PHx pins Output type to push-pull */
1003 GPIOH
->OTYPER
= 0x00000000;
1004 /* No pull-up, pull-down for PHx pins */
1005 GPIOH
->PUPDR
= 0x00000000;
1007 /* Connect PIx pins to FMC Alternate function */
1008 GPIOI
->AFR
[0] = 0xCCCCCCCC;
1009 GPIOI
->AFR
[1] = 0x00000CC0;
1010 /* Configure PIx pins in Alternate function mode */
1011 GPIOI
->MODER
= 0x0028AAAA;
1012 /* Configure PIx pins speed to 50 MHz */
1013 GPIOI
->OSPEEDR
= 0x0028AAAA;
1014 /* Configure PIx pins Output type to push-pull */
1015 GPIOI
->OTYPER
= 0x00000000;
1016 /* No pull-up, pull-down for PIx pins */
1017 GPIOI
->PUPDR
= 0x00000000;
1019 /*-- FMC Configuration ------------------------------------------------------*/
1020 /* Enable the FMC interface clock */
1021 RCC
->AHB3ENR
|= 0x00000001;
1023 /* Configure and enable SDRAM bank1 */
1024 FMC_Bank5_6
->SDCR
[0] = 0x000039D0;
1025 FMC_Bank5_6
->SDTR
[0] = 0x01115351;
1027 /* SDRAM initialization sequence */
1028 /* Clock enable command */
1029 FMC_Bank5_6
->SDCMR
= 0x00000011;
1030 tmpreg
= FMC_Bank5_6
->SDSR
& 0x00000020;
1031 while((tmpreg
!= 0) & (timeout
-- > 0))
1033 tmpreg
= FMC_Bank5_6
->SDSR
& 0x00000020;
1037 for (index
= 0; index
<1000; index
++);
1040 FMC_Bank5_6
->SDCMR
= 0x00000012;
1042 while((tmpreg
!= 0) & (timeout
-- > 0))
1044 tmpreg
= FMC_Bank5_6
->SDSR
& 0x00000020;
1047 /* Auto refresh command */
1048 FMC_Bank5_6
->SDCMR
= 0x00000073;
1050 while((tmpreg
!= 0) & (timeout
-- > 0))
1052 tmpreg
= FMC_Bank5_6
->SDSR
& 0x00000020;
1055 /* MRD register program */
1056 FMC_Bank5_6
->SDCMR
= 0x00046014;
1058 while((tmpreg
!= 0) & (timeout
-- > 0))
1060 tmpreg
= FMC_Bank5_6
->SDSR
& 0x00000020;
1063 /* Set refresh count */
1064 tmpreg
= FMC_Bank5_6
->SDRTR
;
1065 FMC_Bank5_6
->SDRTR
= (tmpreg
| (0x0000027C<<1));
1067 /* Disable write protection */
1068 tmpreg
= FMC_Bank5_6
->SDCR
[0];
1069 FMC_Bank5_6
->SDCR
[0] = (tmpreg
& 0xFFFFFDFF);
1072 Bank1_SDRAM is configured as follow:
1074 FMC_SDRAMTimingInitStructure.FMC_LoadToActiveDelay = 2;
1075 FMC_SDRAMTimingInitStructure.FMC_ExitSelfRefreshDelay = 6;
1076 FMC_SDRAMTimingInitStructure.FMC_SelfRefreshTime = 4;
1077 FMC_SDRAMTimingInitStructure.FMC_RowCycleDelay = 6;
1078 FMC_SDRAMTimingInitStructure.FMC_WriteRecoveryTime = 2;
1079 FMC_SDRAMTimingInitStructure.FMC_RPDelay = 2;
1080 FMC_SDRAMTimingInitStructure.FMC_RCDDelay = 2;
1082 FMC_SDRAMInitStructure.FMC_Bank = SDRAM_BANK;
1083 FMC_SDRAMInitStructure.FMC_ColumnBitsNumber = FMC_ColumnBits_Number_8b;
1084 FMC_SDRAMInitStructure.FMC_RowBitsNumber = FMC_RowBits_Number_11b;
1085 FMC_SDRAMInitStructure.FMC_SDMemoryDataWidth = FMC_SDMemory_Width_16b;
1086 FMC_SDRAMInitStructure.FMC_InternalBankNumber = FMC_InternalBank_Number_4;
1087 FMC_SDRAMInitStructure.FMC_CASLatency = FMC_CAS_Latency_3;
1088 FMC_SDRAMInitStructure.FMC_WriteProtection = FMC_Write_Protection_Disable;
1089 FMC_SDRAMInitStructure.FMC_SDClockPeriod = FMC_SDClock_Period_2;
1090 FMC_SDRAMInitStructure.FMC_ReadBurst = FMC_Read_Burst_disable;
1091 FMC_SDRAMInitStructure.FMC_ReadPipeDelay = FMC_ReadPipe_Delay_1;
1092 FMC_SDRAMInitStructure.FMC_SDRAMTimingStruct = &FMC_SDRAMTimingInitStructure;
1096 #endif /* DATA_IN_ExtSDRAM */
1110 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/