stm32f1xx_hal_rcc_ex.c 31 KB

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