mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Mon Jun 01 10:30:08 2015 +0100
Revision:
553:063b9f2f393c
Child:
613:bc40b8d2aec4
Synchronized with git revision 7ab478cf5c5cb75ac77f65a4ea501ce34ce3bcdf

Full URL: https://github.com/mbedmicro/mbed/commit/7ab478cf5c5cb75ac77f65a4ea501ce34ce3bcdf/

Nucleo_F446RE - adding target

Who changed what in which revision?

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