STM32F103xB HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f1xx_ll_rcc.c 00004 * @author MCD Application Team 00005 * @brief RCC LL module driver. 00006 ****************************************************************************** 00007 * @attention 00008 * 00009 * <h2><center>© Copyright (c) 2016 STMicroelectronics. 00010 * All rights reserved.</center></h2> 00011 * 00012 * This software component is licensed by ST under BSD 3-Clause license, 00013 * the "License"; You may not use this file except in compliance with the 00014 * License. You may obtain a copy of the License at: 00015 * opensource.org/licenses/BSD-3-Clause 00016 * 00017 ****************************************************************************** 00018 */ 00019 00020 #if defined(USE_FULL_LL_DRIVER) 00021 00022 /* Includes ------------------------------------------------------------------*/ 00023 #include "stm32f1xx_ll_rcc.h" 00024 #ifdef USE_FULL_ASSERT 00025 #include "stm32_assert.h" 00026 #else 00027 #define assert_param(expr) ((void)0U) 00028 #endif /* USE_FULL_ASSERT */ 00029 /** @addtogroup STM32F1xx_LL_Driver 00030 * @{ 00031 */ 00032 00033 #if defined(RCC) 00034 00035 /** @defgroup RCC_LL RCC 00036 * @{ 00037 */ 00038 00039 /* Private types -------------------------------------------------------------*/ 00040 /* Private variables ---------------------------------------------------------*/ 00041 /* Private constants ---------------------------------------------------------*/ 00042 /* Private macros ------------------------------------------------------------*/ 00043 /** @addtogroup RCC_LL_Private_Macros 00044 * @{ 00045 */ 00046 #if defined(RCC_PLLI2S_SUPPORT) 00047 #define IS_LL_RCC_I2S_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_I2S2_CLKSOURCE) \ 00048 || ((__VALUE__) == LL_RCC_I2S3_CLKSOURCE)) 00049 #endif /* RCC_PLLI2S_SUPPORT */ 00050 00051 #if defined(USB) || defined(USB_OTG_FS) 00052 #define IS_LL_RCC_USB_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USB_CLKSOURCE)) 00053 #endif /* USB */ 00054 00055 #define IS_LL_RCC_ADC_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_ADC_CLKSOURCE)) 00056 /** 00057 * @} 00058 */ 00059 00060 /* Private function prototypes -----------------------------------------------*/ 00061 /** @defgroup RCC_LL_Private_Functions RCC Private functions 00062 * @{ 00063 */ 00064 uint32_t RCC_GetSystemClockFreq(void); 00065 uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency); 00066 uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency); 00067 uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency); 00068 uint32_t RCC_PLL_GetFreqDomain_SYS(void); 00069 #if defined(RCC_PLLI2S_SUPPORT) 00070 uint32_t RCC_PLLI2S_GetFreqDomain_I2S(void); 00071 #endif /* RCC_PLLI2S_SUPPORT */ 00072 #if defined(RCC_PLL2_SUPPORT) 00073 uint32_t RCC_PLL2_GetFreqClockFreq(void); 00074 #endif /* RCC_PLL2_SUPPORT */ 00075 /** 00076 * @} 00077 */ 00078 00079 /* Exported functions --------------------------------------------------------*/ 00080 /** @addtogroup RCC_LL_Exported_Functions 00081 * @{ 00082 */ 00083 00084 /** @addtogroup RCC_LL_EF_Init 00085 * @{ 00086 */ 00087 00088 /** 00089 * @brief Reset the RCC clock configuration to the default reset state. 00090 * @note The default reset state of the clock configuration is given below: 00091 * - HSI ON and used as system clock source 00092 * - HSE PLL, PLL2 & PLL3 are OFF 00093 * - AHB, APB1 and APB2 prescaler set to 1. 00094 * - CSS, MCO OFF 00095 * - All interrupts disabled 00096 * @note This function doesn't modify the configuration of the 00097 * - Peripheral clocks 00098 * - LSI, LSE and RTC clocks 00099 * @retval An ErrorStatus enumeration value: 00100 * - SUCCESS: RCC registers are de-initialized 00101 * - ERROR: not applicable 00102 */ 00103 ErrorStatus LL_RCC_DeInit(void) 00104 { 00105 /* Set HSION bit */ 00106 LL_RCC_HSI_Enable(); 00107 00108 /* Wait for HSI READY bit */ 00109 while (LL_RCC_HSI_IsReady() != 1U) 00110 {} 00111 00112 /* Configure HSI as system clock source */ 00113 LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSI); 00114 00115 /* Wait till clock switch is ready */ 00116 while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSI) 00117 {} 00118 00119 /* Reset PLLON bit */ 00120 CLEAR_BIT(RCC->CR, RCC_CR_PLLON); 00121 00122 /* Wait for PLL READY bit to be reset */ 00123 while (LL_RCC_PLL_IsReady() != 0U) 00124 {} 00125 00126 /* Reset CFGR register */ 00127 LL_RCC_WriteReg(CFGR, 0x00000000U); 00128 00129 /* Reset HSEON, HSEBYP & CSSON bits */ 00130 CLEAR_BIT(RCC->CR, (RCC_CR_CSSON | RCC_CR_HSEON | RCC_CR_HSEBYP)); 00131 00132 #if defined(RCC_CR_PLL2ON) 00133 /* Reset PLL2ON bit */ 00134 CLEAR_BIT(RCC->CR, RCC_CR_PLL2ON); 00135 #endif /* RCC_CR_PLL2ON */ 00136 00137 #if defined(RCC_CR_PLL3ON) 00138 /* Reset PLL3ON bit */ 00139 CLEAR_BIT(RCC->CR, RCC_CR_PLL3ON); 00140 #endif /* RCC_CR_PLL3ON */ 00141 00142 /* Set HSITRIM bits to the reset value */ 00143 LL_RCC_HSI_SetCalibTrimming(0x10U); 00144 00145 #if defined(RCC_CFGR2_PREDIV1) 00146 /* Reset CFGR2 register */ 00147 LL_RCC_WriteReg(CFGR2, 0x00000000U); 00148 #endif /* RCC_CFGR2_PREDIV1 */ 00149 00150 /* Disable all interrupts */ 00151 LL_RCC_WriteReg(CIR, 0x00000000U); 00152 00153 /* Clear reset flags */ 00154 LL_RCC_ClearResetFlags(); 00155 00156 return SUCCESS; 00157 } 00158 00159 /** 00160 * @} 00161 */ 00162 00163 /** @addtogroup RCC_LL_EF_Get_Freq 00164 * @brief Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks 00165 * and different peripheral clocks available on the device. 00166 * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(**) 00167 * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***) 00168 * @note If SYSCLK source is PLL, function returns values based on 00169 * HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors. 00170 * @note (**) HSI_VALUE is a defined constant but the real value may vary 00171 * depending on the variations in voltage and temperature. 00172 * @note (***) HSE_VALUE is a defined constant, user has to ensure that 00173 * HSE_VALUE is same as the real frequency of the crystal used. 00174 * Otherwise, this function may have wrong result. 00175 * @note The result of this function could be incorrect when using fractional 00176 * value for HSE crystal. 00177 * @note This function can be used by the user application to compute the 00178 * baud-rate for the communication peripherals or configure other parameters. 00179 * @{ 00180 */ 00181 00182 /** 00183 * @brief Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks 00184 * @note Each time SYSCLK, HCLK, PCLK1 and/or PCLK2 clock changes, this function 00185 * must be called to update structure fields. Otherwise, any 00186 * configuration based on this function will be incorrect. 00187 * @param RCC_Clocks pointer to a @ref LL_RCC_ClocksTypeDef structure which will hold the clocks frequencies 00188 * @retval None 00189 */ 00190 void LL_RCC_GetSystemClocksFreq(LL_RCC_ClocksTypeDef *RCC_Clocks) 00191 { 00192 /* Get SYSCLK frequency */ 00193 RCC_Clocks->SYSCLK_Frequency = RCC_GetSystemClockFreq(); 00194 00195 /* HCLK clock frequency */ 00196 RCC_Clocks->HCLK_Frequency = RCC_GetHCLKClockFreq(RCC_Clocks->SYSCLK_Frequency); 00197 00198 /* PCLK1 clock frequency */ 00199 RCC_Clocks->PCLK1_Frequency = RCC_GetPCLK1ClockFreq(RCC_Clocks->HCLK_Frequency); 00200 00201 /* PCLK2 clock frequency */ 00202 RCC_Clocks->PCLK2_Frequency = RCC_GetPCLK2ClockFreq(RCC_Clocks->HCLK_Frequency); 00203 } 00204 00205 #if defined(RCC_CFGR2_I2S2SRC) 00206 /** 00207 * @brief Return I2Sx clock frequency 00208 * @param I2SxSource This parameter can be one of the following values: 00209 * @arg @ref LL_RCC_I2S2_CLKSOURCE 00210 * @arg @ref LL_RCC_I2S3_CLKSOURCE 00211 * @retval I2S clock frequency (in Hz) 00212 */ 00213 uint32_t LL_RCC_GetI2SClockFreq(uint32_t I2SxSource) 00214 { 00215 uint32_t i2s_frequency = LL_RCC_PERIPH_FREQUENCY_NO; 00216 00217 /* Check parameter */ 00218 assert_param(IS_LL_RCC_I2S_CLKSOURCE(I2SxSource)); 00219 00220 /* I2S1CLK clock frequency */ 00221 switch (LL_RCC_GetI2SClockSource(I2SxSource)) 00222 { 00223 case LL_RCC_I2S2_CLKSOURCE_SYSCLK: /*!< System clock selected as I2S clock source */ 00224 case LL_RCC_I2S3_CLKSOURCE_SYSCLK: 00225 i2s_frequency = RCC_GetSystemClockFreq(); 00226 break; 00227 00228 case LL_RCC_I2S2_CLKSOURCE_PLLI2S_VCO: /*!< PLLI2S oscillator clock selected as I2S clock source */ 00229 case LL_RCC_I2S3_CLKSOURCE_PLLI2S_VCO: 00230 default: 00231 i2s_frequency = RCC_PLLI2S_GetFreqDomain_I2S() * 2U; 00232 break; 00233 } 00234 00235 return i2s_frequency; 00236 } 00237 #endif /* RCC_CFGR2_I2S2SRC */ 00238 00239 #if defined(USB) || defined(USB_OTG_FS) 00240 /** 00241 * @brief Return USBx clock frequency 00242 * @param USBxSource This parameter can be one of the following values: 00243 * @arg @ref LL_RCC_USB_CLKSOURCE 00244 * @retval USB clock frequency (in Hz) 00245 * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI), HSE or PLL is not ready 00246 */ 00247 uint32_t LL_RCC_GetUSBClockFreq(uint32_t USBxSource) 00248 { 00249 uint32_t usb_frequency = LL_RCC_PERIPH_FREQUENCY_NO; 00250 00251 /* Check parameter */ 00252 assert_param(IS_LL_RCC_USB_CLKSOURCE(USBxSource)); 00253 00254 /* USBCLK clock frequency */ 00255 switch (LL_RCC_GetUSBClockSource(USBxSource)) 00256 { 00257 #if defined(RCC_CFGR_USBPRE) 00258 case LL_RCC_USB_CLKSOURCE_PLL: /* PLL clock used as USB clock source */ 00259 if (LL_RCC_PLL_IsReady()) 00260 { 00261 usb_frequency = RCC_PLL_GetFreqDomain_SYS(); 00262 } 00263 break; 00264 00265 case LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5: /* PLL clock divided by 1.5 used as USB clock source */ 00266 default: 00267 if (LL_RCC_PLL_IsReady()) 00268 { 00269 usb_frequency = (RCC_PLL_GetFreqDomain_SYS() * 3U) / 2U; 00270 } 00271 break; 00272 #endif /* RCC_CFGR_USBPRE */ 00273 #if defined(RCC_CFGR_OTGFSPRE) 00274 /* USBCLK = PLLVCO/2 00275 = (2 x PLLCLK) / 2 00276 = PLLCLK */ 00277 case LL_RCC_USB_CLKSOURCE_PLL_DIV_2: /* PLL clock used as USB clock source */ 00278 if (LL_RCC_PLL_IsReady()) 00279 { 00280 usb_frequency = RCC_PLL_GetFreqDomain_SYS(); 00281 } 00282 break; 00283 00284 /* USBCLK = PLLVCO/3 00285 = (2 x PLLCLK) / 3 */ 00286 case LL_RCC_USB_CLKSOURCE_PLL_DIV_3: /* PLL clock divided by 3 used as USB clock source */ 00287 default: 00288 if (LL_RCC_PLL_IsReady()) 00289 { 00290 usb_frequency = (RCC_PLL_GetFreqDomain_SYS() * 2U) / 3U; 00291 } 00292 break; 00293 #endif /* RCC_CFGR_OTGFSPRE */ 00294 } 00295 00296 return usb_frequency; 00297 } 00298 #endif /* USB */ 00299 00300 /** 00301 * @brief Return ADCx clock frequency 00302 * @param ADCxSource This parameter can be one of the following values: 00303 * @arg @ref LL_RCC_ADC_CLKSOURCE 00304 * @retval ADC clock frequency (in Hz) 00305 */ 00306 uint32_t LL_RCC_GetADCClockFreq(uint32_t ADCxSource) 00307 { 00308 uint32_t adc_prescaler = 0U; 00309 uint32_t adc_frequency = 0U; 00310 00311 /* Check parameter */ 00312 assert_param(IS_LL_RCC_ADC_CLKSOURCE(ADCxSource)); 00313 00314 /* Get ADC prescaler */ 00315 adc_prescaler = LL_RCC_GetADCClockSource(ADCxSource); 00316 00317 /* ADC frequency = PCLK2 frequency / ADC prescaler (2, 4, 6 or 8) */ 00318 adc_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq())) 00319 / (((adc_prescaler >> POSITION_VAL(ADCxSource)) + 1U) * 2U); 00320 00321 return adc_frequency; 00322 } 00323 00324 /** 00325 * @} 00326 */ 00327 00328 /** 00329 * @} 00330 */ 00331 00332 /** @addtogroup RCC_LL_Private_Functions 00333 * @{ 00334 */ 00335 00336 /** 00337 * @brief Return SYSTEM clock frequency 00338 * @retval SYSTEM clock frequency (in Hz) 00339 */ 00340 uint32_t RCC_GetSystemClockFreq(void) 00341 { 00342 uint32_t frequency = 0U; 00343 00344 /* Get SYSCLK source -------------------------------------------------------*/ 00345 switch (LL_RCC_GetSysClkSource()) 00346 { 00347 case LL_RCC_SYS_CLKSOURCE_STATUS_HSI: /* HSI used as system clock source */ 00348 frequency = HSI_VALUE; 00349 break; 00350 00351 case LL_RCC_SYS_CLKSOURCE_STATUS_HSE: /* HSE used as system clock source */ 00352 frequency = HSE_VALUE; 00353 break; 00354 00355 case LL_RCC_SYS_CLKSOURCE_STATUS_PLL: /* PLL used as system clock source */ 00356 frequency = RCC_PLL_GetFreqDomain_SYS(); 00357 break; 00358 00359 default: 00360 frequency = HSI_VALUE; 00361 break; 00362 } 00363 00364 return frequency; 00365 } 00366 00367 /** 00368 * @brief Return HCLK clock frequency 00369 * @param SYSCLK_Frequency SYSCLK clock frequency 00370 * @retval HCLK clock frequency (in Hz) 00371 */ 00372 uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency) 00373 { 00374 /* HCLK clock frequency */ 00375 return __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, LL_RCC_GetAHBPrescaler()); 00376 } 00377 00378 /** 00379 * @brief Return PCLK1 clock frequency 00380 * @param HCLK_Frequency HCLK clock frequency 00381 * @retval PCLK1 clock frequency (in Hz) 00382 */ 00383 uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency) 00384 { 00385 /* PCLK1 clock frequency */ 00386 return __LL_RCC_CALC_PCLK1_FREQ(HCLK_Frequency, LL_RCC_GetAPB1Prescaler()); 00387 } 00388 00389 /** 00390 * @brief Return PCLK2 clock frequency 00391 * @param HCLK_Frequency HCLK clock frequency 00392 * @retval PCLK2 clock frequency (in Hz) 00393 */ 00394 uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency) 00395 { 00396 /* PCLK2 clock frequency */ 00397 return __LL_RCC_CALC_PCLK2_FREQ(HCLK_Frequency, LL_RCC_GetAPB2Prescaler()); 00398 } 00399 00400 /** 00401 * @brief Return PLL clock frequency used for system domain 00402 * @retval PLL clock frequency (in Hz) 00403 */ 00404 uint32_t RCC_PLL_GetFreqDomain_SYS(void) 00405 { 00406 uint32_t pllinputfreq = 0U, pllsource = 0U; 00407 00408 /* PLL_VCO = (HSE_VALUE, HSI_VALUE or PLL2 / PLL Predivider) * PLL Multiplicator */ 00409 00410 /* Get PLL source */ 00411 pllsource = LL_RCC_PLL_GetMainSource(); 00412 00413 switch (pllsource) 00414 { 00415 case LL_RCC_PLLSOURCE_HSI_DIV_2: /* HSI used as PLL clock source */ 00416 pllinputfreq = HSI_VALUE / 2U; 00417 break; 00418 00419 case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */ 00420 pllinputfreq = HSE_VALUE / (LL_RCC_PLL_GetPrediv() + 1U); 00421 break; 00422 00423 #if defined(RCC_PLL2_SUPPORT) 00424 case LL_RCC_PLLSOURCE_PLL2: /* PLL2 used as PLL clock source */ 00425 pllinputfreq = RCC_PLL2_GetFreqClockFreq() / (LL_RCC_PLL_GetPrediv() + 1U); 00426 break; 00427 #endif /* RCC_PLL2_SUPPORT */ 00428 00429 default: 00430 pllinputfreq = HSI_VALUE / 2U; 00431 break; 00432 } 00433 return __LL_RCC_CALC_PLLCLK_FREQ(pllinputfreq, LL_RCC_PLL_GetMultiplicator()); 00434 } 00435 00436 #if defined(RCC_PLL2_SUPPORT) 00437 /** 00438 * @brief Return PLL clock frequency used for system domain 00439 * @retval PLL clock frequency (in Hz) 00440 */ 00441 uint32_t RCC_PLL2_GetFreqClockFreq(void) 00442 { 00443 return __LL_RCC_CALC_PLL2CLK_FREQ(HSE_VALUE, LL_RCC_PLL2_GetMultiplicator(), LL_RCC_HSE_GetPrediv2()); 00444 } 00445 #endif /* RCC_PLL2_SUPPORT */ 00446 00447 #if defined(RCC_PLLI2S_SUPPORT) 00448 /** 00449 * @brief Return PLL clock frequency used for system domain 00450 * @retval PLL clock frequency (in Hz) 00451 */ 00452 uint32_t RCC_PLLI2S_GetFreqDomain_I2S(void) 00453 { 00454 return __LL_RCC_CALC_PLLI2SCLK_FREQ(HSE_VALUE, LL_RCC_PLLI2S_GetMultiplicator(), LL_RCC_HSE_GetPrediv2()); 00455 } 00456 #endif /* RCC_PLLI2S_SUPPORT */ 00457 00458 /** 00459 * @} 00460 */ 00461 00462 /** 00463 * @} 00464 */ 00465 00466 #endif /* defined(RCC) */ 00467 00468 /** 00469 * @} 00470 */ 00471 00472 #endif /* USE_FULL_LL_DRIVER */ 00473 00474 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/