Advertisement
Guest User

Untitled

a guest
Sep 14th, 2015
1,841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 38.38 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    system_stm32f4xx.c
  4.   * @author  MCD Application Team
  5.   * @version V1.5.0
  6.   * @date    06-March-2015
  7.   * @brief   CMSIS Cortex-M4 Device Peripheral Access Layer System Source File.
  8.   *          This file contains the system clock configuration for STM32F4xx devices.
  9.   *            
  10.   * 1.  This file provides two functions and one global variable to be called from
  11.   *     user application:
  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.
  18.   *
  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.
  22.   *                                    
  23.   *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  24.   *                                 be called whenever the core clock is changed
  25.   *                                 during program execution.
  26.   *
  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.
  30.   *
  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.
  34.   *
  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.
  39.   *
  40.   * 5. This file configures the system clock as follows:
  41.   *=============================================================================
  42.   *                         Supported STM32F446xx devices
  43.   *-----------------------------------------------------------------------------
  44.   *        System Clock source                    | PLL (HSE)
  45.   *-----------------------------------------------------------------------------
  46.   *        SYSCLK(Hz)                             | 180000000
  47.   *-----------------------------------------------------------------------------
  48.   *        HCLK(Hz)                               | 180000000
  49.   *-----------------------------------------------------------------------------
  50.   *        AHB Prescaler                          | 1
  51.   *-----------------------------------------------------------------------------
  52.   *        APB1 Prescaler                         | 4
  53.   *-----------------------------------------------------------------------------
  54.   *        APB2 Prescaler                         | 2
  55.   *-----------------------------------------------------------------------------
  56.   *        HSE Frequency(Hz)                      | 16000000
  57.   *-----------------------------------------------------------------------------
  58.   *        PLL_M                                  | 8
  59.   *-----------------------------------------------------------------------------
  60.   *        PLL_N                                  | 180
  61.   *-----------------------------------------------------------------------------
  62.   *        PLL_P                                  | 2
  63.   *-----------------------------------------------------------------------------
  64.   *        PLL_Q                                  | 2
  65.   *-----------------------------------------------------------------------------
  66.   *        PLL_R                                  | NA
  67.   *-----------------------------------------------------------------------------
  68.   *        PLLI2S_M                               | NA
  69.   *-----------------------------------------------------------------------------
  70.   *        PLLI2S_N                               | NA
  71.   *-----------------------------------------------------------------------------
  72.   *        PLLI2S_P                               | NA
  73.   *-----------------------------------------------------------------------------
  74.   *        PLLI2S_Q                               | NA
  75.   *-----------------------------------------------------------------------------
  76.   *        PLLI2S_R                               | NA
  77.   *-----------------------------------------------------------------------------
  78.   *        I2S input clock                        | NA
  79.   *-----------------------------------------------------------------------------
  80.   *        VDD(V)                                 | 3.3
  81.   *-----------------------------------------------------------------------------
  82.   *        Main regulator output voltage          | Scale1 mode
  83.   *-----------------------------------------------------------------------------
  84.   *        Flash Latency(WS)                      | 5
  85.   *-----------------------------------------------------------------------------
  86.   *        Prefetch Buffer                        | ON
  87.   *-----------------------------------------------------------------------------
  88.   *        Instruction cache                      | ON
  89.   *-----------------------------------------------------------------------------
  90.   *        Data cache                             | ON
  91.   *-----------------------------------------------------------------------------
  92.   *        Require 48MHz for USB OTG FS,          | Disabled
  93.   *        SDIO and RNG clock                     |
  94.   *-----------------------------------------------------------------------------
  95.   *=============================================================================
  96.   ******************************************************************************
  97.   * @attention
  98.   *
  99.   * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
  100.   *
  101.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  102.   * You may not use this file except in compliance with the License.
  103.   * You may obtain a copy of the License at:
  104.   *
  105.   *        http://www.st.com/software_license_agreement_liberty_v2
  106.   *
  107.   * Unless required by applicable law or agreed to in writing, software
  108.   * distributed under the License is distributed on an "AS IS" BASIS,
  109.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  110.   * See the License for the specific language governing permissions and
  111.   * limitations under the License.
  112.   *
  113.   ******************************************************************************
  114.   */
  115.  
  116. /** @addtogroup CMSIS
  117.   * @{
  118.   */
  119.  
  120. /** @addtogroup stm32f4xx_system
  121.   * @{
  122.   */  
  123.  
  124. /** @addtogroup STM32F4xx_System_Private_Includes
  125.   * @{
  126.   */
  127.  
  128. #include "stm32f4xx.h"
  129.  
  130. /**
  131.   * @}
  132.   */
  133.  
  134. /** @addtogroup STM32F4xx_System_Private_TypesDefinitions
  135.   * @{
  136.   */
  137.  
  138. /**
  139.   * @}
  140.   */
  141.  
  142. /** @addtogroup STM32F4xx_System_Private_Defines
  143.   * @{
  144.   */
  145.  
  146. /************************* Miscellaneous Configuration ************************/
  147. /*!< Uncomment the following line if you need to use external SRAM or SDRAM mounted
  148.      on STM324xG_EVAL/STM324x7I_EVAL/STM324x9I_EVAL boards as data memory  */    
  149. #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx)
  150. /* #define DATA_IN_ExtSRAM */
  151. #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx */
  152.  
  153. #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
  154. /* #define DATA_IN_ExtSDRAM */
  155. #endif /* STM32F427_437x || STM32F429_439xx || STM32F446xx */
  156.  
  157. #if defined(STM32F411xE)    
  158. /*!< Uncomment the following line if you need to clock the STM32F411xE by HSE Bypass
  159.      through STLINK MCO pin of STM32F103 microcontroller. The frequency cannot be changed
  160.      and is fixed at 8 MHz.
  161.      Hardware configuration needed for Nucleo Board:
  162.      – SB54, SB55 OFF
  163.      – R35 removed
  164.      – SB16, SB50 ON */
  165. /* #define USE_HSE_BYPASS */
  166.  
  167. #if defined(USE_HSE_BYPASS)    
  168. #define HSE_BYPASS_INPUT_FREQUENCY   8000000
  169. #endif /* USE_HSE_BYPASS */    
  170. #endif /* STM32F411xE */
  171.    
  172. /*!< Uncomment the following line if you need to relocate your vector Table in
  173.      Internal SRAM. */
  174. /* #define VECT_TAB_SRAM */
  175. #define VECT_TAB_OFFSET  0x00 /*!< Vector Table base offset field.
  176.                                    This value must be a multiple of 0x200. */
  177. /******************************************************************************/
  178.  
  179. /************************* PLL Parameters *************************************/
  180. #define PLL_M      8
  181. #define PLL_N      180
  182. #define PLL_P      2
  183. #define PLL_Q      2
  184. #define PLL_R      2
  185.  
  186. #define PLLSAI_M      8
  187. #define PLLSAI_N      192
  188. #define PLLSAI_P      8
  189. #define PLLSAI_Q      2
  190. /******************************************************************************/
  191.  
  192. /**
  193.   * @}
  194.   */
  195.  
  196. /** @addtogroup STM32F4xx_System_Private_Macros
  197.   * @{
  198.   */
  199.  
  200. /**
  201.   * @}
  202.   */
  203.  
  204. /** @addtogroup STM32F4xx_System_Private_Variables
  205.   * @{
  206.   */
  207.  
  208. #if defined(STM32F40_41xxx)
  209.   uint32_t SystemCoreClock = 168000000;
  210. #endif /* STM32F40_41xxx */
  211.  
  212. #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
  213.   uint32_t SystemCoreClock = 180000000;
  214. #endif /* STM32F427_437x || STM32F429_439xx || STM32F446xx */
  215.  
  216. #if defined(STM32F401xx)
  217.   uint32_t SystemCoreClock = 84000000;
  218. #endif /* STM32F401xx */
  219.  
  220. #if defined(STM32F411xE)
  221.   uint32_t SystemCoreClock = 100000000;
  222. #endif /* STM32F401xx */
  223.  
  224. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  225.  
  226. /**
  227.   * @}
  228.   */
  229.  
  230. /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
  231.   * @{
  232.   */
  233.  
  234. static void SetSysClock(void);
  235.  
  236. #if defined(DATA_IN_ExtSRAM) || defined(DATA_IN_ExtSDRAM)
  237. static void SystemInit_ExtMemCtl(void);
  238. #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
  239.  
  240. /**
  241.   * @}
  242.   */
  243.  
  244. /** @addtogroup STM32F4xx_System_Private_Functions
  245.   * @{
  246.   */
  247.  
  248. /**
  249.   * @brief  Setup the microcontroller system
  250.   *         Initialize the Embedded Flash Interface, the PLL and update the
  251.   *         SystemFrequency variable.
  252.   * @param  None
  253.   * @retval None
  254.   */
  255. void SystemInit(void)
  256. {
  257.   /* FPU settings ------------------------------------------------------------*/
  258.   #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  259.     SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */
  260.   #endif
  261.   /* Reset the RCC clock configuration to the default reset state ------------*/
  262.   /* Set HSION bit */
  263.   RCC->CR |= (uint32_t)0x00000001;
  264.  
  265.   /* Reset CFGR register */
  266.   RCC->CFGR = 0x00000000;
  267.  
  268.   /* Reset HSEON, CSSON and PLLON bits */
  269.   RCC->CR &= (uint32_t)0xFEF6FFFF;
  270.  
  271.   /* Reset PLLCFGR register */
  272.   RCC->PLLCFGR = 0x24003010;
  273.  
  274.   /* Reset HSEBYP bit */
  275.   RCC->CR &= (uint32_t)0xFFFBFFFF;
  276.  
  277.   /* Disable all interrupts */
  278.   RCC->CIR = 0x00000000;
  279.  
  280. #if defined(DATA_IN_ExtSRAM) || defined(DATA_IN_ExtSDRAM)
  281.   SystemInit_ExtMemCtl();
  282. #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
  283.          
  284.   /* Configure the System clock source, PLL Multiplier and Divider factors,
  285.      AHB/APBx prescalers and Flash settings ----------------------------------*/
  286.   SetSysClock();
  287.  
  288.   /* Configure the Vector Table location add offset address ------------------*/
  289. #ifdef VECT_TAB_SRAM
  290.   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  291. #else
  292.   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  293. #endif
  294. }
  295.  
  296. /**
  297.    * @brief  Update SystemCoreClock variable according to Clock Register Values.
  298.   *         The SystemCoreClock variable contains the core clock (HCLK), it can
  299.   *         be used by the user application to setup the SysTick timer or configure
  300.   *         other parameters.
  301.   *          
  302.   * @note   Each time the core clock (HCLK) changes, this function must be called
  303.   *         to update SystemCoreClock variable value. Otherwise, any configuration
  304.   *         based on this variable will be incorrect.        
  305.   *    
  306.   * @note   - The system frequency computed by this function is not the real
  307.   *           frequency in the chip. It is calculated based on the predefined
  308.   *           constant and the selected clock source:
  309.   *            
  310.   *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  311.   *                                              
  312.   *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  313.   *                          
  314.   *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  315.   *             or HSI_VALUE(*) multiplied/divided by the PLL factors.
  316.   *        
  317.   *         (*) HSI_VALUE is a constant defined in stm32f4xx.h file (default value
  318.   *             16 MHz) but the real value may vary depending on the variations
  319.   *             in voltage and temperature.  
  320.   *    
  321.   *         (**) HSE_VALUE is a constant defined in stm32f4xx.h file (default value
  322.   *              25 MHz), user has to ensure that HSE_VALUE is same as the real
  323.   *              frequency of the crystal used. Otherwise, this function may
  324.   *              have wrong result.
  325.   *                
  326.   *         - The result of this function could be not correct when using fractional
  327.   *           value for HSE crystal.
  328.   *    
  329.   * @param  None
  330.   * @retval None
  331.   */
  332. void SystemCoreClockUpdate(void)
  333. {
  334.   uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  335. #if defined(STM32F446xx)  
  336.   uint32_t pllr = 2;
  337. #endif /* STM32F446xx */
  338.   /* Get SYSCLK source -------------------------------------------------------*/
  339.   tmp = RCC->CFGR & RCC_CFGR_SWS;
  340.  
  341.   switch (tmp)
  342.   {
  343.     case 0x00:  /* HSI used as system clock source */
  344.       SystemCoreClock = HSI_VALUE;
  345.       break;
  346.     case 0x04:  /* HSE used as system clock source */
  347.       SystemCoreClock = HSE_VALUE;
  348.       break;
  349.     case 0x08:  /* PLL P used as system clock source */
  350.        /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  351.          SYSCLK = PLL_VCO / PLL_P
  352.          */    
  353.       pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  354.       pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  355.      
  356. #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx) || defined(STM32F446xx)
  357.       if (pllsource != 0)
  358.       {
  359.         /* HSE used as PLL clock source */
  360.         pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  361.       }
  362.       else
  363.       {
  364.         /* HSI used as PLL clock source */
  365.         pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);      
  366.       }
  367. #elif defined(STM32F411xE)
  368. #if defined(USE_HSE_BYPASS)
  369.       if (pllsource != 0)
  370.       {
  371.         /* HSE used as PLL clock source */
  372.         pllvco = (HSE_BYPASS_INPUT_FREQUENCY / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  373.       }  
  374. #else  
  375.       if (pllsource == 0)
  376.       {
  377.         /* HSI used as PLL clock source */
  378.         pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  379.       }  
  380. #endif /* USE_HSE_BYPASS */  
  381. #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx || STM32F446xx */  
  382.       pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
  383.       SystemCoreClock = pllvco/pllp;      
  384.       break;
  385. #if defined(STM32F446xx)      
  386.       case 0x0C:  /* PLL R used as system clock source */
  387.        /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  388.          SYSCLK = PLL_VCO / PLL_R
  389.          */    
  390.       pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  391.       pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  392.       if (pllsource != 0)
  393.       {
  394.         /* HSE used as PLL clock source */
  395.         pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  396.       }
  397.       else
  398.       {
  399.         /* HSI used as PLL clock source */
  400.         pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);      
  401.       }
  402.  
  403.       pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >>28) + 1 ) *2;
  404.       SystemCoreClock = pllvco/pllr;      
  405.       break;
  406. #endif /* STM32F446xx */
  407.     default:
  408.       SystemCoreClock = HSI_VALUE;
  409.       break;
  410.   }
  411.   /* Compute HCLK frequency --------------------------------------------------*/
  412.   /* Get HCLK prescaler */
  413.   tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  414.   /* HCLK frequency */
  415.   SystemCoreClock >>= tmp;
  416. }
  417.  
  418. /**
  419.   * @brief  Configures the System clock source, PLL Multiplier and Divider factors,
  420.   *         AHB/APBx prescalers and Flash settings
  421.   * @Note   This function should be called only once the RCC clock configuration  
  422.   *         is reset to the default reset state (done in SystemInit() function).  
  423.   * @param  None
  424.   * @retval None
  425.   */
  426. static void SetSysClock(void)
  427. {
  428. #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx) || defined(STM32F446xx)
  429. /******************************************************************************/
  430. /*            PLL (clocked by HSE) used as System clock source                */
  431. /******************************************************************************/
  432.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  433.  
  434.   /* Enable HSE */
  435.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  436.  
  437.   /* Wait till HSE is ready and if Time out is reached exit */
  438.   do
  439.   {
  440.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  441.     StartUpCounter++;
  442.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  443.  
  444.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  445.   {
  446.     HSEStatus = (uint32_t)0x01;
  447.   }
  448.   else
  449.   {
  450.     HSEStatus = (uint32_t)0x00;
  451.   }
  452.  
  453.   if (HSEStatus == (uint32_t)0x01)
  454.   {
  455.     /* Select regulator voltage output Scale 1 mode */
  456.     RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  457.     PWR->CR |= PWR_CR_VOS;
  458.  
  459.     /* HCLK = SYSCLK / 1*/
  460.     RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
  461.  
  462. #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)    
  463.     /* PCLK2 = HCLK / 2*/
  464.     RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
  465.    
  466.     /* PCLK1 = HCLK / 4*/
  467.     RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
  468. #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx || STM32F446xx */
  469.  
  470. #if defined(STM32F401xx)
  471.     /* PCLK2 = HCLK / 2*/
  472.     RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
  473.    
  474.     /* PCLK1 = HCLK / 4*/
  475.     RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
  476. #endif /* STM32F401xx */
  477.  
  478. #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx)    
  479.     /* Configure the main PLL */
  480.     RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
  481.                    (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
  482. #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx || STM32F401xx */
  483.  
  484. #if defined(STM32F446xx)
  485.     /* Configure the main PLL */
  486.     RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
  487.                    (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24) | (PLL_R << 28);
  488. #endif /* STM32F446xx */    
  489.    
  490.     /* Enable the main PLL */
  491.     RCC->CR |= RCC_CR_PLLON;
  492.  
  493.     /* Wait till the main PLL is ready */
  494.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  495.     {
  496.     }
  497.    
  498. #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
  499.     /* Enable the Over-drive to extend the clock frequency to 180 Mhz */
  500.     PWR->CR |= PWR_CR_ODEN;
  501.     while((PWR->CSR & PWR_CSR_ODRDY) == 0)
  502.     {
  503.     }
  504.     PWR->CR |= PWR_CR_ODSWEN;
  505.     while((PWR->CSR & PWR_CSR_ODSWRDY) == 0)
  506.     {
  507.     }      
  508.     /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  509.     FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
  510. #endif /* STM32F427_437x || STM32F429_439xx || STM32F446xx */
  511.  
  512. #if defined(STM32F40_41xxx)    
  513.     /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  514.     FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
  515. #endif /* STM32F40_41xxx  */
  516.  
  517. #if defined(STM32F401xx)
  518.     /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  519.     FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
  520. #endif /* STM32F401xx */
  521.  
  522.     /* Select the main PLL as system clock source */
  523.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  524.     RCC->CFGR |= RCC_CFGR_SW_PLL;
  525.  
  526.     /* Wait till the main PLL is used as system clock source */
  527.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
  528.     {
  529.     }
  530.   }
  531.   else
  532.   { /* If HSE fails to start-up, the application will have wrong clock
  533.          configuration. User can add here some code to deal with this error */
  534.   }
  535. #elif defined(STM32F411xE)
  536. #if defined(USE_HSE_BYPASS)
  537. /******************************************************************************/
  538. /*            PLL (clocked by HSE) used as System clock source                */
  539. /******************************************************************************/
  540.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  541.  
  542.   /* Enable HSE and HSE BYPASS */
  543.   RCC->CR |= ((uint32_t)RCC_CR_HSEON | RCC_CR_HSEBYP);
  544.  
  545.   /* Wait till HSE is ready and if Time out is reached exit */
  546.   do
  547.   {
  548.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  549.     StartUpCounter++;
  550.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  551.  
  552.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  553.   {
  554.     HSEStatus = (uint32_t)0x01;
  555.   }
  556.   else
  557.   {
  558.     HSEStatus = (uint32_t)0x00;
  559.   }
  560.  
  561.   if (HSEStatus == (uint32_t)0x01)
  562.   {
  563.     /* Select regulator voltage output Scale 1 mode */
  564.     RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  565.     PWR->CR |= PWR_CR_VOS;
  566.  
  567.     /* HCLK = SYSCLK / 1*/
  568.     RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
  569.  
  570.     /* PCLK2 = HCLK / 2*/
  571.     RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
  572.    
  573.     /* PCLK1 = HCLK / 4*/
  574.     RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
  575.  
  576.     /* Configure the main PLL */
  577.     RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
  578.                    (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
  579.    
  580.     /* Enable the main PLL */
  581.     RCC->CR |= RCC_CR_PLLON;
  582.  
  583.     /* Wait till the main PLL is ready */
  584.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  585.     {
  586.     }
  587.  
  588.     /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  589.     FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
  590.  
  591.     /* Select the main PLL as system clock source */
  592.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  593.     RCC->CFGR |= RCC_CFGR_SW_PLL;
  594.  
  595.     /* Wait till the main PLL is used as system clock source */
  596.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
  597.     {
  598.     }
  599.   }
  600.   else
  601.   { /* If HSE fails to start-up, the application will have wrong clock
  602.          configuration. User can add here some code to deal with this error */
  603.   }
  604. #else /* HSI will be used as PLL clock source */
  605.   /* Select regulator voltage output Scale 1 mode */
  606.   RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  607.   PWR->CR |= PWR_CR_VOS;
  608.  
  609.   /* HCLK = SYSCLK / 1*/
  610.   RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
  611.  
  612.   /* PCLK2 = HCLK / 2*/
  613.   RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
  614.  
  615.   /* PCLK1 = HCLK / 4*/
  616.   RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
  617.  
  618.   /* Configure the main PLL */
  619.   RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) | (PLL_Q << 24);
  620.  
  621.   /* Enable the main PLL */
  622.   RCC->CR |= RCC_CR_PLLON;
  623.  
  624.   /* Wait till the main PLL is ready */
  625.   while((RCC->CR & RCC_CR_PLLRDY) == 0)
  626.   {
  627.   }
  628.  
  629.   /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  630.   FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
  631.  
  632.   /* Select the main PLL as system clock source */
  633.   RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  634.   RCC->CFGR |= RCC_CFGR_SW_PLL;
  635.  
  636.   /* Wait till the main PLL is used as system clock source */
  637.   while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
  638.   {
  639.   }
  640. #endif /* USE_HSE_BYPASS */  
  641. #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx */  
  642.  
  643. #if defined(STM32F446xx)
  644.   /* Configure 48MHz clock for USB */
  645.   RCC_PLLSAICmd(DISABLE);
  646.   RCC_PLLSAIConfig(PLLSAI_M, PLLSAI_N, PLLSAI_P, PLLSAI_Q);
  647.   RCC_48MHzClockSourceConfig(RCC_48MHZCLKSource_PLLSAI);
  648.   while (!(RCC->DCKCFGR & RCC_DCKCFGR_CK48MSEL))
  649.   {}
  650.   RCC_PLLSAICmd(ENABLE);
  651. #endif /* STM32F446xx */
  652.  
  653. }
  654.  
  655. /**
  656.   * @brief  Setup the external memory controller. Called in startup_stm32f4xx.s
  657.   *          before jump to __main
  658.   * @param  None
  659.   * @retval None
  660.   */
  661. #ifdef DATA_IN_ExtSRAM
  662. /**
  663.   * @brief  Setup the external memory controller.
  664.   *         Called in startup_stm32f4xx.s before jump to main.
  665.   *         This function configures the external SRAM mounted on STM324xG_EVAL/STM324x7I boards
  666.   *         This SRAM will be used as program data memory (including heap and stack).
  667.   * @param  None
  668.   * @retval None
  669.   */
  670. void SystemInit_ExtMemCtl(void)
  671. {
  672. /*-- GPIOs Configuration -----------------------------------------------------*/
  673. /*
  674.  +-------------------+--------------------+------------------+--------------+
  675.  +                       SRAM pins assignment                               +
  676.  +-------------------+--------------------+------------------+--------------+
  677.  | PD0  <-> FMC_D2  | PE0  <-> FMC_NBL0 | PF0  <-> FMC_A0 | PG0 <-> FMC_A10 |
  678.  | PD1  <-> FMC_D3  | PE1  <-> FMC_NBL1 | PF1  <-> FMC_A1 | PG1 <-> FMC_A11 |
  679.  | PD4  <-> FMC_NOE | PE3  <-> FMC_A19  | PF2  <-> FMC_A2 | PG2 <-> FMC_A12 |
  680.  | PD5  <-> FMC_NWE | PE4  <-> FMC_A20  | PF3  <-> FMC_A3 | PG3 <-> FMC_A13 |
  681.  | PD8  <-> FMC_D13 | PE7  <-> FMC_D4   | PF4  <-> FMC_A4 | PG4 <-> FMC_A14 |
  682.  | PD9  <-> FMC_D14 | PE8  <-> FMC_D5   | PF5  <-> FMC_A5 | PG5 <-> FMC_A15 |
  683.  | PD10 <-> FMC_D15 | PE9  <-> FMC_D6   | PF12 <-> FMC_A6 | PG9 <-> FMC_NE2 |
  684.  | PD11 <-> FMC_A16 | PE10 <-> FMC_D7   | PF13 <-> FMC_A7 |-----------------+
  685.  | PD12 <-> FMC_A17 | PE11 <-> FMC_D8   | PF14 <-> FMC_A8 |
  686.  | PD13 <-> FMC_A18 | PE12 <-> FMC_D9   | PF15 <-> FMC_A9 |
  687.  | PD14 <-> FMC_D0  | PE13 <-> FMC_D10  |-----------------+
  688.  | PD15 <-> FMC_D1  | PE14 <-> FMC_D11  |
  689.  |                  | PE15 <-> FMC_D12  |
  690.  +------------------+------------------+
  691. */
  692.    /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
  693.   RCC->AHB1ENR   |= 0x00000078;
  694.  
  695.   /* Connect PDx pins to FMC Alternate function */
  696.   GPIOD->AFR[0]  = 0x00cc00cc;
  697.   GPIOD->AFR[1]  = 0xcccccccc;
  698.   /* Configure PDx pins in Alternate function mode */  
  699.   GPIOD->MODER   = 0xaaaa0a0a;
  700.   /* Configure PDx pins speed to 100 MHz */  
  701.   GPIOD->OSPEEDR = 0xffff0f0f;
  702.   /* Configure PDx pins Output type to push-pull */  
  703.   GPIOD->OTYPER  = 0x00000000;
  704.   /* No pull-up, pull-down for PDx pins */
  705.   GPIOD->PUPDR   = 0x00000000;
  706.  
  707.   /* Connect PEx pins to FMC Alternate function */
  708.   GPIOE->AFR[0]  = 0xcccccccc;
  709.   GPIOE->AFR[1]  = 0xcccccccc;
  710.   /* Configure PEx pins in Alternate function mode */
  711.   GPIOE->MODER   = 0xaaaaaaaa;
  712.   /* Configure PEx pins speed to 100 MHz */
  713.   GPIOE->OSPEEDR = 0xffffffff;
  714.   /* Configure PEx pins Output type to push-pull */  
  715.   GPIOE->OTYPER  = 0x00000000;
  716.   /* No pull-up, pull-down for PEx pins */
  717.   GPIOE->PUPDR   = 0x00000000;
  718.  
  719.   /* Connect PFx pins to FMC Alternate function */
  720.   GPIOF->AFR[0]  = 0x00cccccc;
  721.   GPIOF->AFR[1]  = 0xcccc0000;
  722.   /* Configure PFx pins in Alternate function mode */  
  723.   GPIOF->MODER   = 0xaa000aaa;
  724.   /* Configure PFx pins speed to 100 MHz */
  725.   GPIOF->OSPEEDR = 0xff000fff;
  726.   /* Configure PFx pins Output type to push-pull */  
  727.   GPIOF->OTYPER  = 0x00000000;
  728.   /* No pull-up, pull-down for PFx pins */
  729.   GPIOF->PUPDR   = 0x00000000;
  730.  
  731.   /* Connect PGx pins to FMC Alternate function */
  732.   GPIOG->AFR[0]  = 0x00cccccc;
  733.   GPIOG->AFR[1]  = 0x000000c0;
  734.   /* Configure PGx pins in Alternate function mode */
  735.   GPIOG->MODER   = 0x00080aaa;
  736.   /* Configure PGx pins speed to 100 MHz */
  737.   GPIOG->OSPEEDR = 0x000c0fff;
  738.   /* Configure PGx pins Output type to push-pull */  
  739.   GPIOG->OTYPER  = 0x00000000;
  740.   /* No pull-up, pull-down for PGx pins */
  741.   GPIOG->PUPDR   = 0x00000000;
  742.  
  743. /*-- FMC Configuration ------------------------------------------------------*/
  744.   /* Enable the FMC/FSMC interface clock */
  745.   RCC->AHB3ENR         |= 0x00000001;
  746.  
  747. #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx)
  748.   /* Configure and enable Bank1_SRAM2 */
  749.   FMC_Bank1->BTCR[2]  = 0x00001011;
  750.   FMC_Bank1->BTCR[3]  = 0x00000201;
  751.   FMC_Bank1E->BWTR[2] = 0x0fffffff;
  752. #endif /* STM32F427_437xx || STM32F429_439xx */
  753.  
  754. #if defined(STM32F40_41xxx)
  755.   /* Configure and enable Bank1_SRAM2 */
  756.   FSMC_Bank1->BTCR[2]  = 0x00001011;
  757.   FSMC_Bank1->BTCR[3]  = 0x00000201;
  758.   FSMC_Bank1E->BWTR[2] = 0x0fffffff;
  759. #endif  /* STM32F40_41xxx */
  760.  
  761. /*
  762.   Bank1_SRAM2 is configured as follow:
  763.   In case of FSMC configuration
  764.   NORSRAMTimingStructure.FSMC_AddressSetupTime = 1;
  765.   NORSRAMTimingStructure.FSMC_AddressHoldTime = 0;
  766.   NORSRAMTimingStructure.FSMC_DataSetupTime = 2;
  767.   NORSRAMTimingStructure.FSMC_BusTurnAroundDuration = 0;
  768.   NORSRAMTimingStructure.FSMC_CLKDivision = 0;
  769.   NORSRAMTimingStructure.FSMC_DataLatency = 0;
  770.   NORSRAMTimingStructure.FSMC_AccessMode = FMC_AccessMode_A;
  771.  
  772.   FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
  773.   FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  774.   FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  775.   FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  776.   FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  777.   FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;  
  778.   FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  779.   FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  780.   FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  781.   FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  782.   FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  783.   FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  784.   FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  785.   FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &NORSRAMTimingStructure;
  786.   FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &NORSRAMTimingStructure;
  787.  
  788.   In case of FMC configuration  
  789.   NORSRAMTimingStructure.FMC_AddressSetupTime = 1;
  790.   NORSRAMTimingStructure.FMC_AddressHoldTime = 0;
  791.   NORSRAMTimingStructure.FMC_DataSetupTime = 2;
  792.   NORSRAMTimingStructure.FMC_BusTurnAroundDuration = 0;
  793.   NORSRAMTimingStructure.FMC_CLKDivision = 0;
  794.   NORSRAMTimingStructure.FMC_DataLatency = 0;
  795.   NORSRAMTimingStructure.FMC_AccessMode = FMC_AccessMode_A;
  796.  
  797.   FMC_NORSRAMInitStructure.FMC_Bank = FMC_Bank1_NORSRAM2;
  798.   FMC_NORSRAMInitStructure.FMC_DataAddressMux = FMC_DataAddressMux_Disable;
  799.   FMC_NORSRAMInitStructure.FMC_MemoryType = FMC_MemoryType_SRAM;
  800.   FMC_NORSRAMInitStructure.FMC_MemoryDataWidth = FMC_MemoryDataWidth_16b;
  801.   FMC_NORSRAMInitStructure.FMC_BurstAccessMode = FMC_BurstAccessMode_Disable;
  802.   FMC_NORSRAMInitStructure.FMC_AsynchronousWait = FMC_AsynchronousWait_Disable;  
  803.   FMC_NORSRAMInitStructure.FMC_WaitSignalPolarity = FMC_WaitSignalPolarity_Low;
  804.   FMC_NORSRAMInitStructure.FMC_WrapMode = FMC_WrapMode_Disable;
  805.   FMC_NORSRAMInitStructure.FMC_WaitSignalActive = FMC_WaitSignalActive_BeforeWaitState;
  806.   FMC_NORSRAMInitStructure.FMC_WriteOperation = FMC_WriteOperation_Enable;
  807.   FMC_NORSRAMInitStructure.FMC_WaitSignal = FMC_WaitSignal_Disable;
  808.   FMC_NORSRAMInitStructure.FMC_ExtendedMode = FMC_ExtendedMode_Disable;
  809.   FMC_NORSRAMInitStructure.FMC_WriteBurst = FMC_WriteBurst_Disable;
  810.   FMC_NORSRAMInitStructure.FMC_ContinousClock = FMC_CClock_SyncOnly;
  811.   FMC_NORSRAMInitStructure.FMC_ReadWriteTimingStruct = &NORSRAMTimingStructure;
  812.   FMC_NORSRAMInitStructure.FMC_WriteTimingStruct = &NORSRAMTimingStructure;
  813. */
  814.  
  815. }
  816. #endif /* DATA_IN_ExtSRAM */
  817.  
  818. #ifdef DATA_IN_ExtSDRAM
  819. /**
  820.   * @brief  Setup the external memory controller.
  821.   *         Called in startup_stm32f4xx.s before jump to main.
  822.   *         This function configures the external SDRAM mounted on STM324x9I_EVAL board
  823.   *         This SDRAM will be used as program data memory (including heap and stack).
  824.   * @param  None
  825.   * @retval None
  826.   */
  827. void SystemInit_ExtMemCtl(void)
  828. {
  829.   register uint32_t tmpreg = 0, timeout = 0xFFFF;
  830.   register uint32_t index;
  831.  
  832.   /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface
  833.       clock */
  834.   RCC->AHB1ENR |= 0x000001FC;
  835.  
  836.   /* Connect PCx pins to FMC Alternate function */
  837.   GPIOC->AFR[0]  = 0x0000000c;
  838.   GPIOC->AFR[1]  = 0x00007700;
  839.   /* Configure PCx pins in Alternate function mode */  
  840.   GPIOC->MODER   = 0x00a00002;
  841.   /* Configure PCx pins speed to 50 MHz */  
  842.   GPIOC->OSPEEDR = 0x00a00002;
  843.   /* Configure PCx pins Output type to push-pull */  
  844.   GPIOC->OTYPER  = 0x00000000;
  845.   /* No pull-up, pull-down for PCx pins */
  846.   GPIOC->PUPDR   = 0x00500000;
  847.  
  848.   /* Connect PDx pins to FMC Alternate function */
  849.   GPIOD->AFR[0]  = 0x000000CC;
  850.   GPIOD->AFR[1]  = 0xCC000CCC;
  851.   /* Configure PDx pins in Alternate function mode */  
  852.   GPIOD->MODER   = 0xA02A000A;
  853.   /* Configure PDx pins speed to 50 MHz */  
  854.   GPIOD->OSPEEDR = 0xA02A000A;
  855.   /* Configure PDx pins Output type to push-pull */  
  856.   GPIOD->OTYPER  = 0x00000000;
  857.   /* No pull-up, pull-down for PDx pins */
  858.   GPIOD->PUPDR   = 0x00000000;
  859.  
  860.   /* Connect PEx pins to FMC Alternate function */
  861.   GPIOE->AFR[0]  = 0xC00000CC;
  862.   GPIOE->AFR[1]  = 0xCCCCCCCC;
  863.   /* Configure PEx pins in Alternate function mode */
  864.   GPIOE->MODER   = 0xAAAA800A;
  865.   /* Configure PEx pins speed to 50 MHz */
  866.   GPIOE->OSPEEDR = 0xAAAA800A;
  867.   /* Configure PEx pins Output type to push-pull */  
  868.   GPIOE->OTYPER  = 0x00000000;
  869.   /* No pull-up, pull-down for PEx pins */
  870.   GPIOE->PUPDR   = 0x00000000;
  871.  
  872.   /* Connect PFx pins to FMC Alternate function */
  873.   GPIOF->AFR[0]  = 0xcccccccc;
  874.   GPIOF->AFR[1]  = 0xcccccccc;
  875.   /* Configure PFx pins in Alternate function mode */  
  876.   GPIOF->MODER   = 0xAA800AAA;
  877.   /* Configure PFx pins speed to 50 MHz */
  878.   GPIOF->OSPEEDR = 0xAA800AAA;
  879.   /* Configure PFx pins Output type to push-pull */  
  880.   GPIOF->OTYPER  = 0x00000000;
  881.   /* No pull-up, pull-down for PFx pins */
  882.   GPIOF->PUPDR   = 0x00000000;
  883.  
  884.   /* Connect PGx pins to FMC Alternate function */
  885.   GPIOG->AFR[0]  = 0xcccccccc;
  886.   GPIOG->AFR[1]  = 0xcccccccc;
  887.   /* Configure PGx pins in Alternate function mode */
  888.   GPIOG->MODER   = 0xaaaaaaaa;
  889.   /* Configure PGx pins speed to 50 MHz */
  890.   GPIOG->OSPEEDR = 0xaaaaaaaa;
  891.   /* Configure PGx pins Output type to push-pull */  
  892.   GPIOG->OTYPER  = 0x00000000;
  893.   /* No pull-up, pull-down for PGx pins */
  894.   GPIOG->PUPDR   = 0x00000000;
  895.  
  896.   /* Connect PHx pins to FMC Alternate function */
  897.   GPIOH->AFR[0]  = 0x00C0CC00;
  898.   GPIOH->AFR[1]  = 0xCCCCCCCC;
  899.   /* Configure PHx pins in Alternate function mode */
  900.   GPIOH->MODER   = 0xAAAA08A0;
  901.   /* Configure PHx pins speed to 50 MHz */
  902.   GPIOH->OSPEEDR = 0xAAAA08A0;
  903.   /* Configure PHx pins Output type to push-pull */  
  904.   GPIOH->OTYPER  = 0x00000000;
  905.   /* No pull-up, pull-down for PHx pins */
  906.   GPIOH->PUPDR   = 0x00000000;
  907.  
  908.   /* Connect PIx pins to FMC Alternate function */
  909.   GPIOI->AFR[0]  = 0xCCCCCCCC;
  910.   GPIOI->AFR[1]  = 0x00000CC0;
  911.   /* Configure PIx pins in Alternate function mode */
  912.   GPIOI->MODER   = 0x0028AAAA;
  913.   /* Configure PIx pins speed to 50 MHz */
  914.   GPIOI->OSPEEDR = 0x0028AAAA;
  915.   /* Configure PIx pins Output type to push-pull */  
  916.   GPIOI->OTYPER  = 0x00000000;
  917.   /* No pull-up, pull-down for PIx pins */
  918.   GPIOI->PUPDR   = 0x00000000;
  919.  
  920. /*-- FMC Configuration ------------------------------------------------------*/
  921.   /* Enable the FMC interface clock */
  922.   RCC->AHB3ENR |= 0x00000001;
  923.  
  924.   /* Configure and enable SDRAM bank1 */
  925.   FMC_Bank5_6->SDCR[0] = 0x000039D0;
  926.   FMC_Bank5_6->SDTR[0] = 0x01115351;      
  927.  
  928.   /* SDRAM initialization sequence */
  929.   /* Clock enable command */
  930.   FMC_Bank5_6->SDCMR = 0x00000011;
  931.   tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  932.   while((tmpreg != 0) & (timeout-- > 0))
  933.   {
  934.     tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  935.   }
  936.  
  937.   /* Delay */
  938.   for (index = 0; index<1000; index++);
  939.  
  940.   /* PALL command */
  941.   FMC_Bank5_6->SDCMR = 0x00000012;          
  942.   timeout = 0xFFFF;
  943.   while((tmpreg != 0) & (timeout-- > 0))
  944.   {
  945.   tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  946.   }
  947.  
  948.   /* Auto refresh command */
  949.   FMC_Bank5_6->SDCMR = 0x00000073;
  950.   timeout = 0xFFFF;
  951.   while((tmpreg != 0) & (timeout-- > 0))
  952.   {
  953.   tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  954.   }
  955.  
  956.   /* MRD register program */
  957.   FMC_Bank5_6->SDCMR = 0x00046014;
  958.   timeout = 0xFFFF;
  959.   while((tmpreg != 0) & (timeout-- > 0))
  960.   {
  961.   tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  962.   }
  963.  
  964.   /* Set refresh count */
  965.   tmpreg = FMC_Bank5_6->SDRTR;
  966.   FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
  967.  
  968.   /* Disable write protection */
  969.   tmpreg = FMC_Bank5_6->SDCR[0];
  970.   FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
  971.  
  972. /*
  973.   Bank1_SDRAM is configured as follow:
  974.  
  975.   FMC_SDRAMTimingInitStructure.FMC_LoadToActiveDelay = 2;      
  976.   FMC_SDRAMTimingInitStructure.FMC_ExitSelfRefreshDelay = 6;  
  977.   FMC_SDRAMTimingInitStructure.FMC_SelfRefreshTime = 4;        
  978.   FMC_SDRAMTimingInitStructure.FMC_RowCycleDelay = 6;        
  979.   FMC_SDRAMTimingInitStructure.FMC_WriteRecoveryTime = 2;      
  980.   FMC_SDRAMTimingInitStructure.FMC_RPDelay = 2;                
  981.   FMC_SDRAMTimingInitStructure.FMC_RCDDelay = 2;              
  982.  
  983.   FMC_SDRAMInitStructure.FMC_Bank = SDRAM_BANK;
  984.   FMC_SDRAMInitStructure.FMC_ColumnBitsNumber = FMC_ColumnBits_Number_8b;
  985.   FMC_SDRAMInitStructure.FMC_RowBitsNumber = FMC_RowBits_Number_11b;
  986.   FMC_SDRAMInitStructure.FMC_SDMemoryDataWidth = FMC_SDMemory_Width_16b;
  987.   FMC_SDRAMInitStructure.FMC_InternalBankNumber = FMC_InternalBank_Number_4;
  988.   FMC_SDRAMInitStructure.FMC_CASLatency = FMC_CAS_Latency_3;
  989.   FMC_SDRAMInitStructure.FMC_WriteProtection = FMC_Write_Protection_Disable;
  990.   FMC_SDRAMInitStructure.FMC_SDClockPeriod = FMC_SDClock_Period_2;
  991.   FMC_SDRAMInitStructure.FMC_ReadBurst = FMC_Read_Burst_disable;
  992.   FMC_SDRAMInitStructure.FMC_ReadPipeDelay = FMC_ReadPipe_Delay_1;
  993.   FMC_SDRAMInitStructure.FMC_SDRAMTimingStruct = &FMC_SDRAMTimingInitStructure;
  994. */
  995.  
  996. }
  997. #endif /* DATA_IN_ExtSDRAM */
  998.  
  999.  
  1000. /**
  1001.   * @}
  1002.   */
  1003.  
  1004. /**
  1005.   * @}
  1006.   */
  1007.  
  1008. /**
  1009.   * @}
  1010.   */    
  1011. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement