stm32f1xx_hal_rcc_ex.c 31 KB

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