stm32f1xx_hal_rcc_ex.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_rcc_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended RCC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities RCC extension peripheral:
  8. * + Extended Peripheral Control functions
  9. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  14. * All rights reserved.</center></h2>
  15. *
  16. * This software component is licensed by ST under BSD 3-Clause license,
  17. * the "License"; You may not use this file except in compliance with the
  18. * License. You may obtain a copy of the License at:
  19. * opensource.org/licenses/BSD-3-Clause
  20. *
  21. ******************************************************************************
  22. */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32f1xx_hal.h"
  25. /** @addtogroup STM32F1xx_HAL_Driver
  26. * @{
  27. */
  28. #ifdef HAL_RCC_MODULE_ENABLED
  29. /** @defgroup RCCEx RCCEx
  30. * @brief RCC Extension HAL module driver.
  31. * @{
  32. */
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private define ------------------------------------------------------------*/
  35. /** @defgroup RCCEx_Private_Constants RCCEx Private Constants
  36. * @{
  37. */
  38. /**
  39. * @}
  40. */
  41. /* Private macro -------------------------------------------------------------*/
  42. /** @defgroup RCCEx_Private_Macros RCCEx Private Macros
  43. * @{
  44. */
  45. /**
  46. * @}
  47. */
  48. /* Private variables ---------------------------------------------------------*/
  49. /* Private function prototypes -----------------------------------------------*/
  50. /* Private functions ---------------------------------------------------------*/
  51. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  52. * @{
  53. */
  54. /** @defgroup RCCEx_Exported_Functions_Group1 Peripheral Control functions
  55. * @brief Extended Peripheral Control functions
  56. *
  57. @verbatim
  58. ===============================================================================
  59. ##### Extended Peripheral Control functions #####
  60. ===============================================================================
  61. [..]
  62. This subsection provides a set of functions allowing to control the RCC Clocks
  63. frequencies.
  64. [..]
  65. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  66. select the RTC clock source; in this case the Backup domain will be reset in
  67. order to modify the RTC Clock source, as consequence RTC registers (including
  68. the backup registers) are set to their reset values.
  69. @endverbatim
  70. * @{
  71. */
  72. /**
  73. * @brief Initializes the RCC extended peripherals clocks according to the specified parameters in the
  74. * RCC_PeriphCLKInitTypeDef.
  75. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  76. * contains the configuration information for the Extended Peripherals clocks(RTC clock).
  77. *
  78. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  79. * the RTC clock source; in this case the Backup domain will be reset in
  80. * order to modify the RTC Clock source, as consequence RTC registers (including
  81. * the backup registers) are set to their reset values.
  82. *
  83. * @note In case of STM32F105xC or STM32F107xC devices, PLLI2S will be enabled if requested on
  84. * one of 2 I2S interfaces. When PLLI2S is enabled, you need to call HAL_RCCEx_DisablePLLI2S to
  85. * manually disable it.
  86. *
  87. * @retval HAL status
  88. */
  89. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  90. {
  91. uint32_t tickstart = 0U, temp_reg = 0U;
  92. #if defined(STM32F105xC) || defined(STM32F107xC)
  93. uint32_t pllactive = 0U;
  94. #endif /* STM32F105xC || STM32F107xC */
  95. /* Check the parameters */
  96. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  97. /*------------------------------- RTC/LCD Configuration ------------------------*/
  98. if ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC))
  99. {
  100. FlagStatus pwrclkchanged = RESET;
  101. /* check for RTC Parameters used to output RTCCLK */
  102. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  103. /* As soon as function is called to change RTC clock source, activation of the
  104. power domain is done. */
  105. /* Requires to enable write access to Backup Domain of necessary */
  106. if (__HAL_RCC_PWR_IS_CLK_DISABLED())
  107. {
  108. __HAL_RCC_PWR_CLK_ENABLE();
  109. pwrclkchanged = SET;
  110. }
  111. if (HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  112. {
  113. /* Enable write access to Backup domain */
  114. SET_BIT(PWR->CR, PWR_CR_DBP);
  115. /* Wait for Backup domain Write protection disable */
  116. tickstart = HAL_GetTick();
  117. while (HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  118. {
  119. if ((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  120. {
  121. return HAL_TIMEOUT;
  122. }
  123. }
  124. }
  125. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  126. temp_reg = (RCC->BDCR & RCC_BDCR_RTCSEL);
  127. if ((temp_reg != 0x00000000U) && (temp_reg != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  128. {
  129. /* Store the content of BDCR register before the reset of Backup Domain */
  130. temp_reg = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  131. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  132. __HAL_RCC_BACKUPRESET_FORCE();
  133. __HAL_RCC_BACKUPRESET_RELEASE();
  134. /* Restore the Content of BDCR register */
  135. RCC->BDCR = temp_reg;
  136. /* Wait for LSERDY if LSE was enabled */
  137. if (HAL_IS_BIT_SET(temp_reg, RCC_BDCR_LSEON))
  138. {
  139. /* Get Start Tick */
  140. tickstart = HAL_GetTick();
  141. /* Wait till LSE is ready */
  142. while (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  143. {
  144. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  145. {
  146. return HAL_TIMEOUT;
  147. }
  148. }
  149. }
  150. }
  151. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  152. /* Require to disable power clock if necessary */
  153. if (pwrclkchanged == SET)
  154. {
  155. __HAL_RCC_PWR_CLK_DISABLE();
  156. }
  157. }
  158. /*------------------------------ ADC clock Configuration ------------------*/
  159. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC) == RCC_PERIPHCLK_ADC)
  160. {
  161. /* Check the parameters */
  162. assert_param(IS_RCC_ADCPLLCLK_DIV(PeriphClkInit->AdcClockSelection));
  163. /* Configure the ADC clock source */
  164. __HAL_RCC_ADC_CONFIG(PeriphClkInit->AdcClockSelection);
  165. }
  166. #if defined(STM32F105xC) || defined(STM32F107xC)
  167. /*------------------------------ I2S2 Configuration ------------------------*/
  168. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S2) == RCC_PERIPHCLK_I2S2)
  169. {
  170. /* Check the parameters */
  171. assert_param(IS_RCC_I2S2CLKSOURCE(PeriphClkInit->I2s2ClockSelection));
  172. /* Configure the I2S2 clock source */
  173. __HAL_RCC_I2S2_CONFIG(PeriphClkInit->I2s2ClockSelection);
  174. }
  175. /*------------------------------ I2S3 Configuration ------------------------*/
  176. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S3) == RCC_PERIPHCLK_I2S3)
  177. {
  178. /* Check the parameters */
  179. assert_param(IS_RCC_I2S3CLKSOURCE(PeriphClkInit->I2s3ClockSelection));
  180. /* Configure the I2S3 clock source */
  181. __HAL_RCC_I2S3_CONFIG(PeriphClkInit->I2s3ClockSelection);
  182. }
  183. /*------------------------------ PLL I2S Configuration ----------------------*/
  184. /* Check that PLLI2S need to be enabled */
  185. if (HAL_IS_BIT_SET(RCC->CFGR2, RCC_CFGR2_I2S2SRC) || HAL_IS_BIT_SET(RCC->CFGR2, RCC_CFGR2_I2S3SRC))
  186. {
  187. /* Update flag to indicate that PLL I2S should be active */
  188. pllactive = 1;
  189. }
  190. /* Check if PLL I2S need to be enabled */
  191. if (pllactive == 1)
  192. {
  193. /* Enable PLL I2S only if not active */
  194. if (HAL_IS_BIT_CLR(RCC->CR, RCC_CR_PLL3ON))
  195. {
  196. /* Check the parameters */
  197. assert_param(IS_RCC_PLLI2S_MUL(PeriphClkInit->PLLI2S.PLLI2SMUL));
  198. assert_param(IS_RCC_HSE_PREDIV2(PeriphClkInit->PLLI2S.HSEPrediv2Value));
  199. /* Prediv2 can be written only when the PLL2 is disabled. */
  200. /* Return an error only if new value is different from the programmed value */
  201. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLL2ON) && \
  202. (__HAL_RCC_HSE_GET_PREDIV2() != PeriphClkInit->PLLI2S.HSEPrediv2Value))
  203. {
  204. return HAL_ERROR;
  205. }
  206. /* Configure the HSE prediv2 factor --------------------------------*/
  207. __HAL_RCC_HSE_PREDIV2_CONFIG(PeriphClkInit->PLLI2S.HSEPrediv2Value);
  208. /* Configure the main PLLI2S multiplication factors. */
  209. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SMUL);
  210. /* Enable the main PLLI2S. */
  211. __HAL_RCC_PLLI2S_ENABLE();
  212. /* Get Start Tick*/
  213. tickstart = HAL_GetTick();
  214. /* Wait till PLLI2S is ready */
  215. while (__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  216. {
  217. if ((HAL_GetTick() - tickstart) > PLLI2S_TIMEOUT_VALUE)
  218. {
  219. return HAL_TIMEOUT;
  220. }
  221. }
  222. }
  223. else
  224. {
  225. /* Return an error only if user wants to change the PLLI2SMUL whereas PLLI2S is active */
  226. if (READ_BIT(RCC->CFGR2, RCC_CFGR2_PLL3MUL) != PeriphClkInit->PLLI2S.PLLI2SMUL)
  227. {
  228. return HAL_ERROR;
  229. }
  230. }
  231. }
  232. #endif /* STM32F105xC || STM32F107xC */
  233. #if defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6)\
  234. || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG)\
  235. || defined(STM32F105xC) || defined(STM32F107xC)
  236. /*------------------------------ USB clock Configuration ------------------*/
  237. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == RCC_PERIPHCLK_USB)
  238. {
  239. /* Check the parameters */
  240. assert_param(IS_RCC_USBPLLCLK_DIV(PeriphClkInit->UsbClockSelection));
  241. /* Configure the USB clock source */
  242. __HAL_RCC_USB_CONFIG(PeriphClkInit->UsbClockSelection);
  243. }
  244. #endif /* STM32F102x6 || STM32F102xB || STM32F103x6 || STM32F103xB || STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
  245. return HAL_OK;
  246. }
  247. /**
  248. * @brief Get the PeriphClkInit according to the internal
  249. * RCC configuration registers.
  250. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  251. * returns the configuration information for the Extended Peripherals clocks(RTC, I2S, ADC clocks).
  252. * @retval None
  253. */
  254. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  255. {
  256. uint32_t srcclk = 0U;
  257. /* Set all possible values for the extended clock type parameter------------*/
  258. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_RTC;
  259. /* Get the RTC configuration -----------------------------------------------*/
  260. srcclk = __HAL_RCC_GET_RTC_SOURCE();
  261. /* Source clock is LSE or LSI*/
  262. PeriphClkInit->RTCClockSelection = srcclk;
  263. /* Get the ADC clock configuration -----------------------------------------*/
  264. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_ADC;
  265. PeriphClkInit->AdcClockSelection = __HAL_RCC_GET_ADC_SOURCE();
  266. #if defined(STM32F105xC) || defined(STM32F107xC)
  267. /* Get the I2S2 clock configuration -----------------------------------------*/
  268. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2S2;
  269. PeriphClkInit->I2s2ClockSelection = __HAL_RCC_GET_I2S2_SOURCE();
  270. /* Get the I2S3 clock configuration -----------------------------------------*/
  271. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2S3;
  272. PeriphClkInit->I2s3ClockSelection = __HAL_RCC_GET_I2S3_SOURCE();
  273. #endif /* STM32F105xC || STM32F107xC */
  274. #if defined(STM32F103xE) || defined(STM32F103xG)
  275. /* Get the I2S2 clock configuration -----------------------------------------*/
  276. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2S2;
  277. PeriphClkInit->I2s2ClockSelection = RCC_I2S2CLKSOURCE_SYSCLK;
  278. /* Get the I2S3 clock configuration -----------------------------------------*/
  279. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2S3;
  280. PeriphClkInit->I2s3ClockSelection = RCC_I2S3CLKSOURCE_SYSCLK;
  281. #endif /* STM32F103xE || STM32F103xG */
  282. #if defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6)\
  283. || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG)\
  284. || defined(STM32F105xC) || defined(STM32F107xC)
  285. /* Get the USB clock configuration -----------------------------------------*/
  286. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USB;
  287. PeriphClkInit->UsbClockSelection = __HAL_RCC_GET_USB_SOURCE();
  288. #endif /* STM32F102x6 || STM32F102xB || STM32F103x6 || STM32F103xB || STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
  289. }
  290. /**
  291. * @brief Returns the peripheral clock frequency
  292. * @note Returns 0 if peripheral clock is unknown
  293. * @param PeriphClk Peripheral clock identifier
  294. * This parameter can be one of the following values:
  295. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  296. * @arg @ref RCC_PERIPHCLK_ADC ADC peripheral clock
  297. @if STM32F103xE
  298. * @arg @ref RCC_PERIPHCLK_I2S2 I2S2 peripheral clock
  299. * @arg @ref RCC_PERIPHCLK_I2S3 I2S3 peripheral clock
  300. * @arg @ref RCC_PERIPHCLK_I2S3 I2S3 peripheral clock
  301. @endif
  302. @if STM32F103xG
  303. * @arg @ref RCC_PERIPHCLK_I2S2 I2S2 peripheral clock
  304. * @arg @ref RCC_PERIPHCLK_I2S3 I2S3 peripheral clock
  305. * @arg @ref RCC_PERIPHCLK_I2S3 I2S3 peripheral clock
  306. * @arg @ref RCC_PERIPHCLK_I2S2 I2S2 peripheral clock
  307. @endif
  308. @if STM32F105xC
  309. * @arg @ref RCC_PERIPHCLK_I2S2 I2S2 peripheral clock
  310. * @arg @ref RCC_PERIPHCLK_I2S3 I2S3 peripheral clock
  311. * @arg @ref RCC_PERIPHCLK_I2S3 I2S3 peripheral clock
  312. * @arg @ref RCC_PERIPHCLK_I2S2 I2S2 peripheral clock
  313. * @arg @ref RCC_PERIPHCLK_I2S3 I2S3 peripheral clock
  314. * @arg @ref RCC_PERIPHCLK_I2S3 I2S3 peripheral clock
  315. * @arg @ref RCC_PERIPHCLK_I2S2 I2S2 peripheral clock
  316. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  317. @endif
  318. @if STM32F107xC
  319. * @arg @ref RCC_PERIPHCLK_I2S2 I2S2 peripheral clock
  320. * @arg @ref RCC_PERIPHCLK_I2S3 I2S3 peripheral clock
  321. * @arg @ref RCC_PERIPHCLK_I2S3 I2S3 peripheral clock
  322. * @arg @ref RCC_PERIPHCLK_I2S2 I2S2 peripheral clock
  323. * @arg @ref RCC_PERIPHCLK_I2S3 I2S3 peripheral clock
  324. * @arg @ref RCC_PERIPHCLK_I2S3 I2S3 peripheral clock
  325. * @arg @ref RCC_PERIPHCLK_I2S2 I2S2 peripheral clock
  326. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  327. @endif
  328. @if STM32F102xx
  329. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  330. @endif
  331. @if STM32F103xx
  332. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  333. @endif
  334. * @retval Frequency in Hz (0: means that no available frequency for the peripheral)
  335. */
  336. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  337. {
  338. #if defined(STM32F105xC) || defined(STM32F107xC)
  339. const uint8_t aPLLMULFactorTable[14] = {0, 0, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 13};
  340. const uint8_t aPredivFactorTable[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  341. uint32_t prediv1 = 0U, pllclk = 0U, pllmul = 0U;
  342. uint32_t pll2mul = 0U, pll3mul = 0U, prediv2 = 0U;
  343. #endif /* STM32F105xC || STM32F107xC */
  344. #if defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6) || \
  345. defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG)
  346. const uint8_t aPLLMULFactorTable[16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16};
  347. const uint8_t aPredivFactorTable[2] = {1, 2};
  348. uint32_t prediv1 = 0U, pllclk = 0U, pllmul = 0U;
  349. #endif /* STM32F102x6 || STM32F102xB || STM32F103x6 || STM32F103xB || STM32F103xE || STM32F103xG */
  350. uint32_t temp_reg = 0U, frequency = 0U;
  351. /* Check the parameters */
  352. assert_param(IS_RCC_PERIPHCLOCK(PeriphClk));
  353. switch (PeriphClk)
  354. {
  355. #if defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6)\
  356. || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG)\
  357. || defined(STM32F105xC) || defined(STM32F107xC)
  358. case RCC_PERIPHCLK_USB:
  359. {
  360. /* Get RCC configuration ------------------------------------------------------*/
  361. temp_reg = RCC->CFGR;
  362. /* Check if PLL is enabled */
  363. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLON))
  364. {
  365. pllmul = aPLLMULFactorTable[(uint32_t)(temp_reg & RCC_CFGR_PLLMULL) >> RCC_CFGR_PLLMULL_Pos];
  366. if ((temp_reg & RCC_CFGR_PLLSRC) != RCC_PLLSOURCE_HSI_DIV2)
  367. {
  368. #if defined(STM32F105xC) || defined(STM32F107xC) || defined(STM32F100xB)\
  369. || defined(STM32F100xE)
  370. prediv1 = aPredivFactorTable[(uint32_t)(RCC->CFGR2 & RCC_CFGR2_PREDIV1) >> RCC_CFGR2_PREDIV1_Pos];
  371. #else
  372. prediv1 = aPredivFactorTable[(uint32_t)(RCC->CFGR & RCC_CFGR_PLLXTPRE) >> RCC_CFGR_PLLXTPRE_Pos];
  373. #endif /* STM32F105xC || STM32F107xC || STM32F100xB || STM32F100xE */
  374. #if defined(STM32F105xC) || defined(STM32F107xC)
  375. if (HAL_IS_BIT_SET(RCC->CFGR2, RCC_CFGR2_PREDIV1SRC))
  376. {
  377. /* PLL2 selected as Prediv1 source */
  378. /* PLLCLK = PLL2CLK / PREDIV1 * PLLMUL with PLL2CLK = HSE/PREDIV2 * PLL2MUL */
  379. prediv2 = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> RCC_CFGR2_PREDIV2_Pos) + 1;
  380. pll2mul = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> RCC_CFGR2_PLL2MUL_Pos) + 2;
  381. pllclk = (uint32_t)((((HSE_VALUE / prediv2) * pll2mul) / prediv1) * pllmul);
  382. }
  383. else
  384. {
  385. /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV1 * PLLMUL */
  386. pllclk = (uint32_t)((HSE_VALUE / prediv1) * pllmul);
  387. }
  388. /* If PLLMUL was set to 13 means that it was to cover the case PLLMUL 6.5 (avoid using float) */
  389. /* In this case need to divide pllclk by 2 */
  390. if (pllmul == aPLLMULFactorTable[(uint32_t)(RCC_CFGR_PLLMULL6_5) >> RCC_CFGR_PLLMULL_Pos])
  391. {
  392. pllclk = pllclk / 2;
  393. }
  394. #else
  395. if ((temp_reg & RCC_CFGR_PLLSRC) != RCC_PLLSOURCE_HSI_DIV2)
  396. {
  397. /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV1 * PLLMUL */
  398. pllclk = (uint32_t)((HSE_VALUE / prediv1) * pllmul);
  399. }
  400. #endif /* STM32F105xC || STM32F107xC */
  401. }
  402. else
  403. {
  404. /* HSI used as PLL clock source : PLLCLK = HSI/2 * PLLMUL */
  405. pllclk = (uint32_t)((HSI_VALUE >> 1) * pllmul);
  406. }
  407. /* Calcul of the USB frequency*/
  408. #if defined(STM32F105xC) || defined(STM32F107xC)
  409. /* USBCLK = PLLVCO = (2 x PLLCLK) / USB prescaler */
  410. if (__HAL_RCC_GET_USB_SOURCE() == RCC_USBCLKSOURCE_PLL_DIV2)
  411. {
  412. /* Prescaler of 2 selected for USB */
  413. frequency = pllclk;
  414. }
  415. else
  416. {
  417. /* Prescaler of 3 selected for USB */
  418. frequency = (2 * pllclk) / 3;
  419. }
  420. #else
  421. /* USBCLK = PLLCLK / USB prescaler */
  422. if (__HAL_RCC_GET_USB_SOURCE() == RCC_USBCLKSOURCE_PLL)
  423. {
  424. /* No prescaler selected for USB */
  425. frequency = pllclk;
  426. }
  427. else
  428. {
  429. /* Prescaler of 1.5 selected for USB */
  430. frequency = (pllclk * 2) / 3;
  431. }
  432. #endif
  433. }
  434. break;
  435. }
  436. #endif /* STM32F102x6 || STM32F102xB || STM32F103x6 || STM32F103xB || STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
  437. #if defined(STM32F103xE) || defined(STM32F103xG) || defined(STM32F105xC) || defined(STM32F107xC)
  438. case RCC_PERIPHCLK_I2S2:
  439. {
  440. #if defined(STM32F103xE) || defined(STM32F103xG)
  441. /* SYSCLK used as source clock for I2S2 */
  442. frequency = HAL_RCC_GetSysClockFreq();
  443. #else
  444. if (__HAL_RCC_GET_I2S2_SOURCE() == RCC_I2S2CLKSOURCE_SYSCLK)
  445. {
  446. /* SYSCLK used as source clock for I2S2 */
  447. frequency = HAL_RCC_GetSysClockFreq();
  448. }
  449. else
  450. {
  451. /* Check if PLLI2S is enabled */
  452. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLL3ON))
  453. {
  454. /* PLLI2SVCO = 2 * PLLI2SCLK = 2 * (HSE/PREDIV2 * PLL3MUL) */
  455. prediv2 = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> RCC_CFGR2_PREDIV2_Pos) + 1;
  456. pll3mul = ((RCC->CFGR2 & RCC_CFGR2_PLL3MUL) >> RCC_CFGR2_PLL3MUL_Pos) + 2;
  457. frequency = (uint32_t)(2 * ((HSE_VALUE / prediv2) * pll3mul));
  458. }
  459. }
  460. #endif /* STM32F103xE || STM32F103xG */
  461. break;
  462. }
  463. case RCC_PERIPHCLK_I2S3:
  464. {
  465. #if defined(STM32F103xE) || defined(STM32F103xG)
  466. /* SYSCLK used as source clock for I2S3 */
  467. frequency = HAL_RCC_GetSysClockFreq();
  468. #else
  469. if (__HAL_RCC_GET_I2S3_SOURCE() == RCC_I2S3CLKSOURCE_SYSCLK)
  470. {
  471. /* SYSCLK used as source clock for I2S3 */
  472. frequency = HAL_RCC_GetSysClockFreq();
  473. }
  474. else
  475. {
  476. /* Check if PLLI2S is enabled */
  477. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLL3ON))
  478. {
  479. /* PLLI2SVCO = 2 * PLLI2SCLK = 2 * (HSE/PREDIV2 * PLL3MUL) */
  480. prediv2 = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> RCC_CFGR2_PREDIV2_Pos) + 1;
  481. pll3mul = ((RCC->CFGR2 & RCC_CFGR2_PLL3MUL) >> RCC_CFGR2_PLL3MUL_Pos) + 2;
  482. frequency = (uint32_t)(2 * ((HSE_VALUE / prediv2) * pll3mul));
  483. }
  484. }
  485. #endif /* STM32F103xE || STM32F103xG */
  486. break;
  487. }
  488. #endif /* STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
  489. case RCC_PERIPHCLK_RTC:
  490. {
  491. /* Get RCC BDCR configuration ------------------------------------------------------*/
  492. temp_reg = RCC->BDCR;
  493. /* Check if LSE is ready if RTC clock selection is LSE */
  494. if (((temp_reg & RCC_BDCR_RTCSEL) == RCC_RTCCLKSOURCE_LSE) && (HAL_IS_BIT_SET(temp_reg, RCC_BDCR_LSERDY)))
  495. {
  496. frequency = LSE_VALUE;
  497. }
  498. /* Check if LSI is ready if RTC clock selection is LSI */
  499. else if (((temp_reg & RCC_BDCR_RTCSEL) == RCC_RTCCLKSOURCE_LSI) && (HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY)))
  500. {
  501. frequency = LSI_VALUE;
  502. }
  503. else if (((temp_reg & RCC_BDCR_RTCSEL) == RCC_RTCCLKSOURCE_HSE_DIV128) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY)))
  504. {
  505. frequency = HSE_VALUE / 128U;
  506. }
  507. /* Clock not enabled for RTC*/
  508. else
  509. {
  510. /* nothing to do: frequency already initialized to 0U */
  511. }
  512. break;
  513. }
  514. case RCC_PERIPHCLK_ADC:
  515. {
  516. frequency = HAL_RCC_GetPCLK2Freq() / (((__HAL_RCC_GET_ADC_SOURCE() >> RCC_CFGR_ADCPRE_Pos) + 1) * 2);
  517. break;
  518. }
  519. default:
  520. {
  521. break;
  522. }
  523. }
  524. return (frequency);
  525. }
  526. /**
  527. * @}
  528. */
  529. #if defined(STM32F105xC) || defined(STM32F107xC)
  530. /** @defgroup RCCEx_Exported_Functions_Group2 PLLI2S Management function
  531. * @brief PLLI2S Management functions
  532. *
  533. @verbatim
  534. ===============================================================================
  535. ##### Extended PLLI2S Management functions #####
  536. ===============================================================================
  537. [..]
  538. This subsection provides a set of functions allowing to control the PLLI2S
  539. activation or deactivation
  540. @endverbatim
  541. * @{
  542. */
  543. /**
  544. * @brief Enable PLLI2S
  545. * @param PLLI2SInit pointer to an RCC_PLLI2SInitTypeDef structure that
  546. * contains the configuration information for the PLLI2S
  547. * @note The PLLI2S configuration not modified if used by I2S2 or I2S3 Interface.
  548. * @retval HAL status
  549. */
  550. HAL_StatusTypeDef HAL_RCCEx_EnablePLLI2S(RCC_PLLI2SInitTypeDef *PLLI2SInit)
  551. {
  552. uint32_t tickstart = 0U;
  553. /* Check that PLL I2S has not been already enabled by I2S2 or I2S3*/
  554. if (HAL_IS_BIT_CLR(RCC->CFGR2, RCC_CFGR2_I2S2SRC) && HAL_IS_BIT_CLR(RCC->CFGR2, RCC_CFGR2_I2S3SRC))
  555. {
  556. /* Check the parameters */
  557. assert_param(IS_RCC_PLLI2S_MUL(PLLI2SInit->PLLI2SMUL));
  558. assert_param(IS_RCC_HSE_PREDIV2(PLLI2SInit->HSEPrediv2Value));
  559. /* Prediv2 can be written only when the PLL2 is disabled. */
  560. /* Return an error only if new value is different from the programmed value */
  561. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLL2ON) && \
  562. (__HAL_RCC_HSE_GET_PREDIV2() != PLLI2SInit->HSEPrediv2Value))
  563. {
  564. return HAL_ERROR;
  565. }
  566. /* Disable the main PLLI2S. */
  567. __HAL_RCC_PLLI2S_DISABLE();
  568. /* Get Start Tick*/
  569. tickstart = HAL_GetTick();
  570. /* Wait till PLLI2S is ready */
  571. while (__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  572. {
  573. if ((HAL_GetTick() - tickstart) > PLLI2S_TIMEOUT_VALUE)
  574. {
  575. return HAL_TIMEOUT;
  576. }
  577. }
  578. /* Configure the HSE prediv2 factor --------------------------------*/
  579. __HAL_RCC_HSE_PREDIV2_CONFIG(PLLI2SInit->HSEPrediv2Value);
  580. /* Configure the main PLLI2S multiplication factors. */
  581. __HAL_RCC_PLLI2S_CONFIG(PLLI2SInit->PLLI2SMUL);
  582. /* Enable the main PLLI2S. */
  583. __HAL_RCC_PLLI2S_ENABLE();
  584. /* Get Start Tick*/
  585. tickstart = HAL_GetTick();
  586. /* Wait till PLLI2S is ready */
  587. while (__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  588. {
  589. if ((HAL_GetTick() - tickstart) > PLLI2S_TIMEOUT_VALUE)
  590. {
  591. return HAL_TIMEOUT;
  592. }
  593. }
  594. }
  595. else
  596. {
  597. /* PLLI2S cannot be modified as already used by I2S2 or I2S3 */
  598. return HAL_ERROR;
  599. }
  600. return HAL_OK;
  601. }
  602. /**
  603. * @brief Disable PLLI2S
  604. * @note PLLI2S is not disabled if used by I2S2 or I2S3 Interface.
  605. * @retval HAL status
  606. */
  607. HAL_StatusTypeDef HAL_RCCEx_DisablePLLI2S(void)
  608. {
  609. uint32_t tickstart = 0U;
  610. /* Disable PLL I2S as not requested by I2S2 or I2S3*/
  611. if (HAL_IS_BIT_CLR(RCC->CFGR2, RCC_CFGR2_I2S2SRC) && HAL_IS_BIT_CLR(RCC->CFGR2, RCC_CFGR2_I2S3SRC))
  612. {
  613. /* Disable the main PLLI2S. */
  614. __HAL_RCC_PLLI2S_DISABLE();
  615. /* Get Start Tick*/
  616. tickstart = HAL_GetTick();
  617. /* Wait till PLLI2S is ready */
  618. while (__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  619. {
  620. if ((HAL_GetTick() - tickstart) > PLLI2S_TIMEOUT_VALUE)
  621. {
  622. return HAL_TIMEOUT;
  623. }
  624. }
  625. }
  626. else
  627. {
  628. /* PLLI2S is currently used by I2S2 or I2S3. Cannot be disabled.*/
  629. return HAL_ERROR;
  630. }
  631. return HAL_OK;
  632. }
  633. /**
  634. * @}
  635. */
  636. /** @defgroup RCCEx_Exported_Functions_Group3 PLL2 Management function
  637. * @brief PLL2 Management functions
  638. *
  639. @verbatim
  640. ===============================================================================
  641. ##### Extended PLL2 Management functions #####
  642. ===============================================================================
  643. [..]
  644. This subsection provides a set of functions allowing to control the PLL2
  645. activation or deactivation
  646. @endverbatim
  647. * @{
  648. */
  649. /**
  650. * @brief Enable PLL2
  651. * @param PLL2Init pointer to an RCC_PLL2InitTypeDef structure that
  652. * contains the configuration information for the PLL2
  653. * @note The PLL2 configuration not modified if used indirectly as system clock.
  654. * @retval HAL status
  655. */
  656. HAL_StatusTypeDef HAL_RCCEx_EnablePLL2(RCC_PLL2InitTypeDef *PLL2Init)
  657. {
  658. uint32_t tickstart = 0U;
  659. /* This bit can not be cleared if the PLL2 clock is used indirectly as system
  660. clock (i.e. it is used as PLL clock entry that is used as system clock). */
  661. if ((__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE) && \
  662. (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && \
  663. ((READ_BIT(RCC->CFGR2, RCC_CFGR2_PREDIV1SRC)) == RCC_CFGR2_PREDIV1SRC_PLL2))
  664. {
  665. return HAL_ERROR;
  666. }
  667. else
  668. {
  669. /* Check the parameters */
  670. assert_param(IS_RCC_PLL2_MUL(PLL2Init->PLL2MUL));
  671. assert_param(IS_RCC_HSE_PREDIV2(PLL2Init->HSEPrediv2Value));
  672. /* Prediv2 can be written only when the PLLI2S is disabled. */
  673. /* Return an error only if new value is different from the programmed value */
  674. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLL3ON) && \
  675. (__HAL_RCC_HSE_GET_PREDIV2() != PLL2Init->HSEPrediv2Value))
  676. {
  677. return HAL_ERROR;
  678. }
  679. /* Disable the main PLL2. */
  680. __HAL_RCC_PLL2_DISABLE();
  681. /* Get Start Tick*/
  682. tickstart = HAL_GetTick();
  683. /* Wait till PLL2 is disabled */
  684. while (__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) != RESET)
  685. {
  686. if ((HAL_GetTick() - tickstart) > PLL2_TIMEOUT_VALUE)
  687. {
  688. return HAL_TIMEOUT;
  689. }
  690. }
  691. /* Configure the HSE prediv2 factor --------------------------------*/
  692. __HAL_RCC_HSE_PREDIV2_CONFIG(PLL2Init->HSEPrediv2Value);
  693. /* Configure the main PLL2 multiplication factors. */
  694. __HAL_RCC_PLL2_CONFIG(PLL2Init->PLL2MUL);
  695. /* Enable the main PLL2. */
  696. __HAL_RCC_PLL2_ENABLE();
  697. /* Get Start Tick*/
  698. tickstart = HAL_GetTick();
  699. /* Wait till PLL2 is ready */
  700. while (__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) == RESET)
  701. {
  702. if ((HAL_GetTick() - tickstart) > PLL2_TIMEOUT_VALUE)
  703. {
  704. return HAL_TIMEOUT;
  705. }
  706. }
  707. }
  708. return HAL_OK;
  709. }
  710. /**
  711. * @brief Disable PLL2
  712. * @note PLL2 is not disabled if used indirectly as system clock.
  713. * @retval HAL status
  714. */
  715. HAL_StatusTypeDef HAL_RCCEx_DisablePLL2(void)
  716. {
  717. uint32_t tickstart = 0U;
  718. /* This bit can not be cleared if the PLL2 clock is used indirectly as system
  719. clock (i.e. it is used as PLL clock entry that is used as system clock). */
  720. if ((__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE) && \
  721. (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && \
  722. ((READ_BIT(RCC->CFGR2, RCC_CFGR2_PREDIV1SRC)) == RCC_CFGR2_PREDIV1SRC_PLL2))
  723. {
  724. return HAL_ERROR;
  725. }
  726. else
  727. {
  728. /* Disable the main PLL2. */
  729. __HAL_RCC_PLL2_DISABLE();
  730. /* Get Start Tick*/
  731. tickstart = HAL_GetTick();
  732. /* Wait till PLL2 is disabled */
  733. while (__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) != RESET)
  734. {
  735. if ((HAL_GetTick() - tickstart) > PLL2_TIMEOUT_VALUE)
  736. {
  737. return HAL_TIMEOUT;
  738. }
  739. }
  740. }
  741. return HAL_OK;
  742. }
  743. /**
  744. * @}
  745. */
  746. #endif /* STM32F105xC || STM32F107xC */
  747. /**
  748. * @}
  749. */
  750. /**
  751. * @}
  752. */
  753. #endif /* HAL_RCC_MODULE_ENABLED */
  754. /**
  755. * @}
  756. */
  757. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/