mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/cmsis/TARGET_STM/TARGET_NUCLEO_F103RB/system_stm32f10x.c@323:9e901b0a5aa1, 2014-09-13 (annotated)
- Committer:
- shaoziyang
- Date:
- Sat Sep 13 14:25:46 2014 +0000
- Revision:
- 323:9e901b0a5aa1
- Parent:
- 167:d5744491c362
test with CLOCK_SETUP = 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 52:a51c77007319 | 1 | /** |
mbed_official | 52:a51c77007319 | 2 | ****************************************************************************** |
mbed_official | 52:a51c77007319 | 3 | * @file system_stm32f10x.c |
mbed_official | 52:a51c77007319 | 4 | * @author MCD Application Team |
mbed_official | 84:f54042cbc282 | 5 | * @version V3.6.1 |
mbed_official | 84:f54042cbc282 | 6 | * @date 05-March-2012 |
mbed_official | 52:a51c77007319 | 7 | * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File. |
mbed_official | 52:a51c77007319 | 8 | * |
mbed_official | 52:a51c77007319 | 9 | * 1. This file provides two functions and one global variable to be called from |
mbed_official | 52:a51c77007319 | 10 | * user application: |
mbed_official | 52:a51c77007319 | 11 | * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier |
mbed_official | 52:a51c77007319 | 12 | * factors, AHB/APBx prescalers and Flash settings). |
mbed_official | 52:a51c77007319 | 13 | * This function is called at startup just after reset and |
mbed_official | 52:a51c77007319 | 14 | * before branch to main program. This call is made inside |
mbed_official | 52:a51c77007319 | 15 | * the "startup_stm32f10x_xx.s" file. |
mbed_official | 52:a51c77007319 | 16 | * |
mbed_official | 52:a51c77007319 | 17 | * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used |
mbed_official | 52:a51c77007319 | 18 | * by the user application to setup the SysTick |
mbed_official | 52:a51c77007319 | 19 | * timer or configure other parameters. |
mbed_official | 52:a51c77007319 | 20 | * |
mbed_official | 52:a51c77007319 | 21 | * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must |
mbed_official | 52:a51c77007319 | 22 | * be called whenever the core clock is changed |
mbed_official | 52:a51c77007319 | 23 | * during program execution. |
mbed_official | 52:a51c77007319 | 24 | * |
mbed_official | 52:a51c77007319 | 25 | * 2. After each device reset the HSI (8 MHz) is used as system clock source. |
mbed_official | 52:a51c77007319 | 26 | * Then SystemInit() function is called, in "startup_stm32f10x_xx.s" file, to |
mbed_official | 52:a51c77007319 | 27 | * configure the system clock before to branch to main program. |
mbed_official | 52:a51c77007319 | 28 | * |
mbed_official | 52:a51c77007319 | 29 | * 3. If the system clock source selected by user fails to startup, the SystemInit() |
mbed_official | 52:a51c77007319 | 30 | * function will do nothing and HSI still used as system clock source. User can |
mbed_official | 52:a51c77007319 | 31 | * add some code to deal with this issue inside the SetSysClock() function. |
mbed_official | 52:a51c77007319 | 32 | * |
mbed_official | 52:a51c77007319 | 33 | * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depedning on |
mbed_official | 52:a51c77007319 | 34 | * the product used), refer to "HSE_VALUE" define in "stm32f10x.h" file. |
mbed_official | 52:a51c77007319 | 35 | * When HSE is used as system clock source, directly or through PLL, and you |
mbed_official | 52:a51c77007319 | 36 | * are using different crystal you have to adapt the HSE value to your own |
mbed_official | 52:a51c77007319 | 37 | * configuration. |
mbed_official | 139:e3413eddde57 | 38 | * 5. This file configures the system clock as follows: |
mbed_official | 139:e3413eddde57 | 39 | *----------------------------------------------------------------------------- |
mbed_official | 139:e3413eddde57 | 40 | * System clock source | 1- PLL_HSE_EXTC | 3- PLL_HSI |
mbed_official | 139:e3413eddde57 | 41 | * | (external 8 MHz clock) | (internal 8 MHz) |
mbed_official | 139:e3413eddde57 | 42 | * | 2- PLL_HSE_XTAL | |
mbed_official | 139:e3413eddde57 | 43 | * | (external 8 MHz xtal) | |
mbed_official | 139:e3413eddde57 | 44 | *----------------------------------------------------------------------------- |
mbed_official | 139:e3413eddde57 | 45 | * SYSCLK(MHz) | 72 | 64 |
mbed_official | 139:e3413eddde57 | 46 | *----------------------------------------------------------------------------- |
mbed_official | 139:e3413eddde57 | 47 | * AHBCLK (MHz) | 72 | 64 |
mbed_official | 139:e3413eddde57 | 48 | *----------------------------------------------------------------------------- |
mbed_official | 139:e3413eddde57 | 49 | * APB1CLK (MHz) | 36 | 32 |
mbed_official | 139:e3413eddde57 | 50 | *----------------------------------------------------------------------------- |
mbed_official | 139:e3413eddde57 | 51 | * APB2CLK (MHz) | 72 | 64 |
mbed_official | 139:e3413eddde57 | 52 | *----------------------------------------------------------------------------- |
mbed_official | 139:e3413eddde57 | 53 | * USB capable (48 MHz precise clock) | YES | NO |
mbed_official | 139:e3413eddde57 | 54 | *----------------------------------------------------------------------------- |
mbed_official | 70:c1fbde68b492 | 55 | ******************************************************************************* |
mbed_official | 70:c1fbde68b492 | 56 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 70:c1fbde68b492 | 57 | * All rights reserved. |
mbed_official | 70:c1fbde68b492 | 58 | * |
mbed_official | 70:c1fbde68b492 | 59 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 70:c1fbde68b492 | 60 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 70:c1fbde68b492 | 61 | * |
mbed_official | 70:c1fbde68b492 | 62 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 70:c1fbde68b492 | 63 | * this list of conditions and the following disclaimer. |
mbed_official | 70:c1fbde68b492 | 64 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 70:c1fbde68b492 | 65 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 70:c1fbde68b492 | 66 | * and/or other materials provided with the distribution. |
mbed_official | 70:c1fbde68b492 | 67 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 70:c1fbde68b492 | 68 | * may be used to endorse or promote products derived from this software |
mbed_official | 70:c1fbde68b492 | 69 | * without specific prior written permission. |
mbed_official | 70:c1fbde68b492 | 70 | * |
mbed_official | 70:c1fbde68b492 | 71 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 70:c1fbde68b492 | 72 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 70:c1fbde68b492 | 73 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 70:c1fbde68b492 | 74 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 70:c1fbde68b492 | 75 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 70:c1fbde68b492 | 76 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 70:c1fbde68b492 | 77 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 70:c1fbde68b492 | 78 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 70:c1fbde68b492 | 79 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 70:c1fbde68b492 | 80 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 70:c1fbde68b492 | 81 | ******************************************************************************* |
mbed_official | 70:c1fbde68b492 | 82 | */ |
mbed_official | 52:a51c77007319 | 83 | |
mbed_official | 52:a51c77007319 | 84 | /** @addtogroup CMSIS |
mbed_official | 52:a51c77007319 | 85 | * @{ |
mbed_official | 52:a51c77007319 | 86 | */ |
mbed_official | 52:a51c77007319 | 87 | |
mbed_official | 52:a51c77007319 | 88 | /** @addtogroup stm32f10x_system |
mbed_official | 52:a51c77007319 | 89 | * @{ |
mbed_official | 52:a51c77007319 | 90 | */ |
mbed_official | 52:a51c77007319 | 91 | |
mbed_official | 52:a51c77007319 | 92 | /** @addtogroup STM32F10x_System_Private_Includes |
mbed_official | 52:a51c77007319 | 93 | * @{ |
mbed_official | 52:a51c77007319 | 94 | */ |
mbed_official | 52:a51c77007319 | 95 | |
mbed_official | 52:a51c77007319 | 96 | #include "stm32f10x.h" |
mbed_official | 52:a51c77007319 | 97 | |
mbed_official | 52:a51c77007319 | 98 | /** |
mbed_official | 52:a51c77007319 | 99 | * @} |
mbed_official | 52:a51c77007319 | 100 | */ |
mbed_official | 52:a51c77007319 | 101 | |
mbed_official | 52:a51c77007319 | 102 | /** @addtogroup STM32F10x_System_Private_TypesDefinitions |
mbed_official | 52:a51c77007319 | 103 | * @{ |
mbed_official | 52:a51c77007319 | 104 | */ |
mbed_official | 52:a51c77007319 | 105 | |
mbed_official | 52:a51c77007319 | 106 | /** |
mbed_official | 52:a51c77007319 | 107 | * @} |
mbed_official | 52:a51c77007319 | 108 | */ |
mbed_official | 52:a51c77007319 | 109 | |
mbed_official | 52:a51c77007319 | 110 | /** @addtogroup STM32F10x_System_Private_Defines |
mbed_official | 52:a51c77007319 | 111 | * @{ |
mbed_official | 52:a51c77007319 | 112 | */ |
mbed_official | 52:a51c77007319 | 113 | |
mbed_official | 52:a51c77007319 | 114 | /*!< Uncomment the following line if you need to relocate your vector Table in |
mbed_official | 52:a51c77007319 | 115 | Internal SRAM. */ |
mbed_official | 52:a51c77007319 | 116 | /* #define VECT_TAB_SRAM */ |
mbed_official | 52:a51c77007319 | 117 | #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field. |
mbed_official | 52:a51c77007319 | 118 | This value must be a multiple of 0x200. */ |
mbed_official | 52:a51c77007319 | 119 | /** |
mbed_official | 52:a51c77007319 | 120 | * @} |
mbed_official | 52:a51c77007319 | 121 | */ |
mbed_official | 52:a51c77007319 | 122 | |
mbed_official | 52:a51c77007319 | 123 | /** @addtogroup STM32F10x_System_Private_Macros |
mbed_official | 52:a51c77007319 | 124 | * @{ |
mbed_official | 52:a51c77007319 | 125 | */ |
mbed_official | 52:a51c77007319 | 126 | |
mbed_official | 139:e3413eddde57 | 127 | /* Select the clock sources (other than HSI) to start with (0=OFF, 1=ON) */ |
mbed_official | 139:e3413eddde57 | 128 | #define USE_PLL_HSE_EXTC (1) /* Use external clock */ |
mbed_official | 139:e3413eddde57 | 129 | #define USE_PLL_HSE_XTAL (1) /* Use external xtal */ |
mbed_official | 139:e3413eddde57 | 130 | |
mbed_official | 52:a51c77007319 | 131 | /** |
mbed_official | 52:a51c77007319 | 132 | * @} |
mbed_official | 52:a51c77007319 | 133 | */ |
mbed_official | 52:a51c77007319 | 134 | |
mbed_official | 52:a51c77007319 | 135 | /** @addtogroup STM32F10x_System_Private_Variables |
mbed_official | 52:a51c77007319 | 136 | * @{ |
mbed_official | 52:a51c77007319 | 137 | */ |
mbed_official | 52:a51c77007319 | 138 | |
mbed_official | 139:e3413eddde57 | 139 | uint32_t SystemCoreClock = 64000000; /* Default with HSI. Will be updated if HSE is used */ |
mbed_official | 52:a51c77007319 | 140 | |
mbed_official | 52:a51c77007319 | 141 | __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; |
mbed_official | 52:a51c77007319 | 142 | /** |
mbed_official | 52:a51c77007319 | 143 | * @} |
mbed_official | 52:a51c77007319 | 144 | */ |
mbed_official | 52:a51c77007319 | 145 | |
mbed_official | 52:a51c77007319 | 146 | /** @addtogroup STM32F10x_System_Private_FunctionPrototypes |
mbed_official | 52:a51c77007319 | 147 | * @{ |
mbed_official | 52:a51c77007319 | 148 | */ |
mbed_official | 52:a51c77007319 | 149 | |
mbed_official | 139:e3413eddde57 | 150 | #if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0) |
mbed_official | 139:e3413eddde57 | 151 | uint8_t SetSysClock_PLL_HSE(uint8_t bypass); |
mbed_official | 52:a51c77007319 | 152 | #endif |
mbed_official | 52:a51c77007319 | 153 | |
mbed_official | 139:e3413eddde57 | 154 | uint8_t SetSysClock_PLL_HSI(void); |
mbed_official | 139:e3413eddde57 | 155 | |
mbed_official | 52:a51c77007319 | 156 | #ifdef DATA_IN_ExtSRAM |
mbed_official | 52:a51c77007319 | 157 | static void SystemInit_ExtMemCtl(void); |
mbed_official | 52:a51c77007319 | 158 | #endif /* DATA_IN_ExtSRAM */ |
mbed_official | 52:a51c77007319 | 159 | |
mbed_official | 52:a51c77007319 | 160 | /** |
mbed_official | 52:a51c77007319 | 161 | * @} |
mbed_official | 52:a51c77007319 | 162 | */ |
mbed_official | 52:a51c77007319 | 163 | |
mbed_official | 52:a51c77007319 | 164 | /** @addtogroup STM32F10x_System_Private_Functions |
mbed_official | 52:a51c77007319 | 165 | * @{ |
mbed_official | 52:a51c77007319 | 166 | */ |
mbed_official | 52:a51c77007319 | 167 | |
mbed_official | 52:a51c77007319 | 168 | /** |
mbed_official | 52:a51c77007319 | 169 | * @brief Setup the microcontroller system |
mbed_official | 52:a51c77007319 | 170 | * Initialize the Embedded Flash Interface, the PLL and update the |
mbed_official | 52:a51c77007319 | 171 | * SystemCoreClock variable. |
mbed_official | 52:a51c77007319 | 172 | * @note This function should be used only after reset. |
mbed_official | 52:a51c77007319 | 173 | * @param None |
mbed_official | 52:a51c77007319 | 174 | * @retval None |
mbed_official | 52:a51c77007319 | 175 | */ |
mbed_official | 52:a51c77007319 | 176 | void SystemInit (void) |
mbed_official | 52:a51c77007319 | 177 | { |
mbed_official | 52:a51c77007319 | 178 | /* Reset the RCC clock configuration to the default reset state(for debug purpose) */ |
mbed_official | 52:a51c77007319 | 179 | /* Set HSION bit */ |
mbed_official | 52:a51c77007319 | 180 | RCC->CR |= (uint32_t)0x00000001; |
mbed_official | 52:a51c77007319 | 181 | |
mbed_official | 52:a51c77007319 | 182 | /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */ |
mbed_official | 52:a51c77007319 | 183 | #ifndef STM32F10X_CL |
mbed_official | 52:a51c77007319 | 184 | RCC->CFGR &= (uint32_t)0xF8FF0000; |
mbed_official | 52:a51c77007319 | 185 | #else |
mbed_official | 52:a51c77007319 | 186 | RCC->CFGR &= (uint32_t)0xF0FF0000; |
mbed_official | 52:a51c77007319 | 187 | #endif /* STM32F10X_CL */ |
mbed_official | 52:a51c77007319 | 188 | |
mbed_official | 52:a51c77007319 | 189 | /* Reset HSEON, CSSON and PLLON bits */ |
mbed_official | 52:a51c77007319 | 190 | RCC->CR &= (uint32_t)0xFEF6FFFF; |
mbed_official | 52:a51c77007319 | 191 | |
mbed_official | 52:a51c77007319 | 192 | /* Reset HSEBYP bit */ |
mbed_official | 52:a51c77007319 | 193 | RCC->CR &= (uint32_t)0xFFFBFFFF; |
mbed_official | 52:a51c77007319 | 194 | |
mbed_official | 52:a51c77007319 | 195 | /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */ |
mbed_official | 52:a51c77007319 | 196 | RCC->CFGR &= (uint32_t)0xFF80FFFF; |
mbed_official | 52:a51c77007319 | 197 | |
mbed_official | 52:a51c77007319 | 198 | #ifdef STM32F10X_CL |
mbed_official | 52:a51c77007319 | 199 | /* Reset PLL2ON and PLL3ON bits */ |
mbed_official | 52:a51c77007319 | 200 | RCC->CR &= (uint32_t)0xEBFFFFFF; |
mbed_official | 52:a51c77007319 | 201 | |
mbed_official | 52:a51c77007319 | 202 | /* Disable all interrupts and clear pending bits */ |
mbed_official | 52:a51c77007319 | 203 | RCC->CIR = 0x00FF0000; |
mbed_official | 52:a51c77007319 | 204 | |
mbed_official | 52:a51c77007319 | 205 | /* Reset CFGR2 register */ |
mbed_official | 52:a51c77007319 | 206 | RCC->CFGR2 = 0x00000000; |
mbed_official | 52:a51c77007319 | 207 | #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL) |
mbed_official | 52:a51c77007319 | 208 | /* Disable all interrupts and clear pending bits */ |
mbed_official | 52:a51c77007319 | 209 | RCC->CIR = 0x009F0000; |
mbed_official | 52:a51c77007319 | 210 | |
mbed_official | 52:a51c77007319 | 211 | /* Reset CFGR2 register */ |
mbed_official | 52:a51c77007319 | 212 | RCC->CFGR2 = 0x00000000; |
mbed_official | 52:a51c77007319 | 213 | #else |
mbed_official | 52:a51c77007319 | 214 | /* Disable all interrupts and clear pending bits */ |
mbed_official | 52:a51c77007319 | 215 | RCC->CIR = 0x009F0000; |
mbed_official | 52:a51c77007319 | 216 | #endif /* STM32F10X_CL */ |
mbed_official | 52:a51c77007319 | 217 | |
mbed_official | 52:a51c77007319 | 218 | #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL) |
mbed_official | 52:a51c77007319 | 219 | #ifdef DATA_IN_ExtSRAM |
mbed_official | 52:a51c77007319 | 220 | SystemInit_ExtMemCtl(); |
mbed_official | 52:a51c77007319 | 221 | #endif /* DATA_IN_ExtSRAM */ |
mbed_official | 52:a51c77007319 | 222 | #endif |
mbed_official | 52:a51c77007319 | 223 | |
mbed_official | 52:a51c77007319 | 224 | #ifdef VECT_TAB_SRAM |
mbed_official | 52:a51c77007319 | 225 | SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */ |
mbed_official | 52:a51c77007319 | 226 | #else |
mbed_official | 52:a51c77007319 | 227 | SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */ |
mbed_official | 167:d5744491c362 | 228 | #endif |
mbed_official | 167:d5744491c362 | 229 | |
mbed_official | 167:d5744491c362 | 230 | /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */ |
mbed_official | 167:d5744491c362 | 231 | /* Configure the Flash Latency cycles and enable prefetch buffer */ |
mbed_official | 167:d5744491c362 | 232 | SetSysClock(); |
mbed_official | 52:a51c77007319 | 233 | } |
mbed_official | 52:a51c77007319 | 234 | |
mbed_official | 52:a51c77007319 | 235 | /** |
mbed_official | 52:a51c77007319 | 236 | * @brief Update SystemCoreClock variable according to Clock Register Values. |
mbed_official | 52:a51c77007319 | 237 | * The SystemCoreClock variable contains the core clock (HCLK), it can |
mbed_official | 52:a51c77007319 | 238 | * be used by the user application to setup the SysTick timer or configure |
mbed_official | 52:a51c77007319 | 239 | * other parameters. |
mbed_official | 52:a51c77007319 | 240 | * |
mbed_official | 52:a51c77007319 | 241 | * @note Each time the core clock (HCLK) changes, this function must be called |
mbed_official | 52:a51c77007319 | 242 | * to update SystemCoreClock variable value. Otherwise, any configuration |
mbed_official | 52:a51c77007319 | 243 | * based on this variable will be incorrect. |
mbed_official | 52:a51c77007319 | 244 | * |
mbed_official | 52:a51c77007319 | 245 | * @note - The system frequency computed by this function is not the real |
mbed_official | 52:a51c77007319 | 246 | * frequency in the chip. It is calculated based on the predefined |
mbed_official | 52:a51c77007319 | 247 | * constant and the selected clock source: |
mbed_official | 52:a51c77007319 | 248 | * |
mbed_official | 52:a51c77007319 | 249 | * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*) |
mbed_official | 52:a51c77007319 | 250 | * |
mbed_official | 52:a51c77007319 | 251 | * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**) |
mbed_official | 52:a51c77007319 | 252 | * |
mbed_official | 52:a51c77007319 | 253 | * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**) |
mbed_official | 52:a51c77007319 | 254 | * or HSI_VALUE(*) multiplied by the PLL factors. |
mbed_official | 52:a51c77007319 | 255 | * |
mbed_official | 52:a51c77007319 | 256 | * (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value |
mbed_official | 52:a51c77007319 | 257 | * 8 MHz) but the real value may vary depending on the variations |
mbed_official | 52:a51c77007319 | 258 | * in voltage and temperature. |
mbed_official | 52:a51c77007319 | 259 | * |
mbed_official | 52:a51c77007319 | 260 | * (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value |
mbed_official | 52:a51c77007319 | 261 | * 8 MHz or 25 MHz, depedning on the product used), user has to ensure |
mbed_official | 52:a51c77007319 | 262 | * that HSE_VALUE is same as the real frequency of the crystal used. |
mbed_official | 52:a51c77007319 | 263 | * Otherwise, this function may have wrong result. |
mbed_official | 52:a51c77007319 | 264 | * |
mbed_official | 52:a51c77007319 | 265 | * - The result of this function could be not correct when using fractional |
mbed_official | 52:a51c77007319 | 266 | * value for HSE crystal. |
mbed_official | 52:a51c77007319 | 267 | * @param None |
mbed_official | 52:a51c77007319 | 268 | * @retval None |
mbed_official | 52:a51c77007319 | 269 | */ |
mbed_official | 52:a51c77007319 | 270 | void SystemCoreClockUpdate (void) |
mbed_official | 52:a51c77007319 | 271 | { |
mbed_official | 52:a51c77007319 | 272 | uint32_t tmp = 0, pllmull = 0, pllsource = 0; |
mbed_official | 52:a51c77007319 | 273 | |
mbed_official | 52:a51c77007319 | 274 | #ifdef STM32F10X_CL |
mbed_official | 52:a51c77007319 | 275 | uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0; |
mbed_official | 52:a51c77007319 | 276 | #endif /* STM32F10X_CL */ |
mbed_official | 52:a51c77007319 | 277 | |
mbed_official | 52:a51c77007319 | 278 | #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL) |
mbed_official | 52:a51c77007319 | 279 | uint32_t prediv1factor = 0; |
mbed_official | 52:a51c77007319 | 280 | #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL or STM32F10X_HD_VL */ |
mbed_official | 52:a51c77007319 | 281 | |
mbed_official | 52:a51c77007319 | 282 | /* Get SYSCLK source -------------------------------------------------------*/ |
mbed_official | 52:a51c77007319 | 283 | tmp = RCC->CFGR & RCC_CFGR_SWS; |
mbed_official | 52:a51c77007319 | 284 | |
mbed_official | 52:a51c77007319 | 285 | switch (tmp) |
mbed_official | 52:a51c77007319 | 286 | { |
mbed_official | 52:a51c77007319 | 287 | case 0x00: /* HSI used as system clock */ |
mbed_official | 52:a51c77007319 | 288 | SystemCoreClock = HSI_VALUE; |
mbed_official | 52:a51c77007319 | 289 | break; |
mbed_official | 52:a51c77007319 | 290 | case 0x04: /* HSE used as system clock */ |
mbed_official | 52:a51c77007319 | 291 | SystemCoreClock = HSE_VALUE; |
mbed_official | 52:a51c77007319 | 292 | break; |
mbed_official | 52:a51c77007319 | 293 | case 0x08: /* PLL used as system clock */ |
mbed_official | 52:a51c77007319 | 294 | |
mbed_official | 52:a51c77007319 | 295 | /* Get PLL clock source and multiplication factor ----------------------*/ |
mbed_official | 52:a51c77007319 | 296 | pllmull = RCC->CFGR & RCC_CFGR_PLLMULL; |
mbed_official | 52:a51c77007319 | 297 | pllsource = RCC->CFGR & RCC_CFGR_PLLSRC; |
mbed_official | 52:a51c77007319 | 298 | |
mbed_official | 52:a51c77007319 | 299 | #ifndef STM32F10X_CL |
mbed_official | 52:a51c77007319 | 300 | pllmull = ( pllmull >> 18) + 2; |
mbed_official | 52:a51c77007319 | 301 | |
mbed_official | 52:a51c77007319 | 302 | if (pllsource == 0x00) |
mbed_official | 52:a51c77007319 | 303 | { |
mbed_official | 52:a51c77007319 | 304 | /* HSI oscillator clock divided by 2 selected as PLL clock entry */ |
mbed_official | 52:a51c77007319 | 305 | SystemCoreClock = (HSI_VALUE >> 1) * pllmull; |
mbed_official | 52:a51c77007319 | 306 | } |
mbed_official | 52:a51c77007319 | 307 | else |
mbed_official | 52:a51c77007319 | 308 | { |
mbed_official | 52:a51c77007319 | 309 | #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL) |
mbed_official | 52:a51c77007319 | 310 | prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1; |
mbed_official | 52:a51c77007319 | 311 | /* HSE oscillator clock selected as PREDIV1 clock entry */ |
mbed_official | 52:a51c77007319 | 312 | SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull; |
mbed_official | 52:a51c77007319 | 313 | #else |
mbed_official | 52:a51c77007319 | 314 | /* HSE selected as PLL clock entry */ |
mbed_official | 52:a51c77007319 | 315 | if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET) |
mbed_official | 52:a51c77007319 | 316 | {/* HSE oscillator clock divided by 2 */ |
mbed_official | 52:a51c77007319 | 317 | SystemCoreClock = (HSE_VALUE >> 1) * pllmull; |
mbed_official | 52:a51c77007319 | 318 | } |
mbed_official | 52:a51c77007319 | 319 | else |
mbed_official | 52:a51c77007319 | 320 | { |
mbed_official | 52:a51c77007319 | 321 | SystemCoreClock = HSE_VALUE * pllmull; |
mbed_official | 52:a51c77007319 | 322 | } |
mbed_official | 52:a51c77007319 | 323 | #endif |
mbed_official | 52:a51c77007319 | 324 | } |
mbed_official | 52:a51c77007319 | 325 | #else |
mbed_official | 52:a51c77007319 | 326 | pllmull = pllmull >> 18; |
mbed_official | 52:a51c77007319 | 327 | |
mbed_official | 52:a51c77007319 | 328 | if (pllmull != 0x0D) |
mbed_official | 52:a51c77007319 | 329 | { |
mbed_official | 52:a51c77007319 | 330 | pllmull += 2; |
mbed_official | 52:a51c77007319 | 331 | } |
mbed_official | 52:a51c77007319 | 332 | else |
mbed_official | 52:a51c77007319 | 333 | { /* PLL multiplication factor = PLL input clock * 6.5 */ |
mbed_official | 52:a51c77007319 | 334 | pllmull = 13 / 2; |
mbed_official | 52:a51c77007319 | 335 | } |
mbed_official | 52:a51c77007319 | 336 | |
mbed_official | 52:a51c77007319 | 337 | if (pllsource == 0x00) |
mbed_official | 52:a51c77007319 | 338 | { |
mbed_official | 52:a51c77007319 | 339 | /* HSI oscillator clock divided by 2 selected as PLL clock entry */ |
mbed_official | 52:a51c77007319 | 340 | SystemCoreClock = (HSI_VALUE >> 1) * pllmull; |
mbed_official | 52:a51c77007319 | 341 | } |
mbed_official | 52:a51c77007319 | 342 | else |
mbed_official | 52:a51c77007319 | 343 | {/* PREDIV1 selected as PLL clock entry */ |
mbed_official | 52:a51c77007319 | 344 | |
mbed_official | 52:a51c77007319 | 345 | /* Get PREDIV1 clock source and division factor */ |
mbed_official | 52:a51c77007319 | 346 | prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC; |
mbed_official | 52:a51c77007319 | 347 | prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1; |
mbed_official | 52:a51c77007319 | 348 | |
mbed_official | 52:a51c77007319 | 349 | if (prediv1source == 0) |
mbed_official | 52:a51c77007319 | 350 | { |
mbed_official | 52:a51c77007319 | 351 | /* HSE oscillator clock selected as PREDIV1 clock entry */ |
mbed_official | 52:a51c77007319 | 352 | SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull; |
mbed_official | 52:a51c77007319 | 353 | } |
mbed_official | 52:a51c77007319 | 354 | else |
mbed_official | 52:a51c77007319 | 355 | {/* PLL2 clock selected as PREDIV1 clock entry */ |
mbed_official | 52:a51c77007319 | 356 | |
mbed_official | 52:a51c77007319 | 357 | /* Get PREDIV2 division factor and PLL2 multiplication factor */ |
mbed_official | 52:a51c77007319 | 358 | prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1; |
mbed_official | 52:a51c77007319 | 359 | pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2; |
mbed_official | 52:a51c77007319 | 360 | SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull; |
mbed_official | 52:a51c77007319 | 361 | } |
mbed_official | 52:a51c77007319 | 362 | } |
mbed_official | 52:a51c77007319 | 363 | #endif /* STM32F10X_CL */ |
mbed_official | 52:a51c77007319 | 364 | break; |
mbed_official | 52:a51c77007319 | 365 | |
mbed_official | 52:a51c77007319 | 366 | default: |
mbed_official | 52:a51c77007319 | 367 | SystemCoreClock = HSI_VALUE; |
mbed_official | 52:a51c77007319 | 368 | break; |
mbed_official | 52:a51c77007319 | 369 | } |
mbed_official | 52:a51c77007319 | 370 | |
mbed_official | 52:a51c77007319 | 371 | /* Compute HCLK clock frequency ----------------*/ |
mbed_official | 52:a51c77007319 | 372 | /* Get HCLK prescaler */ |
mbed_official | 52:a51c77007319 | 373 | tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; |
mbed_official | 52:a51c77007319 | 374 | /* HCLK clock frequency */ |
mbed_official | 52:a51c77007319 | 375 | SystemCoreClock >>= tmp; |
mbed_official | 52:a51c77007319 | 376 | } |
mbed_official | 52:a51c77007319 | 377 | |
mbed_official | 52:a51c77007319 | 378 | /** |
mbed_official | 52:a51c77007319 | 379 | * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers. |
mbed_official | 52:a51c77007319 | 380 | * @param None |
mbed_official | 52:a51c77007319 | 381 | * @retval None |
mbed_official | 52:a51c77007319 | 382 | */ |
mbed_official | 139:e3413eddde57 | 383 | void SetSysClock(void) |
mbed_official | 52:a51c77007319 | 384 | { |
mbed_official | 139:e3413eddde57 | 385 | /* 1- Try to start with HSE and external clock */ |
mbed_official | 139:e3413eddde57 | 386 | #if USE_PLL_HSE_EXTC != 0 |
mbed_official | 139:e3413eddde57 | 387 | if (SetSysClock_PLL_HSE(1) == 0) |
mbed_official | 52:a51c77007319 | 388 | #endif |
mbed_official | 139:e3413eddde57 | 389 | { |
mbed_official | 139:e3413eddde57 | 390 | /* 2- If fail try to start with HSE and external xtal */ |
mbed_official | 139:e3413eddde57 | 391 | #if USE_PLL_HSE_XTAL != 0 |
mbed_official | 139:e3413eddde57 | 392 | if (SetSysClock_PLL_HSE(0) == 0) |
mbed_official | 139:e3413eddde57 | 393 | #endif |
mbed_official | 139:e3413eddde57 | 394 | { |
mbed_official | 139:e3413eddde57 | 395 | /* 3- If fail start with HSI clock */ |
mbed_official | 139:e3413eddde57 | 396 | if (SetSysClock_PLL_HSI() == 0) |
mbed_official | 139:e3413eddde57 | 397 | { |
mbed_official | 139:e3413eddde57 | 398 | while(1) |
mbed_official | 139:e3413eddde57 | 399 | { |
mbed_official | 139:e3413eddde57 | 400 | // [TODO] Put something here to tell the user that a problem occured... |
mbed_official | 139:e3413eddde57 | 401 | } |
mbed_official | 139:e3413eddde57 | 402 | } |
mbed_official | 139:e3413eddde57 | 403 | } |
mbed_official | 139:e3413eddde57 | 404 | } |
mbed_official | 139:e3413eddde57 | 405 | |
mbed_official | 139:e3413eddde57 | 406 | /* Output SYSCLK on MCO pin(PA8) for debugging purpose */ |
mbed_official | 139:e3413eddde57 | 407 | /* |
mbed_official | 139:e3413eddde57 | 408 | // Enable GPIOA clock |
mbed_official | 139:e3413eddde57 | 409 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); |
mbed_official | 139:e3413eddde57 | 410 | GPIO_InitTypeDef GPIO_InitStructure; |
mbed_official | 139:e3413eddde57 | 411 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; |
mbed_official | 139:e3413eddde57 | 412 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; |
mbed_official | 139:e3413eddde57 | 413 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
mbed_official | 139:e3413eddde57 | 414 | GPIO_Init(GPIOA, &GPIO_InitStructure); |
mbed_official | 139:e3413eddde57 | 415 | // Select the clock to output |
mbed_official | 139:e3413eddde57 | 416 | RCC_MCOConfig(RCC_MCO_SYSCLK); |
mbed_official | 139:e3413eddde57 | 417 | */ |
mbed_official | 52:a51c77007319 | 418 | } |
mbed_official | 52:a51c77007319 | 419 | |
mbed_official | 52:a51c77007319 | 420 | /** |
mbed_official | 52:a51c77007319 | 421 | * @brief Setup the external memory controller. Called in startup_stm32f10x.s |
mbed_official | 52:a51c77007319 | 422 | * before jump to __main |
mbed_official | 52:a51c77007319 | 423 | * @param None |
mbed_official | 52:a51c77007319 | 424 | * @retval None |
mbed_official | 52:a51c77007319 | 425 | */ |
mbed_official | 52:a51c77007319 | 426 | #ifdef DATA_IN_ExtSRAM |
mbed_official | 52:a51c77007319 | 427 | /** |
mbed_official | 52:a51c77007319 | 428 | * @brief Setup the external memory controller. |
mbed_official | 52:a51c77007319 | 429 | * Called in startup_stm32f10x_xx.s/.c before jump to main. |
mbed_official | 52:a51c77007319 | 430 | * This function configures the external SRAM mounted on STM3210E-EVAL |
mbed_official | 52:a51c77007319 | 431 | * board (STM32 High density devices). This SRAM will be used as program |
mbed_official | 52:a51c77007319 | 432 | * data memory (including heap and stack). |
mbed_official | 52:a51c77007319 | 433 | * @param None |
mbed_official | 52:a51c77007319 | 434 | * @retval None |
mbed_official | 52:a51c77007319 | 435 | */ |
mbed_official | 52:a51c77007319 | 436 | void SystemInit_ExtMemCtl(void) |
mbed_official | 52:a51c77007319 | 437 | { |
mbed_official | 52:a51c77007319 | 438 | /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is |
mbed_official | 52:a51c77007319 | 439 | required, then adjust the Register Addresses */ |
mbed_official | 52:a51c77007319 | 440 | |
mbed_official | 52:a51c77007319 | 441 | /* Enable FSMC clock */ |
mbed_official | 52:a51c77007319 | 442 | RCC->AHBENR = 0x00000114; |
mbed_official | 52:a51c77007319 | 443 | |
mbed_official | 52:a51c77007319 | 444 | /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */ |
mbed_official | 52:a51c77007319 | 445 | RCC->APB2ENR = 0x000001E0; |
mbed_official | 52:a51c77007319 | 446 | |
mbed_official | 52:a51c77007319 | 447 | /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/ |
mbed_official | 52:a51c77007319 | 448 | /*---------------- SRAM Address lines configuration -------------------------*/ |
mbed_official | 52:a51c77007319 | 449 | /*---------------- NOE and NWE configuration --------------------------------*/ |
mbed_official | 52:a51c77007319 | 450 | /*---------------- NE3 configuration ----------------------------------------*/ |
mbed_official | 52:a51c77007319 | 451 | /*---------------- NBL0, NBL1 configuration ---------------------------------*/ |
mbed_official | 52:a51c77007319 | 452 | |
mbed_official | 52:a51c77007319 | 453 | GPIOD->CRL = 0x44BB44BB; |
mbed_official | 52:a51c77007319 | 454 | GPIOD->CRH = 0xBBBBBBBB; |
mbed_official | 52:a51c77007319 | 455 | |
mbed_official | 52:a51c77007319 | 456 | GPIOE->CRL = 0xB44444BB; |
mbed_official | 52:a51c77007319 | 457 | GPIOE->CRH = 0xBBBBBBBB; |
mbed_official | 52:a51c77007319 | 458 | |
mbed_official | 52:a51c77007319 | 459 | GPIOF->CRL = 0x44BBBBBB; |
mbed_official | 52:a51c77007319 | 460 | GPIOF->CRH = 0xBBBB4444; |
mbed_official | 52:a51c77007319 | 461 | |
mbed_official | 52:a51c77007319 | 462 | GPIOG->CRL = 0x44BBBBBB; |
mbed_official | 52:a51c77007319 | 463 | GPIOG->CRH = 0x44444B44; |
mbed_official | 52:a51c77007319 | 464 | |
mbed_official | 52:a51c77007319 | 465 | /*---------------- FSMC Configuration ---------------------------------------*/ |
mbed_official | 52:a51c77007319 | 466 | /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/ |
mbed_official | 52:a51c77007319 | 467 | |
mbed_official | 52:a51c77007319 | 468 | FSMC_Bank1->BTCR[4] = 0x00001011; |
mbed_official | 52:a51c77007319 | 469 | FSMC_Bank1->BTCR[5] = 0x00000200; |
mbed_official | 52:a51c77007319 | 470 | } |
mbed_official | 52:a51c77007319 | 471 | #endif /* DATA_IN_ExtSRAM */ |
mbed_official | 52:a51c77007319 | 472 | |
mbed_official | 139:e3413eddde57 | 473 | #if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0) |
mbed_official | 139:e3413eddde57 | 474 | /******************************************************************************/ |
mbed_official | 139:e3413eddde57 | 475 | /* PLL (clocked by HSE) used as System clock source */ |
mbed_official | 139:e3413eddde57 | 476 | /******************************************************************************/ |
mbed_official | 139:e3413eddde57 | 477 | uint8_t SetSysClock_PLL_HSE(uint8_t bypass) |
mbed_official | 52:a51c77007319 | 478 | { |
mbed_official | 139:e3413eddde57 | 479 | __IO uint32_t StartUpCounter = 0; |
mbed_official | 139:e3413eddde57 | 480 | __IO uint32_t HSEStatus = 0; |
mbed_official | 52:a51c77007319 | 481 | |
mbed_official | 139:e3413eddde57 | 482 | /* Bypass HSE: can be done only if HSE is OFF */ |
mbed_official | 139:e3413eddde57 | 483 | RCC->CR &= ((uint32_t)~RCC_CR_HSEON); /* To be sure HSE is OFF */ |
mbed_official | 139:e3413eddde57 | 484 | if (bypass != 0) |
mbed_official | 52:a51c77007319 | 485 | { |
mbed_official | 139:e3413eddde57 | 486 | RCC->CR |= ((uint32_t)RCC_CR_HSEBYP); |
mbed_official | 52:a51c77007319 | 487 | } |
mbed_official | 52:a51c77007319 | 488 | else |
mbed_official | 52:a51c77007319 | 489 | { |
mbed_official | 139:e3413eddde57 | 490 | RCC->CR &= ((uint32_t)~RCC_CR_HSEBYP); |
mbed_official | 139:e3413eddde57 | 491 | } |
mbed_official | 52:a51c77007319 | 492 | |
mbed_official | 139:e3413eddde57 | 493 | /* Enable HSE */ |
mbed_official | 139:e3413eddde57 | 494 | RCC->CR |= ((uint32_t)RCC_CR_HSEON); |
mbed_official | 52:a51c77007319 | 495 | |
mbed_official | 139:e3413eddde57 | 496 | /* Wait till HSE is ready */ |
mbed_official | 52:a51c77007319 | 497 | do |
mbed_official | 52:a51c77007319 | 498 | { |
mbed_official | 52:a51c77007319 | 499 | HSEStatus = RCC->CR & RCC_CR_HSERDY; |
mbed_official | 139:e3413eddde57 | 500 | StartUpCounter++; |
mbed_official | 52:a51c77007319 | 501 | } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT)); |
mbed_official | 52:a51c77007319 | 502 | |
mbed_official | 139:e3413eddde57 | 503 | /* Check if HSE has started correctly */ |
mbed_official | 52:a51c77007319 | 504 | if ((RCC->CR & RCC_CR_HSERDY) != RESET) |
mbed_official | 52:a51c77007319 | 505 | { |
mbed_official | 52:a51c77007319 | 506 | /* Enable Prefetch Buffer */ |
mbed_official | 52:a51c77007319 | 507 | FLASH->ACR |= FLASH_ACR_PRFTBE; |
mbed_official | 52:a51c77007319 | 508 | |
mbed_official | 139:e3413eddde57 | 509 | /* Flash wait states |
mbed_official | 139:e3413eddde57 | 510 | 0WS for 0 < SYSCLK <= 24 MHz |
mbed_official | 139:e3413eddde57 | 511 | 1WS for 24 < SYSCLK <= 48 MHz |
mbed_official | 139:e3413eddde57 | 512 | 2WS for 48 < SYSCLK <= 72 MHz */ |
mbed_official | 52:a51c77007319 | 513 | FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY); |
mbed_official | 139:e3413eddde57 | 514 | FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2; |
mbed_official | 52:a51c77007319 | 515 | |
mbed_official | 139:e3413eddde57 | 516 | /* PLL configuration */ |
mbed_official | 139:e3413eddde57 | 517 | /* SYSCLK = 72 MHz (8 MHz * 9) */ |
mbed_official | 52:a51c77007319 | 518 | RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL)); |
mbed_official | 139:e3413eddde57 | 519 | RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9 |
mbed_official | 139:e3413eddde57 | 520 | | RCC_CFGR_HPRE_DIV1 /* HCLK = 72 MHz */ |
mbed_official | 139:e3413eddde57 | 521 | | RCC_CFGR_PPRE2_DIV1 /* PCLK2 = 72 MHz */ |
mbed_official | 139:e3413eddde57 | 522 | | RCC_CFGR_PPRE1_DIV2); /* PCLK1 = 36 MHz */ |
mbed_official | 139:e3413eddde57 | 523 | /* USBCLK = 48 MHz (72 MHz / 1.5) --> USB OK */ |
mbed_official | 52:a51c77007319 | 524 | |
mbed_official | 52:a51c77007319 | 525 | /* Enable PLL */ |
mbed_official | 52:a51c77007319 | 526 | RCC->CR |= RCC_CR_PLLON; |
mbed_official | 52:a51c77007319 | 527 | |
mbed_official | 52:a51c77007319 | 528 | /* Wait till PLL is ready */ |
mbed_official | 52:a51c77007319 | 529 | while((RCC->CR & RCC_CR_PLLRDY) == 0) |
mbed_official | 52:a51c77007319 | 530 | { |
mbed_official | 52:a51c77007319 | 531 | } |
mbed_official | 52:a51c77007319 | 532 | |
mbed_official | 52:a51c77007319 | 533 | /* Select PLL as system clock source */ |
mbed_official | 52:a51c77007319 | 534 | RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW)); |
mbed_official | 139:e3413eddde57 | 535 | RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; |
mbed_official | 52:a51c77007319 | 536 | |
mbed_official | 52:a51c77007319 | 537 | /* Wait till PLL is used as system clock source */ |
mbed_official | 139:e3413eddde57 | 538 | while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL) |
mbed_official | 52:a51c77007319 | 539 | { |
mbed_official | 52:a51c77007319 | 540 | } |
mbed_official | 139:e3413eddde57 | 541 | |
mbed_official | 139:e3413eddde57 | 542 | return 1; // OK |
mbed_official | 52:a51c77007319 | 543 | } |
mbed_official | 52:a51c77007319 | 544 | else |
mbed_official | 139:e3413eddde57 | 545 | { |
mbed_official | 139:e3413eddde57 | 546 | return 0; // FAIL |
mbed_official | 52:a51c77007319 | 547 | } |
mbed_official | 52:a51c77007319 | 548 | } |
mbed_official | 52:a51c77007319 | 549 | #endif |
mbed_official | 52:a51c77007319 | 550 | |
mbed_official | 139:e3413eddde57 | 551 | /******************************************************************************/ |
mbed_official | 139:e3413eddde57 | 552 | /* PLL (clocked by HSI) used as System clock source */ |
mbed_official | 139:e3413eddde57 | 553 | /******************************************************************************/ |
mbed_official | 139:e3413eddde57 | 554 | uint8_t SetSysClock_PLL_HSI(void) |
mbed_official | 139:e3413eddde57 | 555 | { |
mbed_official | 139:e3413eddde57 | 556 | __IO uint32_t StartUpCounter = 0; |
mbed_official | 139:e3413eddde57 | 557 | |
mbed_official | 139:e3413eddde57 | 558 | /* Enable Prefetch Buffer */ |
mbed_official | 139:e3413eddde57 | 559 | FLASH->ACR |= FLASH_ACR_PRFTBE; |
mbed_official | 139:e3413eddde57 | 560 | |
mbed_official | 139:e3413eddde57 | 561 | /* Flash wait states |
mbed_official | 139:e3413eddde57 | 562 | 0WS for 0 < SYSCLK <= 24 MHz |
mbed_official | 139:e3413eddde57 | 563 | 1WS for 24 < SYSCLK <= 48 MHz |
mbed_official | 139:e3413eddde57 | 564 | 2WS for 48 < SYSCLK <= 72 MHz */ |
mbed_official | 139:e3413eddde57 | 565 | FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY); |
mbed_official | 139:e3413eddde57 | 566 | FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2; |
mbed_official | 139:e3413eddde57 | 567 | |
mbed_official | 139:e3413eddde57 | 568 | /* PLL configuration |
mbed_official | 139:e3413eddde57 | 569 | PLLCLK = 64 MHz (HSI/2 * 16) */ |
mbed_official | 139:e3413eddde57 | 570 | RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL)); |
mbed_official | 139:e3413eddde57 | 571 | RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI_Div2 | RCC_CFGR_PLLMULL16 |
mbed_official | 139:e3413eddde57 | 572 | | RCC_CFGR_HPRE_DIV1 /* HCLK = 64 MHz */ |
mbed_official | 139:e3413eddde57 | 573 | | RCC_CFGR_PPRE2_DIV1 /* PCLK2 = 64 MHz */ |
mbed_official | 139:e3413eddde57 | 574 | | RCC_CFGR_PPRE1_DIV2); /* PCLK1 = 32 MHz */ |
mbed_official | 139:e3413eddde57 | 575 | /* USBCLK = 42.667 MHz (64 MHz / 1.5) --> USB NOT POSSIBLE */ |
mbed_official | 139:e3413eddde57 | 576 | |
mbed_official | 139:e3413eddde57 | 577 | /* Enable PLL */ |
mbed_official | 139:e3413eddde57 | 578 | RCC->CR |= RCC_CR_PLLON; |
mbed_official | 139:e3413eddde57 | 579 | |
mbed_official | 139:e3413eddde57 | 580 | /* Wait till PLL is ready */ |
mbed_official | 139:e3413eddde57 | 581 | while((RCC->CR & RCC_CR_PLLRDY) == 0) |
mbed_official | 139:e3413eddde57 | 582 | { |
mbed_official | 139:e3413eddde57 | 583 | } |
mbed_official | 139:e3413eddde57 | 584 | |
mbed_official | 139:e3413eddde57 | 585 | /* Select PLL as system clock source */ |
mbed_official | 139:e3413eddde57 | 586 | RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW)); |
mbed_official | 139:e3413eddde57 | 587 | RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; |
mbed_official | 139:e3413eddde57 | 588 | |
mbed_official | 139:e3413eddde57 | 589 | /* Wait till PLL is used as system clock source */ |
mbed_official | 139:e3413eddde57 | 590 | while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL) |
mbed_official | 139:e3413eddde57 | 591 | { |
mbed_official | 139:e3413eddde57 | 592 | } |
mbed_official | 139:e3413eddde57 | 593 | |
mbed_official | 139:e3413eddde57 | 594 | return 1; // OK |
mbed_official | 139:e3413eddde57 | 595 | } |
mbed_official | 139:e3413eddde57 | 596 | |
mbed_official | 52:a51c77007319 | 597 | /** |
mbed_official | 52:a51c77007319 | 598 | * @} |
mbed_official | 52:a51c77007319 | 599 | */ |
mbed_official | 52:a51c77007319 | 600 | |
mbed_official | 52:a51c77007319 | 601 | /** |
mbed_official | 52:a51c77007319 | 602 | * @} |
mbed_official | 52:a51c77007319 | 603 | */ |
mbed_official | 139:e3413eddde57 | 604 | |
mbed_official | 52:a51c77007319 | 605 | /** |
mbed_official | 52:a51c77007319 | 606 | * @} |
mbed_official | 139:e3413eddde57 | 607 | */ |
mbed_official | 139:e3413eddde57 | 608 | |
mbed_official | 84:f54042cbc282 | 609 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
mbed_official | 167:d5744491c362 | 610 |