rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
system_stm32f4xx.c
Go to the documentation of this file.
1/**
2 ******************************************************************************
3 * @file system_stm32f4xx.c
4 * @author MCD Application Team
5 * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File.
6 *
7 * This file provides two functions and one global variable to be called from
8 * user application:
9 * - SystemInit(): This function is called at startup just after reset and
10 * before branch to main program. This call is made inside
11 * the "startup_stm32f4xx.s" file.
12 *
13 * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
14 * by the user application to setup the SysTick
15 * timer or configure other parameters.
16 *
17 * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
18 * be called whenever the core clock is changed
19 * during program execution.
20 *
21 *
22 ******************************************************************************
23 * @attention
24 *
25 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
26 * All rights reserved.</center></h2>
27 *
28 * This software component is licensed by ST under BSD 3-Clause license,
29 * the "License"; You may not use this file except in compliance with the
30 * License. You may obtain a copy of the License at:
31 * opensource.org/licenses/BSD-3-Clause
32 *
33 ******************************************************************************
34 */
35
36/** @addtogroup CMSIS
37 * @{
38 */
39
40/** @addtogroup stm32f4xx_system
41 * @{
42 */
43
44/** @addtogroup STM32F4xx_System_Private_Includes
45 * @{
46 */
47
48
49#include "stm32f4xx.h"
50
51/* No default! Should be explictly defined in openblt/board.mk */
52#if 0
53#if !defined (HSE_VALUE)
54 #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */
55#endif /* HSE_VALUE */
56#endif
57
58#if !defined (HSI_VALUE)
59 #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
60#endif /* HSI_VALUE */
61
62/**
63 * @}
64 */
65
66/** @addtogroup STM32F4xx_System_Private_TypesDefinitions
67 * @{
68 */
69
70/**
71 * @}
72 */
73
74/** @addtogroup STM32F4xx_System_Private_Defines
75 * @{
76 */
77
78/************************* Miscellaneous Configuration ************************/
79/*!< Uncomment the following line if you need to use external SRAM or SDRAM as data memory */
80#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\
81 || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
82 || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx)
83/* #define DATA_IN_ExtSRAM */
84#endif /* STM32F40xxx || STM32F41xxx || STM32F42xxx || STM32F43xxx || STM32F469xx || STM32F479xx ||\
85 STM32F412Zx || STM32F412Vx */
86
87#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
88 || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
89/* #define DATA_IN_ExtSDRAM */
90#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx ||\
91 STM32F479xx */
92
93/*!< Uncomment the following line if you need to relocate your vector Table in
94 Internal SRAM. */
95/* #define VECT_TAB_SRAM */
96#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
97 This value must be a multiple of 0x200. */
98/******************************************************************************/
99
100/**
101 * @}
102 */
103
104/** @addtogroup STM32F4xx_System_Private_Macros
105 * @{
106 */
107
108/**
109 * @}
110 */
111
112/** @addtogroup STM32F4xx_System_Private_Variables
113 * @{
114 */
115 /* This variable is updated in three ways:
116 1) by calling CMSIS function SystemCoreClockUpdate()
117 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
118 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
119 Note: If you use this function to configure the system clock; then there
120 is no need to call the 2 first functions listed above, since SystemCoreClock
121 variable is updated automatically.
122 */
123uint32_t SystemCoreClock = 16000000;
124const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
125const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
126/**
127 * @}
128 */
129
130/** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
131 * @{
132 */
133
134#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
135 static void SystemInit_ExtMemCtl(void);
136#endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
137
138/**
139 * @}
140 */
141
142/** @addtogroup STM32F4xx_System_Private_Functions
143 * @{
144 */
145
146/**
147 * @brief Setup the microcontroller system
148 * Initialize the FPU setting, vector table location and External memory
149 * configuration.
150 * @param None
151 * @retval None
152 */
153void SystemInit(void)
154{
155 /* FPU settings ------------------------------------------------------------*/
156 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
157 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
158 #endif
159
160#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
162#endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
163
164 /* Configure the Vector Table location add offset address ------------------*/
165#ifdef VECT_TAB_SRAM
166 SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
167#else
168 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
169#endif
170}
171
172/**
173 * @brief Update SystemCoreClock variable according to Clock Register Values.
174 * The SystemCoreClock variable contains the core clock (HCLK), it can
175 * be used by the user application to setup the SysTick timer or configure
176 * other parameters.
177 *
178 * @note Each time the core clock (HCLK) changes, this function must be called
179 * to update SystemCoreClock variable value. Otherwise, any configuration
180 * based on this variable will be incorrect.
181 *
182 * @note - The system frequency computed by this function is not the real
183 * frequency in the chip. It is calculated based on the predefined
184 * constant and the selected clock source:
185 *
186 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
187 *
188 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
189 *
190 * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
191 * or HSI_VALUE(*) multiplied/divided by the PLL factors.
192 *
193 * (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
194 * 16 MHz) but the real value may vary depending on the variations
195 * in voltage and temperature.
196 *
197 * (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (its value
198 * depends on the application requirements), user has to ensure that HSE_VALUE
199 * is same as the real frequency of the crystal used. Otherwise, this function
200 * may have wrong result.
201 *
202 * - The result of this function could be not correct when using fractional
203 * value for HSE crystal.
204 *
205 * @param None
206 * @retval None
207 */
209{
210 uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
211
212 /* Get SYSCLK source -------------------------------------------------------*/
213 tmp = RCC->CFGR & RCC_CFGR_SWS;
214
215 switch (tmp)
216 {
217 case 0x00: /* HSI used as system clock source */
218 SystemCoreClock = HSI_VALUE;
219 break;
220 case 0x04: /* HSE used as system clock source */
221 SystemCoreClock = HSE_VALUE;
222 break;
223 case 0x08: /* PLL used as system clock source */
224
225 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
226 SYSCLK = PLL_VCO / PLL_P
227 */
228 pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
229 pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
230
231 if (pllsource != 0)
232 {
233 /* HSE used as PLL clock source */
234 pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
235 }
236 else
237 {
238 /* HSI used as PLL clock source */
239 pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
240 }
241
242 pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
243 SystemCoreClock = pllvco/pllp;
244 break;
245 default:
246 SystemCoreClock = HSI_VALUE;
247 break;
248 }
249 /* Compute HCLK frequency --------------------------------------------------*/
250 /* Get HCLK prescaler */
251 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
252 /* HCLK frequency */
253 SystemCoreClock >>= tmp;
254}
255
256#if defined (DATA_IN_ExtSRAM) && defined (DATA_IN_ExtSDRAM)
257#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
258 || defined(STM32F469xx) || defined(STM32F479xx)
259/**
260 * @brief Setup the external memory controller.
261 * Called in startup_stm32f4xx.s before jump to main.
262 * This function configures the external memories (SRAM/SDRAM)
263 * This SRAM/SDRAM will be used as program data memory (including heap and stack).
264 * @param None
265 * @retval None
266 */
268{
269 __IO uint32_t tmp = 0x00;
270
271 register uint32_t tmpreg = 0, timeout = 0xFFFF;
272 register __IO uint32_t index;
273
274 /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface clock */
275 RCC->AHB1ENR |= 0x000001F8;
276
277 /* Delay after an RCC peripheral clock enabling */
278 tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
279
280 /* Connect PDx pins to FMC Alternate function */
281 GPIOD->AFR[0] = 0x00CCC0CC;
282 GPIOD->AFR[1] = 0xCCCCCCCC;
283 /* Configure PDx pins in Alternate function mode */
284 GPIOD->MODER = 0xAAAA0A8A;
285 /* Configure PDx pins speed to 100 MHz */
286 GPIOD->OSPEEDR = 0xFFFF0FCF;
287 /* Configure PDx pins Output type to push-pull */
288 GPIOD->OTYPER = 0x00000000;
289 /* No pull-up, pull-down for PDx pins */
290 GPIOD->PUPDR = 0x00000000;
291
292 /* Connect PEx pins to FMC Alternate function */
293 GPIOE->AFR[0] = 0xC00CC0CC;
294 GPIOE->AFR[1] = 0xCCCCCCCC;
295 /* Configure PEx pins in Alternate function mode */
296 GPIOE->MODER = 0xAAAA828A;
297 /* Configure PEx pins speed to 100 MHz */
298 GPIOE->OSPEEDR = 0xFFFFC3CF;
299 /* Configure PEx pins Output type to push-pull */
300 GPIOE->OTYPER = 0x00000000;
301 /* No pull-up, pull-down for PEx pins */
302 GPIOE->PUPDR = 0x00000000;
303
304 /* Connect PFx pins to FMC Alternate function */
305 GPIOF->AFR[0] = 0xCCCCCCCC;
306 GPIOF->AFR[1] = 0xCCCCCCCC;
307 /* Configure PFx pins in Alternate function mode */
308 GPIOF->MODER = 0xAA800AAA;
309 /* Configure PFx pins speed to 50 MHz */
310 GPIOF->OSPEEDR = 0xAA800AAA;
311 /* Configure PFx pins Output type to push-pull */
312 GPIOF->OTYPER = 0x00000000;
313 /* No pull-up, pull-down for PFx pins */
314 GPIOF->PUPDR = 0x00000000;
315
316 /* Connect PGx pins to FMC Alternate function */
317 GPIOG->AFR[0] = 0xCCCCCCCC;
318 GPIOG->AFR[1] = 0xCCCCCCCC;
319 /* Configure PGx pins in Alternate function mode */
320 GPIOG->MODER = 0xAAAAAAAA;
321 /* Configure PGx pins speed to 50 MHz */
322 GPIOG->OSPEEDR = 0xAAAAAAAA;
323 /* Configure PGx pins Output type to push-pull */
324 GPIOG->OTYPER = 0x00000000;
325 /* No pull-up, pull-down for PGx pins */
326 GPIOG->PUPDR = 0x00000000;
327
328 /* Connect PHx pins to FMC Alternate function */
329 GPIOH->AFR[0] = 0x00C0CC00;
330 GPIOH->AFR[1] = 0xCCCCCCCC;
331 /* Configure PHx pins in Alternate function mode */
332 GPIOH->MODER = 0xAAAA08A0;
333 /* Configure PHx pins speed to 50 MHz */
334 GPIOH->OSPEEDR = 0xAAAA08A0;
335 /* Configure PHx pins Output type to push-pull */
336 GPIOH->OTYPER = 0x00000000;
337 /* No pull-up, pull-down for PHx pins */
338 GPIOH->PUPDR = 0x00000000;
339
340 /* Connect PIx pins to FMC Alternate function */
341 GPIOI->AFR[0] = 0xCCCCCCCC;
342 GPIOI->AFR[1] = 0x00000CC0;
343 /* Configure PIx pins in Alternate function mode */
344 GPIOI->MODER = 0x0028AAAA;
345 /* Configure PIx pins speed to 50 MHz */
346 GPIOI->OSPEEDR = 0x0028AAAA;
347 /* Configure PIx pins Output type to push-pull */
348 GPIOI->OTYPER = 0x00000000;
349 /* No pull-up, pull-down for PIx pins */
350 GPIOI->PUPDR = 0x00000000;
351
352/*-- FMC Configuration -------------------------------------------------------*/
353 /* Enable the FMC interface clock */
354 RCC->AHB3ENR |= 0x00000001;
355 /* Delay after an RCC peripheral clock enabling */
356 tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
357
358 FMC_Bank5_6->SDCR[0] = 0x000019E4;
359 FMC_Bank5_6->SDTR[0] = 0x01115351;
360
361 /* SDRAM initialization sequence */
362 /* Clock enable command */
363 FMC_Bank5_6->SDCMR = 0x00000011;
364 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
365 while((tmpreg != 0) && (timeout-- > 0))
366 {
367 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
368 }
369
370 /* Delay */
371 for (index = 0; index<1000; index++);
372
373 /* PALL command */
374 FMC_Bank5_6->SDCMR = 0x00000012;
375 timeout = 0xFFFF;
376 while((tmpreg != 0) && (timeout-- > 0))
377 {
378 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
379 }
380
381 /* Auto refresh command */
382 FMC_Bank5_6->SDCMR = 0x00000073;
383 timeout = 0xFFFF;
384 while((tmpreg != 0) && (timeout-- > 0))
385 {
386 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
387 }
388
389 /* MRD register program */
390 FMC_Bank5_6->SDCMR = 0x00046014;
391 timeout = 0xFFFF;
392 while((tmpreg != 0) && (timeout-- > 0))
393 {
394 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
395 }
396
397 /* Set refresh count */
398 tmpreg = FMC_Bank5_6->SDRTR;
399 FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
400
401 /* Disable write protection */
402 tmpreg = FMC_Bank5_6->SDCR[0];
403 FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
404
405#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
406 /* Configure and enable Bank1_SRAM2 */
407 FMC_Bank1->BTCR[2] = 0x00001011;
408 FMC_Bank1->BTCR[3] = 0x00000201;
409 FMC_Bank1E->BWTR[2] = 0x0fffffff;
410#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
411#if defined(STM32F469xx) || defined(STM32F479xx)
412 /* Configure and enable Bank1_SRAM2 */
413 FMC_Bank1->BTCR[2] = 0x00001091;
414 FMC_Bank1->BTCR[3] = 0x00110212;
415 FMC_Bank1E->BWTR[2] = 0x0fffffff;
416#endif /* STM32F469xx || STM32F479xx */
417
418 (void)(tmp);
419}
420#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
421#elif defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
422/**
423 * @brief Setup the external memory controller.
424 * Called in startup_stm32f4xx.s before jump to main.
425 * This function configures the external memories (SRAM/SDRAM)
426 * This SRAM/SDRAM will be used as program data memory (including heap and stack).
427 * @param None
428 * @retval None
429 */
430void SystemInit_ExtMemCtl(void)
431{
432 __IO uint32_t tmp = 0x00;
433#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
434 || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
435#if defined (DATA_IN_ExtSDRAM)
436 register uint32_t tmpreg = 0, timeout = 0xFFFF;
437 register __IO uint32_t index;
438
439#if defined(STM32F446xx)
440 /* Enable GPIOA, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG interface
441 clock */
442 RCC->AHB1ENR |= 0x0000007D;
443#else
444 /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface
445 clock */
446 RCC->AHB1ENR |= 0x000001F8;
447#endif /* STM32F446xx */
448 /* Delay after an RCC peripheral clock enabling */
449 tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
450
451#if defined(STM32F446xx)
452 /* Connect PAx pins to FMC Alternate function */
453 GPIOA->AFR[0] |= 0xC0000000;
454 GPIOA->AFR[1] |= 0x00000000;
455 /* Configure PDx pins in Alternate function mode */
456 GPIOA->MODER |= 0x00008000;
457 /* Configure PDx pins speed to 50 MHz */
458 GPIOA->OSPEEDR |= 0x00008000;
459 /* Configure PDx pins Output type to push-pull */
460 GPIOA->OTYPER |= 0x00000000;
461 /* No pull-up, pull-down for PDx pins */
462 GPIOA->PUPDR |= 0x00000000;
463
464 /* Connect PCx pins to FMC Alternate function */
465 GPIOC->AFR[0] |= 0x00CC0000;
466 GPIOC->AFR[1] |= 0x00000000;
467 /* Configure PDx pins in Alternate function mode */
468 GPIOC->MODER |= 0x00000A00;
469 /* Configure PDx pins speed to 50 MHz */
470 GPIOC->OSPEEDR |= 0x00000A00;
471 /* Configure PDx pins Output type to push-pull */
472 GPIOC->OTYPER |= 0x00000000;
473 /* No pull-up, pull-down for PDx pins */
474 GPIOC->PUPDR |= 0x00000000;
475#endif /* STM32F446xx */
476
477 /* Connect PDx pins to FMC Alternate function */
478 GPIOD->AFR[0] = 0x000000CC;
479 GPIOD->AFR[1] = 0xCC000CCC;
480 /* Configure PDx pins in Alternate function mode */
481 GPIOD->MODER = 0xA02A000A;
482 /* Configure PDx pins speed to 50 MHz */
483 GPIOD->OSPEEDR = 0xA02A000A;
484 /* Configure PDx pins Output type to push-pull */
485 GPIOD->OTYPER = 0x00000000;
486 /* No pull-up, pull-down for PDx pins */
487 GPIOD->PUPDR = 0x00000000;
488
489 /* Connect PEx pins to FMC Alternate function */
490 GPIOE->AFR[0] = 0xC00000CC;
491 GPIOE->AFR[1] = 0xCCCCCCCC;
492 /* Configure PEx pins in Alternate function mode */
493 GPIOE->MODER = 0xAAAA800A;
494 /* Configure PEx pins speed to 50 MHz */
495 GPIOE->OSPEEDR = 0xAAAA800A;
496 /* Configure PEx pins Output type to push-pull */
497 GPIOE->OTYPER = 0x00000000;
498 /* No pull-up, pull-down for PEx pins */
499 GPIOE->PUPDR = 0x00000000;
500
501 /* Connect PFx pins to FMC Alternate function */
502 GPIOF->AFR[0] = 0xCCCCCCCC;
503 GPIOF->AFR[1] = 0xCCCCCCCC;
504 /* Configure PFx pins in Alternate function mode */
505 GPIOF->MODER = 0xAA800AAA;
506 /* Configure PFx pins speed to 50 MHz */
507 GPIOF->OSPEEDR = 0xAA800AAA;
508 /* Configure PFx pins Output type to push-pull */
509 GPIOF->OTYPER = 0x00000000;
510 /* No pull-up, pull-down for PFx pins */
511 GPIOF->PUPDR = 0x00000000;
512
513 /* Connect PGx pins to FMC Alternate function */
514 GPIOG->AFR[0] = 0xCCCCCCCC;
515 GPIOG->AFR[1] = 0xCCCCCCCC;
516 /* Configure PGx pins in Alternate function mode */
517 GPIOG->MODER = 0xAAAAAAAA;
518 /* Configure PGx pins speed to 50 MHz */
519 GPIOG->OSPEEDR = 0xAAAAAAAA;
520 /* Configure PGx pins Output type to push-pull */
521 GPIOG->OTYPER = 0x00000000;
522 /* No pull-up, pull-down for PGx pins */
523 GPIOG->PUPDR = 0x00000000;
524
525#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
526 || defined(STM32F469xx) || defined(STM32F479xx)
527 /* Connect PHx pins to FMC Alternate function */
528 GPIOH->AFR[0] = 0x00C0CC00;
529 GPIOH->AFR[1] = 0xCCCCCCCC;
530 /* Configure PHx pins in Alternate function mode */
531 GPIOH->MODER = 0xAAAA08A0;
532 /* Configure PHx pins speed to 50 MHz */
533 GPIOH->OSPEEDR = 0xAAAA08A0;
534 /* Configure PHx pins Output type to push-pull */
535 GPIOH->OTYPER = 0x00000000;
536 /* No pull-up, pull-down for PHx pins */
537 GPIOH->PUPDR = 0x00000000;
538
539 /* Connect PIx pins to FMC Alternate function */
540 GPIOI->AFR[0] = 0xCCCCCCCC;
541 GPIOI->AFR[1] = 0x00000CC0;
542 /* Configure PIx pins in Alternate function mode */
543 GPIOI->MODER = 0x0028AAAA;
544 /* Configure PIx pins speed to 50 MHz */
545 GPIOI->OSPEEDR = 0x0028AAAA;
546 /* Configure PIx pins Output type to push-pull */
547 GPIOI->OTYPER = 0x00000000;
548 /* No pull-up, pull-down for PIx pins */
549 GPIOI->PUPDR = 0x00000000;
550#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
551
552/*-- FMC Configuration -------------------------------------------------------*/
553 /* Enable the FMC interface clock */
554 RCC->AHB3ENR |= 0x00000001;
555 /* Delay after an RCC peripheral clock enabling */
556 tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
557
558 /* Configure and enable SDRAM bank1 */
559#if defined(STM32F446xx)
560 FMC_Bank5_6->SDCR[0] = 0x00001954;
561#else
562 FMC_Bank5_6->SDCR[0] = 0x000019E4;
563#endif /* STM32F446xx */
564 FMC_Bank5_6->SDTR[0] = 0x01115351;
565
566 /* SDRAM initialization sequence */
567 /* Clock enable command */
568 FMC_Bank5_6->SDCMR = 0x00000011;
569 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
570 while((tmpreg != 0) && (timeout-- > 0))
571 {
572 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
573 }
574
575 /* Delay */
576 for (index = 0; index<1000; index++);
577
578 /* PALL command */
579 FMC_Bank5_6->SDCMR = 0x00000012;
580 timeout = 0xFFFF;
581 while((tmpreg != 0) && (timeout-- > 0))
582 {
583 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
584 }
585
586 /* Auto refresh command */
587#if defined(STM32F446xx)
588 FMC_Bank5_6->SDCMR = 0x000000F3;
589#else
590 FMC_Bank5_6->SDCMR = 0x00000073;
591#endif /* STM32F446xx */
592 timeout = 0xFFFF;
593 while((tmpreg != 0) && (timeout-- > 0))
594 {
595 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
596 }
597
598 /* MRD register program */
599#if defined(STM32F446xx)
600 FMC_Bank5_6->SDCMR = 0x00044014;
601#else
602 FMC_Bank5_6->SDCMR = 0x00046014;
603#endif /* STM32F446xx */
604 timeout = 0xFFFF;
605 while((tmpreg != 0) && (timeout-- > 0))
606 {
607 tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
608 }
609
610 /* Set refresh count */
611 tmpreg = FMC_Bank5_6->SDRTR;
612#if defined(STM32F446xx)
613 FMC_Bank5_6->SDRTR = (tmpreg | (0x0000050C<<1));
614#else
615 FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
616#endif /* STM32F446xx */
617
618 /* Disable write protection */
619 tmpreg = FMC_Bank5_6->SDCR[0];
620 FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
621#endif /* DATA_IN_ExtSDRAM */
622#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx */
623
624#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\
625 || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
626 || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx)
627
628#if defined(DATA_IN_ExtSRAM)
629/*-- GPIOs Configuration -----------------------------------------------------*/
630 /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
631 RCC->AHB1ENR |= 0x00000078;
632 /* Delay after an RCC peripheral clock enabling */
633 tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN);
634
635 /* Connect PDx pins to FMC Alternate function */
636 GPIOD->AFR[0] = 0x00CCC0CC;
637 GPIOD->AFR[1] = 0xCCCCCCCC;
638 /* Configure PDx pins in Alternate function mode */
639 GPIOD->MODER = 0xAAAA0A8A;
640 /* Configure PDx pins speed to 100 MHz */
641 GPIOD->OSPEEDR = 0xFFFF0FCF;
642 /* Configure PDx pins Output type to push-pull */
643 GPIOD->OTYPER = 0x00000000;
644 /* No pull-up, pull-down for PDx pins */
645 GPIOD->PUPDR = 0x00000000;
646
647 /* Connect PEx pins to FMC Alternate function */
648 GPIOE->AFR[0] = 0xC00CC0CC;
649 GPIOE->AFR[1] = 0xCCCCCCCC;
650 /* Configure PEx pins in Alternate function mode */
651 GPIOE->MODER = 0xAAAA828A;
652 /* Configure PEx pins speed to 100 MHz */
653 GPIOE->OSPEEDR = 0xFFFFC3CF;
654 /* Configure PEx pins Output type to push-pull */
655 GPIOE->OTYPER = 0x00000000;
656 /* No pull-up, pull-down for PEx pins */
657 GPIOE->PUPDR = 0x00000000;
658
659 /* Connect PFx pins to FMC Alternate function */
660 GPIOF->AFR[0] = 0x00CCCCCC;
661 GPIOF->AFR[1] = 0xCCCC0000;
662 /* Configure PFx pins in Alternate function mode */
663 GPIOF->MODER = 0xAA000AAA;
664 /* Configure PFx pins speed to 100 MHz */
665 GPIOF->OSPEEDR = 0xFF000FFF;
666 /* Configure PFx pins Output type to push-pull */
667 GPIOF->OTYPER = 0x00000000;
668 /* No pull-up, pull-down for PFx pins */
669 GPIOF->PUPDR = 0x00000000;
670
671 /* Connect PGx pins to FMC Alternate function */
672 GPIOG->AFR[0] = 0x00CCCCCC;
673 GPIOG->AFR[1] = 0x000000C0;
674 /* Configure PGx pins in Alternate function mode */
675 GPIOG->MODER = 0x00085AAA;
676 /* Configure PGx pins speed to 100 MHz */
677 GPIOG->OSPEEDR = 0x000CAFFF;
678 /* Configure PGx pins Output type to push-pull */
679 GPIOG->OTYPER = 0x00000000;
680 /* No pull-up, pull-down for PGx pins */
681 GPIOG->PUPDR = 0x00000000;
682
683/*-- FMC/FSMC Configuration --------------------------------------------------*/
684 /* Enable the FMC/FSMC interface clock */
685 RCC->AHB3ENR |= 0x00000001;
686
687#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
688 /* Delay after an RCC peripheral clock enabling */
689 tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
690 /* Configure and enable Bank1_SRAM2 */
691 FMC_Bank1->BTCR[2] = 0x00001011;
692 FMC_Bank1->BTCR[3] = 0x00000201;
693 FMC_Bank1E->BWTR[2] = 0x0fffffff;
694#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
695#if defined(STM32F469xx) || defined(STM32F479xx)
696 /* Delay after an RCC peripheral clock enabling */
697 tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
698 /* Configure and enable Bank1_SRAM2 */
699 FMC_Bank1->BTCR[2] = 0x00001091;
700 FMC_Bank1->BTCR[3] = 0x00110212;
701 FMC_Bank1E->BWTR[2] = 0x0fffffff;
702#endif /* STM32F469xx || STM32F479xx */
703#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx)\
704 || defined(STM32F412Zx) || defined(STM32F412Vx)
705 /* Delay after an RCC peripheral clock enabling */
706 tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FSMCEN);
707 /* Configure and enable Bank1_SRAM2 */
708 FSMC_Bank1->BTCR[2] = 0x00001011;
709 FSMC_Bank1->BTCR[3] = 0x00000201;
710 FSMC_Bank1E->BWTR[2] = 0x0FFFFFFF;
711#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F412Zx || STM32F412Vx */
712
713#endif /* DATA_IN_ExtSRAM */
714#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
715 STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx */
716 (void)(tmp);
717}
718#endif /* DATA_IN_ExtSRAM && DATA_IN_ExtSDRAM */
719/**
720 * @}
721 */
722
723/**
724 * @}
725 */
726
727/**
728 * @}
729 */
730/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
static void SystemInit_ExtMemCtl(void)
Setup the external memory controller. Called in startup_stm32f4xx.s before jump to main....
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