|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file system_stm32wbxx.c |
| 4 | + * @author MCD Application Team |
| 5 | + * @brief CMSIS Cortex Device Peripheral Access Layer System Source File |
| 6 | + * |
| 7 | + * This file provides two functions and one global variable to be called from |
| 8 | + * user application: |
| 9 | + * - SystemInit(): This function is called at startup just after reset and |
| 10 | + * before branch to main program. This call is made inside |
| 11 | + * the "startup_stm32wbxx.s" file. |
| 12 | + * |
| 13 | + * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used |
| 14 | + * by the user application to setup the SysTick |
| 15 | + * timer or configure other parameters. |
| 16 | + * |
| 17 | + * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must |
| 18 | + * be called whenever the core clock is changed |
| 19 | + * during program execution. |
| 20 | + * |
| 21 | + * After each device reset the MSI (4 MHz) is used as system clock source. |
| 22 | + * Then SystemInit() function is called, in "startup_stm32wbxx.s" file, to |
| 23 | + * configure the system clock before to branch to main program. |
| 24 | + * |
| 25 | + * This file configures the system clock as follows: |
| 26 | + *============================================================================= |
| 27 | + *----------------------------------------------------------------------------- |
| 28 | + * System Clock source | MSI |
| 29 | + *----------------------------------------------------------------------------- |
| 30 | + * SYSCLK(Hz) | 4000000 |
| 31 | + *----------------------------------------------------------------------------- |
| 32 | + * HCLK(Hz) | 4000000 |
| 33 | + *----------------------------------------------------------------------------- |
| 34 | + * AHB Prescaler | 1 |
| 35 | + *----------------------------------------------------------------------------- |
| 36 | + * APB1 Prescaler | 1 |
| 37 | + *----------------------------------------------------------------------------- |
| 38 | + * APB2 Prescaler | 1 |
| 39 | + *----------------------------------------------------------------------------- |
| 40 | + * PLL_M | 1 |
| 41 | + *----------------------------------------------------------------------------- |
| 42 | + * PLL_N | 8 |
| 43 | + *----------------------------------------------------------------------------- |
| 44 | + * PLL_P | 7 |
| 45 | + *----------------------------------------------------------------------------- |
| 46 | + * PLL_Q | 2 |
| 47 | + *----------------------------------------------------------------------------- |
| 48 | + * PLL_R | 2 |
| 49 | + *----------------------------------------------------------------------------- |
| 50 | + * PLLSAI1_P | NA |
| 51 | + *----------------------------------------------------------------------------- |
| 52 | + * PLLSAI1_Q | NA |
| 53 | + *----------------------------------------------------------------------------- |
| 54 | + * PLLSAI1_R | NA |
| 55 | + *----------------------------------------------------------------------------- |
| 56 | + * Require 48MHz for USB OTG FS, | Disabled |
| 57 | + * SDIO and RNG clock | |
| 58 | + *----------------------------------------------------------------------------- |
| 59 | + *============================================================================= |
| 60 | + ****************************************************************************** |
| 61 | + * @attention |
| 62 | + * |
| 63 | + * <h2><center>© Copyright (c) 2019 STMicroelectronics. |
| 64 | + * All rights reserved.</center></h2> |
| 65 | + * |
| 66 | + * This software component is licensed by ST under BSD 3-Clause license, |
| 67 | + * the "License"; You may not use this file except in compliance with the |
| 68 | + * License. You may obtain a copy of the License at: |
| 69 | + * opensource.org/licenses/BSD-3-Clause |
| 70 | + * |
| 71 | + ****************************************************************************** |
| 72 | + */ |
| 73 | + |
| 74 | +/** @addtogroup CMSIS |
| 75 | + * @{ |
| 76 | + */ |
| 77 | + |
| 78 | +/** @addtogroup stm32WBxx_system |
| 79 | + * @{ |
| 80 | + */ |
| 81 | + |
| 82 | +/** @addtogroup stm32WBxx_System_Private_Includes |
| 83 | + * @{ |
| 84 | + */ |
| 85 | + |
| 86 | +#include "stm32wbxx.h" |
| 87 | + |
| 88 | +#if !defined (HSE_VALUE) |
| 89 | + #define HSE_VALUE (32000000UL) /*!< Value of the External oscillator in Hz */ |
| 90 | +#endif /* HSE_VALUE */ |
| 91 | + |
| 92 | +#if !defined (MSI_VALUE) |
| 93 | + #define MSI_VALUE (4000000UL) /*!< Value of the Internal oscillator in Hz*/ |
| 94 | +#endif /* MSI_VALUE */ |
| 95 | + |
| 96 | +#if !defined (HSI_VALUE) |
| 97 | + #define HSI_VALUE (16000000UL) /*!< Value of the Internal oscillator in Hz*/ |
| 98 | +#endif /* HSI_VALUE */ |
| 99 | + |
| 100 | +#if !defined (LSI_VALUE) |
| 101 | + #define LSI_VALUE (32000UL) /*!< Value of LSI in Hz*/ |
| 102 | +#endif /* LSI_VALUE */ |
| 103 | + |
| 104 | +#if !defined (LSE_VALUE) |
| 105 | + #define LSE_VALUE (32768UL) /*!< Value of LSE in Hz*/ |
| 106 | +#endif /* LSE_VALUE */ |
| 107 | + |
| 108 | +/** |
| 109 | + * @} |
| 110 | + */ |
| 111 | + |
| 112 | +/** @addtogroup STM32WBxx_System_Private_TypesDefinitions |
| 113 | + * @{ |
| 114 | + */ |
| 115 | + |
| 116 | +/** |
| 117 | + * @} |
| 118 | + */ |
| 119 | + |
| 120 | +/** @addtogroup STM32WBxx_System_Private_Defines |
| 121 | + * @{ |
| 122 | + */ |
| 123 | + |
| 124 | +/*!< Uncomment the following line if you need to relocate your vector Table in |
| 125 | + Internal SRAM. */ |
| 126 | +/* #define VECT_TAB_SRAM */ |
| 127 | +#ifndef VECT_TAB_OFFSET |
| 128 | +#define VECT_TAB_OFFSET 0x0U /*!< Vector Table base offset field. |
| 129 | + This value must be a multiple of 0x200. */ |
| 130 | +#endif |
| 131 | +#ifndef VECT_TAB_BASE_ADDRESS |
| 132 | +#define VECT_TAB_BASE_ADDRESS SRAM1_BASE /*!< Vector Table base offset field. |
| 133 | + This value must be a multiple of 0x200. */ |
| 134 | +#endif |
| 135 | +/** |
| 136 | + * @} |
| 137 | + */ |
| 138 | + |
| 139 | +/** @addtogroup STM32WBxx_System_Private_Macros |
| 140 | + * @{ |
| 141 | + */ |
| 142 | + |
| 143 | +/** |
| 144 | + * @} |
| 145 | + */ |
| 146 | + |
| 147 | +/** @addtogroup STM32WBxx_System_Private_Variables |
| 148 | + * @{ |
| 149 | + */ |
| 150 | + /* The SystemCoreClock variable is updated in three ways: |
| 151 | + 1) by calling CMSIS function SystemCoreClockUpdate() |
| 152 | + 2) by calling HAL API function HAL_RCC_GetHCLKFreq() |
| 153 | + 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency |
| 154 | + Note: If you use this function to configure the system clock; then there |
| 155 | + is no need to call the 2 first functions listed above, since SystemCoreClock |
| 156 | + variable is updated automatically. |
| 157 | + */ |
| 158 | + uint32_t SystemCoreClock = 4000000UL ; /*CPU1: M4 on MSI clock after startup (4MHz)*/ |
| 159 | + |
| 160 | + const uint32_t AHBPrescTable[16UL] = {1UL, 3UL, 5UL, 1UL, 1UL, 6UL, 10UL, 32UL, 2UL, 4UL, 8UL, 16UL, 64UL, 128UL, 256UL, 512UL}; |
| 161 | + |
| 162 | + const uint32_t APBPrescTable[8UL] = {0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL}; |
| 163 | + |
| 164 | + const uint32_t MSIRangeTable[16UL] = {100000UL, 200000UL, 400000UL, 800000UL, 1000000UL, 2000000UL, \ |
| 165 | + 4000000UL, 8000000UL, 16000000UL, 24000000UL, 32000000UL, 48000000UL, 0UL, 0UL, 0UL, 0UL}; /* 0UL values are incorrect cases */ |
| 166 | + |
| 167 | + const uint32_t SmpsPrescalerTable[4UL][6UL]={{1UL,3UL,2UL,2UL,1UL,2UL}, \ |
| 168 | + {2UL,6UL,4UL,3UL,2UL,4UL}, \ |
| 169 | + {4UL,12UL,8UL,6UL,4UL,8UL}, \ |
| 170 | + {4UL,12UL,8UL,6UL,4UL,8UL}}; |
| 171 | + |
| 172 | +/** |
| 173 | + * @} |
| 174 | + */ |
| 175 | + |
| 176 | +/** @addtogroup STM32WBxx_System_Private_FunctionPrototypes |
| 177 | + * @{ |
| 178 | + */ |
| 179 | + |
| 180 | +/** |
| 181 | + * @} |
| 182 | + */ |
| 183 | + |
| 184 | +/** @addtogroup STM32WBxx_System_Private_Functions |
| 185 | + * @{ |
| 186 | + */ |
| 187 | + |
| 188 | +/** |
| 189 | + * @brief Setup the microcontroller system. |
| 190 | + * @param None |
| 191 | + * @retval None |
| 192 | + */ |
| 193 | +void SystemInit(void) |
| 194 | +{ |
| 195 | + /* Configure the Vector Table location add offset address ------------------*/ |
| 196 | +#if defined(VECT_TAB_SRAM) && defined(VECT_TAB_BASE_ADDRESS) |
| 197 | + /* program in SRAMx */ |
| 198 | + SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAMx for CPU1 */ |
| 199 | +#else /* program in FLASH */ |
| 200 | + SCB->VTOR = VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */ |
| 201 | +#endif |
| 202 | + |
| 203 | + /* FPU settings ------------------------------------------------------------*/ |
| 204 | + #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) |
| 205 | + SCB->CPACR |= ((3UL << (10UL*2UL))|(3UL << (11UL*2UL))); /* set CP10 and CP11 Full Access */ |
| 206 | + #endif |
| 207 | + |
| 208 | + /* Reset the RCC clock configuration to the default reset state ------------*/ |
| 209 | + /* Set MSION bit */ |
| 210 | + RCC->CR |= RCC_CR_MSION; |
| 211 | + |
| 212 | + /* Reset CFGR register */ |
| 213 | + RCC->CFGR = 0x00070000U; |
| 214 | + |
| 215 | + /* Reset PLLSAI1ON, PLLON, HSECSSON, HSEON, HSION, and MSIPLLON bits */ |
| 216 | + RCC->CR &= (uint32_t)0xFAF6FEFBU; |
| 217 | + |
| 218 | + /*!< Reset LSI1 and LSI2 bits */ |
| 219 | + RCC->CSR &= (uint32_t)0xFFFFFFFAU; |
| 220 | + |
| 221 | + /*!< Reset HSI48ON bit */ |
| 222 | + RCC->CRRCR &= (uint32_t)0xFFFFFFFEU; |
| 223 | + |
| 224 | + /* Reset PLLCFGR register */ |
| 225 | + RCC->PLLCFGR = 0x22041000U; |
| 226 | + |
| 227 | + /* Reset PLLSAI1CFGR register */ |
| 228 | + RCC->PLLSAI1CFGR = 0x22041000U; |
| 229 | + |
| 230 | + /* Reset HSEBYP bit */ |
| 231 | + RCC->CR &= 0xFFFBFFFFU; |
| 232 | + |
| 233 | + /* Disable all interrupts */ |
| 234 | + RCC->CIER = 0x00000000; |
| 235 | +} |
| 236 | + |
| 237 | +/** |
| 238 | + * @brief Update SystemCoreClock variable according to Clock Register Values. |
| 239 | + * The SystemCoreClock variable contains the core clock (HCLK), it can |
| 240 | + * be used by the user application to setup the SysTick timer or configure |
| 241 | + * other parameters. |
| 242 | + * |
| 243 | + * @note Each time the core clock (HCLK) changes, this function must be called |
| 244 | + * to update SystemCoreClock variable value. Otherwise, any configuration |
| 245 | + * based on this variable will be incorrect. |
| 246 | + * |
| 247 | + * @note - The system frequency computed by this function is not the real |
| 248 | + * frequency in the chip. It is calculated based on the predefined |
| 249 | + * constant and the selected clock source: |
| 250 | + * |
| 251 | + * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI_VALUE(*) |
| 252 | + * |
| 253 | + * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) |
| 254 | + * |
| 255 | + * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) |
| 256 | + * |
| 257 | + * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***) |
| 258 | + * or HSI_VALUE(*) or MSI_VALUE(*) multiplied/divided by the PLL factors. |
| 259 | + * |
| 260 | + * (*) MSI_VALUE is a constant defined in stm32wbxx_hal.h file (default value |
| 261 | + * 4 MHz) but the real value may vary depending on the variations |
| 262 | + * in voltage and temperature. |
| 263 | + * |
| 264 | + * (**) HSI_VALUE is a constant defined in stm32wbxx_hal_conf.h file (default value |
| 265 | + * 16 MHz) but the real value may vary depending on the variations |
| 266 | + * in voltage and temperature. |
| 267 | + * |
| 268 | + * (***) HSE_VALUE is a constant defined in stm32wbxx_hal_conf.h file (default value |
| 269 | + * 32 MHz), user has to ensure that HSE_VALUE is same as the real |
| 270 | + * frequency of the crystal used. Otherwise, this function may |
| 271 | + * have wrong result. |
| 272 | + * |
| 273 | + * - The result of this function could be not correct when using fractional |
| 274 | + * value for HSE crystal. |
| 275 | + * |
| 276 | + * @param None |
| 277 | + * @retval None |
| 278 | + */ |
| 279 | +void SystemCoreClockUpdate(void) |
| 280 | +{ |
| 281 | + uint32_t tmp, msirange, pllvco, pllr, pllsource , pllm; |
| 282 | + |
| 283 | + /* Get MSI Range frequency--------------------------------------------------*/ |
| 284 | + |
| 285 | + /*MSI frequency range in Hz*/ |
| 286 | + msirange = MSIRangeTable[(RCC->CR & RCC_CR_MSIRANGE) >> RCC_CR_MSIRANGE_Pos]; |
| 287 | + |
| 288 | + /* Get SYSCLK source -------------------------------------------------------*/ |
| 289 | + switch (RCC->CFGR & RCC_CFGR_SWS) |
| 290 | + { |
| 291 | + case 0x00: /* MSI used as system clock source */ |
| 292 | + SystemCoreClock = msirange; |
| 293 | + break; |
| 294 | + |
| 295 | + case 0x04: /* HSI used as system clock source */ |
| 296 | + /* HSI used as system clock source */ |
| 297 | + SystemCoreClock = HSI_VALUE; |
| 298 | + break; |
| 299 | + |
| 300 | + case 0x08: /* HSE used as system clock source */ |
| 301 | + SystemCoreClock = HSE_VALUE; |
| 302 | + break; |
| 303 | + |
| 304 | + case 0x0C: /* PLL used as system clock source */ |
| 305 | + /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN |
| 306 | + SYSCLK = PLL_VCO / PLLR |
| 307 | + */ |
| 308 | + pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); |
| 309 | + pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1UL ; |
| 310 | + |
| 311 | + if(pllsource == 0x02UL) /* HSI used as PLL clock source */ |
| 312 | + { |
| 313 | + pllvco = (HSI_VALUE / pllm); |
| 314 | + } |
| 315 | + else if(pllsource == 0x03UL) /* HSE used as PLL clock source */ |
| 316 | + { |
| 317 | + pllvco = (HSE_VALUE / pllm); |
| 318 | + } |
| 319 | + else /* MSI used as PLL clock source */ |
| 320 | + { |
| 321 | + pllvco = (msirange / pllm); |
| 322 | + } |
| 323 | + |
| 324 | + pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos); |
| 325 | + pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1UL); |
| 326 | + |
| 327 | + SystemCoreClock = pllvco/pllr; |
| 328 | + break; |
| 329 | + |
| 330 | + default: |
| 331 | + SystemCoreClock = msirange; |
| 332 | + break; |
| 333 | + } |
| 334 | + |
| 335 | + /* Compute HCLK clock frequency --------------------------------------------*/ |
| 336 | + /* Get HCLK1 prescaler */ |
| 337 | + tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)]; |
| 338 | + /* HCLK clock frequency */ |
| 339 | + SystemCoreClock = SystemCoreClock / tmp; |
| 340 | + |
| 341 | +} |
| 342 | + |
| 343 | + |
| 344 | +/** |
| 345 | + * @} |
| 346 | + */ |
| 347 | + |
| 348 | +/** |
| 349 | + * @} |
| 350 | + */ |
| 351 | + |
| 352 | +/** |
| 353 | + * @} |
| 354 | + */ |
| 355 | + |
| 356 | +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
0 commit comments