Projet voilier 4IRA1 Arnaud Vergnet Marino Benassai Bastien Picco Yohan Simard
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f1xx_ll_rcc.c 14KB

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