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.
 
 
 

2814 lines
94 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_rcc_ex.c
  4. * @author MCD Application Team
  5. * @version V1.7.2
  6. * @date 16-June-2017
  7. * @brief Extended RCC HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities RCC extended peripheral:
  10. * + Extended Peripheral Control functions
  11. * + Extended Clock management functions
  12. * + Extended Clock Recovery System Control functions
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  18. *
  19. * Redistribution and use in source and binary forms, with or without modification,
  20. * are permitted provided that the following conditions are met:
  21. * 1. Redistributions of source code must retain the above copyright notice,
  22. * this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright notice,
  24. * this list of conditions and the following disclaimer in the documentation
  25. * and/or other materials provided with the distribution.
  26. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  27. * may be used to endorse or promote products derived from this software
  28. * without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  34. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  36. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  38. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. ******************************************************************************
  42. */
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32l4xx_hal.h"
  45. /** @addtogroup STM32L4xx_HAL_Driver
  46. * @{
  47. */
  48. /** @defgroup RCCEx RCCEx
  49. * @brief RCC Extended HAL module driver
  50. * @{
  51. */
  52. #ifdef HAL_RCC_MODULE_ENABLED
  53. /* Private typedef -----------------------------------------------------------*/
  54. /* Private defines -----------------------------------------------------------*/
  55. /** @defgroup RCCEx_Private_Constants RCCEx Private Constants
  56. * @{
  57. */
  58. #define PLLSAI1_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  59. #define PLLSAI2_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  60. #define PLL_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  61. #define DIVIDER_P_UPDATE 0U
  62. #define DIVIDER_Q_UPDATE 1U
  63. #define DIVIDER_R_UPDATE 2U
  64. #define __LSCO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  65. #define LSCO_GPIO_PORT GPIOA
  66. #define LSCO_PIN GPIO_PIN_2
  67. /**
  68. * @}
  69. */
  70. /* Private macros ------------------------------------------------------------*/
  71. /* Private variables ---------------------------------------------------------*/
  72. /* Private function prototypes -----------------------------------------------*/
  73. /** @defgroup RCCEx_Private_Functions RCCEx Private Functions
  74. * @{
  75. */
  76. static HAL_StatusTypeDef RCCEx_PLLSAI1_Config(RCC_PLLSAI1InitTypeDef *PllSai1, uint32_t Divider);
  77. #if defined(RCC_PLLSAI2_SUPPORT)
  78. static HAL_StatusTypeDef RCCEx_PLLSAI2_Config(RCC_PLLSAI2InitTypeDef *PllSai2, uint32_t Divider);
  79. #endif /* RCC_PLLSAI2_SUPPORT */
  80. /**
  81. * @}
  82. */
  83. /* Exported functions --------------------------------------------------------*/
  84. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  85. * @{
  86. */
  87. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  88. * @brief Extended Peripheral Control functions
  89. *
  90. @verbatim
  91. ===============================================================================
  92. ##### Extended Peripheral Control functions #####
  93. ===============================================================================
  94. [..]
  95. This subsection provides a set of functions allowing to control the RCC Clocks
  96. frequencies.
  97. [..]
  98. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  99. select the RTC clock source; in this case the Backup domain will be reset in
  100. order to modify the RTC Clock source, as consequence RTC registers (including
  101. the backup registers) are set to their reset values.
  102. @endverbatim
  103. * @{
  104. */
  105. /**
  106. * @brief Initialize the RCC extended peripherals clocks according to the specified
  107. * parameters in the RCC_PeriphCLKInitTypeDef.
  108. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  109. * contains a field PeriphClockSelection which can be a combination of the following values:
  110. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  111. * @arg @ref RCC_PERIPHCLK_ADC ADC peripheral clock
  112. @if STM32L462xx
  113. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral clock (only for devices with DFSDM1)
  114. @endif
  115. @if STM32L486xx
  116. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral clock (only for devices with DFSDM1)
  117. @endif
  118. @if STM32L4A6xx
  119. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral clock (only for devices with DFSDM1)
  120. @endif
  121. * @arg @ref RCC_PERIPHCLK_I2C1 I2C1 peripheral clock
  122. * @arg @ref RCC_PERIPHCLK_I2C2 I2C2 peripheral clock
  123. * @arg @ref RCC_PERIPHCLK_I2C3 I2C3 peripheral clock
  124. @if STM32L462xx
  125. * @arg @ref RCC_PERIPHCLK_I2C4 I2C4 peripheral clock (only for devices with I2C4)
  126. @endif
  127. @if STM32L4A6xx
  128. * @arg @ref RCC_PERIPHCLK_I2C4 I2C4 peripheral clock (only for devices with I2C4)
  129. @endif
  130. * @arg @ref RCC_PERIPHCLK_LPTIM1 LPTIM1 peripheral clock
  131. * @arg @ref RCC_PERIPHCLK_LPTIM2 LPTIM2 peripheral clock
  132. * @arg @ref RCC_PERIPHCLK_LPUART1 LPUART1 peripheral clock
  133. * @arg @ref RCC_PERIPHCLK_RNG RNG peripheral clock
  134. * @arg @ref RCC_PERIPHCLK_SAI1 SAI1 peripheral clock
  135. @if STM32L486xx
  136. * @arg @ref RCC_PERIPHCLK_SAI2 SAI2 peripheral clock (only for devices with SAI2)
  137. @endif
  138. @if STM32L4A6xx
  139. * @arg @ref RCC_PERIPHCLK_SAI2 SAI2 peripheral clock (only for devices with SAI2)
  140. @endif
  141. * @arg @ref RCC_PERIPHCLK_SDMMC1 SDMMC1 peripheral clock
  142. @if STM32L443xx
  143. * @arg @ref RCC_PERIPHCLK_SWPMI1 SWPMI1 peripheral clock (only for devices with SWPMI1)
  144. @endif
  145. @if STM32L486xx
  146. * @arg @ref RCC_PERIPHCLK_SWPMI1 SWPMI1 peripheral clock (only for devices with SWPMI1)
  147. @endif
  148. @if STM32L4A6xx
  149. * @arg @ref RCC_PERIPHCLK_SWPMI1 SWPMI1 peripheral clock (only for devices with SWPMI1)
  150. @endif
  151. * @arg @ref RCC_PERIPHCLK_USART1 USART1 peripheral clock
  152. * @arg @ref RCC_PERIPHCLK_USART2 USART1 peripheral clock
  153. * @arg @ref RCC_PERIPHCLK_USART3 USART1 peripheral clock
  154. @if STM32L462xx
  155. * @arg @ref RCC_PERIPHCLK_UART4 USART1 peripheral clock (only for devices with UART4)
  156. @endif
  157. @if STM32L486xx
  158. * @arg @ref RCC_PERIPHCLK_UART4 USART1 peripheral clock (only for devices with UART4)
  159. * @arg @ref RCC_PERIPHCLK_UART5 USART1 peripheral clock (only for devices with UART5)
  160. @endif
  161. @if STM32L4A6xx
  162. * @arg @ref RCC_PERIPHCLK_UART4 USART1 peripheral clock (only for devices with UART4)
  163. * @arg @ref RCC_PERIPHCLK_UART5 USART1 peripheral clock (only for devices with UART5)
  164. @endif
  165. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock (only for devices with USB)
  166. *
  167. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  168. * the RTC clock source: in this case the access to Backup domain is enabled.
  169. *
  170. * @retval HAL status
  171. */
  172. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  173. {
  174. uint32_t tmpregister = 0;
  175. uint32_t tickstart = 0U;
  176. HAL_StatusTypeDef ret = HAL_OK; /* Intermediate status */
  177. HAL_StatusTypeDef status = HAL_OK; /* Final status */
  178. /* Check the parameters */
  179. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  180. /*-------------------------- SAI1 clock source configuration ---------------------*/
  181. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == RCC_PERIPHCLK_SAI1))
  182. {
  183. /* Check the parameters */
  184. assert_param(IS_RCC_SAI1CLK(PeriphClkInit->Sai1ClockSelection));
  185. switch(PeriphClkInit->Sai1ClockSelection)
  186. {
  187. case RCC_SAI1CLKSOURCE_PLL: /* PLL is used as clock source for SAI1*/
  188. /* Enable SAI Clock output generated form System PLL . */
  189. #if defined(RCC_PLLSAI2_SUPPORT)
  190. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SAI3CLK);
  191. #else
  192. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SAI2CLK);
  193. #endif /* RCC_PLLSAI2_SUPPORT */
  194. /* SAI1 clock source config set later after clock selection check */
  195. break;
  196. case RCC_SAI1CLKSOURCE_PLLSAI1: /* PLLSAI1 is used as clock source for SAI1*/
  197. /* PLLSAI1 input clock, parameters M, N & P configuration and clock output (PLLSAI1ClockOut) */
  198. ret = RCCEx_PLLSAI1_Config(&(PeriphClkInit->PLLSAI1), DIVIDER_P_UPDATE);
  199. /* SAI1 clock source config set later after clock selection check */
  200. break;
  201. #if defined(RCC_PLLSAI2_SUPPORT)
  202. case RCC_SAI1CLKSOURCE_PLLSAI2: /* PLLSAI2 is used as clock source for SAI1*/
  203. /* PLLSAI2 input clock, parameters M, N & P configuration clock output (PLLSAI2ClockOut) */
  204. ret = RCCEx_PLLSAI2_Config(&(PeriphClkInit->PLLSAI2), DIVIDER_P_UPDATE);
  205. /* SAI1 clock source config set later after clock selection check */
  206. break;
  207. #endif /* RCC_PLLSAI2_SUPPORT */
  208. case RCC_SAI1CLKSOURCE_PIN: /* External clock is used as source of SAI1 clock*/
  209. /* SAI1 clock source config set later after clock selection check */
  210. break;
  211. default:
  212. ret = HAL_ERROR;
  213. break;
  214. }
  215. if(ret == HAL_OK)
  216. {
  217. /* Set the source of SAI1 clock*/
  218. __HAL_RCC_SAI1_CONFIG(PeriphClkInit->Sai1ClockSelection);
  219. }
  220. else
  221. {
  222. /* set overall return value */
  223. status = ret;
  224. }
  225. }
  226. #if defined(SAI2)
  227. /*-------------------------- SAI2 clock source configuration ---------------------*/
  228. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI2) == RCC_PERIPHCLK_SAI2))
  229. {
  230. /* Check the parameters */
  231. assert_param(IS_RCC_SAI2CLK(PeriphClkInit->Sai2ClockSelection));
  232. switch(PeriphClkInit->Sai2ClockSelection)
  233. {
  234. case RCC_SAI2CLKSOURCE_PLL: /* PLL is used as clock source for SAI2*/
  235. /* Enable SAI Clock output generated form System PLL . */
  236. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SAI3CLK);
  237. /* SAI2 clock source config set later after clock selection check */
  238. break;
  239. case RCC_SAI2CLKSOURCE_PLLSAI1: /* PLLSAI1 is used as clock source for SAI2*/
  240. /* PLLSAI1 input clock, parameters M, N & P configuration and clock output (PLLSAI1ClockOut) */
  241. ret = RCCEx_PLLSAI1_Config(&(PeriphClkInit->PLLSAI1), DIVIDER_P_UPDATE);
  242. /* SAI2 clock source config set later after clock selection check */
  243. break;
  244. case RCC_SAI2CLKSOURCE_PLLSAI2: /* PLLSAI2 is used as clock source for SAI2*/
  245. /* PLLSAI2 input clock, parameters M, N & P configuration and clock output (PLLSAI2ClockOut) */
  246. ret = RCCEx_PLLSAI2_Config(&(PeriphClkInit->PLLSAI2), DIVIDER_P_UPDATE);
  247. /* SAI2 clock source config set later after clock selection check */
  248. break;
  249. case RCC_SAI2CLKSOURCE_PIN: /* External clock is used as source of SAI2 clock*/
  250. /* SAI2 clock source config set later after clock selection check */
  251. break;
  252. default:
  253. ret = HAL_ERROR;
  254. break;
  255. }
  256. if(ret == HAL_OK)
  257. {
  258. /* Set the source of SAI2 clock*/
  259. __HAL_RCC_SAI2_CONFIG(PeriphClkInit->Sai2ClockSelection);
  260. }
  261. else
  262. {
  263. /* set overall return value */
  264. status = ret;
  265. }
  266. }
  267. #endif /* SAI2 */
  268. /*-------------------------- RTC clock source configuration ----------------------*/
  269. if((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC)
  270. {
  271. FlagStatus pwrclkchanged = RESET;
  272. /* Check for RTC Parameters used to output RTCCLK */
  273. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  274. /* Enable Power Clock */
  275. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  276. {
  277. __HAL_RCC_PWR_CLK_ENABLE();
  278. pwrclkchanged = SET;
  279. }
  280. /* Enable write access to Backup domain */
  281. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  282. /* Wait for Backup domain Write protection disable */
  283. tickstart = HAL_GetTick();
  284. while((PWR->CR1 & PWR_CR1_DBP) == RESET)
  285. {
  286. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  287. {
  288. ret = HAL_TIMEOUT;
  289. break;
  290. }
  291. }
  292. if(ret == HAL_OK)
  293. {
  294. /* Reset the Backup domain only if the RTC Clock source selection is modified from default */
  295. tmpregister = READ_BIT(RCC->BDCR, RCC_BDCR_RTCSEL);
  296. if((tmpregister != RCC_RTCCLKSOURCE_NO_CLK) && (tmpregister != PeriphClkInit->RTCClockSelection))
  297. {
  298. /* Store the content of BDCR register before the reset of Backup Domain */
  299. tmpregister = READ_BIT(RCC->BDCR, ~(RCC_BDCR_RTCSEL));
  300. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  301. __HAL_RCC_BACKUPRESET_FORCE();
  302. __HAL_RCC_BACKUPRESET_RELEASE();
  303. /* Restore the Content of BDCR register */
  304. RCC->BDCR = tmpregister;
  305. }
  306. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  307. if (HAL_IS_BIT_SET(tmpregister, RCC_BDCR_LSEON))
  308. {
  309. /* Get Start Tick*/
  310. tickstart = HAL_GetTick();
  311. /* Wait till LSE is ready */
  312. while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == RESET)
  313. {
  314. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  315. {
  316. ret = HAL_TIMEOUT;
  317. break;
  318. }
  319. }
  320. }
  321. if(ret == HAL_OK)
  322. {
  323. /* Apply new RTC clock source selection */
  324. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  325. }
  326. else
  327. {
  328. /* set overall return value */
  329. status = ret;
  330. }
  331. }
  332. else
  333. {
  334. /* set overall return value */
  335. status = ret;
  336. }
  337. /* Restore clock configuration if changed */
  338. if(pwrclkchanged == SET)
  339. {
  340. __HAL_RCC_PWR_CLK_DISABLE();
  341. }
  342. }
  343. /*-------------------------- USART1 clock source configuration -------------------*/
  344. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART1) == RCC_PERIPHCLK_USART1)
  345. {
  346. /* Check the parameters */
  347. assert_param(IS_RCC_USART1CLKSOURCE(PeriphClkInit->Usart1ClockSelection));
  348. /* Configure the USART1 clock source */
  349. __HAL_RCC_USART1_CONFIG(PeriphClkInit->Usart1ClockSelection);
  350. }
  351. /*-------------------------- USART2 clock source configuration -------------------*/
  352. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART2) == RCC_PERIPHCLK_USART2)
  353. {
  354. /* Check the parameters */
  355. assert_param(IS_RCC_USART2CLKSOURCE(PeriphClkInit->Usart2ClockSelection));
  356. /* Configure the USART2 clock source */
  357. __HAL_RCC_USART2_CONFIG(PeriphClkInit->Usart2ClockSelection);
  358. }
  359. #if defined(USART3)
  360. /*-------------------------- USART3 clock source configuration -------------------*/
  361. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART3) == RCC_PERIPHCLK_USART3)
  362. {
  363. /* Check the parameters */
  364. assert_param(IS_RCC_USART3CLKSOURCE(PeriphClkInit->Usart3ClockSelection));
  365. /* Configure the USART3 clock source */
  366. __HAL_RCC_USART3_CONFIG(PeriphClkInit->Usart3ClockSelection);
  367. }
  368. #endif /* USART3 */
  369. #if defined(UART4)
  370. /*-------------------------- UART4 clock source configuration --------------------*/
  371. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_UART4) == RCC_PERIPHCLK_UART4)
  372. {
  373. /* Check the parameters */
  374. assert_param(IS_RCC_UART4CLKSOURCE(PeriphClkInit->Uart4ClockSelection));
  375. /* Configure the UART4 clock source */
  376. __HAL_RCC_UART4_CONFIG(PeriphClkInit->Uart4ClockSelection);
  377. }
  378. #endif /* UART4 */
  379. #if defined(UART5)
  380. /*-------------------------- UART5 clock source configuration --------------------*/
  381. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_UART5) == RCC_PERIPHCLK_UART5)
  382. {
  383. /* Check the parameters */
  384. assert_param(IS_RCC_UART5CLKSOURCE(PeriphClkInit->Uart5ClockSelection));
  385. /* Configure the UART5 clock source */
  386. __HAL_RCC_UART5_CONFIG(PeriphClkInit->Uart5ClockSelection);
  387. }
  388. #endif /* UART5 */
  389. /*-------------------------- LPUART1 clock source configuration ------------------*/
  390. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPUART1) == RCC_PERIPHCLK_LPUART1)
  391. {
  392. /* Check the parameters */
  393. assert_param(IS_RCC_LPUART1CLKSOURCE(PeriphClkInit->Lpuart1ClockSelection));
  394. /* Configure the LPUAR1 clock source */
  395. __HAL_RCC_LPUART1_CONFIG(PeriphClkInit->Lpuart1ClockSelection);
  396. }
  397. /*-------------------------- LPTIM1 clock source configuration -------------------*/
  398. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM1) == (RCC_PERIPHCLK_LPTIM1))
  399. {
  400. assert_param(IS_RCC_LPTIM1CLK(PeriphClkInit->Lptim1ClockSelection));
  401. __HAL_RCC_LPTIM1_CONFIG(PeriphClkInit->Lptim1ClockSelection);
  402. }
  403. /*-------------------------- LPTIM2 clock source configuration -------------------*/
  404. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM2) == (RCC_PERIPHCLK_LPTIM2))
  405. {
  406. assert_param(IS_RCC_LPTIM2CLK(PeriphClkInit->Lptim2ClockSelection));
  407. __HAL_RCC_LPTIM2_CONFIG(PeriphClkInit->Lptim2ClockSelection);
  408. }
  409. /*-------------------------- I2C1 clock source configuration ---------------------*/
  410. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C1) == RCC_PERIPHCLK_I2C1)
  411. {
  412. /* Check the parameters */
  413. assert_param(IS_RCC_I2C1CLKSOURCE(PeriphClkInit->I2c1ClockSelection));
  414. /* Configure the I2C1 clock source */
  415. __HAL_RCC_I2C1_CONFIG(PeriphClkInit->I2c1ClockSelection);
  416. }
  417. #if defined(I2C2)
  418. /*-------------------------- I2C2 clock source configuration ---------------------*/
  419. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C2) == RCC_PERIPHCLK_I2C2)
  420. {
  421. /* Check the parameters */
  422. assert_param(IS_RCC_I2C2CLKSOURCE(PeriphClkInit->I2c2ClockSelection));
  423. /* Configure the I2C2 clock source */
  424. __HAL_RCC_I2C2_CONFIG(PeriphClkInit->I2c2ClockSelection);
  425. }
  426. #endif /* I2C2 */
  427. /*-------------------------- I2C3 clock source configuration ---------------------*/
  428. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C3) == RCC_PERIPHCLK_I2C3)
  429. {
  430. /* Check the parameters */
  431. assert_param(IS_RCC_I2C3CLKSOURCE(PeriphClkInit->I2c3ClockSelection));
  432. /* Configure the I2C3 clock source */
  433. __HAL_RCC_I2C3_CONFIG(PeriphClkInit->I2c3ClockSelection);
  434. }
  435. #if defined(I2C4)
  436. /*-------------------------- I2C4 clock source configuration ---------------------*/
  437. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C4) == RCC_PERIPHCLK_I2C4)
  438. {
  439. /* Check the parameters */
  440. assert_param(IS_RCC_I2C4CLKSOURCE(PeriphClkInit->I2c4ClockSelection));
  441. /* Configure the I2C4 clock source */
  442. __HAL_RCC_I2C4_CONFIG(PeriphClkInit->I2c4ClockSelection);
  443. }
  444. #endif /* I2C4 */
  445. #if defined(USB_OTG_FS) || defined(USB)
  446. /*-------------------------- USB clock source configuration ----------------------*/
  447. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == (RCC_PERIPHCLK_USB))
  448. {
  449. assert_param(IS_RCC_USBCLKSOURCE(PeriphClkInit->UsbClockSelection));
  450. __HAL_RCC_USB_CONFIG(PeriphClkInit->UsbClockSelection);
  451. if(PeriphClkInit->UsbClockSelection == RCC_USBCLKSOURCE_PLL)
  452. {
  453. /* Enable PLL48M1CLK output */
  454. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_48M1CLK);
  455. }
  456. else
  457. {
  458. if(PeriphClkInit->UsbClockSelection == RCC_USBCLKSOURCE_PLLSAI1)
  459. {
  460. /* PLLSAI1 input clock, parameters M, N & Q configuration and clock output (PLLSAI1ClockOut) */
  461. ret = RCCEx_PLLSAI1_Config(&(PeriphClkInit->PLLSAI1), DIVIDER_Q_UPDATE);
  462. if(ret != HAL_OK)
  463. {
  464. /* set overall return value */
  465. status = ret;
  466. }
  467. }
  468. }
  469. }
  470. #endif /* USB_OTG_FS || USB */
  471. #if defined(SDMMC1)
  472. /*-------------------------- SDMMC1 clock source configuration -------------------*/
  473. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDMMC1) == (RCC_PERIPHCLK_SDMMC1))
  474. {
  475. assert_param(IS_RCC_SDMMC1CLKSOURCE(PeriphClkInit->Sdmmc1ClockSelection));
  476. __HAL_RCC_SDMMC1_CONFIG(PeriphClkInit->Sdmmc1ClockSelection);
  477. if(PeriphClkInit->Sdmmc1ClockSelection == RCC_SDMMC1CLKSOURCE_PLL)
  478. {
  479. /* Enable PLL48M1CLK output */
  480. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_48M1CLK);
  481. }
  482. else if(PeriphClkInit->Sdmmc1ClockSelection == RCC_SDMMC1CLKSOURCE_PLLSAI1)
  483. {
  484. /* PLLSAI1 input clock, parameters M, N & Q configuration and clock output (PLLSAI1ClockOut) */
  485. ret = RCCEx_PLLSAI1_Config(&(PeriphClkInit->PLLSAI1), DIVIDER_Q_UPDATE);
  486. if(ret != HAL_OK)
  487. {
  488. /* set overall return value */
  489. status = ret;
  490. }
  491. }
  492. }
  493. #endif /* SDMMC1 */
  494. /*-------------------------- RNG clock source configuration ----------------------*/
  495. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RNG) == (RCC_PERIPHCLK_RNG))
  496. {
  497. assert_param(IS_RCC_RNGCLKSOURCE(PeriphClkInit->RngClockSelection));
  498. __HAL_RCC_RNG_CONFIG(PeriphClkInit->RngClockSelection);
  499. if(PeriphClkInit->RngClockSelection == RCC_RNGCLKSOURCE_PLL)
  500. {
  501. /* Enable PLL48M1CLK output */
  502. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_48M1CLK);
  503. }
  504. else if(PeriphClkInit->RngClockSelection == RCC_RNGCLKSOURCE_PLLSAI1)
  505. {
  506. /* PLLSAI1 input clock, parameters M, N & Q configuration and clock output (PLLSAI1ClockOut) */
  507. ret = RCCEx_PLLSAI1_Config(&(PeriphClkInit->PLLSAI1), DIVIDER_Q_UPDATE);
  508. if(ret != HAL_OK)
  509. {
  510. /* set overall return value */
  511. status = ret;
  512. }
  513. }
  514. }
  515. /*-------------------------- ADC clock source configuration ----------------------*/
  516. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC) == RCC_PERIPHCLK_ADC)
  517. {
  518. /* Check the parameters */
  519. assert_param(IS_RCC_ADCCLKSOURCE(PeriphClkInit->AdcClockSelection));
  520. /* Configure the ADC interface clock source */
  521. __HAL_RCC_ADC_CONFIG(PeriphClkInit->AdcClockSelection);
  522. if(PeriphClkInit->AdcClockSelection == RCC_ADCCLKSOURCE_PLLSAI1)
  523. {
  524. /* PLLSAI1 input clock, parameters M, N & R configuration and clock output (PLLSAI1ClockOut) */
  525. ret = RCCEx_PLLSAI1_Config(&(PeriphClkInit->PLLSAI1), DIVIDER_R_UPDATE);
  526. if(ret != HAL_OK)
  527. {
  528. /* set overall return value */
  529. status = ret;
  530. }
  531. }
  532. #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L496xx) || defined(STM32L4A6xx)
  533. else if(PeriphClkInit->AdcClockSelection == RCC_ADCCLKSOURCE_PLLSAI2)
  534. {
  535. /* PLLSAI2 input clock, parameters M, N & R configuration and clock output (PLLSAI2ClockOut) */
  536. ret = RCCEx_PLLSAI2_Config(&(PeriphClkInit->PLLSAI2), DIVIDER_R_UPDATE);
  537. if(ret != HAL_OK)
  538. {
  539. /* set overall return value */
  540. status = ret;
  541. }
  542. }
  543. #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || STM32L496xx || STM32L4A6xx */
  544. }
  545. #if defined(SWPMI1)
  546. /*-------------------------- SWPMI1 clock source configuration -------------------*/
  547. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SWPMI1) == RCC_PERIPHCLK_SWPMI1)
  548. {
  549. /* Check the parameters */
  550. assert_param(IS_RCC_SWPMI1CLKSOURCE(PeriphClkInit->Swpmi1ClockSelection));
  551. /* Configure the SWPMI1 clock source */
  552. __HAL_RCC_SWPMI1_CONFIG(PeriphClkInit->Swpmi1ClockSelection);
  553. }
  554. #endif /* SWPMI1 */
  555. #if defined(DFSDM1_Filter0)
  556. /*-------------------------- DFSDM1 clock source configuration -------------------*/
  557. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM1) == RCC_PERIPHCLK_DFSDM1)
  558. {
  559. /* Check the parameters */
  560. assert_param(IS_RCC_DFSDM1CLKSOURCE(PeriphClkInit->Dfsdm1ClockSelection));
  561. /* Configure the DFSDM1 interface clock source */
  562. __HAL_RCC_DFSDM1_CONFIG(PeriphClkInit->Dfsdm1ClockSelection);
  563. }
  564. #endif /* DFSDM1_Filter0 */
  565. return status;
  566. }
  567. /**
  568. * @brief Get the RCC_ClkInitStruct according to the internal RCC configuration registers.
  569. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  570. * returns the configuration information for the Extended Peripherals
  571. * clocks(SAI1, SAI2, LPTIM1, LPTIM2, I2C1, I2C2, I2C3, I2C4, LPUART,
  572. * USART1, USART2, USART3, UART4, UART5, RTC, ADCx, DFSDMx, SWPMI1, USB, SDMMC1 and RNG).
  573. * @retval None
  574. */
  575. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  576. {
  577. /* Set all possible values for the extended clock type parameter------------*/
  578. #if defined(STM32L431xx)
  579. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | \
  580. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | \
  581. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | \
  582. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_SWPMI1 | \
  583. RCC_PERIPHCLK_RTC ;
  584. #elif defined(STM32L432xx) || defined(STM32L442xx)
  585. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | \
  586. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C3 | \
  587. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_USB | \
  588. RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_SWPMI1 | \
  589. RCC_PERIPHCLK_RTC ;
  590. #elif defined(STM32L433xx) || defined(STM32L443xx)
  591. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | \
  592. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | \
  593. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_USB | \
  594. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_SWPMI1 | \
  595. RCC_PERIPHCLK_RTC ;
  596. #elif defined(STM32L451xx)
  597. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | \
  598. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | RCC_PERIPHCLK_I2C4 | \
  599. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | \
  600. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_DFSDM1 | \
  601. RCC_PERIPHCLK_RTC ;
  602. #elif defined(STM32L452xx) || defined(STM32L462xx)
  603. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | \
  604. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | RCC_PERIPHCLK_I2C4 | \
  605. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_USB | \
  606. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_DFSDM1 | \
  607. RCC_PERIPHCLK_RTC ;
  608. #elif defined(STM32L471xx)
  609. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | RCC_PERIPHCLK_UART5 | \
  610. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | \
  611. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_SAI2 | \
  612. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_SWPMI1 | RCC_PERIPHCLK_DFSDM1 | \
  613. RCC_PERIPHCLK_RTC ;
  614. #elif defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx)
  615. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | RCC_PERIPHCLK_UART5 | \
  616. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | \
  617. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_SAI2 | RCC_PERIPHCLK_USB | \
  618. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_SWPMI1 | RCC_PERIPHCLK_DFSDM1 | \
  619. RCC_PERIPHCLK_RTC ;
  620. #elif defined(STM32L496xx) || defined(STM32L4A6xx)
  621. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | RCC_PERIPHCLK_UART5 | \
  622. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | RCC_PERIPHCLK_I2C4 | \
  623. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_SAI2 | RCC_PERIPHCLK_USB | \
  624. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_SWPMI1 | RCC_PERIPHCLK_DFSDM1 | \
  625. RCC_PERIPHCLK_RTC ;
  626. #endif /* STM32L431xx */
  627. /* Get the PLLSAI1 Clock configuration -----------------------------------------------*/
  628. PeriphClkInit->PLLSAI1.PLLSAI1Source = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> RCC_PLLCFGR_PLLSRC_Pos);
  629. PeriphClkInit->PLLSAI1.PLLSAI1M = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U;
  630. PeriphClkInit->PLLSAI1.PLLSAI1N = (uint32_t)((RCC->PLLSAI1CFGR & RCC_PLLSAI1CFGR_PLLSAI1N) >> RCC_PLLSAI1CFGR_PLLSAI1N_Pos);
  631. PeriphClkInit->PLLSAI1.PLLSAI1P = (uint32_t)(((RCC->PLLSAI1CFGR & RCC_PLLSAI1CFGR_PLLSAI1P) >> RCC_PLLSAI1CFGR_PLLSAI1P_Pos) << 4U) + 7U;
  632. PeriphClkInit->PLLSAI1.PLLSAI1Q = (uint32_t)(((RCC->PLLSAI1CFGR & RCC_PLLSAI1CFGR_PLLSAI1Q) >> RCC_PLLSAI1CFGR_PLLSAI1Q_Pos)+1U) * 2U;
  633. PeriphClkInit->PLLSAI1.PLLSAI1R = (uint32_t)(((RCC->PLLSAI1CFGR & RCC_PLLSAI1CFGR_PLLSAI1R) >> RCC_PLLSAI1CFGR_PLLSAI1R_Pos)+1U) * 2U;
  634. #if defined(RCC_PLLSAI2_SUPPORT)
  635. /* Get the PLLSAI2 Clock configuration -----------------------------------------------*/
  636. PeriphClkInit->PLLSAI2.PLLSAI2Source = PeriphClkInit->PLLSAI1.PLLSAI1Source;
  637. PeriphClkInit->PLLSAI2.PLLSAI2M = PeriphClkInit->PLLSAI1.PLLSAI1M;
  638. PeriphClkInit->PLLSAI2.PLLSAI2N = (uint32_t)((RCC->PLLSAI2CFGR & RCC_PLLSAI2CFGR_PLLSAI2N) >> RCC_PLLSAI2CFGR_PLLSAI2N_Pos);
  639. PeriphClkInit->PLLSAI2.PLLSAI2P = (uint32_t)(((RCC->PLLSAI2CFGR & RCC_PLLSAI2CFGR_PLLSAI2P) >> RCC_PLLSAI2CFGR_PLLSAI2P_Pos) << 4U) + 7U;
  640. PeriphClkInit->PLLSAI2.PLLSAI2R = (uint32_t)(((RCC->PLLSAI2CFGR & RCC_PLLSAI2CFGR_PLLSAI2R)>> RCC_PLLSAI2CFGR_PLLSAI2R_Pos)+1U) * 2U;
  641. #endif /* RCC_PLLSAI2_SUPPORT */
  642. /* Get the USART1 clock source ---------------------------------------------*/
  643. PeriphClkInit->Usart1ClockSelection = __HAL_RCC_GET_USART1_SOURCE();
  644. /* Get the USART2 clock source ---------------------------------------------*/
  645. PeriphClkInit->Usart2ClockSelection = __HAL_RCC_GET_USART2_SOURCE();
  646. #if defined(USART3)
  647. /* Get the USART3 clock source ---------------------------------------------*/
  648. PeriphClkInit->Usart3ClockSelection = __HAL_RCC_GET_USART3_SOURCE();
  649. #endif /* USART3 */
  650. #if defined(UART4)
  651. /* Get the UART4 clock source ----------------------------------------------*/
  652. PeriphClkInit->Uart4ClockSelection = __HAL_RCC_GET_UART4_SOURCE();
  653. #endif /* UART4 */
  654. #if defined(UART5)
  655. /* Get the UART5 clock source ----------------------------------------------*/
  656. PeriphClkInit->Uart5ClockSelection = __HAL_RCC_GET_UART5_SOURCE();
  657. #endif /* UART5 */
  658. /* Get the LPUART1 clock source --------------------------------------------*/
  659. PeriphClkInit->Lpuart1ClockSelection = __HAL_RCC_GET_LPUART1_SOURCE();
  660. /* Get the I2C1 clock source -----------------------------------------------*/
  661. PeriphClkInit->I2c1ClockSelection = __HAL_RCC_GET_I2C1_SOURCE();
  662. #if defined(I2C2)
  663. /* Get the I2C2 clock source ----------------------------------------------*/
  664. PeriphClkInit->I2c2ClockSelection = __HAL_RCC_GET_I2C2_SOURCE();
  665. #endif /* I2C2 */
  666. /* Get the I2C3 clock source -----------------------------------------------*/
  667. PeriphClkInit->I2c3ClockSelection = __HAL_RCC_GET_I2C3_SOURCE();
  668. #if defined(I2C4)
  669. /* Get the I2C4 clock source -----------------------------------------------*/
  670. PeriphClkInit->I2c4ClockSelection = __HAL_RCC_GET_I2C4_SOURCE();
  671. #endif /* I2C4 */
  672. /* Get the LPTIM1 clock source ---------------------------------------------*/
  673. PeriphClkInit->Lptim1ClockSelection = __HAL_RCC_GET_LPTIM1_SOURCE();
  674. /* Get the LPTIM2 clock source ---------------------------------------------*/
  675. PeriphClkInit->Lptim2ClockSelection = __HAL_RCC_GET_LPTIM2_SOURCE();
  676. /* Get the SAI1 clock source -----------------------------------------------*/
  677. PeriphClkInit->Sai1ClockSelection = __HAL_RCC_GET_SAI1_SOURCE();
  678. #if defined(SAI2)
  679. /* Get the SAI2 clock source -----------------------------------------------*/
  680. PeriphClkInit->Sai2ClockSelection = __HAL_RCC_GET_SAI2_SOURCE();
  681. #endif /* SAI2 */
  682. /* Get the RTC clock source ------------------------------------------------*/
  683. PeriphClkInit->RTCClockSelection = __HAL_RCC_GET_RTC_SOURCE();
  684. #if defined(USB_OTG_FS) || defined(USB)
  685. /* Get the USB clock source ------------------------------------------------*/
  686. PeriphClkInit->UsbClockSelection = __HAL_RCC_GET_USB_SOURCE();
  687. #endif /* USB_OTG_FS || USB */
  688. #if defined(SDMMC1)
  689. /* Get the SDMMC1 clock source ---------------------------------------------*/
  690. PeriphClkInit->Sdmmc1ClockSelection = __HAL_RCC_GET_SDMMC1_SOURCE();
  691. #endif /* SDMMC1 */
  692. /* Get the RNG clock source ------------------------------------------------*/
  693. PeriphClkInit->RngClockSelection = __HAL_RCC_GET_RNG_SOURCE();
  694. /* Get the ADC clock source ------------------------------------------------*/
  695. PeriphClkInit->AdcClockSelection = __HAL_RCC_GET_ADC_SOURCE();
  696. #if defined(SWPMI1)
  697. /* Get the SWPMI1 clock source ---------------------------------------------*/
  698. PeriphClkInit->Swpmi1ClockSelection = __HAL_RCC_GET_SWPMI1_SOURCE();
  699. #endif /* SWPMI1 */
  700. #if defined(DFSDM1_Filter0)
  701. /* Get the DFSDM1 clock source ---------------------------------------------*/
  702. PeriphClkInit->Dfsdm1ClockSelection = __HAL_RCC_GET_DFSDM1_SOURCE();
  703. #endif /* DFSDM1_Filter0 */
  704. }
  705. /**
  706. * @brief Return the peripheral clock frequency for peripherals with clock source from PLLSAIs
  707. * @note Return 0 if peripheral clock identifier not managed by this API
  708. * @param PeriphClk Peripheral clock identifier
  709. * This parameter can be one of the following values:
  710. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  711. * @arg @ref RCC_PERIPHCLK_ADC ADC peripheral clock
  712. @if STM32L462xx
  713. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral clock (only for devices with DFSDM)
  714. @endif
  715. @if STM32L486xx
  716. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral clock (only for devices with DFSDM)
  717. @endif
  718. @if STM32L4A6xx
  719. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral clock (only for devices with DFSDM)
  720. @endif
  721. * @arg @ref RCC_PERIPHCLK_I2C1 I2C1 peripheral clock
  722. * @arg @ref RCC_PERIPHCLK_I2C2 I2C2 peripheral clock
  723. * @arg @ref RCC_PERIPHCLK_I2C3 I2C3 peripheral clock
  724. @if STM32L462xx
  725. * @arg @ref RCC_PERIPHCLK_I2C4 I2C4 peripheral clock (only for devices with I2C4)
  726. @endif
  727. @if STM32L4A6xx
  728. * @arg @ref RCC_PERIPHCLK_I2C4 I2C4 peripheral clock (only for devices with I2C4)
  729. @endif
  730. * @arg @ref RCC_PERIPHCLK_LPTIM1 LPTIM1 peripheral clock
  731. * @arg @ref RCC_PERIPHCLK_LPTIM2 LPTIM2 peripheral clock
  732. * @arg @ref RCC_PERIPHCLK_LPUART1 LPUART1 peripheral clock
  733. * @arg @ref RCC_PERIPHCLK_RNG RNG peripheral clock
  734. * @arg @ref RCC_PERIPHCLK_SAI1 SAI1 peripheral clock
  735. @if STM32L486xx
  736. * @arg @ref RCC_PERIPHCLK_SAI2 SAI2 peripheral clock (only for devices with SAI2)
  737. @endif
  738. @if STM32L4A6xx
  739. * @arg @ref RCC_PERIPHCLK_SAI2 SAI2 peripheral clock (only for devices with SAI2)
  740. @endif
  741. * @arg @ref RCC_PERIPHCLK_SDMMC1 SDMMC1 peripheral clock
  742. @if STM32L443xx
  743. * @arg @ref RCC_PERIPHCLK_SWPMI1 SWPMI1 peripheral clock (only for devices with SWPMI1)
  744. @endif
  745. @if STM32L486xx
  746. * @arg @ref RCC_PERIPHCLK_SWPMI1 SWPMI1 peripheral clock (only for devices with SWPMI1)
  747. @endif
  748. @if STM32L4A6xx
  749. * @arg @ref RCC_PERIPHCLK_SWPMI1 SWPMI1 peripheral clock (only for devices with SWPMI1)
  750. @endif
  751. * @arg @ref RCC_PERIPHCLK_USART1 USART1 peripheral clock
  752. * @arg @ref RCC_PERIPHCLK_USART2 USART1 peripheral clock
  753. * @arg @ref RCC_PERIPHCLK_USART3 USART1 peripheral clock
  754. @if STM32L462xx
  755. * @arg @ref RCC_PERIPHCLK_UART4 UART4 peripheral clock (only for devices with UART4)
  756. @endif
  757. @if STM32L486xx
  758. * @arg @ref RCC_PERIPHCLK_UART4 UART4 peripheral clock (only for devices with UART4)
  759. * @arg @ref RCC_PERIPHCLK_UART5 UART5 peripheral clock (only for devices with UART5)
  760. @endif
  761. @if STM32L4A6xx
  762. * @arg @ref RCC_PERIPHCLK_UART4 UART4 peripheral clock (only for devices with UART4)
  763. * @arg @ref RCC_PERIPHCLK_UART5 UART5 peripheral clock (only for devices with UART5)
  764. @endif
  765. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock (only for devices with USB)
  766. * @retval Frequency in Hz
  767. */
  768. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  769. {
  770. uint32_t frequency = 0U;
  771. uint32_t srcclk = 0U;
  772. uint32_t pllvco = 0U, plln = 0U, pllp = 0U;
  773. /* Check the parameters */
  774. assert_param(IS_RCC_PERIPHCLOCK(PeriphClk));
  775. if(PeriphClk == RCC_PERIPHCLK_RTC)
  776. {
  777. /* Get the current RTC source */
  778. srcclk = __HAL_RCC_GET_RTC_SOURCE();
  779. /* Check if LSE is ready and if RTC clock selection is LSE */
  780. if ((srcclk == RCC_RTCCLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  781. {
  782. frequency = LSE_VALUE;
  783. }
  784. /* Check if LSI is ready and if RTC clock selection is LSI */
  785. else if ((srcclk == RCC_RTCCLKSOURCE_LSI) && (HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY)))
  786. {
  787. frequency = LSI_VALUE;
  788. }
  789. /* Check if HSE is ready and if RTC clock selection is HSI_DIV32*/
  790. else if ((srcclk == RCC_RTCCLKSOURCE_HSE_DIV32) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY)))
  791. {
  792. frequency = HSE_VALUE / 32;
  793. }
  794. /* Clock not enabled for RTC*/
  795. else
  796. {
  797. frequency = 0U;
  798. }
  799. }
  800. else
  801. {
  802. /* Other external peripheral clock source than RTC */
  803. /* Compute PLL clock input */
  804. if(__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_MSI) /* MSI ? */
  805. {
  806. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_MSIRDY))
  807. {
  808. /*MSI frequency range in HZ*/
  809. pllvco = MSIRangeTable[(__HAL_RCC_GET_MSI_RANGE() >> 4U)];
  810. }
  811. else
  812. {
  813. pllvco = 0U;
  814. }
  815. }
  816. else if(__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI) /* HSI ? */
  817. {
  818. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  819. {
  820. pllvco = HSI_VALUE;
  821. }
  822. else
  823. {
  824. pllvco = 0U;
  825. }
  826. }
  827. else if(__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE) /* HSE ? */
  828. {
  829. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY))
  830. {
  831. pllvco = HSE_VALUE;
  832. }
  833. else
  834. {
  835. pllvco = 0U;
  836. }
  837. }
  838. else /* No source */
  839. {
  840. pllvco = 0U;
  841. }
  842. /* f(PLL Source) / PLLM */
  843. pllvco = (pllvco / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  844. switch(PeriphClk)
  845. {
  846. #if defined(SAI2)
  847. case RCC_PERIPHCLK_SAI1:
  848. case RCC_PERIPHCLK_SAI2:
  849. if(PeriphClk == RCC_PERIPHCLK_SAI1)
  850. {
  851. srcclk = __HAL_RCC_GET_SAI1_SOURCE();
  852. if(srcclk == RCC_SAI1CLKSOURCE_PIN)
  853. {
  854. frequency = EXTERNAL_SAI1_CLOCK_VALUE;
  855. }
  856. /* Else, PLL clock output to check below */
  857. }
  858. else /* RCC_PERIPHCLK_SAI2 */
  859. {
  860. srcclk = __HAL_RCC_GET_SAI2_SOURCE();
  861. if(srcclk == RCC_SAI2CLKSOURCE_PIN)
  862. {
  863. frequency = EXTERNAL_SAI2_CLOCK_VALUE;
  864. }
  865. /* Else, PLL clock output to check below */
  866. }
  867. #else
  868. case RCC_PERIPHCLK_SAI1:
  869. if(PeriphClk == RCC_PERIPHCLK_SAI1)
  870. {
  871. srcclk = READ_BIT(RCC->CCIPR, RCC_CCIPR_SAI1SEL);
  872. if(srcclk == RCC_SAI1CLKSOURCE_PIN)
  873. {
  874. frequency = EXTERNAL_SAI1_CLOCK_VALUE;
  875. }
  876. /* Else, PLL clock output to check below */
  877. }
  878. #endif /* SAI2 */
  879. if(frequency == 0U)
  880. {
  881. #if defined(SAI2)
  882. if((srcclk == RCC_SAI1CLKSOURCE_PLL) || (srcclk == RCC_SAI2CLKSOURCE_PLL))
  883. {
  884. if(__HAL_RCC_GET_PLLCLKOUT_CONFIG(RCC_PLL_SAI3CLK) != RESET)
  885. {
  886. /* f(PLLSAI3CLK) = f(VCO input) * PLLN / PLLP */
  887. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  888. #if defined(RCC_PLLP_DIV_2_31_SUPPORT)
  889. pllp = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLPDIV) >> RCC_PLLCFGR_PLLPDIV_Pos;
  890. #endif
  891. if(pllp == 0U)
  892. {
  893. if(READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLP) != RESET)
  894. {
  895. pllp = 17U;
  896. }
  897. else
  898. {
  899. pllp = 7U;
  900. }
  901. }
  902. frequency = (pllvco * plln) / pllp;
  903. }
  904. }
  905. else if(srcclk == 0U) /* RCC_SAI1CLKSOURCE_PLLSAI1 || RCC_SAI2CLKSOURCE_PLLSAI1 */
  906. {
  907. if(__HAL_RCC_GET_PLLSAI1CLKOUT_CONFIG(RCC_PLLSAI1_SAI1CLK) != RESET)
  908. {
  909. /* f(PLLSAI1CLK) = f(VCOSAI1 input) * PLLSAI1N / PLLSAI1P */
  910. plln = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N) >> RCC_PLLSAI1CFGR_PLLSAI1N_Pos;
  911. #if defined(RCC_PLLSAI1P_DIV_2_31_SUPPORT)
  912. pllp = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1PDIV) >> RCC_PLLSAI1CFGR_PLLSAI1PDIV_Pos;
  913. #endif
  914. if(pllp == 0U)
  915. {
  916. if(READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1P) != RESET)
  917. {
  918. pllp = 17U;
  919. }
  920. else
  921. {
  922. pllp = 7U;
  923. }
  924. }
  925. frequency = (pllvco * plln) / pllp;
  926. }
  927. }
  928. #else
  929. if(srcclk == RCC_SAI1CLKSOURCE_PLL)
  930. {
  931. if(__HAL_RCC_GET_PLLCLKOUT_CONFIG(RCC_PLL_SAI2CLK) != RESET)
  932. {
  933. /* f(PLLSAI2CLK) = f(VCO input) * PLLN / PLLP */
  934. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  935. #if defined(RCC_PLLP_DIV_2_31_SUPPORT)
  936. pllp = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLPDIV) >> RCC_PLLCFGR_PLLPDIV_Pos;
  937. #endif
  938. if(pllp == 0U)
  939. {
  940. if(READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLP) != RESET)
  941. {
  942. pllp = 17U;
  943. }
  944. else
  945. {
  946. pllp = 7U;
  947. }
  948. }
  949. frequency = (pllvco * plln) / pllp;
  950. }
  951. else if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  952. {
  953. /* HSI automatically selected as clock source if PLLs not enabled */
  954. frequency = HSI_VALUE;
  955. }
  956. else
  957. {
  958. /* No clock source */
  959. frequency = 0U;
  960. }
  961. }
  962. else if(srcclk == RCC_SAI1CLKSOURCE_PLLSAI1)
  963. {
  964. if(__HAL_RCC_GET_PLLSAI1CLKOUT_CONFIG(RCC_PLLSAI1_SAI1CLK) != RESET)
  965. {
  966. /* f(PLLSAI1CLK) = f(VCOSAI1 input) * PLLSAI1N / PLLSAI1P */
  967. plln = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N) >> RCC_PLLSAI1CFGR_PLLSAI1N_Pos;
  968. #if defined(RCC_PLLSAI1P_DIV_2_31_SUPPORT)
  969. pllp = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1PDIV) >> RCC_PLLSAI1CFGR_PLLSAI1PDIV_Pos;
  970. #endif
  971. if(pllp == 0U)
  972. {
  973. if(READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1P) != RESET)
  974. {
  975. pllp = 17U;
  976. }
  977. else
  978. {
  979. pllp = 7U;
  980. }
  981. }
  982. frequency = (pllvco * plln) / pllp;
  983. }
  984. else if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  985. {
  986. /* HSI automatically selected as clock source if PLLs not enabled */
  987. frequency = HSI_VALUE;
  988. }
  989. else
  990. {
  991. /* No clock source */
  992. frequency = 0U;
  993. }
  994. }
  995. #endif /* SAI2 */
  996. #if defined(RCC_PLLSAI2_SUPPORT)
  997. else if((srcclk == RCC_SAI1CLKSOURCE_PLLSAI2) || (srcclk == RCC_SAI2CLKSOURCE_PLLSAI2))
  998. {
  999. if(__HAL_RCC_GET_PLLSAI2CLKOUT_CONFIG(RCC_PLLSAI2_SAI2CLK) != RESET)
  1000. {
  1001. /* f(PLLSAI2CLK) = f(VCOSAI2 input) * PLLSAI2N / PLLSAI2P */
  1002. plln = READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2N) >> RCC_PLLSAI2CFGR_PLLSAI2N_Pos;
  1003. if(pllp == 0U)
  1004. {
  1005. if(READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2P) != RESET)
  1006. {
  1007. pllp = 17U;
  1008. }
  1009. else
  1010. {
  1011. pllp = 7U;
  1012. }
  1013. }
  1014. frequency = (pllvco * plln) / pllp;
  1015. }
  1016. }
  1017. #endif /* RCC_PLLSAI2_SUPPORT */
  1018. else
  1019. {
  1020. /* No clock source */
  1021. frequency = 0U;
  1022. }
  1023. }
  1024. break;
  1025. #if defined(USB_OTG_FS) || defined(USB)
  1026. case RCC_PERIPHCLK_USB:
  1027. #endif /* USB_OTG_FS || USB */
  1028. case RCC_PERIPHCLK_RNG:
  1029. #if defined(SDMMC1)
  1030. case RCC_PERIPHCLK_SDMMC1:
  1031. #endif /* SDMMC1 */
  1032. srcclk = READ_BIT(RCC->CCIPR, RCC_CCIPR_CLK48SEL);
  1033. if(srcclk == RCC_CCIPR_CLK48SEL) /* MSI ? */
  1034. {
  1035. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_MSIRDY))
  1036. {
  1037. /*MSI frequency range in HZ*/
  1038. frequency = MSIRangeTable[(__HAL_RCC_GET_MSI_RANGE() >> 4U)];
  1039. }
  1040. else
  1041. {
  1042. frequency = 0U;
  1043. }
  1044. }
  1045. else if(srcclk == RCC_CCIPR_CLK48SEL_1) /* PLL ? */
  1046. {
  1047. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLRDY) && HAL_IS_BIT_SET(RCC->PLLCFGR, RCC_PLLCFGR_PLLQEN))
  1048. {
  1049. /* f(PLL48M1CLK) = f(VCO input) * PLLN / PLLQ */
  1050. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  1051. frequency = (pllvco * plln) / (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U) << 1U);
  1052. }
  1053. else
  1054. {
  1055. frequency = 0U;
  1056. }
  1057. }
  1058. else if(srcclk == RCC_CCIPR_CLK48SEL_0) /* PLLSAI1 ? */
  1059. {
  1060. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLSAI1RDY) && HAL_IS_BIT_SET(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1QEN))
  1061. {
  1062. /* f(PLL48M2CLK) = f(VCOSAI1 input) * PLLSAI1N / PLLSAI1Q */
  1063. plln = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N) >> RCC_PLLSAI1CFGR_PLLSAI1N_Pos;
  1064. frequency = (pllvco * plln) / (((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1Q) >> RCC_PLLSAI1CFGR_PLLSAI1Q_Pos) + 1U) << 1U);
  1065. }
  1066. else
  1067. {
  1068. frequency = 0U;
  1069. }
  1070. }
  1071. #if defined(RCC_HSI48_SUPPORT)
  1072. else if((srcclk == 0U) && (HAL_IS_BIT_SET(RCC->CRRCR, RCC_CRRCR_HSI48RDY))) /* HSI48 ? */
  1073. {
  1074. frequency = HSI48_VALUE;
  1075. }
  1076. else /* No clock source */
  1077. {
  1078. frequency = 0U;
  1079. }
  1080. #else
  1081. else /* No clock source */
  1082. {
  1083. frequency = 0U;
  1084. }
  1085. #endif /* RCC_HSI48_SUPPORT */
  1086. break;
  1087. case RCC_PERIPHCLK_USART1:
  1088. /* Get the current USART1 source */
  1089. srcclk = __HAL_RCC_GET_USART1_SOURCE();
  1090. if(srcclk == RCC_USART1CLKSOURCE_PCLK2)
  1091. {
  1092. frequency = HAL_RCC_GetPCLK2Freq();
  1093. }
  1094. else if(srcclk == RCC_USART1CLKSOURCE_SYSCLK)
  1095. {
  1096. frequency = HAL_RCC_GetSysClockFreq();
  1097. }
  1098. else if((srcclk == RCC_USART1CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1099. {
  1100. frequency = HSI_VALUE;
  1101. }
  1102. else if((srcclk == RCC_USART1CLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  1103. {
  1104. frequency = LSE_VALUE;
  1105. }
  1106. /* Clock not enabled for USART1 */
  1107. else
  1108. {
  1109. frequency = 0U;
  1110. }
  1111. break;
  1112. case RCC_PERIPHCLK_USART2:
  1113. /* Get the current USART2 source */
  1114. srcclk = __HAL_RCC_GET_USART2_SOURCE();
  1115. if(srcclk == RCC_USART2CLKSOURCE_PCLK1)
  1116. {
  1117. frequency = HAL_RCC_GetPCLK1Freq();
  1118. }
  1119. else if(srcclk == RCC_USART2CLKSOURCE_SYSCLK)
  1120. {
  1121. frequency = HAL_RCC_GetSysClockFreq();
  1122. }
  1123. else if((srcclk == RCC_USART2CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1124. {
  1125. frequency = HSI_VALUE;
  1126. }
  1127. else if((srcclk == RCC_USART2CLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  1128. {
  1129. frequency = LSE_VALUE;
  1130. }
  1131. /* Clock not enabled for USART2 */
  1132. else
  1133. {
  1134. frequency = 0U;
  1135. }
  1136. break;
  1137. #if defined(USART3)
  1138. case RCC_PERIPHCLK_USART3:
  1139. /* Get the current USART3 source */
  1140. srcclk = __HAL_RCC_GET_USART3_SOURCE();
  1141. if(srcclk == RCC_USART3CLKSOURCE_PCLK1)
  1142. {
  1143. frequency = HAL_RCC_GetPCLK1Freq();
  1144. }
  1145. else if(srcclk == RCC_USART3CLKSOURCE_SYSCLK)
  1146. {
  1147. frequency = HAL_RCC_GetSysClockFreq();
  1148. }
  1149. else if((srcclk == RCC_USART3CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1150. {
  1151. frequency = HSI_VALUE;
  1152. }
  1153. else if((srcclk == RCC_USART3CLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  1154. {
  1155. frequency = LSE_VALUE;
  1156. }
  1157. /* Clock not enabled for USART3 */
  1158. else
  1159. {
  1160. frequency = 0U;
  1161. }
  1162. break;
  1163. #endif /* USART3 */
  1164. #if defined(UART4)
  1165. case RCC_PERIPHCLK_UART4:
  1166. /* Get the current UART4 source */
  1167. srcclk = __HAL_RCC_GET_UART4_SOURCE();
  1168. if(srcclk == RCC_UART4CLKSOURCE_PCLK1)
  1169. {
  1170. frequency = HAL_RCC_GetPCLK1Freq();
  1171. }
  1172. else if(srcclk == RCC_UART4CLKSOURCE_SYSCLK)
  1173. {
  1174. frequency = HAL_RCC_GetSysClockFreq();
  1175. }
  1176. else if((srcclk == RCC_UART4CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1177. {
  1178. frequency = HSI_VALUE;
  1179. }
  1180. else if((srcclk == RCC_UART4CLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  1181. {
  1182. frequency = LSE_VALUE;
  1183. }
  1184. /* Clock not enabled for UART4 */
  1185. else
  1186. {
  1187. frequency = 0U;
  1188. }
  1189. break;
  1190. #endif /* UART4 */
  1191. #if defined(UART5)
  1192. case RCC_PERIPHCLK_UART5:
  1193. /* Get the current UART5 source */
  1194. srcclk = __HAL_RCC_GET_UART5_SOURCE();
  1195. if(srcclk == RCC_UART5CLKSOURCE_PCLK1)
  1196. {
  1197. frequency = HAL_RCC_GetPCLK1Freq();
  1198. }
  1199. else if(srcclk == RCC_UART5CLKSOURCE_SYSCLK)
  1200. {
  1201. frequency = HAL_RCC_GetSysClockFreq();
  1202. }
  1203. else if((srcclk == RCC_UART5CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1204. {
  1205. frequency = HSI_VALUE;
  1206. }
  1207. else if((srcclk == RCC_UART5CLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  1208. {
  1209. frequency = LSE_VALUE;
  1210. }
  1211. /* Clock not enabled for UART5 */
  1212. else
  1213. {
  1214. frequency = 0U;
  1215. }
  1216. break;
  1217. #endif /* UART5 */
  1218. case RCC_PERIPHCLK_LPUART1:
  1219. /* Get the current LPUART1 source */
  1220. srcclk = __HAL_RCC_GET_LPUART1_SOURCE();
  1221. if(srcclk == RCC_LPUART1CLKSOURCE_PCLK1)
  1222. {
  1223. frequency = HAL_RCC_GetPCLK1Freq();
  1224. }
  1225. else if(srcclk == RCC_LPUART1CLKSOURCE_SYSCLK)
  1226. {
  1227. frequency = HAL_RCC_GetSysClockFreq();
  1228. }
  1229. else if((srcclk == RCC_LPUART1CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1230. {
  1231. frequency = HSI_VALUE;
  1232. }
  1233. else if((srcclk == RCC_LPUART1CLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  1234. {
  1235. frequency = LSE_VALUE;
  1236. }
  1237. /* Clock not enabled for LPUART1 */
  1238. else
  1239. {
  1240. frequency = 0U;
  1241. }
  1242. break;
  1243. case RCC_PERIPHCLK_ADC:
  1244. srcclk = __HAL_RCC_GET_ADC_SOURCE();
  1245. if(srcclk == RCC_ADCCLKSOURCE_SYSCLK)
  1246. {
  1247. frequency = HAL_RCC_GetSysClockFreq();
  1248. }
  1249. else if(srcclk == RCC_ADCCLKSOURCE_PLLSAI1)
  1250. {
  1251. if(__HAL_RCC_GET_PLLSAI1CLKOUT_CONFIG(RCC_PLLSAI1_ADC1CLK) != RESET)
  1252. {
  1253. /* f(PLLADC1CLK) = f(VCOSAI1 input) * PLLSAI1N / PLLSAI1R */
  1254. plln = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N) >> RCC_PLLSAI1CFGR_PLLSAI1N_Pos;
  1255. frequency = (pllvco * plln) / (((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1R) >> RCC_PLLSAI1CFGR_PLLSAI1R_Pos) + 1U) << 1U);
  1256. }
  1257. }
  1258. #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L496xx) || defined(STM32L4A6xx)
  1259. else if(srcclk == RCC_ADCCLKSOURCE_PLLSAI2)
  1260. {
  1261. if(__HAL_RCC_GET_PLLSAI2CLKOUT_CONFIG(RCC_PLLSAI2_ADC2CLK) != RESET)
  1262. {
  1263. /* f(PLLADC2CLK) = f(VCOSAI2 input) * PLLSAI2N / PLLSAI2R */
  1264. plln = READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2N) >> RCC_PLLSAI2CFGR_PLLSAI2N_Pos;
  1265. frequency = (pllvco * plln) / (((READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2R) >> RCC_PLLSAI2CFGR_PLLSAI2R_Pos) + 1U) << 1U);
  1266. }
  1267. }
  1268. #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || STM32L496xx || STM32L4A6xx */
  1269. /* Clock not enabled for ADC */
  1270. else
  1271. {
  1272. frequency = 0U;
  1273. }
  1274. break;
  1275. #if defined(DFSDM1_Filter0)
  1276. case RCC_PERIPHCLK_DFSDM1:
  1277. /* Get the current DFSDM1 source */
  1278. srcclk = __HAL_RCC_GET_DFSDM1_SOURCE();
  1279. if(srcclk == RCC_DFSDM1CLKSOURCE_PCLK2)
  1280. {
  1281. frequency = HAL_RCC_GetPCLK2Freq();
  1282. }
  1283. else
  1284. {
  1285. frequency = HAL_RCC_GetSysClockFreq();
  1286. }
  1287. break;
  1288. #endif /* DFSDM1_Filter0 */
  1289. case RCC_PERIPHCLK_I2C1:
  1290. /* Get the current I2C1 source */
  1291. srcclk = __HAL_RCC_GET_I2C1_SOURCE();
  1292. if(srcclk == RCC_I2C1CLKSOURCE_PCLK1)
  1293. {
  1294. frequency = HAL_RCC_GetPCLK1Freq();
  1295. }
  1296. else if(srcclk == RCC_I2C1CLKSOURCE_SYSCLK)
  1297. {
  1298. frequency = HAL_RCC_GetSysClockFreq();
  1299. }
  1300. else if((srcclk == RCC_I2C1CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1301. {
  1302. frequency = HSI_VALUE;
  1303. }
  1304. /* Clock not enabled for I2C1 */
  1305. else
  1306. {
  1307. frequency = 0U;
  1308. }
  1309. break;
  1310. #if defined(I2C2)
  1311. case RCC_PERIPHCLK_I2C2:
  1312. /* Get the current I2C2 source */
  1313. srcclk = __HAL_RCC_GET_I2C2_SOURCE();
  1314. if(srcclk == RCC_I2C2CLKSOURCE_PCLK1)
  1315. {
  1316. frequency = HAL_RCC_GetPCLK1Freq();
  1317. }
  1318. else if(srcclk == RCC_I2C2CLKSOURCE_SYSCLK)
  1319. {
  1320. frequency = HAL_RCC_GetSysClockFreq();
  1321. }
  1322. else if((srcclk == RCC_I2C2CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1323. {
  1324. frequency = HSI_VALUE;
  1325. }
  1326. /* Clock not enabled for I2C2 */
  1327. else
  1328. {
  1329. frequency = 0U;
  1330. }
  1331. break;
  1332. #endif /* I2C2 */
  1333. case RCC_PERIPHCLK_I2C3:
  1334. /* Get the current I2C3 source */
  1335. srcclk = __HAL_RCC_GET_I2C3_SOURCE();
  1336. if(srcclk == RCC_I2C3CLKSOURCE_PCLK1)
  1337. {
  1338. frequency = HAL_RCC_GetPCLK1Freq();
  1339. }
  1340. else if(srcclk == RCC_I2C3CLKSOURCE_SYSCLK)
  1341. {
  1342. frequency = HAL_RCC_GetSysClockFreq();
  1343. }
  1344. else if((srcclk == RCC_I2C3CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1345. {
  1346. frequency = HSI_VALUE;
  1347. }
  1348. /* Clock not enabled for I2C3 */
  1349. else
  1350. {
  1351. frequency = 0U;
  1352. }
  1353. break;
  1354. #if defined(I2C4)
  1355. case RCC_PERIPHCLK_I2C4:
  1356. /* Get the current I2C4 source */
  1357. srcclk = __HAL_RCC_GET_I2C4_SOURCE();
  1358. if(srcclk == RCC_I2C4CLKSOURCE_PCLK1)
  1359. {
  1360. frequency = HAL_RCC_GetPCLK1Freq();
  1361. }
  1362. else if(srcclk == RCC_I2C4CLKSOURCE_SYSCLK)
  1363. {
  1364. frequency = HAL_RCC_GetSysClockFreq();
  1365. }
  1366. else if((srcclk == RCC_I2C4CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1367. {
  1368. frequency = HSI_VALUE;
  1369. }
  1370. /* Clock not enabled for I2C4 */
  1371. else
  1372. {
  1373. frequency = 0U;
  1374. }
  1375. break;
  1376. #endif /* I2C4 */
  1377. case RCC_PERIPHCLK_LPTIM1:
  1378. /* Get the current LPTIM1 source */
  1379. srcclk = __HAL_RCC_GET_LPTIM1_SOURCE();
  1380. if(srcclk == RCC_LPTIM1CLKSOURCE_PCLK1)
  1381. {
  1382. frequency = HAL_RCC_GetPCLK1Freq();
  1383. }
  1384. else if((srcclk == RCC_LPTIM1CLKSOURCE_LSI) && (HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY)))
  1385. {
  1386. frequency = LSI_VALUE;
  1387. }
  1388. else if((srcclk == RCC_LPTIM1CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1389. {
  1390. frequency = HSI_VALUE;
  1391. }
  1392. else if ((srcclk == RCC_LPTIM1CLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  1393. {
  1394. frequency = LSE_VALUE;
  1395. }
  1396. /* Clock not enabled for LPTIM1 */
  1397. else
  1398. {
  1399. frequency = 0U;
  1400. }
  1401. break;
  1402. case RCC_PERIPHCLK_LPTIM2:
  1403. /* Get the current LPTIM2 source */
  1404. srcclk = __HAL_RCC_GET_LPTIM2_SOURCE();
  1405. if(srcclk == RCC_LPTIM2CLKSOURCE_PCLK1)
  1406. {
  1407. frequency = HAL_RCC_GetPCLK1Freq();
  1408. }
  1409. else if((srcclk == RCC_LPTIM2CLKSOURCE_LSI) && (HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY)))
  1410. {
  1411. frequency = LSI_VALUE;
  1412. }
  1413. else if((srcclk == RCC_LPTIM2CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1414. {
  1415. frequency = HSI_VALUE;
  1416. }
  1417. else if ((srcclk == RCC_LPTIM2CLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  1418. {
  1419. frequency = LSE_VALUE;
  1420. }
  1421. /* Clock not enabled for LPTIM2 */
  1422. else
  1423. {
  1424. frequency = 0U;
  1425. }
  1426. break;
  1427. #if defined(SWPMI1)
  1428. case RCC_PERIPHCLK_SWPMI1:
  1429. /* Get the current SWPMI1 source */
  1430. srcclk = __HAL_RCC_GET_SWPMI1_SOURCE();
  1431. if(srcclk == RCC_SWPMI1CLKSOURCE_PCLK1)
  1432. {
  1433. frequency = HAL_RCC_GetPCLK1Freq();
  1434. }
  1435. else if((srcclk == RCC_SWPMI1CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  1436. {
  1437. frequency = HSI_VALUE;
  1438. }
  1439. /* Clock not enabled for SWPMI1 */
  1440. else
  1441. {
  1442. frequency = 0U;
  1443. }
  1444. break;
  1445. #endif /* SWPMI1 */
  1446. default:
  1447. break;
  1448. }
  1449. }
  1450. return(frequency);
  1451. }
  1452. /**
  1453. * @}
  1454. */
  1455. /** @defgroup RCCEx_Exported_Functions_Group2 Extended Clock management functions
  1456. * @brief Extended Clock management functions
  1457. *
  1458. @verbatim
  1459. ===============================================================================
  1460. ##### Extended clock management functions #####
  1461. ===============================================================================
  1462. [..]
  1463. This subsection provides a set of functions allowing to control the
  1464. activation or deactivation of MSI PLL-mode, PLLSAI1, PLLSAI2, LSE CSS,
  1465. Low speed clock output and clock after wake-up from STOP mode.
  1466. @endverbatim
  1467. * @{
  1468. */
  1469. /**
  1470. * @brief Enable PLLSAI1.
  1471. * @param PLLSAI1Init pointer to an RCC_PLLSAI1InitTypeDef structure that
  1472. * contains the configuration information for the PLLSAI1
  1473. * @retval HAL status
  1474. */
  1475. HAL_StatusTypeDef HAL_RCCEx_EnablePLLSAI1(RCC_PLLSAI1InitTypeDef *PLLSAI1Init)
  1476. {
  1477. uint32_t tickstart = 0U;
  1478. HAL_StatusTypeDef status = HAL_OK;
  1479. /* check for PLLSAI1 Parameters used to output PLLSAI1CLK */
  1480. assert_param(IS_RCC_PLLSAI1SOURCE(PLLSAI1Init->PLLSAI1Source));
  1481. assert_param(IS_RCC_PLLSAI1M_VALUE(PLLSAI1Init->PLLSAI1M));
  1482. assert_param(IS_RCC_PLLSAI1N_VALUE(PLLSAI1Init->PLLSAI1N));
  1483. assert_param(IS_RCC_PLLSAI1P_VALUE(PLLSAI1Init->PLLSAI1P));
  1484. assert_param(IS_RCC_PLLSAI1Q_VALUE(PLLSAI1Init->PLLSAI1Q));
  1485. assert_param(IS_RCC_PLLSAI1R_VALUE(PLLSAI1Init->PLLSAI1R));
  1486. assert_param(IS_RCC_PLLSAI1CLOCKOUT_VALUE(PLLSAI1Init->PLLSAI1ClockOut));
  1487. /* Disable the PLLSAI1 */
  1488. __HAL_RCC_PLLSAI1_DISABLE();
  1489. /* Get Start Tick*/
  1490. tickstart = HAL_GetTick();
  1491. /* Wait till PLLSAI1 is ready to be updated */
  1492. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) != RESET)
  1493. {
  1494. if((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1495. {
  1496. status = HAL_TIMEOUT;
  1497. break;
  1498. }
  1499. }
  1500. if(status == HAL_OK)
  1501. {
  1502. /* Configure the PLLSAI1 Multiplication factor N */
  1503. /* Configure the PLLSAI1 Division factors P, Q and R */
  1504. __HAL_RCC_PLLSAI1_CONFIG(PLLSAI1Init->PLLSAI1N, PLLSAI1Init->PLLSAI1P, PLLSAI1Init->PLLSAI1Q, PLLSAI1Init->PLLSAI1R);
  1505. /* Configure the PLLSAI1 Clock output(s) */
  1506. __HAL_RCC_PLLSAI1CLKOUT_ENABLE(PLLSAI1Init->PLLSAI1ClockOut);
  1507. /* Enable the PLLSAI1 again by setting PLLSAI1ON to 1*/
  1508. __HAL_RCC_PLLSAI1_ENABLE();
  1509. /* Get Start Tick*/
  1510. tickstart = HAL_GetTick();
  1511. /* Wait till PLLSAI1 is ready */
  1512. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) == RESET)
  1513. {
  1514. if((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1515. {
  1516. status = HAL_TIMEOUT;
  1517. break;
  1518. }
  1519. }
  1520. }
  1521. return status;
  1522. }
  1523. /**
  1524. * @brief Disable PLLSAI1.
  1525. * @retval HAL status
  1526. */
  1527. HAL_StatusTypeDef HAL_RCCEx_DisablePLLSAI1(void)
  1528. {
  1529. uint32_t tickstart = 0U;
  1530. HAL_StatusTypeDef status = HAL_OK;
  1531. /* Disable the PLLSAI1 */
  1532. __HAL_RCC_PLLSAI1_DISABLE();
  1533. /* Get Start Tick*/
  1534. tickstart = HAL_GetTick();
  1535. /* Wait till PLLSAI1 is ready */
  1536. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) != RESET)
  1537. {
  1538. if((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1539. {
  1540. status = HAL_TIMEOUT;
  1541. break;
  1542. }
  1543. }
  1544. /* Disable the PLLSAI1 Clock outputs */
  1545. __HAL_RCC_PLLSAI1CLKOUT_DISABLE(RCC_PLLSAI1CFGR_PLLSAI1PEN|RCC_PLLSAI1CFGR_PLLSAI1QEN|RCC_PLLSAI1CFGR_PLLSAI1REN);
  1546. /* Reset PLL source to save power if no PLLs on */
  1547. if((READ_BIT(RCC->CR, RCC_CR_PLLRDY) == RESET)
  1548. #if defined(RCC_PLLSAI2_SUPPORT)
  1549. &&
  1550. (READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) == RESET)
  1551. #endif /* RCC_PLLSAI2_SUPPORT */
  1552. )
  1553. {
  1554. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
  1555. }
  1556. return status;
  1557. }
  1558. #if defined(RCC_PLLSAI2_SUPPORT)
  1559. /**
  1560. * @brief Enable PLLSAI2.
  1561. * @param PLLSAI2Init pointer to an RCC_PLLSAI2InitTypeDef structure that
  1562. * contains the configuration information for the PLLSAI2
  1563. * @retval HAL status
  1564. */
  1565. HAL_StatusTypeDef HAL_RCCEx_EnablePLLSAI2(RCC_PLLSAI2InitTypeDef *PLLSAI2Init)
  1566. {
  1567. uint32_t tickstart = 0U;
  1568. HAL_StatusTypeDef status = HAL_OK;
  1569. /* check for PLLSAI2 Parameters used to output PLLSAI2CLK */
  1570. assert_param(IS_RCC_PLLSAI2SOURCE(PLLSAI2Init->PLLSAI2Source));
  1571. assert_param(IS_RCC_PLLSAI2M_VALUE(PLLSAI2Init->PLLSAI2M));
  1572. assert_param(IS_RCC_PLLSAI2N_VALUE(PLLSAI2Init->PLLSAI2N));
  1573. assert_param(IS_RCC_PLLSAI2P_VALUE(PLLSAI2Init->PLLSAI2P));
  1574. assert_param(IS_RCC_PLLSAI2R_VALUE(PLLSAI2Init->PLLSAI2R));
  1575. assert_param(IS_RCC_PLLSAI2CLOCKOUT_VALUE(PLLSAI2Init->PLLSAI2ClockOut));
  1576. /* Disable the PLLSAI2 */
  1577. __HAL_RCC_PLLSAI2_DISABLE();
  1578. /* Get Start Tick*/
  1579. tickstart = HAL_GetTick();
  1580. /* Wait till PLLSAI2 is ready to be updated */
  1581. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) != RESET)
  1582. {
  1583. if((HAL_GetTick() - tickstart) > PLLSAI2_TIMEOUT_VALUE)
  1584. {
  1585. status = HAL_TIMEOUT;
  1586. break;
  1587. }
  1588. }
  1589. if(status == HAL_OK)
  1590. {
  1591. /* Configure the PLLSAI2 Multiplication factor N */
  1592. /* Configure the PLLSAI2 Division factors P and R */
  1593. __HAL_RCC_PLLSAI2_CONFIG(PLLSAI2Init->PLLSAI2N, PLLSAI2Init->PLLSAI2P, PLLSAI2Init->PLLSAI2R);
  1594. /* Configure the PLLSAI2 Clock output(s) */
  1595. __HAL_RCC_PLLSAI2CLKOUT_ENABLE(PLLSAI2Init->PLLSAI2ClockOut);
  1596. /* Enable the PLLSAI2 again by setting PLLSAI2ON to 1*/
  1597. __HAL_RCC_PLLSAI2_ENABLE();
  1598. /* Get Start Tick*/
  1599. tickstart = HAL_GetTick();
  1600. /* Wait till PLLSAI2 is ready */
  1601. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) == RESET)
  1602. {
  1603. if((HAL_GetTick() - tickstart) > PLLSAI2_TIMEOUT_VALUE)
  1604. {
  1605. status = HAL_TIMEOUT;
  1606. break;
  1607. }
  1608. }
  1609. }
  1610. return status;
  1611. }
  1612. /**
  1613. * @brief Disable PLLISAI2.
  1614. * @retval HAL status
  1615. */
  1616. HAL_StatusTypeDef HAL_RCCEx_DisablePLLSAI2(void)
  1617. {
  1618. uint32_t tickstart = 0U;
  1619. HAL_StatusTypeDef status = HAL_OK;
  1620. /* Disable the PLLSAI2 */
  1621. __HAL_RCC_PLLSAI2_DISABLE();
  1622. /* Get Start Tick*/
  1623. tickstart = HAL_GetTick();
  1624. /* Wait till PLLSAI2 is ready */
  1625. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) != RESET)
  1626. {
  1627. if((HAL_GetTick() - tickstart) > PLLSAI2_TIMEOUT_VALUE)
  1628. {
  1629. status = HAL_TIMEOUT;
  1630. break;
  1631. }
  1632. }
  1633. /* Disable the PLLSAI2 Clock outputs */
  1634. __HAL_RCC_PLLSAI2CLKOUT_DISABLE(RCC_PLLSAI2CFGR_PLLSAI2PEN|RCC_PLLSAI2CFGR_PLLSAI2REN);
  1635. /* Reset PLL source to save power if no PLLs on */
  1636. if((READ_BIT(RCC->CR, RCC_CR_PLLRDY) == RESET)
  1637. &&
  1638. (READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) == RESET)
  1639. )
  1640. {
  1641. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
  1642. }
  1643. return status;
  1644. }
  1645. #endif /* RCC_PLLSAI2_SUPPORT */
  1646. /**
  1647. * @brief Configure the oscillator clock source for wakeup from Stop and CSS backup clock.
  1648. * @param WakeUpClk Wakeup clock
  1649. * This parameter can be one of the following values:
  1650. * @arg @ref RCC_STOP_WAKEUPCLOCK_MSI MSI oscillator selection
  1651. * @arg @ref RCC_STOP_WAKEUPCLOCK_HSI HSI oscillator selection
  1652. * @note This function shall not be called after the Clock Security System on HSE has been
  1653. * enabled.
  1654. * @retval None
  1655. */
  1656. void HAL_RCCEx_WakeUpStopCLKConfig(uint32_t WakeUpClk)
  1657. {
  1658. assert_param(IS_RCC_STOP_WAKEUPCLOCK(WakeUpClk));
  1659. __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(WakeUpClk);
  1660. }
  1661. /**
  1662. * @brief Configure the MSI range after standby mode.
  1663. * @note After Standby its frequency can be selected between 4 possible values (1, 2, 4 or 8 MHz).
  1664. * @param MSIRange MSI range
  1665. * This parameter can be one of the following values:
  1666. * @arg @ref RCC_MSIRANGE_4 Range 4 around 1 MHz
  1667. * @arg @ref RCC_MSIRANGE_5 Range 5 around 2 MHz
  1668. * @arg @ref RCC_MSIRANGE_6 Range 6 around 4 MHz (reset value)
  1669. * @arg @ref RCC_MSIRANGE_7 Range 7 around 8 MHz
  1670. * @retval None
  1671. */
  1672. void HAL_RCCEx_StandbyMSIRangeConfig(uint32_t MSIRange)
  1673. {
  1674. assert_param(IS_RCC_MSI_STANDBY_CLOCK_RANGE(MSIRange));
  1675. __HAL_RCC_MSI_STANDBY_RANGE_CONFIG(MSIRange);
  1676. }
  1677. /**
  1678. * @brief Enable the LSE Clock Security System.
  1679. * @note Prior to enable the LSE Clock Security System, LSE oscillator is to be enabled
  1680. * with HAL_RCC_OscConfig() and the LSE oscillator clock is to be selected as RTC
  1681. * clock with HAL_RCCEx_PeriphCLKConfig().
  1682. * @retval None
  1683. */
  1684. void HAL_RCCEx_EnableLSECSS(void)
  1685. {
  1686. SET_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  1687. }
  1688. /**
  1689. * @brief Disable the LSE Clock Security System.
  1690. * @note LSE Clock Security System can only be disabled after a LSE failure detection.
  1691. * @retval None
  1692. */
  1693. void HAL_RCCEx_DisableLSECSS(void)
  1694. {
  1695. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  1696. /* Disable LSE CSS IT if any */
  1697. __HAL_RCC_DISABLE_IT(RCC_IT_LSECSS);
  1698. }
  1699. /**
  1700. * @brief Enable the LSE Clock Security System Interrupt & corresponding EXTI line.
  1701. * @note LSE Clock Security System Interrupt is mapped on RTC EXTI line 19
  1702. * @retval None
  1703. */
  1704. void HAL_RCCEx_EnableLSECSS_IT(void)
  1705. {
  1706. /* Enable LSE CSS */
  1707. SET_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  1708. /* Enable LSE CSS IT */
  1709. __HAL_RCC_ENABLE_IT(RCC_IT_LSECSS);
  1710. /* Enable IT on EXTI Line 19 */
  1711. __HAL_RCC_LSECSS_EXTI_ENABLE_IT();
  1712. __HAL_RCC_LSECSS_EXTI_ENABLE_RISING_EDGE();
  1713. }
  1714. /**
  1715. * @brief Handle the RCC LSE Clock Security System interrupt request.
  1716. * @retval None
  1717. */
  1718. void HAL_RCCEx_LSECSS_IRQHandler(void)
  1719. {
  1720. /* Check RCC LSE CSSF flag */
  1721. if(__HAL_RCC_GET_IT(RCC_IT_LSECSS))
  1722. {
  1723. /* RCC LSE Clock Security System interrupt user callback */
  1724. HAL_RCCEx_LSECSS_Callback();
  1725. /* Clear RCC LSE CSS pending bit */
  1726. __HAL_RCC_CLEAR_IT(RCC_IT_LSECSS);
  1727. }
  1728. }
  1729. /**
  1730. * @brief RCCEx LSE Clock Security System interrupt callback.
  1731. * @retval none
  1732. */
  1733. __weak void HAL_RCCEx_LSECSS_Callback(void)
  1734. {
  1735. /* NOTE : This function should not be modified, when the callback is needed,
  1736. the @ref HAL_RCCEx_LSECSS_Callback should be implemented in the user file
  1737. */
  1738. }
  1739. /**
  1740. * @brief Select the Low Speed clock source to output on LSCO pin (PA2).
  1741. * @param LSCOSource specifies the Low Speed clock source to output.
  1742. * This parameter can be one of the following values:
  1743. * @arg @ref RCC_LSCOSOURCE_LSI LSI clock selected as LSCO source
  1744. * @arg @ref RCC_LSCOSOURCE_LSE LSE clock selected as LSCO source
  1745. * @retval None
  1746. */
  1747. void HAL_RCCEx_EnableLSCO(uint32_t LSCOSource)
  1748. {
  1749. GPIO_InitTypeDef GPIO_InitStruct;
  1750. FlagStatus pwrclkchanged = RESET;
  1751. FlagStatus backupchanged = RESET;
  1752. /* Check the parameters */
  1753. assert_param(IS_RCC_LSCOSOURCE(LSCOSource));
  1754. /* LSCO Pin Clock Enable */
  1755. __LSCO_CLK_ENABLE();
  1756. /* Configue the LSCO pin in analog mode */
  1757. GPIO_InitStruct.Pin = LSCO_PIN;
  1758. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  1759. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  1760. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1761. HAL_GPIO_Init(LSCO_GPIO_PORT, &GPIO_InitStruct);
  1762. /* Update LSCOSEL clock source in Backup Domain control register */
  1763. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  1764. {
  1765. __HAL_RCC_PWR_CLK_ENABLE();
  1766. pwrclkchanged = SET;
  1767. }
  1768. if(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  1769. {
  1770. HAL_PWR_EnableBkUpAccess();
  1771. backupchanged = SET;
  1772. }
  1773. MODIFY_REG(RCC->BDCR, RCC_BDCR_LSCOSEL | RCC_BDCR_LSCOEN, LSCOSource | RCC_BDCR_LSCOEN);
  1774. if(backupchanged == SET)
  1775. {
  1776. HAL_PWR_DisableBkUpAccess();
  1777. }
  1778. if(pwrclkchanged == SET)
  1779. {
  1780. __HAL_RCC_PWR_CLK_DISABLE();
  1781. }
  1782. }
  1783. /**
  1784. * @brief Disable the Low Speed clock output.
  1785. * @retval None
  1786. */
  1787. void HAL_RCCEx_DisableLSCO(void)
  1788. {
  1789. FlagStatus pwrclkchanged = RESET;
  1790. FlagStatus backupchanged = RESET;
  1791. /* Update LSCOEN bit in Backup Domain control register */
  1792. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  1793. {
  1794. __HAL_RCC_PWR_CLK_ENABLE();
  1795. pwrclkchanged = SET;
  1796. }
  1797. if(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  1798. {
  1799. /* Enable access to the backup domain */
  1800. HAL_PWR_EnableBkUpAccess();
  1801. backupchanged = SET;
  1802. }
  1803. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSCOEN);
  1804. /* Restore previous configuration */
  1805. if(backupchanged == SET)
  1806. {
  1807. /* Disable access to the backup domain */
  1808. HAL_PWR_DisableBkUpAccess();
  1809. }
  1810. if(pwrclkchanged == SET)
  1811. {
  1812. __HAL_RCC_PWR_CLK_DISABLE();
  1813. }
  1814. }
  1815. /**
  1816. * @brief Enable the PLL-mode of the MSI.
  1817. * @note Prior to enable the PLL-mode of the MSI for automatic hardware
  1818. * calibration LSE oscillator is to be enabled with HAL_RCC_OscConfig().
  1819. * @retval None
  1820. */
  1821. void HAL_RCCEx_EnableMSIPLLMode(void)
  1822. {
  1823. SET_BIT(RCC->CR, RCC_CR_MSIPLLEN) ;
  1824. }
  1825. /**
  1826. * @brief Disable the PLL-mode of the MSI.
  1827. * @note PLL-mode of the MSI is automatically reset when LSE oscillator is disabled.
  1828. * @retval None
  1829. */
  1830. void HAL_RCCEx_DisableMSIPLLMode(void)
  1831. {
  1832. CLEAR_BIT(RCC->CR, RCC_CR_MSIPLLEN) ;
  1833. }
  1834. /**
  1835. * @}
  1836. */
  1837. #if defined(CRS)
  1838. /** @defgroup RCCEx_Exported_Functions_Group3 Extended Clock Recovery System Control functions
  1839. * @brief Extended Clock Recovery System Control functions
  1840. *
  1841. @verbatim
  1842. ===============================================================================
  1843. ##### Extended Clock Recovery System Control functions #####
  1844. ===============================================================================
  1845. [..]
  1846. For devices with Clock Recovery System feature (CRS), RCC Extention HAL driver can be used as follows:
  1847. (#) In System clock config, HSI48 needs to be enabled
  1848. (#) Enable CRS clock in IP MSP init which will use CRS functions
  1849. (#) Call CRS functions as follows:
  1850. (##) Prepare synchronization configuration necessary for HSI48 calibration
  1851. (+++) Default values can be set for frequency Error Measurement (reload and error limit)
  1852. and also HSI48 oscillator smooth trimming.
  1853. (+++) Macro __HAL_RCC_CRS_RELOADVALUE_CALCULATE can be also used to calculate
  1854. directly reload value with target and sychronization frequencies values
  1855. (##) Call function HAL_RCCEx_CRSConfig which
  1856. (+++) Resets CRS registers to their default values.
  1857. (+++) Configures CRS registers with synchronization configuration
  1858. (+++) Enables automatic calibration and frequency error counter feature
  1859. Note: When using USB LPM (Link Power Management) and the device is in Sleep mode, the
  1860. periodic USB SOF will not be generated by the host. No SYNC signal will therefore be
  1861. provided to the CRS to calibrate the HSI48 on the run. To guarantee the required clock
  1862. precision after waking up from Sleep mode, the LSE or reference clock on the GPIOs
  1863. should be used as SYNC signal.
  1864. (##) A polling function is provided to wait for complete synchronization
  1865. (+++) Call function HAL_RCCEx_CRSWaitSynchronization()
  1866. (+++) According to CRS status, user can decide to adjust again the calibration or continue
  1867. application if synchronization is OK
  1868. (#) User can retrieve information related to synchronization in calling function
  1869. HAL_RCCEx_CRSGetSynchronizationInfo()
  1870. (#) Regarding synchronization status and synchronization information, user can try a new calibration
  1871. in changing synchronization configuration and call again HAL_RCCEx_CRSConfig.
  1872. Note: When the SYNC event is detected during the downcounting phase (before reaching the zero value),
  1873. it means that the actual frequency is lower than the target (and so, that the TRIM value should be
  1874. incremented), while when it is detected during the upcounting phase it means that the actual frequency
  1875. is higher (and that the TRIM value should be decremented).
  1876. (#) In interrupt mode, user can resort to the available macros (__HAL_RCC_CRS_XXX_IT). Interrupts will go
  1877. through CRS Handler (CRS_IRQn/CRS_IRQHandler)
  1878. (++) Call function HAL_RCCEx_CRSConfig()
  1879. (++) Enable CRS_IRQn (thanks to NVIC functions)
  1880. (++) Enable CRS interrupt (__HAL_RCC_CRS_ENABLE_IT)
  1881. (++) Implement CRS status management in the following user callbacks called from
  1882. HAL_RCCEx_CRS_IRQHandler():
  1883. (+++) HAL_RCCEx_CRS_SyncOkCallback()
  1884. (+++) HAL_RCCEx_CRS_SyncWarnCallback()
  1885. (+++) HAL_RCCEx_CRS_ExpectedSyncCallback()
  1886. (+++) HAL_RCCEx_CRS_ErrorCallback()
  1887. (#) To force a SYNC EVENT, user can use the function HAL_RCCEx_CRSSoftwareSynchronizationGenerate().
  1888. This function can be called before calling HAL_RCCEx_CRSConfig (for instance in Systick handler)
  1889. @endverbatim
  1890. * @{
  1891. */
  1892. /**
  1893. * @brief Start automatic synchronization for polling mode
  1894. * @param pInit Pointer on RCC_CRSInitTypeDef structure
  1895. * @retval None
  1896. */
  1897. void HAL_RCCEx_CRSConfig(RCC_CRSInitTypeDef *pInit)
  1898. {
  1899. uint32_t value = 0;
  1900. /* Check the parameters */
  1901. assert_param(IS_RCC_CRS_SYNC_DIV(pInit->Prescaler));
  1902. assert_param(IS_RCC_CRS_SYNC_SOURCE(pInit->Source));
  1903. assert_param(IS_RCC_CRS_SYNC_POLARITY(pInit->Polarity));
  1904. assert_param(IS_RCC_CRS_RELOADVALUE(pInit->ReloadValue));
  1905. assert_param(IS_RCC_CRS_ERRORLIMIT(pInit->ErrorLimitValue));
  1906. assert_param(IS_RCC_CRS_HSI48CALIBRATION(pInit->HSI48CalibrationValue));
  1907. /* CONFIGURATION */
  1908. /* Before configuration, reset CRS registers to their default values*/
  1909. __HAL_RCC_CRS_FORCE_RESET();
  1910. __HAL_RCC_CRS_RELEASE_RESET();
  1911. /* Set the SYNCDIV[2:0] bits according to Prescaler value */
  1912. /* Set the SYNCSRC[1:0] bits according to Source value */
  1913. /* Set the SYNCSPOL bit according to Polarity value */
  1914. value = (pInit->Prescaler | pInit->Source | pInit->Polarity);
  1915. /* Set the RELOAD[15:0] bits according to ReloadValue value */
  1916. value |= pInit->ReloadValue;
  1917. /* Set the FELIM[7:0] bits according to ErrorLimitValue value */
  1918. value |= (pInit->ErrorLimitValue << CRS_CFGR_FELIM_Pos);
  1919. WRITE_REG(CRS->CFGR, value);
  1920. /* Adjust HSI48 oscillator smooth trimming */
  1921. /* Set the TRIM[5:0] bits according to RCC_CRS_HSI48CalibrationValue value */
  1922. MODIFY_REG(CRS->CR, CRS_CR_TRIM, (pInit->HSI48CalibrationValue << CRS_CR_TRIM_Pos));
  1923. /* START AUTOMATIC SYNCHRONIZATION*/
  1924. /* Enable Automatic trimming & Frequency error counter */
  1925. SET_BIT(CRS->CR, CRS_CR_AUTOTRIMEN | CRS_CR_CEN);
  1926. }
  1927. /**
  1928. * @brief Generate the software synchronization event
  1929. * @retval None
  1930. */
  1931. void HAL_RCCEx_CRSSoftwareSynchronizationGenerate(void)
  1932. {
  1933. SET_BIT(CRS->CR, CRS_CR_SWSYNC);
  1934. }
  1935. /**
  1936. * @brief Return synchronization info
  1937. * @param pSynchroInfo Pointer on RCC_CRSSynchroInfoTypeDef structure
  1938. * @retval None
  1939. */
  1940. void HAL_RCCEx_CRSGetSynchronizationInfo(RCC_CRSSynchroInfoTypeDef *pSynchroInfo)
  1941. {
  1942. /* Check the parameter */
  1943. assert_param(pSynchroInfo != NULL);
  1944. /* Get the reload value */
  1945. pSynchroInfo->ReloadValue = (uint32_t)(READ_BIT(CRS->CFGR, CRS_CFGR_RELOAD));
  1946. /* Get HSI48 oscillator smooth trimming */
  1947. pSynchroInfo->HSI48CalibrationValue = (uint32_t)(READ_BIT(CRS->CR, CRS_CR_TRIM) >> CRS_CR_TRIM_Pos);
  1948. /* Get Frequency error capture */
  1949. pSynchroInfo->FreqErrorCapture = (uint32_t)(READ_BIT(CRS->ISR, CRS_ISR_FECAP) >> CRS_ISR_FECAP_Pos);
  1950. /* Get Frequency error direction */
  1951. pSynchroInfo->FreqErrorDirection = (uint32_t)(READ_BIT(CRS->ISR, CRS_ISR_FEDIR));
  1952. }
  1953. /**
  1954. * @brief Wait for CRS Synchronization status.
  1955. * @param Timeout Duration of the timeout
  1956. * @note Timeout is based on the maximum time to receive a SYNC event based on synchronization
  1957. * frequency.
  1958. * @note If Timeout set to HAL_MAX_DELAY, HAL_TIMEOUT will be never returned.
  1959. * @retval Combination of Synchronization status
  1960. * This parameter can be a combination of the following values:
  1961. * @arg @ref RCC_CRS_TIMEOUT
  1962. * @arg @ref RCC_CRS_SYNCOK
  1963. * @arg @ref RCC_CRS_SYNCWARN
  1964. * @arg @ref RCC_CRS_SYNCERR
  1965. * @arg @ref RCC_CRS_SYNCMISS
  1966. * @arg @ref RCC_CRS_TRIMOVF
  1967. */
  1968. uint32_t HAL_RCCEx_CRSWaitSynchronization(uint32_t Timeout)
  1969. {
  1970. uint32_t crsstatus = RCC_CRS_NONE;
  1971. uint32_t tickstart = 0U;
  1972. /* Get timeout */
  1973. tickstart = HAL_GetTick();
  1974. /* Wait for CRS flag or timeout detection */
  1975. do
  1976. {
  1977. if(Timeout != HAL_MAX_DELAY)
  1978. {
  1979. if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  1980. {
  1981. crsstatus = RCC_CRS_TIMEOUT;
  1982. }
  1983. }
  1984. /* Check CRS SYNCOK flag */
  1985. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCOK))
  1986. {
  1987. /* CRS SYNC event OK */
  1988. crsstatus |= RCC_CRS_SYNCOK;
  1989. /* Clear CRS SYNC event OK bit */
  1990. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCOK);
  1991. }
  1992. /* Check CRS SYNCWARN flag */
  1993. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCWARN))
  1994. {
  1995. /* CRS SYNC warning */
  1996. crsstatus |= RCC_CRS_SYNCWARN;
  1997. /* Clear CRS SYNCWARN bit */
  1998. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCWARN);
  1999. }
  2000. /* Check CRS TRIM overflow flag */
  2001. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_TRIMOVF))
  2002. {
  2003. /* CRS SYNC Error */
  2004. crsstatus |= RCC_CRS_TRIMOVF;
  2005. /* Clear CRS Error bit */
  2006. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_TRIMOVF);
  2007. }
  2008. /* Check CRS Error flag */
  2009. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCERR))
  2010. {
  2011. /* CRS SYNC Error */
  2012. crsstatus |= RCC_CRS_SYNCERR;
  2013. /* Clear CRS Error bit */
  2014. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCERR);
  2015. }
  2016. /* Check CRS SYNC Missed flag */
  2017. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCMISS))
  2018. {
  2019. /* CRS SYNC Missed */
  2020. crsstatus |= RCC_CRS_SYNCMISS;
  2021. /* Clear CRS SYNC Missed bit */
  2022. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCMISS);
  2023. }
  2024. /* Check CRS Expected SYNC flag */
  2025. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_ESYNC))
  2026. {
  2027. /* frequency error counter reached a zero value */
  2028. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_ESYNC);
  2029. }
  2030. } while(RCC_CRS_NONE == crsstatus);
  2031. return crsstatus;
  2032. }
  2033. /**
  2034. * @brief Handle the Clock Recovery System interrupt request.
  2035. * @retval None
  2036. */
  2037. void HAL_RCCEx_CRS_IRQHandler(void)
  2038. {
  2039. uint32_t crserror = RCC_CRS_NONE;
  2040. /* Get current IT flags and IT sources values */
  2041. uint32_t itflags = READ_REG(CRS->ISR);
  2042. uint32_t itsources = READ_REG(CRS->CR);
  2043. /* Check CRS SYNCOK flag */
  2044. if(((itflags & RCC_CRS_FLAG_SYNCOK) != RESET) && ((itsources & RCC_CRS_IT_SYNCOK) != RESET))
  2045. {
  2046. /* Clear CRS SYNC event OK flag */
  2047. WRITE_REG(CRS->ICR, CRS_ICR_SYNCOKC);
  2048. /* user callback */
  2049. HAL_RCCEx_CRS_SyncOkCallback();
  2050. }
  2051. /* Check CRS SYNCWARN flag */
  2052. else if(((itflags & RCC_CRS_FLAG_SYNCWARN) != RESET) && ((itsources & RCC_CRS_IT_SYNCWARN) != RESET))
  2053. {
  2054. /* Clear CRS SYNCWARN flag */
  2055. WRITE_REG(CRS->ICR, CRS_ICR_SYNCWARNC);
  2056. /* user callback */
  2057. HAL_RCCEx_CRS_SyncWarnCallback();
  2058. }
  2059. /* Check CRS Expected SYNC flag */
  2060. else if(((itflags & RCC_CRS_FLAG_ESYNC) != RESET) && ((itsources & RCC_CRS_IT_ESYNC) != RESET))
  2061. {
  2062. /* frequency error counter reached a zero value */
  2063. WRITE_REG(CRS->ICR, CRS_ICR_ESYNCC);
  2064. /* user callback */
  2065. HAL_RCCEx_CRS_ExpectedSyncCallback();
  2066. }
  2067. /* Check CRS Error flags */
  2068. else
  2069. {
  2070. if(((itflags & RCC_CRS_FLAG_ERR) != RESET) && ((itsources & RCC_CRS_IT_ERR) != RESET))
  2071. {
  2072. if((itflags & RCC_CRS_FLAG_SYNCERR) != RESET)
  2073. {
  2074. crserror |= RCC_CRS_SYNCERR;
  2075. }
  2076. if((itflags & RCC_CRS_FLAG_SYNCMISS) != RESET)
  2077. {
  2078. crserror |= RCC_CRS_SYNCMISS;
  2079. }
  2080. if((itflags & RCC_CRS_FLAG_TRIMOVF) != RESET)
  2081. {
  2082. crserror |= RCC_CRS_TRIMOVF;
  2083. }
  2084. /* Clear CRS Error flags */
  2085. WRITE_REG(CRS->ICR, CRS_ICR_ERRC);
  2086. /* user error callback */
  2087. HAL_RCCEx_CRS_ErrorCallback(crserror);
  2088. }
  2089. }
  2090. }
  2091. /**
  2092. * @brief RCCEx Clock Recovery System SYNCOK interrupt callback.
  2093. * @retval none
  2094. */
  2095. __weak void HAL_RCCEx_CRS_SyncOkCallback(void)
  2096. {
  2097. /* NOTE : This function should not be modified, when the callback is needed,
  2098. the @ref HAL_RCCEx_CRS_SyncOkCallback should be implemented in the user file
  2099. */
  2100. }
  2101. /**
  2102. * @brief RCCEx Clock Recovery System SYNCWARN interrupt callback.
  2103. * @retval none
  2104. */
  2105. __weak void HAL_RCCEx_CRS_SyncWarnCallback(void)
  2106. {
  2107. /* NOTE : This function should not be modified, when the callback is needed,
  2108. the @ref HAL_RCCEx_CRS_SyncWarnCallback should be implemented in the user file
  2109. */
  2110. }
  2111. /**
  2112. * @brief RCCEx Clock Recovery System Expected SYNC interrupt callback.
  2113. * @retval none
  2114. */
  2115. __weak void HAL_RCCEx_CRS_ExpectedSyncCallback(void)
  2116. {
  2117. /* NOTE : This function should not be modified, when the callback is needed,
  2118. the @ref HAL_RCCEx_CRS_ExpectedSyncCallback should be implemented in the user file
  2119. */
  2120. }
  2121. /**
  2122. * @brief RCCEx Clock Recovery System Error interrupt callback.
  2123. * @param Error Combination of Error status.
  2124. * This parameter can be a combination of the following values:
  2125. * @arg @ref RCC_CRS_SYNCERR
  2126. * @arg @ref RCC_CRS_SYNCMISS
  2127. * @arg @ref RCC_CRS_TRIMOVF
  2128. * @retval none
  2129. */
  2130. __weak void HAL_RCCEx_CRS_ErrorCallback(uint32_t Error)
  2131. {
  2132. /* Prevent unused argument(s) compilation warning */
  2133. UNUSED(Error);
  2134. /* NOTE : This function should not be modified, when the callback is needed,
  2135. the @ref HAL_RCCEx_CRS_ErrorCallback should be implemented in the user file
  2136. */
  2137. }
  2138. /**
  2139. * @}
  2140. */
  2141. #endif /* CRS */
  2142. /**
  2143. * @}
  2144. */
  2145. /** @addtogroup RCCEx_Private_Functions
  2146. * @{
  2147. */
  2148. /**
  2149. * @brief Configure the parameters N & P & optionally M of PLLSAI1 and enable PLLSAI1 output clock(s).
  2150. * @param PllSai1 pointer to an RCC_PLLSAI1InitTypeDef structure that
  2151. * contains the configuration parameters N & P & optionally M as well as PLLSAI1 output clock(s)
  2152. * @param Divider divider parameter to be updated
  2153. *
  2154. * @note PLLSAI1 is temporary disable to apply new parameters
  2155. *
  2156. * @retval HAL status
  2157. */
  2158. static HAL_StatusTypeDef RCCEx_PLLSAI1_Config(RCC_PLLSAI1InitTypeDef *PllSai1, uint32_t Divider)
  2159. {
  2160. uint32_t tickstart = 0U;
  2161. HAL_StatusTypeDef status = HAL_OK;
  2162. /* check for PLLSAI1 Parameters used to output PLLSAI1CLK */
  2163. /* P, Q and R dividers are verified in each specific divider case below */
  2164. assert_param(IS_RCC_PLLSAI1SOURCE(PllSai1->PLLSAI1Source));
  2165. assert_param(IS_RCC_PLLSAI1M_VALUE(PllSai1->PLLSAI1M));
  2166. assert_param(IS_RCC_PLLSAI1N_VALUE(PllSai1->PLLSAI1N));
  2167. assert_param(IS_RCC_PLLSAI1CLOCKOUT_VALUE(PllSai1->PLLSAI1ClockOut));
  2168. /* Check that PLLSAI1 clock source and divider M can be applied */
  2169. if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_NONE)
  2170. {
  2171. /* PLL clock source and divider M already set, check that no request for change */
  2172. if((__HAL_RCC_GET_PLL_OSCSOURCE() != PllSai1->PLLSAI1Source)
  2173. ||
  2174. (PllSai1->PLLSAI1Source == RCC_PLLSOURCE_NONE)
  2175. ||
  2176. (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U) != PllSai1->PLLSAI1M)
  2177. )
  2178. {
  2179. status = HAL_ERROR;
  2180. }
  2181. }
  2182. else
  2183. {
  2184. /* Check PLLSAI1 clock source availability */
  2185. switch(PllSai1->PLLSAI1Source)
  2186. {
  2187. case RCC_PLLSOURCE_MSI:
  2188. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_MSIRDY))
  2189. {
  2190. status = HAL_ERROR;
  2191. }
  2192. break;
  2193. case RCC_PLLSOURCE_HSI:
  2194. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_HSIRDY))
  2195. {
  2196. status = HAL_ERROR;
  2197. }
  2198. break;
  2199. case RCC_PLLSOURCE_HSE:
  2200. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_HSERDY) && HAL_IS_BIT_CLR(RCC->CR, RCC_CR_HSEBYP))
  2201. {
  2202. status = HAL_ERROR;
  2203. }
  2204. break;
  2205. default:
  2206. status = HAL_ERROR;
  2207. break;
  2208. }
  2209. if(status == HAL_OK)
  2210. {
  2211. /* Set PLLSAI1 clock source and divider M */
  2212. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLM, PllSai1->PLLSAI1Source | (PllSai1->PLLSAI1M - 1U) << RCC_PLLCFGR_PLLM_Pos);
  2213. }
  2214. }
  2215. if(status == HAL_OK)
  2216. {
  2217. /* Disable the PLLSAI1 */
  2218. __HAL_RCC_PLLSAI1_DISABLE();
  2219. /* Get Start Tick*/
  2220. tickstart = HAL_GetTick();
  2221. /* Wait till PLLSAI1 is ready to be updated */
  2222. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) != RESET)
  2223. {
  2224. if((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  2225. {
  2226. status = HAL_TIMEOUT;
  2227. break;
  2228. }
  2229. }
  2230. if(status == HAL_OK)
  2231. {
  2232. if(Divider == DIVIDER_P_UPDATE)
  2233. {
  2234. assert_param(IS_RCC_PLLSAI1P_VALUE(PllSai1->PLLSAI1P));
  2235. /* Configure the PLLSAI1 Division factor P and Multiplication factor N*/
  2236. #if defined(RCC_PLLSAI1P_DIV_2_31_SUPPORT)
  2237. MODIFY_REG(RCC->PLLSAI1CFGR,
  2238. RCC_PLLSAI1CFGR_PLLSAI1N | RCC_PLLSAI1CFGR_PLLSAI1PDIV,
  2239. (PllSai1->PLLSAI1N << RCC_PLLSAI1CFGR_PLLSAI1N_Pos) |
  2240. (PllSai1->PLLSAI1P << RCC_PLLSAI1CFGR_PLLSAI1PDIV_Pos));
  2241. #else
  2242. MODIFY_REG(RCC->PLLSAI1CFGR,
  2243. RCC_PLLSAI1CFGR_PLLSAI1N | RCC_PLLSAI1CFGR_PLLSAI1P,
  2244. (PllSai1->PLLSAI1N << RCC_PLLSAI1CFGR_PLLSAI1N_Pos) |
  2245. ((PllSai1->PLLSAI1P >> 4U) << RCC_PLLSAI1CFGR_PLLSAI1P_Pos));
  2246. #endif /* RCC_PLLSAI1P_DIV_2_31_SUPPORT */
  2247. }
  2248. else if(Divider == DIVIDER_Q_UPDATE)
  2249. {
  2250. assert_param(IS_RCC_PLLSAI1Q_VALUE(PllSai1->PLLSAI1Q));
  2251. /* Configure the PLLSAI1 Division factor Q and Multiplication factor N*/
  2252. MODIFY_REG(RCC->PLLSAI1CFGR,
  2253. RCC_PLLSAI1CFGR_PLLSAI1N | RCC_PLLSAI1CFGR_PLLSAI1Q,
  2254. (PllSai1->PLLSAI1N << RCC_PLLSAI1CFGR_PLLSAI1N_Pos) |
  2255. (((PllSai1->PLLSAI1Q >> 1U) - 1U) << RCC_PLLSAI1CFGR_PLLSAI1Q_Pos));
  2256. }
  2257. else
  2258. {
  2259. assert_param(IS_RCC_PLLSAI1R_VALUE(PllSai1->PLLSAI1R));
  2260. /* Configure the PLLSAI1 Division factor R and Multiplication factor N*/
  2261. MODIFY_REG(RCC->PLLSAI1CFGR,
  2262. RCC_PLLSAI1CFGR_PLLSAI1N | RCC_PLLSAI1CFGR_PLLSAI1R,
  2263. (PllSai1->PLLSAI1N << RCC_PLLSAI1CFGR_PLLSAI1N_Pos) |
  2264. (((PllSai1->PLLSAI1R >> 1U) - 1U) << RCC_PLLSAI1CFGR_PLLSAI1R_Pos));
  2265. }
  2266. /* Enable the PLLSAI1 again by setting PLLSAI1ON to 1*/
  2267. __HAL_RCC_PLLSAI1_ENABLE();
  2268. /* Get Start Tick*/
  2269. tickstart = HAL_GetTick();
  2270. /* Wait till PLLSAI1 is ready */
  2271. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) == RESET)
  2272. {
  2273. if((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  2274. {
  2275. status = HAL_TIMEOUT;
  2276. break;
  2277. }
  2278. }
  2279. if(status == HAL_OK)
  2280. {
  2281. /* Configure the PLLSAI1 Clock output(s) */
  2282. __HAL_RCC_PLLSAI1CLKOUT_ENABLE(PllSai1->PLLSAI1ClockOut);
  2283. }
  2284. }
  2285. }
  2286. return status;
  2287. }
  2288. #if defined(RCC_PLLSAI2_SUPPORT)
  2289. /**
  2290. * @brief Configure the parameters N & P & optionally M of PLLSAI2 and enable PLLSAI2 output clock(s).
  2291. * @param PllSai2 pointer to an RCC_PLLSAI2InitTypeDef structure that
  2292. * contains the configuration parameters N & P & optionally M as well as PLLSAI2 output clock(s)
  2293. * @param Divider divider parameter to be updated
  2294. *
  2295. * @note PLLSAI2 is temporary disable to apply new parameters
  2296. *
  2297. * @retval HAL status
  2298. */
  2299. static HAL_StatusTypeDef RCCEx_PLLSAI2_Config(RCC_PLLSAI2InitTypeDef *PllSai2, uint32_t Divider)
  2300. {
  2301. uint32_t tickstart = 0U;
  2302. HAL_StatusTypeDef status = HAL_OK;
  2303. /* check for PLLSAI2 Parameters used to output PLLSAI2CLK */
  2304. /* P, Q and R dividers are verified in each specific divider case below */
  2305. assert_param(IS_RCC_PLLSAI2SOURCE(PllSai2->PLLSAI2Source));
  2306. assert_param(IS_RCC_PLLSAI2M_VALUE(PllSai2->PLLSAI2M));
  2307. assert_param(IS_RCC_PLLSAI2N_VALUE(PllSai2->PLLSAI2N));
  2308. assert_param(IS_RCC_PLLSAI2CLOCKOUT_VALUE(PllSai2->PLLSAI2ClockOut));
  2309. /* Check that PLLSAI2 clock source and divider M can be applied */
  2310. if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_NONE)
  2311. {
  2312. /* PLL clock source and divider M already set, check that no request for change */
  2313. if((__HAL_RCC_GET_PLL_OSCSOURCE() != PllSai2->PLLSAI2Source)
  2314. ||
  2315. (PllSai2->PLLSAI2Source == RCC_PLLSOURCE_NONE)
  2316. ||
  2317. (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U) != PllSai2->PLLSAI2M)
  2318. )
  2319. {
  2320. status = HAL_ERROR;
  2321. }
  2322. }
  2323. else
  2324. {
  2325. /* Check PLLSAI2 clock source availability */
  2326. switch(PllSai2->PLLSAI2Source)
  2327. {
  2328. case RCC_PLLSOURCE_MSI:
  2329. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_MSIRDY))
  2330. {
  2331. status = HAL_ERROR;
  2332. }
  2333. break;
  2334. case RCC_PLLSOURCE_HSI:
  2335. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_HSIRDY))
  2336. {
  2337. status = HAL_ERROR;
  2338. }
  2339. break;
  2340. case RCC_PLLSOURCE_HSE:
  2341. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_HSERDY) && HAL_IS_BIT_CLR(RCC->CR, RCC_CR_HSEBYP))
  2342. {
  2343. status = HAL_ERROR;
  2344. }
  2345. break;
  2346. default:
  2347. status = HAL_ERROR;
  2348. break;
  2349. }
  2350. if(status == HAL_OK)
  2351. {
  2352. /* Set PLLSAI2 clock source and divider M */
  2353. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLM, PllSai2->PLLSAI2Source | (PllSai2->PLLSAI2M - 1U) << RCC_PLLCFGR_PLLM_Pos);
  2354. }
  2355. }
  2356. if(status == HAL_OK)
  2357. {
  2358. /* Disable the PLLSAI2 */
  2359. __HAL_RCC_PLLSAI2_DISABLE();
  2360. /* Get Start Tick*/
  2361. tickstart = HAL_GetTick();
  2362. /* Wait till PLLSAI2 is ready to be updated */
  2363. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) != RESET)
  2364. {
  2365. if((HAL_GetTick() - tickstart) > PLLSAI2_TIMEOUT_VALUE)
  2366. {
  2367. status = HAL_TIMEOUT;
  2368. break;
  2369. }
  2370. }
  2371. if(status == HAL_OK)
  2372. {
  2373. if(Divider == DIVIDER_P_UPDATE)
  2374. {
  2375. assert_param(IS_RCC_PLLSAI2P_VALUE(PllSai2->PLLSAI2P));
  2376. /* Configure the PLLSAI2 Division factor P and Multiplication factor N*/
  2377. MODIFY_REG(RCC->PLLSAI2CFGR,
  2378. RCC_PLLSAI2CFGR_PLLSAI2N | RCC_PLLSAI2CFGR_PLLSAI2P,
  2379. (PllSai2->PLLSAI2N << RCC_PLLSAI2CFGR_PLLSAI2N_Pos) |
  2380. ((PllSai2->PLLSAI2P >> 4U) << RCC_PLLSAI2CFGR_PLLSAI2P_Pos));
  2381. }
  2382. else
  2383. {
  2384. assert_param(IS_RCC_PLLSAI2R_VALUE(PllSai2->PLLSAI2R));
  2385. /* Configure the PLLSAI2 Division factor R and Multiplication factor N*/
  2386. MODIFY_REG(RCC->PLLSAI2CFGR,
  2387. RCC_PLLSAI2CFGR_PLLSAI2N | RCC_PLLSAI2CFGR_PLLSAI2R,
  2388. (PllSai2->PLLSAI2N << RCC_PLLSAI2CFGR_PLLSAI2N_Pos) |
  2389. (((PllSai2->PLLSAI2R >> 1U) - 1U) << RCC_PLLSAI2CFGR_PLLSAI2R_Pos));
  2390. }
  2391. /* Enable the PLLSAI2 again by setting PLLSAI2ON to 1*/
  2392. __HAL_RCC_PLLSAI2_ENABLE();
  2393. /* Get Start Tick*/
  2394. tickstart = HAL_GetTick();
  2395. /* Wait till PLLSAI2 is ready */
  2396. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) == RESET)
  2397. {
  2398. if((HAL_GetTick() - tickstart) > PLLSAI2_TIMEOUT_VALUE)
  2399. {
  2400. status = HAL_TIMEOUT;
  2401. break;
  2402. }
  2403. }
  2404. if(status == HAL_OK)
  2405. {
  2406. /* Configure the PLLSAI2 Clock output(s) */
  2407. __HAL_RCC_PLLSAI2CLKOUT_ENABLE(PllSai2->PLLSAI2ClockOut);
  2408. }
  2409. }
  2410. }
  2411. return status;
  2412. }
  2413. #endif /* RCC_PLLSAI2_SUPPORT */
  2414. /**
  2415. * @}
  2416. */
  2417. /**
  2418. * @}
  2419. */
  2420. #endif /* HAL_RCC_MODULE_ENABLED */
  2421. /**
  2422. * @}
  2423. */
  2424. /**
  2425. * @}
  2426. */
  2427. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/