STM32F479xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f4xx_hal_rcc.c 00004 * @author MCD Application Team 00005 * @brief RCC HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Reset and Clock Control (RCC) peripheral: 00008 * + Initialization and de-initialization functions 00009 * + Peripheral Control functions 00010 * 00011 @verbatim 00012 ============================================================================== 00013 ##### RCC specific features ##### 00014 ============================================================================== 00015 [..] 00016 After reset the device is running from Internal High Speed oscillator 00017 (HSI 16MHz) with Flash 0 wait state, Flash prefetch buffer, D-Cache 00018 and I-Cache are disabled, and all peripherals are off except internal 00019 SRAM, Flash and JTAG. 00020 (+) There is no prescaler on High speed (AHB) and Low speed (APB) busses; 00021 all peripherals mapped on these busses are running at HSI speed. 00022 (+) The clock for all peripherals is switched off, except the SRAM and FLASH. 00023 (+) All GPIOs are in input floating state, except the JTAG pins which 00024 are assigned to be used for debug purpose. 00025 00026 [..] 00027 Once the device started from reset, the user application has to: 00028 (+) Configure the clock source to be used to drive the System clock 00029 (if the application needs higher frequency/performance) 00030 (+) Configure the System clock frequency and Flash settings 00031 (+) Configure the AHB and APB busses prescalers 00032 (+) Enable the clock for the peripheral(s) to be used 00033 (+) Configure the clock source(s) for peripherals which clocks are not 00034 derived from the System clock (I2S, RTC, ADC, USB OTG FS/SDIO/RNG) 00035 00036 ##### RCC Limitations ##### 00037 ============================================================================== 00038 [..] 00039 A delay between an RCC peripheral clock enable and the effective peripheral 00040 enabling should be taken into account in order to manage the peripheral read/write 00041 from/to registers. 00042 (+) This delay depends on the peripheral mapping. 00043 (+) If peripheral is mapped on AHB: the delay is 2 AHB clock cycle 00044 after the clock enable bit is set on the hardware register 00045 (+) If peripheral is mapped on APB: the delay is 2 APB clock cycle 00046 after the clock enable bit is set on the hardware register 00047 00048 [..] 00049 Implemented Workaround: 00050 (+) For AHB & APB peripherals, a dummy read to the peripheral register has been 00051 inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro. 00052 00053 @endverbatim 00054 ****************************************************************************** 00055 * @attention 00056 * 00057 * <h2><center>© Copyright (c) 2017 STMicroelectronics. 00058 * All rights reserved.</center></h2> 00059 * 00060 * This software component is licensed by ST under BSD 3-Clause license, 00061 * the "License"; You may not use this file except in compliance with the 00062 * License. You may obtain a copy of the License at: 00063 * opensource.org/licenses/BSD-3-Clause 00064 * 00065 ****************************************************************************** 00066 */ 00067 00068 /* Includes ------------------------------------------------------------------*/ 00069 #include "stm32f4xx_hal.h" 00070 00071 /** @addtogroup STM32F4xx_HAL_Driver 00072 * @{ 00073 */ 00074 00075 /** @defgroup RCC RCC 00076 * @brief RCC HAL module driver 00077 * @{ 00078 */ 00079 00080 #ifdef HAL_RCC_MODULE_ENABLED 00081 00082 /* Private typedef -----------------------------------------------------------*/ 00083 /* Private define ------------------------------------------------------------*/ 00084 /** @addtogroup RCC_Private_Constants 00085 * @{ 00086 */ 00087 00088 /* Private macro -------------------------------------------------------------*/ 00089 #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() 00090 #define MCO1_GPIO_PORT GPIOA 00091 #define MCO1_PIN GPIO_PIN_8 00092 00093 #define __MCO2_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE() 00094 #define MCO2_GPIO_PORT GPIOC 00095 #define MCO2_PIN GPIO_PIN_9 00096 /** 00097 * @} 00098 */ 00099 00100 /* Private variables ---------------------------------------------------------*/ 00101 /** @defgroup RCC_Private_Variables RCC Private Variables 00102 * @{ 00103 */ 00104 /** 00105 * @} 00106 */ 00107 /* Private function prototypes -----------------------------------------------*/ 00108 /* Private functions ---------------------------------------------------------*/ 00109 00110 /** @defgroup RCC_Exported_Functions RCC Exported Functions 00111 * @{ 00112 */ 00113 00114 /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions 00115 * @brief Initialization and Configuration functions 00116 * 00117 @verbatim 00118 =============================================================================== 00119 ##### Initialization and de-initialization functions ##### 00120 =============================================================================== 00121 [..] 00122 This section provides functions allowing to configure the internal/external oscillators 00123 (HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB1 00124 and APB2). 00125 00126 [..] Internal/external clock and PLL configuration 00127 (#) HSI (high-speed internal), 16 MHz factory-trimmed RC used directly or through 00128 the PLL as System clock source. 00129 00130 (#) LSI (low-speed internal), 32 KHz low consumption RC used as IWDG and/or RTC 00131 clock source. 00132 00133 (#) HSE (high-speed external), 4 to 26 MHz crystal oscillator used directly or 00134 through the PLL as System clock source. Can be used also as RTC clock source. 00135 00136 (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source. 00137 00138 (#) PLL (clocked by HSI or HSE), featuring two different output clocks: 00139 (++) The first output is used to generate the high speed system clock (up to 168 MHz) 00140 (++) The second output is used to generate the clock for the USB OTG FS (48 MHz), 00141 the random analog generator (<=48 MHz) and the SDIO (<= 48 MHz). 00142 00143 (#) CSS (Clock security system), once enable using the macro __HAL_RCC_CSS_ENABLE() 00144 and if a HSE clock failure occurs(HSE used directly or through PLL as System 00145 clock source), the System clocks automatically switched to HSI and an interrupt 00146 is generated if enabled. The interrupt is linked to the Cortex-M4 NMI 00147 (Non-Maskable Interrupt) exception vector. 00148 00149 (#) MCO1 (microcontroller clock output), used to output HSI, LSE, HSE or PLL 00150 clock (through a configurable prescaler) on PA8 pin. 00151 00152 (#) MCO2 (microcontroller clock output), used to output HSE, PLL, SYSCLK or PLLI2S 00153 clock (through a configurable prescaler) on PC9 pin. 00154 00155 [..] System, AHB and APB busses clocks configuration 00156 (#) Several clock sources can be used to drive the System clock (SYSCLK): HSI, 00157 HSE and PLL. 00158 The AHB clock (HCLK) is derived from System clock through configurable 00159 prescaler and used to clock the CPU, memory and peripherals mapped 00160 on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived 00161 from AHB clock through configurable prescalers and used to clock 00162 the peripherals mapped on these busses. You can use 00163 "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks. 00164 00165 (#) For the STM32F405xx/07xx and STM32F415xx/17xx devices, the maximum 00166 frequency of the SYSCLK and HCLK is 168 MHz, PCLK2 84 MHz and PCLK1 42 MHz. 00167 Depending on the device voltage range, the maximum frequency should 00168 be adapted accordingly (refer to the product datasheets for more details). 00169 00170 (#) For the STM32F42xxx, STM32F43xxx, STM32F446xx, STM32F469xx and STM32F479xx devices, 00171 the maximum frequency of the SYSCLK and HCLK is 180 MHz, PCLK2 90 MHz and PCLK1 45 MHz. 00172 Depending on the device voltage range, the maximum frequency should 00173 be adapted accordingly (refer to the product datasheets for more details). 00174 00175 (#) For the STM32F401xx, the maximum frequency of the SYSCLK and HCLK is 84 MHz, 00176 PCLK2 84 MHz and PCLK1 42 MHz. 00177 Depending on the device voltage range, the maximum frequency should 00178 be adapted accordingly (refer to the product datasheets for more details). 00179 00180 (#) For the STM32F41xxx, the maximum frequency of the SYSCLK and HCLK is 100 MHz, 00181 PCLK2 100 MHz and PCLK1 50 MHz. 00182 Depending on the device voltage range, the maximum frequency should 00183 be adapted accordingly (refer to the product datasheets for more details). 00184 00185 @endverbatim 00186 * @{ 00187 */ 00188 00189 /** 00190 * @brief Resets the RCC clock configuration to the default reset state. 00191 * @note The default reset state of the clock configuration is given below: 00192 * - HSI ON and used as system clock source 00193 * - HSE and PLL OFF 00194 * - AHB, APB1 and APB2 prescaler set to 1. 00195 * - CSS, MCO1 and MCO2 OFF 00196 * - All interrupts disabled 00197 * @note This function doesn't modify the configuration of the 00198 * - Peripheral clocks 00199 * - LSI, LSE and RTC clocks 00200 * @retval HAL status 00201 */ 00202 __weak HAL_StatusTypeDef HAL_RCC_DeInit(void) 00203 { 00204 return HAL_OK; 00205 } 00206 00207 /** 00208 * @brief Initializes the RCC Oscillators according to the specified parameters in the 00209 * RCC_OscInitTypeDef. 00210 * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that 00211 * contains the configuration information for the RCC Oscillators. 00212 * @note The PLL is not disabled when used as system clock. 00213 * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not 00214 * supported by this API. User should request a transition to LSE Off 00215 * first and then LSE On or LSE Bypass. 00216 * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not 00217 * supported by this API. User should request a transition to HSE Off 00218 * first and then HSE On or HSE Bypass. 00219 * @retval HAL status 00220 */ 00221 __weak HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct) 00222 { 00223 uint32_t tickstart, pll_config; 00224 00225 /* Check Null pointer */ 00226 if(RCC_OscInitStruct == NULL) 00227 { 00228 return HAL_ERROR; 00229 } 00230 00231 /* Check the parameters */ 00232 assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType)); 00233 /*------------------------------- HSE Configuration ------------------------*/ 00234 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE) 00235 { 00236 /* Check the parameters */ 00237 assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState)); 00238 /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */ 00239 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||\ 00240 ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE))) 00241 { 00242 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF)) 00243 { 00244 return HAL_ERROR; 00245 } 00246 } 00247 else 00248 { 00249 /* Set the new HSE configuration ---------------------------------------*/ 00250 __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState); 00251 00252 /* Check the HSE State */ 00253 if((RCC_OscInitStruct->HSEState) != RCC_HSE_OFF) 00254 { 00255 /* Get Start Tick */ 00256 tickstart = HAL_GetTick(); 00257 00258 /* Wait till HSE is ready */ 00259 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET) 00260 { 00261 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE) 00262 { 00263 return HAL_TIMEOUT; 00264 } 00265 } 00266 } 00267 else 00268 { 00269 /* Get Start Tick */ 00270 tickstart = HAL_GetTick(); 00271 00272 /* Wait till HSE is bypassed or disabled */ 00273 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) 00274 { 00275 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE) 00276 { 00277 return HAL_TIMEOUT; 00278 } 00279 } 00280 } 00281 } 00282 } 00283 /*----------------------------- HSI Configuration --------------------------*/ 00284 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI) 00285 { 00286 /* Check the parameters */ 00287 assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState)); 00288 assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue)); 00289 00290 /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */ 00291 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||\ 00292 ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI))) 00293 { 00294 /* When HSI is used as system clock it will not disabled */ 00295 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON)) 00296 { 00297 return HAL_ERROR; 00298 } 00299 /* Otherwise, just the calibration is allowed */ 00300 else 00301 { 00302 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/ 00303 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue); 00304 } 00305 } 00306 else 00307 { 00308 /* Check the HSI State */ 00309 if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF) 00310 { 00311 /* Enable the Internal High Speed oscillator (HSI). */ 00312 __HAL_RCC_HSI_ENABLE(); 00313 00314 /* Get Start Tick*/ 00315 tickstart = HAL_GetTick(); 00316 00317 /* Wait till HSI is ready */ 00318 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET) 00319 { 00320 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE) 00321 { 00322 return HAL_TIMEOUT; 00323 } 00324 } 00325 00326 /* Adjusts the Internal High Speed oscillator (HSI) calibration value. */ 00327 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue); 00328 } 00329 else 00330 { 00331 /* Disable the Internal High Speed oscillator (HSI). */ 00332 __HAL_RCC_HSI_DISABLE(); 00333 00334 /* Get Start Tick*/ 00335 tickstart = HAL_GetTick(); 00336 00337 /* Wait till HSI is ready */ 00338 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) 00339 { 00340 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE) 00341 { 00342 return HAL_TIMEOUT; 00343 } 00344 } 00345 } 00346 } 00347 } 00348 /*------------------------------ LSI Configuration -------------------------*/ 00349 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI) 00350 { 00351 /* Check the parameters */ 00352 assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState)); 00353 00354 /* Check the LSI State */ 00355 if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF) 00356 { 00357 /* Enable the Internal Low Speed oscillator (LSI). */ 00358 __HAL_RCC_LSI_ENABLE(); 00359 00360 /* Get Start Tick*/ 00361 tickstart = HAL_GetTick(); 00362 00363 /* Wait till LSI is ready */ 00364 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET) 00365 { 00366 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE) 00367 { 00368 return HAL_TIMEOUT; 00369 } 00370 } 00371 } 00372 else 00373 { 00374 /* Disable the Internal Low Speed oscillator (LSI). */ 00375 __HAL_RCC_LSI_DISABLE(); 00376 00377 /* Get Start Tick */ 00378 tickstart = HAL_GetTick(); 00379 00380 /* Wait till LSI is ready */ 00381 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET) 00382 { 00383 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE) 00384 { 00385 return HAL_TIMEOUT; 00386 } 00387 } 00388 } 00389 } 00390 /*------------------------------ LSE Configuration -------------------------*/ 00391 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE) 00392 { 00393 FlagStatus pwrclkchanged = RESET; 00394 00395 /* Check the parameters */ 00396 assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState)); 00397 00398 /* Update LSE configuration in Backup Domain control register */ 00399 /* Requires to enable write access to Backup Domain of necessary */ 00400 if(__HAL_RCC_PWR_IS_CLK_DISABLED()) 00401 { 00402 __HAL_RCC_PWR_CLK_ENABLE(); 00403 pwrclkchanged = SET; 00404 } 00405 00406 if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP)) 00407 { 00408 /* Enable write access to Backup domain */ 00409 SET_BIT(PWR->CR, PWR_CR_DBP); 00410 00411 /* Wait for Backup domain Write protection disable */ 00412 tickstart = HAL_GetTick(); 00413 00414 while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP)) 00415 { 00416 if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE) 00417 { 00418 return HAL_TIMEOUT; 00419 } 00420 } 00421 } 00422 00423 /* Set the new LSE configuration -----------------------------------------*/ 00424 __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState); 00425 /* Check the LSE State */ 00426 if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF) 00427 { 00428 /* Get Start Tick*/ 00429 tickstart = HAL_GetTick(); 00430 00431 /* Wait till LSE is ready */ 00432 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) 00433 { 00434 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE) 00435 { 00436 return HAL_TIMEOUT; 00437 } 00438 } 00439 } 00440 else 00441 { 00442 /* Get Start Tick */ 00443 tickstart = HAL_GetTick(); 00444 00445 /* Wait till LSE is ready */ 00446 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET) 00447 { 00448 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE) 00449 { 00450 return HAL_TIMEOUT; 00451 } 00452 } 00453 } 00454 00455 /* Restore clock configuration if changed */ 00456 if(pwrclkchanged == SET) 00457 { 00458 __HAL_RCC_PWR_CLK_DISABLE(); 00459 } 00460 } 00461 /*-------------------------------- PLL Configuration -----------------------*/ 00462 /* Check the parameters */ 00463 assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState)); 00464 if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE) 00465 { 00466 /* Check if the PLL is used as system clock or not */ 00467 if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL) 00468 { 00469 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON) 00470 { 00471 /* Check the parameters */ 00472 assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource)); 00473 assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM)); 00474 assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN)); 00475 assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP)); 00476 assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ)); 00477 00478 /* Disable the main PLL. */ 00479 __HAL_RCC_PLL_DISABLE(); 00480 00481 /* Get Start Tick */ 00482 tickstart = HAL_GetTick(); 00483 00484 /* Wait till PLL is ready */ 00485 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET) 00486 { 00487 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE) 00488 { 00489 return HAL_TIMEOUT; 00490 } 00491 } 00492 00493 /* Configure the main PLL clock source, multiplication and division factors. */ 00494 WRITE_REG(RCC->PLLCFGR, (RCC_OscInitStruct->PLL.PLLSource | \ 00495 RCC_OscInitStruct->PLL.PLLM | \ 00496 (RCC_OscInitStruct->PLL.PLLN << RCC_PLLCFGR_PLLN_Pos) | \ 00497 (((RCC_OscInitStruct->PLL.PLLP >> 1U) - 1U) << RCC_PLLCFGR_PLLP_Pos) | \ 00498 (RCC_OscInitStruct->PLL.PLLQ << RCC_PLLCFGR_PLLQ_Pos))); 00499 /* Enable the main PLL. */ 00500 __HAL_RCC_PLL_ENABLE(); 00501 00502 /* Get Start Tick */ 00503 tickstart = HAL_GetTick(); 00504 00505 /* Wait till PLL is ready */ 00506 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET) 00507 { 00508 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE) 00509 { 00510 return HAL_TIMEOUT; 00511 } 00512 } 00513 } 00514 else 00515 { 00516 /* Disable the main PLL. */ 00517 __HAL_RCC_PLL_DISABLE(); 00518 00519 /* Get Start Tick */ 00520 tickstart = HAL_GetTick(); 00521 00522 /* Wait till PLL is ready */ 00523 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET) 00524 { 00525 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE) 00526 { 00527 return HAL_TIMEOUT; 00528 } 00529 } 00530 } 00531 } 00532 else 00533 { 00534 /* Check if there is a request to disable the PLL used as System clock source */ 00535 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF) 00536 { 00537 return HAL_ERROR; 00538 } 00539 else 00540 { 00541 /* Do not return HAL_ERROR if request repeats the current configuration */ 00542 pll_config = RCC->PLLCFGR; 00543 #if defined (RCC_PLLCFGR_PLLR) 00544 if (((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF) || 00545 (READ_BIT(pll_config, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) || 00546 (READ_BIT(pll_config, RCC_PLLCFGR_PLLM) != (RCC_OscInitStruct->PLL.PLLM) << RCC_PLLCFGR_PLLM_Pos) || 00547 (READ_BIT(pll_config, RCC_PLLCFGR_PLLN) != (RCC_OscInitStruct->PLL.PLLN) << RCC_PLLCFGR_PLLN_Pos) || 00548 (READ_BIT(pll_config, RCC_PLLCFGR_PLLP) != (((RCC_OscInitStruct->PLL.PLLP >> 1U) - 1U)) << RCC_PLLCFGR_PLLP_Pos) || 00549 (READ_BIT(pll_config, RCC_PLLCFGR_PLLQ) != (RCC_OscInitStruct->PLL.PLLQ << RCC_PLLCFGR_PLLQ_Pos)) || 00550 (READ_BIT(pll_config, RCC_PLLCFGR_PLLR) != (RCC_OscInitStruct->PLL.PLLR << RCC_PLLCFGR_PLLR_Pos))) 00551 #else 00552 if (((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF) || 00553 (READ_BIT(pll_config, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) || 00554 (READ_BIT(pll_config, RCC_PLLCFGR_PLLM) != (RCC_OscInitStruct->PLL.PLLM) << RCC_PLLCFGR_PLLM_Pos) || 00555 (READ_BIT(pll_config, RCC_PLLCFGR_PLLN) != (RCC_OscInitStruct->PLL.PLLN) << RCC_PLLCFGR_PLLN_Pos) || 00556 (READ_BIT(pll_config, RCC_PLLCFGR_PLLP) != (((RCC_OscInitStruct->PLL.PLLP >> 1U) - 1U)) << RCC_PLLCFGR_PLLP_Pos) || 00557 (READ_BIT(pll_config, RCC_PLLCFGR_PLLQ) != (RCC_OscInitStruct->PLL.PLLQ << RCC_PLLCFGR_PLLQ_Pos))) 00558 #endif 00559 { 00560 return HAL_ERROR; 00561 } 00562 } 00563 } 00564 } 00565 return HAL_OK; 00566 } 00567 00568 /** 00569 * @brief Initializes the CPU, AHB and APB busses clocks according to the specified 00570 * parameters in the RCC_ClkInitStruct. 00571 * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that 00572 * contains the configuration information for the RCC peripheral. 00573 * @param FLatency FLASH Latency, this parameter depend on device selected 00574 * 00575 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency 00576 * and updated by HAL_RCC_GetHCLKFreq() function called within this function 00577 * 00578 * @note The HSI is used (enabled by hardware) as system clock source after 00579 * startup from Reset, wake-up from STOP and STANDBY mode, or in case 00580 * of failure of the HSE used directly or indirectly as system clock 00581 * (if the Clock Security System CSS is enabled). 00582 * 00583 * @note A switch from one clock source to another occurs only if the target 00584 * clock source is ready (clock stable after startup delay or PLL locked). 00585 * If a clock source which is not yet ready is selected, the switch will 00586 * occur when the clock source will be ready. 00587 * 00588 * @note Depending on the device voltage range, the software has to set correctly 00589 * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency 00590 * (for more details refer to section above "Initialization/de-initialization functions") 00591 * @retval None 00592 */ 00593 HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency) 00594 { 00595 uint32_t tickstart; 00596 00597 /* Check Null pointer */ 00598 if(RCC_ClkInitStruct == NULL) 00599 { 00600 return HAL_ERROR; 00601 } 00602 00603 /* Check the parameters */ 00604 assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType)); 00605 assert_param(IS_FLASH_LATENCY(FLatency)); 00606 00607 /* To correctly read data from FLASH memory, the number of wait states (LATENCY) 00608 must be correctly programmed according to the frequency of the CPU clock 00609 (HCLK) and the supply voltage of the device. */ 00610 00611 /* Increasing the number of wait states because of higher CPU frequency */ 00612 if(FLatency > __HAL_FLASH_GET_LATENCY()) 00613 { 00614 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */ 00615 __HAL_FLASH_SET_LATENCY(FLatency); 00616 00617 /* Check that the new number of wait states is taken into account to access the Flash 00618 memory by reading the FLASH_ACR register */ 00619 if(__HAL_FLASH_GET_LATENCY() != FLatency) 00620 { 00621 return HAL_ERROR; 00622 } 00623 } 00624 00625 /*-------------------------- HCLK Configuration --------------------------*/ 00626 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK) 00627 { 00628 /* Set the highest APBx dividers in order to ensure that we do not go through 00629 a non-spec phase whatever we decrease or increase HCLK. */ 00630 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1) 00631 { 00632 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_HCLK_DIV16); 00633 } 00634 00635 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2) 00636 { 00637 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, (RCC_HCLK_DIV16 << 3)); 00638 } 00639 00640 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider)); 00641 MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider); 00642 } 00643 00644 /*------------------------- SYSCLK Configuration ---------------------------*/ 00645 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK) 00646 { 00647 assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource)); 00648 00649 /* HSE is selected as System Clock Source */ 00650 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE) 00651 { 00652 /* Check the HSE ready flag */ 00653 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET) 00654 { 00655 return HAL_ERROR; 00656 } 00657 } 00658 /* PLL is selected as System Clock Source */ 00659 else if((RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK) || 00660 (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLRCLK)) 00661 { 00662 /* Check the PLL ready flag */ 00663 if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET) 00664 { 00665 return HAL_ERROR; 00666 } 00667 } 00668 /* HSI is selected as System Clock Source */ 00669 else 00670 { 00671 /* Check the HSI ready flag */ 00672 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET) 00673 { 00674 return HAL_ERROR; 00675 } 00676 } 00677 00678 __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource); 00679 00680 /* Get Start Tick */ 00681 tickstart = HAL_GetTick(); 00682 00683 while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos)) 00684 { 00685 if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE) 00686 { 00687 return HAL_TIMEOUT; 00688 } 00689 } 00690 } 00691 00692 /* Decreasing the number of wait states because of lower CPU frequency */ 00693 if(FLatency < __HAL_FLASH_GET_LATENCY()) 00694 { 00695 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */ 00696 __HAL_FLASH_SET_LATENCY(FLatency); 00697 00698 /* Check that the new number of wait states is taken into account to access the Flash 00699 memory by reading the FLASH_ACR register */ 00700 if(__HAL_FLASH_GET_LATENCY() != FLatency) 00701 { 00702 return HAL_ERROR; 00703 } 00704 } 00705 00706 /*-------------------------- PCLK1 Configuration ---------------------------*/ 00707 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1) 00708 { 00709 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider)); 00710 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider); 00711 } 00712 00713 /*-------------------------- PCLK2 Configuration ---------------------------*/ 00714 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2) 00715 { 00716 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider)); 00717 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U)); 00718 } 00719 00720 /* Update the SystemCoreClock global variable */ 00721 SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> RCC_CFGR_HPRE_Pos]; 00722 00723 /* Configure the source of time base considering new system clocks settings */ 00724 HAL_InitTick (uwTickPrio); 00725 00726 return HAL_OK; 00727 } 00728 00729 /** 00730 * @} 00731 */ 00732 00733 /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions 00734 * @brief RCC clocks control functions 00735 * 00736 @verbatim 00737 =============================================================================== 00738 ##### Peripheral Control functions ##### 00739 =============================================================================== 00740 [..] 00741 This subsection provides a set of functions allowing to control the RCC Clocks 00742 frequencies. 00743 00744 @endverbatim 00745 * @{ 00746 */ 00747 00748 /** 00749 * @brief Selects the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9). 00750 * @note PA8/PC9 should be configured in alternate function mode. 00751 * @param RCC_MCOx specifies the output direction for the clock source. 00752 * This parameter can be one of the following values: 00753 * @arg RCC_MCO1: Clock source to output on MCO1 pin(PA8). 00754 * @arg RCC_MCO2: Clock source to output on MCO2 pin(PC9). 00755 * @param RCC_MCOSource specifies the clock source to output. 00756 * This parameter can be one of the following values: 00757 * @arg RCC_MCO1SOURCE_HSI: HSI clock selected as MCO1 source 00758 * @arg RCC_MCO1SOURCE_LSE: LSE clock selected as MCO1 source 00759 * @arg RCC_MCO1SOURCE_HSE: HSE clock selected as MCO1 source 00760 * @arg RCC_MCO1SOURCE_PLLCLK: main PLL clock selected as MCO1 source 00761 * @arg RCC_MCO2SOURCE_SYSCLK: System clock (SYSCLK) selected as MCO2 source 00762 * @arg RCC_MCO2SOURCE_PLLI2SCLK: PLLI2S clock selected as MCO2 source, available for all STM32F4 devices except STM32F410xx 00763 * @arg RCC_MCO2SOURCE_I2SCLK: I2SCLK clock selected as MCO2 source, available only for STM32F410Rx devices 00764 * @arg RCC_MCO2SOURCE_HSE: HSE clock selected as MCO2 source 00765 * @arg RCC_MCO2SOURCE_PLLCLK: main PLL clock selected as MCO2 source 00766 * @param RCC_MCODiv specifies the MCOx prescaler. 00767 * This parameter can be one of the following values: 00768 * @arg RCC_MCODIV_1: no division applied to MCOx clock 00769 * @arg RCC_MCODIV_2: division by 2 applied to MCOx clock 00770 * @arg RCC_MCODIV_3: division by 3 applied to MCOx clock 00771 * @arg RCC_MCODIV_4: division by 4 applied to MCOx clock 00772 * @arg RCC_MCODIV_5: division by 5 applied to MCOx clock 00773 * @note For STM32F410Rx devices to output I2SCLK clock on MCO2 you should have 00774 * at last one of the SPI clocks enabled (SPI1, SPI2 or SPI5). 00775 * @retval None 00776 */ 00777 void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv) 00778 { 00779 GPIO_InitTypeDef GPIO_InitStruct; 00780 /* Check the parameters */ 00781 assert_param(IS_RCC_MCO(RCC_MCOx)); 00782 assert_param(IS_RCC_MCODIV(RCC_MCODiv)); 00783 /* RCC_MCO1 */ 00784 if(RCC_MCOx == RCC_MCO1) 00785 { 00786 assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource)); 00787 00788 /* MCO1 Clock Enable */ 00789 __MCO1_CLK_ENABLE(); 00790 00791 /* Configure the MCO1 pin in alternate function mode */ 00792 GPIO_InitStruct.Pin = MCO1_PIN; 00793 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 00794 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 00795 GPIO_InitStruct.Pull = GPIO_NOPULL; 00796 GPIO_InitStruct.Alternate = GPIO_AF0_MCO; 00797 HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct); 00798 00799 /* Mask MCO1 and MCO1PRE[2:0] bits then Select MCO1 clock source and prescaler */ 00800 MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO1 | RCC_CFGR_MCO1PRE), (RCC_MCOSource | RCC_MCODiv)); 00801 00802 /* This RCC MCO1 enable feature is available only on STM32F410xx devices */ 00803 #if defined(RCC_CFGR_MCO1EN) 00804 __HAL_RCC_MCO1_ENABLE(); 00805 #endif /* RCC_CFGR_MCO1EN */ 00806 } 00807 #if defined(RCC_CFGR_MCO2) 00808 else 00809 { 00810 assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource)); 00811 00812 /* MCO2 Clock Enable */ 00813 __MCO2_CLK_ENABLE(); 00814 00815 /* Configure the MCO2 pin in alternate function mode */ 00816 GPIO_InitStruct.Pin = MCO2_PIN; 00817 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 00818 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 00819 GPIO_InitStruct.Pull = GPIO_NOPULL; 00820 GPIO_InitStruct.Alternate = GPIO_AF0_MCO; 00821 HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct); 00822 00823 /* Mask MCO2 and MCO2PRE[2:0] bits then Select MCO2 clock source and prescaler */ 00824 MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO2 | RCC_CFGR_MCO2PRE), (RCC_MCOSource | (RCC_MCODiv << 3U))); 00825 00826 /* This RCC MCO2 enable feature is available only on STM32F410Rx devices */ 00827 #if defined(RCC_CFGR_MCO2EN) 00828 __HAL_RCC_MCO2_ENABLE(); 00829 #endif /* RCC_CFGR_MCO2EN */ 00830 } 00831 #endif /* RCC_CFGR_MCO2 */ 00832 } 00833 00834 /** 00835 * @brief Enables the Clock Security System. 00836 * @note If a failure is detected on the HSE oscillator clock, this oscillator 00837 * is automatically disabled and an interrupt is generated to inform the 00838 * software about the failure (Clock Security System Interrupt, CSSI), 00839 * allowing the MCU to perform rescue operations. The CSSI is linked to 00840 * the Cortex-M4 NMI (Non-Maskable Interrupt) exception vector. 00841 * @retval None 00842 */ 00843 void HAL_RCC_EnableCSS(void) 00844 { 00845 *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)ENABLE; 00846 } 00847 00848 /** 00849 * @brief Disables the Clock Security System. 00850 * @retval None 00851 */ 00852 void HAL_RCC_DisableCSS(void) 00853 { 00854 *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)DISABLE; 00855 } 00856 00857 /** 00858 * @brief Returns the SYSCLK frequency 00859 * 00860 * @note The system frequency computed by this function is not the real 00861 * frequency in the chip. It is calculated based on the predefined 00862 * constant and the selected clock source: 00863 * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*) 00864 * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**) 00865 * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**) 00866 * or HSI_VALUE(*) multiplied/divided by the PLL factors. 00867 * @note (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value 00868 * 16 MHz) but the real value may vary depending on the variations 00869 * in voltage and temperature. 00870 * @note (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value 00871 * 25 MHz), user has to ensure that HSE_VALUE is same as the real 00872 * frequency of the crystal used. Otherwise, this function may 00873 * have wrong result. 00874 * 00875 * @note The result of this function could be not correct when using fractional 00876 * value for HSE crystal. 00877 * 00878 * @note This function can be used by the user application to compute the 00879 * baudrate for the communication peripherals or configure other parameters. 00880 * 00881 * @note Each time SYSCLK changes, this function must be called to update the 00882 * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect. 00883 * 00884 * 00885 * @retval SYSCLK frequency 00886 */ 00887 __weak uint32_t HAL_RCC_GetSysClockFreq(void) 00888 { 00889 uint32_t pllm = 0U, pllvco = 0U, pllp = 0U; 00890 uint32_t sysclockfreq = 0U; 00891 00892 /* Get SYSCLK source -------------------------------------------------------*/ 00893 switch (RCC->CFGR & RCC_CFGR_SWS) 00894 { 00895 case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */ 00896 { 00897 sysclockfreq = HSI_VALUE; 00898 break; 00899 } 00900 case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */ 00901 { 00902 sysclockfreq = HSE_VALUE; 00903 break; 00904 } 00905 case RCC_CFGR_SWS_PLL: /* PLL used as system clock source */ 00906 { 00907 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN 00908 SYSCLK = PLL_VCO / PLLP */ 00909 pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM; 00910 if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI) 00911 { 00912 /* HSE used as PLL clock source */ 00913 pllvco = (uint32_t) ((((uint64_t) HSE_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm); 00914 } 00915 else 00916 { 00917 /* HSI used as PLL clock source */ 00918 pllvco = (uint32_t) ((((uint64_t) HSI_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm); 00919 } 00920 pllp = ((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> RCC_PLLCFGR_PLLP_Pos) + 1U) *2U); 00921 00922 sysclockfreq = pllvco/pllp; 00923 break; 00924 } 00925 default: 00926 { 00927 sysclockfreq = HSI_VALUE; 00928 break; 00929 } 00930 } 00931 return sysclockfreq; 00932 } 00933 00934 /** 00935 * @brief Returns the HCLK frequency 00936 * @note Each time HCLK changes, this function must be called to update the 00937 * right HCLK value. Otherwise, any configuration based on this function will be incorrect. 00938 * 00939 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency 00940 * and updated within this function 00941 * @retval HCLK frequency 00942 */ 00943 uint32_t HAL_RCC_GetHCLKFreq(void) 00944 { 00945 return SystemCoreClock; 00946 } 00947 00948 /** 00949 * @brief Returns the PCLK1 frequency 00950 * @note Each time PCLK1 changes, this function must be called to update the 00951 * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect. 00952 * @retval PCLK1 frequency 00953 */ 00954 uint32_t HAL_RCC_GetPCLK1Freq(void) 00955 { 00956 /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/ 00957 return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1)>> RCC_CFGR_PPRE1_Pos]); 00958 } 00959 00960 /** 00961 * @brief Returns the PCLK2 frequency 00962 * @note Each time PCLK2 changes, this function must be called to update the 00963 * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect. 00964 * @retval PCLK2 frequency 00965 */ 00966 uint32_t HAL_RCC_GetPCLK2Freq(void) 00967 { 00968 /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/ 00969 return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2)>> RCC_CFGR_PPRE2_Pos]); 00970 } 00971 00972 /** 00973 * @brief Configures the RCC_OscInitStruct according to the internal 00974 * RCC configuration registers. 00975 * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that 00976 * will be configured. 00977 * @retval None 00978 */ 00979 __weak void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct) 00980 { 00981 /* Set all possible values for the Oscillator type parameter ---------------*/ 00982 RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI; 00983 00984 /* Get the HSE configuration -----------------------------------------------*/ 00985 if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP) 00986 { 00987 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS; 00988 } 00989 else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON) 00990 { 00991 RCC_OscInitStruct->HSEState = RCC_HSE_ON; 00992 } 00993 else 00994 { 00995 RCC_OscInitStruct->HSEState = RCC_HSE_OFF; 00996 } 00997 00998 /* Get the HSI configuration -----------------------------------------------*/ 00999 if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION) 01000 { 01001 RCC_OscInitStruct->HSIState = RCC_HSI_ON; 01002 } 01003 else 01004 { 01005 RCC_OscInitStruct->HSIState = RCC_HSI_OFF; 01006 } 01007 01008 RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR &RCC_CR_HSITRIM) >> RCC_CR_HSITRIM_Pos); 01009 01010 /* Get the LSE configuration -----------------------------------------------*/ 01011 if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP) 01012 { 01013 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS; 01014 } 01015 else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON) 01016 { 01017 RCC_OscInitStruct->LSEState = RCC_LSE_ON; 01018 } 01019 else 01020 { 01021 RCC_OscInitStruct->LSEState = RCC_LSE_OFF; 01022 } 01023 01024 /* Get the LSI configuration -----------------------------------------------*/ 01025 if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION) 01026 { 01027 RCC_OscInitStruct->LSIState = RCC_LSI_ON; 01028 } 01029 else 01030 { 01031 RCC_OscInitStruct->LSIState = RCC_LSI_OFF; 01032 } 01033 01034 /* Get the PLL configuration -----------------------------------------------*/ 01035 if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON) 01036 { 01037 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON; 01038 } 01039 else 01040 { 01041 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF; 01042 } 01043 RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); 01044 RCC_OscInitStruct->PLL.PLLM = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM); 01045 RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos); 01046 RCC_OscInitStruct->PLL.PLLP = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) + RCC_PLLCFGR_PLLP_0) << 1U) >> RCC_PLLCFGR_PLLP_Pos); 01047 RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos); 01048 } 01049 01050 /** 01051 * @brief Configures the RCC_ClkInitStruct according to the internal 01052 * RCC configuration registers. 01053 * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that 01054 * will be configured. 01055 * @param pFLatency Pointer on the Flash Latency. 01056 * @retval None 01057 */ 01058 void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency) 01059 { 01060 /* Set all possible values for the Clock type parameter --------------------*/ 01061 RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; 01062 01063 /* Get the SYSCLK configuration --------------------------------------------*/ 01064 RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW); 01065 01066 /* Get the HCLK configuration ----------------------------------------------*/ 01067 RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE); 01068 01069 /* Get the APB1 configuration ----------------------------------------------*/ 01070 RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1); 01071 01072 /* Get the APB2 configuration ----------------------------------------------*/ 01073 RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3U); 01074 01075 /* Get the Flash Wait State (Latency) configuration ------------------------*/ 01076 *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY); 01077 } 01078 01079 /** 01080 * @brief This function handles the RCC CSS interrupt request. 01081 * @note This API should be called under the NMI_Handler(). 01082 * @retval None 01083 */ 01084 void HAL_RCC_NMI_IRQHandler(void) 01085 { 01086 /* Check RCC CSSF flag */ 01087 if(__HAL_RCC_GET_IT(RCC_IT_CSS)) 01088 { 01089 /* RCC Clock Security System interrupt user callback */ 01090 HAL_RCC_CSSCallback(); 01091 01092 /* Clear RCC CSS pending bit */ 01093 __HAL_RCC_CLEAR_IT(RCC_IT_CSS); 01094 } 01095 } 01096 01097 /** 01098 * @brief RCC Clock Security System interrupt callback 01099 * @retval None 01100 */ 01101 __weak void HAL_RCC_CSSCallback(void) 01102 { 01103 /* NOTE : This function Should not be modified, when the callback is needed, 01104 the HAL_RCC_CSSCallback could be implemented in the user file 01105 */ 01106 } 01107 01108 /** 01109 * @} 01110 */ 01111 01112 /** 01113 * @} 01114 */ 01115 01116 #endif /* HAL_RCC_MODULE_ENABLED */ 01117 /** 01118 * @} 01119 */ 01120 01121 /** 01122 * @} 01123 */ 01124 01125 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/