stm32f1xx_ll_rcc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_ll_rcc.c
  4. * @author MCD Application Team
  5. * @brief RCC LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file in
  13. * the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. ******************************************************************************
  16. */
  17. #if defined(USE_FULL_LL_DRIVER)
  18. /* Includes ------------------------------------------------------------------*/
  19. #include "stm32f1xx_ll_rcc.h"
  20. #ifdef USE_FULL_ASSERT
  21. #include "stm32_assert.h"
  22. #else
  23. #define assert_param(expr) ((void)0U)
  24. #endif /* USE_FULL_ASSERT */
  25. /** @addtogroup STM32F1xx_LL_Driver
  26. * @{
  27. */
  28. #if defined(RCC)
  29. /** @defgroup RCC_LL RCC
  30. * @{
  31. */
  32. /* Private types -------------------------------------------------------------*/
  33. /* Private variables ---------------------------------------------------------*/
  34. /* Private constants ---------------------------------------------------------*/
  35. /* Private macros ------------------------------------------------------------*/
  36. /** @addtogroup RCC_LL_Private_Macros
  37. * @{
  38. */
  39. #if defined(RCC_PLLI2S_SUPPORT)
  40. #define IS_LL_RCC_I2S_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_I2S2_CLKSOURCE) \
  41. || ((__VALUE__) == LL_RCC_I2S3_CLKSOURCE))
  42. #endif /* RCC_PLLI2S_SUPPORT */
  43. #if defined(USB) || defined(USB_OTG_FS)
  44. #define IS_LL_RCC_USB_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USB_CLKSOURCE))
  45. #endif /* USB */
  46. #define IS_LL_RCC_ADC_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_ADC_CLKSOURCE))
  47. /**
  48. * @}
  49. */
  50. /* Private function prototypes -----------------------------------------------*/
  51. /** @defgroup RCC_LL_Private_Functions RCC Private functions
  52. * @{
  53. */
  54. uint32_t RCC_GetSystemClockFreq(void);
  55. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency);
  56. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency);
  57. uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency);
  58. uint32_t RCC_PLL_GetFreqDomain_SYS(void);
  59. #if defined(RCC_PLLI2S_SUPPORT)
  60. uint32_t RCC_PLLI2S_GetFreqDomain_I2S(void);
  61. #endif /* RCC_PLLI2S_SUPPORT */
  62. #if defined(RCC_PLL2_SUPPORT)
  63. uint32_t RCC_PLL2_GetFreqClockFreq(void);
  64. #endif /* RCC_PLL2_SUPPORT */
  65. /**
  66. * @}
  67. */
  68. /* Exported functions --------------------------------------------------------*/
  69. /** @addtogroup RCC_LL_Exported_Functions
  70. * @{
  71. */
  72. /** @addtogroup RCC_LL_EF_Init
  73. * @{
  74. */
  75. /**
  76. * @brief Reset the RCC clock configuration to the default reset state.
  77. * @note The default reset state of the clock configuration is given below:
  78. * - HSI ON and used as system clock source
  79. * - HSE PLL, PLL2 & PLL3 are OFF
  80. * - AHB, APB1 and APB2 prescaler set to 1.
  81. * - CSS, MCO OFF
  82. * - All interrupts disabled
  83. * @note This function doesn't modify the configuration of the
  84. * - Peripheral clocks
  85. * - LSI, LSE and RTC clocks
  86. * @retval An ErrorStatus enumeration value:
  87. * - SUCCESS: RCC registers are de-initialized
  88. * - ERROR: not applicable
  89. */
  90. ErrorStatus LL_RCC_DeInit(void)
  91. {
  92. /* Set HSION bit */
  93. LL_RCC_HSI_Enable();
  94. /* Wait for HSI READY bit */
  95. while (LL_RCC_HSI_IsReady() != 1U)
  96. {}
  97. /* Configure HSI as system clock source */
  98. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSI);
  99. /* Wait till clock switch is ready */
  100. while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSI)
  101. {}
  102. /* Reset PLLON bit */
  103. CLEAR_BIT(RCC->CR, RCC_CR_PLLON);
  104. /* Wait for PLL READY bit to be reset */
  105. while (LL_RCC_PLL_IsReady() != 0U)
  106. {}
  107. /* Reset CFGR register */
  108. LL_RCC_WriteReg(CFGR, 0x00000000U);
  109. /* Reset HSEON, HSEBYP & CSSON bits */
  110. CLEAR_BIT(RCC->CR, (RCC_CR_CSSON | RCC_CR_HSEON | RCC_CR_HSEBYP));
  111. #if defined(RCC_CR_PLL2ON)
  112. /* Reset PLL2ON bit */
  113. CLEAR_BIT(RCC->CR, RCC_CR_PLL2ON);
  114. #endif /* RCC_CR_PLL2ON */
  115. #if defined(RCC_CR_PLL3ON)
  116. /* Reset PLL3ON bit */
  117. CLEAR_BIT(RCC->CR, RCC_CR_PLL3ON);
  118. #endif /* RCC_CR_PLL3ON */
  119. /* Set HSITRIM bits to the reset value */
  120. LL_RCC_HSI_SetCalibTrimming(0x10U);
  121. #if defined(RCC_CFGR2_PREDIV1)
  122. /* Reset CFGR2 register */
  123. LL_RCC_WriteReg(CFGR2, 0x00000000U);
  124. #endif /* RCC_CFGR2_PREDIV1 */
  125. /* Disable all interrupts */
  126. LL_RCC_WriteReg(CIR, 0x00000000U);
  127. /* Clear reset flags */
  128. LL_RCC_ClearResetFlags();
  129. return SUCCESS;
  130. }
  131. /**
  132. * @}
  133. */
  134. /** @addtogroup RCC_LL_EF_Get_Freq
  135. * @brief Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks
  136. * and different peripheral clocks available on the device.
  137. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(**)
  138. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
  139. * @note If SYSCLK source is PLL, function returns values based on
  140. * HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
  141. * @note (**) HSI_VALUE is a defined constant but the real value may vary
  142. * depending on the variations in voltage and temperature.
  143. * @note (***) HSE_VALUE is a defined constant, user has to ensure that
  144. * HSE_VALUE is same as the real frequency of the crystal used.
  145. * Otherwise, this function may have wrong result.
  146. * @note The result of this function could be incorrect when using fractional
  147. * value for HSE crystal.
  148. * @note This function can be used by the user application to compute the
  149. * baud-rate for the communication peripherals or configure other parameters.
  150. * @{
  151. */
  152. /**
  153. * @brief Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks
  154. * @note Each time SYSCLK, HCLK, PCLK1 and/or PCLK2 clock changes, this function
  155. * must be called to update structure fields. Otherwise, any
  156. * configuration based on this function will be incorrect.
  157. * @param RCC_Clocks pointer to a @ref LL_RCC_ClocksTypeDef structure which will hold the clocks frequencies
  158. * @retval None
  159. */
  160. void LL_RCC_GetSystemClocksFreq(LL_RCC_ClocksTypeDef *RCC_Clocks)
  161. {
  162. /* Get SYSCLK frequency */
  163. RCC_Clocks->SYSCLK_Frequency = RCC_GetSystemClockFreq();
  164. /* HCLK clock frequency */
  165. RCC_Clocks->HCLK_Frequency = RCC_GetHCLKClockFreq(RCC_Clocks->SYSCLK_Frequency);
  166. /* PCLK1 clock frequency */
  167. RCC_Clocks->PCLK1_Frequency = RCC_GetPCLK1ClockFreq(RCC_Clocks->HCLK_Frequency);
  168. /* PCLK2 clock frequency */
  169. RCC_Clocks->PCLK2_Frequency = RCC_GetPCLK2ClockFreq(RCC_Clocks->HCLK_Frequency);
  170. }
  171. #if defined(RCC_CFGR2_I2S2SRC)
  172. /**
  173. * @brief Return I2Sx clock frequency
  174. * @param I2SxSource This parameter can be one of the following values:
  175. * @arg @ref LL_RCC_I2S2_CLKSOURCE
  176. * @arg @ref LL_RCC_I2S3_CLKSOURCE
  177. * @retval I2S clock frequency (in Hz)
  178. */
  179. uint32_t LL_RCC_GetI2SClockFreq(uint32_t I2SxSource)
  180. {
  181. uint32_t i2s_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  182. /* Check parameter */
  183. assert_param(IS_LL_RCC_I2S_CLKSOURCE(I2SxSource));
  184. /* I2S1CLK clock frequency */
  185. switch (LL_RCC_GetI2SClockSource(I2SxSource))
  186. {
  187. case LL_RCC_I2S2_CLKSOURCE_SYSCLK: /*!< System clock selected as I2S clock source */
  188. case LL_RCC_I2S3_CLKSOURCE_SYSCLK:
  189. i2s_frequency = RCC_GetSystemClockFreq();
  190. break;
  191. case LL_RCC_I2S2_CLKSOURCE_PLLI2S_VCO: /*!< PLLI2S oscillator clock selected as I2S clock source */
  192. case LL_RCC_I2S3_CLKSOURCE_PLLI2S_VCO:
  193. default:
  194. i2s_frequency = RCC_PLLI2S_GetFreqDomain_I2S() * 2U;
  195. break;
  196. }
  197. return i2s_frequency;
  198. }
  199. #endif /* RCC_CFGR2_I2S2SRC */
  200. #if defined(USB) || defined(USB_OTG_FS)
  201. /**
  202. * @brief Return USBx clock frequency
  203. * @param USBxSource This parameter can be one of the following values:
  204. * @arg @ref LL_RCC_USB_CLKSOURCE
  205. * @retval USB clock frequency (in Hz)
  206. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI), HSE or PLL is not ready
  207. */
  208. uint32_t LL_RCC_GetUSBClockFreq(uint32_t USBxSource)
  209. {
  210. uint32_t usb_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  211. /* Check parameter */
  212. assert_param(IS_LL_RCC_USB_CLKSOURCE(USBxSource));
  213. /* USBCLK clock frequency */
  214. switch (LL_RCC_GetUSBClockSource(USBxSource))
  215. {
  216. #if defined(RCC_CFGR_USBPRE)
  217. case LL_RCC_USB_CLKSOURCE_PLL: /* PLL clock used as USB clock source */
  218. if (LL_RCC_PLL_IsReady())
  219. {
  220. usb_frequency = RCC_PLL_GetFreqDomain_SYS();
  221. }
  222. break;
  223. case LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5: /* PLL clock divided by 1.5 used as USB clock source */
  224. default:
  225. if (LL_RCC_PLL_IsReady())
  226. {
  227. usb_frequency = (RCC_PLL_GetFreqDomain_SYS() * 3U) / 2U;
  228. }
  229. break;
  230. #endif /* RCC_CFGR_USBPRE */
  231. #if defined(RCC_CFGR_OTGFSPRE)
  232. /* USBCLK = PLLVCO/2
  233. = (2 x PLLCLK) / 2
  234. = PLLCLK */
  235. case LL_RCC_USB_CLKSOURCE_PLL_DIV_2: /* PLL clock used as USB clock source */
  236. if (LL_RCC_PLL_IsReady())
  237. {
  238. usb_frequency = RCC_PLL_GetFreqDomain_SYS();
  239. }
  240. break;
  241. /* USBCLK = PLLVCO/3
  242. = (2 x PLLCLK) / 3 */
  243. case LL_RCC_USB_CLKSOURCE_PLL_DIV_3: /* PLL clock divided by 3 used as USB clock source */
  244. default:
  245. if (LL_RCC_PLL_IsReady())
  246. {
  247. usb_frequency = (RCC_PLL_GetFreqDomain_SYS() * 2U) / 3U;
  248. }
  249. break;
  250. #endif /* RCC_CFGR_OTGFSPRE */
  251. }
  252. return usb_frequency;
  253. }
  254. #endif /* USB */
  255. /**
  256. * @brief Return ADCx clock frequency
  257. * @param ADCxSource This parameter can be one of the following values:
  258. * @arg @ref LL_RCC_ADC_CLKSOURCE
  259. * @retval ADC clock frequency (in Hz)
  260. */
  261. uint32_t LL_RCC_GetADCClockFreq(uint32_t ADCxSource)
  262. {
  263. uint32_t adc_prescaler = 0U;
  264. uint32_t adc_frequency = 0U;
  265. /* Check parameter */
  266. assert_param(IS_LL_RCC_ADC_CLKSOURCE(ADCxSource));
  267. /* Get ADC prescaler */
  268. adc_prescaler = LL_RCC_GetADCClockSource(ADCxSource);
  269. /* ADC frequency = PCLK2 frequency / ADC prescaler (2, 4, 6 or 8) */
  270. adc_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()))
  271. / (((adc_prescaler >> POSITION_VAL(ADCxSource)) + 1U) * 2U);
  272. return adc_frequency;
  273. }
  274. /**
  275. * @}
  276. */
  277. /**
  278. * @}
  279. */
  280. /** @addtogroup RCC_LL_Private_Functions
  281. * @{
  282. */
  283. /**
  284. * @brief Return SYSTEM clock frequency
  285. * @retval SYSTEM clock frequency (in Hz)
  286. */
  287. uint32_t RCC_GetSystemClockFreq(void)
  288. {
  289. uint32_t frequency = 0U;
  290. /* Get SYSCLK source -------------------------------------------------------*/
  291. switch (LL_RCC_GetSysClkSource())
  292. {
  293. case LL_RCC_SYS_CLKSOURCE_STATUS_HSI: /* HSI used as system clock source */
  294. frequency = HSI_VALUE;
  295. break;
  296. case LL_RCC_SYS_CLKSOURCE_STATUS_HSE: /* HSE used as system clock source */
  297. frequency = HSE_VALUE;
  298. break;
  299. case LL_RCC_SYS_CLKSOURCE_STATUS_PLL: /* PLL used as system clock source */
  300. frequency = RCC_PLL_GetFreqDomain_SYS();
  301. break;
  302. default:
  303. frequency = HSI_VALUE;
  304. break;
  305. }
  306. return frequency;
  307. }
  308. /**
  309. * @brief Return HCLK clock frequency
  310. * @param SYSCLK_Frequency SYSCLK clock frequency
  311. * @retval HCLK clock frequency (in Hz)
  312. */
  313. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency)
  314. {
  315. /* HCLK clock frequency */
  316. return __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, LL_RCC_GetAHBPrescaler());
  317. }
  318. /**
  319. * @brief Return PCLK1 clock frequency
  320. * @param HCLK_Frequency HCLK clock frequency
  321. * @retval PCLK1 clock frequency (in Hz)
  322. */
  323. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency)
  324. {
  325. /* PCLK1 clock frequency */
  326. return __LL_RCC_CALC_PCLK1_FREQ(HCLK_Frequency, LL_RCC_GetAPB1Prescaler());
  327. }
  328. /**
  329. * @brief Return PCLK2 clock frequency
  330. * @param HCLK_Frequency HCLK clock frequency
  331. * @retval PCLK2 clock frequency (in Hz)
  332. */
  333. uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency)
  334. {
  335. /* PCLK2 clock frequency */
  336. return __LL_RCC_CALC_PCLK2_FREQ(HCLK_Frequency, LL_RCC_GetAPB2Prescaler());
  337. }
  338. /**
  339. * @brief Return PLL clock frequency used for system domain
  340. * @retval PLL clock frequency (in Hz)
  341. */
  342. uint32_t RCC_PLL_GetFreqDomain_SYS(void)
  343. {
  344. uint32_t pllinputfreq = 0U, pllsource = 0U;
  345. /* PLL_VCO = (HSE_VALUE, HSI_VALUE or PLL2 / PLL Predivider) * PLL Multiplicator */
  346. /* Get PLL source */
  347. pllsource = LL_RCC_PLL_GetMainSource();
  348. switch (pllsource)
  349. {
  350. case LL_RCC_PLLSOURCE_HSI_DIV_2: /* HSI used as PLL clock source */
  351. pllinputfreq = HSI_VALUE / 2U;
  352. break;
  353. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  354. pllinputfreq = HSE_VALUE / (LL_RCC_PLL_GetPrediv() + 1U);
  355. break;
  356. #if defined(RCC_PLL2_SUPPORT)
  357. case LL_RCC_PLLSOURCE_PLL2: /* PLL2 used as PLL clock source */
  358. pllinputfreq = RCC_PLL2_GetFreqClockFreq() / (LL_RCC_PLL_GetPrediv() + 1U);
  359. break;
  360. #endif /* RCC_PLL2_SUPPORT */
  361. default:
  362. pllinputfreq = HSI_VALUE / 2U;
  363. break;
  364. }
  365. return __LL_RCC_CALC_PLLCLK_FREQ(pllinputfreq, LL_RCC_PLL_GetMultiplicator());
  366. }
  367. #if defined(RCC_PLL2_SUPPORT)
  368. /**
  369. * @brief Return PLL clock frequency used for system domain
  370. * @retval PLL clock frequency (in Hz)
  371. */
  372. uint32_t RCC_PLL2_GetFreqClockFreq(void)
  373. {
  374. return __LL_RCC_CALC_PLL2CLK_FREQ(HSE_VALUE, LL_RCC_PLL2_GetMultiplicator(), LL_RCC_HSE_GetPrediv2());
  375. }
  376. #endif /* RCC_PLL2_SUPPORT */
  377. #if defined(RCC_PLLI2S_SUPPORT)
  378. /**
  379. * @brief Return PLL clock frequency used for system domain
  380. * @retval PLL clock frequency (in Hz)
  381. */
  382. uint32_t RCC_PLLI2S_GetFreqDomain_I2S(void)
  383. {
  384. return __LL_RCC_CALC_PLLI2SCLK_FREQ(HSE_VALUE, LL_RCC_PLLI2S_GetMultiplicator(), LL_RCC_HSE_GetPrediv2());
  385. }
  386. #endif /* RCC_PLLI2S_SUPPORT */
  387. /**
  388. * @}
  389. */
  390. /**
  391. * @}
  392. */
  393. #endif /* defined(RCC) */
  394. /**
  395. * @}
  396. */
  397. #endif /* USE_FULL_LL_DRIVER */