mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_STM/TARGET_STM32L4/device/system_stm32l4xx.c@181:96ed750bd169, 2018-01-17 (annotated)
- Committer:
- Anna Bridge
- Date:
- Wed Jan 17 15:23:54 2018 +0000
- Revision:
- 181:96ed750bd169
- Parent:
- 170:19eb464bc2be
mbed-dev libray. Release version 158
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 170:19eb464bc2be | 1 | /** |
Kojto | 170:19eb464bc2be | 2 | ****************************************************************************** |
Kojto | 170:19eb464bc2be | 3 | * @file system_stm32l4xx.c |
Kojto | 170:19eb464bc2be | 4 | * @author MCD Application Team |
Kojto | 170:19eb464bc2be | 5 | * @version V1.3.1 |
Kojto | 170:19eb464bc2be | 6 | * @date 21-April-2017 |
Kojto | 170:19eb464bc2be | 7 | * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File |
Kojto | 170:19eb464bc2be | 8 | * |
Kojto | 170:19eb464bc2be | 9 | * This file provides two functions and one global variable to be called from |
Kojto | 170:19eb464bc2be | 10 | * user application: |
Kojto | 170:19eb464bc2be | 11 | * - SystemInit(): This function is called at startup just after reset and |
Kojto | 170:19eb464bc2be | 12 | * before branch to main program. This call is made inside |
Kojto | 170:19eb464bc2be | 13 | * the "startup_stm32l4xx.s" file. |
Kojto | 170:19eb464bc2be | 14 | * |
Kojto | 170:19eb464bc2be | 15 | * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used |
Kojto | 170:19eb464bc2be | 16 | * by the user application to setup the SysTick |
Kojto | 170:19eb464bc2be | 17 | * timer or configure other parameters. |
Kojto | 170:19eb464bc2be | 18 | * |
Kojto | 170:19eb464bc2be | 19 | * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must |
Kojto | 170:19eb464bc2be | 20 | * be called whenever the core clock is changed |
Kojto | 170:19eb464bc2be | 21 | * during program execution. |
Kojto | 170:19eb464bc2be | 22 | * |
Kojto | 170:19eb464bc2be | 23 | * After each device reset the MSI (4 MHz) is used as system clock source. |
Kojto | 170:19eb464bc2be | 24 | * Then SystemInit() function is called, in "startup_stm32l4xx.s" file, to |
Kojto | 170:19eb464bc2be | 25 | * configure the system clock before to branch to main program. |
Kojto | 170:19eb464bc2be | 26 | * |
Kojto | 170:19eb464bc2be | 27 | * This file configures the system clock as follows: |
Kojto | 170:19eb464bc2be | 28 | *============================================================================= |
Kojto | 170:19eb464bc2be | 29 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 30 | * System Clock source | MSI |
Kojto | 170:19eb464bc2be | 31 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 32 | * SYSCLK(Hz) | 4000000 |
Kojto | 170:19eb464bc2be | 33 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 34 | * HCLK(Hz) | 4000000 |
Kojto | 170:19eb464bc2be | 35 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 36 | * AHB Prescaler | 1 |
Kojto | 170:19eb464bc2be | 37 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 38 | * APB1 Prescaler | 1 |
Kojto | 170:19eb464bc2be | 39 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 40 | * APB2 Prescaler | 1 |
Kojto | 170:19eb464bc2be | 41 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 42 | * PLL_M | 1 |
Kojto | 170:19eb464bc2be | 43 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 44 | * PLL_N | 8 |
Kojto | 170:19eb464bc2be | 45 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 46 | * PLL_P | 7 |
Kojto | 170:19eb464bc2be | 47 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 48 | * PLL_Q | 2 |
Kojto | 170:19eb464bc2be | 49 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 50 | * PLL_R | 2 |
Kojto | 170:19eb464bc2be | 51 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 52 | * PLLSAI1_P | NA |
Kojto | 170:19eb464bc2be | 53 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 54 | * PLLSAI1_Q | NA |
Kojto | 170:19eb464bc2be | 55 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 56 | * PLLSAI1_R | NA |
Kojto | 170:19eb464bc2be | 57 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 58 | * PLLSAI2_P | NA |
Kojto | 170:19eb464bc2be | 59 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 60 | * PLLSAI2_Q | NA |
Kojto | 170:19eb464bc2be | 61 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 62 | * PLLSAI2_R | NA |
Kojto | 170:19eb464bc2be | 63 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 64 | * Require 48MHz for USB OTG FS, | Disabled |
Kojto | 170:19eb464bc2be | 65 | * SDIO and RNG clock | |
Kojto | 170:19eb464bc2be | 66 | *----------------------------------------------------------------------------- |
Kojto | 170:19eb464bc2be | 67 | *============================================================================= |
Kojto | 170:19eb464bc2be | 68 | ****************************************************************************** |
Kojto | 170:19eb464bc2be | 69 | * @attention |
Kojto | 170:19eb464bc2be | 70 | * |
Kojto | 170:19eb464bc2be | 71 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
Kojto | 170:19eb464bc2be | 72 | * |
Kojto | 170:19eb464bc2be | 73 | * Redistribution and use in source and binary forms, with or without modification, |
Kojto | 170:19eb464bc2be | 74 | * are permitted provided that the following conditions are met: |
Kojto | 170:19eb464bc2be | 75 | * 1. Redistributions of source code must retain the above copyright notice, |
Kojto | 170:19eb464bc2be | 76 | * this list of conditions and the following disclaimer. |
Kojto | 170:19eb464bc2be | 77 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
Kojto | 170:19eb464bc2be | 78 | * this list of conditions and the following disclaimer in the documentation |
Kojto | 170:19eb464bc2be | 79 | * and/or other materials provided with the distribution. |
Kojto | 170:19eb464bc2be | 80 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
Kojto | 170:19eb464bc2be | 81 | * may be used to endorse or promote products derived from this software |
Kojto | 170:19eb464bc2be | 82 | * without specific prior written permission. |
Kojto | 170:19eb464bc2be | 83 | * |
Kojto | 170:19eb464bc2be | 84 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Kojto | 170:19eb464bc2be | 85 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Kojto | 170:19eb464bc2be | 86 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Kojto | 170:19eb464bc2be | 87 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
Kojto | 170:19eb464bc2be | 88 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
Kojto | 170:19eb464bc2be | 89 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
Kojto | 170:19eb464bc2be | 90 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
Kojto | 170:19eb464bc2be | 91 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
Kojto | 170:19eb464bc2be | 92 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Kojto | 170:19eb464bc2be | 93 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Kojto | 170:19eb464bc2be | 94 | * |
Kojto | 170:19eb464bc2be | 95 | ****************************************************************************** |
Kojto | 170:19eb464bc2be | 96 | */ |
Kojto | 170:19eb464bc2be | 97 | |
Kojto | 170:19eb464bc2be | 98 | /** @addtogroup CMSIS |
Kojto | 170:19eb464bc2be | 99 | * @{ |
Kojto | 170:19eb464bc2be | 100 | */ |
Kojto | 170:19eb464bc2be | 101 | |
Kojto | 170:19eb464bc2be | 102 | /** @addtogroup stm32l4xx_system |
Kojto | 170:19eb464bc2be | 103 | * @{ |
Kojto | 170:19eb464bc2be | 104 | */ |
Kojto | 170:19eb464bc2be | 105 | |
Kojto | 170:19eb464bc2be | 106 | /** @addtogroup STM32L4xx_System_Private_Includes |
Kojto | 170:19eb464bc2be | 107 | * @{ |
Kojto | 170:19eb464bc2be | 108 | */ |
Kojto | 170:19eb464bc2be | 109 | |
Kojto | 170:19eb464bc2be | 110 | #include "stm32l4xx.h" |
Kojto | 170:19eb464bc2be | 111 | |
Kojto | 170:19eb464bc2be | 112 | #if !defined (HSE_VALUE) |
Kojto | 170:19eb464bc2be | 113 | #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ |
Kojto | 170:19eb464bc2be | 114 | #endif /* HSE_VALUE */ |
Kojto | 170:19eb464bc2be | 115 | |
Kojto | 170:19eb464bc2be | 116 | #if !defined (MSI_VALUE) |
Kojto | 170:19eb464bc2be | 117 | #define MSI_VALUE ((uint32_t)4000000) /*!< Value of the Internal oscillator in Hz*/ |
Kojto | 170:19eb464bc2be | 118 | #endif /* MSI_VALUE */ |
Kojto | 170:19eb464bc2be | 119 | |
Kojto | 170:19eb464bc2be | 120 | #if !defined (HSI_VALUE) |
Kojto | 170:19eb464bc2be | 121 | #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ |
Kojto | 170:19eb464bc2be | 122 | #endif /* HSI_VALUE */ |
Kojto | 170:19eb464bc2be | 123 | |
Kojto | 170:19eb464bc2be | 124 | /** |
Kojto | 170:19eb464bc2be | 125 | * @} |
Kojto | 170:19eb464bc2be | 126 | */ |
Kojto | 170:19eb464bc2be | 127 | |
Kojto | 170:19eb464bc2be | 128 | /** @addtogroup STM32L4xx_System_Private_TypesDefinitions |
Kojto | 170:19eb464bc2be | 129 | * @{ |
Kojto | 170:19eb464bc2be | 130 | */ |
Kojto | 170:19eb464bc2be | 131 | |
Kojto | 170:19eb464bc2be | 132 | /** |
Kojto | 170:19eb464bc2be | 133 | * @} |
Kojto | 170:19eb464bc2be | 134 | */ |
Kojto | 170:19eb464bc2be | 135 | |
Kojto | 170:19eb464bc2be | 136 | /** @addtogroup STM32L4xx_System_Private_Defines |
Kojto | 170:19eb464bc2be | 137 | * @{ |
Kojto | 170:19eb464bc2be | 138 | */ |
Kojto | 170:19eb464bc2be | 139 | |
Kojto | 170:19eb464bc2be | 140 | /************************* Miscellaneous Configuration ************************/ |
Kojto | 170:19eb464bc2be | 141 | /*!< Uncomment the following line if you need to relocate your vector Table in |
Kojto | 170:19eb464bc2be | 142 | Internal SRAM. */ |
Kojto | 170:19eb464bc2be | 143 | /* #define VECT_TAB_SRAM */ |
Kojto | 170:19eb464bc2be | 144 | #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. |
Kojto | 170:19eb464bc2be | 145 | This value must be a multiple of 0x200. */ |
Kojto | 170:19eb464bc2be | 146 | /******************************************************************************/ |
Kojto | 170:19eb464bc2be | 147 | /** |
Kojto | 170:19eb464bc2be | 148 | * @} |
Kojto | 170:19eb464bc2be | 149 | */ |
Kojto | 170:19eb464bc2be | 150 | |
Kojto | 170:19eb464bc2be | 151 | /** @addtogroup STM32L4xx_System_Private_Macros |
Kojto | 170:19eb464bc2be | 152 | * @{ |
Kojto | 170:19eb464bc2be | 153 | */ |
Kojto | 170:19eb464bc2be | 154 | |
Kojto | 170:19eb464bc2be | 155 | /** |
Kojto | 170:19eb464bc2be | 156 | * @} |
Kojto | 170:19eb464bc2be | 157 | */ |
Kojto | 170:19eb464bc2be | 158 | |
Kojto | 170:19eb464bc2be | 159 | /** @addtogroup STM32L4xx_System_Private_Variables |
Kojto | 170:19eb464bc2be | 160 | * @{ |
Kojto | 170:19eb464bc2be | 161 | */ |
Kojto | 170:19eb464bc2be | 162 | /* The SystemCoreClock variable is updated in three ways: |
Kojto | 170:19eb464bc2be | 163 | 1) by calling CMSIS function SystemCoreClockUpdate() |
Kojto | 170:19eb464bc2be | 164 | 2) by calling HAL API function HAL_RCC_GetHCLKFreq() |
Kojto | 170:19eb464bc2be | 165 | 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency |
Kojto | 170:19eb464bc2be | 166 | Note: If you use this function to configure the system clock; then there |
Kojto | 170:19eb464bc2be | 167 | is no need to call the 2 first functions listed above, since SystemCoreClock |
Kojto | 170:19eb464bc2be | 168 | variable is updated automatically. |
Kojto | 170:19eb464bc2be | 169 | */ |
Kojto | 170:19eb464bc2be | 170 | uint32_t SystemCoreClock = 4000000; |
Kojto | 170:19eb464bc2be | 171 | |
Kojto | 170:19eb464bc2be | 172 | const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; |
Kojto | 170:19eb464bc2be | 173 | const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4}; |
Kojto | 170:19eb464bc2be | 174 | const uint32_t MSIRangeTable[12] = {100000, 200000, 400000, 800000, 1000000, 2000000, \ |
Kojto | 170:19eb464bc2be | 175 | 4000000, 8000000, 16000000, 24000000, 32000000, 48000000}; |
Kojto | 170:19eb464bc2be | 176 | /** |
Kojto | 170:19eb464bc2be | 177 | * @} |
Kojto | 170:19eb464bc2be | 178 | */ |
Kojto | 170:19eb464bc2be | 179 | |
Kojto | 170:19eb464bc2be | 180 | /** @addtogroup STM32L4xx_System_Private_FunctionPrototypes |
Kojto | 170:19eb464bc2be | 181 | * @{ |
Kojto | 170:19eb464bc2be | 182 | */ |
Kojto | 170:19eb464bc2be | 183 | |
Kojto | 170:19eb464bc2be | 184 | /** |
Kojto | 170:19eb464bc2be | 185 | * @} |
Kojto | 170:19eb464bc2be | 186 | */ |
Kojto | 170:19eb464bc2be | 187 | |
Kojto | 170:19eb464bc2be | 188 | /** @addtogroup STM32L4xx_System_Private_Functions |
Kojto | 170:19eb464bc2be | 189 | * @{ |
Kojto | 170:19eb464bc2be | 190 | */ |
Kojto | 170:19eb464bc2be | 191 | |
Kojto | 170:19eb464bc2be | 192 | /*+ MBED */ |
Kojto | 170:19eb464bc2be | 193 | #if 0 |
Kojto | 170:19eb464bc2be | 194 | /*- MBED */ |
Kojto | 170:19eb464bc2be | 195 | |
Kojto | 170:19eb464bc2be | 196 | /** |
Kojto | 170:19eb464bc2be | 197 | * @brief Setup the microcontroller system. |
Kojto | 170:19eb464bc2be | 198 | * @param None |
Kojto | 170:19eb464bc2be | 199 | * @retval None |
Kojto | 170:19eb464bc2be | 200 | */ |
Kojto | 170:19eb464bc2be | 201 | |
Kojto | 170:19eb464bc2be | 202 | void SystemInit(void) |
Kojto | 170:19eb464bc2be | 203 | { |
Kojto | 170:19eb464bc2be | 204 | /* FPU settings ------------------------------------------------------------*/ |
Kojto | 170:19eb464bc2be | 205 | #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) |
Kojto | 170:19eb464bc2be | 206 | SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ |
Kojto | 170:19eb464bc2be | 207 | #endif |
Kojto | 170:19eb464bc2be | 208 | /* Reset the RCC clock configuration to the default reset state ------------*/ |
Kojto | 170:19eb464bc2be | 209 | /* Set MSION bit */ |
Kojto | 170:19eb464bc2be | 210 | RCC->CR |= RCC_CR_MSION; |
Kojto | 170:19eb464bc2be | 211 | |
Kojto | 170:19eb464bc2be | 212 | /* Reset CFGR register */ |
Kojto | 170:19eb464bc2be | 213 | RCC->CFGR = 0x00000000; |
Kojto | 170:19eb464bc2be | 214 | |
Kojto | 170:19eb464bc2be | 215 | /* Reset HSEON, CSSON , HSION, and PLLON bits */ |
Kojto | 170:19eb464bc2be | 216 | RCC->CR &= (uint32_t)0xEAF6FFFF; |
Kojto | 170:19eb464bc2be | 217 | |
Kojto | 170:19eb464bc2be | 218 | /* Reset PLLCFGR register */ |
Kojto | 170:19eb464bc2be | 219 | RCC->PLLCFGR = 0x00001000; |
Kojto | 170:19eb464bc2be | 220 | |
Kojto | 170:19eb464bc2be | 221 | /* Reset HSEBYP bit */ |
Kojto | 170:19eb464bc2be | 222 | RCC->CR &= (uint32_t)0xFFFBFFFF; |
Kojto | 170:19eb464bc2be | 223 | |
Kojto | 170:19eb464bc2be | 224 | /* Disable all interrupts */ |
Kojto | 170:19eb464bc2be | 225 | RCC->CIER = 0x00000000; |
Kojto | 170:19eb464bc2be | 226 | |
Kojto | 170:19eb464bc2be | 227 | /* Configure the Vector Table location add offset address ------------------*/ |
Kojto | 170:19eb464bc2be | 228 | #ifdef VECT_TAB_SRAM |
Kojto | 170:19eb464bc2be | 229 | SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ |
Kojto | 170:19eb464bc2be | 230 | #else |
Kojto | 170:19eb464bc2be | 231 | SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */ |
Kojto | 170:19eb464bc2be | 232 | #endif |
Kojto | 170:19eb464bc2be | 233 | } |
Kojto | 170:19eb464bc2be | 234 | |
Kojto | 170:19eb464bc2be | 235 | /*+ MBED */ |
Kojto | 170:19eb464bc2be | 236 | #endif |
Kojto | 170:19eb464bc2be | 237 | /*- MBED */ |
Kojto | 170:19eb464bc2be | 238 | |
Kojto | 170:19eb464bc2be | 239 | /** |
Kojto | 170:19eb464bc2be | 240 | * @brief Update SystemCoreClock variable according to Clock Register Values. |
Kojto | 170:19eb464bc2be | 241 | * The SystemCoreClock variable contains the core clock (HCLK), it can |
Kojto | 170:19eb464bc2be | 242 | * be used by the user application to setup the SysTick timer or configure |
Kojto | 170:19eb464bc2be | 243 | * other parameters. |
Kojto | 170:19eb464bc2be | 244 | * |
Kojto | 170:19eb464bc2be | 245 | * @note Each time the core clock (HCLK) changes, this function must be called |
Kojto | 170:19eb464bc2be | 246 | * to update SystemCoreClock variable value. Otherwise, any configuration |
Kojto | 170:19eb464bc2be | 247 | * based on this variable will be incorrect. |
Kojto | 170:19eb464bc2be | 248 | * |
Kojto | 170:19eb464bc2be | 249 | * @note - The system frequency computed by this function is not the real |
Kojto | 170:19eb464bc2be | 250 | * frequency in the chip. It is calculated based on the predefined |
Kojto | 170:19eb464bc2be | 251 | * constant and the selected clock source: |
Kojto | 170:19eb464bc2be | 252 | * |
Kojto | 170:19eb464bc2be | 253 | * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI_VALUE(*) |
Kojto | 170:19eb464bc2be | 254 | * |
Kojto | 170:19eb464bc2be | 255 | * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) |
Kojto | 170:19eb464bc2be | 256 | * |
Kojto | 170:19eb464bc2be | 257 | * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) |
Kojto | 170:19eb464bc2be | 258 | * |
Kojto | 170:19eb464bc2be | 259 | * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***) |
Kojto | 170:19eb464bc2be | 260 | * or HSI_VALUE(*) or MSI_VALUE(*) multiplied/divided by the PLL factors. |
Kojto | 170:19eb464bc2be | 261 | * |
Kojto | 170:19eb464bc2be | 262 | * (*) MSI_VALUE is a constant defined in stm32l4xx_hal.h file (default value |
Kojto | 170:19eb464bc2be | 263 | * 4 MHz) but the real value may vary depending on the variations |
Kojto | 170:19eb464bc2be | 264 | * in voltage and temperature. |
Kojto | 170:19eb464bc2be | 265 | * |
Kojto | 170:19eb464bc2be | 266 | * (**) HSI_VALUE is a constant defined in stm32l4xx_hal.h file (default value |
Kojto | 170:19eb464bc2be | 267 | * 16 MHz) but the real value may vary depending on the variations |
Kojto | 170:19eb464bc2be | 268 | * in voltage and temperature. |
Kojto | 170:19eb464bc2be | 269 | * |
Kojto | 170:19eb464bc2be | 270 | * (***) HSE_VALUE is a constant defined in stm32l4xx_hal.h file (default value |
Kojto | 170:19eb464bc2be | 271 | * 8 MHz), user has to ensure that HSE_VALUE is same as the real |
Kojto | 170:19eb464bc2be | 272 | * frequency of the crystal used. Otherwise, this function may |
Kojto | 170:19eb464bc2be | 273 | * have wrong result. |
Kojto | 170:19eb464bc2be | 274 | * |
Kojto | 170:19eb464bc2be | 275 | * - The result of this function could be not correct when using fractional |
Kojto | 170:19eb464bc2be | 276 | * value for HSE crystal. |
Kojto | 170:19eb464bc2be | 277 | * |
Kojto | 170:19eb464bc2be | 278 | * @param None |
Kojto | 170:19eb464bc2be | 279 | * @retval None |
Kojto | 170:19eb464bc2be | 280 | */ |
Kojto | 170:19eb464bc2be | 281 | void SystemCoreClockUpdate(void) |
Kojto | 170:19eb464bc2be | 282 | { |
Kojto | 170:19eb464bc2be | 283 | uint32_t tmp = 0, msirange = 0, pllvco = 0, pllr = 2, pllsource = 0, pllm = 2; |
Kojto | 170:19eb464bc2be | 284 | |
Kojto | 170:19eb464bc2be | 285 | /* Get MSI Range frequency--------------------------------------------------*/ |
Kojto | 170:19eb464bc2be | 286 | if((RCC->CR & RCC_CR_MSIRGSEL) == RESET) |
Kojto | 170:19eb464bc2be | 287 | { /* MSISRANGE from RCC_CSR applies */ |
Kojto | 170:19eb464bc2be | 288 | msirange = (RCC->CSR & RCC_CSR_MSISRANGE) >> 8; |
Kojto | 170:19eb464bc2be | 289 | } |
Kojto | 170:19eb464bc2be | 290 | else |
Kojto | 170:19eb464bc2be | 291 | { /* MSIRANGE from RCC_CR applies */ |
Kojto | 170:19eb464bc2be | 292 | msirange = (RCC->CR & RCC_CR_MSIRANGE) >> 4; |
Kojto | 170:19eb464bc2be | 293 | } |
Kojto | 170:19eb464bc2be | 294 | /*MSI frequency range in HZ*/ |
Kojto | 170:19eb464bc2be | 295 | msirange = MSIRangeTable[msirange]; |
Kojto | 170:19eb464bc2be | 296 | |
Kojto | 170:19eb464bc2be | 297 | /* Get SYSCLK source -------------------------------------------------------*/ |
Kojto | 170:19eb464bc2be | 298 | switch (RCC->CFGR & RCC_CFGR_SWS) |
Kojto | 170:19eb464bc2be | 299 | { |
Kojto | 170:19eb464bc2be | 300 | case 0x00: /* MSI used as system clock source */ |
Kojto | 170:19eb464bc2be | 301 | SystemCoreClock = msirange; |
Kojto | 170:19eb464bc2be | 302 | break; |
Kojto | 170:19eb464bc2be | 303 | |
Kojto | 170:19eb464bc2be | 304 | case 0x04: /* HSI used as system clock source */ |
Kojto | 170:19eb464bc2be | 305 | SystemCoreClock = HSI_VALUE; |
Kojto | 170:19eb464bc2be | 306 | break; |
Kojto | 170:19eb464bc2be | 307 | |
Kojto | 170:19eb464bc2be | 308 | case 0x08: /* HSE used as system clock source */ |
Kojto | 170:19eb464bc2be | 309 | SystemCoreClock = HSE_VALUE; |
Kojto | 170:19eb464bc2be | 310 | break; |
Kojto | 170:19eb464bc2be | 311 | |
Kojto | 170:19eb464bc2be | 312 | case 0x0C: /* PLL used as system clock source */ |
Kojto | 170:19eb464bc2be | 313 | /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN |
Kojto | 170:19eb464bc2be | 314 | SYSCLK = PLL_VCO / PLLR |
Kojto | 170:19eb464bc2be | 315 | */ |
Kojto | 170:19eb464bc2be | 316 | pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); |
Kojto | 170:19eb464bc2be | 317 | pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1 ; |
Kojto | 170:19eb464bc2be | 318 | |
Kojto | 170:19eb464bc2be | 319 | switch (pllsource) |
Kojto | 170:19eb464bc2be | 320 | { |
Kojto | 170:19eb464bc2be | 321 | case 0x02: /* HSI used as PLL clock source */ |
Kojto | 170:19eb464bc2be | 322 | pllvco = (HSI_VALUE / pllm); |
Kojto | 170:19eb464bc2be | 323 | break; |
Kojto | 170:19eb464bc2be | 324 | |
Kojto | 170:19eb464bc2be | 325 | case 0x03: /* HSE used as PLL clock source */ |
Kojto | 170:19eb464bc2be | 326 | pllvco = (HSE_VALUE / pllm); |
Kojto | 170:19eb464bc2be | 327 | break; |
Kojto | 170:19eb464bc2be | 328 | |
Kojto | 170:19eb464bc2be | 329 | default: /* MSI used as PLL clock source */ |
Kojto | 170:19eb464bc2be | 330 | pllvco = (msirange / pllm); |
Kojto | 170:19eb464bc2be | 331 | break; |
Kojto | 170:19eb464bc2be | 332 | } |
Kojto | 170:19eb464bc2be | 333 | pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8); |
Kojto | 170:19eb464bc2be | 334 | pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1) * 2; |
Kojto | 170:19eb464bc2be | 335 | SystemCoreClock = pllvco/pllr; |
Kojto | 170:19eb464bc2be | 336 | break; |
Kojto | 170:19eb464bc2be | 337 | |
Kojto | 170:19eb464bc2be | 338 | default: |
Kojto | 170:19eb464bc2be | 339 | SystemCoreClock = msirange; |
Kojto | 170:19eb464bc2be | 340 | break; |
Kojto | 170:19eb464bc2be | 341 | } |
Kojto | 170:19eb464bc2be | 342 | /* Compute HCLK clock frequency --------------------------------------------*/ |
Kojto | 170:19eb464bc2be | 343 | /* Get HCLK prescaler */ |
Kojto | 170:19eb464bc2be | 344 | tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; |
Kojto | 170:19eb464bc2be | 345 | /* HCLK clock frequency */ |
Kojto | 170:19eb464bc2be | 346 | SystemCoreClock >>= tmp; |
Kojto | 170:19eb464bc2be | 347 | } |
Kojto | 170:19eb464bc2be | 348 | |
Kojto | 170:19eb464bc2be | 349 | |
Kojto | 170:19eb464bc2be | 350 | /** |
Kojto | 170:19eb464bc2be | 351 | * @} |
Kojto | 170:19eb464bc2be | 352 | */ |
Kojto | 170:19eb464bc2be | 353 | |
Kojto | 170:19eb464bc2be | 354 | /** |
Kojto | 170:19eb464bc2be | 355 | * @} |
Kojto | 170:19eb464bc2be | 356 | */ |
Kojto | 170:19eb464bc2be | 357 | |
Kojto | 170:19eb464bc2be | 358 | /** |
Kojto | 170:19eb464bc2be | 359 | * @} |
Kojto | 170:19eb464bc2be | 360 | */ |
Kojto | 170:19eb464bc2be | 361 | |
Kojto | 170:19eb464bc2be | 362 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |