Modified version of the mbed library for use with the Nucleo boards.

Dependents:   EEPROMWrite Full-Project

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Sep 28 14:00:11 2015 +0100
Revision:
632:7687fb9c4f91
Synchronized with git revision f7ce4ed029cc611121464252ff28d5e8beb895b0

Full URL: https://github.com/mbedmicro/mbed/commit/f7ce4ed029cc611121464252ff28d5e8beb895b0/

NUCLEO_F303K8 - add support of the STM32F303K8

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 632:7687fb9c4f91 1 /**
mbed_official 632:7687fb9c4f91 2 ******************************************************************************
mbed_official 632:7687fb9c4f91 3 * @file system_stm32f3xx.c
mbed_official 632:7687fb9c4f91 4 * @author MCD Application Team
mbed_official 632:7687fb9c4f91 5 * @version V2.1.0
mbed_official 632:7687fb9c4f91 6 * @date 12-Sept-2014
mbed_official 632:7687fb9c4f91 7 * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File.
mbed_official 632:7687fb9c4f91 8 *
mbed_official 632:7687fb9c4f91 9 * 1. This file provides two functions and one global variable to be called from
mbed_official 632:7687fb9c4f91 10 * user application:
mbed_official 632:7687fb9c4f91 11 * - SystemInit(): This function is called at startup just after reset and
mbed_official 632:7687fb9c4f91 12 * before branch to main program. This call is made inside
mbed_official 632:7687fb9c4f91 13 * the "startup_stm32f3xx.s" file.
mbed_official 632:7687fb9c4f91 14 *
mbed_official 632:7687fb9c4f91 15 * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
mbed_official 632:7687fb9c4f91 16 * by the user application to setup the SysTick
mbed_official 632:7687fb9c4f91 17 * timer or configure other parameters.
mbed_official 632:7687fb9c4f91 18 *
mbed_official 632:7687fb9c4f91 19 * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
mbed_official 632:7687fb9c4f91 20 * be called whenever the core clock is changed
mbed_official 632:7687fb9c4f91 21 * during program execution.
mbed_official 632:7687fb9c4f91 22 *
mbed_official 632:7687fb9c4f91 23 * 2. After each device reset the HSI (8 MHz) is used as system clock source.
mbed_official 632:7687fb9c4f91 24 * Then SystemInit() function is called, in "startup_stm32f3xx.s" file, to
mbed_official 632:7687fb9c4f91 25 * configure the system clock before to branch to main program.
mbed_official 632:7687fb9c4f91 26 *
mbed_official 632:7687fb9c4f91 27 * 3. This file configures the system clock as follows:
mbed_official 632:7687fb9c4f91 28 *-----------------------------------------------------------------------------
mbed_official 632:7687fb9c4f91 29 * System clock source | 1- PLL_HSE_EXTC | 3- PLL_HSI
mbed_official 632:7687fb9c4f91 30 * | (external 8 MHz clock) | (internal 8 MHz)
mbed_official 632:7687fb9c4f91 31 * | 2- PLL_HSE_XTAL |
mbed_official 632:7687fb9c4f91 32 * | (external 8 MHz xtal) |
mbed_official 632:7687fb9c4f91 33 *-----------------------------------------------------------------------------
mbed_official 632:7687fb9c4f91 34 * SYSCLK(MHz) | 72 | 64
mbed_official 632:7687fb9c4f91 35 *-----------------------------------------------------------------------------
mbed_official 632:7687fb9c4f91 36 * AHBCLK (MHz) | 72 | 64
mbed_official 632:7687fb9c4f91 37 *-----------------------------------------------------------------------------
mbed_official 632:7687fb9c4f91 38 * APB1CLK (MHz) | 36 | 32
mbed_official 632:7687fb9c4f91 39 *-----------------------------------------------------------------------------
mbed_official 632:7687fb9c4f91 40 * APB2CLK (MHz) | 72 | 64
mbed_official 632:7687fb9c4f91 41 *-----------------------------------------------------------------------------
mbed_official 632:7687fb9c4f91 42 * USB capable (48 MHz precise clock) | NO | NO
mbed_official 632:7687fb9c4f91 43 *-----------------------------------------------------------------------------
mbed_official 632:7687fb9c4f91 44 ******************************************************************************
mbed_official 632:7687fb9c4f91 45 * @attention
mbed_official 632:7687fb9c4f91 46 *
mbed_official 632:7687fb9c4f91 47 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 632:7687fb9c4f91 48 *
mbed_official 632:7687fb9c4f91 49 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 632:7687fb9c4f91 50 * are permitted provided that the following conditions are met:
mbed_official 632:7687fb9c4f91 51 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 632:7687fb9c4f91 52 * this list of conditions and the following disclaimer.
mbed_official 632:7687fb9c4f91 53 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 632:7687fb9c4f91 54 * this list of conditions and the following disclaimer in the documentation
mbed_official 632:7687fb9c4f91 55 * and/or other materials provided with the distribution.
mbed_official 632:7687fb9c4f91 56 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 632:7687fb9c4f91 57 * may be used to endorse or promote products derived from this software
mbed_official 632:7687fb9c4f91 58 * without specific prior written permission.
mbed_official 632:7687fb9c4f91 59 *
mbed_official 632:7687fb9c4f91 60 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 632:7687fb9c4f91 61 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 632:7687fb9c4f91 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 632:7687fb9c4f91 63 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 632:7687fb9c4f91 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 632:7687fb9c4f91 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 632:7687fb9c4f91 66 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 632:7687fb9c4f91 67 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 632:7687fb9c4f91 68 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 632:7687fb9c4f91 69 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 632:7687fb9c4f91 70 *
mbed_official 632:7687fb9c4f91 71 ******************************************************************************
mbed_official 632:7687fb9c4f91 72 */
mbed_official 632:7687fb9c4f91 73
mbed_official 632:7687fb9c4f91 74 /** @addtogroup CMSIS
mbed_official 632:7687fb9c4f91 75 * @{
mbed_official 632:7687fb9c4f91 76 */
mbed_official 632:7687fb9c4f91 77
mbed_official 632:7687fb9c4f91 78 /** @addtogroup stm32f3xx_system
mbed_official 632:7687fb9c4f91 79 * @{
mbed_official 632:7687fb9c4f91 80 */
mbed_official 632:7687fb9c4f91 81
mbed_official 632:7687fb9c4f91 82 /** @addtogroup STM32F3xx_System_Private_Includes
mbed_official 632:7687fb9c4f91 83 * @{
mbed_official 632:7687fb9c4f91 84 */
mbed_official 632:7687fb9c4f91 85
mbed_official 632:7687fb9c4f91 86 #include "stm32f3xx.h"
mbed_official 632:7687fb9c4f91 87 #include "hal_tick.h"
mbed_official 632:7687fb9c4f91 88
mbed_official 632:7687fb9c4f91 89 /**
mbed_official 632:7687fb9c4f91 90 * @}
mbed_official 632:7687fb9c4f91 91 */
mbed_official 632:7687fb9c4f91 92
mbed_official 632:7687fb9c4f91 93 /** @addtogroup STM32F3xx_System_Private_TypesDefinitions
mbed_official 632:7687fb9c4f91 94 * @{
mbed_official 632:7687fb9c4f91 95 */
mbed_official 632:7687fb9c4f91 96
mbed_official 632:7687fb9c4f91 97 /**
mbed_official 632:7687fb9c4f91 98 * @}
mbed_official 632:7687fb9c4f91 99 */
mbed_official 632:7687fb9c4f91 100
mbed_official 632:7687fb9c4f91 101 /** @addtogroup STM32F3xx_System_Private_Defines
mbed_official 632:7687fb9c4f91 102 * @{
mbed_official 632:7687fb9c4f91 103 */
mbed_official 632:7687fb9c4f91 104 #if !defined (HSE_VALUE)
mbed_official 632:7687fb9c4f91 105 #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz.
mbed_official 632:7687fb9c4f91 106 This value can be provided and adapted by the user application. */
mbed_official 632:7687fb9c4f91 107 #endif /* HSE_VALUE */
mbed_official 632:7687fb9c4f91 108
mbed_official 632:7687fb9c4f91 109 #if !defined (HSI_VALUE)
mbed_official 632:7687fb9c4f91 110 #define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz.
mbed_official 632:7687fb9c4f91 111 This value can be provided and adapted by the user application. */
mbed_official 632:7687fb9c4f91 112 #endif /* HSI_VALUE */
mbed_official 632:7687fb9c4f91 113
mbed_official 632:7687fb9c4f91 114 /*!< Uncomment the following line if you need to relocate your vector Table in
mbed_official 632:7687fb9c4f91 115 Internal SRAM. */
mbed_official 632:7687fb9c4f91 116 /* #define VECT_TAB_SRAM */
mbed_official 632:7687fb9c4f91 117 #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
mbed_official 632:7687fb9c4f91 118 This value must be a multiple of 0x200. */
mbed_official 632:7687fb9c4f91 119 /**
mbed_official 632:7687fb9c4f91 120 * @}
mbed_official 632:7687fb9c4f91 121 */
mbed_official 632:7687fb9c4f91 122
mbed_official 632:7687fb9c4f91 123 /** @addtogroup STM32F3xx_System_Private_Macros
mbed_official 632:7687fb9c4f91 124 * @{
mbed_official 632:7687fb9c4f91 125 */
mbed_official 632:7687fb9c4f91 126
mbed_official 632:7687fb9c4f91 127 /* Select the clock sources (other than HSI) to start with (0=OFF, 1=ON) */
mbed_official 632:7687fb9c4f91 128 #define USE_PLL_HSE_EXTC (1) /* Use external clock */
mbed_official 632:7687fb9c4f91 129 #define USE_PLL_HSE_XTAL (1) /* Use external xtal */
mbed_official 632:7687fb9c4f91 130
mbed_official 632:7687fb9c4f91 131 /**
mbed_official 632:7687fb9c4f91 132 * @}
mbed_official 632:7687fb9c4f91 133 */
mbed_official 632:7687fb9c4f91 134
mbed_official 632:7687fb9c4f91 135 /** @addtogroup STM32F3xx_System_Private_Variables
mbed_official 632:7687fb9c4f91 136 * @{
mbed_official 632:7687fb9c4f91 137 */
mbed_official 632:7687fb9c4f91 138 /* This variable is updated in three ways:
mbed_official 632:7687fb9c4f91 139 1) by calling CMSIS function SystemCoreClockUpdate()
mbed_official 632:7687fb9c4f91 140 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
mbed_official 632:7687fb9c4f91 141 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
mbed_official 632:7687fb9c4f91 142 Note: If you use this function to configure the system clock there is no need to
mbed_official 632:7687fb9c4f91 143 call the 2 first functions listed above, since SystemCoreClock variable is
mbed_official 632:7687fb9c4f91 144 updated automatically.
mbed_official 632:7687fb9c4f91 145 */
mbed_official 632:7687fb9c4f91 146 uint32_t SystemCoreClock = 72000000;
mbed_official 632:7687fb9c4f91 147 const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
mbed_official 632:7687fb9c4f91 148
mbed_official 632:7687fb9c4f91 149 /**
mbed_official 632:7687fb9c4f91 150 * @}
mbed_official 632:7687fb9c4f91 151 */
mbed_official 632:7687fb9c4f91 152
mbed_official 632:7687fb9c4f91 153 /** @addtogroup STM32F3xx_System_Private_FunctionPrototypes
mbed_official 632:7687fb9c4f91 154 * @{
mbed_official 632:7687fb9c4f91 155 */
mbed_official 632:7687fb9c4f91 156
mbed_official 632:7687fb9c4f91 157 #if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0)
mbed_official 632:7687fb9c4f91 158 uint8_t SetSysClock_PLL_HSE(uint8_t bypass);
mbed_official 632:7687fb9c4f91 159 #endif
mbed_official 632:7687fb9c4f91 160
mbed_official 632:7687fb9c4f91 161 uint8_t SetSysClock_PLL_HSI(void);
mbed_official 632:7687fb9c4f91 162
mbed_official 632:7687fb9c4f91 163 /**
mbed_official 632:7687fb9c4f91 164 * @}
mbed_official 632:7687fb9c4f91 165 */
mbed_official 632:7687fb9c4f91 166
mbed_official 632:7687fb9c4f91 167 /** @addtogroup STM32F3xx_System_Private_Functions
mbed_official 632:7687fb9c4f91 168 * @{
mbed_official 632:7687fb9c4f91 169 */
mbed_official 632:7687fb9c4f91 170
mbed_official 632:7687fb9c4f91 171 /**
mbed_official 632:7687fb9c4f91 172 * @brief Setup the microcontroller system
mbed_official 632:7687fb9c4f91 173 * Initialize the FPU setting, vector table location and the PLL configuration is reset.
mbed_official 632:7687fb9c4f91 174 * @param None
mbed_official 632:7687fb9c4f91 175 * @retval None
mbed_official 632:7687fb9c4f91 176 */
mbed_official 632:7687fb9c4f91 177 void SystemInit(void)
mbed_official 632:7687fb9c4f91 178 {
mbed_official 632:7687fb9c4f91 179 /* FPU settings ------------------------------------------------------------*/
mbed_official 632:7687fb9c4f91 180 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
mbed_official 632:7687fb9c4f91 181 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
mbed_official 632:7687fb9c4f91 182 #endif
mbed_official 632:7687fb9c4f91 183
mbed_official 632:7687fb9c4f91 184 /* Reset the RCC clock configuration to the default reset state ------------*/
mbed_official 632:7687fb9c4f91 185 /* Set HSION bit */
mbed_official 632:7687fb9c4f91 186 RCC->CR |= (uint32_t)0x00000001;
mbed_official 632:7687fb9c4f91 187
mbed_official 632:7687fb9c4f91 188 /* Reset CFGR register */
mbed_official 632:7687fb9c4f91 189 RCC->CFGR &= 0xF87FC00C;
mbed_official 632:7687fb9c4f91 190
mbed_official 632:7687fb9c4f91 191 /* Reset HSEON, CSSON and PLLON bits */
mbed_official 632:7687fb9c4f91 192 RCC->CR &= (uint32_t)0xFEF6FFFF;
mbed_official 632:7687fb9c4f91 193
mbed_official 632:7687fb9c4f91 194 /* Reset HSEBYP bit */
mbed_official 632:7687fb9c4f91 195 RCC->CR &= (uint32_t)0xFFFBFFFF;
mbed_official 632:7687fb9c4f91 196
mbed_official 632:7687fb9c4f91 197 /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE bits */
mbed_official 632:7687fb9c4f91 198 RCC->CFGR &= (uint32_t)0xFF80FFFF;
mbed_official 632:7687fb9c4f91 199
mbed_official 632:7687fb9c4f91 200 /* Reset PREDIV1[3:0] bits */
mbed_official 632:7687fb9c4f91 201 RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;
mbed_official 632:7687fb9c4f91 202
mbed_official 632:7687fb9c4f91 203 /* Reset USARTSW[1:0], I2CSW and TIMs bits */
mbed_official 632:7687fb9c4f91 204 RCC->CFGR3 &= (uint32_t)0xFF00FCCC;
mbed_official 632:7687fb9c4f91 205
mbed_official 632:7687fb9c4f91 206 /* Disable all interrupts */
mbed_official 632:7687fb9c4f91 207 RCC->CIR = 0x00000000;
mbed_official 632:7687fb9c4f91 208
mbed_official 632:7687fb9c4f91 209 #ifdef VECT_TAB_SRAM
mbed_official 632:7687fb9c4f91 210 SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
mbed_official 632:7687fb9c4f91 211 #else
mbed_official 632:7687fb9c4f91 212 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
mbed_official 632:7687fb9c4f91 213 #endif
mbed_official 632:7687fb9c4f91 214
mbed_official 632:7687fb9c4f91 215 /* Configure the Cube driver */
mbed_official 632:7687fb9c4f91 216 SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
mbed_official 632:7687fb9c4f91 217 HAL_Init();
mbed_official 632:7687fb9c4f91 218
mbed_official 632:7687fb9c4f91 219 /* Configure the System clock source, PLL Multiplier and Divider factors,
mbed_official 632:7687fb9c4f91 220 AHB/APBx prescalers and Flash settings */
mbed_official 632:7687fb9c4f91 221 SetSysClock();
mbed_official 632:7687fb9c4f91 222
mbed_official 632:7687fb9c4f91 223 /* Reset the timer to avoid issues after the RAM initialization */
mbed_official 632:7687fb9c4f91 224 TIM_MST_RESET_ON;
mbed_official 632:7687fb9c4f91 225 TIM_MST_RESET_OFF;
mbed_official 632:7687fb9c4f91 226 }
mbed_official 632:7687fb9c4f91 227
mbed_official 632:7687fb9c4f91 228 /**
mbed_official 632:7687fb9c4f91 229 * @brief Update SystemCoreClock variable according to Clock Register Values.
mbed_official 632:7687fb9c4f91 230 * The SystemCoreClock variable contains the core clock (HCLK), it can
mbed_official 632:7687fb9c4f91 231 * be used by the user application to setup the SysTick timer or configure
mbed_official 632:7687fb9c4f91 232 * other parameters.
mbed_official 632:7687fb9c4f91 233 *
mbed_official 632:7687fb9c4f91 234 * @note Each time the core clock (HCLK) changes, this function must be called
mbed_official 632:7687fb9c4f91 235 * to update SystemCoreClock variable value. Otherwise, any configuration
mbed_official 632:7687fb9c4f91 236 * based on this variable will be incorrect.
mbed_official 632:7687fb9c4f91 237 *
mbed_official 632:7687fb9c4f91 238 * @note - The system frequency computed by this function is not the real
mbed_official 632:7687fb9c4f91 239 * frequency in the chip. It is calculated based on the predefined
mbed_official 632:7687fb9c4f91 240 * constant and the selected clock source:
mbed_official 632:7687fb9c4f91 241 *
mbed_official 632:7687fb9c4f91 242 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
mbed_official 632:7687fb9c4f91 243 *
mbed_official 632:7687fb9c4f91 244 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
mbed_official 632:7687fb9c4f91 245 *
mbed_official 632:7687fb9c4f91 246 * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
mbed_official 632:7687fb9c4f91 247 * or HSI_VALUE(*) multiplied/divided by the PLL factors.
mbed_official 632:7687fb9c4f91 248 *
mbed_official 632:7687fb9c4f91 249 * (*) HSI_VALUE is a constant defined in stm32f3xx_hal.h file (default value
mbed_official 632:7687fb9c4f91 250 * 8 MHz) but the real value may vary depending on the variations
mbed_official 632:7687fb9c4f91 251 * in voltage and temperature.
mbed_official 632:7687fb9c4f91 252 *
mbed_official 632:7687fb9c4f91 253 * (**) HSE_VALUE is a constant defined in stm32f3xx_hal.h file (default value
mbed_official 632:7687fb9c4f91 254 * 8 MHz), user has to ensure that HSE_VALUE is same as the real
mbed_official 632:7687fb9c4f91 255 * frequency of the crystal used. Otherwise, this function may
mbed_official 632:7687fb9c4f91 256 * have wrong result.
mbed_official 632:7687fb9c4f91 257 *
mbed_official 632:7687fb9c4f91 258 * - The result of this function could be not correct when using fractional
mbed_official 632:7687fb9c4f91 259 * value for HSE crystal.
mbed_official 632:7687fb9c4f91 260 *
mbed_official 632:7687fb9c4f91 261 * @param None
mbed_official 632:7687fb9c4f91 262 * @retval None
mbed_official 632:7687fb9c4f91 263 */
mbed_official 632:7687fb9c4f91 264 void SystemCoreClockUpdate (void)
mbed_official 632:7687fb9c4f91 265 {
mbed_official 632:7687fb9c4f91 266 uint32_t tmp = 0, pllmull = 0, pllsource = 0, predivfactor = 0;
mbed_official 632:7687fb9c4f91 267
mbed_official 632:7687fb9c4f91 268 /* Get SYSCLK source -------------------------------------------------------*/
mbed_official 632:7687fb9c4f91 269 tmp = RCC->CFGR & RCC_CFGR_SWS;
mbed_official 632:7687fb9c4f91 270
mbed_official 632:7687fb9c4f91 271 switch (tmp)
mbed_official 632:7687fb9c4f91 272 {
mbed_official 632:7687fb9c4f91 273 case RCC_CFGR_SWS_HSI: /* HSI used as system clock */
mbed_official 632:7687fb9c4f91 274 SystemCoreClock = HSI_VALUE;
mbed_official 632:7687fb9c4f91 275 break;
mbed_official 632:7687fb9c4f91 276 case RCC_CFGR_SWS_HSE: /* HSE used as system clock */
mbed_official 632:7687fb9c4f91 277 SystemCoreClock = HSE_VALUE;
mbed_official 632:7687fb9c4f91 278 break;
mbed_official 632:7687fb9c4f91 279 case RCC_CFGR_SWS_PLL: /* PLL used as system clock */
mbed_official 632:7687fb9c4f91 280 /* Get PLL clock source and multiplication factor ----------------------*/
mbed_official 632:7687fb9c4f91 281 pllmull = RCC->CFGR & RCC_CFGR_PLLMUL;
mbed_official 632:7687fb9c4f91 282 pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
mbed_official 632:7687fb9c4f91 283 pllmull = ( pllmull >> 18) + 2;
mbed_official 632:7687fb9c4f91 284
mbed_official 632:7687fb9c4f91 285 #if defined (STM32F302xE) || defined (STM32F303xE) || defined (STM32F398xx)
mbed_official 632:7687fb9c4f91 286 predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
mbed_official 632:7687fb9c4f91 287 if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV)
mbed_official 632:7687fb9c4f91 288 {
mbed_official 632:7687fb9c4f91 289 /* HSE oscillator clock selected as PREDIV1 clock entry */
mbed_official 632:7687fb9c4f91 290 SystemCoreClock = (HSE_VALUE / predivfactor) * pllmull;
mbed_official 632:7687fb9c4f91 291 }
mbed_official 632:7687fb9c4f91 292 else
mbed_official 632:7687fb9c4f91 293 {
mbed_official 632:7687fb9c4f91 294 /* HSI oscillator clock selected as PREDIV1 clock entry */
mbed_official 632:7687fb9c4f91 295 SystemCoreClock = (HSI_VALUE / predivfactor) * pllmull;
mbed_official 632:7687fb9c4f91 296 }
mbed_official 632:7687fb9c4f91 297 #else
mbed_official 632:7687fb9c4f91 298 if (pllsource == RCC_CFGR_PLLSRC_HSI_DIV2)
mbed_official 632:7687fb9c4f91 299 {
mbed_official 632:7687fb9c4f91 300 /* HSI oscillator clock divided by 2 selected as PLL clock entry */
mbed_official 632:7687fb9c4f91 301 SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
mbed_official 632:7687fb9c4f91 302 }
mbed_official 632:7687fb9c4f91 303 else
mbed_official 632:7687fb9c4f91 304 {
mbed_official 632:7687fb9c4f91 305 predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
mbed_official 632:7687fb9c4f91 306 /* HSE oscillator clock selected as PREDIV1 clock entry */
mbed_official 632:7687fb9c4f91 307 SystemCoreClock = (HSE_VALUE / predivfactor) * pllmull;
mbed_official 632:7687fb9c4f91 308 }
mbed_official 632:7687fb9c4f91 309 #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
mbed_official 632:7687fb9c4f91 310 break;
mbed_official 632:7687fb9c4f91 311 default: /* HSI used as system clock */
mbed_official 632:7687fb9c4f91 312 SystemCoreClock = HSI_VALUE;
mbed_official 632:7687fb9c4f91 313 break;
mbed_official 632:7687fb9c4f91 314 }
mbed_official 632:7687fb9c4f91 315 /* Compute HCLK clock frequency ----------------*/
mbed_official 632:7687fb9c4f91 316 /* Get HCLK prescaler */
mbed_official 632:7687fb9c4f91 317 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
mbed_official 632:7687fb9c4f91 318 /* HCLK clock frequency */
mbed_official 632:7687fb9c4f91 319 SystemCoreClock >>= tmp;
mbed_official 632:7687fb9c4f91 320 }
mbed_official 632:7687fb9c4f91 321
mbed_official 632:7687fb9c4f91 322 /**
mbed_official 632:7687fb9c4f91 323 * @brief Configures the System clock source, PLL Multiplier and Divider factors,
mbed_official 632:7687fb9c4f91 324 * AHB/APBx prescalers and Flash settings
mbed_official 632:7687fb9c4f91 325 * @note This function should be called only once the RCC clock configuration
mbed_official 632:7687fb9c4f91 326 * is reset to the default reset state (done in SystemInit() function).
mbed_official 632:7687fb9c4f91 327 * @param None
mbed_official 632:7687fb9c4f91 328 * @retval None
mbed_official 632:7687fb9c4f91 329 */
mbed_official 632:7687fb9c4f91 330 void SetSysClock(void)
mbed_official 632:7687fb9c4f91 331 {
mbed_official 632:7687fb9c4f91 332 /* 1- Try to start with HSE and external clock */
mbed_official 632:7687fb9c4f91 333 #if USE_PLL_HSE_EXTC != 0
mbed_official 632:7687fb9c4f91 334 if (SetSysClock_PLL_HSE(1) == 0)
mbed_official 632:7687fb9c4f91 335 #endif
mbed_official 632:7687fb9c4f91 336 {
mbed_official 632:7687fb9c4f91 337 /* 2- If fail try to start with HSE and external xtal */
mbed_official 632:7687fb9c4f91 338 #if USE_PLL_HSE_XTAL != 0
mbed_official 632:7687fb9c4f91 339 if (SetSysClock_PLL_HSE(0) == 0)
mbed_official 632:7687fb9c4f91 340 #endif
mbed_official 632:7687fb9c4f91 341 {
mbed_official 632:7687fb9c4f91 342 /* 3- If fail start with HSI clock */
mbed_official 632:7687fb9c4f91 343 if (SetSysClock_PLL_HSI() == 0)
mbed_official 632:7687fb9c4f91 344 {
mbed_official 632:7687fb9c4f91 345 while(1)
mbed_official 632:7687fb9c4f91 346 {
mbed_official 632:7687fb9c4f91 347 // [TODO] Put something here to tell the user that a problem occured...
mbed_official 632:7687fb9c4f91 348 }
mbed_official 632:7687fb9c4f91 349 }
mbed_official 632:7687fb9c4f91 350 }
mbed_official 632:7687fb9c4f91 351 }
mbed_official 632:7687fb9c4f91 352
mbed_official 632:7687fb9c4f91 353 /* Output clock on MCO1 pin(PA8) for debugging purpose */
mbed_official 632:7687fb9c4f91 354 //HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_SYSCLK, RCC_MCO_DIV1); // 72 MHz or 64 MHz
mbed_official 632:7687fb9c4f91 355 }
mbed_official 632:7687fb9c4f91 356
mbed_official 632:7687fb9c4f91 357 #if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0)
mbed_official 632:7687fb9c4f91 358 /******************************************************************************/
mbed_official 632:7687fb9c4f91 359 /* PLL (clocked by HSE) used as System clock source */
mbed_official 632:7687fb9c4f91 360 /******************************************************************************/
mbed_official 632:7687fb9c4f91 361 uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
mbed_official 632:7687fb9c4f91 362 {
mbed_official 632:7687fb9c4f91 363 RCC_ClkInitTypeDef RCC_ClkInitStruct;
mbed_official 632:7687fb9c4f91 364 RCC_OscInitTypeDef RCC_OscInitStruct;
mbed_official 632:7687fb9c4f91 365
mbed_official 632:7687fb9c4f91 366 /* Enable HSE oscillator and activate PLL with HSE as source */
mbed_official 632:7687fb9c4f91 367 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
mbed_official 632:7687fb9c4f91 368 if (bypass == 0)
mbed_official 632:7687fb9c4f91 369 {
mbed_official 632:7687fb9c4f91 370 RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */
mbed_official 632:7687fb9c4f91 371 }
mbed_official 632:7687fb9c4f91 372 else
mbed_official 632:7687fb9c4f91 373 {
mbed_official 632:7687fb9c4f91 374 RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* External 8 MHz clock on OSC_IN */
mbed_official 632:7687fb9c4f91 375 }
mbed_official 632:7687fb9c4f91 376 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
mbed_official 632:7687fb9c4f91 377 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
mbed_official 632:7687fb9c4f91 378 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; // 72 MHz (8 MHz * 9)
mbed_official 632:7687fb9c4f91 379 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
mbed_official 632:7687fb9c4f91 380 {
mbed_official 632:7687fb9c4f91 381 return 0; // FAIL
mbed_official 632:7687fb9c4f91 382 }
mbed_official 632:7687fb9c4f91 383
mbed_official 632:7687fb9c4f91 384 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
mbed_official 632:7687fb9c4f91 385 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
mbed_official 632:7687fb9c4f91 386 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 72 MHz
mbed_official 632:7687fb9c4f91 387 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 72 MHz
mbed_official 632:7687fb9c4f91 388 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 36 MHz
mbed_official 632:7687fb9c4f91 389 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 72 MHz
mbed_official 632:7687fb9c4f91 390 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
mbed_official 632:7687fb9c4f91 391 {
mbed_official 632:7687fb9c4f91 392 return 0; // FAIL
mbed_official 632:7687fb9c4f91 393 }
mbed_official 632:7687fb9c4f91 394
mbed_official 632:7687fb9c4f91 395 /* Output clock on MCO1 pin(PA8) for debugging purpose */
mbed_official 632:7687fb9c4f91 396 //if (bypass == 0)
mbed_official 632:7687fb9c4f91 397 // HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSE, RCC_MCO_DIV2); // 4 MHz with xtal
mbed_official 632:7687fb9c4f91 398 //else
mbed_official 632:7687fb9c4f91 399 // HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSE, RCC_MCO_DIV1); // 8 MHz with ext clock
mbed_official 632:7687fb9c4f91 400
mbed_official 632:7687fb9c4f91 401 return 1; // OK
mbed_official 632:7687fb9c4f91 402 }
mbed_official 632:7687fb9c4f91 403 #endif
mbed_official 632:7687fb9c4f91 404
mbed_official 632:7687fb9c4f91 405 /******************************************************************************/
mbed_official 632:7687fb9c4f91 406 /* PLL (clocked by HSI) used as System clock source */
mbed_official 632:7687fb9c4f91 407 /******************************************************************************/
mbed_official 632:7687fb9c4f91 408 uint8_t SetSysClock_PLL_HSI(void)
mbed_official 632:7687fb9c4f91 409 {
mbed_official 632:7687fb9c4f91 410 RCC_ClkInitTypeDef RCC_ClkInitStruct;
mbed_official 632:7687fb9c4f91 411 RCC_OscInitTypeDef RCC_OscInitStruct;
mbed_official 632:7687fb9c4f91 412
mbed_official 632:7687fb9c4f91 413 /* Enable HSI oscillator and activate PLL with HSI as source */
mbed_official 632:7687fb9c4f91 414 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
mbed_official 632:7687fb9c4f91 415 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
mbed_official 632:7687fb9c4f91 416 RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
mbed_official 632:7687fb9c4f91 417 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
mbed_official 632:7687fb9c4f91 418 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
mbed_official 632:7687fb9c4f91 419 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
mbed_official 632:7687fb9c4f91 420 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; // 64 MHz (8 MHz/2 * 16)
mbed_official 632:7687fb9c4f91 421 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
mbed_official 632:7687fb9c4f91 422 {
mbed_official 632:7687fb9c4f91 423 return 0; // FAIL
mbed_official 632:7687fb9c4f91 424 }
mbed_official 632:7687fb9c4f91 425
mbed_official 632:7687fb9c4f91 426 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
mbed_official 632:7687fb9c4f91 427 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
mbed_official 632:7687fb9c4f91 428 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 64 MHz
mbed_official 632:7687fb9c4f91 429 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 64 MHz
mbed_official 632:7687fb9c4f91 430 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 32 MHz
mbed_official 632:7687fb9c4f91 431 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 64 MHz
mbed_official 632:7687fb9c4f91 432 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
mbed_official 632:7687fb9c4f91 433 {
mbed_official 632:7687fb9c4f91 434 return 0; // FAIL
mbed_official 632:7687fb9c4f91 435 }
mbed_official 632:7687fb9c4f91 436
mbed_official 632:7687fb9c4f91 437 /* Output clock on MCO1 pin(PA8) for debugging purpose */
mbed_official 632:7687fb9c4f91 438 //HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSI, RCC_MCO_DIV1); // 8 MHz
mbed_official 632:7687fb9c4f91 439
mbed_official 632:7687fb9c4f91 440 return 1; // OK
mbed_official 632:7687fb9c4f91 441 }
mbed_official 632:7687fb9c4f91 442
mbed_official 632:7687fb9c4f91 443 /**
mbed_official 632:7687fb9c4f91 444 * @}
mbed_official 632:7687fb9c4f91 445 */
mbed_official 632:7687fb9c4f91 446
mbed_official 632:7687fb9c4f91 447 /**
mbed_official 632:7687fb9c4f91 448 * @}
mbed_official 632:7687fb9c4f91 449 */
mbed_official 632:7687fb9c4f91 450
mbed_official 632:7687fb9c4f91 451 /**
mbed_official 632:7687fb9c4f91 452 * @}
mbed_official 632:7687fb9c4f91 453 */
mbed_official 632:7687fb9c4f91 454
mbed_official 632:7687fb9c4f91 455 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
mbed_official 632:7687fb9c4f91 456