system_stm32f4xx.c


1
/**
2
  ******************************************************************************
3
  * @file    system_stm32f4xx.c
4
  * @author  MCD Application Team
5
  * @version V1.2.0RC2
6
  * @date    20-February-2013
7
  * @brief   CMSIS Cortex-M4 Device Peripheral Access Layer System Source File.
8
  *          This file contains the system clock configuration for STM32F4xx devices,
9
  *          and is generated by the clock configuration tool
10
  *          stm32f4xx_Clock_Configuration_V1.1.0.xls
11
  *
12
  * 1.  This file provides two functions and one global variable to be called from
13
  *     user application:
14
  *      - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
15
  *                      and Divider factors, AHB/APBx prescalers and Flash settings),
16
  *                      depending on the configuration made in the clock xls tool.
17
  *                      This function is called at startup just after reset and
18
  *                      before branch to main program. This call is made inside
19
  *                      the "startup_stm32f4xx.s" file.
20
  *
21
  *      - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
22
  *                                  by the user application to setup the SysTick
23
  *                                  timer or configure other parameters.
24
  *
25
  *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
26
  *                                 be called whenever the core clock is changed
27
  *                                 during program execution.
28
  *
29
  * 2. After each device reset the HSI (16 MHz) is used as system clock source.
30
  *    Then SystemInit() function is called, in "startup_stm32f4xx.s" file, to
31
  *    configure the system clock before to branch to main program.
32
  *
33
  * 3. If the system clock source selected by user fails to startup, the SystemInit()
34
  *    function will do nothing and HSI still used as system clock source. User can
35
  *    add some code to deal with this issue inside the SetSysClock() function.
36
  *
37
  * 4. The default value of HSE crystal is set to 25MHz, refer to "HSE_VALUE" define
38
  *    in "stm32f4xx.h" file. When HSE is used as system clock source, directly or
39
  *    through PLL, and you are using different crystal you have to adapt the HSE
40
  *    value to your own configuration.
41
  *
42
  * 5. This file configures the system clock as follows:
43
  *=============================================================================
44
  *=============================================================================
45
  *        Supported STM32F40xx/41xx/427x/437x/429x/439x devices
46
  *-----------------------------------------------------------------------------
47
  *        System Clock source                    | PLL (HSE)
48
  *-----------------------------------------------------------------------------
49
  *        SYSCLK(Hz)                             | 168000000
50
  *-----------------------------------------------------------------------------
51
  *        HCLK(Hz)                               | 168000000
52
  *-----------------------------------------------------------------------------
53
  *        AHB Prescaler                          | 1
54
  *-----------------------------------------------------------------------------
55
  *        APB1 Prescaler                         | 4
56
  *-----------------------------------------------------------------------------
57
  *        APB2 Prescaler                         | 2
58
  *-----------------------------------------------------------------------------
59
  *        HSE Frequency(Hz)                      | 25000000
60
  *-----------------------------------------------------------------------------
61
  *        PLL_M                                  | 25
62
  *-----------------------------------------------------------------------------
63
  *        PLL_N                                  | 336
64
  *-----------------------------------------------------------------------------
65
  *        PLL_P                                  | 2
66
  *-----------------------------------------------------------------------------
67
  *        PLL_Q                                  | 7
68
  *-----------------------------------------------------------------------------
69
  *        PLLI2S_N                               | NA
70
  *-----------------------------------------------------------------------------
71
  *        PLLI2S_R                               | NA
72
  *-----------------------------------------------------------------------------
73
  *        I2S input clock                        | NA
74
  *-----------------------------------------------------------------------------
75
  *        VDD(V)                                 | 3.3
76
  *-----------------------------------------------------------------------------
77
  *        Main regulator output voltage          | Scale1 mode
78
  *-----------------------------------------------------------------------------
79
  *        Flash Latency(WS)                      | 5
80
  *-----------------------------------------------------------------------------
81
  *        Prefetch Buffer                        | ON
82
  *-----------------------------------------------------------------------------
83
  *        Instruction cache                      | ON
84
  *-----------------------------------------------------------------------------
85
  *        Data cache                             | ON
86
  *-----------------------------------------------------------------------------
87
  *        Require 48MHz for USB OTG FS,          | Disabled
88
  *        SDIO and RNG clock                     |
89
  *-----------------------------------------------------------------------------
90
  *=============================================================================
91
  ******************************************************************************
92
  * @attention
93
  *
94
  * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
95
  *
96
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
97
  * You may not use this file except in compliance with the License.
98
  * You may obtain a copy of the License at:
99
  *
100
  *        http://www.st.com/software_license_agreement_liberty_v2
101
  *
102
  * Unless required by applicable law or agreed to in writing, software
103
  * distributed under the License is distributed on an "AS IS" BASIS,
104
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
105
  * See the License for the specific language governing permissions and
106
  * limitations under the License.
107
  *
108
  ******************************************************************************
109
  */
110
111
/** @addtogroup CMSIS
112
  * @{
113
  */
114
115
/** @addtogroup stm32f4xx_system
116
  * @{
117
  */
118
119
/** @addtogroup STM32F4xx_System_Private_Includes
120
  * @{
121
  */
122
123
#include "stm32f4xx.h"
124
125
/**
126
  * @}
127
  */
128
129
/** @addtogroup STM32F4xx_System_Private_TypesDefinitions
130
  * @{
131
  */
132
133
/**
134
  * @}
135
  */
136
137
/** @addtogroup STM32F4xx_System_Private_Defines
138
  * @{
139
  */
140
141
/************************* Miscellaneous Configuration ************************/
142
/*!< Uncomment the following line if you need to use external SRAM or SDRAM mounted
143
     on STM324xG_EVAL/STM324x7I_EVAL/STM324x9I_EVAL boards as data memory  */
144
/* #define DATA_IN_ExtSRAM */
145
/* #define DATA_IN_ExtSDRAM */
146
147
/*!< Uncomment the following line if you need to relocate your vector Table in
148
     Internal SRAM. */
149
/* #define VECT_TAB_SRAM */
150
#define VECT_TAB_OFFSET  0x00 /*!< Vector Table base offset field.
151
This value must be a multiple of 0x200. */
152
/******************************************************************************/
153
154
/************************* PLL Parameters *************************************/
155
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
156
#define PLL_M      25
157
#define PLL_N      336
158
159
/* SYSCLK = PLL_VCO / PLL_P */
160
#define PLL_P      2
161
162
/* USB OTG FS, SDIO and RNG Clock =  PLL_VCO / PLLQ */
163
#define PLL_Q      7
164
165
/******************************************************************************/
166
167
/**
168
  * @}
169
  */
170
171
/** @addtogroup STM32F4xx_System_Private_Macros
172
  * @{
173
  */
174
175
/**
176
  * @}
177
  */
178
179
/** @addtogroup STM32F4xx_System_Private_Variables
180
  * @{
181
  */
182
183
uint32_t SystemCoreClock = 168000000;
184
185
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
186
187
/**
188
  * @}
189
  */
190
191
/** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
192
  * @{
193
  */
194
195
static void SetSysClock(void);
196
#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
197
static void SystemInit_ExtMemCtl(void);
198
#endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
199
200
/**
201
  * @}
202
  */
203
204
/** @addtogroup STM32F4xx_System_Private_Functions
205
  * @{
206
  */
207
208
/**
209
  * @brief  Setup the microcontroller system
210
  *         Initialize the Embedded Flash Interface, the PLL and update the
211
  *         SystemFrequency variable.
212
  * @param  None
213
  * @retval None
214
  */
215
void SystemInit(void)
216
{
217
    /* FPU settings ------------------------------------------------------------*/
218
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
219
    SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */
220
#endif
221
    /* Reset the RCC clock configuration to the default reset state ------------*/
222
    /* Set HSION bit */
223
    RCC->CR |= (uint32_t)0x00000001;
224
225
    /* Reset CFGR register */
226
    RCC->CFGR = 0x00000000;
227
228
    /* Reset HSEON, CSSON and PLLON bits */
229
    RCC->CR &= (uint32_t)0xFEF6FFFF;
230
231
    /* Reset PLLCFGR register */
232
    RCC->PLLCFGR = 0x24003010;
233
234
    /* Reset HSEBYP bit */
235
    RCC->CR &= (uint32_t)0xFFFBFFFF;
236
237
    /* Disable all interrupts */
238
    RCC->CIR = 0x00000000;
239
240
#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
241
    SystemInit_ExtMemCtl();
242
#endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
243
244
    /* Configure the System clock source, PLL Multiplier and Divider factors,
245
       AHB/APBx prescalers and Flash settings ----------------------------------*/
246
    SetSysClock();
247
248
    /* Configure the Vector Table location add offset address ------------------*/
249
#ifdef VECT_TAB_SRAM
250
    SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
251
#else
252
    SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
253
#endif
254
}
255
256
/**
257
   * @brief  Update SystemCoreClock variable according to Clock Register Values.
258
  *         The SystemCoreClock variable contains the core clock (HCLK), it can
259
  *         be used by the user application to setup the SysTick timer or configure
260
  *         other parameters.
261
  *
262
  * @note   Each time the core clock (HCLK) changes, this function must be called
263
  *         to update SystemCoreClock variable value. Otherwise, any configuration
264
  *         based on this variable will be incorrect.
265
  *
266
  * @note   - The system frequency computed by this function is not the real
267
  *           frequency in the chip. It is calculated based on the predefined
268
  *           constant and the selected clock source:
269
  *
270
  *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
271
  *
272
  *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
273
  *
274
  *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
275
  *             or HSI_VALUE(*) multiplied/divided by the PLL factors.
276
  *
277
  *         (*) HSI_VALUE is a constant defined in stm32f4xx.h file (default value
278
  *             16 MHz) but the real value may vary depending on the variations
279
  *             in voltage and temperature.
280
  *
281
  *         (**) HSE_VALUE is a constant defined in stm32f4xx.h file (default value
282
  *              25 MHz), user has to ensure that HSE_VALUE is same as the real
283
  *              frequency of the crystal used. Otherwise, this function may
284
  *              have wrong result.
285
  *
286
  *         - The result of this function could be not correct when using fractional
287
  *           value for HSE crystal.
288
  *
289
  * @param  None
290
  * @retval None
291
  */
292
void SystemCoreClockUpdate(void)
293
{
294
    uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
295
296
    /* Get SYSCLK source -------------------------------------------------------*/
297
    tmp = RCC->CFGR & RCC_CFGR_SWS;
298
299
    switch (tmp)
300
    {
301
    case 0x00:  /* HSI used as system clock source */
302
        SystemCoreClock = HSI_VALUE;
303
        break;
304
    case 0x04:  /* HSE used as system clock source */
305
        SystemCoreClock = HSE_VALUE;
306
        break;
307
    case 0x08:  /* PLL used as system clock source */
308
309
        /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
310
           SYSCLK = PLL_VCO / PLL_P
311
           */
312
        pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
313
        pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
314
315
        if (pllsource != 0)
316
        {
317
            /* HSE used as PLL clock source */
318
            pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
319
        }
320
        else
321
        {
322
            /* HSI used as PLL clock source */
323
            pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
324
        }
325
326
        pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
327
        SystemCoreClock = pllvco/pllp;
328
        break;
329
    default:
330
        SystemCoreClock = HSI_VALUE;
331
        break;
332
    }
333
    /* Compute HCLK frequency --------------------------------------------------*/
334
    /* Get HCLK prescaler */
335
    tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
336
    /* HCLK frequency */
337
    SystemCoreClock >>= tmp;
338
}
339
340
/**
341
  * @brief  Configures the System clock source, PLL Multiplier and Divider factors,
342
  *         AHB/APBx prescalers and Flash settings
343
  * @Note   This function should be called only once the RCC clock configuration
344
  *         is reset to the default reset state (done in SystemInit() function).
345
  * @param  None
346
  * @retval None
347
  */
348
static void SetSysClock(void)
349
{
350
    /******************************************************************************/
351
    /*            PLL (clocked by HSE) used as System clock source                */
352
    /******************************************************************************/
353
    __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
354
355
    /* Enable HSE */
356
    RCC->CR |= ((uint32_t)RCC_CR_HSEON);
357
358
    /* Wait till HSE is ready and if Time out is reached exit */
359
    do
360
    {
361
        HSEStatus = RCC->CR & RCC_CR_HSERDY;
362
        StartUpCounter++;
363
    }
364
    while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
365
366
    if ((RCC->CR & RCC_CR_HSERDY) != RESET)
367
    {
368
        HSEStatus = (uint32_t)0x01;
369
    }
370
    else
371
    {
372
        HSEStatus = (uint32_t)0x00;
373
    }
374
375
    if (HSEStatus == (uint32_t)0x01)
376
    {
377
        /* Select regulator voltage output Scale 1 mode, System frequency up to 168 MHz */
378
        RCC->APB1ENR |= RCC_APB1ENR_PWREN;
379
        PWR->CR |= PWR_CR_VOS;
380
381
        /* HCLK = SYSCLK / 1*/
382
        RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
383
384
        /* PCLK2 = HCLK / 2*/
385
        RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
386
387
        /* PCLK1 = HCLK / 4*/
388
        RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
389
390
        /* Configure the main PLL */
391
        RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
392
                       (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
393
394
        /* Enable the main PLL */
395
        RCC->CR |= RCC_CR_PLLON;
396
397
        /* Wait till the main PLL is ready */
398
        while((RCC->CR & RCC_CR_PLLRDY) == 0)
399
        {
400
        }
401
402
        /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
403
        FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
404
405
        /* Select the main PLL as system clock source */
406
        RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
407
        RCC->CFGR |= RCC_CFGR_SW_PLL;
408
409
        /* Wait till the main PLL is used as system clock source */
410
        while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
411
        {
412
        }
413
    }
414
    else
415
    {
416
        /* If HSE fails to start-up, the application will have wrong clock
417
             configuration. User can add here some code to deal with this error */
418
    }
419
420
}
421
422
/**
423
  * @brief  Setup the external memory controller. Called in startup_stm32f4xx.s
424
  *          before jump to __main
425
  * @param  None
426
  * @retval None
427
  */
428
#ifdef DATA_IN_ExtSRAM
429
/**
430
  * @brief  Setup the external memory controller.
431
  *         Called in startup_stm32f4xx.s before jump to main.
432
  *         This function configures the external SRAM mounted on STM324xG_EVAL/STM324x7I boards
433
  *         This SRAM will be used as program data memory (including heap and stack).
434
  * @param  None
435
  * @retval None
436
  */
437
void SystemInit_ExtMemCtl(void)
438
{
439
    /*-- GPIOs Configuration -----------------------------------------------------*/
440
    /*
441
     +-------------------+--------------------+------------------+--------------+
442
     +                       SRAM pins assignment                               +
443
     +-------------------+--------------------+------------------+--------------+
444
     | PD0  <-> FMC_D2  | PE0  <-> FMC_NBL0 | PF0  <-> FMC_A0 | PG0 <-> FMC_A10 |
445
     | PD1  <-> FMC_D3  | PE1  <-> FMC_NBL1 | PF1  <-> FMC_A1 | PG1 <-> FMC_A11 |
446
     | PD4  <-> FMC_NOE | PE3  <-> FMC_A19  | PF2  <-> FMC_A2 | PG2 <-> FMC_A12 |
447
     | PD5  <-> FMC_NWE | PE4  <-> FMC_A20  | PF3  <-> FMC_A3 | PG3 <-> FMC_A13 |
448
     | PD8  <-> FMC_D13 | PE7  <-> FMC_D4   | PF4  <-> FMC_A4 | PG4 <-> FMC_A14 |
449
     | PD9  <-> FMC_D14 | PE8  <-> FMC_D5   | PF5  <-> FMC_A5 | PG5 <-> FMC_A15 |
450
     | PD10 <-> FMC_D15 | PE9  <-> FMC_D6   | PF12 <-> FMC_A6 | PG9 <-> FMC_NE2 |
451
     | PD11 <-> FMC_A16 | PE10 <-> FMC_D7   | PF13 <-> FMC_A7 |-----------------+
452
     | PD12 <-> FMC_A17 | PE11 <-> FMC_D8   | PF14 <-> FMC_A8 |
453
     | PD13 <-> FMC_A18 | PE12 <-> FMC_D9   | PF15 <-> FMC_A9 |
454
     | PD14 <-> FMC_D0  | PE13 <-> FMC_D10  |-----------------+
455
     | PD15 <-> FMC_D1  | PE14 <-> FMC_D11  |
456
     |                  | PE15 <-> FMC_D12  |
457
     +------------------+------------------+
458
    */
459
    /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
460
    RCC->AHB1ENR   |= 0x00000078;
461
462
    /* Connect PDx pins to FMC Alternate function */
463
    GPIOD->AFR[0]  = 0x00cc00cc;
464
    GPIOD->AFR[1]  = 0xcccccccc;
465
    /* Configure PDx pins in Alternate function mode */
466
    GPIOD->MODER   = 0xaaaa0a0a;
467
    /* Configure PDx pins speed to 100 MHz */
468
    GPIOD->OSPEEDR = 0xffff0f0f;
469
    /* Configure PDx pins Output type to push-pull */
470
    GPIOD->OTYPER  = 0x00000000;
471
    /* No pull-up, pull-down for PDx pins */
472
    GPIOD->PUPDR   = 0x00000000;
473
474
    /* Connect PEx pins to FMC Alternate function */
475
    GPIOE->AFR[0]  = 0xcccccccc;
476
    GPIOE->AFR[1]  = 0xcccccccc;
477
    /* Configure PEx pins in Alternate function mode */
478
    GPIOE->MODER   = 0xaaaaaaaa;
479
    /* Configure PEx pins speed to 100 MHz */
480
    GPIOE->OSPEEDR = 0xffffffff;
481
    /* Configure PEx pins Output type to push-pull */
482
    GPIOE->OTYPER  = 0x00000000;
483
    /* No pull-up, pull-down for PEx pins */
484
    GPIOE->PUPDR   = 0x00000000;
485
486
    /* Connect PFx pins to FMC Alternate function */
487
    GPIOF->AFR[0]  = 0x00cccccc;
488
    GPIOF->AFR[1]  = 0xcccc0000;
489
    /* Configure PFx pins in Alternate function mode */
490
    GPIOF->MODER   = 0xaa000aaa;
491
    /* Configure PFx pins speed to 100 MHz */
492
    GPIOF->OSPEEDR = 0xff000fff;
493
    /* Configure PFx pins Output type to push-pull */
494
    GPIOF->OTYPER  = 0x00000000;
495
    /* No pull-up, pull-down for PFx pins */
496
    GPIOF->PUPDR   = 0x00000000;
497
498
    /* Connect PGx pins to FMC Alternate function */
499
    GPIOG->AFR[0]  = 0x00cccccc;
500
    GPIOG->AFR[1]  = 0x000000c0;
501
    /* Configure PGx pins in Alternate function mode */
502
    GPIOG->MODER   = 0x00080aaa;
503
    /* Configure PGx pins speed to 100 MHz */
504
    GPIOG->OSPEEDR = 0x000c0fff;
505
    /* Configure PGx pins Output type to push-pull */
506
    GPIOG->OTYPER  = 0x00000000;
507
    /* No pull-up, pull-down for PGx pins */
508
    GPIOG->PUPDR   = 0x00000000;
509
510
    /*-- FMC Configuration ------------------------------------------------------*/
511
    /* Enable the FMC interface clock */
512
    RCC->AHB3ENR         |= 0x00000001;
513
514
    /* Configure and enable Bank1_SRAM2 */
515
    FMC_Bank1->BTCR[2]  = 0x00001011;
516
    FMC_Bank1->BTCR[3]  = 0x00000201;
517
    FMC_Bank1E->BWTR[2] = 0x0fffffff;
518
    /*
519
      Bank1_SRAM2 is configured as follow:
520
521
      NORSRAMTimingStructure.FMC_AddressSetupTime = 1;
522
      NORSRAMTimingStructure.FMC_AddressHoldTime = 0;
523
      NORSRAMTimingStructure.FMC_DataSetupTime = 2;
524
      NORSRAMTimingStructure.FMC_BusTurnAroundDuration = 0;
525
      NORSRAMTimingStructure.FMC_CLKDivision = 0;
526
      NORSRAMTimingStructure.FMC_DataLatency = 0;
527
      NORSRAMTimingStructure.FMC_AccessMode = FMC_AccessMode_A;
528
529
      FMC_NORSRAMInitStructure.FMC_Bank = FMC_Bank1_NORSRAM2;
530
      FMC_NORSRAMInitStructure.FMC_DataAddressMux = FMC_DataAddressMux_Disable;
531
      FMC_NORSRAMInitStructure.FMC_MemoryType = FMC_MemoryType_SRAM;
532
      FMC_NORSRAMInitStructure.FMC_MemoryDataWidth = FMC_MemoryDataWidth_16b;
533
      FMC_NORSRAMInitStructure.FMC_BurstAccessMode = FMC_BurstAccessMode_Disable;
534
      FMC_NORSRAMInitStructure.FMC_AsynchronousWait = FMC_AsynchronousWait_Disable;
535
      FMC_NORSRAMInitStructure.FMC_WaitSignalPolarity = FMC_WaitSignalPolarity_Low;
536
      FMC_NORSRAMInitStructure.FMC_WrapMode = FMC_WrapMode_Disable;
537
      FMC_NORSRAMInitStructure.FMC_WaitSignalActive = FMC_WaitSignalActive_BeforeWaitState;
538
      FMC_NORSRAMInitStructure.FMC_WriteOperation = FMC_WriteOperation_Enable;
539
      FMC_NORSRAMInitStructure.FMC_WaitSignal = FMC_WaitSignal_Disable;
540
      FMC_NORSRAMInitStructure.FMC_ExtendedMode = FMC_ExtendedMode_Disable;
541
      FMC_NORSRAMInitStructure.FMC_WriteBurst = FMC_WriteBurst_Disable;
542
      FMC_NORSRAMInitStructure.FMC_ContinousClock = FMC_CClock_SyncOnly;
543
      FMC_NORSRAMInitStructure.FMC_ReadWriteTimingStruct = &NORSRAMTimingStructure;
544
      FMC_NORSRAMInitStructure.FMC_WriteTimingStruct = &NORSRAMTimingStructure;
545
    */
546
547
}
548
#endif /* DATA_IN_ExtSRAM */
549
550
#ifdef DATA_IN_ExtSDRAM
551
/**
552
  * @brief  Setup the external memory controller.
553
  *         Called in startup_stm32f4xx.s before jump to main.
554
  *         This function configures the external SDRAM mounted on STM324x9I_EVAL board
555
  *         This SDRAM will be used as program data memory (including heap and stack).
556
  * @param  None
557
  * @retval None
558
  */
559
void SystemInit_ExtMemCtl(void)
560
{
561
    register uint32_t tmpreg = 0, timeout = 0xFFFF;
562
    register uint32_t index;
563
564
    /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface
565
        clock */
566
    RCC->AHB1ENR |= 0x000001FC;
567
568
    /* Connect PCx pins to FMC Alternate function */
569
    GPIOC->AFR[0]  = 0x0000000c;
570
    GPIOC->AFR[1]  = 0x00007700;
571
    /* Configure PCx pins in Alternate function mode */
572
    GPIOC->MODER   = 0x00a00002;
573
    /* Configure PCx pins speed to 50 MHz */
574
    GPIOC->OSPEEDR = 0x00a00002;
575
    /* Configure PCx pins Output type to push-pull */
576
    GPIOC->OTYPER  = 0x00000000;
577
    /* No pull-up, pull-down for PCx pins */
578
    GPIOC->PUPDR   = 0x00500000;
579
580
    /* Connect PDx pins to FMC Alternate function */
581
    GPIOD->AFR[0]  = 0x000000CC;
582
    GPIOD->AFR[1]  = 0xCC000CCC;
583
    /* Configure PDx pins in Alternate function mode */
584
    GPIOD->MODER   = 0xA02A000A;
585
    /* Configure PDx pins speed to 50 MHz */
586
    GPIOD->OSPEEDR = 0xA02A000A;
587
    /* Configure PDx pins Output type to push-pull */
588
    GPIOD->OTYPER  = 0x00000000;
589
    /* No pull-up, pull-down for PDx pins */
590
    GPIOD->PUPDR   = 0x00000000;
591
592
    /* Connect PEx pins to FMC Alternate function */
593
    GPIOE->AFR[0]  = 0xC00000CC;
594
    GPIOE->AFR[1]  = 0xCCCCCCCC;
595
    /* Configure PEx pins in Alternate function mode */
596
    GPIOE->MODER   = 0xAAAA800A;
597
    /* Configure PEx pins speed to 50 MHz */
598
    GPIOE->OSPEEDR = 0xAAAA800A;
599
    /* Configure PEx pins Output type to push-pull */
600
    GPIOE->OTYPER  = 0x00000000;
601
    /* No pull-up, pull-down for PEx pins */
602
    GPIOE->PUPDR   = 0x00000000;
603
604
    /* Connect PFx pins to FMC Alternate function */
605
    GPIOF->AFR[0]  = 0xcccccccc;
606
    GPIOF->AFR[1]  = 0xcccccccc;
607
    /* Configure PFx pins in Alternate function mode */
608
    GPIOF->MODER   = 0xAA800AAA;
609
    /* Configure PFx pins speed to 50 MHz */
610
    GPIOF->OSPEEDR = 0xAA800AAA;
611
    /* Configure PFx pins Output type to push-pull */
612
    GPIOF->OTYPER  = 0x00000000;
613
    /* No pull-up, pull-down for PFx pins */
614
    GPIOF->PUPDR   = 0x00000000;
615
616
    /* Connect PGx pins to FMC Alternate function */
617
    GPIOG->AFR[0]  = 0xcccccccc;
618
    GPIOG->AFR[1]  = 0xcccccccc;
619
    /* Configure PGx pins in Alternate function mode */
620
    GPIOG->MODER   = 0xaaaaaaaa;
621
    /* Configure PGx pins speed to 50 MHz */
622
    GPIOG->OSPEEDR = 0xaaaaaaaa;
623
    /* Configure PGx pins Output type to push-pull */
624
    GPIOG->OTYPER  = 0x00000000;
625
    /* No pull-up, pull-down for PGx pins */
626
    GPIOG->PUPDR   = 0x00000000;
627
628
    /* Connect PHx pins to FMC Alternate function */
629
    GPIOH->AFR[0]  = 0x00C0CC00;
630
    GPIOH->AFR[1]  = 0xCCCCCCCC;
631
    /* Configure PHx pins in Alternate function mode */
632
    GPIOH->MODER   = 0xAAAA08A0;
633
    /* Configure PHx pins speed to 50 MHz */
634
    GPIOH->OSPEEDR = 0xAAAA08A0;
635
    /* Configure PHx pins Output type to push-pull */
636
    GPIOH->OTYPER  = 0x00000000;
637
    /* No pull-up, pull-down for PHx pins */
638
    GPIOH->PUPDR   = 0x00000000;
639
640
    /* Connect PIx pins to FMC Alternate function */
641
    GPIOI->AFR[0]  = 0xCCCCCCCC;
642
    GPIOI->AFR[1]  = 0x00000CC0;
643
    /* Configure PIx pins in Alternate function mode */
644
    GPIOI->MODER   = 0x0028AAAA;
645
    /* Configure PIx pins speed to 50 MHz */
646
    GPIOI->OSPEEDR = 0x0028AAAA;
647
    /* Configure PIx pins Output type to push-pull */
648
    GPIOI->OTYPER  = 0x00000000;
649
    /* No pull-up, pull-down for PIx pins */
650
    GPIOI->PUPDR   = 0x00000000;
651
652
    /*-- FMC Configuration ------------------------------------------------------*/
653
    /* Enable the FMC interface clock */
654
    RCC->AHB3ENR |= 0x00000001;
655
656
    /* Configure and enable SDRAM bank1 */
657
    FMC_Bank5_6->SDCR[0] = 0x000029D0;
658
    FMC_Bank5_6->SDTR[0] = 0x01115351;
659
660
    /* SDRAM initialization sequence */
661
    /* Clock enable command */
662
    FMC_Bank5_6->SDCMR = 0x00000011;
663
    tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
664
    while((tmpreg != 0) & (timeout-- > 0))
665
    {
666
        tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
667
    }
668
669
    /* Delay */
670
    for (index = 0; index<1000; index++);
671
672
    /* PALL command */
673
    FMC_Bank5_6->SDCMR = 0x00000012;
674
    timeout = 0xFFFF;
675
    while((tmpreg != 0) & (timeout-- > 0))
676
    {
677
        tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
678
    }
679
680
    /* Auto refresh command */
681
    FMC_Bank5_6->SDCMR = 0x00000073;
682
    timeout = 0xFFFF;
683
    while((tmpreg != 0) & (timeout-- > 0))
684
    {
685
        tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
686
    }
687
688
    /* MRD register program */
689
    FMC_Bank5_6->SDCMR = 0x00046014;
690
    timeout = 0xFFFF;
691
    while((tmpreg != 0) & (timeout-- > 0))
692
    {
693
        tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
694
    }
695
696
    /* Set refresh count */
697
    tmpreg = FMC_Bank5_6->SDRTR;
698
    FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
699
700
    /* Disable write protection */
701
    tmpreg = FMC_Bank5_6->SDCR[0];
702
    FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
703
704
    /*
705
      Bank1_SDRAM is configured as follow:
706
707
      FMC_SDRAMTimingInitStructure.FMC_LoadToActiveDelay = 2;
708
      FMC_SDRAMTimingInitStructure.FMC_ExitSelfRefreshDelay = 6;
709
      FMC_SDRAMTimingInitStructure.FMC_SelfRefreshTime = 4;
710
      FMC_SDRAMTimingInitStructure.FMC_RowCycleDelay = 6;
711
      FMC_SDRAMTimingInitStructure.FMC_WriteRecoveryTime = 2;
712
      FMC_SDRAMTimingInitStructure.FMC_RPDelay = 2;
713
      FMC_SDRAMTimingInitStructure.FMC_RCDDelay = 2;
714
715
      FMC_SDRAMInitStructure.FMC_Bank = SDRAM_BANK;
716
      FMC_SDRAMInitStructure.FMC_ColumnBitsNumber = FMC_ColumnBits_Number_8b;
717
      FMC_SDRAMInitStructure.FMC_RowBitsNumber = FMC_RowBits_Number_11b;
718
      FMC_SDRAMInitStructure.FMC_SDMemoryDataWidth = FMC_SDMemory_Width_16b;
719
      FMC_SDRAMInitStructure.FMC_InternalBankNumber = FMC_InternalBank_Number_4;
720
      FMC_SDRAMInitStructure.FMC_CASLatency = FMC_CAS_Latency_3;
721
      FMC_SDRAMInitStructure.FMC_WriteProtection = FMC_Write_Protection_Disable;
722
      FMC_SDRAMInitStructure.FMC_SDClockPeriod = FMC_SDClock_Period_2;
723
      FMC_SDRAMInitStructure.FMC_ReadBurst = FMC_Read_Burst_disable;
724
      FMC_SDRAMInitStructure.FMC_ReadPipeDelay = FMC_ReadPipe_Delay_1;
725
      FMC_SDRAMInitStructure.FMC_SDRAMTimingStruct = &FMC_SDRAMTimingInitStructure;
726
    */
727
728
}
729
#endif /* DATA_IN_ExtSDRAM */
730
731
732
/**
733
  * @}
734
  */
735
736
/**
737
  * @}
738
  */
739
740
/**
741
  * @}
742
  */
743
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/