mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/cmsis/TARGET_STM/TARGET_STM32F407VG/system_stm32f4xx.c@323:9e901b0a5aa1, 2014-09-13 (annotated)
- Committer:
- shaoziyang
- Date:
- Sat Sep 13 14:25:46 2014 +0000
- Revision:
- 323:9e901b0a5aa1
- Parent:
- 296:ec1b66a3d094
test with CLOCK_SETUP = 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 133:d4dda5c437f0 | 1 | /** |
mbed_official | 133:d4dda5c437f0 | 2 | ****************************************************************************** |
mbed_official | 133:d4dda5c437f0 | 3 | * @file system_stm32f4xx.c |
mbed_official | 133:d4dda5c437f0 | 4 | * @author MCD Application Team |
mbed_official | 242:7074e42da0b2 | 5 | * @version V2.1.0RC2 |
mbed_official | 242:7074e42da0b2 | 6 | * @date 14-May-2014 |
mbed_official | 133:d4dda5c437f0 | 7 | * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File. |
mbed_official | 133:d4dda5c437f0 | 8 | * |
mbed_official | 133:d4dda5c437f0 | 9 | * This file provides two functions and one global variable to be called from |
mbed_official | 133:d4dda5c437f0 | 10 | * user application: |
mbed_official | 133:d4dda5c437f0 | 11 | * - SystemInit(): This function is called at startup just after reset and |
mbed_official | 133:d4dda5c437f0 | 12 | * before branch to main program. This call is made inside |
mbed_official | 133:d4dda5c437f0 | 13 | * the "startup_stm32f4xx.s" file. |
mbed_official | 133:d4dda5c437f0 | 14 | * |
mbed_official | 133:d4dda5c437f0 | 15 | * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used |
mbed_official | 133:d4dda5c437f0 | 16 | * by the user application to setup the SysTick |
mbed_official | 133:d4dda5c437f0 | 17 | * timer or configure other parameters. |
mbed_official | 133:d4dda5c437f0 | 18 | * |
mbed_official | 133:d4dda5c437f0 | 19 | * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must |
mbed_official | 133:d4dda5c437f0 | 20 | * be called whenever the core clock is changed |
mbed_official | 133:d4dda5c437f0 | 21 | * during program execution. |
mbed_official | 133:d4dda5c437f0 | 22 | * |
mbed_official | 242:7074e42da0b2 | 23 | * This file configures the system clock as follows: |
mbed_official | 242:7074e42da0b2 | 24 | *----------------------------------------------------------------------------- |
mbed_official | 242:7074e42da0b2 | 25 | * System clock source | 1- PLL_HSE_EXTC | 3- PLL_HSI |
mbed_official | 242:7074e42da0b2 | 26 | * | (external 8 MHz clock) | (internal 16 MHz) |
mbed_official | 242:7074e42da0b2 | 27 | * | 2- PLL_HSE_XTAL | |
mbed_official | 242:7074e42da0b2 | 28 | * | (external 8 MHz xtal) | |
mbed_official | 242:7074e42da0b2 | 29 | *----------------------------------------------------------------------------- |
mbed_official | 242:7074e42da0b2 | 30 | * SYSCLK(MHz) | 168 | 168 |
mbed_official | 242:7074e42da0b2 | 31 | *----------------------------------------------------------------------------- |
mbed_official | 242:7074e42da0b2 | 32 | * AHBCLK (MHz) | 168 | 168 |
mbed_official | 242:7074e42da0b2 | 33 | *----------------------------------------------------------------------------- |
mbed_official | 242:7074e42da0b2 | 34 | * APB1CLK (MHz) | 42 | 42 |
mbed_official | 242:7074e42da0b2 | 35 | *----------------------------------------------------------------------------- |
mbed_official | 242:7074e42da0b2 | 36 | * APB2CLK (MHz) | 84 | 84 |
mbed_official | 242:7074e42da0b2 | 37 | *----------------------------------------------------------------------------- |
mbed_official | 242:7074e42da0b2 | 38 | * USB capable (48 MHz precise clock) | YES | NO |
mbed_official | 242:7074e42da0b2 | 39 | *----------------------------------------------------------------------------- |
mbed_official | 133:d4dda5c437f0 | 40 | ****************************************************************************** |
mbed_official | 133:d4dda5c437f0 | 41 | * @attention |
mbed_official | 133:d4dda5c437f0 | 42 | * |
mbed_official | 133:d4dda5c437f0 | 43 | * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> |
mbed_official | 133:d4dda5c437f0 | 44 | * |
mbed_official | 133:d4dda5c437f0 | 45 | * Redistribution and use in source and binary forms, with or without modification, |
mbed_official | 133:d4dda5c437f0 | 46 | * are permitted provided that the following conditions are met: |
mbed_official | 133:d4dda5c437f0 | 47 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 133:d4dda5c437f0 | 48 | * this list of conditions and the following disclaimer. |
mbed_official | 133:d4dda5c437f0 | 49 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 133:d4dda5c437f0 | 50 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 133:d4dda5c437f0 | 51 | * and/or other materials provided with the distribution. |
mbed_official | 133:d4dda5c437f0 | 52 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 133:d4dda5c437f0 | 53 | * may be used to endorse or promote products derived from this software |
mbed_official | 133:d4dda5c437f0 | 54 | * without specific prior written permission. |
mbed_official | 133:d4dda5c437f0 | 55 | * |
mbed_official | 133:d4dda5c437f0 | 56 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 133:d4dda5c437f0 | 57 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 133:d4dda5c437f0 | 58 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 133:d4dda5c437f0 | 59 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 133:d4dda5c437f0 | 60 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 133:d4dda5c437f0 | 61 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 133:d4dda5c437f0 | 62 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 133:d4dda5c437f0 | 63 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 133:d4dda5c437f0 | 64 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 133:d4dda5c437f0 | 65 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 133:d4dda5c437f0 | 66 | * |
mbed_official | 133:d4dda5c437f0 | 67 | ****************************************************************************** |
mbed_official | 133:d4dda5c437f0 | 68 | */ |
mbed_official | 133:d4dda5c437f0 | 69 | |
mbed_official | 133:d4dda5c437f0 | 70 | /** @addtogroup CMSIS |
mbed_official | 133:d4dda5c437f0 | 71 | * @{ |
mbed_official | 133:d4dda5c437f0 | 72 | */ |
mbed_official | 133:d4dda5c437f0 | 73 | |
mbed_official | 133:d4dda5c437f0 | 74 | /** @addtogroup stm32f4xx_system |
mbed_official | 133:d4dda5c437f0 | 75 | * @{ |
mbed_official | 133:d4dda5c437f0 | 76 | */ |
mbed_official | 133:d4dda5c437f0 | 77 | |
mbed_official | 133:d4dda5c437f0 | 78 | /** @addtogroup STM32F4xx_System_Private_Includes |
mbed_official | 133:d4dda5c437f0 | 79 | * @{ |
mbed_official | 133:d4dda5c437f0 | 80 | */ |
mbed_official | 133:d4dda5c437f0 | 81 | |
mbed_official | 242:7074e42da0b2 | 82 | #include "stm32f4xx.h" |
mbed_official | 242:7074e42da0b2 | 83 | #include "hal_tick.h" |
mbed_official | 242:7074e42da0b2 | 84 | |
mbed_official | 242:7074e42da0b2 | 85 | #if !defined (HSE_VALUE) |
mbed_official | 242:7074e42da0b2 | 86 | #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */ |
mbed_official | 242:7074e42da0b2 | 87 | #endif /* HSE_VALUE */ |
mbed_official | 242:7074e42da0b2 | 88 | |
mbed_official | 242:7074e42da0b2 | 89 | #if !defined (HSI_VALUE) |
mbed_official | 242:7074e42da0b2 | 90 | #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ |
mbed_official | 242:7074e42da0b2 | 91 | #endif /* HSI_VALUE */ |
mbed_official | 133:d4dda5c437f0 | 92 | |
mbed_official | 133:d4dda5c437f0 | 93 | /** |
mbed_official | 133:d4dda5c437f0 | 94 | * @} |
mbed_official | 133:d4dda5c437f0 | 95 | */ |
mbed_official | 133:d4dda5c437f0 | 96 | |
mbed_official | 133:d4dda5c437f0 | 97 | /** @addtogroup STM32F4xx_System_Private_TypesDefinitions |
mbed_official | 133:d4dda5c437f0 | 98 | * @{ |
mbed_official | 133:d4dda5c437f0 | 99 | */ |
mbed_official | 133:d4dda5c437f0 | 100 | |
mbed_official | 133:d4dda5c437f0 | 101 | /** |
mbed_official | 133:d4dda5c437f0 | 102 | * @} |
mbed_official | 133:d4dda5c437f0 | 103 | */ |
mbed_official | 133:d4dda5c437f0 | 104 | |
mbed_official | 133:d4dda5c437f0 | 105 | /** @addtogroup STM32F4xx_System_Private_Defines |
mbed_official | 133:d4dda5c437f0 | 106 | * @{ |
mbed_official | 133:d4dda5c437f0 | 107 | */ |
mbed_official | 133:d4dda5c437f0 | 108 | |
mbed_official | 133:d4dda5c437f0 | 109 | /************************* Miscellaneous Configuration ************************/ |
mbed_official | 242:7074e42da0b2 | 110 | /*!< Uncomment the following line if you need to use external SRAM or SDRAM mounted |
mbed_official | 242:7074e42da0b2 | 111 | on STM324xG_EVAL/STM324x9I_EVAL boards as data memory */ |
mbed_official | 242:7074e42da0b2 | 112 | #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) |
mbed_official | 242:7074e42da0b2 | 113 | /* #define DATA_IN_ExtSRAM */ |
mbed_official | 242:7074e42da0b2 | 114 | #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */ |
mbed_official | 242:7074e42da0b2 | 115 | |
mbed_official | 242:7074e42da0b2 | 116 | #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) |
mbed_official | 242:7074e42da0b2 | 117 | /* #define DATA_IN_ExtSDRAM */ |
mbed_official | 242:7074e42da0b2 | 118 | #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */ |
mbed_official | 242:7074e42da0b2 | 119 | |
mbed_official | 242:7074e42da0b2 | 120 | #if defined(DATA_IN_ExtSRAM) && defined(DATA_IN_ExtSDRAM) |
mbed_official | 242:7074e42da0b2 | 121 | #error "Please select DATA_IN_ExtSRAM or DATA_IN_ExtSDRAM " |
mbed_official | 242:7074e42da0b2 | 122 | #endif /* DATA_IN_ExtSRAM && DATA_IN_ExtSDRAM */ |
mbed_official | 133:d4dda5c437f0 | 123 | |
mbed_official | 133:d4dda5c437f0 | 124 | /*!< Uncomment the following line if you need to relocate your vector Table in |
mbed_official | 133:d4dda5c437f0 | 125 | Internal SRAM. */ |
mbed_official | 133:d4dda5c437f0 | 126 | /* #define VECT_TAB_SRAM */ |
mbed_official | 133:d4dda5c437f0 | 127 | #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. |
mbed_official | 133:d4dda5c437f0 | 128 | This value must be a multiple of 0x200. */ |
mbed_official | 133:d4dda5c437f0 | 129 | /******************************************************************************/ |
mbed_official | 133:d4dda5c437f0 | 130 | |
mbed_official | 133:d4dda5c437f0 | 131 | /** |
mbed_official | 133:d4dda5c437f0 | 132 | * @} |
mbed_official | 133:d4dda5c437f0 | 133 | */ |
mbed_official | 133:d4dda5c437f0 | 134 | |
mbed_official | 133:d4dda5c437f0 | 135 | /** @addtogroup STM32F4xx_System_Private_Macros |
mbed_official | 133:d4dda5c437f0 | 136 | * @{ |
mbed_official | 133:d4dda5c437f0 | 137 | */ |
mbed_official | 133:d4dda5c437f0 | 138 | |
mbed_official | 242:7074e42da0b2 | 139 | /* Select the clock sources (other than HSI) to start with (0=OFF, 1=ON) */ |
mbed_official | 242:7074e42da0b2 | 140 | #define USE_PLL_HSE_EXTC (1) /* Use external clock */ |
mbed_official | 242:7074e42da0b2 | 141 | #define USE_PLL_HSE_XTAL (1) /* Use external xtal */ |
mbed_official | 242:7074e42da0b2 | 142 | |
mbed_official | 133:d4dda5c437f0 | 143 | /** |
mbed_official | 133:d4dda5c437f0 | 144 | * @} |
mbed_official | 133:d4dda5c437f0 | 145 | */ |
mbed_official | 133:d4dda5c437f0 | 146 | |
mbed_official | 133:d4dda5c437f0 | 147 | /** @addtogroup STM32F4xx_System_Private_Variables |
mbed_official | 133:d4dda5c437f0 | 148 | * @{ |
mbed_official | 133:d4dda5c437f0 | 149 | */ |
mbed_official | 133:d4dda5c437f0 | 150 | /* This variable is updated in three ways: |
mbed_official | 133:d4dda5c437f0 | 151 | 1) by calling CMSIS function SystemCoreClockUpdate() |
mbed_official | 133:d4dda5c437f0 | 152 | 2) by calling HAL API function HAL_RCC_GetHCLKFreq() |
mbed_official | 133:d4dda5c437f0 | 153 | 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency |
mbed_official | 133:d4dda5c437f0 | 154 | Note: If you use this function to configure the system clock; then there |
mbed_official | 133:d4dda5c437f0 | 155 | is no need to call the 2 first functions listed above, since SystemCoreClock |
mbed_official | 133:d4dda5c437f0 | 156 | variable is updated automatically. |
mbed_official | 133:d4dda5c437f0 | 157 | */ |
mbed_official | 133:d4dda5c437f0 | 158 | uint32_t SystemCoreClock = 168000000; /* [CHANGED FOR MBED] */ |
mbed_official | 242:7074e42da0b2 | 159 | const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; |
mbed_official | 133:d4dda5c437f0 | 160 | |
mbed_official | 133:d4dda5c437f0 | 161 | /** |
mbed_official | 133:d4dda5c437f0 | 162 | * @} |
mbed_official | 133:d4dda5c437f0 | 163 | */ |
mbed_official | 133:d4dda5c437f0 | 164 | |
mbed_official | 133:d4dda5c437f0 | 165 | /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes |
mbed_official | 133:d4dda5c437f0 | 166 | * @{ |
mbed_official | 133:d4dda5c437f0 | 167 | */ |
mbed_official | 133:d4dda5c437f0 | 168 | |
mbed_official | 242:7074e42da0b2 | 169 | #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM) |
mbed_official | 242:7074e42da0b2 | 170 | static void SystemInit_ExtMemCtl(void); |
mbed_official | 242:7074e42da0b2 | 171 | #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */ |
mbed_official | 242:7074e42da0b2 | 172 | |
mbed_official | 242:7074e42da0b2 | 173 | #if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0) |
mbed_official | 242:7074e42da0b2 | 174 | uint8_t SetSysClock_PLL_HSE(uint8_t bypass); |
mbed_official | 242:7074e42da0b2 | 175 | #endif |
mbed_official | 242:7074e42da0b2 | 176 | |
mbed_official | 242:7074e42da0b2 | 177 | uint8_t SetSysClock_PLL_HSI(void); |
mbed_official | 133:d4dda5c437f0 | 178 | |
mbed_official | 133:d4dda5c437f0 | 179 | /** |
mbed_official | 133:d4dda5c437f0 | 180 | * @} |
mbed_official | 133:d4dda5c437f0 | 181 | */ |
mbed_official | 133:d4dda5c437f0 | 182 | |
mbed_official | 133:d4dda5c437f0 | 183 | /** @addtogroup STM32F4xx_System_Private_Functions |
mbed_official | 133:d4dda5c437f0 | 184 | * @{ |
mbed_official | 133:d4dda5c437f0 | 185 | */ |
mbed_official | 133:d4dda5c437f0 | 186 | |
mbed_official | 133:d4dda5c437f0 | 187 | /** |
mbed_official | 133:d4dda5c437f0 | 188 | * @brief Setup the microcontroller system |
mbed_official | 133:d4dda5c437f0 | 189 | * Initialize the FPU setting, vector table location and External memory |
mbed_official | 133:d4dda5c437f0 | 190 | * configuration. |
mbed_official | 133:d4dda5c437f0 | 191 | * @param None |
mbed_official | 133:d4dda5c437f0 | 192 | * @retval None |
mbed_official | 133:d4dda5c437f0 | 193 | */ |
mbed_official | 133:d4dda5c437f0 | 194 | void SystemInit(void) |
mbed_official | 133:d4dda5c437f0 | 195 | { |
mbed_official | 133:d4dda5c437f0 | 196 | /* FPU settings ------------------------------------------------------------*/ |
mbed_official | 133:d4dda5c437f0 | 197 | #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) |
mbed_official | 133:d4dda5c437f0 | 198 | SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ |
mbed_official | 133:d4dda5c437f0 | 199 | #endif |
mbed_official | 133:d4dda5c437f0 | 200 | /* Reset the RCC clock configuration to the default reset state ------------*/ |
mbed_official | 133:d4dda5c437f0 | 201 | /* Set HSION bit */ |
mbed_official | 133:d4dda5c437f0 | 202 | RCC->CR |= (uint32_t)0x00000001; |
mbed_official | 133:d4dda5c437f0 | 203 | |
mbed_official | 133:d4dda5c437f0 | 204 | /* Reset CFGR register */ |
mbed_official | 133:d4dda5c437f0 | 205 | RCC->CFGR = 0x00000000; |
mbed_official | 133:d4dda5c437f0 | 206 | |
mbed_official | 133:d4dda5c437f0 | 207 | /* Reset HSEON, CSSON and PLLON bits */ |
mbed_official | 133:d4dda5c437f0 | 208 | RCC->CR &= (uint32_t)0xFEF6FFFF; |
mbed_official | 133:d4dda5c437f0 | 209 | |
mbed_official | 133:d4dda5c437f0 | 210 | /* Reset PLLCFGR register */ |
mbed_official | 133:d4dda5c437f0 | 211 | RCC->PLLCFGR = 0x24003010; |
mbed_official | 133:d4dda5c437f0 | 212 | |
mbed_official | 133:d4dda5c437f0 | 213 | /* Reset HSEBYP bit */ |
mbed_official | 133:d4dda5c437f0 | 214 | RCC->CR &= (uint32_t)0xFFFBFFFF; |
mbed_official | 133:d4dda5c437f0 | 215 | |
mbed_official | 133:d4dda5c437f0 | 216 | /* Disable all interrupts */ |
mbed_official | 133:d4dda5c437f0 | 217 | RCC->CIR = 0x00000000; |
mbed_official | 133:d4dda5c437f0 | 218 | |
mbed_official | 242:7074e42da0b2 | 219 | #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM) |
mbed_official | 242:7074e42da0b2 | 220 | SystemInit_ExtMemCtl(); |
mbed_official | 242:7074e42da0b2 | 221 | #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */ |
mbed_official | 242:7074e42da0b2 | 222 | |
mbed_official | 133:d4dda5c437f0 | 223 | /* Configure the Vector Table location add offset address ------------------*/ |
mbed_official | 133:d4dda5c437f0 | 224 | #ifdef VECT_TAB_SRAM |
mbed_official | 133:d4dda5c437f0 | 225 | SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ |
mbed_official | 133:d4dda5c437f0 | 226 | #else |
mbed_official | 133:d4dda5c437f0 | 227 | SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */ |
mbed_official | 133:d4dda5c437f0 | 228 | #endif |
mbed_official | 133:d4dda5c437f0 | 229 | |
mbed_official | 242:7074e42da0b2 | 230 | /* Configure the Cube driver */ |
mbed_official | 242:7074e42da0b2 | 231 | SystemCoreClock = 16000000; // At this stage the HSI is used as system clock |
mbed_official | 133:d4dda5c437f0 | 232 | HAL_Init(); |
mbed_official | 242:7074e42da0b2 | 233 | |
mbed_official | 242:7074e42da0b2 | 234 | /* Configure the System clock source, PLL Multiplier and Divider factors, |
mbed_official | 242:7074e42da0b2 | 235 | AHB/APBx prescalers and Flash settings */ |
mbed_official | 242:7074e42da0b2 | 236 | SetSysClock(); |
mbed_official | 242:7074e42da0b2 | 237 | SystemCoreClockUpdate(); |
mbed_official | 242:7074e42da0b2 | 238 | |
mbed_official | 242:7074e42da0b2 | 239 | /* Reset the timer to avoid issues after the RAM initialization */ |
mbed_official | 242:7074e42da0b2 | 240 | TIM_MST_RESET_ON; |
mbed_official | 242:7074e42da0b2 | 241 | TIM_MST_RESET_OFF; |
mbed_official | 133:d4dda5c437f0 | 242 | } |
mbed_official | 133:d4dda5c437f0 | 243 | |
mbed_official | 133:d4dda5c437f0 | 244 | /** |
mbed_official | 133:d4dda5c437f0 | 245 | * @brief Update SystemCoreClock variable according to Clock Register Values. |
mbed_official | 133:d4dda5c437f0 | 246 | * The SystemCoreClock variable contains the core clock (HCLK), it can |
mbed_official | 133:d4dda5c437f0 | 247 | * be used by the user application to setup the SysTick timer or configure |
mbed_official | 133:d4dda5c437f0 | 248 | * other parameters. |
mbed_official | 133:d4dda5c437f0 | 249 | * |
mbed_official | 133:d4dda5c437f0 | 250 | * @note Each time the core clock (HCLK) changes, this function must be called |
mbed_official | 133:d4dda5c437f0 | 251 | * to update SystemCoreClock variable value. Otherwise, any configuration |
mbed_official | 133:d4dda5c437f0 | 252 | * based on this variable will be incorrect. |
mbed_official | 133:d4dda5c437f0 | 253 | * |
mbed_official | 133:d4dda5c437f0 | 254 | * @note - The system frequency computed by this function is not the real |
mbed_official | 133:d4dda5c437f0 | 255 | * frequency in the chip. It is calculated based on the predefined |
mbed_official | 133:d4dda5c437f0 | 256 | * constant and the selected clock source: |
mbed_official | 133:d4dda5c437f0 | 257 | * |
mbed_official | 133:d4dda5c437f0 | 258 | * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*) |
mbed_official | 133:d4dda5c437f0 | 259 | * |
mbed_official | 133:d4dda5c437f0 | 260 | * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**) |
mbed_official | 133:d4dda5c437f0 | 261 | * |
mbed_official | 133:d4dda5c437f0 | 262 | * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**) |
mbed_official | 133:d4dda5c437f0 | 263 | * or HSI_VALUE(*) multiplied/divided by the PLL factors. |
mbed_official | 133:d4dda5c437f0 | 264 | * |
mbed_official | 133:d4dda5c437f0 | 265 | * (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value |
mbed_official | 133:d4dda5c437f0 | 266 | * 16 MHz) but the real value may vary depending on the variations |
mbed_official | 133:d4dda5c437f0 | 267 | * in voltage and temperature. |
mbed_official | 133:d4dda5c437f0 | 268 | * |
mbed_official | 133:d4dda5c437f0 | 269 | * (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (its value |
mbed_official | 133:d4dda5c437f0 | 270 | * depends on the application requirements), user has to ensure that HSE_VALUE |
mbed_official | 133:d4dda5c437f0 | 271 | * is same as the real frequency of the crystal used. Otherwise, this function |
mbed_official | 133:d4dda5c437f0 | 272 | * may have wrong result. |
mbed_official | 133:d4dda5c437f0 | 273 | * |
mbed_official | 133:d4dda5c437f0 | 274 | * - The result of this function could be not correct when using fractional |
mbed_official | 133:d4dda5c437f0 | 275 | * value for HSE crystal. |
mbed_official | 133:d4dda5c437f0 | 276 | * |
mbed_official | 133:d4dda5c437f0 | 277 | * @param None |
mbed_official | 133:d4dda5c437f0 | 278 | * @retval None |
mbed_official | 133:d4dda5c437f0 | 279 | */ |
mbed_official | 133:d4dda5c437f0 | 280 | void SystemCoreClockUpdate(void) |
mbed_official | 133:d4dda5c437f0 | 281 | { |
mbed_official | 133:d4dda5c437f0 | 282 | uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2; |
mbed_official | 133:d4dda5c437f0 | 283 | |
mbed_official | 133:d4dda5c437f0 | 284 | /* Get SYSCLK source -------------------------------------------------------*/ |
mbed_official | 133:d4dda5c437f0 | 285 | tmp = RCC->CFGR & RCC_CFGR_SWS; |
mbed_official | 133:d4dda5c437f0 | 286 | |
mbed_official | 133:d4dda5c437f0 | 287 | switch (tmp) |
mbed_official | 133:d4dda5c437f0 | 288 | { |
mbed_official | 133:d4dda5c437f0 | 289 | case 0x00: /* HSI used as system clock source */ |
mbed_official | 133:d4dda5c437f0 | 290 | SystemCoreClock = HSI_VALUE; |
mbed_official | 133:d4dda5c437f0 | 291 | break; |
mbed_official | 133:d4dda5c437f0 | 292 | case 0x04: /* HSE used as system clock source */ |
mbed_official | 133:d4dda5c437f0 | 293 | SystemCoreClock = HSE_VALUE; |
mbed_official | 133:d4dda5c437f0 | 294 | break; |
mbed_official | 133:d4dda5c437f0 | 295 | case 0x08: /* PLL used as system clock source */ |
mbed_official | 133:d4dda5c437f0 | 296 | |
mbed_official | 133:d4dda5c437f0 | 297 | /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N |
mbed_official | 133:d4dda5c437f0 | 298 | SYSCLK = PLL_VCO / PLL_P |
mbed_official | 133:d4dda5c437f0 | 299 | */ |
mbed_official | 133:d4dda5c437f0 | 300 | pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22; |
mbed_official | 133:d4dda5c437f0 | 301 | pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM; |
mbed_official | 133:d4dda5c437f0 | 302 | |
mbed_official | 133:d4dda5c437f0 | 303 | if (pllsource != 0) |
mbed_official | 133:d4dda5c437f0 | 304 | { |
mbed_official | 133:d4dda5c437f0 | 305 | /* HSE used as PLL clock source */ |
mbed_official | 133:d4dda5c437f0 | 306 | pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6); |
mbed_official | 133:d4dda5c437f0 | 307 | } |
mbed_official | 133:d4dda5c437f0 | 308 | else |
mbed_official | 133:d4dda5c437f0 | 309 | { |
mbed_official | 133:d4dda5c437f0 | 310 | /* HSI used as PLL clock source */ |
mbed_official | 133:d4dda5c437f0 | 311 | pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6); |
mbed_official | 133:d4dda5c437f0 | 312 | } |
mbed_official | 133:d4dda5c437f0 | 313 | |
mbed_official | 133:d4dda5c437f0 | 314 | pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2; |
mbed_official | 133:d4dda5c437f0 | 315 | SystemCoreClock = pllvco/pllp; |
mbed_official | 133:d4dda5c437f0 | 316 | break; |
mbed_official | 133:d4dda5c437f0 | 317 | default: |
mbed_official | 133:d4dda5c437f0 | 318 | SystemCoreClock = HSI_VALUE; |
mbed_official | 133:d4dda5c437f0 | 319 | break; |
mbed_official | 133:d4dda5c437f0 | 320 | } |
mbed_official | 133:d4dda5c437f0 | 321 | /* Compute HCLK frequency --------------------------------------------------*/ |
mbed_official | 133:d4dda5c437f0 | 322 | /* Get HCLK prescaler */ |
mbed_official | 133:d4dda5c437f0 | 323 | tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; |
mbed_official | 133:d4dda5c437f0 | 324 | /* HCLK frequency */ |
mbed_official | 133:d4dda5c437f0 | 325 | SystemCoreClock >>= tmp; |
mbed_official | 133:d4dda5c437f0 | 326 | } |
mbed_official | 133:d4dda5c437f0 | 327 | |
mbed_official | 242:7074e42da0b2 | 328 | #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM) |
mbed_official | 242:7074e42da0b2 | 329 | /** |
mbed_official | 242:7074e42da0b2 | 330 | * @brief Setup the external memory controller. |
mbed_official | 242:7074e42da0b2 | 331 | * Called in startup_stm32f4xx.s before jump to main. |
mbed_official | 242:7074e42da0b2 | 332 | * This function configures the external memories (SRAM/SDRAM) |
mbed_official | 242:7074e42da0b2 | 333 | * This SRAM/SDRAM will be used as program data memory (including heap and stack). |
mbed_official | 242:7074e42da0b2 | 334 | * @param None |
mbed_official | 242:7074e42da0b2 | 335 | * @retval None |
mbed_official | 242:7074e42da0b2 | 336 | */ |
mbed_official | 242:7074e42da0b2 | 337 | void SystemInit_ExtMemCtl(void) |
mbed_official | 242:7074e42da0b2 | 338 | { |
mbed_official | 242:7074e42da0b2 | 339 | #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) |
mbed_official | 242:7074e42da0b2 | 340 | #if defined (DATA_IN_ExtSDRAM) |
mbed_official | 242:7074e42da0b2 | 341 | register uint32_t tmpreg = 0, timeout = 0xFFFF; |
mbed_official | 242:7074e42da0b2 | 342 | register uint32_t index; |
mbed_official | 242:7074e42da0b2 | 343 | |
mbed_official | 242:7074e42da0b2 | 344 | /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface |
mbed_official | 242:7074e42da0b2 | 345 | clock */ |
mbed_official | 242:7074e42da0b2 | 346 | RCC->AHB1ENR |= 0x000001F8; |
mbed_official | 242:7074e42da0b2 | 347 | |
mbed_official | 242:7074e42da0b2 | 348 | /* Connect PDx pins to FMC Alternate function */ |
mbed_official | 242:7074e42da0b2 | 349 | GPIOD->AFR[0] = 0x000000CC; |
mbed_official | 242:7074e42da0b2 | 350 | GPIOD->AFR[1] = 0xCC000CCC; |
mbed_official | 242:7074e42da0b2 | 351 | /* Configure PDx pins in Alternate function mode */ |
mbed_official | 242:7074e42da0b2 | 352 | GPIOD->MODER = 0xA02A000A; |
mbed_official | 242:7074e42da0b2 | 353 | /* Configure PDx pins speed to 50 MHz */ |
mbed_official | 242:7074e42da0b2 | 354 | GPIOD->OSPEEDR = 0xA02A000A; |
mbed_official | 242:7074e42da0b2 | 355 | /* Configure PDx pins Output type to push-pull */ |
mbed_official | 242:7074e42da0b2 | 356 | GPIOD->OTYPER = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 357 | /* No pull-up, pull-down for PDx pins */ |
mbed_official | 242:7074e42da0b2 | 358 | GPIOD->PUPDR = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 359 | |
mbed_official | 242:7074e42da0b2 | 360 | /* Connect PEx pins to FMC Alternate function */ |
mbed_official | 242:7074e42da0b2 | 361 | GPIOE->AFR[0] = 0xC00000CC; |
mbed_official | 242:7074e42da0b2 | 362 | GPIOE->AFR[1] = 0xCCCCCCCC; |
mbed_official | 242:7074e42da0b2 | 363 | /* Configure PEx pins in Alternate function mode */ |
mbed_official | 242:7074e42da0b2 | 364 | GPIOE->MODER = 0xAAAA800A; |
mbed_official | 242:7074e42da0b2 | 365 | /* Configure PEx pins speed to 50 MHz */ |
mbed_official | 242:7074e42da0b2 | 366 | GPIOE->OSPEEDR = 0xAAAA800A; |
mbed_official | 242:7074e42da0b2 | 367 | /* Configure PEx pins Output type to push-pull */ |
mbed_official | 242:7074e42da0b2 | 368 | GPIOE->OTYPER = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 369 | /* No pull-up, pull-down for PEx pins */ |
mbed_official | 242:7074e42da0b2 | 370 | GPIOE->PUPDR = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 371 | |
mbed_official | 242:7074e42da0b2 | 372 | /* Connect PFx pins to FMC Alternate function */ |
mbed_official | 242:7074e42da0b2 | 373 | GPIOF->AFR[0] = 0xCCCCCCCC; |
mbed_official | 242:7074e42da0b2 | 374 | GPIOF->AFR[1] = 0xCCCCCCCC; |
mbed_official | 242:7074e42da0b2 | 375 | /* Configure PFx pins in Alternate function mode */ |
mbed_official | 242:7074e42da0b2 | 376 | GPIOF->MODER = 0xAA800AAA; |
mbed_official | 242:7074e42da0b2 | 377 | /* Configure PFx pins speed to 50 MHz */ |
mbed_official | 242:7074e42da0b2 | 378 | GPIOF->OSPEEDR = 0xAA800AAA; |
mbed_official | 242:7074e42da0b2 | 379 | /* Configure PFx pins Output type to push-pull */ |
mbed_official | 242:7074e42da0b2 | 380 | GPIOF->OTYPER = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 381 | /* No pull-up, pull-down for PFx pins */ |
mbed_official | 242:7074e42da0b2 | 382 | GPIOF->PUPDR = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 383 | |
mbed_official | 242:7074e42da0b2 | 384 | /* Connect PGx pins to FMC Alternate function */ |
mbed_official | 242:7074e42da0b2 | 385 | GPIOG->AFR[0] = 0xCCCCCCCC; |
mbed_official | 242:7074e42da0b2 | 386 | GPIOG->AFR[1] = 0xCCCCCCCC; |
mbed_official | 242:7074e42da0b2 | 387 | /* Configure PGx pins in Alternate function mode */ |
mbed_official | 242:7074e42da0b2 | 388 | GPIOG->MODER = 0xAAAAAAAA; |
mbed_official | 242:7074e42da0b2 | 389 | /* Configure PGx pins speed to 50 MHz */ |
mbed_official | 242:7074e42da0b2 | 390 | GPIOG->OSPEEDR = 0xAAAAAAAA; |
mbed_official | 242:7074e42da0b2 | 391 | /* Configure PGx pins Output type to push-pull */ |
mbed_official | 242:7074e42da0b2 | 392 | GPIOG->OTYPER = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 393 | /* No pull-up, pull-down for PGx pins */ |
mbed_official | 242:7074e42da0b2 | 394 | GPIOG->PUPDR = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 395 | |
mbed_official | 242:7074e42da0b2 | 396 | /* Connect PHx pins to FMC Alternate function */ |
mbed_official | 242:7074e42da0b2 | 397 | GPIOH->AFR[0] = 0x00C0CC00; |
mbed_official | 242:7074e42da0b2 | 398 | GPIOH->AFR[1] = 0xCCCCCCCC; |
mbed_official | 242:7074e42da0b2 | 399 | /* Configure PHx pins in Alternate function mode */ |
mbed_official | 242:7074e42da0b2 | 400 | GPIOH->MODER = 0xAAAA08A0; |
mbed_official | 242:7074e42da0b2 | 401 | /* Configure PHx pins speed to 50 MHz */ |
mbed_official | 242:7074e42da0b2 | 402 | GPIOH->OSPEEDR = 0xAAAA08A0; |
mbed_official | 242:7074e42da0b2 | 403 | /* Configure PHx pins Output type to push-pull */ |
mbed_official | 242:7074e42da0b2 | 404 | GPIOH->OTYPER = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 405 | /* No pull-up, pull-down for PHx pins */ |
mbed_official | 242:7074e42da0b2 | 406 | GPIOH->PUPDR = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 407 | |
mbed_official | 242:7074e42da0b2 | 408 | /* Connect PIx pins to FMC Alternate function */ |
mbed_official | 242:7074e42da0b2 | 409 | GPIOI->AFR[0] = 0xCCCCCCCC; |
mbed_official | 242:7074e42da0b2 | 410 | GPIOI->AFR[1] = 0x00000CC0; |
mbed_official | 242:7074e42da0b2 | 411 | /* Configure PIx pins in Alternate function mode */ |
mbed_official | 242:7074e42da0b2 | 412 | GPIOI->MODER = 0x0028AAAA; |
mbed_official | 242:7074e42da0b2 | 413 | /* Configure PIx pins speed to 50 MHz */ |
mbed_official | 242:7074e42da0b2 | 414 | GPIOI->OSPEEDR = 0x0028AAAA; |
mbed_official | 242:7074e42da0b2 | 415 | /* Configure PIx pins Output type to push-pull */ |
mbed_official | 242:7074e42da0b2 | 416 | GPIOI->OTYPER = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 417 | /* No pull-up, pull-down for PIx pins */ |
mbed_official | 242:7074e42da0b2 | 418 | GPIOI->PUPDR = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 419 | |
mbed_official | 242:7074e42da0b2 | 420 | /*-- FMC Configuration ------------------------------------------------------*/ |
mbed_official | 242:7074e42da0b2 | 421 | /* Enable the FMC interface clock */ |
mbed_official | 242:7074e42da0b2 | 422 | RCC->AHB3ENR |= 0x00000001; |
mbed_official | 242:7074e42da0b2 | 423 | |
mbed_official | 242:7074e42da0b2 | 424 | /* Configure and enable SDRAM bank1 */ |
mbed_official | 242:7074e42da0b2 | 425 | FMC_Bank5_6->SDCR[0] = 0x000019E0; |
mbed_official | 242:7074e42da0b2 | 426 | FMC_Bank5_6->SDTR[0] = 0x01115351; |
mbed_official | 242:7074e42da0b2 | 427 | |
mbed_official | 242:7074e42da0b2 | 428 | /* SDRAM initialization sequence */ |
mbed_official | 242:7074e42da0b2 | 429 | /* Clock enable command */ |
mbed_official | 242:7074e42da0b2 | 430 | FMC_Bank5_6->SDCMR = 0x00000011; |
mbed_official | 242:7074e42da0b2 | 431 | tmpreg = FMC_Bank5_6->SDSR & 0x00000020; |
mbed_official | 242:7074e42da0b2 | 432 | while((tmpreg != 0) && (timeout-- > 0)) |
mbed_official | 242:7074e42da0b2 | 433 | { |
mbed_official | 242:7074e42da0b2 | 434 | tmpreg = FMC_Bank5_6->SDSR & 0x00000020; |
mbed_official | 242:7074e42da0b2 | 435 | } |
mbed_official | 242:7074e42da0b2 | 436 | |
mbed_official | 242:7074e42da0b2 | 437 | /* Delay */ |
mbed_official | 242:7074e42da0b2 | 438 | for (index = 0; index<1000; index++); |
mbed_official | 242:7074e42da0b2 | 439 | |
mbed_official | 242:7074e42da0b2 | 440 | /* PALL command */ |
mbed_official | 242:7074e42da0b2 | 441 | FMC_Bank5_6->SDCMR = 0x00000012; |
mbed_official | 242:7074e42da0b2 | 442 | timeout = 0xFFFF; |
mbed_official | 242:7074e42da0b2 | 443 | while((tmpreg != 0) && (timeout-- > 0)) |
mbed_official | 242:7074e42da0b2 | 444 | { |
mbed_official | 242:7074e42da0b2 | 445 | tmpreg = FMC_Bank5_6->SDSR & 0x00000020; |
mbed_official | 242:7074e42da0b2 | 446 | } |
mbed_official | 242:7074e42da0b2 | 447 | |
mbed_official | 242:7074e42da0b2 | 448 | /* Auto refresh command */ |
mbed_official | 242:7074e42da0b2 | 449 | FMC_Bank5_6->SDCMR = 0x00000073; |
mbed_official | 242:7074e42da0b2 | 450 | timeout = 0xFFFF; |
mbed_official | 242:7074e42da0b2 | 451 | while((tmpreg != 0) && (timeout-- > 0)) |
mbed_official | 242:7074e42da0b2 | 452 | { |
mbed_official | 242:7074e42da0b2 | 453 | tmpreg = FMC_Bank5_6->SDSR & 0x00000020; |
mbed_official | 242:7074e42da0b2 | 454 | } |
mbed_official | 242:7074e42da0b2 | 455 | |
mbed_official | 242:7074e42da0b2 | 456 | /* MRD register program */ |
mbed_official | 242:7074e42da0b2 | 457 | FMC_Bank5_6->SDCMR = 0x00046014; |
mbed_official | 242:7074e42da0b2 | 458 | timeout = 0xFFFF; |
mbed_official | 242:7074e42da0b2 | 459 | while((tmpreg != 0) && (timeout-- > 0)) |
mbed_official | 242:7074e42da0b2 | 460 | { |
mbed_official | 242:7074e42da0b2 | 461 | tmpreg = FMC_Bank5_6->SDSR & 0x00000020; |
mbed_official | 242:7074e42da0b2 | 462 | } |
mbed_official | 242:7074e42da0b2 | 463 | |
mbed_official | 242:7074e42da0b2 | 464 | /* Set refresh count */ |
mbed_official | 242:7074e42da0b2 | 465 | tmpreg = FMC_Bank5_6->SDRTR; |
mbed_official | 242:7074e42da0b2 | 466 | FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1)); |
mbed_official | 242:7074e42da0b2 | 467 | |
mbed_official | 242:7074e42da0b2 | 468 | /* Disable write protection */ |
mbed_official | 242:7074e42da0b2 | 469 | tmpreg = FMC_Bank5_6->SDCR[0]; |
mbed_official | 242:7074e42da0b2 | 470 | FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF); |
mbed_official | 242:7074e42da0b2 | 471 | #endif /* DATA_IN_ExtSDRAM */ |
mbed_official | 242:7074e42da0b2 | 472 | #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */ |
mbed_official | 242:7074e42da0b2 | 473 | |
mbed_official | 242:7074e42da0b2 | 474 | #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) |
mbed_official | 242:7074e42da0b2 | 475 | #if defined(DATA_IN_ExtSRAM) |
mbed_official | 242:7074e42da0b2 | 476 | /*-- GPIOs Configuration -----------------------------------------------------*/ |
mbed_official | 242:7074e42da0b2 | 477 | /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */ |
mbed_official | 242:7074e42da0b2 | 478 | RCC->AHB1ENR |= 0x00000078; |
mbed_official | 242:7074e42da0b2 | 479 | |
mbed_official | 242:7074e42da0b2 | 480 | /* Connect PDx pins to FMC Alternate function */ |
mbed_official | 242:7074e42da0b2 | 481 | GPIOD->AFR[0] = 0x00CCC0CC; |
mbed_official | 242:7074e42da0b2 | 482 | GPIOD->AFR[1] = 0xCCCCCCCC; |
mbed_official | 242:7074e42da0b2 | 483 | /* Configure PDx pins in Alternate function mode */ |
mbed_official | 242:7074e42da0b2 | 484 | GPIOD->MODER = 0xAAAA0A8A; |
mbed_official | 242:7074e42da0b2 | 485 | /* Configure PDx pins speed to 100 MHz */ |
mbed_official | 242:7074e42da0b2 | 486 | GPIOD->OSPEEDR = 0xFFFF0FCF; |
mbed_official | 242:7074e42da0b2 | 487 | /* Configure PDx pins Output type to push-pull */ |
mbed_official | 242:7074e42da0b2 | 488 | GPIOD->OTYPER = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 489 | /* No pull-up, pull-down for PDx pins */ |
mbed_official | 242:7074e42da0b2 | 490 | GPIOD->PUPDR = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 491 | |
mbed_official | 242:7074e42da0b2 | 492 | /* Connect PEx pins to FMC Alternate function */ |
mbed_official | 242:7074e42da0b2 | 493 | GPIOE->AFR[0] = 0xC00CC0CC; |
mbed_official | 242:7074e42da0b2 | 494 | GPIOE->AFR[1] = 0xCCCCCCCC; |
mbed_official | 242:7074e42da0b2 | 495 | /* Configure PEx pins in Alternate function mode */ |
mbed_official | 242:7074e42da0b2 | 496 | GPIOE->MODER = 0xAAAA828A; |
mbed_official | 242:7074e42da0b2 | 497 | /* Configure PEx pins speed to 100 MHz */ |
mbed_official | 242:7074e42da0b2 | 498 | GPIOE->OSPEEDR = 0xFFFFC3CF; |
mbed_official | 242:7074e42da0b2 | 499 | /* Configure PEx pins Output type to push-pull */ |
mbed_official | 242:7074e42da0b2 | 500 | GPIOE->OTYPER = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 501 | /* No pull-up, pull-down for PEx pins */ |
mbed_official | 242:7074e42da0b2 | 502 | GPIOE->PUPDR = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 503 | |
mbed_official | 242:7074e42da0b2 | 504 | /* Connect PFx pins to FMC Alternate function */ |
mbed_official | 242:7074e42da0b2 | 505 | GPIOF->AFR[0] = 0x00CCCCCC; |
mbed_official | 242:7074e42da0b2 | 506 | GPIOF->AFR[1] = 0xCCCC0000; |
mbed_official | 242:7074e42da0b2 | 507 | /* Configure PFx pins in Alternate function mode */ |
mbed_official | 242:7074e42da0b2 | 508 | GPIOF->MODER = 0xAA000AAA; |
mbed_official | 242:7074e42da0b2 | 509 | /* Configure PFx pins speed to 100 MHz */ |
mbed_official | 242:7074e42da0b2 | 510 | GPIOF->OSPEEDR = 0xFF000FFF; |
mbed_official | 242:7074e42da0b2 | 511 | /* Configure PFx pins Output type to push-pull */ |
mbed_official | 242:7074e42da0b2 | 512 | GPIOF->OTYPER = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 513 | /* No pull-up, pull-down for PFx pins */ |
mbed_official | 242:7074e42da0b2 | 514 | GPIOF->PUPDR = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 515 | |
mbed_official | 242:7074e42da0b2 | 516 | /* Connect PGx pins to FMC Alternate function */ |
mbed_official | 242:7074e42da0b2 | 517 | GPIOG->AFR[0] = 0x00CCCCCC; |
mbed_official | 242:7074e42da0b2 | 518 | GPIOG->AFR[1] = 0x000000C0; |
mbed_official | 242:7074e42da0b2 | 519 | /* Configure PGx pins in Alternate function mode */ |
mbed_official | 242:7074e42da0b2 | 520 | GPIOG->MODER = 0x00085AAA; |
mbed_official | 242:7074e42da0b2 | 521 | /* Configure PGx pins speed to 100 MHz */ |
mbed_official | 242:7074e42da0b2 | 522 | GPIOG->OSPEEDR = 0x000CAFFF; |
mbed_official | 242:7074e42da0b2 | 523 | /* Configure PGx pins Output type to push-pull */ |
mbed_official | 242:7074e42da0b2 | 524 | GPIOG->OTYPER = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 525 | /* No pull-up, pull-down for PGx pins */ |
mbed_official | 242:7074e42da0b2 | 526 | GPIOG->PUPDR = 0x00000000; |
mbed_official | 242:7074e42da0b2 | 527 | |
mbed_official | 242:7074e42da0b2 | 528 | /*-- FMC/FSMC Configuration --------------------------------------------------*/ |
mbed_official | 242:7074e42da0b2 | 529 | /* Enable the FMC/FSMC interface clock */ |
mbed_official | 242:7074e42da0b2 | 530 | RCC->AHB3ENR |= 0x00000001; |
mbed_official | 242:7074e42da0b2 | 531 | |
mbed_official | 242:7074e42da0b2 | 532 | #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) |
mbed_official | 242:7074e42da0b2 | 533 | /* Configure and enable Bank1_SRAM2 */ |
mbed_official | 242:7074e42da0b2 | 534 | FMC_Bank1->BTCR[2] = 0x00001011; |
mbed_official | 242:7074e42da0b2 | 535 | FMC_Bank1->BTCR[3] = 0x00000201; |
mbed_official | 242:7074e42da0b2 | 536 | FMC_Bank1E->BWTR[2] = 0x0fffffff; |
mbed_official | 242:7074e42da0b2 | 537 | #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */ |
mbed_official | 242:7074e42da0b2 | 538 | |
mbed_official | 242:7074e42da0b2 | 539 | #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx) |
mbed_official | 242:7074e42da0b2 | 540 | /* Configure and enable Bank1_SRAM2 */ |
mbed_official | 242:7074e42da0b2 | 541 | FSMC_Bank1->BTCR[2] = 0x00001011; |
mbed_official | 242:7074e42da0b2 | 542 | FSMC_Bank1->BTCR[3] = 0x00000201; |
mbed_official | 242:7074e42da0b2 | 543 | FSMC_Bank1E->BWTR[2] = 0x0FFFFFFF; |
mbed_official | 242:7074e42da0b2 | 544 | #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx */ |
mbed_official | 242:7074e42da0b2 | 545 | |
mbed_official | 242:7074e42da0b2 | 546 | #endif /* DATA_IN_ExtSRAM */ |
mbed_official | 242:7074e42da0b2 | 547 | #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */ |
mbed_official | 242:7074e42da0b2 | 548 | } |
mbed_official | 242:7074e42da0b2 | 549 | #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */ |
mbed_official | 242:7074e42da0b2 | 550 | |
mbed_official | 242:7074e42da0b2 | 551 | /** |
mbed_official | 242:7074e42da0b2 | 552 | * @brief Configures the System clock source, PLL Multiplier and Divider factors, |
mbed_official | 242:7074e42da0b2 | 553 | * AHB/APBx prescalers and Flash settings |
mbed_official | 242:7074e42da0b2 | 554 | * @note This function should be called only once the RCC clock configuration |
mbed_official | 242:7074e42da0b2 | 555 | * is reset to the default reset state (done in SystemInit() function). |
mbed_official | 242:7074e42da0b2 | 556 | * @param None |
mbed_official | 242:7074e42da0b2 | 557 | * @retval None |
mbed_official | 242:7074e42da0b2 | 558 | */ |
mbed_official | 242:7074e42da0b2 | 559 | void SetSysClock(void) |
mbed_official | 242:7074e42da0b2 | 560 | { |
mbed_official | 242:7074e42da0b2 | 561 | /* 1- Try to start with HSE and external clock */ |
mbed_official | 242:7074e42da0b2 | 562 | #if USE_PLL_HSE_EXTC != 0 |
mbed_official | 242:7074e42da0b2 | 563 | if (SetSysClock_PLL_HSE(1) == 0) |
mbed_official | 242:7074e42da0b2 | 564 | #endif |
mbed_official | 242:7074e42da0b2 | 565 | { |
mbed_official | 242:7074e42da0b2 | 566 | /* 2- If fail try to start with HSE and external xtal */ |
mbed_official | 242:7074e42da0b2 | 567 | #if USE_PLL_HSE_XTAL != 0 |
mbed_official | 242:7074e42da0b2 | 568 | if (SetSysClock_PLL_HSE(0) == 0) |
mbed_official | 242:7074e42da0b2 | 569 | #endif |
mbed_official | 242:7074e42da0b2 | 570 | { |
mbed_official | 242:7074e42da0b2 | 571 | /* 3- If fail start with HSI clock */ |
mbed_official | 242:7074e42da0b2 | 572 | if (SetSysClock_PLL_HSI() == 0) |
mbed_official | 242:7074e42da0b2 | 573 | { |
mbed_official | 242:7074e42da0b2 | 574 | while(1) |
mbed_official | 242:7074e42da0b2 | 575 | { |
mbed_official | 242:7074e42da0b2 | 576 | // [TODO] Put something here to tell the user that a problem occured... |
mbed_official | 242:7074e42da0b2 | 577 | } |
mbed_official | 242:7074e42da0b2 | 578 | } |
mbed_official | 242:7074e42da0b2 | 579 | } |
mbed_official | 242:7074e42da0b2 | 580 | } |
mbed_official | 242:7074e42da0b2 | 581 | |
mbed_official | 242:7074e42da0b2 | 582 | /* Output clock on MCO2 pin(PC9) for debugging purpose */ |
mbed_official | 242:7074e42da0b2 | 583 | //HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_1); // 84 MHz |
mbed_official | 242:7074e42da0b2 | 584 | } |
mbed_official | 242:7074e42da0b2 | 585 | |
mbed_official | 242:7074e42da0b2 | 586 | #if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0) |
mbed_official | 242:7074e42da0b2 | 587 | /******************************************************************************/ |
mbed_official | 242:7074e42da0b2 | 588 | /* PLL (clocked by HSE) used as System clock source */ |
mbed_official | 242:7074e42da0b2 | 589 | /******************************************************************************/ |
mbed_official | 242:7074e42da0b2 | 590 | uint8_t SetSysClock_PLL_HSE(uint8_t bypass) |
mbed_official | 133:d4dda5c437f0 | 591 | { |
mbed_official | 133:d4dda5c437f0 | 592 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
mbed_official | 133:d4dda5c437f0 | 593 | RCC_OscInitTypeDef RCC_OscInitStruct; |
mbed_official | 133:d4dda5c437f0 | 594 | |
mbed_official | 133:d4dda5c437f0 | 595 | /* The voltage scaling allows optimizing the power consumption when the device is |
mbed_official | 133:d4dda5c437f0 | 596 | clocked below the maximum system frequency, to update the voltage scaling value |
mbed_official | 133:d4dda5c437f0 | 597 | regarding system frequency refer to product datasheet. */ |
mbed_official | 133:d4dda5c437f0 | 598 | __PWR_CLK_ENABLE(); |
mbed_official | 133:d4dda5c437f0 | 599 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); |
mbed_official | 133:d4dda5c437f0 | 600 | |
mbed_official | 242:7074e42da0b2 | 601 | /* Enable HSE oscillator and activate PLL with HSE as source */ |
mbed_official | 133:d4dda5c437f0 | 602 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
mbed_official | 242:7074e42da0b2 | 603 | if (bypass == 0) |
mbed_official | 242:7074e42da0b2 | 604 | { |
mbed_official | 242:7074e42da0b2 | 605 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */ |
mbed_official | 242:7074e42da0b2 | 606 | } |
mbed_official | 242:7074e42da0b2 | 607 | else |
mbed_official | 242:7074e42da0b2 | 608 | { |
mbed_official | 242:7074e42da0b2 | 609 | RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* External 8 MHz clock on OSC_IN */ |
mbed_official | 242:7074e42da0b2 | 610 | } |
mbed_official | 242:7074e42da0b2 | 611 | RCC_OscInitStruct.HSIState = RCC_HSI_OFF; |
mbed_official | 133:d4dda5c437f0 | 612 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
mbed_official | 133:d4dda5c437f0 | 613 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
mbed_official | 242:7074e42da0b2 | 614 | RCC_OscInitStruct.PLL.PLLM = 8; // VCO input clock = 1 MHz (8 MHz / 8) |
mbed_official | 242:7074e42da0b2 | 615 | RCC_OscInitStruct.PLL.PLLN = 336; // VCO output clock = 336 MHz (1 MHz * 336) |
mbed_official | 242:7074e42da0b2 | 616 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; // PLLCLK = 168 MHz (336 MHz / 2) |
mbed_official | 242:7074e42da0b2 | 617 | RCC_OscInitStruct.PLL.PLLQ = 7; // USB clock = 48 MHz (336 MHz / 7) --> OK for USB |
mbed_official | 133:d4dda5c437f0 | 618 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
mbed_official | 133:d4dda5c437f0 | 619 | { |
mbed_official | 242:7074e42da0b2 | 620 | return 0; // FAIL |
mbed_official | 133:d4dda5c437f0 | 621 | } |
mbed_official | 133:d4dda5c437f0 | 622 | |
mbed_official | 133:d4dda5c437f0 | 623 | /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ |
mbed_official | 133:d4dda5c437f0 | 624 | RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
mbed_official | 242:7074e42da0b2 | 625 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 168 MHz |
mbed_official | 133:d4dda5c437f0 | 626 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 168 MHz |
mbed_official | 133:d4dda5c437f0 | 627 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; // 42 MHz |
mbed_official | 133:d4dda5c437f0 | 628 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; // 84 MHz (SPI1 clock...) |
mbed_official | 133:d4dda5c437f0 | 629 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) |
mbed_official | 133:d4dda5c437f0 | 630 | { |
mbed_official | 242:7074e42da0b2 | 631 | return 0; // FAIL |
mbed_official | 133:d4dda5c437f0 | 632 | } |
mbed_official | 133:d4dda5c437f0 | 633 | |
mbed_official | 242:7074e42da0b2 | 634 | /* Output clock on MCO1 pin(PA8) for debugging purpose */ |
mbed_official | 242:7074e42da0b2 | 635 | /* |
mbed_official | 242:7074e42da0b2 | 636 | if (bypass == 0) |
mbed_official | 242:7074e42da0b2 | 637 | HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_2); // 4 MHz |
mbed_official | 242:7074e42da0b2 | 638 | else |
mbed_official | 242:7074e42da0b2 | 639 | HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1); // 8 MHz |
mbed_official | 242:7074e42da0b2 | 640 | */ |
mbed_official | 242:7074e42da0b2 | 641 | |
mbed_official | 242:7074e42da0b2 | 642 | return 1; // OK |
mbed_official | 242:7074e42da0b2 | 643 | } |
mbed_official | 242:7074e42da0b2 | 644 | #endif |
mbed_official | 242:7074e42da0b2 | 645 | |
mbed_official | 242:7074e42da0b2 | 646 | /******************************************************************************/ |
mbed_official | 242:7074e42da0b2 | 647 | /* PLL (clocked by HSI) used as System clock source */ |
mbed_official | 242:7074e42da0b2 | 648 | /******************************************************************************/ |
mbed_official | 242:7074e42da0b2 | 649 | uint8_t SetSysClock_PLL_HSI(void) |
mbed_official | 242:7074e42da0b2 | 650 | { |
mbed_official | 242:7074e42da0b2 | 651 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
mbed_official | 242:7074e42da0b2 | 652 | RCC_OscInitTypeDef RCC_OscInitStruct; |
mbed_official | 133:d4dda5c437f0 | 653 | |
mbed_official | 242:7074e42da0b2 | 654 | /* The voltage scaling allows optimizing the power consumption when the device is |
mbed_official | 242:7074e42da0b2 | 655 | clocked below the maximum system frequency, to update the voltage scaling value |
mbed_official | 242:7074e42da0b2 | 656 | regarding system frequency refer to product datasheet. */ |
mbed_official | 242:7074e42da0b2 | 657 | __PWR_CLK_ENABLE(); |
mbed_official | 242:7074e42da0b2 | 658 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); |
mbed_official | 242:7074e42da0b2 | 659 | |
mbed_official | 242:7074e42da0b2 | 660 | /* Enable HSI oscillator and activate PLL with HSI as source */ |
mbed_official | 242:7074e42da0b2 | 661 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE; |
mbed_official | 242:7074e42da0b2 | 662 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
mbed_official | 242:7074e42da0b2 | 663 | RCC_OscInitStruct.HSEState = RCC_HSE_OFF; |
mbed_official | 242:7074e42da0b2 | 664 | RCC_OscInitStruct.HSICalibrationValue = 16; |
mbed_official | 242:7074e42da0b2 | 665 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
mbed_official | 242:7074e42da0b2 | 666 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
mbed_official | 242:7074e42da0b2 | 667 | RCC_OscInitStruct.PLL.PLLM = 16; // VCO input clock = 1 MHz (16 MHz / 16) |
mbed_official | 242:7074e42da0b2 | 668 | RCC_OscInitStruct.PLL.PLLN = 336; // VCO output clock = 336 MHz (1 MHz * 336) |
mbed_official | 242:7074e42da0b2 | 669 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; // PLLCLK = 168 MHz (336 MHz / 2) |
mbed_official | 242:7074e42da0b2 | 670 | RCC_OscInitStruct.PLL.PLLQ = 7; // USB clock = 48 MHz (336 MHz / 7) --> freq is ok but not precise enough |
mbed_official | 242:7074e42da0b2 | 671 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
mbed_official | 242:7074e42da0b2 | 672 | { |
mbed_official | 242:7074e42da0b2 | 673 | return 0; // FAIL |
mbed_official | 242:7074e42da0b2 | 674 | } |
mbed_official | 242:7074e42da0b2 | 675 | |
mbed_official | 242:7074e42da0b2 | 676 | /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ |
mbed_official | 242:7074e42da0b2 | 677 | RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
mbed_official | 242:7074e42da0b2 | 678 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 168 MHz |
mbed_official | 242:7074e42da0b2 | 679 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 168 MHz |
mbed_official | 242:7074e42da0b2 | 680 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; // 42 MHz |
mbed_official | 242:7074e42da0b2 | 681 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; // 84 MHz |
mbed_official | 242:7074e42da0b2 | 682 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) |
mbed_official | 242:7074e42da0b2 | 683 | { |
mbed_official | 242:7074e42da0b2 | 684 | return 0; // FAIL |
mbed_official | 242:7074e42da0b2 | 685 | } |
mbed_official | 133:d4dda5c437f0 | 686 | |
mbed_official | 242:7074e42da0b2 | 687 | /* Output clock on MCO1 pin(PA8) for debugging purpose */ |
mbed_official | 242:7074e42da0b2 | 688 | //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1); // 16 MHz |
mbed_official | 242:7074e42da0b2 | 689 | |
mbed_official | 242:7074e42da0b2 | 690 | return 1; // OK |
mbed_official | 133:d4dda5c437f0 | 691 | } |
mbed_official | 133:d4dda5c437f0 | 692 | |
mbed_official | 133:d4dda5c437f0 | 693 | /** |
mbed_official | 133:d4dda5c437f0 | 694 | * @} |
mbed_official | 133:d4dda5c437f0 | 695 | */ |
mbed_official | 133:d4dda5c437f0 | 696 | |
mbed_official | 133:d4dda5c437f0 | 697 | /** |
mbed_official | 133:d4dda5c437f0 | 698 | * @} |
mbed_official | 133:d4dda5c437f0 | 699 | */ |
mbed_official | 133:d4dda5c437f0 | 700 | |
mbed_official | 133:d4dda5c437f0 | 701 | /** |
mbed_official | 133:d4dda5c437f0 | 702 | * @} |
mbed_official | 133:d4dda5c437f0 | 703 | */ |
mbed_official | 133:d4dda5c437f0 | 704 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |