stm32f1xx_ll_rcc.c 15 KB

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