Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

3743 wiersze
149 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_rcc_ex.c
  4. * @author MCD Application Team
  5. * @brief Extension 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) 2017 STMicroelectronics.
  14. * All rights reserved.</center></h2>
  15. *
  16. * This software component is licensed by ST under BSD 3-Clause license,
  17. * the "License"; You may not use this file except in compliance with the
  18. * License. You may obtain a copy of the License at:
  19. * opensource.org/licenses/BSD-3-Clause
  20. *
  21. ******************************************************************************
  22. */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32f4xx_hal.h"
  25. /** @addtogroup STM32F4xx_HAL_Driver
  26. * @{
  27. */
  28. /** @defgroup RCCEx RCCEx
  29. * @brief RCCEx HAL module driver
  30. * @{
  31. */
  32. #ifdef HAL_RCC_MODULE_ENABLED
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private define ------------------------------------------------------------*/
  35. /** @addtogroup RCCEx_Private_Constants
  36. * @{
  37. */
  38. /**
  39. * @}
  40. */
  41. /* Private macro -------------------------------------------------------------*/
  42. /* Private variables ---------------------------------------------------------*/
  43. /* Private function prototypes -----------------------------------------------*/
  44. /* Private functions ---------------------------------------------------------*/
  45. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  46. * @{
  47. */
  48. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  49. * @brief Extended Peripheral Control functions
  50. *
  51. @verbatim
  52. ===============================================================================
  53. ##### Extended Peripheral Control functions #####
  54. ===============================================================================
  55. [..]
  56. This subsection provides a set of functions allowing to control the RCC Clocks
  57. frequencies.
  58. [..]
  59. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  60. select the RTC clock source; in this case the Backup domain will be reset in
  61. order to modify the RTC Clock source, as consequence RTC registers (including
  62. the backup registers) and RCC_BDCR register are set to their reset values.
  63. @endverbatim
  64. * @{
  65. */
  66. #if defined(STM32F446xx)
  67. /**
  68. * @brief Initializes the RCC extended peripherals clocks according to the specified
  69. * parameters in the RCC_PeriphCLKInitTypeDef.
  70. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  71. * contains the configuration information for the Extended Peripherals
  72. * clocks(I2S, SAI, LTDC RTC and TIM).
  73. *
  74. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  75. * the RTC clock source; in this case the Backup domain will be reset in
  76. * order to modify the RTC Clock source, as consequence RTC registers (including
  77. * the backup registers) and RCC_BDCR register are set to their reset values.
  78. *
  79. * @retval HAL status
  80. */
  81. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  82. {
  83. uint32_t tickstart = 0U;
  84. uint32_t tmpreg1 = 0U;
  85. uint32_t plli2sp = 0U;
  86. uint32_t plli2sq = 0U;
  87. uint32_t plli2sr = 0U;
  88. uint32_t pllsaip = 0U;
  89. uint32_t pllsaiq = 0U;
  90. uint32_t plli2sused = 0U;
  91. uint32_t pllsaiused = 0U;
  92. /* Check the peripheral clock selection parameters */
  93. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  94. /*------------------------ I2S APB1 configuration --------------------------*/
  95. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB1) == (RCC_PERIPHCLK_I2S_APB1))
  96. {
  97. /* Check the parameters */
  98. assert_param(IS_RCC_I2SAPB1CLKSOURCE(PeriphClkInit->I2sApb1ClockSelection));
  99. /* Configure I2S Clock source */
  100. __HAL_RCC_I2S_APB1_CONFIG(PeriphClkInit->I2sApb1ClockSelection);
  101. /* Enable the PLLI2S when it's used as clock source for I2S */
  102. if(PeriphClkInit->I2sApb1ClockSelection == RCC_I2SAPB1CLKSOURCE_PLLI2S)
  103. {
  104. plli2sused = 1U;
  105. }
  106. }
  107. /*--------------------------------------------------------------------------*/
  108. /*---------------------------- I2S APB2 configuration ----------------------*/
  109. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB2) == (RCC_PERIPHCLK_I2S_APB2))
  110. {
  111. /* Check the parameters */
  112. assert_param(IS_RCC_I2SAPB2CLKSOURCE(PeriphClkInit->I2sApb2ClockSelection));
  113. /* Configure I2S Clock source */
  114. __HAL_RCC_I2S_APB2_CONFIG(PeriphClkInit->I2sApb2ClockSelection);
  115. /* Enable the PLLI2S when it's used as clock source for I2S */
  116. if(PeriphClkInit->I2sApb2ClockSelection == RCC_I2SAPB2CLKSOURCE_PLLI2S)
  117. {
  118. plli2sused = 1U;
  119. }
  120. }
  121. /*--------------------------------------------------------------------------*/
  122. /*--------------------------- SAI1 configuration ---------------------------*/
  123. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == (RCC_PERIPHCLK_SAI1))
  124. {
  125. /* Check the parameters */
  126. assert_param(IS_RCC_SAI1CLKSOURCE(PeriphClkInit->Sai1ClockSelection));
  127. /* Configure SAI1 Clock source */
  128. __HAL_RCC_SAI1_CONFIG(PeriphClkInit->Sai1ClockSelection);
  129. /* Enable the PLLI2S when it's used as clock source for SAI */
  130. if(PeriphClkInit->Sai1ClockSelection == RCC_SAI1CLKSOURCE_PLLI2S)
  131. {
  132. plli2sused = 1U;
  133. }
  134. /* Enable the PLLSAI when it's used as clock source for SAI */
  135. if(PeriphClkInit->Sai1ClockSelection == RCC_SAI1CLKSOURCE_PLLSAI)
  136. {
  137. pllsaiused = 1U;
  138. }
  139. }
  140. /*--------------------------------------------------------------------------*/
  141. /*-------------------------- SAI2 configuration ----------------------------*/
  142. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI2) == (RCC_PERIPHCLK_SAI2))
  143. {
  144. /* Check the parameters */
  145. assert_param(IS_RCC_SAI2CLKSOURCE(PeriphClkInit->Sai2ClockSelection));
  146. /* Configure SAI2 Clock source */
  147. __HAL_RCC_SAI2_CONFIG(PeriphClkInit->Sai2ClockSelection);
  148. /* Enable the PLLI2S when it's used as clock source for SAI */
  149. if(PeriphClkInit->Sai2ClockSelection == RCC_SAI2CLKSOURCE_PLLI2S)
  150. {
  151. plli2sused = 1U;
  152. }
  153. /* Enable the PLLSAI when it's used as clock source for SAI */
  154. if(PeriphClkInit->Sai2ClockSelection == RCC_SAI2CLKSOURCE_PLLSAI)
  155. {
  156. pllsaiused = 1U;
  157. }
  158. }
  159. /*--------------------------------------------------------------------------*/
  160. /*----------------------------- RTC configuration --------------------------*/
  161. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  162. {
  163. /* Check for RTC Parameters used to output RTCCLK */
  164. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  165. /* Enable Power Clock*/
  166. __HAL_RCC_PWR_CLK_ENABLE();
  167. /* Enable write access to Backup domain */
  168. PWR->CR |= PWR_CR_DBP;
  169. /* Get tick */
  170. tickstart = HAL_GetTick();
  171. while((PWR->CR & PWR_CR_DBP) == RESET)
  172. {
  173. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  174. {
  175. return HAL_TIMEOUT;
  176. }
  177. }
  178. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  179. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  180. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  181. {
  182. /* Store the content of BDCR register before the reset of Backup Domain */
  183. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  184. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  185. __HAL_RCC_BACKUPRESET_FORCE();
  186. __HAL_RCC_BACKUPRESET_RELEASE();
  187. /* Restore the Content of BDCR register */
  188. RCC->BDCR = tmpreg1;
  189. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  190. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  191. {
  192. /* Get tick */
  193. tickstart = HAL_GetTick();
  194. /* Wait till LSE is ready */
  195. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  196. {
  197. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  198. {
  199. return HAL_TIMEOUT;
  200. }
  201. }
  202. }
  203. }
  204. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  205. }
  206. /*--------------------------------------------------------------------------*/
  207. /*---------------------------- TIM configuration ---------------------------*/
  208. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  209. {
  210. /* Configure Timer Prescaler */
  211. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  212. }
  213. /*--------------------------------------------------------------------------*/
  214. /*---------------------------- FMPI2C1 Configuration -----------------------*/
  215. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_FMPI2C1) == RCC_PERIPHCLK_FMPI2C1)
  216. {
  217. /* Check the parameters */
  218. assert_param(IS_RCC_FMPI2C1CLKSOURCE(PeriphClkInit->Fmpi2c1ClockSelection));
  219. /* Configure the FMPI2C1 clock source */
  220. __HAL_RCC_FMPI2C1_CONFIG(PeriphClkInit->Fmpi2c1ClockSelection);
  221. }
  222. /*--------------------------------------------------------------------------*/
  223. /*------------------------------ CEC Configuration -------------------------*/
  224. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CEC) == RCC_PERIPHCLK_CEC)
  225. {
  226. /* Check the parameters */
  227. assert_param(IS_RCC_CECCLKSOURCE(PeriphClkInit->CecClockSelection));
  228. /* Configure the CEC clock source */
  229. __HAL_RCC_CEC_CONFIG(PeriphClkInit->CecClockSelection);
  230. }
  231. /*--------------------------------------------------------------------------*/
  232. /*----------------------------- CLK48 Configuration ------------------------*/
  233. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48)
  234. {
  235. /* Check the parameters */
  236. assert_param(IS_RCC_CLK48CLKSOURCE(PeriphClkInit->Clk48ClockSelection));
  237. /* Configure the CLK48 clock source */
  238. __HAL_RCC_CLK48_CONFIG(PeriphClkInit->Clk48ClockSelection);
  239. /* Enable the PLLSAI when it's used as clock source for CLK48 */
  240. if(PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLSAIP)
  241. {
  242. pllsaiused = 1U;
  243. }
  244. }
  245. /*--------------------------------------------------------------------------*/
  246. /*----------------------------- SDIO Configuration -------------------------*/
  247. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDIO) == RCC_PERIPHCLK_SDIO)
  248. {
  249. /* Check the parameters */
  250. assert_param(IS_RCC_SDIOCLKSOURCE(PeriphClkInit->SdioClockSelection));
  251. /* Configure the SDIO clock source */
  252. __HAL_RCC_SDIO_CONFIG(PeriphClkInit->SdioClockSelection);
  253. }
  254. /*--------------------------------------------------------------------------*/
  255. /*------------------------------ SPDIFRX Configuration ---------------------*/
  256. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SPDIFRX) == RCC_PERIPHCLK_SPDIFRX)
  257. {
  258. /* Check the parameters */
  259. assert_param(IS_RCC_SPDIFRXCLKSOURCE(PeriphClkInit->SpdifClockSelection));
  260. /* Configure the SPDIFRX clock source */
  261. __HAL_RCC_SPDIFRX_CONFIG(PeriphClkInit->SpdifClockSelection);
  262. /* Enable the PLLI2S when it's used as clock source for SPDIFRX */
  263. if(PeriphClkInit->SpdifClockSelection == RCC_SPDIFRXCLKSOURCE_PLLI2SP)
  264. {
  265. plli2sused = 1U;
  266. }
  267. }
  268. /*--------------------------------------------------------------------------*/
  269. /*---------------------------- PLLI2S Configuration ------------------------*/
  270. /* PLLI2S is configured when a peripheral will use it as source clock : SAI1, SAI2, I2S on APB1,
  271. I2S on APB2 or SPDIFRX */
  272. if((plli2sused == 1U) || (PeriphClkInit->PeriphClockSelection == RCC_PERIPHCLK_PLLI2S))
  273. {
  274. /* Disable the PLLI2S */
  275. __HAL_RCC_PLLI2S_DISABLE();
  276. /* Get tick */
  277. tickstart = HAL_GetTick();
  278. /* Wait till PLLI2S is disabled */
  279. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  280. {
  281. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  282. {
  283. /* return in case of Timeout detected */
  284. return HAL_TIMEOUT;
  285. }
  286. }
  287. /* check for common PLLI2S Parameters */
  288. assert_param(IS_RCC_PLLI2SM_VALUE(PeriphClkInit->PLLI2S.PLLI2SM));
  289. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  290. /*------ In Case of PLLI2S is selected as source clock for I2S -----------*/
  291. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB1) == RCC_PERIPHCLK_I2S_APB1) && (PeriphClkInit->I2sApb1ClockSelection == RCC_I2SAPB1CLKSOURCE_PLLI2S)) ||
  292. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB2) == RCC_PERIPHCLK_I2S_APB2) && (PeriphClkInit->I2sApb2ClockSelection == RCC_I2SAPB2CLKSOURCE_PLLI2S)))
  293. {
  294. /* check for Parameters */
  295. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  296. /* Read PLLI2SP/PLLI2SQ value from PLLI2SCFGR register (this value is not needed for I2S configuration) */
  297. plli2sp = ((((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SP) >> RCC_PLLI2SCFGR_PLLI2SP_Pos) + 1U) << 1U);
  298. plli2sq = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> RCC_PLLI2SCFGR_PLLI2SQ_Pos);
  299. /* Configure the PLLI2S division factors */
  300. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  301. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  302. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , plli2sp, plli2sq, PeriphClkInit->PLLI2S.PLLI2SR);
  303. }
  304. /*------- In Case of PLLI2S is selected as source clock for SAI ----------*/
  305. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == RCC_PERIPHCLK_SAI1) && (PeriphClkInit->Sai1ClockSelection == RCC_SAI1CLKSOURCE_PLLI2S)) ||
  306. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI2) == RCC_PERIPHCLK_SAI2) && (PeriphClkInit->Sai2ClockSelection == RCC_SAI2CLKSOURCE_PLLI2S)))
  307. {
  308. /* Check for PLLI2S Parameters */
  309. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  310. /* Check for PLLI2S/DIVQ parameters */
  311. assert_param(IS_RCC_PLLI2S_DIVQ_VALUE(PeriphClkInit->PLLI2SDivQ));
  312. /* Read PLLI2SP/PLLI2SR value from PLLI2SCFGR register (this value is not needed for SAI configuration) */
  313. plli2sp = ((((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SP) >> RCC_PLLI2SCFGR_PLLI2SP_Pos) + 1U) << 1U);
  314. plli2sr = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  315. /* Configure the PLLI2S division factors */
  316. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  317. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  318. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  319. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , plli2sp, PeriphClkInit->PLLI2S.PLLI2SQ, plli2sr);
  320. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  321. __HAL_RCC_PLLI2S_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLI2SDivQ);
  322. }
  323. /*------ In Case of PLLI2S is selected as source clock for SPDIFRX -------*/
  324. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SPDIFRX) == RCC_PERIPHCLK_SPDIFRX) && (PeriphClkInit->SpdifClockSelection == RCC_SPDIFRXCLKSOURCE_PLLI2SP))
  325. {
  326. /* check for Parameters */
  327. assert_param(IS_RCC_PLLI2SP_VALUE(PeriphClkInit->PLLI2S.PLLI2SP));
  328. /* Read PLLI2SR value from PLLI2SCFGR register (this value is not need for SAI configuration) */
  329. plli2sq = ((((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SP) >> RCC_PLLI2SCFGR_PLLI2SP_Pos) + 1U) << 1U);
  330. plli2sr = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  331. /* Configure the PLLI2S division factors */
  332. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  333. /* SPDIFRXCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SP */
  334. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SP, plli2sq, plli2sr);
  335. }
  336. /*----------------- In Case of PLLI2S is just selected -----------------*/
  337. if((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S)
  338. {
  339. /* Check for Parameters */
  340. assert_param(IS_RCC_PLLI2SP_VALUE(PeriphClkInit->PLLI2S.PLLI2SP));
  341. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  342. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  343. /* Configure the PLLI2S division factors */
  344. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  345. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SP, PeriphClkInit->PLLI2S.PLLI2SQ, PeriphClkInit->PLLI2S.PLLI2SR);
  346. }
  347. /* Enable the PLLI2S */
  348. __HAL_RCC_PLLI2S_ENABLE();
  349. /* Get tick */
  350. tickstart = HAL_GetTick();
  351. /* Wait till PLLI2S is ready */
  352. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  353. {
  354. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  355. {
  356. /* return in case of Timeout detected */
  357. return HAL_TIMEOUT;
  358. }
  359. }
  360. }
  361. /*--------------------------------------------------------------------------*/
  362. /*----------------------------- PLLSAI Configuration -----------------------*/
  363. /* PLLSAI is configured when a peripheral will use it as source clock : SAI1, SAI2, CLK48 or SDIO */
  364. if(pllsaiused == 1U)
  365. {
  366. /* Disable PLLSAI Clock */
  367. __HAL_RCC_PLLSAI_DISABLE();
  368. /* Get tick */
  369. tickstart = HAL_GetTick();
  370. /* Wait till PLLSAI is disabled */
  371. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  372. {
  373. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  374. {
  375. /* return in case of Timeout detected */
  376. return HAL_TIMEOUT;
  377. }
  378. }
  379. /* Check the PLLSAI division factors */
  380. assert_param(IS_RCC_PLLSAIM_VALUE(PeriphClkInit->PLLSAI.PLLSAIM));
  381. assert_param(IS_RCC_PLLSAIN_VALUE(PeriphClkInit->PLLSAI.PLLSAIN));
  382. /*------ In Case of PLLSAI is selected as source clock for SAI -----------*/
  383. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == RCC_PERIPHCLK_SAI1) && (PeriphClkInit->Sai1ClockSelection == RCC_SAI1CLKSOURCE_PLLSAI)) ||
  384. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI2) == RCC_PERIPHCLK_SAI2) && (PeriphClkInit->Sai2ClockSelection == RCC_SAI2CLKSOURCE_PLLSAI)))
  385. {
  386. /* check for PLLSAIQ Parameter */
  387. assert_param(IS_RCC_PLLSAIQ_VALUE(PeriphClkInit->PLLSAI.PLLSAIQ));
  388. /* check for PLLSAI/DIVQ Parameter */
  389. assert_param(IS_RCC_PLLSAI_DIVQ_VALUE(PeriphClkInit->PLLSAIDivQ));
  390. /* Read PLLSAIP value from PLLSAICFGR register (this value is not needed for SAI configuration) */
  391. pllsaip = ((((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIP) >> RCC_PLLSAICFGR_PLLSAIP_Pos) + 1U) << 1U);
  392. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  393. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  394. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  395. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIM, PeriphClkInit->PLLSAI.PLLSAIN , pllsaip, PeriphClkInit->PLLSAI.PLLSAIQ, 0U);
  396. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  397. __HAL_RCC_PLLSAI_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLSAIDivQ);
  398. }
  399. /*------ In Case of PLLSAI is selected as source clock for CLK48 ---------*/
  400. /* In Case of PLLI2S is selected as source clock for CLK48 */
  401. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48) && (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLSAIP))
  402. {
  403. /* check for Parameters */
  404. assert_param(IS_RCC_PLLSAIP_VALUE(PeriphClkInit->PLLSAI.PLLSAIP));
  405. /* Read PLLSAIQ value from PLLI2SCFGR register (this value is not need for SAI configuration) */
  406. pllsaiq = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  407. /* Configure the PLLSAI division factors */
  408. /* PLLSAI_VCO = f(VCO clock) = f(PLLSAI clock input) * (PLLI2SN/PLLSAIM) */
  409. /* 48CLK = f(PLLSAI clock output) = f(VCO clock) / PLLSAIP */
  410. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIM, PeriphClkInit->PLLSAI.PLLSAIN , PeriphClkInit->PLLSAI.PLLSAIP, pllsaiq, 0U);
  411. }
  412. /* Enable PLLSAI Clock */
  413. __HAL_RCC_PLLSAI_ENABLE();
  414. /* Get tick */
  415. tickstart = HAL_GetTick();
  416. /* Wait till PLLSAI is ready */
  417. while(__HAL_RCC_PLLSAI_GET_FLAG() == RESET)
  418. {
  419. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  420. {
  421. /* return in case of Timeout detected */
  422. return HAL_TIMEOUT;
  423. }
  424. }
  425. }
  426. return HAL_OK;
  427. }
  428. /**
  429. * @brief Get the RCC_PeriphCLKInitTypeDef according to the internal
  430. * RCC configuration registers.
  431. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  432. * will be configured.
  433. * @retval None
  434. */
  435. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  436. {
  437. uint32_t tempreg;
  438. /* Set all possible values for the extended clock type parameter------------*/
  439. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_I2S_APB2 |\
  440. RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_SAI2 |\
  441. RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC |\
  442. RCC_PERIPHCLK_CEC | RCC_PERIPHCLK_FMPI2C1 |\
  443. RCC_PERIPHCLK_CLK48 | RCC_PERIPHCLK_SDIO |\
  444. RCC_PERIPHCLK_SPDIFRX;
  445. /* Get the PLLI2S Clock configuration --------------------------------------*/
  446. PeriphClkInit->PLLI2S.PLLI2SM = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM) >> RCC_PLLI2SCFGR_PLLI2SM_Pos);
  447. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> RCC_PLLI2SCFGR_PLLI2SN_Pos);
  448. PeriphClkInit->PLLI2S.PLLI2SP = (uint32_t)((((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SP) >> RCC_PLLI2SCFGR_PLLI2SP_Pos) + 1U) << 1U);
  449. PeriphClkInit->PLLI2S.PLLI2SQ = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> RCC_PLLI2SCFGR_PLLI2SQ_Pos);
  450. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  451. /* Get the PLLSAI Clock configuration --------------------------------------*/
  452. PeriphClkInit->PLLSAI.PLLSAIM = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIM) >> RCC_PLLSAICFGR_PLLSAIM_Pos);
  453. PeriphClkInit->PLLSAI.PLLSAIN = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> RCC_PLLSAICFGR_PLLSAIN_Pos);
  454. PeriphClkInit->PLLSAI.PLLSAIP = (uint32_t)((((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIP) >> RCC_PLLSAICFGR_PLLSAIP_Pos) + 1U) << 1U);
  455. PeriphClkInit->PLLSAI.PLLSAIQ = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  456. /* Get the PLLSAI/PLLI2S division factors ----------------------------------*/
  457. PeriphClkInit->PLLI2SDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) >> RCC_DCKCFGR_PLLI2SDIVQ_Pos);
  458. PeriphClkInit->PLLSAIDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> RCC_DCKCFGR_PLLSAIDIVQ_Pos);
  459. /* Get the SAI1 clock configuration ----------------------------------------*/
  460. PeriphClkInit->Sai1ClockSelection = __HAL_RCC_GET_SAI1_SOURCE();
  461. /* Get the SAI2 clock configuration ----------------------------------------*/
  462. PeriphClkInit->Sai2ClockSelection = __HAL_RCC_GET_SAI2_SOURCE();
  463. /* Get the I2S APB1 clock configuration ------------------------------------*/
  464. PeriphClkInit->I2sApb1ClockSelection = __HAL_RCC_GET_I2S_APB1_SOURCE();
  465. /* Get the I2S APB2 clock configuration ------------------------------------*/
  466. PeriphClkInit->I2sApb2ClockSelection = __HAL_RCC_GET_I2S_APB2_SOURCE();
  467. /* Get the RTC Clock configuration -----------------------------------------*/
  468. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  469. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  470. /* Get the CEC clock configuration -----------------------------------------*/
  471. PeriphClkInit->CecClockSelection = __HAL_RCC_GET_CEC_SOURCE();
  472. /* Get the FMPI2C1 clock configuration -------------------------------------*/
  473. PeriphClkInit->Fmpi2c1ClockSelection = __HAL_RCC_GET_FMPI2C1_SOURCE();
  474. /* Get the CLK48 clock configuration ----------------------------------------*/
  475. PeriphClkInit->Clk48ClockSelection = __HAL_RCC_GET_CLK48_SOURCE();
  476. /* Get the SDIO clock configuration ----------------------------------------*/
  477. PeriphClkInit->SdioClockSelection = __HAL_RCC_GET_SDIO_SOURCE();
  478. /* Get the SPDIFRX clock configuration -------------------------------------*/
  479. PeriphClkInit->SpdifClockSelection = __HAL_RCC_GET_SPDIFRX_SOURCE();
  480. /* Get the TIM Prescaler configuration -------------------------------------*/
  481. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  482. {
  483. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  484. }
  485. else
  486. {
  487. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  488. }
  489. }
  490. /**
  491. * @brief Return the peripheral clock frequency for a given peripheral(SAI..)
  492. * @note Return 0 if peripheral clock identifier not managed by this API
  493. * @param PeriphClk Peripheral clock identifier
  494. * This parameter can be one of the following values:
  495. * @arg RCC_PERIPHCLK_SAI1: SAI1 peripheral clock
  496. * @arg RCC_PERIPHCLK_SAI2: SAI2 peripheral clock
  497. * @arg RCC_PERIPHCLK_I2S_APB1: I2S APB1 peripheral clock
  498. * @arg RCC_PERIPHCLK_I2S_APB2: I2S APB2 peripheral clock
  499. * @retval Frequency in KHz
  500. */
  501. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  502. {
  503. uint32_t tmpreg1 = 0U;
  504. /* This variable used to store the SAI clock frequency (value in Hz) */
  505. uint32_t frequency = 0U;
  506. /* This variable used to store the VCO Input (value in Hz) */
  507. uint32_t vcoinput = 0U;
  508. /* This variable used to store the SAI clock source */
  509. uint32_t saiclocksource = 0U;
  510. uint32_t srcclk = 0U;
  511. /* This variable used to store the VCO Output (value in Hz) */
  512. uint32_t vcooutput = 0U;
  513. switch (PeriphClk)
  514. {
  515. case RCC_PERIPHCLK_SAI1:
  516. case RCC_PERIPHCLK_SAI2:
  517. {
  518. saiclocksource = RCC->DCKCFGR;
  519. saiclocksource &= (RCC_DCKCFGR_SAI1SRC | RCC_DCKCFGR_SAI2SRC);
  520. switch (saiclocksource)
  521. {
  522. case 0U: /* PLLSAI is the clock source for SAI*/
  523. {
  524. /* Configure the PLLSAI division factor */
  525. /* PLLSAI_VCO Input = PLL_SOURCE/PLLSAIM */
  526. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  527. {
  528. /* In Case the PLL Source is HSI (Internal Clock) */
  529. vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIM));
  530. }
  531. else
  532. {
  533. /* In Case the PLL Source is HSE (External Clock) */
  534. vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIM)));
  535. }
  536. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  537. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  538. tmpreg1 = (RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> 24U;
  539. frequency = (vcoinput * ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> 6U))/(tmpreg1);
  540. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  541. tmpreg1 = (((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> 8U) + 1U);
  542. frequency = frequency/(tmpreg1);
  543. break;
  544. }
  545. case RCC_DCKCFGR_SAI1SRC_0: /* PLLI2S is the clock source for SAI*/
  546. case RCC_DCKCFGR_SAI2SRC_0: /* PLLI2S is the clock source for SAI*/
  547. {
  548. /* Configure the PLLI2S division factor */
  549. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  550. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  551. {
  552. /* In Case the PLL Source is HSI (Internal Clock) */
  553. vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  554. }
  555. else
  556. {
  557. /* In Case the PLL Source is HSE (External Clock) */
  558. vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM)));
  559. }
  560. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  561. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  562. tmpreg1 = (RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> 24U;
  563. frequency = (vcoinput * ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U))/(tmpreg1);
  564. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  565. tmpreg1 = ((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) + 1U);
  566. frequency = frequency/(tmpreg1);
  567. break;
  568. }
  569. case RCC_DCKCFGR_SAI1SRC_1: /* PLLR is the clock source for SAI*/
  570. case RCC_DCKCFGR_SAI2SRC_1: /* PLLR is the clock source for SAI*/
  571. {
  572. /* Configure the PLLI2S division factor */
  573. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  574. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  575. {
  576. /* In Case the PLL Source is HSI (Internal Clock) */
  577. vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  578. }
  579. else
  580. {
  581. /* In Case the PLL Source is HSE (External Clock) */
  582. vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM)));
  583. }
  584. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  585. /* SAI_CLK_x = PLL_VCO Output/PLLR */
  586. tmpreg1 = (RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U;
  587. frequency = (vcoinput * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U))/(tmpreg1);
  588. break;
  589. }
  590. case RCC_DCKCFGR_SAI1SRC: /* External clock is the clock source for SAI*/
  591. {
  592. frequency = EXTERNAL_CLOCK_VALUE;
  593. break;
  594. }
  595. case RCC_DCKCFGR_SAI2SRC: /* PLLSRC(HSE or HSI) is the clock source for SAI*/
  596. {
  597. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  598. {
  599. /* In Case the PLL Source is HSI (Internal Clock) */
  600. frequency = (uint32_t)(HSI_VALUE);
  601. }
  602. else
  603. {
  604. /* In Case the PLL Source is HSE (External Clock) */
  605. frequency = (uint32_t)(HSE_VALUE);
  606. }
  607. break;
  608. }
  609. default :
  610. {
  611. break;
  612. }
  613. }
  614. break;
  615. }
  616. case RCC_PERIPHCLK_I2S_APB1:
  617. {
  618. /* Get the current I2S source */
  619. srcclk = __HAL_RCC_GET_I2S_APB1_SOURCE();
  620. switch (srcclk)
  621. {
  622. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  623. case RCC_I2SAPB1CLKSOURCE_EXT:
  624. {
  625. /* Set the I2S clock to the external clock value */
  626. frequency = EXTERNAL_CLOCK_VALUE;
  627. break;
  628. }
  629. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  630. case RCC_I2SAPB1CLKSOURCE_PLLI2S:
  631. {
  632. /* Configure the PLLI2S division factor */
  633. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  634. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  635. {
  636. /* Get the I2S source clock value */
  637. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  638. }
  639. else
  640. {
  641. /* Get the I2S source clock value */
  642. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  643. }
  644. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  645. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  646. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  647. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  648. break;
  649. }
  650. /* Check if I2S clock selection is PLL VCO Output divided by PLLR used as I2S clock */
  651. case RCC_I2SAPB1CLKSOURCE_PLLR:
  652. {
  653. /* Configure the PLL division factor R */
  654. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  655. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  656. {
  657. /* Get the I2S source clock value */
  658. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  659. }
  660. else
  661. {
  662. /* Get the I2S source clock value */
  663. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  664. }
  665. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  666. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U) & (RCC_PLLCFGR_PLLN >> 6U)));
  667. /* I2S_CLK = PLL_VCO Output/PLLR */
  668. frequency = (uint32_t)(vcooutput /(((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U) & (RCC_PLLCFGR_PLLR >> 28U)));
  669. break;
  670. }
  671. /* Check if I2S clock selection is HSI or HSE depending from PLL source Clock */
  672. case RCC_I2SAPB1CLKSOURCE_PLLSRC:
  673. {
  674. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  675. {
  676. frequency = HSE_VALUE;
  677. }
  678. else
  679. {
  680. frequency = HSI_VALUE;
  681. }
  682. break;
  683. }
  684. /* Clock not enabled for I2S*/
  685. default:
  686. {
  687. frequency = 0U;
  688. break;
  689. }
  690. }
  691. break;
  692. }
  693. case RCC_PERIPHCLK_I2S_APB2:
  694. {
  695. /* Get the current I2S source */
  696. srcclk = __HAL_RCC_GET_I2S_APB2_SOURCE();
  697. switch (srcclk)
  698. {
  699. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  700. case RCC_I2SAPB2CLKSOURCE_EXT:
  701. {
  702. /* Set the I2S clock to the external clock value */
  703. frequency = EXTERNAL_CLOCK_VALUE;
  704. break;
  705. }
  706. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  707. case RCC_I2SAPB2CLKSOURCE_PLLI2S:
  708. {
  709. /* Configure the PLLI2S division factor */
  710. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  711. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  712. {
  713. /* Get the I2S source clock value */
  714. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  715. }
  716. else
  717. {
  718. /* Get the I2S source clock value */
  719. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  720. }
  721. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  722. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  723. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  724. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  725. break;
  726. }
  727. /* Check if I2S clock selection is PLL VCO Output divided by PLLR used as I2S clock */
  728. case RCC_I2SAPB2CLKSOURCE_PLLR:
  729. {
  730. /* Configure the PLL division factor R */
  731. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  732. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  733. {
  734. /* Get the I2S source clock value */
  735. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  736. }
  737. else
  738. {
  739. /* Get the I2S source clock value */
  740. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  741. }
  742. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  743. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U) & (RCC_PLLCFGR_PLLN >> 6U)));
  744. /* I2S_CLK = PLL_VCO Output/PLLR */
  745. frequency = (uint32_t)(vcooutput /(((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U) & (RCC_PLLCFGR_PLLR >> 28U)));
  746. break;
  747. }
  748. /* Check if I2S clock selection is HSI or HSE depending from PLL source Clock */
  749. case RCC_I2SAPB2CLKSOURCE_PLLSRC:
  750. {
  751. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  752. {
  753. frequency = HSE_VALUE;
  754. }
  755. else
  756. {
  757. frequency = HSI_VALUE;
  758. }
  759. break;
  760. }
  761. /* Clock not enabled for I2S*/
  762. default:
  763. {
  764. frequency = 0U;
  765. break;
  766. }
  767. }
  768. break;
  769. }
  770. }
  771. return frequency;
  772. }
  773. #endif /* STM32F446xx */
  774. #if defined(STM32F469xx) || defined(STM32F479xx)
  775. /**
  776. * @brief Initializes the RCC extended peripherals clocks according to the specified
  777. * parameters in the RCC_PeriphCLKInitTypeDef.
  778. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  779. * contains the configuration information for the Extended Peripherals
  780. * clocks(I2S, SAI, LTDC, RTC and TIM).
  781. *
  782. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  783. * the RTC clock source; in this case the Backup domain will be reset in
  784. * order to modify the RTC Clock source, as consequence RTC registers (including
  785. * the backup registers) and RCC_BDCR register are set to their reset values.
  786. *
  787. * @retval HAL status
  788. */
  789. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  790. {
  791. uint32_t tickstart = 0U;
  792. uint32_t tmpreg1 = 0U;
  793. uint32_t pllsaip = 0U;
  794. uint32_t pllsaiq = 0U;
  795. uint32_t pllsair = 0U;
  796. /* Check the parameters */
  797. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  798. /*--------------------------- CLK48 Configuration --------------------------*/
  799. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48)
  800. {
  801. /* Check the parameters */
  802. assert_param(IS_RCC_CLK48CLKSOURCE(PeriphClkInit->Clk48ClockSelection));
  803. /* Configure the CLK48 clock source */
  804. __HAL_RCC_CLK48_CONFIG(PeriphClkInit->Clk48ClockSelection);
  805. }
  806. /*--------------------------------------------------------------------------*/
  807. /*------------------------------ SDIO Configuration ------------------------*/
  808. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDIO) == RCC_PERIPHCLK_SDIO)
  809. {
  810. /* Check the parameters */
  811. assert_param(IS_RCC_SDIOCLKSOURCE(PeriphClkInit->SdioClockSelection));
  812. /* Configure the SDIO clock source */
  813. __HAL_RCC_SDIO_CONFIG(PeriphClkInit->SdioClockSelection);
  814. }
  815. /*--------------------------------------------------------------------------*/
  816. /*----------------------- SAI/I2S Configuration (PLLI2S) -------------------*/
  817. /*------------------- Common configuration SAI/I2S -------------------------*/
  818. /* In Case of SAI or I2S Clock Configuration through PLLI2S, PLLI2SN division
  819. factor is common parameters for both peripherals */
  820. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == RCC_PERIPHCLK_I2S) ||
  821. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLI2S) == RCC_PERIPHCLK_SAI_PLLI2S) ||
  822. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S))
  823. {
  824. /* check for Parameters */
  825. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  826. /* Disable the PLLI2S */
  827. __HAL_RCC_PLLI2S_DISABLE();
  828. /* Get tick */
  829. tickstart = HAL_GetTick();
  830. /* Wait till PLLI2S is disabled */
  831. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  832. {
  833. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  834. {
  835. /* return in case of Timeout detected */
  836. return HAL_TIMEOUT;
  837. }
  838. }
  839. /*---------------------- I2S configuration -------------------------------*/
  840. /* In Case of I2S Clock Configuration through PLLI2S, PLLI2SR must be added
  841. only for I2S configuration */
  842. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == (RCC_PERIPHCLK_I2S))
  843. {
  844. /* check for Parameters */
  845. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  846. /* Configure the PLLI2S division factors */
  847. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) x (PLLI2SN/PLLM) */
  848. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  849. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SR);
  850. }
  851. /*---------------------------- SAI configuration -------------------------*/
  852. /* In Case of SAI Clock Configuration through PLLI2S, PLLI2SQ and PLLI2S_DIVQ must
  853. be added only for SAI configuration */
  854. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLI2S) == (RCC_PERIPHCLK_SAI_PLLI2S))
  855. {
  856. /* Check the PLLI2S division factors */
  857. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  858. assert_param(IS_RCC_PLLI2S_DIVQ_VALUE(PeriphClkInit->PLLI2SDivQ));
  859. /* Read PLLI2SR value from PLLI2SCFGR register (this value is not need for SAI configuration) */
  860. tmpreg1 = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  861. /* Configure the PLLI2S division factors */
  862. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  863. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  864. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  865. __HAL_RCC_PLLI2S_SAICLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SQ , tmpreg1);
  866. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  867. __HAL_RCC_PLLI2S_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLI2SDivQ);
  868. }
  869. /*----------------- In Case of PLLI2S is just selected -----------------*/
  870. if((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S)
  871. {
  872. /* Check for Parameters */
  873. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  874. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  875. /* Configure the PLLI2S multiplication and division factors */
  876. __HAL_RCC_PLLI2S_SAICLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN, PeriphClkInit->PLLI2S.PLLI2SQ, PeriphClkInit->PLLI2S.PLLI2SR);
  877. }
  878. /* Enable the PLLI2S */
  879. __HAL_RCC_PLLI2S_ENABLE();
  880. /* Get tick */
  881. tickstart = HAL_GetTick();
  882. /* Wait till PLLI2S is ready */
  883. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  884. {
  885. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  886. {
  887. /* return in case of Timeout detected */
  888. return HAL_TIMEOUT;
  889. }
  890. }
  891. }
  892. /*--------------------------------------------------------------------------*/
  893. /*----------------------- SAI/LTDC Configuration (PLLSAI) ------------------*/
  894. /*----------------------- Common configuration SAI/LTDC --------------------*/
  895. /* In Case of SAI, LTDC or CLK48 Clock Configuration through PLLSAI, PLLSAIN division
  896. factor is common parameters for these peripherals */
  897. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLSAI) == RCC_PERIPHCLK_SAI_PLLSAI) ||
  898. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == RCC_PERIPHCLK_LTDC) ||
  899. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48) &&
  900. (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLSAIP)))
  901. {
  902. /* Check the PLLSAI division factors */
  903. assert_param(IS_RCC_PLLSAIN_VALUE(PeriphClkInit->PLLSAI.PLLSAIN));
  904. /* Disable PLLSAI Clock */
  905. __HAL_RCC_PLLSAI_DISABLE();
  906. /* Get tick */
  907. tickstart = HAL_GetTick();
  908. /* Wait till PLLSAI is disabled */
  909. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  910. {
  911. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  912. {
  913. /* return in case of Timeout detected */
  914. return HAL_TIMEOUT;
  915. }
  916. }
  917. /*---------------------------- SAI configuration -------------------------*/
  918. /* In Case of SAI Clock Configuration through PLLSAI, PLLSAIQ and PLLSAI_DIVQ must
  919. be added only for SAI configuration */
  920. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLSAI) == (RCC_PERIPHCLK_SAI_PLLSAI))
  921. {
  922. assert_param(IS_RCC_PLLSAIQ_VALUE(PeriphClkInit->PLLSAI.PLLSAIQ));
  923. assert_param(IS_RCC_PLLSAI_DIVQ_VALUE(PeriphClkInit->PLLSAIDivQ));
  924. /* Read PLLSAIP value from PLLSAICFGR register (this value is not needed for SAI configuration) */
  925. pllsaip = ((((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIP) >> RCC_PLLSAICFGR_PLLSAIP_Pos) + 1U) << 1U);
  926. /* Read PLLSAIR value from PLLSAICFGR register (this value is not need for SAI configuration) */
  927. pllsair = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> RCC_PLLSAICFGR_PLLSAIR_Pos);
  928. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  929. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  930. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  931. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN, pllsaip, PeriphClkInit->PLLSAI.PLLSAIQ, pllsair);
  932. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  933. __HAL_RCC_PLLSAI_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLSAIDivQ);
  934. }
  935. /*---------------------------- LTDC configuration ------------------------*/
  936. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == (RCC_PERIPHCLK_LTDC))
  937. {
  938. assert_param(IS_RCC_PLLSAIR_VALUE(PeriphClkInit->PLLSAI.PLLSAIR));
  939. assert_param(IS_RCC_PLLSAI_DIVR_VALUE(PeriphClkInit->PLLSAIDivR));
  940. /* Read PLLSAIP value from PLLSAICFGR register (this value is not needed for SAI configuration) */
  941. pllsaip = ((((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIP) >> RCC_PLLSAICFGR_PLLSAIP_Pos) + 1U) << 1U);
  942. /* Read PLLSAIQ value from PLLSAICFGR register (this value is not need for SAI configuration) */
  943. pllsaiq = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  944. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  945. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  946. /* LTDC_CLK(first level) = PLLSAI_VCO Output/PLLSAIR */
  947. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN, pllsaip, pllsaiq, PeriphClkInit->PLLSAI.PLLSAIR);
  948. /* LTDC_CLK = LTDC_CLK(first level)/PLLSAIDIVR */
  949. __HAL_RCC_PLLSAI_PLLSAICLKDIVR_CONFIG(PeriphClkInit->PLLSAIDivR);
  950. }
  951. /*---------------------------- CLK48 configuration ------------------------*/
  952. /* Configure the PLLSAI when it is used as clock source for CLK48 */
  953. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == (RCC_PERIPHCLK_CLK48)) &&
  954. (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLSAIP))
  955. {
  956. assert_param(IS_RCC_PLLSAIP_VALUE(PeriphClkInit->PLLSAI.PLLSAIP));
  957. /* Read PLLSAIQ value from PLLSAICFGR register (this value is not need for SAI configuration) */
  958. pllsaiq = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  959. /* Read PLLSAIR value from PLLSAICFGR register (this value is not need for SAI configuration) */
  960. pllsair = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> RCC_PLLSAICFGR_PLLSAIR_Pos);
  961. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  962. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  963. /* CLK48_CLK(first level) = PLLSAI_VCO Output/PLLSAIP */
  964. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN, PeriphClkInit->PLLSAI.PLLSAIP, pllsaiq, pllsair);
  965. }
  966. /* Enable PLLSAI Clock */
  967. __HAL_RCC_PLLSAI_ENABLE();
  968. /* Get tick */
  969. tickstart = HAL_GetTick();
  970. /* Wait till PLLSAI is ready */
  971. while(__HAL_RCC_PLLSAI_GET_FLAG() == RESET)
  972. {
  973. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  974. {
  975. /* return in case of Timeout detected */
  976. return HAL_TIMEOUT;
  977. }
  978. }
  979. }
  980. /*--------------------------------------------------------------------------*/
  981. /*---------------------------- RTC configuration ---------------------------*/
  982. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  983. {
  984. /* Check for RTC Parameters used to output RTCCLK */
  985. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  986. /* Enable Power Clock*/
  987. __HAL_RCC_PWR_CLK_ENABLE();
  988. /* Enable write access to Backup domain */
  989. PWR->CR |= PWR_CR_DBP;
  990. /* Get tick */
  991. tickstart = HAL_GetTick();
  992. while((PWR->CR & PWR_CR_DBP) == RESET)
  993. {
  994. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  995. {
  996. return HAL_TIMEOUT;
  997. }
  998. }
  999. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  1000. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  1001. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  1002. {
  1003. /* Store the content of BDCR register before the reset of Backup Domain */
  1004. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  1005. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  1006. __HAL_RCC_BACKUPRESET_FORCE();
  1007. __HAL_RCC_BACKUPRESET_RELEASE();
  1008. /* Restore the Content of BDCR register */
  1009. RCC->BDCR = tmpreg1;
  1010. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  1011. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  1012. {
  1013. /* Get tick */
  1014. tickstart = HAL_GetTick();
  1015. /* Wait till LSE is ready */
  1016. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  1017. {
  1018. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  1019. {
  1020. return HAL_TIMEOUT;
  1021. }
  1022. }
  1023. }
  1024. }
  1025. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  1026. }
  1027. /*--------------------------------------------------------------------------*/
  1028. /*---------------------------- TIM configuration ---------------------------*/
  1029. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  1030. {
  1031. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  1032. }
  1033. return HAL_OK;
  1034. }
  1035. /**
  1036. * @brief Configures the RCC_PeriphCLKInitTypeDef according to the internal
  1037. * RCC configuration registers.
  1038. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  1039. * will be configured.
  1040. * @retval None
  1041. */
  1042. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1043. {
  1044. uint32_t tempreg;
  1045. /* Set all possible values for the extended clock type parameter------------*/
  1046. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S | RCC_PERIPHCLK_SAI_PLLSAI |\
  1047. RCC_PERIPHCLK_SAI_PLLI2S | RCC_PERIPHCLK_LTDC |\
  1048. RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC |\
  1049. RCC_PERIPHCLK_CLK48 | RCC_PERIPHCLK_SDIO;
  1050. /* Get the PLLI2S Clock configuration --------------------------------------*/
  1051. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> RCC_PLLI2SCFGR_PLLI2SN_Pos);
  1052. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  1053. PeriphClkInit->PLLI2S.PLLI2SQ = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> RCC_PLLI2SCFGR_PLLI2SQ_Pos);
  1054. /* Get the PLLSAI Clock configuration --------------------------------------*/
  1055. PeriphClkInit->PLLSAI.PLLSAIN = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> RCC_PLLSAICFGR_PLLSAIN_Pos);
  1056. PeriphClkInit->PLLSAI.PLLSAIR = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> RCC_PLLSAICFGR_PLLSAIR_Pos);
  1057. PeriphClkInit->PLLSAI.PLLSAIQ = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  1058. /* Get the PLLSAI/PLLI2S division factors ----------------------------------*/
  1059. PeriphClkInit->PLLI2SDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) >> RCC_DCKCFGR_PLLI2SDIVQ_Pos);
  1060. PeriphClkInit->PLLSAIDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> RCC_DCKCFGR_PLLSAIDIVQ_Pos);
  1061. PeriphClkInit->PLLSAIDivR = (uint32_t)(RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVR);
  1062. /* Get the RTC Clock configuration -----------------------------------------*/
  1063. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  1064. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  1065. /* Get the CLK48 clock configuration -------------------------------------*/
  1066. PeriphClkInit->Clk48ClockSelection = __HAL_RCC_GET_CLK48_SOURCE();
  1067. /* Get the SDIO clock configuration ----------------------------------------*/
  1068. PeriphClkInit->SdioClockSelection = __HAL_RCC_GET_SDIO_SOURCE();
  1069. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  1070. {
  1071. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  1072. }
  1073. else
  1074. {
  1075. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  1076. }
  1077. }
  1078. /**
  1079. * @brief Return the peripheral clock frequency for a given peripheral(SAI..)
  1080. * @note Return 0 if peripheral clock identifier not managed by this API
  1081. * @param PeriphClk Peripheral clock identifier
  1082. * This parameter can be one of the following values:
  1083. * @arg RCC_PERIPHCLK_I2S: I2S peripheral clock
  1084. * @retval Frequency in KHz
  1085. */
  1086. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  1087. {
  1088. /* This variable used to store the I2S clock frequency (value in Hz) */
  1089. uint32_t frequency = 0U;
  1090. /* This variable used to store the VCO Input (value in Hz) */
  1091. uint32_t vcoinput = 0U;
  1092. uint32_t srcclk = 0U;
  1093. /* This variable used to store the VCO Output (value in Hz) */
  1094. uint32_t vcooutput = 0U;
  1095. switch (PeriphClk)
  1096. {
  1097. case RCC_PERIPHCLK_I2S:
  1098. {
  1099. /* Get the current I2S source */
  1100. srcclk = __HAL_RCC_GET_I2S_SOURCE();
  1101. switch (srcclk)
  1102. {
  1103. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  1104. case RCC_I2SCLKSOURCE_EXT:
  1105. {
  1106. /* Set the I2S clock to the external clock value */
  1107. frequency = EXTERNAL_CLOCK_VALUE;
  1108. break;
  1109. }
  1110. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  1111. case RCC_I2SCLKSOURCE_PLLI2S:
  1112. {
  1113. /* Configure the PLLI2S division factor */
  1114. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  1115. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1116. {
  1117. /* Get the I2S source clock value */
  1118. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1119. }
  1120. else
  1121. {
  1122. /* Get the I2S source clock value */
  1123. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1124. }
  1125. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  1126. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  1127. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  1128. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  1129. break;
  1130. }
  1131. /* Clock not enabled for I2S*/
  1132. default:
  1133. {
  1134. frequency = 0U;
  1135. break;
  1136. }
  1137. }
  1138. break;
  1139. }
  1140. }
  1141. return frequency;
  1142. }
  1143. #endif /* STM32F469xx || STM32F479xx */
  1144. #if defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  1145. /**
  1146. * @brief Initializes the RCC extended peripherals clocks according to the specified
  1147. * parameters in the RCC_PeriphCLKInitTypeDef.
  1148. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  1149. * contains the configuration information for the Extended Peripherals
  1150. * clocks(I2S, LTDC RTC and TIM).
  1151. *
  1152. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  1153. * the RTC clock source; in this case the Backup domain will be reset in
  1154. * order to modify the RTC Clock source, as consequence RTC registers (including
  1155. * the backup registers) and RCC_BDCR register are set to their reset values.
  1156. *
  1157. * @retval HAL status
  1158. */
  1159. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1160. {
  1161. uint32_t tickstart = 0U;
  1162. uint32_t tmpreg1 = 0U;
  1163. #if defined(STM32F413xx) || defined(STM32F423xx)
  1164. uint32_t plli2sq = 0U;
  1165. #endif /* STM32F413xx || STM32F423xx */
  1166. uint32_t plli2sused = 0U;
  1167. /* Check the peripheral clock selection parameters */
  1168. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  1169. /*----------------------------------- I2S APB1 configuration ---------------*/
  1170. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB1) == (RCC_PERIPHCLK_I2S_APB1))
  1171. {
  1172. /* Check the parameters */
  1173. assert_param(IS_RCC_I2SAPB1CLKSOURCE(PeriphClkInit->I2sApb1ClockSelection));
  1174. /* Configure I2S Clock source */
  1175. __HAL_RCC_I2S_APB1_CONFIG(PeriphClkInit->I2sApb1ClockSelection);
  1176. /* Enable the PLLI2S when it's used as clock source for I2S */
  1177. if(PeriphClkInit->I2sApb1ClockSelection == RCC_I2SAPB1CLKSOURCE_PLLI2S)
  1178. {
  1179. plli2sused = 1U;
  1180. }
  1181. }
  1182. /*--------------------------------------------------------------------------*/
  1183. /*----------------------------------- I2S APB2 configuration ---------------*/
  1184. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB2) == (RCC_PERIPHCLK_I2S_APB2))
  1185. {
  1186. /* Check the parameters */
  1187. assert_param(IS_RCC_I2SAPB2CLKSOURCE(PeriphClkInit->I2sApb2ClockSelection));
  1188. /* Configure I2S Clock source */
  1189. __HAL_RCC_I2S_APB2_CONFIG(PeriphClkInit->I2sApb2ClockSelection);
  1190. /* Enable the PLLI2S when it's used as clock source for I2S */
  1191. if(PeriphClkInit->I2sApb2ClockSelection == RCC_I2SAPB2CLKSOURCE_PLLI2S)
  1192. {
  1193. plli2sused = 1U;
  1194. }
  1195. }
  1196. /*--------------------------------------------------------------------------*/
  1197. #if defined(STM32F413xx) || defined(STM32F423xx)
  1198. /*----------------------- SAI1 Block A configuration -----------------------*/
  1199. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAIA) == (RCC_PERIPHCLK_SAIA))
  1200. {
  1201. /* Check the parameters */
  1202. assert_param(IS_RCC_SAIACLKSOURCE(PeriphClkInit->SaiAClockSelection));
  1203. /* Configure SAI1 Clock source */
  1204. __HAL_RCC_SAI_BLOCKACLKSOURCE_CONFIG(PeriphClkInit->SaiAClockSelection);
  1205. /* Enable the PLLI2S when it's used as clock source for SAI */
  1206. if(PeriphClkInit->SaiAClockSelection == RCC_SAIACLKSOURCE_PLLI2SR)
  1207. {
  1208. plli2sused = 1U;
  1209. }
  1210. /* Enable the PLLSAI when it's used as clock source for SAI */
  1211. if(PeriphClkInit->SaiAClockSelection == RCC_SAIACLKSOURCE_PLLR)
  1212. {
  1213. /* Check for PLL/DIVR parameters */
  1214. assert_param(IS_RCC_PLL_DIVR_VALUE(PeriphClkInit->PLLDivR));
  1215. /* SAI_CLK_x = SAI_CLK(first level)/PLLDIVR */
  1216. __HAL_RCC_PLL_PLLSAICLKDIVR_CONFIG(PeriphClkInit->PLLDivR);
  1217. }
  1218. }
  1219. /*--------------------------------------------------------------------------*/
  1220. /*---------------------- SAI1 Block B configuration ------------------------*/
  1221. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAIB) == (RCC_PERIPHCLK_SAIB))
  1222. {
  1223. /* Check the parameters */
  1224. assert_param(IS_RCC_SAIBCLKSOURCE(PeriphClkInit->SaiBClockSelection));
  1225. /* Configure SAI1 Clock source */
  1226. __HAL_RCC_SAI_BLOCKBCLKSOURCE_CONFIG(PeriphClkInit->SaiBClockSelection);
  1227. /* Enable the PLLI2S when it's used as clock source for SAI */
  1228. if(PeriphClkInit->SaiBClockSelection == RCC_SAIBCLKSOURCE_PLLI2SR)
  1229. {
  1230. plli2sused = 1U;
  1231. }
  1232. /* Enable the PLLSAI when it's used as clock source for SAI */
  1233. if(PeriphClkInit->SaiBClockSelection == RCC_SAIBCLKSOURCE_PLLR)
  1234. {
  1235. /* Check for PLL/DIVR parameters */
  1236. assert_param(IS_RCC_PLL_DIVR_VALUE(PeriphClkInit->PLLDivR));
  1237. /* SAI_CLK_x = SAI_CLK(first level)/PLLDIVR */
  1238. __HAL_RCC_PLL_PLLSAICLKDIVR_CONFIG(PeriphClkInit->PLLDivR);
  1239. }
  1240. }
  1241. /*--------------------------------------------------------------------------*/
  1242. #endif /* STM32F413xx || STM32F423xx */
  1243. /*------------------------------------ RTC configuration -------------------*/
  1244. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  1245. {
  1246. /* Check for RTC Parameters used to output RTCCLK */
  1247. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  1248. /* Enable Power Clock*/
  1249. __HAL_RCC_PWR_CLK_ENABLE();
  1250. /* Enable write access to Backup domain */
  1251. PWR->CR |= PWR_CR_DBP;
  1252. /* Get tick */
  1253. tickstart = HAL_GetTick();
  1254. while((PWR->CR & PWR_CR_DBP) == RESET)
  1255. {
  1256. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  1257. {
  1258. return HAL_TIMEOUT;
  1259. }
  1260. }
  1261. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  1262. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  1263. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  1264. {
  1265. /* Store the content of BDCR register before the reset of Backup Domain */
  1266. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  1267. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  1268. __HAL_RCC_BACKUPRESET_FORCE();
  1269. __HAL_RCC_BACKUPRESET_RELEASE();
  1270. /* Restore the Content of BDCR register */
  1271. RCC->BDCR = tmpreg1;
  1272. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  1273. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  1274. {
  1275. /* Get tick */
  1276. tickstart = HAL_GetTick();
  1277. /* Wait till LSE is ready */
  1278. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  1279. {
  1280. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  1281. {
  1282. return HAL_TIMEOUT;
  1283. }
  1284. }
  1285. }
  1286. }
  1287. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  1288. }
  1289. /*--------------------------------------------------------------------------*/
  1290. /*------------------------------------ TIM configuration -------------------*/
  1291. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  1292. {
  1293. /* Configure Timer Prescaler */
  1294. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  1295. }
  1296. /*--------------------------------------------------------------------------*/
  1297. /*------------------------------------- FMPI2C1 Configuration --------------*/
  1298. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_FMPI2C1) == RCC_PERIPHCLK_FMPI2C1)
  1299. {
  1300. /* Check the parameters */
  1301. assert_param(IS_RCC_FMPI2C1CLKSOURCE(PeriphClkInit->Fmpi2c1ClockSelection));
  1302. /* Configure the FMPI2C1 clock source */
  1303. __HAL_RCC_FMPI2C1_CONFIG(PeriphClkInit->Fmpi2c1ClockSelection);
  1304. }
  1305. /*--------------------------------------------------------------------------*/
  1306. /*------------------------------------- CLK48 Configuration ----------------*/
  1307. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48)
  1308. {
  1309. /* Check the parameters */
  1310. assert_param(IS_RCC_CLK48CLKSOURCE(PeriphClkInit->Clk48ClockSelection));
  1311. /* Configure the SDIO clock source */
  1312. __HAL_RCC_CLK48_CONFIG(PeriphClkInit->Clk48ClockSelection);
  1313. /* Enable the PLLI2S when it's used as clock source for CLK48 */
  1314. if(PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLI2SQ)
  1315. {
  1316. plli2sused = 1U;
  1317. }
  1318. }
  1319. /*--------------------------------------------------------------------------*/
  1320. /*------------------------------------- SDIO Configuration -----------------*/
  1321. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDIO) == RCC_PERIPHCLK_SDIO)
  1322. {
  1323. /* Check the parameters */
  1324. assert_param(IS_RCC_SDIOCLKSOURCE(PeriphClkInit->SdioClockSelection));
  1325. /* Configure the SDIO clock source */
  1326. __HAL_RCC_SDIO_CONFIG(PeriphClkInit->SdioClockSelection);
  1327. }
  1328. /*--------------------------------------------------------------------------*/
  1329. /*-------------------------------------- PLLI2S Configuration --------------*/
  1330. /* PLLI2S is configured when a peripheral will use it as source clock : I2S on APB1 or
  1331. I2S on APB2*/
  1332. if((plli2sused == 1U) || (PeriphClkInit->PeriphClockSelection == RCC_PERIPHCLK_PLLI2S))
  1333. {
  1334. /* Disable the PLLI2S */
  1335. __HAL_RCC_PLLI2S_DISABLE();
  1336. /* Get tick */
  1337. tickstart = HAL_GetTick();
  1338. /* Wait till PLLI2S is disabled */
  1339. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  1340. {
  1341. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  1342. {
  1343. /* return in case of Timeout detected */
  1344. return HAL_TIMEOUT;
  1345. }
  1346. }
  1347. /* check for common PLLI2S Parameters */
  1348. assert_param(IS_RCC_PLLI2SCLKSOURCE(PeriphClkInit->PLLI2SSelection));
  1349. assert_param(IS_RCC_PLLI2SM_VALUE(PeriphClkInit->PLLI2S.PLLI2SM));
  1350. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  1351. /*-------------------- Set the PLL I2S clock -----------------------------*/
  1352. __HAL_RCC_PLL_I2S_CONFIG(PeriphClkInit->PLLI2SSelection);
  1353. /*------- In Case of PLLI2S is selected as source clock for I2S ----------*/
  1354. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB1) == RCC_PERIPHCLK_I2S_APB1) && (PeriphClkInit->I2sApb1ClockSelection == RCC_I2SAPB1CLKSOURCE_PLLI2S)) ||
  1355. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB2) == RCC_PERIPHCLK_I2S_APB2) && (PeriphClkInit->I2sApb2ClockSelection == RCC_I2SAPB2CLKSOURCE_PLLI2S)) ||
  1356. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48) && (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLI2SQ)) ||
  1357. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDIO) == RCC_PERIPHCLK_SDIO) && (PeriphClkInit->SdioClockSelection == RCC_SDIOCLKSOURCE_CLK48) && (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLI2SQ)))
  1358. {
  1359. /* check for Parameters */
  1360. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  1361. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  1362. /* Configure the PLLI2S division factors */
  1363. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM)*/
  1364. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  1365. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SQ, PeriphClkInit->PLLI2S.PLLI2SR);
  1366. }
  1367. #if defined(STM32F413xx) || defined(STM32F423xx)
  1368. /*------- In Case of PLLI2S is selected as source clock for SAI ----------*/
  1369. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAIA) == RCC_PERIPHCLK_SAIA) && (PeriphClkInit->SaiAClockSelection == RCC_SAIACLKSOURCE_PLLI2SR)) ||
  1370. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAIB) == RCC_PERIPHCLK_SAIB) && (PeriphClkInit->SaiBClockSelection == RCC_SAIBCLKSOURCE_PLLI2SR)))
  1371. {
  1372. /* Check for PLLI2S Parameters */
  1373. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  1374. /* Check for PLLI2S/DIVR parameters */
  1375. assert_param(IS_RCC_PLLI2S_DIVR_VALUE(PeriphClkInit->PLLI2SDivR));
  1376. /* Read PLLI2SQ value from PLLI2SCFGR register (this value is not needed for SAI configuration) */
  1377. plli2sq = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> RCC_PLLI2SCFGR_PLLI2SQ_Pos);
  1378. /* Configure the PLLI2S division factors */
  1379. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  1380. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  1381. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  1382. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN, plli2sq, PeriphClkInit->PLLI2S.PLLI2SR);
  1383. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVR */
  1384. __HAL_RCC_PLLI2S_PLLSAICLKDIVR_CONFIG(PeriphClkInit->PLLI2SDivR);
  1385. }
  1386. #endif /* STM32F413xx || STM32F423xx */
  1387. /*----------------- In Case of PLLI2S is just selected ------------------*/
  1388. if((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S)
  1389. {
  1390. /* Check for Parameters */
  1391. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  1392. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  1393. /* Configure the PLLI2S division factors */
  1394. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM)*/
  1395. /* SPDIFRXCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SP */
  1396. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SQ, PeriphClkInit->PLLI2S.PLLI2SR);
  1397. }
  1398. /* Enable the PLLI2S */
  1399. __HAL_RCC_PLLI2S_ENABLE();
  1400. /* Get tick */
  1401. tickstart = HAL_GetTick();
  1402. /* Wait till PLLI2S is ready */
  1403. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  1404. {
  1405. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  1406. {
  1407. /* return in case of Timeout detected */
  1408. return HAL_TIMEOUT;
  1409. }
  1410. }
  1411. }
  1412. /*--------------------------------------------------------------------------*/
  1413. /*-------------------- DFSDM1 clock source configuration -------------------*/
  1414. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM1) == RCC_PERIPHCLK_DFSDM1)
  1415. {
  1416. /* Check the parameters */
  1417. assert_param(IS_RCC_DFSDM1CLKSOURCE(PeriphClkInit->Dfsdm1ClockSelection));
  1418. /* Configure the DFSDM1 interface clock source */
  1419. __HAL_RCC_DFSDM1_CONFIG(PeriphClkInit->Dfsdm1ClockSelection);
  1420. }
  1421. /*--------------------------------------------------------------------------*/
  1422. /*-------------------- DFSDM1 Audio clock source configuration -------------*/
  1423. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM1_AUDIO) == RCC_PERIPHCLK_DFSDM1_AUDIO)
  1424. {
  1425. /* Check the parameters */
  1426. assert_param(IS_RCC_DFSDM1AUDIOCLKSOURCE(PeriphClkInit->Dfsdm1AudioClockSelection));
  1427. /* Configure the DFSDM1 Audio interface clock source */
  1428. __HAL_RCC_DFSDM1AUDIO_CONFIG(PeriphClkInit->Dfsdm1AudioClockSelection);
  1429. }
  1430. /*--------------------------------------------------------------------------*/
  1431. #if defined(STM32F413xx) || defined(STM32F423xx)
  1432. /*-------------------- DFSDM2 clock source configuration -------------------*/
  1433. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM2) == RCC_PERIPHCLK_DFSDM2)
  1434. {
  1435. /* Check the parameters */
  1436. assert_param(IS_RCC_DFSDM2CLKSOURCE(PeriphClkInit->Dfsdm2ClockSelection));
  1437. /* Configure the DFSDM1 interface clock source */
  1438. __HAL_RCC_DFSDM2_CONFIG(PeriphClkInit->Dfsdm2ClockSelection);
  1439. }
  1440. /*--------------------------------------------------------------------------*/
  1441. /*-------------------- DFSDM2 Audio clock source configuration -------------*/
  1442. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM2_AUDIO) == RCC_PERIPHCLK_DFSDM2_AUDIO)
  1443. {
  1444. /* Check the parameters */
  1445. assert_param(IS_RCC_DFSDM2AUDIOCLKSOURCE(PeriphClkInit->Dfsdm2AudioClockSelection));
  1446. /* Configure the DFSDM1 Audio interface clock source */
  1447. __HAL_RCC_DFSDM2AUDIO_CONFIG(PeriphClkInit->Dfsdm2AudioClockSelection);
  1448. }
  1449. /*--------------------------------------------------------------------------*/
  1450. /*---------------------------- LPTIM1 Configuration ------------------------*/
  1451. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM1) == RCC_PERIPHCLK_LPTIM1)
  1452. {
  1453. /* Check the parameters */
  1454. assert_param(IS_RCC_LPTIM1CLKSOURCE(PeriphClkInit->Lptim1ClockSelection));
  1455. /* Configure the LPTIM1 clock source */
  1456. __HAL_RCC_LPTIM1_CONFIG(PeriphClkInit->Lptim1ClockSelection);
  1457. }
  1458. /*--------------------------------------------------------------------------*/
  1459. #endif /* STM32F413xx || STM32F423xx */
  1460. return HAL_OK;
  1461. }
  1462. /**
  1463. * @brief Get the RCC_PeriphCLKInitTypeDef according to the internal
  1464. * RCC configuration registers.
  1465. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  1466. * will be configured.
  1467. * @retval None
  1468. */
  1469. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1470. {
  1471. uint32_t tempreg;
  1472. /* Set all possible values for the extended clock type parameter------------*/
  1473. #if defined(STM32F413xx) || defined(STM32F423xx)
  1474. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_I2S_APB2 |\
  1475. RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC |\
  1476. RCC_PERIPHCLK_FMPI2C1 | RCC_PERIPHCLK_CLK48 |\
  1477. RCC_PERIPHCLK_SDIO | RCC_PERIPHCLK_DFSDM1 |\
  1478. RCC_PERIPHCLK_DFSDM1_AUDIO | RCC_PERIPHCLK_DFSDM2 |\
  1479. RCC_PERIPHCLK_DFSDM2_AUDIO | RCC_PERIPHCLK_LPTIM1 |\
  1480. RCC_PERIPHCLK_SAIA | RCC_PERIPHCLK_SAIB;
  1481. #else /* STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx */
  1482. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_I2S_APB2 |\
  1483. RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC |\
  1484. RCC_PERIPHCLK_FMPI2C1 | RCC_PERIPHCLK_CLK48 |\
  1485. RCC_PERIPHCLK_SDIO | RCC_PERIPHCLK_DFSDM1 |\
  1486. RCC_PERIPHCLK_DFSDM1_AUDIO;
  1487. #endif /* STM32F413xx || STM32F423xx */
  1488. /* Get the PLLI2S Clock configuration --------------------------------------*/
  1489. PeriphClkInit->PLLI2S.PLLI2SM = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM) >> RCC_PLLI2SCFGR_PLLI2SM_Pos);
  1490. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> RCC_PLLI2SCFGR_PLLI2SN_Pos);
  1491. PeriphClkInit->PLLI2S.PLLI2SQ = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> RCC_PLLI2SCFGR_PLLI2SQ_Pos);
  1492. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  1493. #if defined(STM32F413xx) || defined(STM32F423xx)
  1494. /* Get the PLL/PLLI2S division factors -------------------------------------*/
  1495. PeriphClkInit->PLLI2SDivR = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVR) >> RCC_DCKCFGR_PLLI2SDIVR_Pos);
  1496. PeriphClkInit->PLLDivR = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLDIVR) >> RCC_DCKCFGR_PLLDIVR_Pos);
  1497. #endif /* STM32F413xx || STM32F423xx */
  1498. /* Get the I2S APB1 clock configuration ------------------------------------*/
  1499. PeriphClkInit->I2sApb1ClockSelection = __HAL_RCC_GET_I2S_APB1_SOURCE();
  1500. /* Get the I2S APB2 clock configuration ------------------------------------*/
  1501. PeriphClkInit->I2sApb2ClockSelection = __HAL_RCC_GET_I2S_APB2_SOURCE();
  1502. /* Get the RTC Clock configuration -----------------------------------------*/
  1503. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  1504. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  1505. /* Get the FMPI2C1 clock configuration -------------------------------------*/
  1506. PeriphClkInit->Fmpi2c1ClockSelection = __HAL_RCC_GET_FMPI2C1_SOURCE();
  1507. /* Get the CLK48 clock configuration ---------------------------------------*/
  1508. PeriphClkInit->Clk48ClockSelection = __HAL_RCC_GET_CLK48_SOURCE();
  1509. /* Get the SDIO clock configuration ----------------------------------------*/
  1510. PeriphClkInit->SdioClockSelection = __HAL_RCC_GET_SDIO_SOURCE();
  1511. /* Get the DFSDM1 clock configuration --------------------------------------*/
  1512. PeriphClkInit->Dfsdm1ClockSelection = __HAL_RCC_GET_DFSDM1_SOURCE();
  1513. /* Get the DFSDM1 Audio clock configuration --------------------------------*/
  1514. PeriphClkInit->Dfsdm1AudioClockSelection = __HAL_RCC_GET_DFSDM1AUDIO_SOURCE();
  1515. #if defined(STM32F413xx) || defined(STM32F423xx)
  1516. /* Get the DFSDM2 clock configuration --------------------------------------*/
  1517. PeriphClkInit->Dfsdm2ClockSelection = __HAL_RCC_GET_DFSDM2_SOURCE();
  1518. /* Get the DFSDM2 Audio clock configuration --------------------------------*/
  1519. PeriphClkInit->Dfsdm2AudioClockSelection = __HAL_RCC_GET_DFSDM2AUDIO_SOURCE();
  1520. /* Get the LPTIM1 clock configuration --------------------------------------*/
  1521. PeriphClkInit->Lptim1ClockSelection = __HAL_RCC_GET_LPTIM1_SOURCE();
  1522. /* Get the SAI1 Block Aclock configuration ---------------------------------*/
  1523. PeriphClkInit->SaiAClockSelection = __HAL_RCC_GET_SAI_BLOCKA_SOURCE();
  1524. /* Get the SAI1 Block B clock configuration --------------------------------*/
  1525. PeriphClkInit->SaiBClockSelection = __HAL_RCC_GET_SAI_BLOCKB_SOURCE();
  1526. #endif /* STM32F413xx || STM32F423xx */
  1527. /* Get the TIM Prescaler configuration -------------------------------------*/
  1528. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  1529. {
  1530. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  1531. }
  1532. else
  1533. {
  1534. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  1535. }
  1536. }
  1537. /**
  1538. * @brief Return the peripheral clock frequency for a given peripheral(I2S..)
  1539. * @note Return 0 if peripheral clock identifier not managed by this API
  1540. * @param PeriphClk Peripheral clock identifier
  1541. * This parameter can be one of the following values:
  1542. * @arg RCC_PERIPHCLK_I2S_APB1: I2S APB1 peripheral clock
  1543. * @arg RCC_PERIPHCLK_I2S_APB2: I2S APB2 peripheral clock
  1544. * @retval Frequency in KHz
  1545. */
  1546. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  1547. {
  1548. /* This variable used to store the I2S clock frequency (value in Hz) */
  1549. uint32_t frequency = 0U;
  1550. /* This variable used to store the VCO Input (value in Hz) */
  1551. uint32_t vcoinput = 0U;
  1552. uint32_t srcclk = 0U;
  1553. /* This variable used to store the VCO Output (value in Hz) */
  1554. uint32_t vcooutput = 0U;
  1555. switch (PeriphClk)
  1556. {
  1557. case RCC_PERIPHCLK_I2S_APB1:
  1558. {
  1559. /* Get the current I2S source */
  1560. srcclk = __HAL_RCC_GET_I2S_APB1_SOURCE();
  1561. switch (srcclk)
  1562. {
  1563. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  1564. case RCC_I2SAPB1CLKSOURCE_EXT:
  1565. {
  1566. /* Set the I2S clock to the external clock value */
  1567. frequency = EXTERNAL_CLOCK_VALUE;
  1568. break;
  1569. }
  1570. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  1571. case RCC_I2SAPB1CLKSOURCE_PLLI2S:
  1572. {
  1573. if((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SSRC) == RCC_PLLI2SCFGR_PLLI2SSRC)
  1574. {
  1575. /* Get the I2S source clock value */
  1576. vcoinput = (uint32_t)(EXTERNAL_CLOCK_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  1577. }
  1578. else
  1579. {
  1580. /* Configure the PLLI2S division factor */
  1581. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  1582. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1583. {
  1584. /* Get the I2S source clock value */
  1585. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  1586. }
  1587. else
  1588. {
  1589. /* Get the I2S source clock value */
  1590. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  1591. }
  1592. }
  1593. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  1594. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  1595. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  1596. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  1597. break;
  1598. }
  1599. /* Check if I2S clock selection is PLL VCO Output divided by PLLR used as I2S clock */
  1600. case RCC_I2SAPB1CLKSOURCE_PLLR:
  1601. {
  1602. /* Configure the PLL division factor R */
  1603. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  1604. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1605. {
  1606. /* Get the I2S source clock value */
  1607. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1608. }
  1609. else
  1610. {
  1611. /* Get the I2S source clock value */
  1612. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1613. }
  1614. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  1615. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U) & (RCC_PLLCFGR_PLLN >> 6U)));
  1616. /* I2S_CLK = PLL_VCO Output/PLLR */
  1617. frequency = (uint32_t)(vcooutput /(((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U) & (RCC_PLLCFGR_PLLR >> 28U)));
  1618. break;
  1619. }
  1620. /* Check if I2S clock selection is HSI or HSE depending from PLL source Clock */
  1621. case RCC_I2SAPB1CLKSOURCE_PLLSRC:
  1622. {
  1623. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1624. {
  1625. frequency = HSE_VALUE;
  1626. }
  1627. else
  1628. {
  1629. frequency = HSI_VALUE;
  1630. }
  1631. break;
  1632. }
  1633. /* Clock not enabled for I2S*/
  1634. default:
  1635. {
  1636. frequency = 0U;
  1637. break;
  1638. }
  1639. }
  1640. break;
  1641. }
  1642. case RCC_PERIPHCLK_I2S_APB2:
  1643. {
  1644. /* Get the current I2S source */
  1645. srcclk = __HAL_RCC_GET_I2S_APB2_SOURCE();
  1646. switch (srcclk)
  1647. {
  1648. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  1649. case RCC_I2SAPB2CLKSOURCE_EXT:
  1650. {
  1651. /* Set the I2S clock to the external clock value */
  1652. frequency = EXTERNAL_CLOCK_VALUE;
  1653. break;
  1654. }
  1655. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  1656. case RCC_I2SAPB2CLKSOURCE_PLLI2S:
  1657. {
  1658. if((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SSRC) == RCC_PLLI2SCFGR_PLLI2SSRC)
  1659. {
  1660. /* Get the I2S source clock value */
  1661. vcoinput = (uint32_t)(EXTERNAL_CLOCK_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  1662. }
  1663. else
  1664. {
  1665. /* Configure the PLLI2S division factor */
  1666. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  1667. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1668. {
  1669. /* Get the I2S source clock value */
  1670. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  1671. }
  1672. else
  1673. {
  1674. /* Get the I2S source clock value */
  1675. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  1676. }
  1677. }
  1678. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  1679. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  1680. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  1681. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  1682. break;
  1683. }
  1684. /* Check if I2S clock selection is PLL VCO Output divided by PLLR used as I2S clock */
  1685. case RCC_I2SAPB2CLKSOURCE_PLLR:
  1686. {
  1687. /* Configure the PLL division factor R */
  1688. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  1689. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1690. {
  1691. /* Get the I2S source clock value */
  1692. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1693. }
  1694. else
  1695. {
  1696. /* Get the I2S source clock value */
  1697. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1698. }
  1699. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  1700. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U) & (RCC_PLLCFGR_PLLN >> 6U)));
  1701. /* I2S_CLK = PLL_VCO Output/PLLR */
  1702. frequency = (uint32_t)(vcooutput /(((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U) & (RCC_PLLCFGR_PLLR >> 28U)));
  1703. break;
  1704. }
  1705. /* Check if I2S clock selection is HSI or HSE depending from PLL source Clock */
  1706. case RCC_I2SAPB2CLKSOURCE_PLLSRC:
  1707. {
  1708. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1709. {
  1710. frequency = HSE_VALUE;
  1711. }
  1712. else
  1713. {
  1714. frequency = HSI_VALUE;
  1715. }
  1716. break;
  1717. }
  1718. /* Clock not enabled for I2S*/
  1719. default:
  1720. {
  1721. frequency = 0U;
  1722. break;
  1723. }
  1724. }
  1725. break;
  1726. }
  1727. }
  1728. return frequency;
  1729. }
  1730. #endif /* STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
  1731. #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx)
  1732. /**
  1733. * @brief Initializes the RCC extended peripherals clocks according to the specified parameters in the
  1734. * RCC_PeriphCLKInitTypeDef.
  1735. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  1736. * contains the configuration information for the Extended Peripherals clocks(I2S and RTC clocks).
  1737. *
  1738. * @note A caution to be taken when HAL_RCCEx_PeriphCLKConfig() is used to select RTC clock selection, in this case
  1739. * the Reset of Backup domain will be applied in order to modify the RTC Clock source as consequence all backup
  1740. * domain (RTC and RCC_BDCR register expect BKPSRAM) will be reset
  1741. *
  1742. * @retval HAL status
  1743. */
  1744. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1745. {
  1746. uint32_t tickstart = 0U;
  1747. uint32_t tmpreg1 = 0U;
  1748. /* Check the parameters */
  1749. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  1750. /*---------------------------- RTC configuration ---------------------------*/
  1751. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  1752. {
  1753. /* Check for RTC Parameters used to output RTCCLK */
  1754. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  1755. /* Enable Power Clock*/
  1756. __HAL_RCC_PWR_CLK_ENABLE();
  1757. /* Enable write access to Backup domain */
  1758. PWR->CR |= PWR_CR_DBP;
  1759. /* Get tick */
  1760. tickstart = HAL_GetTick();
  1761. while((PWR->CR & PWR_CR_DBP) == RESET)
  1762. {
  1763. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  1764. {
  1765. return HAL_TIMEOUT;
  1766. }
  1767. }
  1768. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  1769. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  1770. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  1771. {
  1772. /* Store the content of BDCR register before the reset of Backup Domain */
  1773. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  1774. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  1775. __HAL_RCC_BACKUPRESET_FORCE();
  1776. __HAL_RCC_BACKUPRESET_RELEASE();
  1777. /* Restore the Content of BDCR register */
  1778. RCC->BDCR = tmpreg1;
  1779. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  1780. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  1781. {
  1782. /* Get tick */
  1783. tickstart = HAL_GetTick();
  1784. /* Wait till LSE is ready */
  1785. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  1786. {
  1787. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  1788. {
  1789. return HAL_TIMEOUT;
  1790. }
  1791. }
  1792. }
  1793. }
  1794. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  1795. }
  1796. /*--------------------------------------------------------------------------*/
  1797. /*---------------------------- TIM configuration ---------------------------*/
  1798. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  1799. {
  1800. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  1801. }
  1802. /*--------------------------------------------------------------------------*/
  1803. /*---------------------------- FMPI2C1 Configuration -----------------------*/
  1804. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_FMPI2C1) == RCC_PERIPHCLK_FMPI2C1)
  1805. {
  1806. /* Check the parameters */
  1807. assert_param(IS_RCC_FMPI2C1CLKSOURCE(PeriphClkInit->Fmpi2c1ClockSelection));
  1808. /* Configure the FMPI2C1 clock source */
  1809. __HAL_RCC_FMPI2C1_CONFIG(PeriphClkInit->Fmpi2c1ClockSelection);
  1810. }
  1811. /*--------------------------------------------------------------------------*/
  1812. /*---------------------------- LPTIM1 Configuration ------------------------*/
  1813. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM1) == RCC_PERIPHCLK_LPTIM1)
  1814. {
  1815. /* Check the parameters */
  1816. assert_param(IS_RCC_LPTIM1CLKSOURCE(PeriphClkInit->Lptim1ClockSelection));
  1817. /* Configure the LPTIM1 clock source */
  1818. __HAL_RCC_LPTIM1_CONFIG(PeriphClkInit->Lptim1ClockSelection);
  1819. }
  1820. /*---------------------------- I2S Configuration ---------------------------*/
  1821. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == RCC_PERIPHCLK_I2S)
  1822. {
  1823. /* Check the parameters */
  1824. assert_param(IS_RCC_I2SAPBCLKSOURCE(PeriphClkInit->I2SClockSelection));
  1825. /* Configure the I2S clock source */
  1826. __HAL_RCC_I2S_CONFIG(PeriphClkInit->I2SClockSelection);
  1827. }
  1828. return HAL_OK;
  1829. }
  1830. /**
  1831. * @brief Configures the RCC_OscInitStruct according to the internal
  1832. * RCC configuration registers.
  1833. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  1834. * will be configured.
  1835. * @retval None
  1836. */
  1837. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1838. {
  1839. uint32_t tempreg;
  1840. /* Set all possible values for the extended clock type parameter------------*/
  1841. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_FMPI2C1 | RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC;
  1842. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  1843. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  1844. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  1845. {
  1846. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  1847. }
  1848. else
  1849. {
  1850. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  1851. }
  1852. /* Get the FMPI2C1 clock configuration -------------------------------------*/
  1853. PeriphClkInit->Fmpi2c1ClockSelection = __HAL_RCC_GET_FMPI2C1_SOURCE();
  1854. /* Get the I2S clock configuration -----------------------------------------*/
  1855. PeriphClkInit->I2SClockSelection = __HAL_RCC_GET_I2S_SOURCE();
  1856. }
  1857. /**
  1858. * @brief Return the peripheral clock frequency for a given peripheral(SAI..)
  1859. * @note Return 0 if peripheral clock identifier not managed by this API
  1860. * @param PeriphClk Peripheral clock identifier
  1861. * This parameter can be one of the following values:
  1862. * @arg RCC_PERIPHCLK_I2S: I2S peripheral clock
  1863. * @retval Frequency in KHz
  1864. */
  1865. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  1866. {
  1867. /* This variable used to store the I2S clock frequency (value in Hz) */
  1868. uint32_t frequency = 0U;
  1869. /* This variable used to store the VCO Input (value in Hz) */
  1870. uint32_t vcoinput = 0U;
  1871. uint32_t srcclk = 0U;
  1872. /* This variable used to store the VCO Output (value in Hz) */
  1873. uint32_t vcooutput = 0U;
  1874. switch (PeriphClk)
  1875. {
  1876. case RCC_PERIPHCLK_I2S:
  1877. {
  1878. /* Get the current I2S source */
  1879. srcclk = __HAL_RCC_GET_I2S_SOURCE();
  1880. switch (srcclk)
  1881. {
  1882. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  1883. case RCC_I2SAPBCLKSOURCE_EXT:
  1884. {
  1885. /* Set the I2S clock to the external clock value */
  1886. frequency = EXTERNAL_CLOCK_VALUE;
  1887. break;
  1888. }
  1889. /* Check if I2S clock selection is PLL VCO Output divided by PLLR used as I2S clock */
  1890. case RCC_I2SAPBCLKSOURCE_PLLR:
  1891. {
  1892. /* Configure the PLL division factor R */
  1893. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  1894. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1895. {
  1896. /* Get the I2S source clock value */
  1897. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1898. }
  1899. else
  1900. {
  1901. /* Get the I2S source clock value */
  1902. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1903. }
  1904. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  1905. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U) & (RCC_PLLCFGR_PLLN >> 6U)));
  1906. /* I2S_CLK = PLL_VCO Output/PLLR */
  1907. frequency = (uint32_t)(vcooutput /(((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U) & (RCC_PLLCFGR_PLLR >> 28U)));
  1908. break;
  1909. }
  1910. /* Check if I2S clock selection is HSI or HSE depending from PLL source Clock */
  1911. case RCC_I2SAPBCLKSOURCE_PLLSRC:
  1912. {
  1913. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1914. {
  1915. frequency = HSE_VALUE;
  1916. }
  1917. else
  1918. {
  1919. frequency = HSI_VALUE;
  1920. }
  1921. break;
  1922. }
  1923. /* Clock not enabled for I2S*/
  1924. default:
  1925. {
  1926. frequency = 0U;
  1927. break;
  1928. }
  1929. }
  1930. break;
  1931. }
  1932. }
  1933. return frequency;
  1934. }
  1935. #endif /* STM32F410Tx || STM32F410Cx || STM32F410Rx */
  1936. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  1937. /**
  1938. * @brief Initializes the RCC extended peripherals clocks according to the specified
  1939. * parameters in the RCC_PeriphCLKInitTypeDef.
  1940. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  1941. * contains the configuration information for the Extended Peripherals
  1942. * clocks(I2S, SAI, LTDC RTC and TIM).
  1943. *
  1944. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  1945. * the RTC clock source; in this case the Backup domain will be reset in
  1946. * order to modify the RTC Clock source, as consequence RTC registers (including
  1947. * the backup registers) and RCC_BDCR register are set to their reset values.
  1948. *
  1949. * @retval HAL status
  1950. */
  1951. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1952. {
  1953. uint32_t tickstart = 0U;
  1954. uint32_t tmpreg1 = 0U;
  1955. /* Check the parameters */
  1956. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  1957. /*----------------------- SAI/I2S Configuration (PLLI2S) -------------------*/
  1958. /*----------------------- Common configuration SAI/I2S ---------------------*/
  1959. /* In Case of SAI or I2S Clock Configuration through PLLI2S, PLLI2SN division
  1960. factor is common parameters for both peripherals */
  1961. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == RCC_PERIPHCLK_I2S) ||
  1962. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLI2S) == RCC_PERIPHCLK_SAI_PLLI2S))
  1963. {
  1964. /* check for Parameters */
  1965. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  1966. /* Disable the PLLI2S */
  1967. __HAL_RCC_PLLI2S_DISABLE();
  1968. /* Get tick */
  1969. tickstart = HAL_GetTick();
  1970. /* Wait till PLLI2S is disabled */
  1971. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  1972. {
  1973. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  1974. {
  1975. /* return in case of Timeout detected */
  1976. return HAL_TIMEOUT;
  1977. }
  1978. }
  1979. /*---------------------------- I2S configuration -------------------------*/
  1980. /* In Case of I2S Clock Configuration through PLLI2S, PLLI2SR must be added
  1981. only for I2S configuration */
  1982. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == (RCC_PERIPHCLK_I2S))
  1983. {
  1984. /* check for Parameters */
  1985. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  1986. /* Configure the PLLI2S division factors */
  1987. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLM) */
  1988. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  1989. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SR);
  1990. }
  1991. /*---------------------------- SAI configuration -------------------------*/
  1992. /* In Case of SAI Clock Configuration through PLLI2S, PLLI2SQ and PLLI2S_DIVQ must
  1993. be added only for SAI configuration */
  1994. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLI2S) == (RCC_PERIPHCLK_SAI_PLLI2S))
  1995. {
  1996. /* Check the PLLI2S division factors */
  1997. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  1998. assert_param(IS_RCC_PLLI2S_DIVQ_VALUE(PeriphClkInit->PLLI2SDivQ));
  1999. /* Read PLLI2SR value from PLLI2SCFGR register (this value is not need for SAI configuration) */
  2000. tmpreg1 = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  2001. /* Configure the PLLI2S division factors */
  2002. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  2003. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  2004. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  2005. __HAL_RCC_PLLI2S_SAICLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SQ , tmpreg1);
  2006. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  2007. __HAL_RCC_PLLI2S_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLI2SDivQ);
  2008. }
  2009. /* Enable the PLLI2S */
  2010. __HAL_RCC_PLLI2S_ENABLE();
  2011. /* Get tick */
  2012. tickstart = HAL_GetTick();
  2013. /* Wait till PLLI2S is ready */
  2014. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  2015. {
  2016. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  2017. {
  2018. /* return in case of Timeout detected */
  2019. return HAL_TIMEOUT;
  2020. }
  2021. }
  2022. }
  2023. /*--------------------------------------------------------------------------*/
  2024. /*----------------------- SAI/LTDC Configuration (PLLSAI) ------------------*/
  2025. /*----------------------- Common configuration SAI/LTDC --------------------*/
  2026. /* In Case of SAI or LTDC Clock Configuration through PLLSAI, PLLSAIN division
  2027. factor is common parameters for both peripherals */
  2028. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLSAI) == RCC_PERIPHCLK_SAI_PLLSAI) ||
  2029. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == RCC_PERIPHCLK_LTDC))
  2030. {
  2031. /* Check the PLLSAI division factors */
  2032. assert_param(IS_RCC_PLLSAIN_VALUE(PeriphClkInit->PLLSAI.PLLSAIN));
  2033. /* Disable PLLSAI Clock */
  2034. __HAL_RCC_PLLSAI_DISABLE();
  2035. /* Get tick */
  2036. tickstart = HAL_GetTick();
  2037. /* Wait till PLLSAI is disabled */
  2038. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  2039. {
  2040. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  2041. {
  2042. /* return in case of Timeout detected */
  2043. return HAL_TIMEOUT;
  2044. }
  2045. }
  2046. /*---------------------------- SAI configuration -------------------------*/
  2047. /* In Case of SAI Clock Configuration through PLLSAI, PLLSAIQ and PLLSAI_DIVQ must
  2048. be added only for SAI configuration */
  2049. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLSAI) == (RCC_PERIPHCLK_SAI_PLLSAI))
  2050. {
  2051. assert_param(IS_RCC_PLLSAIQ_VALUE(PeriphClkInit->PLLSAI.PLLSAIQ));
  2052. assert_param(IS_RCC_PLLSAI_DIVQ_VALUE(PeriphClkInit->PLLSAIDivQ));
  2053. /* Read PLLSAIR value from PLLSAICFGR register (this value is not need for SAI configuration) */
  2054. tmpreg1 = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> RCC_PLLSAICFGR_PLLSAIR_Pos);
  2055. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  2056. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  2057. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  2058. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN , PeriphClkInit->PLLSAI.PLLSAIQ, tmpreg1);
  2059. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  2060. __HAL_RCC_PLLSAI_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLSAIDivQ);
  2061. }
  2062. /*---------------------------- LTDC configuration ------------------------*/
  2063. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == (RCC_PERIPHCLK_LTDC))
  2064. {
  2065. assert_param(IS_RCC_PLLSAIR_VALUE(PeriphClkInit->PLLSAI.PLLSAIR));
  2066. assert_param(IS_RCC_PLLSAI_DIVR_VALUE(PeriphClkInit->PLLSAIDivR));
  2067. /* Read PLLSAIR value from PLLSAICFGR register (this value is not need for SAI configuration) */
  2068. tmpreg1 = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  2069. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  2070. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  2071. /* LTDC_CLK(first level) = PLLSAI_VCO Output/PLLSAIR */
  2072. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN , tmpreg1, PeriphClkInit->PLLSAI.PLLSAIR);
  2073. /* LTDC_CLK = LTDC_CLK(first level)/PLLSAIDIVR */
  2074. __HAL_RCC_PLLSAI_PLLSAICLKDIVR_CONFIG(PeriphClkInit->PLLSAIDivR);
  2075. }
  2076. /* Enable PLLSAI Clock */
  2077. __HAL_RCC_PLLSAI_ENABLE();
  2078. /* Get tick */
  2079. tickstart = HAL_GetTick();
  2080. /* Wait till PLLSAI is ready */
  2081. while(__HAL_RCC_PLLSAI_GET_FLAG() == RESET)
  2082. {
  2083. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  2084. {
  2085. /* return in case of Timeout detected */
  2086. return HAL_TIMEOUT;
  2087. }
  2088. }
  2089. }
  2090. /*--------------------------------------------------------------------------*/
  2091. /*---------------------------- RTC configuration ---------------------------*/
  2092. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  2093. {
  2094. /* Check for RTC Parameters used to output RTCCLK */
  2095. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  2096. /* Enable Power Clock*/
  2097. __HAL_RCC_PWR_CLK_ENABLE();
  2098. /* Enable write access to Backup domain */
  2099. PWR->CR |= PWR_CR_DBP;
  2100. /* Get tick */
  2101. tickstart = HAL_GetTick();
  2102. while((PWR->CR & PWR_CR_DBP) == RESET)
  2103. {
  2104. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  2105. {
  2106. return HAL_TIMEOUT;
  2107. }
  2108. }
  2109. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  2110. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  2111. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  2112. {
  2113. /* Store the content of BDCR register before the reset of Backup Domain */
  2114. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  2115. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  2116. __HAL_RCC_BACKUPRESET_FORCE();
  2117. __HAL_RCC_BACKUPRESET_RELEASE();
  2118. /* Restore the Content of BDCR register */
  2119. RCC->BDCR = tmpreg1;
  2120. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  2121. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  2122. {
  2123. /* Get tick */
  2124. tickstart = HAL_GetTick();
  2125. /* Wait till LSE is ready */
  2126. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  2127. {
  2128. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  2129. {
  2130. return HAL_TIMEOUT;
  2131. }
  2132. }
  2133. }
  2134. }
  2135. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  2136. }
  2137. /*--------------------------------------------------------------------------*/
  2138. /*---------------------------- TIM configuration ---------------------------*/
  2139. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  2140. {
  2141. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  2142. }
  2143. return HAL_OK;
  2144. }
  2145. /**
  2146. * @brief Configures the PeriphClkInit according to the internal
  2147. * RCC configuration registers.
  2148. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  2149. * will be configured.
  2150. * @retval None
  2151. */
  2152. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  2153. {
  2154. uint32_t tempreg;
  2155. /* Set all possible values for the extended clock type parameter------------*/
  2156. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S | RCC_PERIPHCLK_SAI_PLLSAI | RCC_PERIPHCLK_SAI_PLLI2S | RCC_PERIPHCLK_LTDC | RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC;
  2157. /* Get the PLLI2S Clock configuration -----------------------------------------------*/
  2158. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> RCC_PLLI2SCFGR_PLLI2SN_Pos);
  2159. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  2160. PeriphClkInit->PLLI2S.PLLI2SQ = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> RCC_PLLI2SCFGR_PLLI2SQ_Pos);
  2161. /* Get the PLLSAI Clock configuration -----------------------------------------------*/
  2162. PeriphClkInit->PLLSAI.PLLSAIN = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> RCC_PLLSAICFGR_PLLSAIN_Pos);
  2163. PeriphClkInit->PLLSAI.PLLSAIR = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> RCC_PLLSAICFGR_PLLSAIR_Pos);
  2164. PeriphClkInit->PLLSAI.PLLSAIQ = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  2165. /* Get the PLLSAI/PLLI2S division factors -----------------------------------------------*/
  2166. PeriphClkInit->PLLI2SDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) >> RCC_DCKCFGR_PLLI2SDIVQ_Pos);
  2167. PeriphClkInit->PLLSAIDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> RCC_DCKCFGR_PLLSAIDIVQ_Pos);
  2168. PeriphClkInit->PLLSAIDivR = (uint32_t)(RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVR);
  2169. /* Get the RTC Clock configuration -----------------------------------------------*/
  2170. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  2171. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  2172. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  2173. {
  2174. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  2175. }
  2176. else
  2177. {
  2178. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  2179. }
  2180. }
  2181. /**
  2182. * @brief Return the peripheral clock frequency for a given peripheral(SAI..)
  2183. * @note Return 0 if peripheral clock identifier not managed by this API
  2184. * @param PeriphClk Peripheral clock identifier
  2185. * This parameter can be one of the following values:
  2186. * @arg RCC_PERIPHCLK_I2S: I2S peripheral clock
  2187. * @retval Frequency in KHz
  2188. */
  2189. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  2190. {
  2191. /* This variable used to store the I2S clock frequency (value in Hz) */
  2192. uint32_t frequency = 0U;
  2193. /* This variable used to store the VCO Input (value in Hz) */
  2194. uint32_t vcoinput = 0U;
  2195. uint32_t srcclk = 0U;
  2196. /* This variable used to store the VCO Output (value in Hz) */
  2197. uint32_t vcooutput = 0U;
  2198. switch (PeriphClk)
  2199. {
  2200. case RCC_PERIPHCLK_I2S:
  2201. {
  2202. /* Get the current I2S source */
  2203. srcclk = __HAL_RCC_GET_I2S_SOURCE();
  2204. switch (srcclk)
  2205. {
  2206. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  2207. case RCC_I2SCLKSOURCE_EXT:
  2208. {
  2209. /* Set the I2S clock to the external clock value */
  2210. frequency = EXTERNAL_CLOCK_VALUE;
  2211. break;
  2212. }
  2213. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  2214. case RCC_I2SCLKSOURCE_PLLI2S:
  2215. {
  2216. /* Configure the PLLI2S division factor */
  2217. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  2218. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  2219. {
  2220. /* Get the I2S source clock value */
  2221. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  2222. }
  2223. else
  2224. {
  2225. /* Get the I2S source clock value */
  2226. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  2227. }
  2228. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  2229. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  2230. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  2231. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  2232. break;
  2233. }
  2234. /* Clock not enabled for I2S */
  2235. default:
  2236. {
  2237. frequency = 0U;
  2238. break;
  2239. }
  2240. }
  2241. break;
  2242. }
  2243. }
  2244. return frequency;
  2245. }
  2246. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  2247. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx) ||\
  2248. defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE)
  2249. /**
  2250. * @brief Initializes the RCC extended peripherals clocks according to the specified parameters in the
  2251. * RCC_PeriphCLKInitTypeDef.
  2252. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  2253. * contains the configuration information for the Extended Peripherals clocks(I2S and RTC clocks).
  2254. *
  2255. * @note A caution to be taken when HAL_RCCEx_PeriphCLKConfig() is used to select RTC clock selection, in this case
  2256. * the Reset of Backup domain will be applied in order to modify the RTC Clock source as consequence all backup
  2257. * domain (RTC and RCC_BDCR register expect BKPSRAM) will be reset
  2258. *
  2259. * @retval HAL status
  2260. */
  2261. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  2262. {
  2263. uint32_t tickstart = 0U;
  2264. uint32_t tmpreg1 = 0U;
  2265. /* Check the parameters */
  2266. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  2267. /*---------------------------- I2S configuration ---------------------------*/
  2268. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == RCC_PERIPHCLK_I2S) ||
  2269. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S))
  2270. {
  2271. /* check for Parameters */
  2272. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  2273. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  2274. #if defined(STM32F411xE)
  2275. assert_param(IS_RCC_PLLI2SM_VALUE(PeriphClkInit->PLLI2S.PLLI2SM));
  2276. #endif /* STM32F411xE */
  2277. /* Disable the PLLI2S */
  2278. __HAL_RCC_PLLI2S_DISABLE();
  2279. /* Get tick */
  2280. tickstart = HAL_GetTick();
  2281. /* Wait till PLLI2S is disabled */
  2282. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  2283. {
  2284. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  2285. {
  2286. /* return in case of Timeout detected */
  2287. return HAL_TIMEOUT;
  2288. }
  2289. }
  2290. #if defined(STM32F411xE)
  2291. /* Configure the PLLI2S division factors */
  2292. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  2293. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  2294. __HAL_RCC_PLLI2S_I2SCLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN, PeriphClkInit->PLLI2S.PLLI2SR);
  2295. #else
  2296. /* Configure the PLLI2S division factors */
  2297. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLM) */
  2298. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  2299. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SR);
  2300. #endif /* STM32F411xE */
  2301. /* Enable the PLLI2S */
  2302. __HAL_RCC_PLLI2S_ENABLE();
  2303. /* Get tick */
  2304. tickstart = HAL_GetTick();
  2305. /* Wait till PLLI2S is ready */
  2306. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  2307. {
  2308. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  2309. {
  2310. /* return in case of Timeout detected */
  2311. return HAL_TIMEOUT;
  2312. }
  2313. }
  2314. }
  2315. /*---------------------------- RTC configuration ---------------------------*/
  2316. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  2317. {
  2318. /* Check for RTC Parameters used to output RTCCLK */
  2319. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  2320. /* Enable Power Clock*/
  2321. __HAL_RCC_PWR_CLK_ENABLE();
  2322. /* Enable write access to Backup domain */
  2323. PWR->CR |= PWR_CR_DBP;
  2324. /* Get tick */
  2325. tickstart = HAL_GetTick();
  2326. while((PWR->CR & PWR_CR_DBP) == RESET)
  2327. {
  2328. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  2329. {
  2330. return HAL_TIMEOUT;
  2331. }
  2332. }
  2333. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  2334. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  2335. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  2336. {
  2337. /* Store the content of BDCR register before the reset of Backup Domain */
  2338. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  2339. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  2340. __HAL_RCC_BACKUPRESET_FORCE();
  2341. __HAL_RCC_BACKUPRESET_RELEASE();
  2342. /* Restore the Content of BDCR register */
  2343. RCC->BDCR = tmpreg1;
  2344. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  2345. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  2346. {
  2347. /* Get tick */
  2348. tickstart = HAL_GetTick();
  2349. /* Wait till LSE is ready */
  2350. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  2351. {
  2352. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  2353. {
  2354. return HAL_TIMEOUT;
  2355. }
  2356. }
  2357. }
  2358. }
  2359. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  2360. }
  2361. #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE)
  2362. /*---------------------------- TIM configuration ---------------------------*/
  2363. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  2364. {
  2365. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  2366. }
  2367. #endif /* STM32F401xC || STM32F401xE || STM32F411xE */
  2368. return HAL_OK;
  2369. }
  2370. /**
  2371. * @brief Configures the RCC_OscInitStruct according to the internal
  2372. * RCC configuration registers.
  2373. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  2374. * will be configured.
  2375. * @retval None
  2376. */
  2377. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  2378. {
  2379. uint32_t tempreg;
  2380. /* Set all possible values for the extended clock type parameter------------*/
  2381. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S | RCC_PERIPHCLK_RTC;
  2382. /* Get the PLLI2S Clock configuration --------------------------------------*/
  2383. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> RCC_PLLI2SCFGR_PLLI2SN_Pos);
  2384. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  2385. #if defined(STM32F411xE)
  2386. PeriphClkInit->PLLI2S.PLLI2SM = (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM);
  2387. #endif /* STM32F411xE */
  2388. /* Get the RTC Clock configuration -----------------------------------------*/
  2389. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  2390. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  2391. #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE)
  2392. /* Get the TIM Prescaler configuration -------------------------------------*/
  2393. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  2394. {
  2395. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  2396. }
  2397. else
  2398. {
  2399. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  2400. }
  2401. #endif /* STM32F401xC || STM32F401xE || STM32F411xE */
  2402. }
  2403. /**
  2404. * @brief Return the peripheral clock frequency for a given peripheral(SAI..)
  2405. * @note Return 0 if peripheral clock identifier not managed by this API
  2406. * @param PeriphClk Peripheral clock identifier
  2407. * This parameter can be one of the following values:
  2408. * @arg RCC_PERIPHCLK_I2S: I2S peripheral clock
  2409. * @retval Frequency in KHz
  2410. */
  2411. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  2412. {
  2413. /* This variable used to store the I2S clock frequency (value in Hz) */
  2414. uint32_t frequency = 0U;
  2415. /* This variable used to store the VCO Input (value in Hz) */
  2416. uint32_t vcoinput = 0U;
  2417. uint32_t srcclk = 0U;
  2418. /* This variable used to store the VCO Output (value in Hz) */
  2419. uint32_t vcooutput = 0U;
  2420. switch (PeriphClk)
  2421. {
  2422. case RCC_PERIPHCLK_I2S:
  2423. {
  2424. /* Get the current I2S source */
  2425. srcclk = __HAL_RCC_GET_I2S_SOURCE();
  2426. switch (srcclk)
  2427. {
  2428. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  2429. case RCC_I2SCLKSOURCE_EXT:
  2430. {
  2431. /* Set the I2S clock to the external clock value */
  2432. frequency = EXTERNAL_CLOCK_VALUE;
  2433. break;
  2434. }
  2435. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  2436. case RCC_I2SCLKSOURCE_PLLI2S:
  2437. {
  2438. #if defined(STM32F411xE)
  2439. /* Configure the PLLI2S division factor */
  2440. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  2441. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  2442. {
  2443. /* Get the I2S source clock value */
  2444. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  2445. }
  2446. else
  2447. {
  2448. /* Get the I2S source clock value */
  2449. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  2450. }
  2451. #else
  2452. /* Configure the PLLI2S division factor */
  2453. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  2454. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  2455. {
  2456. /* Get the I2S source clock value */
  2457. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  2458. }
  2459. else
  2460. {
  2461. /* Get the I2S source clock value */
  2462. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  2463. }
  2464. #endif /* STM32F411xE */
  2465. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  2466. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  2467. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  2468. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  2469. break;
  2470. }
  2471. /* Clock not enabled for I2S*/
  2472. default:
  2473. {
  2474. frequency = 0U;
  2475. break;
  2476. }
  2477. }
  2478. break;
  2479. }
  2480. }
  2481. return frequency;
  2482. }
  2483. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F401xC || STM32F401xE || STM32F411xE */
  2484. #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) ||\
  2485. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  2486. /**
  2487. * @brief Select LSE mode
  2488. *
  2489. * @note This mode is only available for STM32F410xx/STM32F411xx/STM32F446xx/STM32F469xx/STM32F479xx/STM32F412Zx/STM32F412Vx/STM32F412Rx/STM32F412Cx devices.
  2490. *
  2491. * @param Mode specifies the LSE mode.
  2492. * This parameter can be one of the following values:
  2493. * @arg RCC_LSE_LOWPOWER_MODE: LSE oscillator in low power mode selection
  2494. * @arg RCC_LSE_HIGHDRIVE_MODE: LSE oscillator in High Drive mode selection
  2495. * @retval None
  2496. */
  2497. void HAL_RCCEx_SelectLSEMode(uint8_t Mode)
  2498. {
  2499. /* Check the parameters */
  2500. assert_param(IS_RCC_LSE_MODE(Mode));
  2501. if(Mode == RCC_LSE_HIGHDRIVE_MODE)
  2502. {
  2503. SET_BIT(RCC->BDCR, RCC_BDCR_LSEMOD);
  2504. }
  2505. else
  2506. {
  2507. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEMOD);
  2508. }
  2509. }
  2510. #endif /* STM32F410xx || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
  2511. /** @defgroup RCCEx_Exported_Functions_Group2 Extended Clock management functions
  2512. * @brief Extended Clock management functions
  2513. *
  2514. @verbatim
  2515. ===============================================================================
  2516. ##### Extended clock management functions #####
  2517. ===============================================================================
  2518. [..]
  2519. This subsection provides a set of functions allowing to control the
  2520. activation or deactivation of PLLI2S, PLLSAI.
  2521. @endverbatim
  2522. * @{
  2523. */
  2524. #if defined(RCC_PLLI2S_SUPPORT)
  2525. /**
  2526. * @brief Enable PLLI2S.
  2527. * @param PLLI2SInit pointer to an RCC_PLLI2SInitTypeDef structure that
  2528. * contains the configuration information for the PLLI2S
  2529. * @retval HAL status
  2530. */
  2531. HAL_StatusTypeDef HAL_RCCEx_EnablePLLI2S(RCC_PLLI2SInitTypeDef *PLLI2SInit)
  2532. {
  2533. uint32_t tickstart;
  2534. /* Check for parameters */
  2535. assert_param(IS_RCC_PLLI2SN_VALUE(PLLI2SInit->PLLI2SN));
  2536. assert_param(IS_RCC_PLLI2SR_VALUE(PLLI2SInit->PLLI2SR));
  2537. #if defined(RCC_PLLI2SCFGR_PLLI2SM)
  2538. assert_param(IS_RCC_PLLI2SM_VALUE(PLLI2SInit->PLLI2SM));
  2539. #endif /* RCC_PLLI2SCFGR_PLLI2SM */
  2540. #if defined(RCC_PLLI2SCFGR_PLLI2SP)
  2541. assert_param(IS_RCC_PLLI2SP_VALUE(PLLI2SInit->PLLI2SP));
  2542. #endif /* RCC_PLLI2SCFGR_PLLI2SP */
  2543. #if defined(RCC_PLLI2SCFGR_PLLI2SQ)
  2544. assert_param(IS_RCC_PLLI2SQ_VALUE(PLLI2SInit->PLLI2SQ));
  2545. #endif /* RCC_PLLI2SCFGR_PLLI2SQ */
  2546. /* Disable the PLLI2S */
  2547. __HAL_RCC_PLLI2S_DISABLE();
  2548. /* Wait till PLLI2S is disabled */
  2549. tickstart = HAL_GetTick();
  2550. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  2551. {
  2552. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  2553. {
  2554. /* return in case of Timeout detected */
  2555. return HAL_TIMEOUT;
  2556. }
  2557. }
  2558. /* Configure the PLLI2S division factors */
  2559. #if defined(STM32F446xx)
  2560. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  2561. /* I2SPCLK = PLLI2S_VCO / PLLI2SP */
  2562. /* I2SQCLK = PLLI2S_VCO / PLLI2SQ */
  2563. /* I2SRCLK = PLLI2S_VCO / PLLI2SR */
  2564. __HAL_RCC_PLLI2S_CONFIG(PLLI2SInit->PLLI2SM, PLLI2SInit->PLLI2SN, \
  2565. PLLI2SInit->PLLI2SP, PLLI2SInit->PLLI2SQ, PLLI2SInit->PLLI2SR);
  2566. #elif defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) ||\
  2567. defined(STM32F413xx) || defined(STM32F423xx)
  2568. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM)*/
  2569. /* I2SQCLK = PLLI2S_VCO / PLLI2SQ */
  2570. /* I2SRCLK = PLLI2S_VCO / PLLI2SR */
  2571. __HAL_RCC_PLLI2S_CONFIG(PLLI2SInit->PLLI2SM, PLLI2SInit->PLLI2SN, \
  2572. PLLI2SInit->PLLI2SQ, PLLI2SInit->PLLI2SR);
  2573. #elif defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  2574. defined(STM32F469xx) || defined(STM32F479xx)
  2575. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * PLLI2SN */
  2576. /* I2SQCLK = PLLI2S_VCO / PLLI2SQ */
  2577. /* I2SRCLK = PLLI2S_VCO / PLLI2SR */
  2578. __HAL_RCC_PLLI2S_SAICLK_CONFIG(PLLI2SInit->PLLI2SN, PLLI2SInit->PLLI2SQ, PLLI2SInit->PLLI2SR);
  2579. #elif defined(STM32F411xE)
  2580. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  2581. /* I2SRCLK = PLLI2S_VCO / PLLI2SR */
  2582. __HAL_RCC_PLLI2S_I2SCLK_CONFIG(PLLI2SInit->PLLI2SM, PLLI2SInit->PLLI2SN, PLLI2SInit->PLLI2SR);
  2583. #else
  2584. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) x PLLI2SN */
  2585. /* I2SRCLK = PLLI2S_VCO / PLLI2SR */
  2586. __HAL_RCC_PLLI2S_CONFIG(PLLI2SInit->PLLI2SN, PLLI2SInit->PLLI2SR);
  2587. #endif /* STM32F446xx */
  2588. /* Enable the PLLI2S */
  2589. __HAL_RCC_PLLI2S_ENABLE();
  2590. /* Wait till PLLI2S is ready */
  2591. tickstart = HAL_GetTick();
  2592. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  2593. {
  2594. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  2595. {
  2596. /* return in case of Timeout detected */
  2597. return HAL_TIMEOUT;
  2598. }
  2599. }
  2600. return HAL_OK;
  2601. }
  2602. /**
  2603. * @brief Disable PLLI2S.
  2604. * @retval HAL status
  2605. */
  2606. HAL_StatusTypeDef HAL_RCCEx_DisablePLLI2S(void)
  2607. {
  2608. uint32_t tickstart;
  2609. /* Disable the PLLI2S */
  2610. __HAL_RCC_PLLI2S_DISABLE();
  2611. /* Wait till PLLI2S is disabled */
  2612. tickstart = HAL_GetTick();
  2613. while(READ_BIT(RCC->CR, RCC_CR_PLLI2SRDY) != RESET)
  2614. {
  2615. if((HAL_GetTick() - tickstart) > PLLI2S_TIMEOUT_VALUE)
  2616. {
  2617. /* return in case of Timeout detected */
  2618. return HAL_TIMEOUT;
  2619. }
  2620. }
  2621. return HAL_OK;
  2622. }
  2623. #endif /* RCC_PLLI2S_SUPPORT */
  2624. #if defined(RCC_PLLSAI_SUPPORT)
  2625. /**
  2626. * @brief Enable PLLSAI.
  2627. * @param PLLSAIInit pointer to an RCC_PLLSAIInitTypeDef structure that
  2628. * contains the configuration information for the PLLSAI
  2629. * @retval HAL status
  2630. */
  2631. HAL_StatusTypeDef HAL_RCCEx_EnablePLLSAI(RCC_PLLSAIInitTypeDef *PLLSAIInit)
  2632. {
  2633. uint32_t tickstart;
  2634. /* Check for parameters */
  2635. assert_param(IS_RCC_PLLSAIN_VALUE(PLLSAIInit->PLLSAIN));
  2636. assert_param(IS_RCC_PLLSAIQ_VALUE(PLLSAIInit->PLLSAIQ));
  2637. #if defined(RCC_PLLSAICFGR_PLLSAIM)
  2638. assert_param(IS_RCC_PLLSAIM_VALUE(PLLSAIInit->PLLSAIM));
  2639. #endif /* RCC_PLLSAICFGR_PLLSAIM */
  2640. #if defined(RCC_PLLSAICFGR_PLLSAIP)
  2641. assert_param(IS_RCC_PLLSAIP_VALUE(PLLSAIInit->PLLSAIP));
  2642. #endif /* RCC_PLLSAICFGR_PLLSAIP */
  2643. #if defined(RCC_PLLSAICFGR_PLLSAIR)
  2644. assert_param(IS_RCC_PLLSAIR_VALUE(PLLSAIInit->PLLSAIR));
  2645. #endif /* RCC_PLLSAICFGR_PLLSAIR */
  2646. /* Disable the PLLSAI */
  2647. __HAL_RCC_PLLSAI_DISABLE();
  2648. /* Wait till PLLSAI is disabled */
  2649. tickstart = HAL_GetTick();
  2650. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  2651. {
  2652. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  2653. {
  2654. /* return in case of Timeout detected */
  2655. return HAL_TIMEOUT;
  2656. }
  2657. }
  2658. /* Configure the PLLSAI division factors */
  2659. #if defined(STM32F446xx)
  2660. /* PLLSAI_VCO = f(VCO clock) = f(PLLSAI clock input) * (PLLSAIN/PLLSAIM) */
  2661. /* SAIPCLK = PLLSAI_VCO / PLLSAIP */
  2662. /* SAIQCLK = PLLSAI_VCO / PLLSAIQ */
  2663. /* SAIRCLK = PLLSAI_VCO / PLLSAIR */
  2664. __HAL_RCC_PLLSAI_CONFIG(PLLSAIInit->PLLSAIM, PLLSAIInit->PLLSAIN, \
  2665. PLLSAIInit->PLLSAIP, PLLSAIInit->PLLSAIQ, 0U);
  2666. #elif defined(STM32F469xx) || defined(STM32F479xx)
  2667. /* PLLSAI_VCO = f(VCO clock) = f(PLLSAI clock input) * PLLSAIN */
  2668. /* SAIPCLK = PLLSAI_VCO / PLLSAIP */
  2669. /* SAIQCLK = PLLSAI_VCO / PLLSAIQ */
  2670. /* SAIRCLK = PLLSAI_VCO / PLLSAIR */
  2671. __HAL_RCC_PLLSAI_CONFIG(PLLSAIInit->PLLSAIN, PLLSAIInit->PLLSAIP, \
  2672. PLLSAIInit->PLLSAIQ, PLLSAIInit->PLLSAIR);
  2673. #else
  2674. /* PLLSAI_VCO = f(VCO clock) = f(PLLSAI clock input) x PLLSAIN */
  2675. /* SAIQCLK = PLLSAI_VCO / PLLSAIQ */
  2676. /* SAIRCLK = PLLSAI_VCO / PLLSAIR */
  2677. __HAL_RCC_PLLSAI_CONFIG(PLLSAIInit->PLLSAIN, PLLSAIInit->PLLSAIQ, PLLSAIInit->PLLSAIR);
  2678. #endif /* STM32F446xx */
  2679. /* Enable the PLLSAI */
  2680. __HAL_RCC_PLLSAI_ENABLE();
  2681. /* Wait till PLLSAI is ready */
  2682. tickstart = HAL_GetTick();
  2683. while(__HAL_RCC_PLLSAI_GET_FLAG() == RESET)
  2684. {
  2685. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  2686. {
  2687. /* return in case of Timeout detected */
  2688. return HAL_TIMEOUT;
  2689. }
  2690. }
  2691. return HAL_OK;
  2692. }
  2693. /**
  2694. * @brief Disable PLLSAI.
  2695. * @retval HAL status
  2696. */
  2697. HAL_StatusTypeDef HAL_RCCEx_DisablePLLSAI(void)
  2698. {
  2699. uint32_t tickstart;
  2700. /* Disable the PLLSAI */
  2701. __HAL_RCC_PLLSAI_DISABLE();
  2702. /* Wait till PLLSAI is disabled */
  2703. tickstart = HAL_GetTick();
  2704. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  2705. {
  2706. if((HAL_GetTick() - tickstart) > PLLSAI_TIMEOUT_VALUE)
  2707. {
  2708. /* return in case of Timeout detected */
  2709. return HAL_TIMEOUT;
  2710. }
  2711. }
  2712. return HAL_OK;
  2713. }
  2714. #endif /* RCC_PLLSAI_SUPPORT */
  2715. /**
  2716. * @}
  2717. */
  2718. #if defined(STM32F446xx)
  2719. /**
  2720. * @brief Returns the SYSCLK frequency
  2721. *
  2722. * @note This function implementation is valid only for STM32F446xx devices.
  2723. * @note This function add the PLL/PLLR System clock source
  2724. *
  2725. * @note The system frequency computed by this function is not the real
  2726. * frequency in the chip. It is calculated based on the predefined
  2727. * constant and the selected clock source:
  2728. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  2729. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  2730. * @note If SYSCLK source is PLL or PLLR, function returns values based on HSE_VALUE(**)
  2731. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  2732. * @note (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  2733. * 16 MHz) but the real value may vary depending on the variations
  2734. * in voltage and temperature.
  2735. * @note (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  2736. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  2737. * frequency of the crystal used. Otherwise, this function may
  2738. * have wrong result.
  2739. *
  2740. * @note The result of this function could be not correct when using fractional
  2741. * value for HSE crystal.
  2742. *
  2743. * @note This function can be used by the user application to compute the
  2744. * baudrate for the communication peripherals or configure other parameters.
  2745. *
  2746. * @note Each time SYSCLK changes, this function must be called to update the
  2747. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  2748. *
  2749. *
  2750. * @retval SYSCLK frequency
  2751. */
  2752. uint32_t HAL_RCC_GetSysClockFreq(void)
  2753. {
  2754. uint32_t pllm = 0U;
  2755. uint32_t pllvco = 0U;
  2756. uint32_t pllp = 0U;
  2757. uint32_t pllr = 0U;
  2758. uint32_t sysclockfreq = 0U;
  2759. /* Get SYSCLK source -------------------------------------------------------*/
  2760. switch (RCC->CFGR & RCC_CFGR_SWS)
  2761. {
  2762. case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
  2763. {
  2764. sysclockfreq = HSI_VALUE;
  2765. break;
  2766. }
  2767. case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
  2768. {
  2769. sysclockfreq = HSE_VALUE;
  2770. break;
  2771. }
  2772. case RCC_CFGR_SWS_PLL: /* PLL/PLLP used as system clock source */
  2773. {
  2774. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  2775. SYSCLK = PLL_VCO / PLLP */
  2776. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  2777. if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI)
  2778. {
  2779. /* HSE used as PLL clock source */
  2780. pllvco = (uint32_t) ((((uint64_t) HSE_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
  2781. }
  2782. else
  2783. {
  2784. /* HSI used as PLL clock source */
  2785. pllvco = (uint32_t) ((((uint64_t) HSI_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
  2786. }
  2787. pllp = ((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> RCC_PLLCFGR_PLLP_Pos) + 1U) *2U);
  2788. sysclockfreq = pllvco/pllp;
  2789. break;
  2790. }
  2791. case RCC_CFGR_SWS_PLLR: /* PLL/PLLR used as system clock source */
  2792. {
  2793. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  2794. SYSCLK = PLL_VCO / PLLR */
  2795. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  2796. if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI)
  2797. {
  2798. /* HSE used as PLL clock source */
  2799. pllvco = (uint32_t) ((((uint64_t) HSE_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
  2800. }
  2801. else
  2802. {
  2803. /* HSI used as PLL clock source */
  2804. pllvco = (uint32_t) ((((uint64_t) HSI_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
  2805. }
  2806. pllr = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos);
  2807. sysclockfreq = pllvco/pllr;
  2808. break;
  2809. }
  2810. default:
  2811. {
  2812. sysclockfreq = HSI_VALUE;
  2813. break;
  2814. }
  2815. }
  2816. return sysclockfreq;
  2817. }
  2818. #endif /* STM32F446xx */
  2819. /**
  2820. * @}
  2821. */
  2822. /**
  2823. * @}
  2824. */
  2825. /**
  2826. * @brief Resets the RCC clock configuration to the default reset state.
  2827. * @note The default reset state of the clock configuration is given below:
  2828. * - HSI ON and used as system clock source
  2829. * - HSE, PLL, PLLI2S and PLLSAI OFF
  2830. * - AHB, APB1 and APB2 prescaler set to 1.
  2831. * - CSS, MCO1 and MCO2 OFF
  2832. * - All interrupts disabled
  2833. * @note This function doesn't modify the configuration of the
  2834. * - Peripheral clocks
  2835. * - LSI, LSE and RTC clocks
  2836. * @retval HAL status
  2837. */
  2838. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  2839. {
  2840. uint32_t tickstart;
  2841. /* Get Start Tick */
  2842. tickstart = HAL_GetTick();
  2843. /* Set HSION bit to the reset value */
  2844. SET_BIT(RCC->CR, RCC_CR_HSION);
  2845. /* Wait till HSI is ready */
  2846. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == RESET)
  2847. {
  2848. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  2849. {
  2850. return HAL_TIMEOUT;
  2851. }
  2852. }
  2853. /* Set HSITRIM[4:0] bits to the reset value */
  2854. SET_BIT(RCC->CR, RCC_CR_HSITRIM_4);
  2855. /* Get Start Tick */
  2856. tickstart = HAL_GetTick();
  2857. /* Reset CFGR register */
  2858. CLEAR_REG(RCC->CFGR);
  2859. /* Wait till clock switch is ready */
  2860. while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != RESET)
  2861. {
  2862. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  2863. {
  2864. return HAL_TIMEOUT;
  2865. }
  2866. }
  2867. /* Get Start Tick */
  2868. tickstart = HAL_GetTick();
  2869. /* Clear HSEON, HSEBYP and CSSON bits */
  2870. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSEBYP | RCC_CR_CSSON);
  2871. /* Wait till HSE is disabled */
  2872. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) != RESET)
  2873. {
  2874. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  2875. {
  2876. return HAL_TIMEOUT;
  2877. }
  2878. }
  2879. /* Get Start Tick */
  2880. tickstart = HAL_GetTick();
  2881. /* Clear PLLON bit */
  2882. CLEAR_BIT(RCC->CR, RCC_CR_PLLON);
  2883. /* Wait till PLL is disabled */
  2884. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != RESET)
  2885. {
  2886. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  2887. {
  2888. return HAL_TIMEOUT;
  2889. }
  2890. }
  2891. #if defined(RCC_PLLI2S_SUPPORT)
  2892. /* Get Start Tick */
  2893. tickstart = HAL_GetTick();
  2894. /* Reset PLLI2SON bit */
  2895. CLEAR_BIT(RCC->CR, RCC_CR_PLLI2SON);
  2896. /* Wait till PLLI2S is disabled */
  2897. while (READ_BIT(RCC->CR, RCC_CR_PLLI2SRDY) != RESET)
  2898. {
  2899. if ((HAL_GetTick() - tickstart) > PLLI2S_TIMEOUT_VALUE)
  2900. {
  2901. return HAL_TIMEOUT;
  2902. }
  2903. }
  2904. #endif /* RCC_PLLI2S_SUPPORT */
  2905. #if defined(RCC_PLLSAI_SUPPORT)
  2906. /* Get Start Tick */
  2907. tickstart = HAL_GetTick();
  2908. /* Reset PLLSAI bit */
  2909. CLEAR_BIT(RCC->CR, RCC_CR_PLLSAION);
  2910. /* Wait till PLLSAI is disabled */
  2911. while (READ_BIT(RCC->CR, RCC_CR_PLLSAIRDY) != RESET)
  2912. {
  2913. if ((HAL_GetTick() - tickstart) > PLLSAI_TIMEOUT_VALUE)
  2914. {
  2915. return HAL_TIMEOUT;
  2916. }
  2917. }
  2918. #endif /* RCC_PLLSAI_SUPPORT */
  2919. /* Once PLL, PLLI2S and PLLSAI are OFF, reset PLLCFGR register to default value */
  2920. #if defined(STM32F412Cx) || defined(STM32F412Rx) || defined(STM32F412Vx) || defined(STM32F412Zx) || defined(STM32F413xx) || \
  2921. defined(STM32F423xx) || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
  2922. RCC->PLLCFGR = RCC_PLLCFGR_PLLM_4 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | RCC_PLLCFGR_PLLQ_2 | RCC_PLLCFGR_PLLR_1;
  2923. #elif defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx)
  2924. RCC->PLLCFGR = RCC_PLLCFGR_PLLR_0 | RCC_PLLCFGR_PLLR_1 | RCC_PLLCFGR_PLLR_2 | RCC_PLLCFGR_PLLM_4 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | RCC_PLLCFGR_PLLQ_0 | RCC_PLLCFGR_PLLQ_1 | RCC_PLLCFGR_PLLQ_2 | RCC_PLLCFGR_PLLQ_3;
  2925. #else
  2926. RCC->PLLCFGR = RCC_PLLCFGR_PLLM_4 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | RCC_PLLCFGR_PLLQ_2;
  2927. #endif /* STM32F412Cx || STM32F412Rx || STM32F412Vx || STM32F412Zx || STM32F413xx || STM32F423xx || STM32F446xx || STM32F469xx || STM32F479xx */
  2928. /* Reset PLLI2SCFGR register to default value */
  2929. #if defined(STM32F412Cx) || defined(STM32F412Rx) || defined(STM32F412Vx) || defined(STM32F412Zx) || defined(STM32F413xx) || \
  2930. defined(STM32F423xx) || defined(STM32F446xx)
  2931. RCC->PLLI2SCFGR = RCC_PLLI2SCFGR_PLLI2SM_4 | RCC_PLLI2SCFGR_PLLI2SN_6 | RCC_PLLI2SCFGR_PLLI2SN_7 | RCC_PLLI2SCFGR_PLLI2SQ_2 | RCC_PLLI2SCFGR_PLLI2SR_1;
  2932. #elif defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)
  2933. RCC->PLLI2SCFGR = RCC_PLLI2SCFGR_PLLI2SN_6 | RCC_PLLI2SCFGR_PLLI2SN_7 | RCC_PLLI2SCFGR_PLLI2SR_1;
  2934. #elif defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
  2935. RCC->PLLI2SCFGR = RCC_PLLI2SCFGR_PLLI2SN_6 | RCC_PLLI2SCFGR_PLLI2SN_7 | RCC_PLLI2SCFGR_PLLI2SQ_2 | RCC_PLLI2SCFGR_PLLI2SR_1;
  2936. #elif defined(STM32F411xE)
  2937. RCC->PLLI2SCFGR = RCC_PLLI2SCFGR_PLLI2SM_4 | RCC_PLLI2SCFGR_PLLI2SN_6 | RCC_PLLI2SCFGR_PLLI2SN_7 | RCC_PLLI2SCFGR_PLLI2SR_1;
  2938. #endif /* STM32F412Cx || STM32F412Rx || STM32F412Vx || STM32F412Zx || STM32F413xx || STM32F423xx || STM32F446xx */
  2939. /* Reset PLLSAICFGR register */
  2940. #if defined(STM32F427xx) || defined(STM32F429xx) || defined(STM32F437xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
  2941. RCC->PLLSAICFGR = RCC_PLLSAICFGR_PLLSAIN_6 | RCC_PLLSAICFGR_PLLSAIN_7 | RCC_PLLSAICFGR_PLLSAIQ_2 | RCC_PLLSAICFGR_PLLSAIR_1;
  2942. #elif defined(STM32F446xx)
  2943. RCC->PLLSAICFGR = RCC_PLLSAICFGR_PLLSAIM_4 | RCC_PLLSAICFGR_PLLSAIN_6 | RCC_PLLSAICFGR_PLLSAIN_7 | RCC_PLLSAICFGR_PLLSAIQ_2;
  2944. #endif /* STM32F427xx || STM32F429xx || STM32F437xx || STM32F439xx || STM32F469xx || STM32F479xx */
  2945. /* Disable all interrupts */
  2946. CLEAR_BIT(RCC->CIR, RCC_CIR_LSIRDYIE | RCC_CIR_LSERDYIE | RCC_CIR_HSIRDYIE | RCC_CIR_HSERDYIE | RCC_CIR_PLLRDYIE);
  2947. #if defined(RCC_CIR_PLLI2SRDYIE)
  2948. CLEAR_BIT(RCC->CIR, RCC_CIR_PLLI2SRDYIE);
  2949. #endif /* RCC_CIR_PLLI2SRDYIE */
  2950. #if defined(RCC_CIR_PLLSAIRDYIE)
  2951. CLEAR_BIT(RCC->CIR, RCC_CIR_PLLSAIRDYIE);
  2952. #endif /* RCC_CIR_PLLSAIRDYIE */
  2953. /* Clear all interrupt flags */
  2954. SET_BIT(RCC->CIR, RCC_CIR_LSIRDYC | RCC_CIR_LSERDYC | RCC_CIR_HSIRDYC | RCC_CIR_HSERDYC | RCC_CIR_PLLRDYC | RCC_CIR_CSSC);
  2955. #if defined(RCC_CIR_PLLI2SRDYC)
  2956. SET_BIT(RCC->CIR, RCC_CIR_PLLI2SRDYC);
  2957. #endif /* RCC_CIR_PLLI2SRDYC */
  2958. #if defined(RCC_CIR_PLLSAIRDYC)
  2959. SET_BIT(RCC->CIR, RCC_CIR_PLLSAIRDYC);
  2960. #endif /* RCC_CIR_PLLSAIRDYC */
  2961. /* Clear LSION bit */
  2962. CLEAR_BIT(RCC->CSR, RCC_CSR_LSION);
  2963. /* Reset all CSR flags */
  2964. SET_BIT(RCC->CSR, RCC_CSR_RMVF);
  2965. /* Update the SystemCoreClock global variable */
  2966. SystemCoreClock = HSI_VALUE;
  2967. /* Adapt Systick interrupt period */
  2968. if(HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
  2969. {
  2970. return HAL_ERROR;
  2971. }
  2972. else
  2973. {
  2974. return HAL_OK;
  2975. }
  2976. }
  2977. #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) ||\
  2978. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  2979. /**
  2980. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  2981. * RCC_OscInitTypeDef.
  2982. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  2983. * contains the configuration information for the RCC Oscillators.
  2984. * @note The PLL is not disabled when used as system clock.
  2985. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  2986. * supported by this API. User should request a transition to LSE Off
  2987. * first and then LSE On or LSE Bypass.
  2988. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  2989. * supported by this API. User should request a transition to HSE Off
  2990. * first and then HSE On or HSE Bypass.
  2991. * @note This function add the PLL/PLLR factor management during PLL configuration this feature
  2992. * is only available in STM32F410xx/STM32F446xx/STM32F469xx/STM32F479xx/STM32F412Zx/STM32F412Vx/STM32F412Rx/STM32F412Cx devices
  2993. * @retval HAL status
  2994. */
  2995. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  2996. {
  2997. uint32_t tickstart = 0U;
  2998. /* Check the parameters */
  2999. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  3000. /*------------------------------- HSE Configuration ------------------------*/
  3001. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  3002. {
  3003. /* Check the parameters */
  3004. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  3005. /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
  3006. #if defined(STM32F446xx)
  3007. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||\
  3008. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)) ||\
  3009. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLLR) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))
  3010. #else
  3011. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||\
  3012. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))
  3013. #endif /* STM32F446xx */
  3014. {
  3015. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  3016. {
  3017. return HAL_ERROR;
  3018. }
  3019. }
  3020. else
  3021. {
  3022. /* Set the new HSE configuration ---------------------------------------*/
  3023. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  3024. /* Check the HSE State */
  3025. if((RCC_OscInitStruct->HSEState) != RCC_HSE_OFF)
  3026. {
  3027. /* Get Start Tick*/
  3028. tickstart = HAL_GetTick();
  3029. /* Wait till HSE is ready */
  3030. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  3031. {
  3032. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  3033. {
  3034. return HAL_TIMEOUT;
  3035. }
  3036. }
  3037. }
  3038. else
  3039. {
  3040. /* Get Start Tick*/
  3041. tickstart = HAL_GetTick();
  3042. /* Wait till HSE is bypassed or disabled */
  3043. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  3044. {
  3045. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  3046. {
  3047. return HAL_TIMEOUT;
  3048. }
  3049. }
  3050. }
  3051. }
  3052. }
  3053. /*----------------------------- HSI Configuration --------------------------*/
  3054. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  3055. {
  3056. /* Check the parameters */
  3057. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  3058. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  3059. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  3060. #if defined(STM32F446xx)
  3061. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||\
  3062. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)) ||\
  3063. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLLR) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))
  3064. #else
  3065. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||\
  3066. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))
  3067. #endif /* STM32F446xx */
  3068. {
  3069. /* When HSI is used as system clock it will not disabled */
  3070. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  3071. {
  3072. return HAL_ERROR;
  3073. }
  3074. /* Otherwise, just the calibration is allowed */
  3075. else
  3076. {
  3077. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  3078. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  3079. }
  3080. }
  3081. else
  3082. {
  3083. /* Check the HSI State */
  3084. if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
  3085. {
  3086. /* Enable the Internal High Speed oscillator (HSI). */
  3087. __HAL_RCC_HSI_ENABLE();
  3088. /* Get Start Tick*/
  3089. tickstart = HAL_GetTick();
  3090. /* Wait till HSI is ready */
  3091. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  3092. {
  3093. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  3094. {
  3095. return HAL_TIMEOUT;
  3096. }
  3097. }
  3098. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  3099. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  3100. }
  3101. else
  3102. {
  3103. /* Disable the Internal High Speed oscillator (HSI). */
  3104. __HAL_RCC_HSI_DISABLE();
  3105. /* Get Start Tick*/
  3106. tickstart = HAL_GetTick();
  3107. /* Wait till HSI is ready */
  3108. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  3109. {
  3110. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  3111. {
  3112. return HAL_TIMEOUT;
  3113. }
  3114. }
  3115. }
  3116. }
  3117. }
  3118. /*------------------------------ LSI Configuration -------------------------*/
  3119. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  3120. {
  3121. /* Check the parameters */
  3122. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  3123. /* Check the LSI State */
  3124. if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
  3125. {
  3126. /* Enable the Internal Low Speed oscillator (LSI). */
  3127. __HAL_RCC_LSI_ENABLE();
  3128. /* Get Start Tick*/
  3129. tickstart = HAL_GetTick();
  3130. /* Wait till LSI is ready */
  3131. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  3132. {
  3133. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  3134. {
  3135. return HAL_TIMEOUT;
  3136. }
  3137. }
  3138. }
  3139. else
  3140. {
  3141. /* Disable the Internal Low Speed oscillator (LSI). */
  3142. __HAL_RCC_LSI_DISABLE();
  3143. /* Get Start Tick*/
  3144. tickstart = HAL_GetTick();
  3145. /* Wait till LSI is ready */
  3146. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  3147. {
  3148. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  3149. {
  3150. return HAL_TIMEOUT;
  3151. }
  3152. }
  3153. }
  3154. }
  3155. /*------------------------------ LSE Configuration -------------------------*/
  3156. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  3157. {
  3158. FlagStatus pwrclkchanged = RESET;
  3159. /* Check the parameters */
  3160. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  3161. /* Update LSE configuration in Backup Domain control register */
  3162. /* Requires to enable write access to Backup Domain of necessary */
  3163. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  3164. {
  3165. __HAL_RCC_PWR_CLK_ENABLE();
  3166. pwrclkchanged = SET;
  3167. }
  3168. if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  3169. {
  3170. /* Enable write access to Backup domain */
  3171. SET_BIT(PWR->CR, PWR_CR_DBP);
  3172. /* Wait for Backup domain Write protection disable */
  3173. tickstart = HAL_GetTick();
  3174. while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  3175. {
  3176. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  3177. {
  3178. return HAL_TIMEOUT;
  3179. }
  3180. }
  3181. }
  3182. /* Set the new LSE configuration -----------------------------------------*/
  3183. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  3184. /* Check the LSE State */
  3185. if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
  3186. {
  3187. /* Get Start Tick*/
  3188. tickstart = HAL_GetTick();
  3189. /* Wait till LSE is ready */
  3190. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  3191. {
  3192. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  3193. {
  3194. return HAL_TIMEOUT;
  3195. }
  3196. }
  3197. }
  3198. else
  3199. {
  3200. /* Get Start Tick*/
  3201. tickstart = HAL_GetTick();
  3202. /* Wait till LSE is ready */
  3203. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  3204. {
  3205. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  3206. {
  3207. return HAL_TIMEOUT;
  3208. }
  3209. }
  3210. }
  3211. /* Restore clock configuration if changed */
  3212. if(pwrclkchanged == SET)
  3213. {
  3214. __HAL_RCC_PWR_CLK_DISABLE();
  3215. }
  3216. }
  3217. /*-------------------------------- PLL Configuration -----------------------*/
  3218. /* Check the parameters */
  3219. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  3220. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  3221. {
  3222. /* Check if the PLL is used as system clock or not */
  3223. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  3224. {
  3225. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  3226. {
  3227. /* Check the parameters */
  3228. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  3229. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  3230. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  3231. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  3232. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  3233. assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
  3234. /* Disable the main PLL. */
  3235. __HAL_RCC_PLL_DISABLE();
  3236. /* Get Start Tick*/
  3237. tickstart = HAL_GetTick();
  3238. /* Wait till PLL is ready */
  3239. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  3240. {
  3241. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  3242. {
  3243. return HAL_TIMEOUT;
  3244. }
  3245. }
  3246. /* Configure the main PLL clock source, multiplication and division factors. */
  3247. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  3248. RCC_OscInitStruct->PLL.PLLM,
  3249. RCC_OscInitStruct->PLL.PLLN,
  3250. RCC_OscInitStruct->PLL.PLLP,
  3251. RCC_OscInitStruct->PLL.PLLQ,
  3252. RCC_OscInitStruct->PLL.PLLR);
  3253. /* Enable the main PLL. */
  3254. __HAL_RCC_PLL_ENABLE();
  3255. /* Get Start Tick*/
  3256. tickstart = HAL_GetTick();
  3257. /* Wait till PLL is ready */
  3258. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  3259. {
  3260. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  3261. {
  3262. return HAL_TIMEOUT;
  3263. }
  3264. }
  3265. }
  3266. else
  3267. {
  3268. /* Disable the main PLL. */
  3269. __HAL_RCC_PLL_DISABLE();
  3270. /* Get Start Tick*/
  3271. tickstart = HAL_GetTick();
  3272. /* Wait till PLL is ready */
  3273. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  3274. {
  3275. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  3276. {
  3277. return HAL_TIMEOUT;
  3278. }
  3279. }
  3280. }
  3281. }
  3282. else
  3283. {
  3284. return HAL_ERROR;
  3285. }
  3286. }
  3287. return HAL_OK;
  3288. }
  3289. /**
  3290. * @brief Configures the RCC_OscInitStruct according to the internal
  3291. * RCC configuration registers.
  3292. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that will be configured.
  3293. *
  3294. * @note This function is only available in case of STM32F410xx/STM32F446xx/STM32F469xx/STM32F479xx/STM32F412Zx/STM32F412Vx/STM32F412Rx/STM32F412Cx devices.
  3295. * @note This function add the PLL/PLLR factor management
  3296. * @retval None
  3297. */
  3298. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  3299. {
  3300. /* Set all possible values for the Oscillator type parameter ---------------*/
  3301. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  3302. /* Get the HSE configuration -----------------------------------------------*/
  3303. if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  3304. {
  3305. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  3306. }
  3307. else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
  3308. {
  3309. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  3310. }
  3311. else
  3312. {
  3313. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  3314. }
  3315. /* Get the HSI configuration -----------------------------------------------*/
  3316. if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
  3317. {
  3318. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  3319. }
  3320. else
  3321. {
  3322. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  3323. }
  3324. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR &RCC_CR_HSITRIM) >> RCC_CR_HSITRIM_Pos);
  3325. /* Get the LSE configuration -----------------------------------------------*/
  3326. if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  3327. {
  3328. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  3329. }
  3330. else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  3331. {
  3332. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  3333. }
  3334. else
  3335. {
  3336. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  3337. }
  3338. /* Get the LSI configuration -----------------------------------------------*/
  3339. if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
  3340. {
  3341. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  3342. }
  3343. else
  3344. {
  3345. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  3346. }
  3347. /* Get the PLL configuration -----------------------------------------------*/
  3348. if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
  3349. {
  3350. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  3351. }
  3352. else
  3353. {
  3354. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  3355. }
  3356. RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  3357. RCC_OscInitStruct->PLL.PLLM = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM);
  3358. RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  3359. RCC_OscInitStruct->PLL.PLLP = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) + RCC_PLLCFGR_PLLP_0) << 1U) >> RCC_PLLCFGR_PLLP_Pos);
  3360. RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos);
  3361. RCC_OscInitStruct->PLL.PLLR = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos);
  3362. }
  3363. #endif /* STM32F410xx || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
  3364. #endif /* HAL_RCC_MODULE_ENABLED */
  3365. /**
  3366. * @}
  3367. */
  3368. /**
  3369. * @}
  3370. */
  3371. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/