|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file system_stm32h7xx.c |
| 4 | + * @author MCD Application Team |
| 5 | + * @brief CMSIS Cortex-M7 Device Peripheral Access Layer System Source File. |
| 6 | + * This provides system initialization template function is case of |
| 7 | + * an application using a single core STM32H7 device |
| 8 | + * |
| 9 | + * This file provides two functions and one global variable to be called from |
| 10 | + * user application: |
| 11 | + * - SystemInit(): This function is called at startup just after reset and |
| 12 | + * before branch to main program. This call is made inside |
| 13 | + * the "startup_stm32h7xx.s" file. |
| 14 | + * |
| 15 | + * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used |
| 16 | + * by the user application to setup the SysTick |
| 17 | + * timer or configure other parameters. |
| 18 | + * |
| 19 | + * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must |
| 20 | + * be called whenever the core clock is changed |
| 21 | + * during program execution. |
| 22 | + * |
| 23 | + * |
| 24 | + ****************************************************************************** |
| 25 | + * @attention |
| 26 | + * |
| 27 | + * <h2><center>© Copyright (c) 2017 STMicroelectronics. |
| 28 | + * All rights reserved.</center></h2> |
| 29 | + * |
| 30 | + * This software component is licensed by ST under BSD 3-Clause license, |
| 31 | + * the "License"; You may not use this file except in compliance with the |
| 32 | + * License. You may obtain a copy of the License at: |
| 33 | + * opensource.org/licenses/BSD-3-Clause |
| 34 | + * |
| 35 | + ****************************************************************************** |
| 36 | + */ |
| 37 | + |
| 38 | +/** @addtogroup CMSIS |
| 39 | + * @{ |
| 40 | + */ |
| 41 | + |
| 42 | +/** @addtogroup stm32h7xx_system |
| 43 | + * @{ |
| 44 | + */ |
| 45 | + |
| 46 | +/** @addtogroup STM32H7xx_System_Private_Includes |
| 47 | + * @{ |
| 48 | + */ |
| 49 | + |
| 50 | +#include "stm32h7xx.h" |
| 51 | + |
| 52 | +#if !defined (HSE_VALUE) |
| 53 | +#define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */ |
| 54 | +#endif /* HSE_VALUE */ |
| 55 | + |
| 56 | +#if !defined (CSI_VALUE) |
| 57 | + #define CSI_VALUE ((uint32_t)4000000) /*!< Value of the Internal oscillator in Hz*/ |
| 58 | +#endif /* CSI_VALUE */ |
| 59 | + |
| 60 | +#if !defined (HSI_VALUE) |
| 61 | + #define HSI_VALUE ((uint32_t)64000000) /*!< Value of the Internal oscillator in Hz*/ |
| 62 | +#endif /* HSI_VALUE */ |
| 63 | + |
| 64 | + |
| 65 | +/** |
| 66 | + * @} |
| 67 | + */ |
| 68 | + |
| 69 | +/** @addtogroup STM32H7xx_System_Private_TypesDefinitions |
| 70 | + * @{ |
| 71 | + */ |
| 72 | + |
| 73 | +/** |
| 74 | + * @} |
| 75 | + */ |
| 76 | + |
| 77 | +/** @addtogroup STM32H7xx_System_Private_Defines |
| 78 | + * @{ |
| 79 | + */ |
| 80 | + |
| 81 | +/************************* Miscellaneous Configuration ************************/ |
| 82 | +/*!< Uncomment the following line if you need to relocate your vector Table in |
| 83 | + Internal SRAM. */ |
| 84 | +/* #define VECT_TAB_SRAM */ |
| 85 | +#ifndef VECT_TAB_OFFSET |
| 86 | +#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. |
| 87 | + This value must be a multiple of 0x200. */ |
| 88 | +#endif |
| 89 | +/******************************************************************************/ |
| 90 | + |
| 91 | +/** |
| 92 | + * @} |
| 93 | + */ |
| 94 | + |
| 95 | +/** @addtogroup STM32H7xx_System_Private_Macros |
| 96 | + * @{ |
| 97 | + */ |
| 98 | + |
| 99 | +/** |
| 100 | + * @} |
| 101 | + */ |
| 102 | + |
| 103 | +/** @addtogroup STM32H7xx_System_Private_Variables |
| 104 | + * @{ |
| 105 | + */ |
| 106 | + /* This variable is updated in three ways: |
| 107 | + 1) by calling CMSIS function SystemCoreClockUpdate() |
| 108 | + 2) by calling HAL API function HAL_RCC_GetHCLKFreq() |
| 109 | + 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency |
| 110 | + Note: If you use this function to configure the system clock; then there |
| 111 | + is no need to call the 2 first functions listed above, since SystemCoreClock |
| 112 | + variable is updated automatically. |
| 113 | + */ |
| 114 | + uint32_t SystemCoreClock = 64000000; |
| 115 | + uint32_t SystemD2Clock = 64000000; |
| 116 | + const uint8_t D1CorePrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9}; |
| 117 | + |
| 118 | +/** |
| 119 | + * @} |
| 120 | + */ |
| 121 | + |
| 122 | +/** @addtogroup STM32H7xx_System_Private_FunctionPrototypes |
| 123 | + * @{ |
| 124 | + */ |
| 125 | + |
| 126 | +/** |
| 127 | + * @} |
| 128 | + */ |
| 129 | + |
| 130 | +/** @addtogroup STM32H7xx_System_Private_Functions |
| 131 | + * @{ |
| 132 | + */ |
| 133 | + |
| 134 | +/** |
| 135 | + * @brief Setup the microcontroller system |
| 136 | + * Initialize the FPU setting and vector table location |
| 137 | + * configuration. |
| 138 | + * @param None |
| 139 | + * @retval None |
| 140 | + */ |
| 141 | +void SystemInit (void) |
| 142 | +{ |
| 143 | + /* FPU settings ------------------------------------------------------------*/ |
| 144 | + #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) |
| 145 | + SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ |
| 146 | + #endif |
| 147 | + /* Reset the RCC clock configuration to the default reset state ------------*/ |
| 148 | + /* Set HSION bit */ |
| 149 | + RCC->CR |= RCC_CR_HSION; |
| 150 | + |
| 151 | + /* Reset CFGR register */ |
| 152 | + RCC->CFGR = 0x00000000; |
| 153 | + |
| 154 | + /* Reset HSEON, CSSON , CSION,RC48ON, CSIKERON PLL1ON, PLL2ON and PLL3ON bits */ |
| 155 | + RCC->CR &= (uint32_t)0xEAF6ED7F; |
| 156 | + |
| 157 | + /* Reset D1CFGR register */ |
| 158 | + RCC->D1CFGR = 0x00000000; |
| 159 | + |
| 160 | + /* Reset D2CFGR register */ |
| 161 | + RCC->D2CFGR = 0x00000000; |
| 162 | + |
| 163 | + /* Reset D3CFGR register */ |
| 164 | + RCC->D3CFGR = 0x00000000; |
| 165 | + |
| 166 | + /* Reset PLLCKSELR register */ |
| 167 | + RCC->PLLCKSELR = 0x00000000; |
| 168 | + |
| 169 | + /* Reset PLLCFGR register */ |
| 170 | + RCC->PLLCFGR = 0x00000000; |
| 171 | + /* Reset PLL1DIVR register */ |
| 172 | + RCC->PLL1DIVR = 0x00000000; |
| 173 | + /* Reset PLL1FRACR register */ |
| 174 | + RCC->PLL1FRACR = 0x00000000; |
| 175 | + |
| 176 | + /* Reset PLL2DIVR register */ |
| 177 | + RCC->PLL2DIVR = 0x00000000; |
| 178 | + |
| 179 | + /* Reset PLL2FRACR register */ |
| 180 | + |
| 181 | + RCC->PLL2FRACR = 0x00000000; |
| 182 | + /* Reset PLL3DIVR register */ |
| 183 | + RCC->PLL3DIVR = 0x00000000; |
| 184 | + |
| 185 | + /* Reset PLL3FRACR register */ |
| 186 | + RCC->PLL3FRACR = 0x00000000; |
| 187 | + |
| 188 | + /* Reset HSEBYP bit */ |
| 189 | + RCC->CR &= (uint32_t)0xFFFBFFFF; |
| 190 | + |
| 191 | + /* Disable all interrupts */ |
| 192 | + RCC->CIER = 0x00000000; |
| 193 | + |
| 194 | + |
| 195 | + /* Change the switch matrix read issuing capability to 1 for the AXI SRAM target (Target 7) */ |
| 196 | + *((__IO uint32_t*)0x51008108) = 0x000000001; |
| 197 | + |
| 198 | + |
| 199 | + /* Configure the Vector Table location add offset address ------------------*/ |
| 200 | +#ifdef VECT_TAB_SRAM |
| 201 | + SCB->VTOR = D1_AXISRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ |
| 202 | +#else |
| 203 | + SCB->VTOR = FLASH_BANK1_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */ |
| 204 | +#endif |
| 205 | + |
| 206 | + |
| 207 | +} |
| 208 | + |
| 209 | +/** |
| 210 | + * @brief Update SystemCoreClock variable according to Clock Register Values. |
| 211 | + * The SystemCoreClock variable contains the core clock , it can |
| 212 | + * be used by the user application to setup the SysTick timer or configure |
| 213 | + * other parameters. |
| 214 | + * |
| 215 | + * @note Each time the core clock changes, this function must be called |
| 216 | + * to update SystemCoreClock variable value. Otherwise, any configuration |
| 217 | + * based on this variable will be incorrect. |
| 218 | + * |
| 219 | + * @note - The system frequency computed by this function is not the real |
| 220 | + * frequency in the chip. It is calculated based on the predefined |
| 221 | + * constant and the selected clock source: |
| 222 | + * |
| 223 | + * - If SYSCLK source is CSI, SystemCoreClock will contain the CSI_VALUE(*) |
| 224 | + * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) |
| 225 | + * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) |
| 226 | + * - If SYSCLK source is PLL, SystemCoreClock will contain the CSI_VALUE(*), |
| 227 | + * HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors. |
| 228 | + * |
| 229 | + * (*) CSI_VALUE is a constant defined in stm32h7xx_hal.h file (default value |
| 230 | + * 4 MHz) but the real value may vary depending on the variations |
| 231 | + * in voltage and temperature. |
| 232 | + * (**) HSI_VALUE is a constant defined in stm32h7xx_hal.h file (default value |
| 233 | + * 64 MHz) but the real value may vary depending on the variations |
| 234 | + * in voltage and temperature. |
| 235 | + * |
| 236 | + * (***)HSE_VALUE is a constant defined in stm32h7xx_hal.h file (default value |
| 237 | + * 25 MHz), user has to ensure that HSE_VALUE is same as the real |
| 238 | + * frequency of the crystal used. Otherwise, this function may |
| 239 | + * have wrong result. |
| 240 | + * |
| 241 | + * - The result of this function could be not correct when using fractional |
| 242 | + * value for HSE crystal. |
| 243 | + * @param None |
| 244 | + * @retval None |
| 245 | + */ |
| 246 | +void SystemCoreClockUpdate (void) |
| 247 | +{ |
| 248 | +uint32_t pllp = 2, pllsource = 0, pllm = 2 ,tmp, pllfracen =0 , hsivalue = 0; |
| 249 | +float fracn1, pllvco = 0 ; |
| 250 | + |
| 251 | + /* Get SYSCLK source -------------------------------------------------------*/ |
| 252 | + |
| 253 | + switch (RCC->CFGR & RCC_CFGR_SWS) |
| 254 | + { |
| 255 | + case 0x00: /* HSI used as system clock source */ |
| 256 | + |
| 257 | + SystemCoreClock = (uint32_t) (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV)>> 3)); |
| 258 | + |
| 259 | + break; |
| 260 | + |
| 261 | + case 0x08: /* CSI used as system clock source */ |
| 262 | + SystemCoreClock = CSI_VALUE; |
| 263 | + break; |
| 264 | + |
| 265 | + case 0x10: /* HSE used as system clock source */ |
| 266 | + SystemCoreClock = HSE_VALUE; |
| 267 | + break; |
| 268 | + |
| 269 | + case 0x18: /* PLL1 used as system clock source */ |
| 270 | + |
| 271 | + /* PLL_VCO = (HSE_VALUE or HSI_VALUE or CSI_VALUE/ PLLM) * PLLN |
| 272 | + SYSCLK = PLL_VCO / PLLR |
| 273 | + */ |
| 274 | + pllsource = (RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC); |
| 275 | + pllm = ((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> 4) ; |
| 276 | + pllfracen = RCC->PLLCFGR & RCC_PLLCFGR_PLL1FRACEN; |
| 277 | + fracn1 = (pllfracen* ((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1)>> 3)); |
| 278 | + switch (pllsource) |
| 279 | + { |
| 280 | + |
| 281 | + case 0x00: /* HSI used as PLL clock source */ |
| 282 | + hsivalue = (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV)>> 3)) ; |
| 283 | + pllvco = (hsivalue/ pllm) * ((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/0x2000) +1 ); |
| 284 | + break; |
| 285 | + |
| 286 | + case 0x01: /* CSI used as PLL clock source */ |
| 287 | + pllvco = (CSI_VALUE / pllm) * ((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/0x2000) +1 ); |
| 288 | + break; |
| 289 | + |
| 290 | + case 0x02: /* HSE used as PLL clock source */ |
| 291 | + pllvco = (HSE_VALUE / pllm) * ((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/0x2000) +1 ); |
| 292 | + break; |
| 293 | + |
| 294 | + default: |
| 295 | + pllvco = (CSI_VALUE / pllm) * ((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/0x2000) +1 ); |
| 296 | + break; |
| 297 | + } |
| 298 | + pllp = (((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >>9) + 1 ) ; |
| 299 | + SystemCoreClock = (uint32_t) (pllvco/pllp); |
| 300 | + break; |
| 301 | + |
| 302 | + default: |
| 303 | + SystemCoreClock = CSI_VALUE; |
| 304 | + break; |
| 305 | + } |
| 306 | + |
| 307 | + /* Compute HCLK frequency --------------------------------------------------*/ |
| 308 | + /* Get HCLK prescaler */ |
| 309 | + tmp = D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_D1CPRE)>> POSITION_VAL(RCC_D1CFGR_D1CPRE_0)]; |
| 310 | + /* HCLK frequency */ |
| 311 | + SystemCoreClock >>= tmp; |
| 312 | +} |
| 313 | + |
| 314 | +/** |
| 315 | + * @} |
| 316 | + */ |
| 317 | + |
| 318 | +/** |
| 319 | + * @} |
| 320 | + */ |
| 321 | + |
| 322 | +/** |
| 323 | + * @} |
| 324 | + */ |
| 325 | +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
0 commit comments