You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2604 lines
108 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_rcc_ex.c
  4. * @author MCD Application Team
  5. * @version V1.5.2
  6. * @date 22-September-2016
  7. * @brief Extension RCC HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities RCC extension peripheral:
  10. * + Extended Peripheral Control functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  16. *
  17. * Redistribution and use in source and binary forms, with or without modification,
  18. * are permitted provided that the following conditions are met:
  19. * 1. Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ******************************************************************************
  40. */
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32f4xx_hal.h"
  43. /** @addtogroup STM32F4xx_HAL_Driver
  44. * @{
  45. */
  46. /** @defgroup RCCEx RCCEx
  47. * @brief RCCEx HAL module driver
  48. * @{
  49. */
  50. #ifdef HAL_RCC_MODULE_ENABLED
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* Private define ------------------------------------------------------------*/
  53. /** @addtogroup RCCEx_Private_Constants
  54. * @{
  55. */
  56. /**
  57. * @}
  58. */
  59. /* Private macro -------------------------------------------------------------*/
  60. /* Private variables ---------------------------------------------------------*/
  61. /* Private function prototypes -----------------------------------------------*/
  62. /* Private functions ---------------------------------------------------------*/
  63. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  64. * @{
  65. */
  66. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  67. * @brief Extended Peripheral Control functions
  68. *
  69. @verbatim
  70. ===============================================================================
  71. ##### Extended Peripheral Control functions #####
  72. ===============================================================================
  73. [..]
  74. This subsection provides a set of functions allowing to control the RCC Clocks
  75. frequencies.
  76. [..]
  77. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  78. select the RTC clock source; in this case the Backup domain will be reset in
  79. order to modify the RTC Clock source, as consequence RTC registers (including
  80. the backup registers) and RCC_BDCR register are set to their reset values.
  81. @endverbatim
  82. * @{
  83. */
  84. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || \
  85. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
  86. defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) || \
  87. defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
  88. defined(STM32F412Rx) || defined(STM32F412Cx)
  89. /**
  90. * @brief Resets the RCC clock configuration to the default reset state.
  91. * @note The default reset state of the clock configuration is given below:
  92. * - HSI ON and used as system clock source
  93. * - HSE, PLL and PLLI2S OFF
  94. * - AHB, APB1 and APB2 prescaler set to 1.
  95. * - CSS, MCO1 and MCO2 OFF
  96. * - All interrupts disabled
  97. * @note This function doesn't modify the configuration of the
  98. * - Peripheral clocks
  99. * - LSI, LSE and RTC clocks
  100. * @retval None
  101. */
  102. void HAL_RCC_DeInit(void)
  103. {
  104. /* Set HSION bit */
  105. SET_BIT(RCC->CR, RCC_CR_HSION | RCC_CR_HSITRIM_4);
  106. /* Reset CFGR register */
  107. CLEAR_REG(RCC->CFGR);
  108. /* Reset HSEON, CSSON, PLLON, PLLI2S */
  109. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_CSSON | RCC_CR_PLLON| RCC_CR_PLLI2SON);
  110. /* Reset PLLCFGR register */
  111. CLEAR_REG(RCC->PLLCFGR);
  112. SET_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM_4 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | RCC_PLLCFGR_PLLQ_2);
  113. /* Reset PLLI2SCFGR register */
  114. CLEAR_REG(RCC->PLLI2SCFGR);
  115. SET_BIT(RCC->PLLI2SCFGR, RCC_PLLI2SCFGR_PLLI2SN_6 | RCC_PLLI2SCFGR_PLLI2SN_7 | RCC_PLLI2SCFGR_PLLI2SR_1);
  116. /* Reset HSEBYP bit */
  117. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  118. /* Disable all interrupts */
  119. CLEAR_REG(RCC->CIR);
  120. /* Update the SystemCoreClock global variable */
  121. SystemCoreClock = HSI_VALUE;
  122. }
  123. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||
  124. STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx ||
  125. STM32F412Rx || STM32F412Cx */
  126. #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx)
  127. /**
  128. * @brief Resets the RCC clock configuration to the default reset state.
  129. * @note The default reset state of the clock configuration is given below:
  130. * - HSI ON and used as system clock source
  131. * - HSE and PLL OFF
  132. * - AHB, APB1 and APB2 prescaler set to 1.
  133. * - CSS, MCO1 and MCO2 OFF
  134. * - All interrupts disabled
  135. * @note This function doesn't modify the configuration of the
  136. * - Peripheral clocks
  137. * - LSI, LSE and RTC clocks
  138. * @retval None
  139. */
  140. void HAL_RCC_DeInit(void)
  141. {
  142. /* Set HSION bit */
  143. SET_BIT(RCC->CR, RCC_CR_HSION | RCC_CR_HSITRIM_4);
  144. /* Reset CFGR register */
  145. CLEAR_REG(RCC->CFGR);
  146. /* Reset HSEON, CSSON, PLLON */
  147. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_CSSON | RCC_CR_PLLON);
  148. /* Reset PLLCFGR register */
  149. CLEAR_REG(RCC->PLLCFGR);
  150. SET_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR_1 | RCC_PLLCFGR_PLLM_4 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | RCC_PLLCFGR_PLLQ_2);
  151. /* Reset HSEBYP bit */
  152. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  153. /* Disable all interrupts */
  154. CLEAR_REG(RCC->CIR);
  155. /* Update the SystemCoreClock global variable */
  156. SystemCoreClock = HSI_VALUE;
  157. }
  158. #endif /* STM32F410Tx || STM32F410Cx || STM32F410Rx */
  159. #if defined(STM32F446xx)
  160. /**
  161. * @brief Initializes the RCC extended peripherals clocks according to the specified
  162. * parameters in the RCC_PeriphCLKInitTypeDef.
  163. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  164. * contains the configuration information for the Extended Peripherals
  165. * clocks(I2S, SAI, LTDC RTC and TIM).
  166. *
  167. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  168. * the RTC clock source; in this case the Backup domain will be reset in
  169. * order to modify the RTC Clock source, as consequence RTC registers (including
  170. * the backup registers) and RCC_BDCR register are set to their reset values.
  171. *
  172. * @retval HAL status
  173. */
  174. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  175. {
  176. uint32_t tickstart = 0U;
  177. uint32_t tmpreg1 = 0U;
  178. uint32_t plli2sp = 0U;
  179. uint32_t plli2sq = 0U;
  180. uint32_t plli2sr = 0U;
  181. uint32_t pllsaip = 0U;
  182. uint32_t pllsaiq = 0U;
  183. uint32_t plli2sused = 0U;
  184. uint32_t pllsaiused = 0U;
  185. /* Check the peripheral clock selection parameters */
  186. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  187. /*------------------------ I2S APB1 configuration --------------------------*/
  188. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB1) == (RCC_PERIPHCLK_I2S_APB1))
  189. {
  190. /* Check the parameters */
  191. assert_param(IS_RCC_I2SAPB1CLKSOURCE(PeriphClkInit->I2sApb1ClockSelection));
  192. /* Configure I2S Clock source */
  193. __HAL_RCC_I2S_APB1_CONFIG(PeriphClkInit->I2sApb1ClockSelection);
  194. /* Enable the PLLI2S when it's used as clock source for I2S */
  195. if(PeriphClkInit->I2sApb1ClockSelection == RCC_I2SAPB1CLKSOURCE_PLLI2S)
  196. {
  197. plli2sused = 1U;
  198. }
  199. }
  200. /*--------------------------------------------------------------------------*/
  201. /*---------------------------- I2S APB2 configuration ----------------------*/
  202. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB2) == (RCC_PERIPHCLK_I2S_APB2))
  203. {
  204. /* Check the parameters */
  205. assert_param(IS_RCC_I2SAPB2CLKSOURCE(PeriphClkInit->I2sApb2ClockSelection));
  206. /* Configure I2S Clock source */
  207. __HAL_RCC_I2S_APB2_CONFIG(PeriphClkInit->I2sApb2ClockSelection);
  208. /* Enable the PLLI2S when it's used as clock source for I2S */
  209. if(PeriphClkInit->I2sApb2ClockSelection == RCC_I2SAPB2CLKSOURCE_PLLI2S)
  210. {
  211. plli2sused = 1U;
  212. }
  213. }
  214. /*--------------------------------------------------------------------------*/
  215. /*--------------------------- SAI1 configuration ---------------------------*/
  216. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == (RCC_PERIPHCLK_SAI1))
  217. {
  218. /* Check the parameters */
  219. assert_param(IS_RCC_SAI1CLKSOURCE(PeriphClkInit->Sai1ClockSelection));
  220. /* Configure SAI1 Clock source */
  221. __HAL_RCC_SAI1_CONFIG(PeriphClkInit->Sai1ClockSelection);
  222. /* Enable the PLLI2S when it's used as clock source for SAI */
  223. if(PeriphClkInit->Sai1ClockSelection == RCC_SAI1CLKSOURCE_PLLI2S)
  224. {
  225. plli2sused = 1U;
  226. }
  227. /* Enable the PLLSAI when it's used as clock source for SAI */
  228. if(PeriphClkInit->Sai1ClockSelection == RCC_SAI1CLKSOURCE_PLLSAI)
  229. {
  230. pllsaiused = 1U;
  231. }
  232. }
  233. /*--------------------------------------------------------------------------*/
  234. /*-------------------------- SAI2 configuration ----------------------------*/
  235. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI2) == (RCC_PERIPHCLK_SAI2))
  236. {
  237. /* Check the parameters */
  238. assert_param(IS_RCC_SAI2CLKSOURCE(PeriphClkInit->Sai2ClockSelection));
  239. /* Configure SAI2 Clock source */
  240. __HAL_RCC_SAI2_CONFIG(PeriphClkInit->Sai2ClockSelection);
  241. /* Enable the PLLI2S when it's used as clock source for SAI */
  242. if(PeriphClkInit->Sai2ClockSelection == RCC_SAI2CLKSOURCE_PLLI2S)
  243. {
  244. plli2sused = 1U;
  245. }
  246. /* Enable the PLLSAI when it's used as clock source for SAI */
  247. if(PeriphClkInit->Sai2ClockSelection == RCC_SAI2CLKSOURCE_PLLSAI)
  248. {
  249. pllsaiused = 1U;
  250. }
  251. }
  252. /*--------------------------------------------------------------------------*/
  253. /*----------------------------- RTC configuration --------------------------*/
  254. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  255. {
  256. /* Check for RTC Parameters used to output RTCCLK */
  257. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  258. /* Enable Power Clock*/
  259. __HAL_RCC_PWR_CLK_ENABLE();
  260. /* Enable write access to Backup domain */
  261. PWR->CR |= PWR_CR_DBP;
  262. /* Get tick */
  263. tickstart = HAL_GetTick();
  264. while((PWR->CR & PWR_CR_DBP) == RESET)
  265. {
  266. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  267. {
  268. return HAL_TIMEOUT;
  269. }
  270. }
  271. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  272. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  273. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  274. {
  275. /* Store the content of BDCR register before the reset of Backup Domain */
  276. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  277. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  278. __HAL_RCC_BACKUPRESET_FORCE();
  279. __HAL_RCC_BACKUPRESET_RELEASE();
  280. /* Restore the Content of BDCR register */
  281. RCC->BDCR = tmpreg1;
  282. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  283. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  284. {
  285. /* Get tick */
  286. tickstart = HAL_GetTick();
  287. /* Wait till LSE is ready */
  288. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  289. {
  290. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  291. {
  292. return HAL_TIMEOUT;
  293. }
  294. }
  295. }
  296. }
  297. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  298. }
  299. /*--------------------------------------------------------------------------*/
  300. /*---------------------------- TIM configuration ---------------------------*/
  301. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  302. {
  303. /* Configure Timer Prescaler */
  304. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  305. }
  306. /*--------------------------------------------------------------------------*/
  307. /*---------------------------- FMPI2C1 Configuration -----------------------*/
  308. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_FMPI2C1) == RCC_PERIPHCLK_FMPI2C1)
  309. {
  310. /* Check the parameters */
  311. assert_param(IS_RCC_FMPI2C1CLKSOURCE(PeriphClkInit->Fmpi2c1ClockSelection));
  312. /* Configure the FMPI2C1 clock source */
  313. __HAL_RCC_FMPI2C1_CONFIG(PeriphClkInit->Fmpi2c1ClockSelection);
  314. }
  315. /*--------------------------------------------------------------------------*/
  316. /*------------------------------ CEC Configuration -------------------------*/
  317. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CEC) == RCC_PERIPHCLK_CEC)
  318. {
  319. /* Check the parameters */
  320. assert_param(IS_RCC_CECCLKSOURCE(PeriphClkInit->CecClockSelection));
  321. /* Configure the CEC clock source */
  322. __HAL_RCC_CEC_CONFIG(PeriphClkInit->CecClockSelection);
  323. }
  324. /*--------------------------------------------------------------------------*/
  325. /*----------------------------- CLK48 Configuration ------------------------*/
  326. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48)
  327. {
  328. /* Check the parameters */
  329. assert_param(IS_RCC_CLK48CLKSOURCE(PeriphClkInit->Clk48ClockSelection));
  330. /* Configure the CLK48 clock source */
  331. __HAL_RCC_CLK48_CONFIG(PeriphClkInit->Clk48ClockSelection);
  332. /* Enable the PLLSAI when it's used as clock source for CLK48 */
  333. if(PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLSAIP)
  334. {
  335. pllsaiused = 1U;
  336. }
  337. }
  338. /*--------------------------------------------------------------------------*/
  339. /*----------------------------- SDIO Configuration -------------------------*/
  340. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDIO) == RCC_PERIPHCLK_SDIO)
  341. {
  342. /* Check the parameters */
  343. assert_param(IS_RCC_SDIOCLKSOURCE(PeriphClkInit->SdioClockSelection));
  344. /* Configure the SDIO clock source */
  345. __HAL_RCC_SDIO_CONFIG(PeriphClkInit->SdioClockSelection);
  346. }
  347. /*--------------------------------------------------------------------------*/
  348. /*------------------------------ SPDIFRX Configuration ---------------------*/
  349. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SPDIFRX) == RCC_PERIPHCLK_SPDIFRX)
  350. {
  351. /* Check the parameters */
  352. assert_param(IS_RCC_SPDIFRXCLKSOURCE(PeriphClkInit->SpdifClockSelection));
  353. /* Configure the SPDIFRX clock source */
  354. __HAL_RCC_SPDIFRX_CONFIG(PeriphClkInit->SpdifClockSelection);
  355. /* Enable the PLLI2S when it's used as clock source for SPDIFRX */
  356. if(PeriphClkInit->SpdifClockSelection == RCC_SPDIFRXCLKSOURCE_PLLI2SP)
  357. {
  358. plli2sused = 1U;
  359. }
  360. }
  361. /*--------------------------------------------------------------------------*/
  362. /*---------------------------- PLLI2S Configuration ------------------------*/
  363. /* PLLI2S is configured when a peripheral will use it as source clock : SAI1, SAI2, I2S on APB1,
  364. I2S on APB2 or SPDIFRX */
  365. if((plli2sused == 1U) || (PeriphClkInit->PeriphClockSelection == RCC_PERIPHCLK_PLLI2S))
  366. {
  367. /* Disable the PLLI2S */
  368. __HAL_RCC_PLLI2S_DISABLE();
  369. /* Get tick */
  370. tickstart = HAL_GetTick();
  371. /* Wait till PLLI2S is disabled */
  372. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  373. {
  374. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  375. {
  376. /* return in case of Timeout detected */
  377. return HAL_TIMEOUT;
  378. }
  379. }
  380. /* check for common PLLI2S Parameters */
  381. assert_param(IS_RCC_PLLI2SM_VALUE(PeriphClkInit->PLLI2S.PLLI2SM));
  382. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  383. /*------ In Case of PLLI2S is selected as source clock for I2S -----------*/
  384. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB1) == RCC_PERIPHCLK_I2S_APB1) && (PeriphClkInit->I2sApb1ClockSelection == RCC_I2SAPB1CLKSOURCE_PLLI2S)) ||
  385. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB2) == RCC_PERIPHCLK_I2S_APB2) && (PeriphClkInit->I2sApb2ClockSelection == RCC_I2SAPB2CLKSOURCE_PLLI2S)))
  386. {
  387. /* check for Parameters */
  388. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  389. /* Read PLLI2SP/PLLI2SQ value from PLLI2SCFGR register (this value is not needed for I2S configuration) */
  390. plli2sp = ((((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SP) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SP)) + 1U) << 1U);
  391. plli2sq = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SQ));
  392. /* Configure the PLLI2S division factors */
  393. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  394. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  395. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , plli2sp, plli2sq, PeriphClkInit->PLLI2S.PLLI2SR);
  396. }
  397. /*------- In Case of PLLI2S is selected as source clock for SAI ----------*/
  398. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == RCC_PERIPHCLK_SAI1) && (PeriphClkInit->Sai1ClockSelection == RCC_SAI1CLKSOURCE_PLLI2S)) ||
  399. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI2) == RCC_PERIPHCLK_SAI2) && (PeriphClkInit->Sai2ClockSelection == RCC_SAI2CLKSOURCE_PLLI2S)))
  400. {
  401. /* Check for PLLI2S Parameters */
  402. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  403. /* Check for PLLI2S/DIVQ parameters */
  404. assert_param(IS_RCC_PLLI2S_DIVQ_VALUE(PeriphClkInit->PLLI2SDivQ));
  405. /* Read PLLI2SP/PLLI2SR value from PLLI2SCFGR register (this value is not needed for SAI configuration) */
  406. plli2sp = ((((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SP) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SP)) + 1U) << 1U);
  407. plli2sr = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SR));
  408. /* Configure the PLLI2S division factors */
  409. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  410. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  411. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  412. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , plli2sp, PeriphClkInit->PLLI2S.PLLI2SQ, plli2sr);
  413. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  414. __HAL_RCC_PLLI2S_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLI2SDivQ);
  415. }
  416. /*------ In Case of PLLI2S is selected as source clock for SPDIFRX -------*/
  417. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SPDIFRX) == RCC_PERIPHCLK_SPDIFRX) && (PeriphClkInit->SpdifClockSelection == RCC_SPDIFRXCLKSOURCE_PLLI2SP))
  418. {
  419. /* check for Parameters */
  420. assert_param(IS_RCC_PLLI2SP_VALUE(PeriphClkInit->PLLI2S.PLLI2SP));
  421. /* Read PLLI2SR value from PLLI2SCFGR register (this value is not need for SAI configuration) */
  422. plli2sq = ((((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SP) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SP)) + 1U) << 1U);
  423. plli2sr = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SR));
  424. /* Configure the PLLI2S division factors */
  425. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  426. /* SPDIFRXCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SP */
  427. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SP, plli2sq, plli2sr);
  428. }
  429. /*----------------- In Case of PLLI2S is just selected -----------------*/
  430. if((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S)
  431. {
  432. /* Check for Parameters */
  433. assert_param(IS_RCC_PLLI2SP_VALUE(PeriphClkInit->PLLI2S.PLLI2SP));
  434. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  435. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  436. /* Configure the PLLI2S division factors */
  437. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  438. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SP, PeriphClkInit->PLLI2S.PLLI2SQ, PeriphClkInit->PLLI2S.PLLI2SR);
  439. }
  440. /* Enable the PLLI2S */
  441. __HAL_RCC_PLLI2S_ENABLE();
  442. /* Get tick */
  443. tickstart = HAL_GetTick();
  444. /* Wait till PLLI2S is ready */
  445. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  446. {
  447. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  448. {
  449. /* return in case of Timeout detected */
  450. return HAL_TIMEOUT;
  451. }
  452. }
  453. }
  454. /*--------------------------------------------------------------------------*/
  455. /*----------------------------- PLLSAI Configuration -----------------------*/
  456. /* PLLSAI is configured when a peripheral will use it as source clock : SAI1, SAI2, CLK48 or SDIO */
  457. if(pllsaiused == 1U)
  458. {
  459. /* Disable PLLSAI Clock */
  460. __HAL_RCC_PLLSAI_DISABLE();
  461. /* Get tick */
  462. tickstart = HAL_GetTick();
  463. /* Wait till PLLSAI is disabled */
  464. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  465. {
  466. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  467. {
  468. /* return in case of Timeout detected */
  469. return HAL_TIMEOUT;
  470. }
  471. }
  472. /* Check the PLLSAI division factors */
  473. assert_param(IS_RCC_PLLSAIM_VALUE(PeriphClkInit->PLLSAI.PLLSAIM));
  474. assert_param(IS_RCC_PLLSAIN_VALUE(PeriphClkInit->PLLSAI.PLLSAIN));
  475. /*------ In Case of PLLSAI is selected as source clock for SAI -----------*/
  476. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == RCC_PERIPHCLK_SAI1) && (PeriphClkInit->Sai1ClockSelection == RCC_SAI1CLKSOURCE_PLLSAI)) ||
  477. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI2) == RCC_PERIPHCLK_SAI2) && (PeriphClkInit->Sai2ClockSelection == RCC_SAI2CLKSOURCE_PLLSAI)))
  478. {
  479. /* check for PLLSAIQ Parameter */
  480. assert_param(IS_RCC_PLLSAIQ_VALUE(PeriphClkInit->PLLSAI.PLLSAIQ));
  481. /* check for PLLSAI/DIVQ Parameter */
  482. assert_param(IS_RCC_PLLSAI_DIVQ_VALUE(PeriphClkInit->PLLSAIDivQ));
  483. /* Read PLLSAIP value from PLLSAICFGR register (this value is not needed for SAI configuration) */
  484. pllsaip = ((((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIP) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIP)) + 1U) << 1U);
  485. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  486. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  487. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  488. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIM, PeriphClkInit->PLLSAI.PLLSAIN , pllsaip, PeriphClkInit->PLLSAI.PLLSAIQ, 0U);
  489. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  490. __HAL_RCC_PLLSAI_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLSAIDivQ);
  491. }
  492. /*------ In Case of PLLSAI is selected as source clock for CLK48 ---------*/
  493. /* In Case of PLLI2S is selected as source clock for CLK48 */
  494. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48) && (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLSAIP))
  495. {
  496. /* check for Parameters */
  497. assert_param(IS_RCC_PLLSAIP_VALUE(PeriphClkInit->PLLSAI.PLLSAIP));
  498. /* Read PLLSAIQ value from PLLI2SCFGR register (this value is not need for SAI configuration) */
  499. pllsaiq = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIQ));
  500. /* Configure the PLLSAI division factors */
  501. /* PLLSAI_VCO = f(VCO clock) = f(PLLSAI clock input) * (PLLI2SN/PLLSAIM) */
  502. /* 48CLK = f(PLLSAI clock output) = f(VCO clock) / PLLSAIP */
  503. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIM, PeriphClkInit->PLLSAI.PLLSAIN , PeriphClkInit->PLLSAI.PLLSAIP, pllsaiq, 0U);
  504. }
  505. /* Enable PLLSAI Clock */
  506. __HAL_RCC_PLLSAI_ENABLE();
  507. /* Get tick */
  508. tickstart = HAL_GetTick();
  509. /* Wait till PLLSAI is ready */
  510. while(__HAL_RCC_PLLSAI_GET_FLAG() == RESET)
  511. {
  512. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  513. {
  514. /* return in case of Timeout detected */
  515. return HAL_TIMEOUT;
  516. }
  517. }
  518. }
  519. return HAL_OK;
  520. }
  521. /**
  522. * @brief Get the RCC_PeriphCLKInitTypeDef according to the internal
  523. * RCC configuration registers.
  524. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  525. * will be configured.
  526. * @retval None
  527. */
  528. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  529. {
  530. uint32_t tempreg;
  531. /* Set all possible values for the extended clock type parameter------------*/
  532. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_I2S_APB2 |\
  533. RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_SAI2 |\
  534. RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC |\
  535. RCC_PERIPHCLK_CEC | RCC_PERIPHCLK_FMPI2C1 |\
  536. RCC_PERIPHCLK_CLK48 | RCC_PERIPHCLK_SDIO |\
  537. RCC_PERIPHCLK_SPDIFRX;
  538. /* Get the PLLI2S Clock configuration --------------------------------------*/
  539. PeriphClkInit->PLLI2S.PLLI2SM = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SM));
  540. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SN));
  541. PeriphClkInit->PLLI2S.PLLI2SP = (uint32_t)((((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SP) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SP)) + 1U) << 1U);
  542. PeriphClkInit->PLLI2S.PLLI2SQ = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SQ));
  543. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SR));
  544. /* Get the PLLSAI Clock configuration --------------------------------------*/
  545. PeriphClkInit->PLLSAI.PLLSAIM = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIM) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIM));
  546. PeriphClkInit->PLLSAI.PLLSAIN = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIN));
  547. PeriphClkInit->PLLSAI.PLLSAIP = (uint32_t)((((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIP) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIP)) + 1U) << 1U);
  548. PeriphClkInit->PLLSAI.PLLSAIQ = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIQ));
  549. /* Get the PLLSAI/PLLI2S division factors ----------------------------------*/
  550. PeriphClkInit->PLLI2SDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) >> POSITION_VAL(RCC_DCKCFGR_PLLI2SDIVQ));
  551. PeriphClkInit->PLLSAIDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> POSITION_VAL(RCC_DCKCFGR_PLLSAIDIVQ));
  552. /* Get the SAI1 clock configuration ----------------------------------------*/
  553. PeriphClkInit->Sai1ClockSelection = __HAL_RCC_GET_SAI1_SOURCE();
  554. /* Get the SAI2 clock configuration ----------------------------------------*/
  555. PeriphClkInit->Sai2ClockSelection = __HAL_RCC_GET_SAI2_SOURCE();
  556. /* Get the I2S APB1 clock configuration ------------------------------------*/
  557. PeriphClkInit->I2sApb1ClockSelection = __HAL_RCC_GET_I2S_APB1_SOURCE();
  558. /* Get the I2S APB2 clock configuration ------------------------------------*/
  559. PeriphClkInit->I2sApb2ClockSelection = __HAL_RCC_GET_I2S_APB2_SOURCE();
  560. /* Get the RTC Clock configuration -----------------------------------------*/
  561. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  562. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  563. /* Get the CEC clock configuration -----------------------------------------*/
  564. PeriphClkInit->CecClockSelection = __HAL_RCC_GET_CEC_SOURCE();
  565. /* Get the FMPI2C1 clock configuration -------------------------------------*/
  566. PeriphClkInit->Fmpi2c1ClockSelection = __HAL_RCC_GET_FMPI2C1_SOURCE();
  567. /* Get the CLK48 clock configuration ----------------------------------------*/
  568. PeriphClkInit->Clk48ClockSelection = __HAL_RCC_GET_CLK48_SOURCE();
  569. /* Get the SDIO clock configuration ----------------------------------------*/
  570. PeriphClkInit->SdioClockSelection = __HAL_RCC_GET_SDIO_SOURCE();
  571. /* Get the SPDIFRX clock configuration -------------------------------------*/
  572. PeriphClkInit->SpdifClockSelection = __HAL_RCC_GET_SPDIFRX_SOURCE();
  573. /* Get the TIM Prescaler configuration -------------------------------------*/
  574. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  575. {
  576. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  577. }
  578. else
  579. {
  580. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  581. }
  582. }
  583. /**
  584. * @brief Return the peripheral clock frequency for a given peripheral(SAI..)
  585. * @note Return 0 if peripheral clock identifier not managed by this API
  586. * @param PeriphClk: Peripheral clock identifier
  587. * This parameter can be one of the following values:
  588. * @arg RCC_PERIPHCLK_SAI1: SAI1 peripheral clock
  589. * @arg RCC_PERIPHCLK_SAI2: SAI2 peripheral clock
  590. * @retval Frequency in KHz
  591. */
  592. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  593. {
  594. uint32_t tmpreg1 = 0U;
  595. /* This variable used to store the SAI clock frequency (value in Hz) */
  596. uint32_t frequency = 0U;
  597. /* This variable used to store the VCO Input (value in Hz) */
  598. uint32_t vcoinput = 0U;
  599. /* This variable used to store the SAI clock source */
  600. uint32_t saiclocksource = 0U;
  601. if ((PeriphClk == RCC_PERIPHCLK_SAI1) || (PeriphClk == RCC_PERIPHCLK_SAI2))
  602. {
  603. saiclocksource = RCC->DCKCFGR;
  604. saiclocksource &= (RCC_DCKCFGR_SAI1SRC | RCC_DCKCFGR_SAI2SRC);
  605. switch (saiclocksource)
  606. {
  607. case 0U: /* PLLSAI is the clock source for SAI*/
  608. {
  609. /* Configure the PLLSAI division factor */
  610. /* PLLSAI_VCO Input = PLL_SOURCE/PLLSAIM */
  611. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  612. {
  613. /* In Case the PLL Source is HSI (Internal Clock) */
  614. vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIM));
  615. }
  616. else
  617. {
  618. /* In Case the PLL Source is HSE (External Clock) */
  619. vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIM)));
  620. }
  621. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  622. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  623. tmpreg1 = (RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> 24U;
  624. frequency = (vcoinput * ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> 6U))/(tmpreg1);
  625. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  626. tmpreg1 = (((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> 8U) + 1U);
  627. frequency = frequency/(tmpreg1);
  628. break;
  629. }
  630. case RCC_DCKCFGR_SAI1SRC_0: /* PLLI2S is the clock source for SAI*/
  631. case RCC_DCKCFGR_SAI2SRC_0: /* PLLI2S is the clock source for SAI*/
  632. {
  633. /* Configure the PLLI2S division factor */
  634. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  635. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  636. {
  637. /* In Case the PLL Source is HSI (Internal Clock) */
  638. vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  639. }
  640. else
  641. {
  642. /* In Case the PLL Source is HSE (External Clock) */
  643. vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM)));
  644. }
  645. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  646. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  647. tmpreg1 = (RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> 24U;
  648. frequency = (vcoinput * ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U))/(tmpreg1);
  649. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  650. tmpreg1 = ((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) + 1U);
  651. frequency = frequency/(tmpreg1);
  652. break;
  653. }
  654. case RCC_DCKCFGR_SAI1SRC_1: /* PLLR is the clock source for SAI*/
  655. case RCC_DCKCFGR_SAI2SRC_1: /* PLLR is the clock source for SAI*/
  656. {
  657. /* Configure the PLLI2S division factor */
  658. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  659. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  660. {
  661. /* In Case the PLL Source is HSI (Internal Clock) */
  662. vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  663. }
  664. else
  665. {
  666. /* In Case the PLL Source is HSE (External Clock) */
  667. vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM)));
  668. }
  669. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  670. /* SAI_CLK_x = PLL_VCO Output/PLLR */
  671. tmpreg1 = (RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U;
  672. frequency = (vcoinput * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U))/(tmpreg1);
  673. break;
  674. }
  675. case RCC_DCKCFGR_SAI1SRC: /* External clock is the clock source for SAI*/
  676. {
  677. frequency = EXTERNAL_CLOCK_VALUE;
  678. break;
  679. }
  680. case RCC_DCKCFGR_SAI2SRC: /* PLLSRC(HSE or HSI) is the clock source for SAI*/
  681. {
  682. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  683. {
  684. /* In Case the PLL Source is HSI (Internal Clock) */
  685. frequency = (uint32_t)(HSI_VALUE);
  686. }
  687. else
  688. {
  689. /* In Case the PLL Source is HSE (External Clock) */
  690. frequency = (uint32_t)(HSE_VALUE);
  691. }
  692. break;
  693. }
  694. default :
  695. {
  696. break;
  697. }
  698. }
  699. }
  700. return frequency;
  701. }
  702. #endif /* STM32F446xx */
  703. #if defined(STM32F469xx) || defined(STM32F479xx)
  704. /**
  705. * @brief Initializes the RCC extended peripherals clocks according to the specified
  706. * parameters in the RCC_PeriphCLKInitTypeDef.
  707. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  708. * contains the configuration information for the Extended Peripherals
  709. * clocks(I2S, SAI, LTDC, RTC and TIM).
  710. *
  711. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  712. * the RTC clock source; in this case the Backup domain will be reset in
  713. * order to modify the RTC Clock source, as consequence RTC registers (including
  714. * the backup registers) and RCC_BDCR register are set to their reset values.
  715. *
  716. * @retval HAL status
  717. */
  718. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  719. {
  720. uint32_t tickstart = 0U;
  721. uint32_t tmpreg1 = 0U;
  722. uint32_t pllsaip = 0U;
  723. uint32_t pllsaiq = 0U;
  724. uint32_t pllsair = 0U;
  725. /* Check the parameters */
  726. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  727. /*--------------------------- CLK48 Configuration --------------------------*/
  728. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48)
  729. {
  730. /* Check the parameters */
  731. assert_param(IS_RCC_CLK48CLKSOURCE(PeriphClkInit->Clk48ClockSelection));
  732. /* Configure the CLK48 clock source */
  733. __HAL_RCC_CLK48_CONFIG(PeriphClkInit->Clk48ClockSelection);
  734. }
  735. /*--------------------------------------------------------------------------*/
  736. /*------------------------------ SDIO Configuration ------------------------*/
  737. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDIO) == RCC_PERIPHCLK_SDIO)
  738. {
  739. /* Check the parameters */
  740. assert_param(IS_RCC_SDIOCLKSOURCE(PeriphClkInit->SdioClockSelection));
  741. /* Configure the SDIO clock source */
  742. __HAL_RCC_SDIO_CONFIG(PeriphClkInit->SdioClockSelection);
  743. }
  744. /*--------------------------------------------------------------------------*/
  745. /*----------------------- SAI/I2S Configuration (PLLI2S) -------------------*/
  746. /*------------------- Common configuration SAI/I2S -------------------------*/
  747. /* In Case of SAI or I2S Clock Configuration through PLLI2S, PLLI2SN division
  748. factor is common parameters for both peripherals */
  749. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == RCC_PERIPHCLK_I2S) ||
  750. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLI2S) == RCC_PERIPHCLK_SAI_PLLI2S) ||
  751. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S))
  752. {
  753. /* check for Parameters */
  754. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  755. /* Disable the PLLI2S */
  756. __HAL_RCC_PLLI2S_DISABLE();
  757. /* Get tick */
  758. tickstart = HAL_GetTick();
  759. /* Wait till PLLI2S is disabled */
  760. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  761. {
  762. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  763. {
  764. /* return in case of Timeout detected */
  765. return HAL_TIMEOUT;
  766. }
  767. }
  768. /*---------------------- I2S configuration -------------------------------*/
  769. /* In Case of I2S Clock Configuration through PLLI2S, PLLI2SR must be added
  770. only for I2S configuration */
  771. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == (RCC_PERIPHCLK_I2S))
  772. {
  773. /* check for Parameters */
  774. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  775. /* Configure the PLLI2S division factors */
  776. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) x (PLLI2SN/PLLM) */
  777. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  778. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SR);
  779. }
  780. /*---------------------------- SAI configuration -------------------------*/
  781. /* In Case of SAI Clock Configuration through PLLI2S, PLLI2SQ and PLLI2S_DIVQ must
  782. be added only for SAI configuration */
  783. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLI2S) == (RCC_PERIPHCLK_SAI_PLLI2S))
  784. {
  785. /* Check the PLLI2S division factors */
  786. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  787. assert_param(IS_RCC_PLLI2S_DIVQ_VALUE(PeriphClkInit->PLLI2SDivQ));
  788. /* Read PLLI2SR value from PLLI2SCFGR register (this value is not need for SAI configuration) */
  789. tmpreg1 = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SR));
  790. /* Configure the PLLI2S division factors */
  791. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  792. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  793. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  794. __HAL_RCC_PLLI2S_SAICLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SQ , tmpreg1);
  795. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  796. __HAL_RCC_PLLI2S_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLI2SDivQ);
  797. }
  798. /*----------------- In Case of PLLI2S is just selected -----------------*/
  799. if((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S)
  800. {
  801. /* Check for Parameters */
  802. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  803. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  804. /* Configure the PLLI2S multiplication and division factors */
  805. __HAL_RCC_PLLI2S_SAICLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN, PeriphClkInit->PLLI2S.PLLI2SQ, PeriphClkInit->PLLI2S.PLLI2SR);
  806. }
  807. /* Enable the PLLI2S */
  808. __HAL_RCC_PLLI2S_ENABLE();
  809. /* Get tick */
  810. tickstart = HAL_GetTick();
  811. /* Wait till PLLI2S is ready */
  812. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  813. {
  814. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  815. {
  816. /* return in case of Timeout detected */
  817. return HAL_TIMEOUT;
  818. }
  819. }
  820. }
  821. /*--------------------------------------------------------------------------*/
  822. /*----------------------- SAI/LTDC Configuration (PLLSAI) ------------------*/
  823. /*----------------------- Common configuration SAI/LTDC --------------------*/
  824. /* In Case of SAI, LTDC or CLK48 Clock Configuration through PLLSAI, PLLSAIN division
  825. factor is common parameters for these peripherals */
  826. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLSAI) == RCC_PERIPHCLK_SAI_PLLSAI) ||
  827. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == RCC_PERIPHCLK_LTDC) ||
  828. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48) &&
  829. (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLSAIP)))
  830. {
  831. /* Check the PLLSAI division factors */
  832. assert_param(IS_RCC_PLLSAIN_VALUE(PeriphClkInit->PLLSAI.PLLSAIN));
  833. /* Disable PLLSAI Clock */
  834. __HAL_RCC_PLLSAI_DISABLE();
  835. /* Get tick */
  836. tickstart = HAL_GetTick();
  837. /* Wait till PLLSAI is disabled */
  838. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  839. {
  840. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  841. {
  842. /* return in case of Timeout detected */
  843. return HAL_TIMEOUT;
  844. }
  845. }
  846. /*---------------------------- SAI configuration -------------------------*/
  847. /* In Case of SAI Clock Configuration through PLLSAI, PLLSAIQ and PLLSAI_DIVQ must
  848. be added only for SAI configuration */
  849. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLSAI) == (RCC_PERIPHCLK_SAI_PLLSAI))
  850. {
  851. assert_param(IS_RCC_PLLSAIQ_VALUE(PeriphClkInit->PLLSAI.PLLSAIQ));
  852. assert_param(IS_RCC_PLLSAI_DIVQ_VALUE(PeriphClkInit->PLLSAIDivQ));
  853. /* Read PLLSAIP value from PLLSAICFGR register (this value is not needed for SAI configuration) */
  854. pllsaip = ((((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIP) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIP)) + 1U) << 1U);
  855. /* Read PLLSAIR value from PLLSAICFGR register (this value is not need for SAI configuration) */
  856. pllsair = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIR));
  857. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  858. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  859. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  860. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN, pllsaip, PeriphClkInit->PLLSAI.PLLSAIQ, pllsair);
  861. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  862. __HAL_RCC_PLLSAI_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLSAIDivQ);
  863. }
  864. /*---------------------------- LTDC configuration ------------------------*/
  865. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == (RCC_PERIPHCLK_LTDC))
  866. {
  867. assert_param(IS_RCC_PLLSAIR_VALUE(PeriphClkInit->PLLSAI.PLLSAIR));
  868. assert_param(IS_RCC_PLLSAI_DIVR_VALUE(PeriphClkInit->PLLSAIDivR));
  869. /* Read PLLSAIP value from PLLSAICFGR register (this value is not needed for SAI configuration) */
  870. pllsaip = ((((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIP) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIP)) + 1U) << 1U);
  871. /* Read PLLSAIQ value from PLLSAICFGR register (this value is not need for SAI configuration) */
  872. pllsaiq = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIQ));
  873. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  874. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  875. /* LTDC_CLK(first level) = PLLSAI_VCO Output/PLLSAIR */
  876. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN, pllsaip, pllsaiq, PeriphClkInit->PLLSAI.PLLSAIR);
  877. /* LTDC_CLK = LTDC_CLK(first level)/PLLSAIDIVR */
  878. __HAL_RCC_PLLSAI_PLLSAICLKDIVR_CONFIG(PeriphClkInit->PLLSAIDivR);
  879. }
  880. /*---------------------------- CLK48 configuration ------------------------*/
  881. /* Configure the PLLSAI when it is used as clock source for CLK48 */
  882. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == (RCC_PERIPHCLK_CLK48)) &&
  883. (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLSAIP))
  884. {
  885. assert_param(IS_RCC_PLLSAIP_VALUE(PeriphClkInit->PLLSAI.PLLSAIP));
  886. /* Read PLLSAIQ value from PLLSAICFGR register (this value is not need for SAI configuration) */
  887. pllsaiq = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIQ));
  888. /* Read PLLSAIR value from PLLSAICFGR register (this value is not need for SAI configuration) */
  889. pllsair = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIR));
  890. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  891. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  892. /* CLK48_CLK(first level) = PLLSAI_VCO Output/PLLSAIP */
  893. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN, PeriphClkInit->PLLSAI.PLLSAIP, pllsaiq, pllsair);
  894. }
  895. /* Enable PLLSAI Clock */
  896. __HAL_RCC_PLLSAI_ENABLE();
  897. /* Get tick */
  898. tickstart = HAL_GetTick();
  899. /* Wait till PLLSAI is ready */
  900. while(__HAL_RCC_PLLSAI_GET_FLAG() == RESET)
  901. {
  902. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  903. {
  904. /* return in case of Timeout detected */
  905. return HAL_TIMEOUT;
  906. }
  907. }
  908. }
  909. /*--------------------------------------------------------------------------*/
  910. /*---------------------------- RTC configuration ---------------------------*/
  911. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  912. {
  913. /* Check for RTC Parameters used to output RTCCLK */
  914. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  915. /* Enable Power Clock*/
  916. __HAL_RCC_PWR_CLK_ENABLE();
  917. /* Enable write access to Backup domain */
  918. PWR->CR |= PWR_CR_DBP;
  919. /* Get tick */
  920. tickstart = HAL_GetTick();
  921. while((PWR->CR & PWR_CR_DBP) == RESET)
  922. {
  923. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  924. {
  925. return HAL_TIMEOUT;
  926. }
  927. }
  928. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  929. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  930. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  931. {
  932. /* Store the content of BDCR register before the reset of Backup Domain */
  933. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  934. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  935. __HAL_RCC_BACKUPRESET_FORCE();
  936. __HAL_RCC_BACKUPRESET_RELEASE();
  937. /* Restore the Content of BDCR register */
  938. RCC->BDCR = tmpreg1;
  939. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  940. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  941. {
  942. /* Get tick */
  943. tickstart = HAL_GetTick();
  944. /* Wait till LSE is ready */
  945. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  946. {
  947. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  948. {
  949. return HAL_TIMEOUT;
  950. }
  951. }
  952. }
  953. }
  954. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  955. }
  956. /*--------------------------------------------------------------------------*/
  957. /*---------------------------- TIM configuration ---------------------------*/
  958. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  959. {
  960. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  961. }
  962. return HAL_OK;
  963. }
  964. /**
  965. * @brief Configures the RCC_PeriphCLKInitTypeDef according to the internal
  966. * RCC configuration registers.
  967. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  968. * will be configured.
  969. * @retval None
  970. */
  971. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  972. {
  973. uint32_t tempreg;
  974. /* Set all possible values for the extended clock type parameter------------*/
  975. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S | RCC_PERIPHCLK_SAI_PLLSAI |\
  976. RCC_PERIPHCLK_SAI_PLLI2S | RCC_PERIPHCLK_LTDC |\
  977. RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC |\
  978. RCC_PERIPHCLK_CLK48 | RCC_PERIPHCLK_SDIO;
  979. /* Get the PLLI2S Clock configuration --------------------------------------*/
  980. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SN));
  981. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SR));
  982. PeriphClkInit->PLLI2S.PLLI2SQ = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SQ));
  983. /* Get the PLLSAI Clock configuration --------------------------------------*/
  984. PeriphClkInit->PLLSAI.PLLSAIN = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIN));
  985. PeriphClkInit->PLLSAI.PLLSAIR = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIR));
  986. PeriphClkInit->PLLSAI.PLLSAIQ = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIQ));
  987. /* Get the PLLSAI/PLLI2S division factors ----------------------------------*/
  988. PeriphClkInit->PLLI2SDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) >> POSITION_VAL(RCC_DCKCFGR_PLLI2SDIVQ));
  989. PeriphClkInit->PLLSAIDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> POSITION_VAL(RCC_DCKCFGR_PLLSAIDIVQ));
  990. PeriphClkInit->PLLSAIDivR = (uint32_t)(RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVR);
  991. /* Get the RTC Clock configuration -----------------------------------------*/
  992. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  993. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  994. /* Get the CLK48 clock configuration -------------------------------------*/
  995. PeriphClkInit->Clk48ClockSelection = __HAL_RCC_GET_CLK48_SOURCE();
  996. /* Get the SDIO clock configuration ----------------------------------------*/
  997. PeriphClkInit->SdioClockSelection = __HAL_RCC_GET_SDIO_SOURCE();
  998. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  999. {
  1000. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  1001. }
  1002. else
  1003. {
  1004. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  1005. }
  1006. }
  1007. #endif /* STM32F469xx || STM32F479xx */
  1008. #if defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx)
  1009. /**
  1010. * @brief Initializes the RCC extended peripherals clocks according to the specified
  1011. * parameters in the RCC_PeriphCLKInitTypeDef.
  1012. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  1013. * contains the configuration information for the Extended Peripherals
  1014. * clocks(I2S, LTDC RTC and TIM).
  1015. *
  1016. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  1017. * the RTC clock source; in this case the Backup domain will be reset in
  1018. * order to modify the RTC Clock source, as consequence RTC registers (including
  1019. * the backup registers) and RCC_BDCR register are set to their reset values.
  1020. *
  1021. * @retval HAL status
  1022. */
  1023. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1024. {
  1025. uint32_t tickstart = 0U;
  1026. uint32_t tmpreg1 = 0U;
  1027. uint32_t plli2sq = 0U;
  1028. uint32_t plli2sused = 0U;
  1029. /* Check the peripheral clock selection parameters */
  1030. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  1031. /*----------------------------------- I2S APB1 configuration ---------------*/
  1032. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB1) == (RCC_PERIPHCLK_I2S_APB1))
  1033. {
  1034. /* Check the parameters */
  1035. assert_param(IS_RCC_I2SAPB1CLKSOURCE(PeriphClkInit->I2sApb1ClockSelection));
  1036. /* Configure I2S Clock source */
  1037. __HAL_RCC_I2S_APB1_CONFIG(PeriphClkInit->I2sApb1ClockSelection);
  1038. /* Enable the PLLI2S when it's used as clock source for I2S */
  1039. if(PeriphClkInit->I2sApb1ClockSelection == RCC_I2SAPB1CLKSOURCE_PLLI2S)
  1040. {
  1041. plli2sused = 1U;
  1042. }
  1043. }
  1044. /*--------------------------------------------------------------------------*/
  1045. /*----------------------------------- I2S APB2 configuration ---------------*/
  1046. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB2) == (RCC_PERIPHCLK_I2S_APB2))
  1047. {
  1048. /* Check the parameters */
  1049. assert_param(IS_RCC_I2SAPB2CLKSOURCE(PeriphClkInit->I2sApb2ClockSelection));
  1050. /* Configure I2S Clock source */
  1051. __HAL_RCC_I2S_APB2_CONFIG(PeriphClkInit->I2sApb2ClockSelection);
  1052. /* Enable the PLLI2S when it's used as clock source for I2S */
  1053. if(PeriphClkInit->I2sApb2ClockSelection == RCC_I2SAPB2CLKSOURCE_PLLI2S)
  1054. {
  1055. plli2sused = 1U;
  1056. }
  1057. }
  1058. /*--------------------------------------------------------------------------*/
  1059. /*------------------------------------ RTC configuration -------------------*/
  1060. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  1061. {
  1062. /* Check for RTC Parameters used to output RTCCLK */
  1063. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  1064. /* Enable Power Clock*/
  1065. __HAL_RCC_PWR_CLK_ENABLE();
  1066. /* Enable write access to Backup domain */
  1067. PWR->CR |= PWR_CR_DBP;
  1068. /* Get tick */
  1069. tickstart = HAL_GetTick();
  1070. while((PWR->CR & PWR_CR_DBP) == RESET)
  1071. {
  1072. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  1073. {
  1074. return HAL_TIMEOUT;
  1075. }
  1076. }
  1077. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  1078. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  1079. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  1080. {
  1081. /* Store the content of BDCR register before the reset of Backup Domain */
  1082. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  1083. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  1084. __HAL_RCC_BACKUPRESET_FORCE();
  1085. __HAL_RCC_BACKUPRESET_RELEASE();
  1086. /* Restore the Content of BDCR register */
  1087. RCC->BDCR = tmpreg1;
  1088. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  1089. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  1090. {
  1091. /* Get tick */
  1092. tickstart = HAL_GetTick();
  1093. /* Wait till LSE is ready */
  1094. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  1095. {
  1096. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  1097. {
  1098. return HAL_TIMEOUT;
  1099. }
  1100. }
  1101. }
  1102. }
  1103. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  1104. }
  1105. /*--------------------------------------------------------------------------*/
  1106. /*------------------------------------ TIM configuration -------------------*/
  1107. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  1108. {
  1109. /* Configure Timer Prescaler */
  1110. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  1111. }
  1112. /*--------------------------------------------------------------------------*/
  1113. /*------------------------------------- FMPI2C1 Configuration --------------*/
  1114. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_FMPI2C1) == RCC_PERIPHCLK_FMPI2C1)
  1115. {
  1116. /* Check the parameters */
  1117. assert_param(IS_RCC_FMPI2C1CLKSOURCE(PeriphClkInit->Fmpi2c1ClockSelection));
  1118. /* Configure the FMPI2C1 clock source */
  1119. __HAL_RCC_FMPI2C1_CONFIG(PeriphClkInit->Fmpi2c1ClockSelection);
  1120. }
  1121. /*--------------------------------------------------------------------------*/
  1122. /*------------------------------------- CLK48 Configuration ----------------*/
  1123. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48)
  1124. {
  1125. /* Check the parameters */
  1126. assert_param(IS_RCC_CLK48CLKSOURCE(PeriphClkInit->Clk48ClockSelection));
  1127. /* Configure the SDIO clock source */
  1128. __HAL_RCC_CLK48_CONFIG(PeriphClkInit->Clk48ClockSelection);
  1129. /* Enable the PLLI2S when it's used as clock source for CLK48 */
  1130. if(PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLI2SQ)
  1131. {
  1132. plli2sused = 1U;
  1133. }
  1134. }
  1135. /*--------------------------------------------------------------------------*/
  1136. /*------------------------------------- SDIO Configuration -----------------*/
  1137. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDIO) == RCC_PERIPHCLK_SDIO)
  1138. {
  1139. /* Check the parameters */
  1140. assert_param(IS_RCC_SDIOCLKSOURCE(PeriphClkInit->SdioClockSelection));
  1141. /* Configure the SDIO clock source */
  1142. __HAL_RCC_SDIO_CONFIG(PeriphClkInit->SdioClockSelection);
  1143. }
  1144. /*--------------------------------------------------------------------------*/
  1145. /*-------------------------------------- PLLI2S Configuration --------------*/
  1146. /* PLLI2S is configured when a peripheral will use it as source clock : I2S on APB1 or
  1147. I2S on APB2*/
  1148. if((plli2sused == 1U) || (PeriphClkInit->PeriphClockSelection == RCC_PERIPHCLK_PLLI2S))
  1149. {
  1150. /* Disable the PLLI2S */
  1151. __HAL_RCC_PLLI2S_DISABLE();
  1152. /* Get tick */
  1153. tickstart = HAL_GetTick();
  1154. /* Wait till PLLI2S is disabled */
  1155. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  1156. {
  1157. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  1158. {
  1159. /* return in case of Timeout detected */
  1160. return HAL_TIMEOUT;
  1161. }
  1162. }
  1163. /* check for common PLLI2S Parameters */
  1164. assert_param(IS_RCC_PLLI2SCLKSOURCE(PeriphClkInit->PLLI2SSelection));
  1165. assert_param(IS_RCC_PLLI2SM_VALUE(PeriphClkInit->PLLI2S.PLLI2SM));
  1166. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  1167. /*-------------------- Set the PLL I2S clock -----------------------------*/
  1168. __HAL_RCC_PLL_I2S_CONFIG(PeriphClkInit->PLLI2SSelection);
  1169. /*------- In Case of PLLI2S is selected as source clock for I2S ----------*/
  1170. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB1) == RCC_PERIPHCLK_I2S_APB1) && (PeriphClkInit->I2sApb1ClockSelection == RCC_I2SAPB1CLKSOURCE_PLLI2S)) ||
  1171. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB2) == RCC_PERIPHCLK_I2S_APB2) && (PeriphClkInit->I2sApb2ClockSelection == RCC_I2SAPB2CLKSOURCE_PLLI2S)) ||
  1172. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48) && (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLI2SQ)) ||
  1173. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDIO) == RCC_PERIPHCLK_SDIO) && (PeriphClkInit->SdioClockSelection == RCC_SDIOCLKSOURCE_CLK48) && (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLI2SQ)))
  1174. {
  1175. /* check for Parameters */
  1176. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  1177. /* Read PLLI2SQ value from PLLI2SCFGR register (this value is not needed for I2S configuration) */
  1178. plli2sq = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SQ));
  1179. /* Configure the PLLI2S division factors */
  1180. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM)*/
  1181. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  1182. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , plli2sq, PeriphClkInit->PLLI2S.PLLI2SR);
  1183. }
  1184. /*----------------- In Case of PLLI2S is just selected ------------------*/
  1185. if((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S)
  1186. {
  1187. /* Check for Parameters */
  1188. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  1189. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  1190. /* Configure the PLLI2S division factors */
  1191. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM)*/
  1192. /* SPDIFRXCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SP */
  1193. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SQ, PeriphClkInit->PLLI2S.PLLI2SR);
  1194. }
  1195. /* Enable the PLLI2S */
  1196. __HAL_RCC_PLLI2S_ENABLE();
  1197. /* Get tick */
  1198. tickstart = HAL_GetTick();
  1199. /* Wait till PLLI2S is ready */
  1200. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  1201. {
  1202. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  1203. {
  1204. /* return in case of Timeout detected */
  1205. return HAL_TIMEOUT;
  1206. }
  1207. }
  1208. }
  1209. /*--------------------------------------------------------------------------*/
  1210. /*-------------------- DFSDM1 clock source configuration -------------------*/
  1211. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM1) == RCC_PERIPHCLK_DFSDM1)
  1212. {
  1213. /* Check the parameters */
  1214. assert_param(IS_RCC_DFSDM1CLKSOURCE(PeriphClkInit->Dfsdm1ClockSelection));
  1215. /* Configure the DFSDM1 interface clock source */
  1216. __HAL_RCC_DFSDM1_CONFIG(PeriphClkInit->Dfsdm1ClockSelection);
  1217. }
  1218. /*--------------------------------------------------------------------------*/
  1219. /*-------------------- DFSDM1 Audio clock source configuration -------------*/
  1220. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM1_AUDIO) == RCC_PERIPHCLK_DFSDM1_AUDIO)
  1221. {
  1222. /* Check the parameters */
  1223. assert_param(IS_RCC_DFSDM1AUDIOCLKSOURCE(PeriphClkInit->Dfsdm1AudioClockSelection));
  1224. /* Configure the DFSDM1 Audio interface clock source */
  1225. __HAL_RCC_DFSDM1AUDIO_CONFIG(PeriphClkInit->Dfsdm1AudioClockSelection);
  1226. }
  1227. return HAL_OK;
  1228. }
  1229. /**
  1230. * @brief Get the RCC_PeriphCLKInitTypeDef according to the internal
  1231. * RCC configuration registers.
  1232. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  1233. * will be configured.
  1234. * @retval None
  1235. */
  1236. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1237. {
  1238. uint32_t tempreg;
  1239. /* Set all possible values for the extended clock type parameter------------*/
  1240. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_I2S_APB2 |\
  1241. RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC |\
  1242. RCC_PERIPHCLK_FMPI2C1 | RCC_PERIPHCLK_CLK48 |\
  1243. RCC_PERIPHCLK_SDIO;
  1244. /* Get the PLLI2S Clock configuration --------------------------------------*/
  1245. PeriphClkInit->PLLI2S.PLLI2SM = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SM));
  1246. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SN));
  1247. PeriphClkInit->PLLI2S.PLLI2SQ = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SQ));
  1248. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SR));
  1249. /* Get the I2S APB1 clock configuration ------------------------------------*/
  1250. PeriphClkInit->I2sApb1ClockSelection = __HAL_RCC_GET_I2S_APB1_SOURCE();
  1251. /* Get the I2S APB2 clock configuration ------------------------------------*/
  1252. PeriphClkInit->I2sApb2ClockSelection = __HAL_RCC_GET_I2S_APB2_SOURCE();
  1253. /* Get the RTC Clock configuration -----------------------------------------*/
  1254. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  1255. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  1256. /* Get the FMPI2C1 clock configuration -------------------------------------*/
  1257. PeriphClkInit->Fmpi2c1ClockSelection = __HAL_RCC_GET_FMPI2C1_SOURCE();
  1258. /* Get the CLK48 clock configuration ----------------------------------------*/
  1259. PeriphClkInit->Clk48ClockSelection = __HAL_RCC_GET_CLK48_SOURCE();
  1260. /* Get the SDIO clock configuration ----------------------------------------*/
  1261. PeriphClkInit->SdioClockSelection = __HAL_RCC_GET_SDIO_SOURCE();
  1262. /* Get the TIM Prescaler configuration -------------------------------------*/
  1263. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  1264. {
  1265. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  1266. }
  1267. else
  1268. {
  1269. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  1270. }
  1271. }
  1272. #endif /* STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx */
  1273. #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx)
  1274. /**
  1275. * @brief Initializes the RCC extended peripherals clocks according to the specified parameters in the
  1276. * RCC_PeriphCLKInitTypeDef.
  1277. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  1278. * contains the configuration information for the Extended Peripherals clocks(I2S and RTC clocks).
  1279. *
  1280. * @note A caution to be taken when HAL_RCCEx_PeriphCLKConfig() is used to select RTC clock selection, in this case
  1281. * the Reset of Backup domain will be applied in order to modify the RTC Clock source as consequence all backup
  1282. * domain (RTC and RCC_BDCR register expect BKPSRAM) will be reset
  1283. *
  1284. * @retval HAL status
  1285. */
  1286. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1287. {
  1288. uint32_t tickstart = 0U;
  1289. uint32_t tmpreg1 = 0U;
  1290. /* Check the parameters */
  1291. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  1292. /*---------------------------- RTC configuration ---------------------------*/
  1293. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  1294. {
  1295. /* Check for RTC Parameters used to output RTCCLK */
  1296. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  1297. /* Enable Power Clock*/
  1298. __HAL_RCC_PWR_CLK_ENABLE();
  1299. /* Enable write access to Backup domain */
  1300. PWR->CR |= PWR_CR_DBP;
  1301. /* Get tick */
  1302. tickstart = HAL_GetTick();
  1303. while((PWR->CR & PWR_CR_DBP) == RESET)
  1304. {
  1305. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  1306. {
  1307. return HAL_TIMEOUT;
  1308. }
  1309. }
  1310. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  1311. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  1312. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  1313. {
  1314. /* Store the content of BDCR register before the reset of Backup Domain */
  1315. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  1316. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  1317. __HAL_RCC_BACKUPRESET_FORCE();
  1318. __HAL_RCC_BACKUPRESET_RELEASE();
  1319. /* Restore the Content of BDCR register */
  1320. RCC->BDCR = tmpreg1;
  1321. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  1322. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  1323. {
  1324. /* Get tick */
  1325. tickstart = HAL_GetTick();
  1326. /* Wait till LSE is ready */
  1327. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  1328. {
  1329. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  1330. {
  1331. return HAL_TIMEOUT;
  1332. }
  1333. }
  1334. }
  1335. }
  1336. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  1337. }
  1338. /*--------------------------------------------------------------------------*/
  1339. /*---------------------------- TIM configuration ---------------------------*/
  1340. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  1341. {
  1342. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  1343. }
  1344. /*--------------------------------------------------------------------------*/
  1345. /*---------------------------- FMPI2C1 Configuration -----------------------*/
  1346. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_FMPI2C1) == RCC_PERIPHCLK_FMPI2C1)
  1347. {
  1348. /* Check the parameters */
  1349. assert_param(IS_RCC_FMPI2C1CLKSOURCE(PeriphClkInit->Fmpi2c1ClockSelection));
  1350. /* Configure the FMPI2C1 clock source */
  1351. __HAL_RCC_FMPI2C1_CONFIG(PeriphClkInit->Fmpi2c1ClockSelection);
  1352. }
  1353. /*--------------------------------------------------------------------------*/
  1354. /*---------------------------- LPTIM1 Configuration ------------------------*/
  1355. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM1) == RCC_PERIPHCLK_LPTIM1)
  1356. {
  1357. /* Check the parameters */
  1358. assert_param(IS_RCC_LPTIM1CLKSOURCE(PeriphClkInit->Lptim1ClockSelection));
  1359. /* Configure the LPTIM1 clock source */
  1360. __HAL_RCC_LPTIM1_CONFIG(PeriphClkInit->Lptim1ClockSelection);
  1361. }
  1362. /*---------------------------- I2S Configuration ------------------------*/
  1363. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == RCC_PERIPHCLK_I2S)
  1364. {
  1365. /* Check the parameters */
  1366. assert_param(IS_RCC_I2SAPBCLKSOURCE(PeriphClkInit->I2SClockSelection));
  1367. /* Configure the I2S clock source */
  1368. __HAL_RCC_I2S_CONFIG(PeriphClkInit->I2SClockSelection);
  1369. }
  1370. return HAL_OK;
  1371. }
  1372. /**
  1373. * @brief Configures the RCC_OscInitStruct according to the internal
  1374. * RCC configuration registers.
  1375. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  1376. * will be configured.
  1377. * @retval None
  1378. */
  1379. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1380. {
  1381. uint32_t tempreg;
  1382. /* Set all possible values for the extended clock type parameter------------*/
  1383. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_FMPI2C1 | RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC;
  1384. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  1385. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  1386. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  1387. {
  1388. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  1389. }
  1390. else
  1391. {
  1392. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  1393. }
  1394. /* Get the FMPI2C1 clock configuration -------------------------------------*/
  1395. PeriphClkInit->Fmpi2c1ClockSelection = __HAL_RCC_GET_FMPI2C1_SOURCE();
  1396. /* Get the I2S clock configuration -----------------------------------------*/
  1397. PeriphClkInit->I2SClockSelection = __HAL_RCC_GET_I2S_SOURCE();
  1398. }
  1399. #endif /* STM32F410Tx || STM32F410Cx || STM32F410Rx */
  1400. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  1401. /**
  1402. * @brief Initializes the RCC extended peripherals clocks according to the specified
  1403. * parameters in the RCC_PeriphCLKInitTypeDef.
  1404. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  1405. * contains the configuration information for the Extended Peripherals
  1406. * clocks(I2S, SAI, LTDC RTC and TIM).
  1407. *
  1408. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  1409. * the RTC clock source; in this case the Backup domain will be reset in
  1410. * order to modify the RTC Clock source, as consequence RTC registers (including
  1411. * the backup registers) and RCC_BDCR register are set to their reset values.
  1412. *
  1413. * @retval HAL status
  1414. */
  1415. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1416. {
  1417. uint32_t tickstart = 0U;
  1418. uint32_t tmpreg1 = 0U;
  1419. /* Check the parameters */
  1420. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  1421. /*----------------------- SAI/I2S Configuration (PLLI2S) -------------------*/
  1422. /*----------------------- Common configuration SAI/I2S ----------------------*/
  1423. /* In Case of SAI or I2S Clock Configuration through PLLI2S, PLLI2SN division
  1424. factor is common parameters for both peripherals */
  1425. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == RCC_PERIPHCLK_I2S) ||
  1426. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLI2S) == RCC_PERIPHCLK_SAI_PLLI2S))
  1427. {
  1428. /* check for Parameters */
  1429. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  1430. /* Disable the PLLI2S */
  1431. __HAL_RCC_PLLI2S_DISABLE();
  1432. /* Get tick */
  1433. tickstart = HAL_GetTick();
  1434. /* Wait till PLLI2S is disabled */
  1435. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  1436. {
  1437. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  1438. {
  1439. /* return in case of Timeout detected */
  1440. return HAL_TIMEOUT;
  1441. }
  1442. }
  1443. /*---------------------------- I2S configuration -------------------------------*/
  1444. /* In Case of I2S Clock Configuration through PLLI2S, PLLI2SR must be added
  1445. only for I2S configuration */
  1446. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == (RCC_PERIPHCLK_I2S))
  1447. {
  1448. /* check for Parameters */
  1449. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  1450. /* Configure the PLLI2S division factors */
  1451. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLM) */
  1452. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  1453. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SR);
  1454. }
  1455. /*---------------------------- SAI configuration -------------------------------*/
  1456. /* In Case of SAI Clock Configuration through PLLI2S, PLLI2SQ and PLLI2S_DIVQ must
  1457. be added only for SAI configuration */
  1458. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLI2S) == (RCC_PERIPHCLK_SAI_PLLI2S))
  1459. {
  1460. /* Check the PLLI2S division factors */
  1461. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  1462. assert_param(IS_RCC_PLLI2S_DIVQ_VALUE(PeriphClkInit->PLLI2SDivQ));
  1463. /* Read PLLI2SR value from PLLI2SCFGR register (this value is not need for SAI configuration) */
  1464. tmpreg1 = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SR));
  1465. /* Configure the PLLI2S division factors */
  1466. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  1467. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  1468. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  1469. __HAL_RCC_PLLI2S_SAICLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SQ , tmpreg1);
  1470. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  1471. __HAL_RCC_PLLI2S_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLI2SDivQ);
  1472. }
  1473. /* Enable the PLLI2S */
  1474. __HAL_RCC_PLLI2S_ENABLE();
  1475. /* Get tick */
  1476. tickstart = HAL_GetTick();
  1477. /* Wait till PLLI2S is ready */
  1478. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  1479. {
  1480. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  1481. {
  1482. /* return in case of Timeout detected */
  1483. return HAL_TIMEOUT;
  1484. }
  1485. }
  1486. }
  1487. /*--------------------------------------------------------------------------*/
  1488. /*----------------------- SAI/LTDC Configuration (PLLSAI) ------------------*/
  1489. /*----------------------- Common configuration SAI/LTDC --------------------*/
  1490. /* In Case of SAI or LTDC Clock Configuration through PLLSAI, PLLSAIN division
  1491. factor is common parameters for both peripherals */
  1492. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLSAI) == RCC_PERIPHCLK_SAI_PLLSAI) ||
  1493. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == RCC_PERIPHCLK_LTDC))
  1494. {
  1495. /* Check the PLLSAI division factors */
  1496. assert_param(IS_RCC_PLLSAIN_VALUE(PeriphClkInit->PLLSAI.PLLSAIN));
  1497. /* Disable PLLSAI Clock */
  1498. __HAL_RCC_PLLSAI_DISABLE();
  1499. /* Get tick */
  1500. tickstart = HAL_GetTick();
  1501. /* Wait till PLLSAI is disabled */
  1502. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  1503. {
  1504. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  1505. {
  1506. /* return in case of Timeout detected */
  1507. return HAL_TIMEOUT;
  1508. }
  1509. }
  1510. /*---------------------------- SAI configuration -------------------------*/
  1511. /* In Case of SAI Clock Configuration through PLLSAI, PLLSAIQ and PLLSAI_DIVQ must
  1512. be added only for SAI configuration */
  1513. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLSAI) == (RCC_PERIPHCLK_SAI_PLLSAI))
  1514. {
  1515. assert_param(IS_RCC_PLLSAIQ_VALUE(PeriphClkInit->PLLSAI.PLLSAIQ));
  1516. assert_param(IS_RCC_PLLSAI_DIVQ_VALUE(PeriphClkInit->PLLSAIDivQ));
  1517. /* Read PLLSAIR value from PLLSAICFGR register (this value is not need for SAI configuration) */
  1518. tmpreg1 = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIR));
  1519. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  1520. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  1521. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  1522. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN , PeriphClkInit->PLLSAI.PLLSAIQ, tmpreg1);
  1523. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  1524. __HAL_RCC_PLLSAI_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLSAIDivQ);
  1525. }
  1526. /*---------------------------- LTDC configuration ------------------------*/
  1527. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == (RCC_PERIPHCLK_LTDC))
  1528. {
  1529. assert_param(IS_RCC_PLLSAIR_VALUE(PeriphClkInit->PLLSAI.PLLSAIR));
  1530. assert_param(IS_RCC_PLLSAI_DIVR_VALUE(PeriphClkInit->PLLSAIDivR));
  1531. /* Read PLLSAIR value from PLLSAICFGR register (this value is not need for SAI configuration) */
  1532. tmpreg1 = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIQ));
  1533. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  1534. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  1535. /* LTDC_CLK(first level) = PLLSAI_VCO Output/PLLSAIR */
  1536. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN , tmpreg1, PeriphClkInit->PLLSAI.PLLSAIR);
  1537. /* LTDC_CLK = LTDC_CLK(first level)/PLLSAIDIVR */
  1538. __HAL_RCC_PLLSAI_PLLSAICLKDIVR_CONFIG(PeriphClkInit->PLLSAIDivR);
  1539. }
  1540. /* Enable PLLSAI Clock */
  1541. __HAL_RCC_PLLSAI_ENABLE();
  1542. /* Get tick */
  1543. tickstart = HAL_GetTick();
  1544. /* Wait till PLLSAI is ready */
  1545. while(__HAL_RCC_PLLSAI_GET_FLAG() == RESET)
  1546. {
  1547. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  1548. {
  1549. /* return in case of Timeout detected */
  1550. return HAL_TIMEOUT;
  1551. }
  1552. }
  1553. }
  1554. /*--------------------------------------------------------------------------*/
  1555. /*---------------------------- RTC configuration ---------------------------*/
  1556. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  1557. {
  1558. /* Check for RTC Parameters used to output RTCCLK */
  1559. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  1560. /* Enable Power Clock*/
  1561. __HAL_RCC_PWR_CLK_ENABLE();
  1562. /* Enable write access to Backup domain */
  1563. PWR->CR |= PWR_CR_DBP;
  1564. /* Get tick */
  1565. tickstart = HAL_GetTick();
  1566. while((PWR->CR & PWR_CR_DBP) == RESET)
  1567. {
  1568. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  1569. {
  1570. return HAL_TIMEOUT;
  1571. }
  1572. }
  1573. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  1574. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  1575. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  1576. {
  1577. /* Store the content of BDCR register before the reset of Backup Domain */
  1578. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  1579. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  1580. __HAL_RCC_BACKUPRESET_FORCE();
  1581. __HAL_RCC_BACKUPRESET_RELEASE();
  1582. /* Restore the Content of BDCR register */
  1583. RCC->BDCR = tmpreg1;
  1584. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  1585. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  1586. {
  1587. /* Get tick */
  1588. tickstart = HAL_GetTick();
  1589. /* Wait till LSE is ready */
  1590. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  1591. {
  1592. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  1593. {
  1594. return HAL_TIMEOUT;
  1595. }
  1596. }
  1597. }
  1598. }
  1599. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  1600. }
  1601. /*--------------------------------------------------------------------------*/
  1602. /*---------------------------- TIM configuration ---------------------------*/
  1603. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  1604. {
  1605. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  1606. }
  1607. return HAL_OK;
  1608. }
  1609. /**
  1610. * @brief Configures the PeriphClkInit according to the internal
  1611. * RCC configuration registers.
  1612. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  1613. * will be configured.
  1614. * @retval None
  1615. */
  1616. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1617. {
  1618. uint32_t tempreg;
  1619. /* Set all possible values for the extended clock type parameter------------*/
  1620. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S | RCC_PERIPHCLK_SAI_PLLSAI | RCC_PERIPHCLK_SAI_PLLI2S | RCC_PERIPHCLK_LTDC | RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC;
  1621. /* Get the PLLI2S Clock configuration -----------------------------------------------*/
  1622. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SN));
  1623. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SR));
  1624. PeriphClkInit->PLLI2S.PLLI2SQ = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SQ));
  1625. /* Get the PLLSAI Clock configuration -----------------------------------------------*/
  1626. PeriphClkInit->PLLSAI.PLLSAIN = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIN));
  1627. PeriphClkInit->PLLSAI.PLLSAIR = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIR));
  1628. PeriphClkInit->PLLSAI.PLLSAIQ = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> POSITION_VAL(RCC_PLLSAICFGR_PLLSAIQ));
  1629. /* Get the PLLSAI/PLLI2S division factors -----------------------------------------------*/
  1630. PeriphClkInit->PLLI2SDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) >> POSITION_VAL(RCC_DCKCFGR_PLLI2SDIVQ));
  1631. PeriphClkInit->PLLSAIDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> POSITION_VAL(RCC_DCKCFGR_PLLSAIDIVQ));
  1632. PeriphClkInit->PLLSAIDivR = (uint32_t)(RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVR);
  1633. /* Get the RTC Clock configuration -----------------------------------------------*/
  1634. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  1635. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  1636. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  1637. {
  1638. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  1639. }
  1640. else
  1641. {
  1642. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  1643. }
  1644. }
  1645. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  1646. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx) ||\
  1647. defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE)
  1648. /**
  1649. * @brief Initializes the RCC extended peripherals clocks according to the specified parameters in the
  1650. * RCC_PeriphCLKInitTypeDef.
  1651. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  1652. * contains the configuration information for the Extended Peripherals clocks(I2S and RTC clocks).
  1653. *
  1654. * @note A caution to be taken when HAL_RCCEx_PeriphCLKConfig() is used to select RTC clock selection, in this case
  1655. * the Reset of Backup domain will be applied in order to modify the RTC Clock source as consequence all backup
  1656. * domain (RTC and RCC_BDCR register expect BKPSRAM) will be reset
  1657. *
  1658. * @retval HAL status
  1659. */
  1660. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1661. {
  1662. uint32_t tickstart = 0U;
  1663. uint32_t tmpreg1 = 0U;
  1664. /* Check the parameters */
  1665. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  1666. /*---------------------------- I2S configuration ---------------------------*/
  1667. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == (RCC_PERIPHCLK_I2S))
  1668. {
  1669. /* check for Parameters */
  1670. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  1671. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  1672. #if defined(STM32F411xE)
  1673. assert_param(IS_RCC_PLLI2SM_VALUE(PeriphClkInit->PLLI2S.PLLI2SM));
  1674. #endif /* STM32F411xE */
  1675. /* Disable the PLLI2S */
  1676. __HAL_RCC_PLLI2S_DISABLE();
  1677. /* Get tick */
  1678. tickstart = HAL_GetTick();
  1679. /* Wait till PLLI2S is disabled */
  1680. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  1681. {
  1682. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  1683. {
  1684. /* return in case of Timeout detected */
  1685. return HAL_TIMEOUT;
  1686. }
  1687. }
  1688. #if defined(STM32F411xE)
  1689. /* Configure the PLLI2S division factors */
  1690. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  1691. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  1692. __HAL_RCC_PLLI2S_I2SCLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN, PeriphClkInit->PLLI2S.PLLI2SR);
  1693. #else
  1694. /* Configure the PLLI2S division factors */
  1695. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLM) */
  1696. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  1697. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SR);
  1698. #endif /* STM32F411xE */
  1699. /* Enable the PLLI2S */
  1700. __HAL_RCC_PLLI2S_ENABLE();
  1701. /* Get tick */
  1702. tickstart = HAL_GetTick();
  1703. /* Wait till PLLI2S is ready */
  1704. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  1705. {
  1706. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  1707. {
  1708. /* return in case of Timeout detected */
  1709. return HAL_TIMEOUT;
  1710. }
  1711. }
  1712. }
  1713. /*---------------------------- RTC configuration ---------------------------*/
  1714. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  1715. {
  1716. /* Check for RTC Parameters used to output RTCCLK */
  1717. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  1718. /* Enable Power Clock*/
  1719. __HAL_RCC_PWR_CLK_ENABLE();
  1720. /* Enable write access to Backup domain */
  1721. PWR->CR |= PWR_CR_DBP;
  1722. /* Get tick */
  1723. tickstart = HAL_GetTick();
  1724. while((PWR->CR & PWR_CR_DBP) == RESET)
  1725. {
  1726. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  1727. {
  1728. return HAL_TIMEOUT;
  1729. }
  1730. }
  1731. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  1732. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  1733. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  1734. {
  1735. /* Store the content of BDCR register before the reset of Backup Domain */
  1736. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  1737. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  1738. __HAL_RCC_BACKUPRESET_FORCE();
  1739. __HAL_RCC_BACKUPRESET_RELEASE();
  1740. /* Restore the Content of BDCR register */
  1741. RCC->BDCR = tmpreg1;
  1742. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  1743. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  1744. {
  1745. /* Get tick */
  1746. tickstart = HAL_GetTick();
  1747. /* Wait till LSE is ready */
  1748. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  1749. {
  1750. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  1751. {
  1752. return HAL_TIMEOUT;
  1753. }
  1754. }
  1755. }
  1756. }
  1757. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  1758. }
  1759. #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE)
  1760. /*---------------------------- TIM configuration ---------------------------*/
  1761. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  1762. {
  1763. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  1764. }
  1765. #endif /* STM32F401xC || STM32F401xE || STM32F411xE */
  1766. return HAL_OK;
  1767. }
  1768. /**
  1769. * @brief Configures the RCC_OscInitStruct according to the internal
  1770. * RCC configuration registers.
  1771. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  1772. * will be configured.
  1773. * @retval None
  1774. */
  1775. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1776. {
  1777. uint32_t tempreg;
  1778. /* Set all possible values for the extended clock type parameter------------*/
  1779. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S | RCC_PERIPHCLK_RTC;
  1780. /* Get the PLLI2S Clock configuration --------------------------------------*/
  1781. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SN));
  1782. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SR));
  1783. #if defined(STM32F411xE)
  1784. PeriphClkInit->PLLI2S.PLLI2SM = (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM);
  1785. #endif /* STM32F411xE */
  1786. /* Get the RTC Clock configuration -----------------------------------------*/
  1787. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  1788. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  1789. #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE)
  1790. /* Get the TIM Prescaler configuration -------------------------------------*/
  1791. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  1792. {
  1793. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  1794. }
  1795. else
  1796. {
  1797. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  1798. }
  1799. #endif /* STM32F401xC || STM32F401xE || STM32F411xE */
  1800. }
  1801. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F401xC || STM32F401xE || STM32F411xE */
  1802. #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) ||\
  1803. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx)
  1804. /**
  1805. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  1806. * RCC_OscInitTypeDef.
  1807. * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
  1808. * contains the configuration information for the RCC Oscillators.
  1809. * @note The PLL is not disabled when used as system clock.
  1810. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  1811. * supported by this API. User should request a transition to LSE Off
  1812. * first and then LSE On or LSE Bypass.
  1813. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  1814. * supported by this API. User should request a transition to HSE Off
  1815. * first and then HSE On or HSE Bypass.
  1816. * @note This function add the PLL/PLLR factor management during PLL configuration this feature
  1817. * is only available in STM32F410xx/STM32F446xx/STM32F469xx/STM32F479xx/STM32F412Zx/STM32F412Vx/STM32F412Rx/STM32F412Cx devices
  1818. * @retval HAL status
  1819. */
  1820. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1821. {
  1822. uint32_t tickstart = 0U;
  1823. /* Check the parameters */
  1824. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  1825. /*------------------------------- HSE Configuration ------------------------*/
  1826. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  1827. {
  1828. /* Check the parameters */
  1829. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  1830. /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
  1831. #if defined(STM32F446xx)
  1832. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||\
  1833. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)) ||\
  1834. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLLR) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))
  1835. #else
  1836. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||\
  1837. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))
  1838. #endif /* STM32F446xx */
  1839. {
  1840. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  1841. {
  1842. return HAL_ERROR;
  1843. }
  1844. }
  1845. else
  1846. {
  1847. /* Set the new HSE configuration ---------------------------------------*/
  1848. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  1849. /* Check the HSE State */
  1850. if((RCC_OscInitStruct->HSEState) != RCC_HSE_OFF)
  1851. {
  1852. /* Get Start Tick*/
  1853. tickstart = HAL_GetTick();
  1854. /* Wait till HSE is ready */
  1855. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  1856. {
  1857. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  1858. {
  1859. return HAL_TIMEOUT;
  1860. }
  1861. }
  1862. }
  1863. else
  1864. {
  1865. /* Get Start Tick*/
  1866. tickstart = HAL_GetTick();
  1867. /* Wait till HSE is bypassed or disabled */
  1868. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  1869. {
  1870. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  1871. {
  1872. return HAL_TIMEOUT;
  1873. }
  1874. }
  1875. }
  1876. }
  1877. }
  1878. /*----------------------------- HSI Configuration --------------------------*/
  1879. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  1880. {
  1881. /* Check the parameters */
  1882. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  1883. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  1884. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  1885. #if defined(STM32F446xx)
  1886. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||\
  1887. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)) ||\
  1888. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLLR) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))
  1889. #else
  1890. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||\
  1891. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))
  1892. #endif /* STM32F446xx */
  1893. {
  1894. /* When HSI is used as system clock it will not disabled */
  1895. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  1896. {
  1897. return HAL_ERROR;
  1898. }
  1899. /* Otherwise, just the calibration is allowed */
  1900. else
  1901. {
  1902. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  1903. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  1904. }
  1905. }
  1906. else
  1907. {
  1908. /* Check the HSI State */
  1909. if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
  1910. {
  1911. /* Enable the Internal High Speed oscillator (HSI). */
  1912. __HAL_RCC_HSI_ENABLE();
  1913. /* Get Start Tick*/
  1914. tickstart = HAL_GetTick();
  1915. /* Wait till HSI is ready */
  1916. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  1917. {
  1918. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  1919. {
  1920. return HAL_TIMEOUT;
  1921. }
  1922. }
  1923. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  1924. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  1925. }
  1926. else
  1927. {
  1928. /* Disable the Internal High Speed oscillator (HSI). */
  1929. __HAL_RCC_HSI_DISABLE();
  1930. /* Get Start Tick*/
  1931. tickstart = HAL_GetTick();
  1932. /* Wait till HSI is ready */
  1933. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  1934. {
  1935. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  1936. {
  1937. return HAL_TIMEOUT;
  1938. }
  1939. }
  1940. }
  1941. }
  1942. }
  1943. /*------------------------------ LSI Configuration -------------------------*/
  1944. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  1945. {
  1946. /* Check the parameters */
  1947. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  1948. /* Check the LSI State */
  1949. if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
  1950. {
  1951. /* Enable the Internal Low Speed oscillator (LSI). */
  1952. __HAL_RCC_LSI_ENABLE();
  1953. /* Get Start Tick*/
  1954. tickstart = HAL_GetTick();
  1955. /* Wait till LSI is ready */
  1956. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  1957. {
  1958. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  1959. {
  1960. return HAL_TIMEOUT;
  1961. }
  1962. }
  1963. }
  1964. else
  1965. {
  1966. /* Disable the Internal Low Speed oscillator (LSI). */
  1967. __HAL_RCC_LSI_DISABLE();
  1968. /* Get Start Tick*/
  1969. tickstart = HAL_GetTick();
  1970. /* Wait till LSI is ready */
  1971. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  1972. {
  1973. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  1974. {
  1975. return HAL_TIMEOUT;
  1976. }
  1977. }
  1978. }
  1979. }
  1980. /*------------------------------ LSE Configuration -------------------------*/
  1981. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  1982. {
  1983. /* Check the parameters */
  1984. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  1985. /* Enable Power Clock*/
  1986. __HAL_RCC_PWR_CLK_ENABLE();
  1987. /* Enable write access to Backup domain */
  1988. PWR->CR |= PWR_CR_DBP;
  1989. /* Wait for Backup domain Write protection disable */
  1990. tickstart = HAL_GetTick();
  1991. while((PWR->CR & PWR_CR_DBP) == RESET)
  1992. {
  1993. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  1994. {
  1995. return HAL_TIMEOUT;
  1996. }
  1997. }
  1998. /* Set the new LSE configuration -----------------------------------------*/
  1999. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  2000. /* Check the LSE State */
  2001. if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
  2002. {
  2003. /* Get Start Tick*/
  2004. tickstart = HAL_GetTick();
  2005. /* Wait till LSE is ready */
  2006. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  2007. {
  2008. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  2009. {
  2010. return HAL_TIMEOUT;
  2011. }
  2012. }
  2013. }
  2014. else
  2015. {
  2016. /* Get Start Tick*/
  2017. tickstart = HAL_GetTick();
  2018. /* Wait till LSE is ready */
  2019. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  2020. {
  2021. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  2022. {
  2023. return HAL_TIMEOUT;
  2024. }
  2025. }
  2026. }
  2027. }
  2028. /*-------------------------------- PLL Configuration -----------------------*/
  2029. /* Check the parameters */
  2030. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  2031. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  2032. {
  2033. /* Check if the PLL is used as system clock or not */
  2034. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  2035. {
  2036. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  2037. {
  2038. /* Check the parameters */
  2039. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  2040. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  2041. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  2042. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  2043. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  2044. assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
  2045. /* Disable the main PLL. */
  2046. __HAL_RCC_PLL_DISABLE();
  2047. /* Get Start Tick*/
  2048. tickstart = HAL_GetTick();
  2049. /* Wait till PLL is ready */
  2050. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  2051. {
  2052. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  2053. {
  2054. return HAL_TIMEOUT;
  2055. }
  2056. }
  2057. /* Configure the main PLL clock source, multiplication and division factors. */
  2058. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  2059. RCC_OscInitStruct->PLL.PLLM,
  2060. RCC_OscInitStruct->PLL.PLLN,
  2061. RCC_OscInitStruct->PLL.PLLP,
  2062. RCC_OscInitStruct->PLL.PLLQ,
  2063. RCC_OscInitStruct->PLL.PLLR);
  2064. /* Enable the main PLL. */
  2065. __HAL_RCC_PLL_ENABLE();
  2066. /* Get Start Tick*/
  2067. tickstart = HAL_GetTick();
  2068. /* Wait till PLL is ready */
  2069. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  2070. {
  2071. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  2072. {
  2073. return HAL_TIMEOUT;
  2074. }
  2075. }
  2076. }
  2077. else
  2078. {
  2079. /* Disable the main PLL. */
  2080. __HAL_RCC_PLL_DISABLE();
  2081. /* Get Start Tick*/
  2082. tickstart = HAL_GetTick();
  2083. /* Wait till PLL is ready */
  2084. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  2085. {
  2086. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  2087. {
  2088. return HAL_TIMEOUT;
  2089. }
  2090. }
  2091. }
  2092. }
  2093. else
  2094. {
  2095. return HAL_ERROR;
  2096. }
  2097. }
  2098. return HAL_OK;
  2099. }
  2100. /**
  2101. * @brief Configures the RCC_OscInitStruct according to the internal
  2102. * RCC configuration registers.
  2103. * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that will be configured.
  2104. *
  2105. * @note This function is only available in case of STM32F410xx/STM32F446xx/STM32F469xx/STM32F479xx/STM32F412Zx/STM32F412Vx/STM32F412Rx/STM32F412Cx devices.
  2106. * @note This function add the PLL/PLLR factor management
  2107. * @retval None
  2108. */
  2109. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  2110. {
  2111. /* Set all possible values for the Oscillator type parameter ---------------*/
  2112. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  2113. /* Get the HSE configuration -----------------------------------------------*/
  2114. if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  2115. {
  2116. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  2117. }
  2118. else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
  2119. {
  2120. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  2121. }
  2122. else
  2123. {
  2124. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  2125. }
  2126. /* Get the HSI configuration -----------------------------------------------*/
  2127. if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
  2128. {
  2129. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  2130. }
  2131. else
  2132. {
  2133. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  2134. }
  2135. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR &RCC_CR_HSITRIM) >> POSITION_VAL(RCC_CR_HSITRIM));
  2136. /* Get the LSE configuration -----------------------------------------------*/
  2137. if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  2138. {
  2139. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  2140. }
  2141. else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  2142. {
  2143. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  2144. }
  2145. else
  2146. {
  2147. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  2148. }
  2149. /* Get the LSI configuration -----------------------------------------------*/
  2150. if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
  2151. {
  2152. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  2153. }
  2154. else
  2155. {
  2156. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  2157. }
  2158. /* Get the PLL configuration -----------------------------------------------*/
  2159. if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
  2160. {
  2161. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  2162. }
  2163. else
  2164. {
  2165. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  2166. }
  2167. RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  2168. RCC_OscInitStruct->PLL.PLLM = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM);
  2169. RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN));
  2170. RCC_OscInitStruct->PLL.PLLP = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) + RCC_PLLCFGR_PLLP_0) << 1U) >> POSITION_VAL(RCC_PLLCFGR_PLLP));
  2171. RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLQ) >> POSITION_VAL(RCC_PLLCFGR_PLLQ));
  2172. RCC_OscInitStruct->PLL.PLLR = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> POSITION_VAL(RCC_PLLCFGR_PLLR));
  2173. }
  2174. #endif /* STM32F410xx || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx */
  2175. #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) ||\
  2176. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx)
  2177. /**
  2178. * @brief Select LSE mode
  2179. *
  2180. * @note This mode is only available for STM32F410xx/STM32F411xx/STM32F446xx/STM32F469xx/STM32F479xx/STM32F412Zx/STM32F412Vx/STM32F412Rx/STM32F412Cx devices.
  2181. *
  2182. * @param Mode: specifies the LSE mode.
  2183. * This parameter can be one of the following values:
  2184. * @arg RCC_LSE_LOWPOWER_MODE: LSE oscillator in low power mode selection
  2185. * @arg RCC_LSE_HIGHDRIVE_MODE: LSE oscillator in High Drive mode selection
  2186. * @retval None
  2187. */
  2188. void HAL_RCCEx_SelectLSEMode(uint8_t Mode)
  2189. {
  2190. /* Check the parameters */
  2191. assert_param(IS_RCC_LSE_MODE(Mode));
  2192. if(Mode == RCC_LSE_HIGHDRIVE_MODE)
  2193. {
  2194. SET_BIT(RCC->BDCR, RCC_BDCR_LSEMOD);
  2195. }
  2196. else
  2197. {
  2198. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEMOD);
  2199. }
  2200. }
  2201. #endif /* STM32F410xx || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx */
  2202. #if defined(STM32F446xx)
  2203. /**
  2204. * @brief Returns the SYSCLK frequency
  2205. *
  2206. * @note This function implementation is valid only for STM32F446xx devices.
  2207. * @note This function add the PLL/PLLR System clock source
  2208. *
  2209. * @note The system frequency computed by this function is not the real
  2210. * frequency in the chip. It is calculated based on the predefined
  2211. * constant and the selected clock source:
  2212. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  2213. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  2214. * @note If SYSCLK source is PLL or PLLR, function returns values based on HSE_VALUE(**)
  2215. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  2216. * @note (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  2217. * 16 MHz) but the real value may vary depending on the variations
  2218. * in voltage and temperature.
  2219. * @note (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  2220. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  2221. * frequency of the crystal used. Otherwise, this function may
  2222. * have wrong result.
  2223. *
  2224. * @note The result of this function could be not correct when using fractional
  2225. * value for HSE crystal.
  2226. *
  2227. * @note This function can be used by the user application to compute the
  2228. * baudrate for the communication peripherals or configure other parameters.
  2229. *
  2230. * @note Each time SYSCLK changes, this function must be called to update the
  2231. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  2232. *
  2233. *
  2234. * @retval SYSCLK frequency
  2235. */
  2236. uint32_t HAL_RCC_GetSysClockFreq(void)
  2237. {
  2238. uint32_t pllm = 0U;
  2239. uint32_t pllvco = 0U;
  2240. uint32_t pllp = 0U;
  2241. uint32_t pllr = 0U;
  2242. uint32_t sysclockfreq = 0U;
  2243. /* Get SYSCLK source -------------------------------------------------------*/
  2244. switch (RCC->CFGR & RCC_CFGR_SWS)
  2245. {
  2246. case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
  2247. {
  2248. sysclockfreq = HSI_VALUE;
  2249. break;
  2250. }
  2251. case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
  2252. {
  2253. sysclockfreq = HSE_VALUE;
  2254. break;
  2255. }
  2256. case RCC_CFGR_SWS_PLL: /* PLL/PLLP used as system clock source */
  2257. {
  2258. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  2259. SYSCLK = PLL_VCO / PLLP */
  2260. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  2261. if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI)
  2262. {
  2263. /* HSE used as PLL clock source */
  2264. pllvco = ((HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN)));
  2265. }
  2266. else
  2267. {
  2268. /* HSI used as PLL clock source */
  2269. pllvco = ((HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN)));
  2270. }
  2271. pllp = ((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> POSITION_VAL(RCC_PLLCFGR_PLLP)) + 1U) *2U);
  2272. sysclockfreq = pllvco/pllp;
  2273. break;
  2274. }
  2275. case RCC_CFGR_SWS_PLLR: /* PLL/PLLR used as system clock source */
  2276. {
  2277. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  2278. SYSCLK = PLL_VCO / PLLR */
  2279. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  2280. if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI)
  2281. {
  2282. /* HSE used as PLL clock source */
  2283. pllvco = ((HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN)));
  2284. }
  2285. else
  2286. {
  2287. /* HSI used as PLL clock source */
  2288. pllvco = ((HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN)));
  2289. }
  2290. pllr = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> POSITION_VAL(RCC_PLLCFGR_PLLR));
  2291. sysclockfreq = pllvco/pllr;
  2292. break;
  2293. }
  2294. default:
  2295. {
  2296. sysclockfreq = HSI_VALUE;
  2297. break;
  2298. }
  2299. }
  2300. return sysclockfreq;
  2301. }
  2302. #endif /* STM32F446xx */
  2303. /**
  2304. * @}
  2305. */
  2306. /**
  2307. * @}
  2308. */
  2309. #endif /* HAL_RCC_MODULE_ENABLED */
  2310. /**
  2311. * @}
  2312. */
  2313. /**
  2314. * @}
  2315. */
  2316. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/