stm32f1xx_ll_rcc.c 16 KB

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