stm32f1xx_hal_rcc_ex.c 29 KB

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